Skip to content

Commit

Permalink
Merge pull request #6325 from onflow/ramtin/evm-populate-genesis-bloc…
Browse files Browse the repository at this point in the history
…k-feilds

[Flow EVM] Populate all fields for the genesis block
  • Loading branch information
ramtinms authored Aug 13, 2024
2 parents 67d5beb + 20d111c commit 9e31160
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 49 deletions.
2 changes: 1 addition & 1 deletion fvm/evm/evm.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func SetupEnvironment(

backend := backends.NewWrappedEnvironment(fvmEnv)
emulator := evm.NewEmulator(backend, StorageAccountAddress(chainID))
blockStore := handler.NewBlockStore(backend, StorageAccountAddress(chainID))
blockStore := handler.NewBlockStore(chainID, backend, StorageAccountAddress(chainID))
addressAllocator := handler.NewAddressAllocator()

if tracer != debug.NopTracer {
Expand Down
26 changes: 19 additions & 7 deletions fvm/evm/handler/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ const (
)

type BlockStore struct {
chainID flow.ChainID
backend types.Backend
rootAddress flow.Address
}

var _ types.BlockStore = &BlockStore{}

// NewBlockStore constructs a new block store
func NewBlockStore(backend types.Backend, rootAddress flow.Address) *BlockStore {
func NewBlockStore(
chainID flow.ChainID,
backend types.Backend,
rootAddress flow.Address,
) *BlockStore {
return &BlockStore{
chainID: chainID,
backend: backend,
rootAddress: rootAddress,
}
Expand Down Expand Up @@ -145,7 +151,7 @@ func (bs *BlockStore) LatestBlock() (*types.Block, error) {
return nil, err
}
if len(data) == 0 {
return types.GenesisBlock, nil
return types.GenesisBlock(bs.chainID), nil
}
return types.NewBlockFromBytes(data)
}
Expand All @@ -171,10 +177,13 @@ func (bs *BlockStore) getBlockHashList() (*BlockHashList, error) {
// return nil, err
// }
// if bhl.IsEmpty() {
// err = bhl.Push(types.GenesisBlock.Height, types.GenesisBlockHash)
// if err != nil {
// return nil, err
// }
// err = bhl.Push(
// types.GenesisBlock(bs.chainID).Height,
// types.GenesisBlockHash(bs.chainID),
// )
// if err != nil {
// return nil, err
// }
// }
// return bhl, nil
}
Expand All @@ -192,7 +201,10 @@ func (bs *BlockStore) checkLegacyAndMigrate() (*BlockHashList, error) {
return nil, err
}
if bhl.IsEmpty() {
err = bhl.Push(types.GenesisBlock.Height, types.GenesisBlockHash)
err = bhl.Push(
types.GenesisBlock(bs.chainID).Height,
types.GenesisBlockHash(bs.chainID),
)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion fvm/evm/handler/blockstore_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func benchmarkBlockProposalGrowth(b *testing.B, txCounts int) {
testutils.RunWithTestBackend(b, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(b, backend, func(rootAddr flow.Address) {

bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(flow.Testnet, backend, rootAddr)
for i := 0; i < txCounts; i++ {
bp, err := bs.BlockProposal()
require.NoError(b, err)
Expand Down
23 changes: 13 additions & 10 deletions fvm/evm/handler/blockstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@ import (

func TestBlockStore(t *testing.T) {

var chainID = flow.Testnet
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(root flow.Address) {
bs := handler.NewBlockStore(backend, root)
bs := handler.NewBlockStore(chainID, backend, root)

// check the Genesis block
b, err := bs.LatestBlock()
require.NoError(t, err)
require.Equal(t, types.GenesisBlock, b)
require.Equal(t, types.GenesisBlock(chainID), b)
h, err := bs.BlockHash(0)
require.NoError(t, err)
require.Equal(t, types.GenesisBlockHash, h)
require.Equal(t, types.GenesisBlockHash(chainID), h)

// test block proposal construction from the Genesis block
bp, err := bs.BlockProposal()
require.NoError(t, err)
require.Equal(t, uint64(1), bp.Height)
expectedParentHash, err := types.GenesisBlock.Hash()
expectedParentHash, err := types.GenesisBlock(chainID).Hash()
require.NoError(t, err)
require.Equal(t, expectedParentHash, bp.ParentBlockHash)

Expand All @@ -47,7 +48,7 @@ func TestBlockStore(t *testing.T) {
require.NoError(t, err)

// reset the bs and check if it still return the block proposal
bs = handler.NewBlockStore(backend, root)
bs = handler.NewBlockStore(chainID, backend, root)
retbp, err = bs.BlockProposal()
require.NoError(t, err)
require.Equal(t, bp, retbp)
Expand All @@ -60,7 +61,7 @@ func TestBlockStore(t *testing.T) {
// this should still return the genesis block
retb, err := bs.LatestBlock()
require.NoError(t, err)
require.Equal(t, types.GenesisBlock, retb)
require.Equal(t, types.GenesisBlock(chainID), retb)

// commit the changes
err = bs.CommitBlockProposal(bp)
Expand All @@ -78,7 +79,7 @@ func TestBlockStore(t *testing.T) {
// genesis
h, err = bs.BlockHash(0)
require.NoError(t, err)
require.Equal(t, types.GenesisBlockHash, h)
require.Equal(t, types.GenesisBlockHash(chainID), h)

// block 1
h, err = bs.BlockHash(1)
Expand All @@ -99,6 +100,7 @@ func TestBlockStore(t *testing.T) {

// TODO: we can remove this when the previewnet is out
func TestBlockStoreMigration(t *testing.T) {
var chainID = flow.Testnet
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(root flow.Address) {
legacyCapacity := 16
Expand All @@ -114,7 +116,7 @@ func TestBlockStoreMigration(t *testing.T) {
legacy.Encode(),
)
require.NoError(t, err)
bs := handler.NewBlockStore(backend, root)
bs := handler.NewBlockStore(chainID, backend, root)

for i := 0; i <= maxHeightAdded-legacyCapacity; i++ {
h, err := bs.BlockHash(uint64(i))
Expand All @@ -138,10 +140,11 @@ func TestBlockStoreMigration(t *testing.T) {
// and storage of blocks works as it should, the breaking change was introduced
// in this PR https://github.com/onflow/flow-go/pull/5660
func TestBlockStore_AddedTimestamp(t *testing.T) {
var chainID = flow.Testnet
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(root flow.Address) {

bs := handler.NewBlockStore(backend, root)
bs := handler.NewBlockStore(chainID, backend, root)

// block type before breaking change (no timestamp and total gas used)
type oldBlockV1 struct {
Expand All @@ -154,7 +157,7 @@ func TestBlockStore_AddedTimestamp(t *testing.T) {
TransactionHashes []gethCommon.Hash
}

g := types.GenesisBlock
g := types.GenesisBlock(chainID)
h, err := g.Hash()
require.NoError(t, err)

Expand Down
36 changes: 19 additions & 17 deletions fvm/evm/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
var flowTokenAddress = common.MustBytesToAddress(systemcontracts.SystemContractsForChain(flow.Emulator).FlowToken.Address.Bytes())
var randomBeaconAddress = systemcontracts.SystemContractsForChain(flow.Emulator).RandomBeaconHistory.Address

const defaultChainID = flow.Testnet

func TestHandler_TransactionRunOrPanic(t *testing.T) {
t.Parallel()

Expand All @@ -47,7 +49,7 @@ func TestHandler_TransactionRunOrPanic(t *testing.T) {

sc := systemcontracts.SystemContractsForChain(flow.Emulator)

bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(defaultChainID, backend, rootAddr)

aa := handler.NewAddressAllocator()

Expand Down Expand Up @@ -125,7 +127,7 @@ func TestHandler_TransactionRunOrPanic(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {
bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(defaultChainID, backend, rootAddr)
aa := handler.NewAddressAllocator()
em := &testutils.TestEmulator{
RunTransactionFunc: func(tx *gethTypes.Transaction) (*types.Result, error) {
Expand Down Expand Up @@ -184,7 +186,7 @@ func TestHandler_TransactionRunOrPanic(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {
bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(defaultChainID, backend, rootAddr)
aa := handler.NewAddressAllocator()
em := &testutils.TestEmulator{
RunTransactionFunc: func(tx *gethTypes.Transaction) (*types.Result, error) {
Expand Down Expand Up @@ -278,7 +280,7 @@ func TestHandler_OpsWithoutEmulator(t *testing.T) {

// test call last executed block without initialization
b := handler.LastExecutedBlock()
require.Equal(t, types.GenesisBlock, b)
require.Equal(t, types.GenesisBlock(defaultChainID), b)

// do some changes
address := testutils.RandomAddress(t)
Expand Down Expand Up @@ -452,7 +454,7 @@ func TestHandler_COA(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {
bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(defaultChainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

// Withdraw calls are only possible within FOA accounts
Expand Down Expand Up @@ -530,7 +532,7 @@ func TestHandler_COA(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {
bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(defaultChainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

// test non fatal error of emulator
Expand Down Expand Up @@ -723,7 +725,7 @@ func TestHandler_TransactionRun(t *testing.T) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {

bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(chainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

result := &types.Result{
Expand Down Expand Up @@ -767,7 +769,7 @@ func TestHandler_TransactionRun(t *testing.T) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {

bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(chainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

result := &types.Result{
Expand Down Expand Up @@ -812,7 +814,7 @@ func TestHandler_TransactionRun(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {
bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(chainID, backend, rootAddr)
aa := handler.NewAddressAllocator()
evmErr := fmt.Errorf("%w: next nonce %v, tx nonce %v", gethCore.ErrNonceTooLow, 1, 0)
em := &testutils.TestEmulator{
Expand Down Expand Up @@ -864,7 +866,7 @@ func TestHandler_TransactionRun(t *testing.T) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {
sc := systemcontracts.SystemContractsForChain(chainID)

bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(chainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

gasConsumed := testutils.RandomGas(1000)
Expand Down Expand Up @@ -965,7 +967,7 @@ func TestHandler_TransactionRun(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {
bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(chainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

gasConsumed := testutils.RandomGas(1000)
Expand Down Expand Up @@ -1012,7 +1014,7 @@ func TestHandler_TransactionRun(t *testing.T) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {

bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(defaultChainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

nonce := uint64(1)
Expand Down Expand Up @@ -1104,7 +1106,7 @@ func TestHandler_TransactionRun(t *testing.T) {
require.NoError(t, err)
tracer.WithBlockID(blockID)

bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(chainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

em := &testutils.TestEmulator{
Expand Down Expand Up @@ -1174,7 +1176,7 @@ func TestHandler_TransactionRun(t *testing.T) {
require.NoError(t, err)
tracer.WithBlockID(flow.Identifier{0x1})

bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(defaultChainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

em := &testutils.TestEmulator{
Expand Down Expand Up @@ -1213,7 +1215,7 @@ func TestHandler_TransactionRun(t *testing.T) {
testutils.RunWithTestBackend(t, func(backend *testutils.TestBackend) {
testutils.RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) {
testutils.RunWithEOATestAccount(t, backend, rootAddr, func(eoa *testutils.EOATestAccount) {
bs := handler.NewBlockStore(backend, rootAddr)
bs := handler.NewBlockStore(defaultChainID, backend, rootAddr)
aa := handler.NewAddressAllocator()

const batchSize = 3
Expand Down Expand Up @@ -1375,7 +1377,7 @@ func TestHandler_Metrics(t *testing.T) {
rootAddr,
flowTokenAddress,
rootAddr,
handler.NewBlockStore(backend, rootAddr),
handler.NewBlockStore(defaultChainID, backend, rootAddr),
handler.NewAddressAllocator(),
backend,
em,
Expand Down Expand Up @@ -1467,7 +1469,7 @@ func SetupHandler(t testing.TB, backend types.Backend, rootAddr flow.Address) *h
rootAddr,
flowTokenAddress,
rootAddr,
handler.NewBlockStore(backend, rootAddr),
handler.NewBlockStore(defaultChainID, backend, rootAddr),
handler.NewAddressAllocator(),
backend,
emulator.NewEmulator(backend, rootAddr),
Expand Down
Loading

0 comments on commit 9e31160

Please sign in to comment.