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

[Flow EVM] replace block.TransactionHashes with TransactionHashRoot #6222

Merged
merged 15 commits into from
Jul 18, 2024
Merged
2 changes: 1 addition & 1 deletion engine/execution/state/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBootstrapLedger(t *testing.T) {
}

func TestBootstrapLedger_ZeroTokenSupply(t *testing.T) {
expectedStateCommitmentBytes, _ := hex.DecodeString("e0cf0fd62dd620333d5ce07720231533dad07c84efaed76ad88cf4c55ee5344d")
expectedStateCommitmentBytes, _ := hex.DecodeString("8d634458df94d3a284d7363686269c31c45b5ddd017215ec0505ea03a5f547d1")
expectedStateCommitment, err := flow.ToStateCommitment(expectedStateCommitmentBytes)
require.NoError(t, err)

Expand Down
27 changes: 18 additions & 9 deletions fvm/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,8 @@ func TestEVMRun(t *testing.T) {
// assert event fields are correct
require.Len(t, output.Events, 1)
txEvent := output.Events[0]
txEventPayload := testutils.TxEventToPayload(t, txEvent, sc.EVMContract.Address)
require.NoError(t, err)

// commit block
blockEventPayload, snapshot := callEVMHeartBeat(t,
Expand All @@ -116,13 +118,14 @@ func TestEVMRun(t *testing.T) {
require.NotEmpty(t, blockEventPayload.Hash)
require.Equal(t, uint64(43785), blockEventPayload.TotalGasUsed)
require.NotEmpty(t, blockEventPayload.Hash)
require.Len(t, blockEventPayload.TransactionHashes, 1)
require.NotEmpty(t, blockEventPayload.ReceiptRoot)

txEventPayload := testutils.TxEventToPayload(t, txEvent, sc.EVMContract.Address)
require.NoError(t, err)
txHashes := types.TransactionHashes{txEventPayload.Hash}
require.Equal(t,
txHashes.RootHash(),
blockEventPayload.TransactionHashRoot,
)
require.NotEmpty(t, blockEventPayload.ReceiptRoot)

require.NotEmpty(t, txEventPayload.Hash)
require.Equal(t, innerTxBytes, txEventPayload.Payload)
require.Equal(t, uint16(types.ErrCodeNoError), txEventPayload.ErrorCode)
require.Equal(t, uint16(0), txEventPayload.Index)
Expand Down Expand Up @@ -458,6 +461,7 @@ func TestEVMBatchRun(t *testing.T) {
snapshot = snapshot.Append(state)

require.Len(t, output.Events, batchCount)
txHashes := make(types.TransactionHashes, 0)
for i, event := range output.Events {
if i == batchCount { // last one is block executed
continue
Expand All @@ -471,6 +475,7 @@ func TestEVMBatchRun(t *testing.T) {
event, err := types.DecodeTransactionEventPayload(cadenceEvent)
require.NoError(t, err)

txHashes = append(txHashes, event.Hash)
var logs []*gethTypes.Log
err = rlp.DecodeBytes(event.Logs, &logs)
require.NoError(t, err)
Expand All @@ -490,7 +495,10 @@ func TestEVMBatchRun(t *testing.T) {

require.NotEmpty(t, blockEventPayload.Hash)
require.Equal(t, uint64(155513), blockEventPayload.TotalGasUsed)
require.Len(t, blockEventPayload.TransactionHashes, 5)
require.Equal(t,
txHashes.RootHash(),
blockEventPayload.TransactionHashRoot,
)

// retrieve the values
retrieveCode := []byte(fmt.Sprintf(
Expand Down Expand Up @@ -972,10 +980,11 @@ func TestEVMAddressDeposit(t *testing.T) {

require.NotEmpty(t, blockEventPayload.Hash)
require.Equal(t, uint64(21000), blockEventPayload.TotalGasUsed)
require.Len(t, blockEventPayload.TransactionHashes, 1)

txHashes := types.TransactionHashes{txEventPayload.Hash}
require.Equal(t,
txEventPayload.Hash,
blockEventPayload.TransactionHashes[0],
txHashes.RootHash(),
blockEventPayload.TransactionHashRoot,
)
})
}
Expand Down
3 changes: 1 addition & 2 deletions fvm/evm/handler/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ func (bs *BlockStore) ResetBlockProposal() error {

// CommitBlockProposal commits the block proposal to the chain
func (bs *BlockStore) CommitBlockProposal(bp *types.BlockProposal) error {
// populate receipt root hash
bp.PopulateReceiptRoot()
bp.PopulateRoots()

blockBytes, err := bp.Block.ToBytes()
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions fvm/evm/handler/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ func (h *ContractHandler) batchRun(rlpEncodedTxs [][]byte, coinbase types.Addres
}

// if there were no valid transactions skip emitting events
// and committing a new block
if len(bp.TransactionHashes) == 0 {
// and update the block proposal
if len(bp.TxHashes) == 0 {
return res, nil
}

Expand Down
24 changes: 10 additions & 14 deletions fvm/evm/handler/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func TestHandler_TransactionRunOrPanic(t *testing.T) {
events := backend.Events()
require.Len(t, events, 1)
txEventPayload := testutils.TxEventToPayload(t, events[0], rootAddr)

require.NoError(t, err)
// check logs
var logs []*gethTypes.Log
err = rlp.DecodeBytes(txEventPayload.Logs, &logs)
Expand All @@ -106,15 +106,11 @@ func TestHandler_TransactionRunOrPanic(t *testing.T) {
events = backend.Events()
require.Len(t, events, 2)
blockEventPayload := testutils.BlockEventToPayload(t, events[1], rootAddr)
// make sure block transaction list references the above transaction id

require.Len(t, blockEventPayload.TransactionHashes, 1)
eventTxID := blockEventPayload.TransactionHashes[0] // only one hash in block
// make sure the transaction id included in the block transaction list is the same as tx sumbmitted
assert.Equal(
t,
evmTx.Hash(),
eventTxID,
types.TransactionHashes{txEventPayload.Hash}.RootHash(),
blockEventPayload.TransactionHashRoot,
)
})
})
Expand Down Expand Up @@ -342,7 +338,7 @@ func TestHandler_COA(t *testing.T) {
require.Len(t, events, 3)

// Block level expected values
txHashes := make([]gethCommon.Hash, 0)
txHashes := make(types.TransactionHashes, 0)
totalGasUsed := uint64(0)

// deploy COA transaction event
Expand Down Expand Up @@ -375,12 +371,12 @@ func TestHandler_COA(t *testing.T) {
events = backend.Events()
require.Len(t, events, 4)
blockEventPayload := testutils.BlockEventToPayload(t, events[3], rootAddr)
for i, txHash := range txHashes {
require.Equal(t,
txHash,
blockEventPayload.TransactionHashes[i],
)
}
assert.Equal(
t,
txHashes.RootHash(),
blockEventPayload.TransactionHashRoot,
)

require.Equal(t, totalGasUsed, blockEventPayload.TotalGasUsed)

// check gas usage
Expand Down
12 changes: 6 additions & 6 deletions fvm/evm/stdlib/contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "FlowToken"
access(all)
contract EVM {

// Entitlements enabling finer-graned access control on a CadenceOwnedAccount
// Entitlements enabling finer-grained access control on a CadenceOwnedAccount
access(all) entitlement Validate
access(all) entitlement Withdraw
access(all) entitlement Call
Expand All @@ -30,13 +30,13 @@ contract EVM {
totalGasUsed: UInt64,
// parent block hash
parentHash: [UInt8; 32],
// hash of all the transaction receipts
// root hash of all the transaction receipts
receiptRoot: [UInt8; 32],
// all the transactions included in the block
transactionHashes: [[UInt8; 32]]
// root hash of all the transaction hashes
transactionHashRoot: [UInt8; 32],
)

/// Transaction executed event is emitted everytime a transaction
/// Transaction executed event is emitted every time a transaction
/// is executed by the EVM (even if failed).
access(all)
event TransactionExecuted(
Expand All @@ -58,7 +58,7 @@ contract EVM {
contractAddress: String,
// RLP encoded logs
logs: [UInt8],
// block height in which transaction was inclued
// block height in which transaction was included
blockHeight: UInt64,
/// captures the hex encoded data that is returned from
/// the evm. For contract deployments
Expand Down
Loading
Loading