diff --git a/storage/pebble/blocks.go b/storage/pebble/blocks.go index d4596681d..e7286596e 100644 --- a/storage/pebble/blocks.go +++ b/storage/pebble/blocks.go @@ -4,6 +4,7 @@ import ( "encoding/binary" "errors" "fmt" + "slices" "sync" "github.com/cockroachdb/pebble" @@ -17,6 +18,11 @@ import ( "github.com/onflow/flow-evm-gateway/storage" ) +var testnetBrokenParentHashBlockHeights = []uint64{ + 1, + 1385491, +} + var _ storage.BlockIndexer = &Blocks{} type Blocks struct { @@ -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 }