From d24d992cc4819395ac21e0af91a22e0f300c0a2d Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 11 Sep 2024 13:38:23 +0300 Subject: [PATCH 1/2] dbft: fix extensible sender check Sender must be presented in the list of validators, and sort.Search doesn't guarantee that. Signed-off-by: Anna Shaleva --- consensus/dbft/dbft.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/consensus/dbft/dbft.go b/consensus/dbft/dbft.go index 0effb69379..d88cfbbe82 100644 --- a/consensus/dbft/dbft.go +++ b/consensus/dbft/dbft.go @@ -1669,8 +1669,8 @@ func (c *DBFT) IsExtensibleAllowed(h uint64, u common.Address) error { if err != nil { return fmt.Errorf("failed to get validators: %w", err) } - n := sort.Search(len(validators), func(i int) bool { return validators[i].Cmp(u) >= 0 }) - if n >= len(validators) { + _, found := slices.BinarySearchFunc(validators, u, common.Address.Cmp) + if !found { return fmt.Errorf("address is not a validator") } return nil From 2eb049fbd9a52794e9fdcfb4da992811cd8c8759 Mon Sep 17 00:00:00 2001 From: Anna Shaleva Date: Wed, 11 Sep 2024 13:45:44 +0300 Subject: [PATCH 2/2] dbft: use slices package for sorting Signed-off-by: Anna Shaleva --- consensus/dbft/dbft.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/consensus/dbft/dbft.go b/consensus/dbft/dbft.go index d88cfbbe82..e0fcf17d4c 100644 --- a/consensus/dbft/dbft.go +++ b/consensus/dbft/dbft.go @@ -26,7 +26,6 @@ import ( "fmt" "io" "math/big" - "sort" "sync" "sync/atomic" "time" @@ -460,12 +459,8 @@ func New(config *params.DBFTConfig, _ ethdb.Database) (*DBFT, error) { return } - var sorted = make([]common.Hash, len(hashes)) - copy(sorted, hashes) - sort.Slice(sorted, func(i, j int) bool { - return sorted[i].Cmp(sorted[j]) < 0 - }) - + sorted := slices.Clone(hashes) + slices.SortFunc(sorted, common.Hash.Cmp) c.txCbList.Store(sorted) c.requestTxs(sorted) @@ -1621,11 +1616,8 @@ func (c *DBFT) OnTransaction(txs []*types.Transaction) { var cbList = c.txCbList.Load() if cbList != nil { for _, tx := range txs { - var list = cbList.([]common.Hash) - var i = sort.Search(len(list), func(i int) bool { - return list[i].Cmp(tx.Hash()) >= 0 - }) - if i < len(list) && list[i].Cmp(tx.Hash()) == 0 { + _, found := slices.BinarySearchFunc(cbList.([]common.Hash), tx.Hash(), common.Hash.Cmp) + if found { c.txs <- tx } }