Skip to content

Commit

Permalink
Handle broken chain due to ParentHash mismatches in testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Oct 2, 2024
1 parent e58d96f commit f14ad0c
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion storage/pebble/blocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/binary"
"errors"
"fmt"
"slices"
"sync"

"github.com/cockroachdb/pebble"
Expand All @@ -17,6 +18,11 @@ import (
"github.com/onflow/flow-evm-gateway/storage"
)

var testnetBrokenParentHashBlockHeights = []uint64{
1,
1385491,
}

var _ storage.BlockIndexer = &Blocks{}

type Blocks struct {
Expand Down Expand Up @@ -270,5 +276,25 @@ func (b *Blocks) getBlock(keyCode byte, key []byte) (*models.Block, error) {
return nil, fmt.Errorf("failed to get block: %w", err)
}

return models.NewBlockFromBytes(data)
block, err := models.NewBlockFromBytes(data)
if err != nil {
return nil, err
}

if b.chainID == flowGo.Testnet && slices.Contains(testnetBrokenParentHashBlockHeights, block.Height) {
parentBlock, err := b.getBlock(blockHeightKey, uint64Bytes(block.Height-1))
if err != nil {
return nil, err
}
// Due to the breaking change of the block hash calculation, after the
// introduction of the `PrevRandao` field, we need to manually set the
// `ParentBlockHash` field, for the 2 affected blocks on `testnet`
// network. `mainnet` was not affected by this.
block.ParentBlockHash, err = parentBlock.Hash()
if err != nil {
return nil, err
}
}

return block, nil
}

0 comments on commit f14ad0c

Please sign in to comment.