Skip to content

Commit

Permalink
check empty ids before querying validator set contract
Browse files Browse the repository at this point in the history
  • Loading branch information
lucca30 committed Jan 27, 2025
1 parent 24fe3cc commit a01c180
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion consensus/bor/heimdall/span/spanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ func (c *ChainSpanner) GetCurrentValidatorsByBlockNrOrHash(ctx context.Context,
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
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)
Expand Down
8 changes: 6 additions & 2 deletions consensus/bor/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"

"github.com/ethereum/go-ethereum/consensus/bor/valset"
"github.com/ethereum/go-ethereum/log"

lru "github.com/hashicorp/golang-lru"

Expand Down Expand Up @@ -159,8 +160,11 @@ func (s *Snapshot) apply(headers []*types.Header, c *Bor) (*Snapshot, error) {
v := getUpdatedValidatorSet(snap.ValidatorSet.Copy(), newVals)
v.IncrementProposerPriority(1)

valsWithId, _ := c.spanner.GetCurrentValidatorsByHash(context.Background(), header.Hash(), header.Number.Uint64())
v.IncludeIds(valsWithId)
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())
v.IncludeIds(valsWithId)
}
snap.ValidatorSet = v
}
}
Expand Down
12 changes: 12 additions & 0 deletions consensus/bor/valset/validator_set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
if vals.IsNilOrEmpty() {
panic("empty validator set")
Expand Down
56 changes: 56 additions & 0 deletions consensus/bor/valset/validator_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -257,3 +257,59 @@ func TestValidatorSet_IncludeIds_EmptySet(t *testing.T) {

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)
}
})
}
}

0 comments on commit a01c180

Please sign in to comment.