From 2dd5cbfb5c3e5c1cbad766baa052bb7d85c2c653 Mon Sep 17 00:00:00 2001 From: Callum Waters Date: Mon, 8 Feb 2021 17:36:21 +0100 Subject: [PATCH] light: remove witnesses in order of decreasing index (#6065) --- light/client.go | 11 +++++++++-- light/detector.go | 8 ++++++-- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/light/client.go b/light/client.go index 17c665b2b..c271c790e 100644 --- a/light/client.go +++ b/light/client.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "sort" "time" "github.com/tendermint/tendermint/libs/log" @@ -1002,6 +1003,9 @@ func (c *Client) compareFirstHeaderWithWitnesses(ctx context.Context, h *types.S compareCtx, cancel := context.WithCancel(ctx) defer cancel() + c.providerMutex.Lock() + defer c.providerMutex.Unlock() + if len(c.witnesses) < 1 { return errNoWitnesses{} } @@ -1035,8 +1039,11 @@ and remove witness. Otherwise, use the different primary`, e.WitnessIndex), "wit } } - for _, idx := range witnessesToRemove { - c.removeWitness(idx) + // we need to make sure that we remove witnesses by index in the reverse + // order so as to not affect the indexes themselves + sort.Ints(witnessesToRemove) + for i := len(witnessesToRemove) - 1; i >= 0; i-- { + c.removeWitness(witnessesToRemove[i]) } return nil diff --git a/light/detector.go b/light/detector.go index 612f186d3..b0e0a129a 100644 --- a/light/detector.go +++ b/light/detector.go @@ -5,6 +5,7 @@ import ( "context" "errors" "fmt" + "sort" "time" "github.com/tendermint/tendermint/light/provider" @@ -121,8 +122,11 @@ func (c *Client) detectDivergence(ctx context.Context, primaryTrace []*types.Lig } } - for _, idx := range witnessesToRemove { - c.removeWitness(idx) + // we need to make sure that we remove witnesses by index in the reverse + // order so as to not affect the indexes themselves + sort.Ints(witnessesToRemove) + for i := len(witnessesToRemove) - 1; i >= 0; i-- { + c.removeWitness(witnessesToRemove[i]) } // 1. If we had at least one witness that returned the same header then we