Skip to content

Commit

Permalink
Merge pull request ethereum#107 from maticnetwork/jdkanani/fix-use-hash
Browse files Browse the repository at this point in the history
fix: use hash instead of block number incase of reorg
  • Loading branch information
jdkanani authored Feb 16, 2021
2 parents 684b6b5 + 43e4177 commit 6f0f08d
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
30 changes: 18 additions & 12 deletions consensus/bor/bor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand Down Expand Up @@ -846,13 +846,16 @@ func (c *Bor) Close() error {
}

// GetCurrentSpan get current span from contract
func (c *Bor) GetCurrentSpan(snapshotNumber uint64) (*Span, error) {
func (c *Bor) GetCurrentSpan(headerHash common.Hash) (*Span, error) {
// block
blockNr := rpc.BlockNumber(snapshotNumber)
blockNr := rpc.BlockNumberOrHashWithHash(headerHash, false)

// 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)
Expand All @@ -862,11 +865,11 @@ func (c *Bor) GetCurrentSpan(snapshotNumber uint64) (*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,
}, rpc.BlockNumberOrHash{BlockNumber: &blockNr}, nil)
}, blockNr, nil)
if err != nil {
return nil, err
}
Expand All @@ -892,13 +895,16 @@ func (c *Bor) GetCurrentSpan(snapshotNumber uint64) (*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)
Expand All @@ -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
Expand Down Expand Up @@ -949,7 +955,7 @@ func (c *Bor) checkAndCommitSpan(
chain core.ChainContext,
) error {
headerNumber := header.Number.Uint64()
span, err := c.GetCurrentSpan(headerNumber - 1)
span, err := c.GetCurrentSpan(header.ParentHash)
if err != nil {
return err
}
Expand Down Expand Up @@ -1167,7 +1173,7 @@ func (c *Bor) getNextHeimdallSpanForTest(
chain core.ChainContext,
) (*HeimdallSpan, error) {
headerNumber := header.Number.Uint64()
span, err := c.GetCurrentSpan(headerNumber - 1)
span, err := c.GetCurrentSpan(header.ParentHash)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion tests/bor/bor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down

0 comments on commit 6f0f08d

Please sign in to comment.