Merge pull request #107 from maticnetwork/jdkanani/fix-use-hash

fix: use hash instead of block number incase of reorg
This commit is contained in:
Jaynti Kanani 2021-02-16 15:11:01 +05:30 committed by GitHub
commit 6f0f08daec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 13 deletions

View file

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

View file

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