From 1608484de85a3670aee7a81f8b84e0722c320d1f Mon Sep 17 00:00:00 2001 From: Callum Waters Date: Mon, 8 Feb 2021 12:34:02 +0100 Subject: [PATCH] remove witnesses in order of decreasing index (#6058) --- 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 0b0ee748e..13501e9fb 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" @@ -983,6 +984,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 } @@ -1016,8 +1020,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 743658f9c..a9510b9e3 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" @@ -127,8 +128,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