Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Genesis block hash calculation for Testnet network #566

Merged
merged 3 commits into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
franklywatson marked this conversation as resolved.
Show resolved Hide resolved
"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
}
Loading