Skip to content

Commit

Permalink
Handle broken chain due to ParentHash mismatch in testnet
Browse files Browse the repository at this point in the history
  • Loading branch information
m-Peter committed Sep 23, 2024
1 parent 4d70b64 commit 2a4ca9c
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions models/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import (
"math/big"

"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/evm/events"
"github.com/onflow/flow-go/fvm/evm/types"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"
gethCommon "github.com/onflow/go-ethereum/common"
gethCrypto "github.com/onflow/go-ethereum/crypto"
Expand All @@ -18,6 +20,19 @@ var (
EarliestBlockNumber = big.NewInt(0)
)

var testnetEVMBlockExecutedEventID = common.NewAddressLocation(
nil,
common.Address(systemcontracts.SystemContractsForChain(flow.Testnet).EVMContract.Address),
string(events.EventTypeBlockExecuted),
).ID()

// Testnet block heights at which the block hash calculation had a breaking change.
// To properly handle this, we must assign the parent block hash, with a manual
// mapping.
var testnetParentHashMapping = map[uint64]gethCommon.Hash{
1385491: gethCommon.HexToHash("0xfc9a0204b12bc4875ed880300d07f27de4f9857be67d13cf72f53b09e8942ced"),
}

func GenesisBlock(chainID flow.ChainID) *Block {
block := types.GenesisBlock(chainID)
if chainID == flow.Testnet {
Expand Down Expand Up @@ -106,6 +121,14 @@ func decodeBlockEvent(event cadence.Event) (*Block, error) {
return nil, fmt.Errorf("failed to cadence decode block [%s]: %w", event.String(), err)
}

if event.EventType.ID() == testnetEVMBlockExecutedEventID {
for height, parentBlockHash := range testnetParentHashMapping {
if payload.Height == height {
payload.ParentBlockHash = parentBlockHash
}
}
}

return &Block{
Block: &types.Block{
ParentBlockHash: payload.ParentBlockHash,
Expand Down

0 comments on commit 2a4ca9c

Please sign in to comment.