diff --git a/consensus/bor/bor.go b/consensus/bor/bor.go index 556fbbec39f9f74c71354746fc089fa62a0049ef..635270ef2bae326b0a6ee636f5ab3fdfdb625cc4 100644 --- a/consensus/bor/bor.go +++ b/consensus/bor/bor.go @@ -455,7 +455,7 @@ func (c *Bor) snapshot(chain consensus.ChainHeaderReader, number uint64, hash co hash := checkpoint.Hash() // get validators and current span - validators, err := c.GetCurrentValidators(number, number+1) + validators, err := c.GetCurrentValidators(hash, number+1) if err != nil { return nil, err } @@ -609,7 +609,7 @@ func (c *Bor) Prepare(chain consensus.ChainHeaderReader, header *types.Header) e // get validator set if number if (number+1)%c.config.Sprint == 0 { - newValidators, err := c.GetCurrentValidators(snap.Number, number+1) + newValidators, err := c.GetCurrentValidators(header.ParentHash, number+1) if err != nil { return errors.New("unknown validators") } @@ -853,6 +853,9 @@ func (c *Bor) GetCurrentSpan(headerHash common.Hash) (*Span, error) { // method method := "getCurrentSpan" + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + data, err := c.validatorSetABI.Pack(method) if err != nil { log.Error("Unable to pack tx for getCurrentSpan", "error", err) @@ -862,7 +865,7 @@ func (c *Bor) GetCurrentSpan(headerHash common.Hash) (*Span, error) { msgData := (hexutil.Bytes)(data) toAddress := common.HexToAddress(c.config.ValidatorContract) gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) - result, err := c.ethAPI.Call(context.Background(), ethapi.CallArgs{ + result, err := c.ethAPI.Call(ctx, ethapi.CallArgs{ Gas: &gas, To: &toAddress, Data: &msgData, @@ -892,13 +895,16 @@ func (c *Bor) GetCurrentSpan(headerHash common.Hash) (*Span, error) { } // GetCurrentValidators get current validators -func (c *Bor) GetCurrentValidators(snapshotNumber uint64, blockNumber uint64) ([]*Validator, error) { +func (c *Bor) GetCurrentValidators(headerHash common.Hash, blockNumber uint64) ([]*Validator, error) { // block - blockNr := rpc.BlockNumber(snapshotNumber) + blockNr := rpc.BlockNumberOrHashWithHash(headerHash, false) // method method := "getBorValidators" + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + data, err := c.validatorSetABI.Pack(method, big.NewInt(0).SetUint64(blockNumber)) if err != nil { log.Error("Unable to pack tx for getValidator", "error", err) @@ -909,11 +915,11 @@ func (c *Bor) GetCurrentValidators(snapshotNumber uint64, blockNumber uint64) ([ msgData := (hexutil.Bytes)(data) toAddress := common.HexToAddress(c.config.ValidatorContract) gas := (hexutil.Uint64)(uint64(math.MaxUint64 / 2)) - result, err := c.ethAPI.Call(context.Background(), ethapi.CallArgs{ + result, err := c.ethAPI.Call(ctx, ethapi.CallArgs{ Gas: &gas, To: &toAddress, Data: &msgData, - }, rpc.BlockNumberOrHash{BlockNumber: &blockNr}, nil) + }, blockNr, nil) if err != nil { panic(err) // return nil, err diff --git a/tests/bor/bor_test.go b/tests/bor/bor_test.go index 4270e41999a505dafa793721b88401fd2bc64c39..979a8dc0443455678caabd3cfc9de80feb416a1c 100644 --- a/tests/bor/bor_test.go +++ b/tests/bor/bor_test.go @@ -42,7 +42,7 @@ func TestInsertingSpanSizeBlocks(t *testing.T) { } assert.True(t, h.AssertCalled(t, "FetchWithRetry", spanPath, "")) - validators, err := _bor.GetCurrentValidators(sprintSize, spanSize) // check validator set at the first block of new span + validators, err := _bor.GetCurrentValidators(block.Hash(), spanSize) // check validator set at the first block of new span if err != nil { t.Fatalf("%s", err) }