From fc1ab4d36f2adef71d76db9f20c0f5e45ff804b1 Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:54:02 +0200 Subject: [PATCH 1/8] remove previous block formats --- fvm/evm/types/block.go | 174 ++--------------------------------------- 1 file changed, 6 insertions(+), 168 deletions(-) diff --git a/fvm/evm/types/block.go b/fvm/evm/types/block.go index 0cd6410b7af..3b13b3e06ab 100644 --- a/fvm/evm/types/block.go +++ b/fvm/evm/types/block.go @@ -215,176 +215,14 @@ func NewBlockProposal( type TransactionHashes []gethCommon.Hash -func (th TransactionHashes) Len() int { - return len(th) +func (t TransactionHashes) Len() int { + return len(t) } -func (th TransactionHashes) EncodeIndex(index int, buffer *bytes.Buffer) { - buffer.Write(th[index].Bytes()) +func (t TransactionHashes) EncodeIndex(index int, buffer *bytes.Buffer) { + buffer.Write(t[index].Bytes()) } -func (txs TransactionHashes) RootHash() gethCommon.Hash { - return gethTypes.DeriveSha(txs, gethTrie.NewStackTrie(nil)) -} - -// todo remove this if confirmed we no longer need it on testnet, mainnet and previewnet. - -// Below block type section, defines earlier block types, -// this is being used to decode blocks that were stored -// before block type changes. It allows us to still decode -// a block that would otherwise be invalid if decoded into -// latest version of the above Block type. - -type blockV0 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - UUIDIndex uint64 - TotalSupply uint64 - StateRoot gethCommon.Hash - ReceiptRoot gethCommon.Hash -} - -// adds TransactionHashes - -type blockV1 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - UUIDIndex uint64 - TotalSupply uint64 - StateRoot gethCommon.Hash - ReceiptRoot gethCommon.Hash - TransactionHashes []gethCommon.Hash -} - -// removes UUIDIndex - -type blockV2 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - TotalSupply uint64 - StateRoot gethCommon.Hash - ReceiptRoot gethCommon.Hash - TransactionHashes []gethCommon.Hash -} - -// removes state root - -type blockV3 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - TotalSupply uint64 - ReceiptRoot gethCommon.Hash - TransactionHashes []gethCommon.Hash -} - -// change total supply type - -type blockV4 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - TotalSupply *big.Int - ReceiptRoot gethCommon.Hash - TransactionHashes []gethCommon.Hash -} - -// adds timestamp - -type blockV5 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - Timestamp uint64 - TotalSupply *big.Int - ReceiptRoot gethCommon.Hash - TransactionHashes []gethCommon.Hash -} - -// adds total gas used - -type blockV6 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - Timestamp uint64 - TotalSupply *big.Int - ReceiptRoot gethCommon.Hash - TransactionHashes []gethCommon.Hash - TotalGasUsed uint64 -} - -// decodeBlockBreakingChanges will try to decode the bytes into all -// previous versions of block type, if it succeeds it will return the -// migrated block, otherwise it will return nil. -func decodeBlockBreakingChanges(encoded []byte) *Block { - b0 := &blockV0{} - if err := gethRLP.DecodeBytes(encoded, b0); err == nil { - return &Block{ - ParentBlockHash: b0.ParentBlockHash, - Height: b0.Height, - ReceiptRoot: b0.ReceiptRoot, - TotalSupply: big.NewInt(int64(b0.TotalSupply)), - } - } - - b1 := &blockV1{} - if err := gethRLP.DecodeBytes(encoded, b1); err == nil { - return &Block{ - ParentBlockHash: b1.ParentBlockHash, - Height: b1.Height, - TotalSupply: big.NewInt(int64(b1.TotalSupply)), - ReceiptRoot: b1.ReceiptRoot, - } - } - - b2 := &blockV2{} - if err := gethRLP.DecodeBytes(encoded, b2); err == nil { - return &Block{ - ParentBlockHash: b2.ParentBlockHash, - Height: b2.Height, - TotalSupply: big.NewInt(int64(b2.TotalSupply)), - ReceiptRoot: b2.ReceiptRoot, - } - } - - b3 := &blockV3{} - if err := gethRLP.DecodeBytes(encoded, b3); err == nil { - return &Block{ - ParentBlockHash: b3.ParentBlockHash, - Height: b3.Height, - TotalSupply: big.NewInt(int64(b3.TotalSupply)), - ReceiptRoot: b3.ReceiptRoot, - } - } - - b4 := &blockV4{} - if err := gethRLP.DecodeBytes(encoded, b4); err == nil { - return &Block{ - ParentBlockHash: b4.ParentBlockHash, - Height: b4.Height, - TotalSupply: b4.TotalSupply, - ReceiptRoot: b4.ReceiptRoot, - } - } - - b5 := &blockV5{} - if err := gethRLP.DecodeBytes(encoded, b5); err == nil { - return &Block{ - ParentBlockHash: b5.ParentBlockHash, - Height: b5.Height, - Timestamp: b5.Timestamp, - TotalSupply: b5.TotalSupply, - ReceiptRoot: b5.ReceiptRoot, - } - } - - b6 := &blockV6{} - if err := gethRLP.DecodeBytes(encoded, b6); err == nil { - return &Block{ - ParentBlockHash: b5.ParentBlockHash, - Height: b5.Height, - Timestamp: b5.Timestamp, - TotalSupply: b5.TotalSupply, - ReceiptRoot: b5.ReceiptRoot, - } - } - - return nil +func (t TransactionHashes) RootHash() gethCommon.Hash { + return gethTypes.DeriveSha(t, gethTrie.NewStackTrie(nil)) } From 330f8e8df2a092e21053ca4f4c5ee566c3418edc Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:54:57 +0200 Subject: [PATCH 2/8] update block from bytes --- fvm/evm/types/block.go | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/fvm/evm/types/block.go b/fvm/evm/types/block.go index 3b13b3e06ab..1ca51b03795 100644 --- a/fvm/evm/types/block.go +++ b/fvm/evm/types/block.go @@ -79,15 +79,7 @@ func NewBlock( // NewBlockFromBytes constructs a new block from encoded data func NewBlockFromBytes(encoded []byte) (*Block, error) { res := &Block{} - - err := gethRLP.DecodeBytes(encoded, res) - if err != nil { - res = decodeBlockBreakingChanges(encoded) - if res == nil { - return nil, err - } - } - return res, nil + return res, gethRLP.DecodeBytes(encoded, res) } // GenesisTimestamp returns the block time stamp for EVM genesis block From 9f17c280b4281d5f577cdbd38889d6731543fdfd Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:56:40 +0200 Subject: [PATCH 3/8] remove previewnet migration test --- fvm/evm/handler/blockstore_test.go | 36 --------- fvm/evm/types/block_test.go | 121 ----------------------------- 2 files changed, 157 deletions(-) diff --git a/fvm/evm/handler/blockstore_test.go b/fvm/evm/handler/blockstore_test.go index bb8c9a0deac..482ff553edc 100644 --- a/fvm/evm/handler/blockstore_test.go +++ b/fvm/evm/handler/blockstore_test.go @@ -98,42 +98,6 @@ 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 - maxHeightAdded := 32 - legacy := types.NewBlockHashList(16) - for i := 0; i <= maxHeightAdded; i++ { - err := legacy.Push(uint64(i), gethCommon.Hash{byte(i)}) - require.NoError(t, err) - } - err := backend.SetValue( - root[:], - []byte(handler.BlockStoreBlockHashesKey), - legacy.Encode(), - ) - require.NoError(t, err) - bs := handler.NewBlockStore(chainID, backend, root) - - for i := 0; i <= maxHeightAdded-legacyCapacity; i++ { - h, err := bs.BlockHash(uint64(i)) - require.NoError(t, err) - require.Equal(t, gethCommon.Hash{}, h) - } - - for i := maxHeightAdded - legacyCapacity + 1; i <= maxHeightAdded; i++ { - h, err := bs.BlockHash(uint64(i)) - require.NoError(t, err) - require.Equal(t, gethCommon.Hash{byte(i)}, h) - } - }) - }) - -} - // This test reproduces a state before a breaking change on the Block type, // which added a timestamp and total gas used, // then it adds new blocks and makes sure the retrival diff --git a/fvm/evm/types/block_test.go b/fvm/evm/types/block_test.go index 3d95568b5cd..aa9ee1d260c 100644 --- a/fvm/evm/types/block_test.go +++ b/fvm/evm/types/block_test.go @@ -6,7 +6,6 @@ import ( gethCommon "github.com/onflow/go-ethereum/common" gethTypes "github.com/onflow/go-ethereum/core/types" - gethRLP "github.com/onflow/go-ethereum/rlp" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -77,123 +76,3 @@ func Test_BlockProposal(t *testing.T) { bp.PopulateRoots() require.NotEqual(t, gethTypes.EmptyReceiptsHash, bp.ReceiptRoot) } - -func Test_DecodeBlocks(t *testing.T) { - bv0 := blockV0{ - ParentBlockHash: GenesisBlockHash(flow.Previewnet.Chain().ChainID()), - Height: 1, - UUIDIndex: 2, - TotalSupply: 3, - StateRoot: gethCommon.Hash{0x01}, - ReceiptRoot: gethCommon.Hash{0x02}, - } - b0, err := gethRLP.EncodeToBytes(bv0) - require.NoError(t, err) - - b := decodeBlockBreakingChanges(b0) - - require.Equal(t, b.TotalSupply.Uint64(), bv0.TotalSupply) - require.Equal(t, b.Height, bv0.Height) - require.Equal(t, b.ParentBlockHash, bv0.ParentBlockHash) - require.Empty(t, b.Timestamp) - require.Empty(t, b.TotalGasUsed) - - bv1 := blockV1{ - ParentBlockHash: GenesisBlockHash(flow.Previewnet.Chain().ChainID()), - Height: 1, - UUIDIndex: 2, - TotalSupply: 3, - StateRoot: gethCommon.Hash{0x01}, - ReceiptRoot: gethCommon.Hash{0x02}, - TransactionHashes: []gethCommon.Hash{{0x04}}, - } - - b1, err := gethRLP.EncodeToBytes(bv1) - require.NoError(t, err) - - b = decodeBlockBreakingChanges(b1) - - require.Equal(t, b.TotalSupply.Uint64(), bv1.TotalSupply) - require.Equal(t, b.Height, bv1.Height) - require.Equal(t, b.ParentBlockHash, bv1.ParentBlockHash) - require.Empty(t, b.Timestamp) - require.Empty(t, b.TotalGasUsed) - - bv2 := blockV2{ - ParentBlockHash: GenesisBlockHash(flow.Previewnet.Chain().ChainID()), - Height: 1, - TotalSupply: 2, - StateRoot: gethCommon.Hash{0x01}, - ReceiptRoot: gethCommon.Hash{0x02}, - TransactionHashes: []gethCommon.Hash{{0x04}}, - } - - b2, err := gethRLP.EncodeToBytes(bv2) - require.NoError(t, err) - - b = decodeBlockBreakingChanges(b2) - - require.Equal(t, b.TotalSupply.Uint64(), bv2.TotalSupply) - require.Equal(t, b.Height, bv2.Height) - require.Equal(t, b.ParentBlockHash, bv2.ParentBlockHash) - require.Empty(t, b.Timestamp) - require.Empty(t, b.TotalGasUsed) - - bv3 := blockV3{ - ParentBlockHash: GenesisBlockHash(flow.Previewnet.Chain().ChainID()), - Height: 1, - TotalSupply: 2, - ReceiptRoot: gethCommon.Hash{0x02}, - TransactionHashes: []gethCommon.Hash{{0x04}}, - } - - b3, err := gethRLP.EncodeToBytes(bv3) - require.NoError(t, err) - - b = decodeBlockBreakingChanges(b3) - - require.Equal(t, b.TotalSupply.Uint64(), bv3.TotalSupply) - require.Equal(t, b.Height, bv3.Height) - require.Equal(t, b.ParentBlockHash, bv3.ParentBlockHash) - require.Empty(t, b.Timestamp) - require.Empty(t, b.TotalGasUsed) - - bv4 := blockV4{ - ParentBlockHash: GenesisBlockHash(flow.Previewnet.Chain().ChainID()), - Height: 1, - TotalSupply: big.NewInt(4), - ReceiptRoot: gethCommon.Hash{0x02}, - TransactionHashes: []gethCommon.Hash{{0x04}}, - } - - b4, err := gethRLP.EncodeToBytes(bv4) - require.NoError(t, err) - - b = decodeBlockBreakingChanges(b4) - - require.Equal(t, b.TotalSupply, bv4.TotalSupply) - require.Equal(t, b.Height, bv4.Height) - require.Equal(t, b.ParentBlockHash, bv4.ParentBlockHash) - require.Empty(t, b.Timestamp) - require.Empty(t, b.TotalGasUsed) - - bv5 := blockV5{ - ParentBlockHash: GenesisBlockHash(flow.Previewnet.Chain().ChainID()), - Height: 1, - TotalSupply: big.NewInt(2), - ReceiptRoot: gethCommon.Hash{0x02}, - TransactionHashes: []gethCommon.Hash{{0x04}}, - Timestamp: 100, - } - - b5, err := gethRLP.EncodeToBytes(bv5) - require.NoError(t, err) - - b = decodeBlockBreakingChanges(b5) - - require.Equal(t, b.Timestamp, bv5.Timestamp) - require.Equal(t, b.TotalSupply, bv5.TotalSupply) - require.Equal(t, b.Height, bv5.Height) - require.Equal(t, b.ParentBlockHash, bv5.ParentBlockHash) - require.Empty(t, b.TotalGasUsed) -} From 8dc6c0b694eeac587fc87232d8dbdacadbe518be Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Mon, 19 Aug 2024 15:58:37 +0200 Subject: [PATCH 4/8] remove block hashlist --- fvm/evm/types/blockHashList.go | 175 ---------------------------- fvm/evm/types/blockHashList_test.go | 60 ---------- 2 files changed, 235 deletions(-) delete mode 100644 fvm/evm/types/blockHashList.go delete mode 100644 fvm/evm/types/blockHashList_test.go diff --git a/fvm/evm/types/blockHashList.go b/fvm/evm/types/blockHashList.go deleted file mode 100644 index 3a1b5756768..00000000000 --- a/fvm/evm/types/blockHashList.go +++ /dev/null @@ -1,175 +0,0 @@ -package types - -import ( - "encoding/binary" - "fmt" - - gethCommon "github.com/onflow/go-ethereum/common" -) - -const ( - capacityEncodingSize = 8 - tailEncodingSize = 8 - countEncodingSize = 8 - heightEncodingSize = 8 - hashEncodingSize = 32 - minEncodedByteSize = capacityEncodingSize + - tailEncodingSize + - countEncodingSize + - heightEncodingSize -) - -// BlockHashList holds the last `capacity` number of block hashes in the list -// TODO: Clean up, BlockHashList would become deprecated when previewnet is out. -type BlockHashList struct { - blocks []gethCommon.Hash - capacity int - tail int // element index to write to - count int // number of elements (count <= capacity) - height uint64 // keeps the height of last added block -} - -// NewBlockHashList constructs a new block hash list of the given capacity -func NewBlockHashList(capacity int) *BlockHashList { - return &BlockHashList{ - blocks: make([]gethCommon.Hash, capacity), - capacity: capacity, - tail: 0, - count: 0, - height: 0, - } -} - -// Push pushes a block hash for the next height to the list. -// If the list is full, it overwrites the oldest element. -func (bhl *BlockHashList) Push(height uint64, bh gethCommon.Hash) error { - if bhl.IsEmpty() && height != 0 { - return fmt.Errorf("out of the order block hash, expected: 0, got: %d", height) - } - if !bhl.IsEmpty() && height != bhl.height+1 { - return fmt.Errorf("out of the order block hash, expected: %d, got: %d", bhl.height+1, height) - } - bhl.blocks[bhl.tail] = bh - bhl.tail = (bhl.tail + 1) % bhl.capacity - bhl.height = height - if bhl.count != bhl.capacity { - bhl.count++ - } - return nil -} - -// IsEmpty returns true if the list is empty -func (bhl *BlockHashList) IsEmpty() bool { - return bhl.count == 0 -} - -// LastAddedBlockHash returns the last block hash added to the list -// for empty list it returns empty hash value -func (bhl *BlockHashList) LastAddedBlockHash() gethCommon.Hash { - if bhl.count == 0 { - // return empty hash - return gethCommon.Hash{} - } - indx := bhl.tail - 1 - if indx < 0 { - indx = bhl.capacity - 1 - } - return bhl.blocks[indx] -} - -// MinAvailableHeight returns the min available height in the list -func (bhl *BlockHashList) MinAvailableHeight() uint64 { - return bhl.height - (uint64(bhl.count) - 1) -} - -// MaxAvailableHeight returns the max available height in the list -func (bhl *BlockHashList) MaxAvailableHeight() uint64 { - return bhl.height -} - -// BlockHashByIndex returns the block hash by block height -func (bhl *BlockHashList) BlockHashByHeight(height uint64) (found bool, bh gethCommon.Hash) { - if bhl.count == 0 || // empty - height > bhl.height || // height too high - height < bhl.MinAvailableHeight() { // height too low - return false, gethCommon.Hash{} - } - - diff := bhl.height - height - indx := bhl.tail - int(diff) - 1 - if indx < 0 { - indx = bhl.capacity + indx - } - return true, bhl.blocks[indx] -} - -func (bhl *BlockHashList) Encode() []byte { - encodedByteSize := capacityEncodingSize + - tailEncodingSize + - countEncodingSize + - heightEncodingSize + - len(bhl.blocks)*hashEncodingSize - - buffer := make([]byte, encodedByteSize) - pos := 0 - - // encode capacity - binary.BigEndian.PutUint64(buffer[pos:], uint64(bhl.capacity)) - pos += capacityEncodingSize - - // encode tail - binary.BigEndian.PutUint64(buffer[pos:], uint64(bhl.tail)) - pos += tailEncodingSize - - // encode count - binary.BigEndian.PutUint64(buffer[pos:], uint64(bhl.count)) - pos += countEncodingSize - - // encode height - binary.BigEndian.PutUint64(buffer[pos:], uint64(bhl.height)) - pos += heightEncodingSize - - // encode hashes - for i := 0; i < bhl.count; i++ { - copy(buffer[pos:pos+hashEncodingSize], bhl.blocks[i][:]) - pos += hashEncodingSize - } - return buffer -} - -func NewBlockHashListFromEncoded(encoded []byte) (*BlockHashList, error) { - if len(encoded) < minEncodedByteSize { - return nil, fmt.Errorf("encoded input too short: %d < %d", len(encoded), minEncodedByteSize) - } - - pos := 0 - // decode capacity - capacity := binary.BigEndian.Uint64(encoded[pos:]) - pos += capacityEncodingSize - - // create bhl - bhl := NewBlockHashList(int(capacity)) - - // decode tail - bhl.tail = int(binary.BigEndian.Uint64(encoded[pos:])) - pos += tailEncodingSize - - // decode count - bhl.count = int(binary.BigEndian.Uint64(encoded[pos:])) - pos += countEncodingSize - - // decode height - bhl.height = binary.BigEndian.Uint64(encoded[pos:]) - pos += heightEncodingSize - - // decode hashes - if len(encoded[pos:]) < bhl.count*hashEncodingSize { - return nil, fmt.Errorf("encoded input too short: %d < %d", len(encoded), minEncodedByteSize) - } - for i := 0; i < bhl.count; i++ { - bhl.blocks[i] = gethCommon.BytesToHash(encoded[pos : pos+hashEncodingSize]) - pos += hashEncodingSize - } - - return bhl, nil -} diff --git a/fvm/evm/types/blockHashList_test.go b/fvm/evm/types/blockHashList_test.go deleted file mode 100644 index 5d4ca70a6c6..00000000000 --- a/fvm/evm/types/blockHashList_test.go +++ /dev/null @@ -1,60 +0,0 @@ -package types_test - -import ( - "testing" - - gethCommon "github.com/onflow/go-ethereum/common" - "github.com/stretchr/testify/require" - - "github.com/onflow/flow-go/fvm/evm/types" -) - -func TestBlockHashList(t *testing.T) { - - capacity := 5 - bhl := types.NewBlockHashList(capacity) - require.True(t, bhl.IsEmpty()) - - require.Equal(t, gethCommon.Hash{}, bhl.LastAddedBlockHash()) - - found, h := bhl.BlockHashByHeight(0) - require.False(t, found) - require.Equal(t, gethCommon.Hash{}, h) - - // first full range - for i := 0; i < capacity; i++ { - err := bhl.Push(uint64(i), gethCommon.Hash{byte(i)}) - require.NoError(t, err) - require.Equal(t, uint64(0), bhl.MinAvailableHeight()) - require.Equal(t, uint64(i), bhl.MaxAvailableHeight()) - } - for i := 0; i < capacity; i++ { - found, h := bhl.BlockHashByHeight(uint64(i)) - require.True(t, found) - require.Equal(t, gethCommon.Hash{byte(i)}, h) - } - require.Equal(t, gethCommon.Hash{byte(capacity - 1)}, bhl.LastAddedBlockHash()) - - // over border range - for i := capacity; i < capacity+3; i++ { - err := bhl.Push(uint64(i), gethCommon.Hash{byte(i)}) - require.NoError(t, err) - require.Equal(t, uint64(i-capacity+1), bhl.MinAvailableHeight()) - require.Equal(t, uint64(i), bhl.MaxAvailableHeight()) - } - for i := 0; i < capacity-2; i++ { - found, _ := bhl.BlockHashByHeight(uint64(i)) - require.False(t, found) - } - for i := capacity - 2; i < capacity+3; i++ { - found, h := bhl.BlockHashByHeight(uint64(i)) - require.True(t, found) - require.Equal(t, gethCommon.Hash{byte(i)}, h) - } - require.Equal(t, gethCommon.Hash{byte(capacity + 2)}, bhl.LastAddedBlockHash()) - - encoded := bhl.Encode() - bhl2, err := types.NewBlockHashListFromEncoded(encoded) - require.NoError(t, err) - require.Equal(t, bhl, bhl2) -} From f00e65802417f631826ea4fce1852b0327c2b5bd Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:02:12 +0200 Subject: [PATCH 5/8] remove decoding legacy account --- fvm/evm/emulator/state/account.go | 46 +------------------------------ 1 file changed, 1 insertion(+), 45 deletions(-) diff --git a/fvm/evm/emulator/state/account.go b/fvm/evm/emulator/state/account.go index 4ed0180e36f..0b1077e1084 100644 --- a/fvm/evm/emulator/state/account.go +++ b/fvm/evm/emulator/state/account.go @@ -1,8 +1,6 @@ package state import ( - "math/big" - "github.com/holiman/uint256" gethCommon "github.com/onflow/go-ethereum/common" gethTypes "github.com/onflow/go-ethereum/core/types" @@ -59,47 +57,5 @@ func DecodeAccount(inp []byte) (*Account, error) { return nil, nil } a := &Account{} - // try decoding - err := rlp.DecodeBytes(inp, a) - if err != nil { - // try legacy decoding (fall back) - a = decodeLegacy(inp) - // if no success return the initial error - if a == nil { - return nil, err - } - } - return a, nil - -} - -// TODO: remove it when the Preview is shut down -// Legacy account type - used big.Int for balances -type accountV0 struct { - // address - Address gethCommon.Address - // balance of the address - Balance *big.Int - // nonce of the address - Nonce uint64 - // hash of the code - // if no code the gethTypes.EmptyCodeHash is stored - CodeHash gethCommon.Hash - // the id of the collection holds storage slots for this account - // this value is nil for EOA accounts - CollectionID []byte -} - -func decodeLegacy(encoded []byte) *Account { - a0 := &accountV0{} - if err := rlp.DecodeBytes(encoded, a0); err == nil { - return &Account{ - Address: a0.Address, - Balance: uint256.MustFromBig(a0.Balance), - Nonce: a0.Nonce, - CodeHash: a0.CodeHash, - CollectionID: a0.CollectionID, - } - } - return nil + return a, rlp.DecodeBytes(inp, a) } From e3ad1f4335947582474050d6dae307ce2f57af46 Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:02:23 +0200 Subject: [PATCH 6/8] remove usage of legacy hash list --- fvm/evm/handler/blockstore.go | 68 ++++------------------------------- 1 file changed, 6 insertions(+), 62 deletions(-) diff --git a/fvm/evm/handler/blockstore.go b/fvm/evm/handler/blockstore.go index 588ae93277f..c1dcf77cb49 100644 --- a/fvm/evm/handler/blockstore.go +++ b/fvm/evm/handler/blockstore.go @@ -14,7 +14,6 @@ const ( BlockHashListCapacity = 256 BlockStoreLatestBlockKey = "LatestBlock" BlockStoreLatestBlockProposalKey = "LatestBlockProposal" - BlockStoreBlockHashesKey = "LatestBlockHashes" ) type BlockStore struct { @@ -167,75 +166,20 @@ func (bs *BlockStore) BlockHash(height uint64) (gethCommon.Hash, error) { } func (bs *BlockStore) getBlockHashList() (*BlockHashList, error) { - // check legacy block hash list first - return bs.checkLegacyAndMigrate() - // TODO: when preview net is out, we can remove the call to legacy and uncomment below - // BlockStoreBlockHashesKey constant also be removed - // - // bhl, err := NewBlockHashList(bs.backend, bs.rootAddress, BlockHashListCapacity) - // if err != nil { - // return nil, err - // } - // if bhl.IsEmpty() { - // err = bhl.Push( - // types.GenesisBlock(bs.chainID).Height, - // types.GenesisBlockHash(bs.chainID), - // ) - // if err != nil { - // return nil, err - // } - // } - // return bhl, nil -} - -func (bs *BlockStore) checkLegacyAndMigrate() (*BlockHashList, error) { - data, err := bs.backend.GetValue(bs.rootAddress[:], []byte(BlockStoreBlockHashesKey)) - if err != nil { - return nil, err - } - - // no legacy found - if len(data) == 0 { - bhl, err := NewBlockHashList(bs.backend, bs.rootAddress, BlockHashListCapacity) - if err != nil { - return nil, err - } - if bhl.IsEmpty() { - err = bhl.Push( - types.GenesisBlock(bs.chainID).Height, - types.GenesisBlockHash(bs.chainID), - ) - if err != nil { - return nil, err - } - } - return bhl, nil - } - - legacy, err := types.NewBlockHashListFromEncoded(data) - if err != nil { - return nil, err - } - - // migrate the data bhl, err := NewBlockHashList(bs.backend, bs.rootAddress, BlockHashListCapacity) if err != nil { return nil, err } - for i := uint64(0); i <= legacy.MaxAvailableHeight(); i++ { - // for the non-existing ones we insert empty hash - _, bh := legacy.BlockHashByHeight(i) - err = bhl.Push(i, bh) + + if bhl.IsEmpty() { + err = bhl.Push( + types.GenesisBlock(bs.chainID).Height, + types.GenesisBlockHash(bs.chainID), + ) if err != nil { return nil, err } } - // reset the old key - err = bs.backend.SetValue(bs.rootAddress[:], []byte(BlockStoreBlockHashesKey), nil) - if err != nil { - return nil, err - } - return bhl, nil } From be7802c81a8549a9282b3ddffb9fe7f6eee78d3d Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Mon, 19 Aug 2024 16:14:09 +0200 Subject: [PATCH 7/8] remove previewnet comment --- fvm/evm/types/block.go | 1 - 1 file changed, 1 deletion(-) diff --git a/fvm/evm/types/block.go b/fvm/evm/types/block.go index 1ca51b03795..bd2ab4e8408 100644 --- a/fvm/evm/types/block.go +++ b/fvm/evm/types/block.go @@ -84,7 +84,6 @@ func NewBlockFromBytes(encoded []byte) (*Block, error) { // GenesisTimestamp returns the block time stamp for EVM genesis block func GenesisTimestamp(flowChainID flow.ChainID) uint64 { - // default evm chain ID is previewNet switch flowChainID { case flow.Testnet: return uint64(time.Date(2024, time.August, 1, 0, 0, 0, 0, time.UTC).Unix()) From 9fa185f9c8f8b5de5ac08902a10573f7f7c1414d Mon Sep 17 00:00:00 2001 From: sideninja <75445744+sideninja@users.noreply.github.com> Date: Mon, 19 Aug 2024 17:37:55 +0200 Subject: [PATCH 8/8] remove legacy timestamp test --- fvm/evm/handler/blockstore_test.go | 109 ----------------------------- 1 file changed, 109 deletions(-) diff --git a/fvm/evm/handler/blockstore_test.go b/fvm/evm/handler/blockstore_test.go index 482ff553edc..b98a86eabcc 100644 --- a/fvm/evm/handler/blockstore_test.go +++ b/fvm/evm/handler/blockstore_test.go @@ -5,7 +5,6 @@ import ( "testing" gethCommon "github.com/onflow/go-ethereum/common" - gethRLP "github.com/onflow/go-ethereum/rlp" "github.com/stretchr/testify/require" "github.com/onflow/flow-go/fvm/evm/handler" @@ -97,111 +96,3 @@ func TestBlockStore(t *testing.T) { }) } - -// This test reproduces a state before a breaking change on the Block type, -// which added a timestamp and total gas used, -// then it adds new blocks and makes sure the retrival -// 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(chainID, backend, root) - - // block type before breaking change (no timestamp and total gas used) - type oldBlockV1 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - UUIDIndex uint64 - TotalSupply uint64 - StateRoot gethCommon.Hash - ReceiptRoot gethCommon.Hash - TransactionHashes []gethCommon.Hash - } - - g := types.GenesisBlock(chainID) - h, err := g.Hash() - require.NoError(t, err) - - b := oldBlockV1{ - ParentBlockHash: h, - Height: 1, - ReceiptRoot: g.ReceiptRoot, - UUIDIndex: 123, - TotalSupply: 1, - StateRoot: h, - TransactionHashes: []gethCommon.Hash{h}, - } - blockBytes, err := gethRLP.EncodeToBytes(b) - require.NoError(t, err) - - // store a block without timestamp, simulate existing state before the breaking change - err = backend.SetValue(root[:], []byte(handler.BlockStoreLatestBlockKey), blockBytes) - require.NoError(t, err) - - block, err := bs.LatestBlock() - require.NoError(t, err) - - require.Empty(t, block.Timestamp) - require.Empty(t, block.TotalGasUsed) - require.Equal(t, b.Height, block.Height) - require.Equal(t, b.ParentBlockHash, block.ParentBlockHash) - require.Equal(t, b.ReceiptRoot, block.ReceiptRoot) - - // added timestamp - type oldBlockV2 struct { - ParentBlockHash gethCommon.Hash - Height uint64 - Timestamp uint64 - TotalSupply *big.Int - ReceiptRoot gethCommon.Hash - TransactionHashes []gethCommon.Hash - } - - b2 := oldBlockV2{ - ParentBlockHash: h, - Height: 2, - TotalSupply: g.TotalSupply, - ReceiptRoot: g.ReceiptRoot, - Timestamp: 1, - } - blockBytes2, err := gethRLP.EncodeToBytes(b2) - require.NoError(t, err) - - // store a block without timestamp, simulate existing state before the breaking change - err = backend.SetValue(root[:], []byte(handler.BlockStoreLatestBlockKey), blockBytes2) - require.NoError(t, err) - - err = bs.ResetBlockProposal() - require.NoError(t, err) - - block2, err := bs.LatestBlock() - require.NoError(t, err) - - require.Empty(t, block2.TotalGasUsed) - require.Equal(t, b2.Height, block2.Height) - require.Equal(t, b2.ParentBlockHash, block2.ParentBlockHash) - require.Equal(t, b2.TotalSupply, block2.TotalSupply) - require.Equal(t, b2.ReceiptRoot, block2.ReceiptRoot) - require.Equal(t, b2.Timestamp, block2.Timestamp) - - bp, err := bs.BlockProposal() - require.NoError(t, err) - - blockBytes3, err := gethRLP.EncodeToBytes(bp.Block) - require.NoError(t, err) - - err = backend.SetValue(root[:], []byte(handler.BlockStoreLatestBlockKey), blockBytes3) - require.NoError(t, err) - - bb, err := bs.LatestBlock() - require.NoError(t, err) - require.NotNil(t, bb.ParentBlockHash) - require.NotNil(t, bb.TotalGasUsed) - require.NotNil(t, bb.Timestamp) - require.Equal(t, b2.Height+1, bb.Height) - }) - }) -}