check empty ids before querying validator set contract

This commit is contained in:
Lucca Martins 2025-01-27 09:13:35 -03:00
parent 24fe3cc8fc
commit a01c180246
No known key found for this signature in database
GPG key ID: DC3D7F76BDAE23BF
4 changed files with 75 additions and 3 deletions

View file

@ -111,7 +111,7 @@ func (c *ChainSpanner) GetCurrentValidatorsByBlockNrOrHash(ctx context.Context,
return valz, nil return valz, nil
} }
// Try to get bor validators with Id from ValidatorSet contract by querying each element on mapping(uint256 => Validator[]) public producers // tryGetBorValidatorsWithId Try to get bor validators with Id from ValidatorSet contract by querying each element on mapping(uint256 => Validator[]) public producers
// If fails then returns GetBorValidators without id // If fails then returns GetBorValidators without id
func (c *ChainSpanner) tryGetBorValidatorsWithId(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, blockNumber uint64, toAddress common.Address, gas hexutil.Uint64) ([]*valset.Validator, error) { func (c *ChainSpanner) tryGetBorValidatorsWithId(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, blockNumber uint64, toAddress common.Address, gas hexutil.Uint64) ([]*valset.Validator, error) {
firstEndBlock, err := c.getFirstEndBlock(ctx, blockNrOrHash, toAddress, gas) firstEndBlock, err := c.getFirstEndBlock(ctx, blockNrOrHash, toAddress, gas)

View file

@ -5,6 +5,7 @@ import (
"encoding/json" "encoding/json"
"github.com/ethereum/go-ethereum/consensus/bor/valset" "github.com/ethereum/go-ethereum/consensus/bor/valset"
"github.com/ethereum/go-ethereum/log"
lru "github.com/hashicorp/golang-lru" lru "github.com/hashicorp/golang-lru"
@ -159,8 +160,11 @@ func (s *Snapshot) apply(headers []*types.Header, c *Bor) (*Snapshot, error) {
v := getUpdatedValidatorSet(snap.ValidatorSet.Copy(), newVals) v := getUpdatedValidatorSet(snap.ValidatorSet.Copy(), newVals)
v.IncrementProposerPriority(1) v.IncrementProposerPriority(1)
if v.CheckEmptyId() {
log.Warn("Empty id found on validator set. Querying on the validatorSet contract")
valsWithId, _ := c.spanner.GetCurrentValidatorsByHash(context.Background(), header.Hash(), header.Number.Uint64()) valsWithId, _ := c.spanner.GetCurrentValidatorsByHash(context.Background(), header.Hash(), header.Number.Uint64())
v.IncludeIds(valsWithId) v.IncludeIds(valsWithId)
}
snap.ValidatorSet = v snap.ValidatorSet = v
} }
} }

View file

@ -128,6 +128,18 @@ func (vals *ValidatorSet) IncludeIds(valsWithId []*Validator) {
} }
} }
// CheckEmptyId checks if any validator in the ValidatorSet has an empty ID (ID == 0).
// Returns true if at least one validator has an empty ID.
// Returns false if all validators have non-zero IDs or if the ValidatorSet is empty.
func (vals *ValidatorSet) CheckEmptyId() bool {
for _, val := range vals.Validators {
if val.ID == 0 {
return true
}
}
return false
}
func (vals *ValidatorSet) RescalePriorities(diffMax int64) { func (vals *ValidatorSet) RescalePriorities(diffMax int64) {
if vals.IsNilOrEmpty() { if vals.IsNilOrEmpty() {
panic("empty validator set") panic("empty validator set")

View file

@ -257,3 +257,59 @@ func TestValidatorSet_IncludeIds_EmptySet(t *testing.T) {
assert.Equal(t, 0, valSet.Size(), "ValidatorSet remains empty") assert.Equal(t, 0, valSet.Size(), "ValidatorSet remains empty")
} }
func TestCheckEmptyId(t *testing.T) {
tests := []struct {
name string
validatorSet ValidatorSet
expected bool
}{
{
name: "Empty ValidatorSet",
validatorSet: ValidatorSet{Validators: []*Validator{}},
expected: false,
},
{
name: "All Validators with Non-Zero IDs",
validatorSet: ValidatorSet{
Validators: []*Validator{
{ID: 1},
{ID: 2},
{ID: 3},
},
},
expected: false,
},
{
name: "One Validator with ID Zero",
validatorSet: ValidatorSet{
Validators: []*Validator{
{ID: 0},
{ID: 2},
{ID: 3},
},
},
expected: true,
},
{
name: "All Validators with ID Zero",
validatorSet: ValidatorSet{
Validators: []*Validator{
{ID: 0},
{ID: 0},
{ID: 0},
},
},
expected: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.validatorSet.CheckEmptyId()
if result != tt.expected {
t.Errorf("expected %v, got %v", tt.expected, result)
}
})
}
}