From 22963a115ddf2c3fddfa34351a019066421a58c3 Mon Sep 17 00:00:00 2001 From: maskpp Date: Fri, 18 Nov 2022 17:11:04 +0800 Subject: [PATCH] feat(trace): Add header and upgrade TransactionTrace (#181) * Add header and upgrade TransactionTrace * Add header * change blockResult struct * rm chainID from TransactionData * Rename blockResult to blockTrace in everywhere * fix comments --- core/types/l2trace.go | 8 +- core/types/l2trace_block.go | 75 +++---------------- .../{api_blockresult.go => api_blocktrace.go} | 45 ++++++----- ...kresult_test.go => api_blocktrace_test.go} | 28 +++---- ethclient/ethclient.go | 22 +++--- internal/utesting/zktrie_gen_witness_test.go | 26 +++---- trie/zkproof/writer.go | 18 ++--- 7 files changed, 88 insertions(+), 134 deletions(-) rename eth/tracers/{api_blockresult.go => api_blocktrace.go} (86%) rename eth/tracers/{api_blockresult_test.go => api_blocktrace_test.go} (96%) diff --git a/core/types/l2trace.go b/core/types/l2trace.go index a92bd14220..99e34a64b2 100644 --- a/core/types/l2trace.go +++ b/core/types/l2trace.go @@ -21,9 +21,11 @@ var ( } ) -// BlockResult contains block execution traces and results required for rollers. -type BlockResult struct { - BlockTrace *BlockTrace `json:"blockTrace"` +// BlockTrace contains block execution traces and results required for rollers. +type BlockTrace struct { + Coinbase *AccountWrapper `json:"coinbase"` + Header *Header `json:"header"` + Transactions []*TransactionData `json:"transactions"` StorageTrace *StorageTrace `json:"storageTrace"` ExecutionResults []*ExecutionResult `json:"executionResults"` MPTWitness *json.RawMessage `json:"mptwitness,omitempty"` diff --git a/core/types/l2trace_block.go b/core/types/l2trace_block.go index b9c09e424f..3e643383d0 100644 --- a/core/types/l2trace_block.go +++ b/core/types/l2trace_block.go @@ -4,78 +4,23 @@ import ( "math/big" "github.com/scroll-tech/go-ethereum/common" - "github.com/scroll-tech/go-ethereum/common/hexutil" "github.com/scroll-tech/go-ethereum/params" ) -type BlockTrace struct { - Number *hexutil.Big `json:"number"` - Hash common.Hash `json:"hash"` - GasLimit uint64 `json:"gasLimit"` - Difficulty *hexutil.Big `json:"difficulty"` - BaseFee *hexutil.Big `json:"baseFee"` - Coinbase *AccountWrapper `json:"coinbase"` - Time uint64 `json:"time"` - Transactions []*TransactionTrace `json:"transactions"` +type TransactionData struct { + IsCreate bool `json:"isCreate"` + From common.Address `json:"from"` + *Transaction } -type TransactionTrace struct { - Type uint8 `json:"type"` - Nonce uint64 `json:"nonce"` - TxHash string `json:"txHash"` - Gas uint64 `json:"gas"` - GasPrice *hexutil.Big `json:"gasPrice"` - From common.Address `json:"from"` - To *common.Address `json:"to"` - ChainId *hexutil.Big `json:"chainId"` - Value *hexutil.Big `json:"value"` - Data string `json:"data"` - IsCreate bool `json:"isCreate"` - V *hexutil.Big `json:"v"` - R *hexutil.Big `json:"r"` - S *hexutil.Big `json:"s"` -} - -// NewTraceBlock supports necessary fields for roller. -func NewTraceBlock(config *params.ChainConfig, block *Block, coinbase *AccountWrapper) *BlockTrace { - txs := make([]*TransactionTrace, block.Transactions().Len()) - for i, tx := range block.Transactions() { - txs[i] = newTraceTransaction(tx, block.NumberU64(), config) - } - - return &BlockTrace{ - Number: (*hexutil.Big)(block.Number()), - Hash: block.Hash(), - GasLimit: block.GasLimit(), - Difficulty: (*hexutil.Big)(block.Difficulty()), - BaseFee: (*hexutil.Big)(block.BaseFee()), - Coinbase: coinbase, - Time: block.Time(), - Transactions: txs, - } -} - -// newTraceTransaction returns a transaction that will serialize to the trace +// NewTraceTransaction returns a transaction that will serialize to the trace // representation, with the given location metadata set (if available). -func newTraceTransaction(tx *Transaction, blockNumber uint64, config *params.ChainConfig) *TransactionTrace { +func NewTraceTransaction(tx *Transaction, blockNumber uint64, config *params.ChainConfig) *TransactionData { signer := MakeSigner(config, big.NewInt(0).SetUint64(blockNumber)) from, _ := Sender(signer, tx) - v, r, s := tx.RawSignatureValues() - result := &TransactionTrace{ - Type: tx.Type(), - TxHash: tx.Hash().String(), - Nonce: tx.Nonce(), - ChainId: (*hexutil.Big)(tx.ChainId()), - From: from, - Gas: tx.Gas(), - GasPrice: (*hexutil.Big)(tx.GasPrice()), - To: tx.To(), - Value: (*hexutil.Big)(tx.Value()), - Data: hexutil.Encode(tx.Data()), - IsCreate: tx.To() == nil, - V: (*hexutil.Big)(v), - R: (*hexutil.Big)(r), - S: (*hexutil.Big)(s), + return &TransactionData{ + From: from, + IsCreate: tx.To() == nil, + Transaction: tx, } - return result } diff --git a/eth/tracers/api_blockresult.go b/eth/tracers/api_blocktrace.go similarity index 86% rename from eth/tracers/api_blockresult.go rename to eth/tracers/api_blocktrace.go index cb3bd6de6a..c3959577a3 100644 --- a/eth/tracers/api_blockresult.go +++ b/eth/tracers/api_blocktrace.go @@ -19,7 +19,7 @@ import ( ) type TraceBlock interface { - GetBlockResultByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *TraceConfig) (trace *types.BlockResult, err error) + GetBlockTraceByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *TraceConfig) (trace *types.BlockTrace, err error) } type traceEnv struct { @@ -42,8 +42,8 @@ type traceEnv struct { executionResults []*types.ExecutionResult } -// GetBlockResultByNumberOrHash replays the block and returns the structured BlockResult by hash or number. -func (api *API) GetBlockResultByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *TraceConfig) (trace *types.BlockResult, err error) { +// GetBlockTraceByNumberOrHash replays the block and returns the structured BlockTrace by hash or number. +func (api *API) GetBlockTraceByNumberOrHash(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash, config *TraceConfig) (trace *types.BlockTrace, err error) { var block *types.Block if number, ok := blockNrOrHash.Number(); ok { block, err = api.blockByNumber(ctx, number) @@ -75,7 +75,7 @@ func (api *API) GetBlockResultByNumberOrHash(ctx context.Context, blockNrOrHash return nil, err } - return api.getBlockResult(block, env) + return api.getBlockTrace(block, env) } // Make trace environment for current block. @@ -130,7 +130,7 @@ func (api *API) createTraceEnv(ctx context.Context, config *TraceConfig, block * return env, nil } -func (api *API) getBlockResult(block *types.Block, env *traceEnv) (*types.BlockResult, error) { +func (api *API) getBlockTrace(block *types.Block, env *traceEnv) (*types.BlockTrace, error) { // Execute all the transaction contained within the block concurrently var ( txs = block.Transactions() @@ -190,7 +190,7 @@ func (api *API) getBlockResult(block *types.Block, env *traceEnv) (*types.BlockR } } - return api.fillBlockResult(env, block) + return api.fillBlockTrace(env, block) } func (api *API) getTxResult(env *traceEnv, state *state.StateDB, index int, block *types.Block) error { @@ -328,22 +328,29 @@ func (api *API) getTxResult(env *traceEnv, state *state.StateDB, index int, bloc return nil } -// Fill blockResult content after all the txs are finished running. -func (api *API) fillBlockResult(env *traceEnv, block *types.Block) (*types.BlockResult, error) { +// Fill blockTrace content after all the txs are finished running. +func (api *API) fillBlockTrace(env *traceEnv, block *types.Block) (*types.BlockTrace, error) { statedb := env.state - txs := block.Transactions() - coinbase := types.AccountWrapper{ - Address: env.coinbase, - Nonce: statedb.GetNonce(env.coinbase), - Balance: (*hexutil.Big)(statedb.GetBalance(env.coinbase)), - CodeHash: statedb.GetCodeHash(env.coinbase), + + txs := make([]*types.TransactionData, block.Transactions().Len()) + for i, tx := range block.Transactions() { + txs[i] = types.NewTraceTransaction(tx, block.NumberU64(), api.backend.ChainConfig()) } - blockResult := &types.BlockResult{ - BlockTrace: types.NewTraceBlock(api.backend.ChainConfig(), block, &coinbase), + + blockTrace := &types.BlockTrace{ + Coinbase: &types.AccountWrapper{ + Address: env.coinbase, + Nonce: statedb.GetNonce(env.coinbase), + Balance: (*hexutil.Big)(statedb.GetBalance(env.coinbase)), + CodeHash: statedb.GetCodeHash(env.coinbase), + }, + Header: block.Header(), StorageTrace: env.StorageTrace, ExecutionResults: env.executionResults, + Transactions: txs, } - for i, tx := range txs { + + for i, tx := range block.Transactions() { evmTrace := env.executionResults[i] // probably a Contract Call if len(tx.Data()) != 0 && tx.To() != nil { @@ -358,10 +365,10 @@ func (api *API) fillBlockResult(env *traceEnv, block *types.Block) (*types.Block // only zktrie model has the ability to get `mptwitness`. if api.backend.ChainConfig().Zktrie { - if err := zkproof.FillBlockResultForMPTWitness(zkproof.MPTWitnessType(api.backend.CacheConfig().MPTWitness), blockResult); err != nil { + if err := zkproof.FillBlockTraceForMPTWitness(zkproof.MPTWitnessType(api.backend.CacheConfig().MPTWitness), blockTrace); err != nil { log.Error("fill mpt witness fail", "error", err) } } - return blockResult, nil + return blockTrace, nil } diff --git a/eth/tracers/api_blockresult_test.go b/eth/tracers/api_blocktrace_test.go similarity index 96% rename from eth/tracers/api_blockresult_test.go rename to eth/tracers/api_blocktrace_test.go index cbd30c87f7..aeb39e1d6c 100644 --- a/eth/tracers/api_blockresult_test.go +++ b/eth/tracers/api_blocktrace_test.go @@ -42,7 +42,7 @@ var erc20MetaData = &bind.MetaData{ Bin: "0x60806040523480156200001157600080fd5b50604051620014b2380380620014b2833981810160405260a08110156200003757600080fd5b815160208301516040808501805191519395929483019291846401000000008211156200006357600080fd5b9083019060208201858111156200007957600080fd5b82516401000000008111828201881017156200009457600080fd5b82525081516020918201929091019080838360005b83811015620000c3578181015183820152602001620000a9565b50505050905090810190601f168015620000f15780820380516001836020036101000a031916815260200191505b50604052602001805160405193929190846401000000008211156200011557600080fd5b9083019060208201858111156200012b57600080fd5b82516401000000008111828201881017156200014657600080fd5b82525081516020918201929091019080838360005b83811015620001755781810151838201526020016200015b565b50505050905090810190601f168015620001a35780820380516001836020036101000a031916815260200191505b5060405260209081015185519093508592508491620001c8916003918501906200026b565b508051620001de9060049060208401906200026b565b50506005805461ff001960ff1990911660121716905550600680546001600160a01b038088166001600160a01b0319928316179092556007805492871692909116919091179055620002308162000255565b50506005805462010000600160b01b0319163362010000021790555062000307915050565b6005805460ff191660ff92909216919091179055565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f10620002ae57805160ff1916838001178555620002de565b82800160010185558215620002de579182015b82811115620002de578251825591602001919060010190620002c1565b50620002ec929150620002f0565b5090565b5b80821115620002ec5760008155600101620002f1565b61119b80620003176000396000f3fe608060405234801561001057600080fd5b506004361061010b5760003560e01c80635c975abb116100a257806395d89b411161007157806395d89b41146103015780639dc29fac14610309578063a457c2d714610335578063a9059cbb14610361578063dd62ed3e1461038d5761010b565b80635c975abb1461029d57806370a08231146102a55780638456cb59146102cb5780638e50817a146102d35761010b565b8063313ce567116100de578063313ce5671461021d578063395093511461023b5780633f4ba83a1461026757806340c10f19146102715761010b565b806306fdde0314610110578063095ea7b31461018d57806318160ddd146101cd57806323b872dd146101e7575b600080fd5b6101186103bb565b6040805160208082528351818301528351919283929083019185019080838360005b8381101561015257818101518382015260200161013a565b50505050905090810190601f16801561017f5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6101b9600480360360408110156101a357600080fd5b506001600160a01b038135169060200135610451565b604080519115158252519081900360200190f35b6101d561046e565b60408051918252519081900360200190f35b6101b9600480360360608110156101fd57600080fd5b506001600160a01b03813581169160208101359091169060400135610474565b6102256104fb565b6040805160ff9092168252519081900360200190f35b6101b96004803603604081101561025157600080fd5b506001600160a01b038135169060200135610504565b61026f610552565b005b61026f6004803603604081101561028757600080fd5b506001600160a01b0381351690602001356105a9565b6101b9610654565b6101d5600480360360208110156102bb57600080fd5b50356001600160a01b0316610662565b61026f61067d565b61026f600480360360408110156102e957600080fd5b506001600160a01b03813581169160200135166106d2565b610118610757565b61026f6004803603604081101561031f57600080fd5b506001600160a01b0381351690602001356107b8565b6101b96004803603604081101561034b57600080fd5b506001600160a01b03813516906020013561085f565b6101b96004803603604081101561037757600080fd5b506001600160a01b0381351690602001356108c7565b6101d5600480360360408110156103a357600080fd5b506001600160a01b03813581169160200135166108db565b60038054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156104475780601f1061041c57610100808354040283529160200191610447565b820191906000526020600020905b81548152906001019060200180831161042a57829003601f168201915b5050505050905090565b600061046561045e610906565b848461090a565b50600192915050565b60025490565b60006104818484846109f6565b6104f18461048d610906565b6104ec85604051806060016040528060288152602001611085602891396001600160a01b038a166000908152600160205260408120906104cb610906565b6001600160a01b031681526020810191909152604001600020549190610b51565b61090a565b5060019392505050565b60055460ff1690565b6000610465610511610906565b846104ec8560016000610522610906565b6001600160a01b03908116825260208083019390935260409182016000908120918c168152925290205490610be8565b6007546001600160a01b0316331461059f576040805162461bcd60e51b815260206004820152600b60248201526a1b9bdd08185b1b1bddd95960aa1b604482015290519081900360640190fd5b6105a7610c49565b565b600554610100900460ff16156105f9576040805162461bcd60e51b815260206004820152601060248201526f14185d5cd8589b194e881c185d5cd95960821b604482015290519081900360640190fd5b6006546001600160a01b03163314610646576040805162461bcd60e51b815260206004820152600b60248201526a1b9bdd08185b1b1bddd95960aa1b604482015290519081900360640190fd5b6106508282610ced565b5050565b600554610100900460ff1690565b6001600160a01b031660009081526020819052604090205490565b6007546001600160a01b031633146106ca576040805162461bcd60e51b815260206004820152600b60248201526a1b9bdd08185b1b1bddd95960aa1b604482015290519081900360640190fd5b6105a7610ddd565b6005546201000090046001600160a01b03163314610726576040805162461bcd60e51b815260206004820152600c60248201526b6f6e6c7920466163746f727960a01b604482015290519081900360640190fd5b600780546001600160a01b039283166001600160a01b03199182161790915560068054939092169216919091179055565b60048054604080516020601f60026000196101006001881615020190951694909404938401819004810282018101909252828152606093909290918301828280156104475780601f1061041c57610100808354040283529160200191610447565b600554610100900460ff1615610808576040805162461bcd60e51b815260206004820152601060248201526f14185d5cd8589b194e881c185d5cd95960821b604482015290519081900360640190fd5b6006546001600160a01b03163314610855576040805162461bcd60e51b815260206004820152600b60248201526a1b9bdd08185b1b1bddd95960aa1b604482015290519081900360640190fd5b6106508282610e65565b600061046561086c610906565b846104ec856040518060600160405280602581526020016111176025913960016000610896610906565b6001600160a01b03908116825260208083019390935260409182016000908120918d16815292529020549190610b51565b60006104656108d4610906565b84846109f6565b6001600160a01b03918216600090815260016020908152604080832093909416825291909152205490565b3390565b6001600160a01b03831661094f5760405162461bcd60e51b81526004018080602001828103825260248152602001806110f36024913960400191505060405180910390fd5b6001600160a01b0382166109945760405162461bcd60e51b815260040180806020018281038252602281526020018061103d6022913960400191505060405180910390fd5b6001600160a01b03808416600081815260016020908152604080832094871680845294825291829020859055815185815291517f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b9259281900390910190a3505050565b6001600160a01b038316610a3b5760405162461bcd60e51b81526004018080602001828103825260258152602001806110ce6025913960400191505060405180910390fd5b6001600160a01b038216610a805760405162461bcd60e51b8152600401808060200182810382526023815260200180610ff86023913960400191505060405180910390fd5b610a8b838383610f61565b610ac88160405180606001604052806026815260200161105f602691396001600160a01b0386166000908152602081905260409020549190610b51565b6001600160a01b038085166000908152602081905260408082209390935590841681522054610af79082610be8565b6001600160a01b038084166000818152602081815260409182902094909455805185815290519193928716927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef92918290030190a3505050565b60008184841115610be05760405162461bcd60e51b81526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610ba5578181015183820152602001610b8d565b50505050905090810190601f168015610bd25780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b505050900390565b600082820183811015610c42576040805162461bcd60e51b815260206004820152601b60248201527f536166654d6174683a206164646974696f6e206f766572666c6f770000000000604482015290519081900360640190fd5b9392505050565b600554610100900460ff16610c9c576040805162461bcd60e51b815260206004820152601460248201527314185d5cd8589b194e881b9bdd081c185d5cd95960621b604482015290519081900360640190fd5b6005805461ff00191690557f5db9ee0a495bf2e6ff9c91a7834c1ba4fdd244a5e8aa4e537bd38aeae4b073aa610cd0610906565b604080516001600160a01b039092168252519081900360200190a1565b6001600160a01b038216610d48576040805162461bcd60e51b815260206004820152601f60248201527f45524332303a206d696e7420746f20746865207a65726f206164647265737300604482015290519081900360640190fd5b610d5460008383610f61565b600254610d619082610be8565b6002556001600160a01b038216600090815260208190526040902054610d879082610be8565b6001600160a01b0383166000818152602081815260408083209490945583518581529351929391927fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9281900390910190a35050565b600554610100900460ff1615610e2d576040805162461bcd60e51b815260206004820152601060248201526f14185d5cd8589b194e881c185d5cd95960821b604482015290519081900360640190fd5b6005805461ff0019166101001790557f62e78cea01bee320cd4e420270b5ea74000d11b0c9f74754ebdbfc544b05a258610cd0610906565b6001600160a01b038216610eaa5760405162461bcd60e51b81526004018080602001828103825260218152602001806110ad6021913960400191505060405180910390fd5b610eb682600083610f61565b610ef38160405180606001604052806022815260200161101b602291396001600160a01b0385166000908152602081905260409020549190610b51565b6001600160a01b038316600090815260208190526040902055600254610f199082610fb5565b6002556040805182815290516000916001600160a01b038516917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef9181900360200190a35050565b610f6c838383610fb0565b610f74610654565b15610fb05760405162461bcd60e51b815260040180806020018281038252602a81526020018061113c602a913960400191505060405180910390fd5b505050565b6000610c4283836040518060400160405280601e81526020017f536166654d6174683a207375627472616374696f6e206f766572666c6f770000815250610b5156fe45524332303a207472616e7366657220746f20746865207a65726f206164647265737345524332303a206275726e20616d6f756e7420657863656564732062616c616e636545524332303a20617070726f766520746f20746865207a65726f206164647265737345524332303a207472616e7366657220616d6f756e7420657863656564732062616c616e636545524332303a207472616e7366657220616d6f756e74206578636565647320616c6c6f77616e636545524332303a206275726e2066726f6d20746865207a65726f206164647265737345524332303a207472616e736665722066726f6d20746865207a65726f206164647265737345524332303a20617070726f76652066726f6d20746865207a65726f206164647265737345524332303a2064656372656173656420616c6c6f77616e63652062656c6f77207a65726f45524332305061757361626c653a20746f6b656e207472616e73666572207768696c6520706175736564a2646970667358221220e96342bec8f6c2bf72815a39998973b64c3bed57770f402e9a7b7eeda0265d4c64736f6c634300060c0033", } -func TestAPI_GetBlockResultByNumberOrHash(t *testing.T) { +func TestAPI_GetBlockTraceByNumberOrHash(t *testing.T) { t.Parallel() erc20Abi, err := erc20MetaData.GetAbi() assert.NoError(t, err) @@ -91,25 +91,25 @@ func TestAPI_GetBlockResultByNumberOrHash(t *testing.T) { api := NewAPI(backend) // get trace hash := block.Hash() - blockResult, err := api.GetBlockResultByNumberOrHash(context.Background(), rpc.BlockNumberOrHash{ + blockTrace, err := api.GetBlockTraceByNumberOrHash(context.Background(), rpc.BlockNumberOrHash{ BlockHash: &hash, }, nil) assert.NoError(t, err) // check chain status - checkChainAndProof(t, backend, parent, block, blockResult) + checkChainAndProof(t, backend, parent, block, blockTrace) // check txs. - checkTxs(t, block.Transactions(), blockResult.BlockTrace.Transactions) + checkTxs(t, block.Transactions(), blockTrace.Transactions) txTraces, err := api.TraceBlockByNumber(context.Background(), 2, nil) assert.NoError(t, err) // check executionResults - checkStructLogs(t, txTraces, blockResult.ExecutionResults) + checkStructLogs(t, txTraces, blockTrace.ExecutionResults) // check coinbase - checkCoinbase(t, backend, blockResult.BlockTrace.Coinbase) + checkCoinbase(t, backend, blockTrace.Coinbase) } func verifyProof(t *testing.T, expect [][]byte, actual []hexutil.Bytes) { @@ -120,18 +120,18 @@ func verifyProof(t *testing.T, expect [][]byte, actual []hexutil.Bytes) { } } -func checkChainAndProof(t *testing.T, b *testBackend, parent *types.Block, block *types.Block, blockResult *types.BlockResult) { +func checkChainAndProof(t *testing.T, b *testBackend, parent *types.Block, block *types.Block, blockTrace *types.BlockTrace) { assert.Equal(t, parent.Hash(), block.ParentHash()) - storgeTrace := blockResult.StorageTrace + storgeTrace := blockTrace.StorageTrace assert.Equal(t, parent.Root().String(), storgeTrace.RootBefore.String()) assert.Equal(t, block.Root().String(), storgeTrace.RootAfter.String()) statedb, err := b.chain.StateAt(parent.Root()) assert.NoError(t, err) - storageProof := blockResult.StorageTrace.StorageProofs - for _, tx := range blockResult.BlockTrace.Transactions { - for _, addr := range []common.Address{tx.From, *tx.To} { + storageProof := blockTrace.StorageTrace.StorageProofs + for _, tx := range blockTrace.Transactions { + for _, addr := range []common.Address{tx.From, *tx.To()} { // verify proofs if data2, ok := storgeTrace.Proofs[addr.String()]; ok { data1, err := statedb.GetProof(addr) @@ -150,12 +150,12 @@ func checkChainAndProof(t *testing.T, b *testBackend, parent *types.Block, block } } -func checkTxs(t *testing.T, expect []*types.Transaction, actual []*types.TransactionTrace) { +func checkTxs(t *testing.T, expect []*types.Transaction, actual []*types.TransactionData) { assert.Equal(t, len(expect), len(actual)) for i := range expect { eTx, aTx := expect[i], actual[i] - assert.Equal(t, eTx.Hash().String(), aTx.TxHash) - assert.Equal(t, eTx.Gas(), aTx.Gas) + assert.Equal(t, eTx.Hash().String(), aTx.Hash()) + assert.Equal(t, eTx.Gas(), aTx.Gas()) } } diff --git a/ethclient/ethclient.go b/ethclient/ethclient.go index 02de047686..1f2693735b 100644 --- a/ethclient/ethclient.go +++ b/ethclient/ethclient.go @@ -325,21 +325,21 @@ func (ec *Client) SubscribeNewHead(ctx context.Context, ch chan<- *types.Header) return ec.c.EthSubscribe(ctx, ch, "newHeads") } -// GetBlockResultByHash returns the BlockResult given the block hash. -func (ec *Client) GetBlockResultByHash(ctx context.Context, blockHash common.Hash) (*types.BlockResult, error) { - blockResult := &types.BlockResult{} - return blockResult, ec.c.CallContext(ctx, &blockResult, "scroll_getBlockResultByNumberOrHash", blockHash) +// GetBlockTraceByHash returns the BlockTrace given the block hash. +func (ec *Client) GetBlockTraceByHash(ctx context.Context, blockHash common.Hash) (*types.BlockTrace, error) { + blockTrace := &types.BlockTrace{} + return blockTrace, ec.c.CallContext(ctx, &blockTrace, "scroll_getBlockTraceByNumberOrHash", blockHash) } -// GetBlockResultByNumber returns the BlockResult given the block number. -func (ec *Client) GetBlockResultByNumber(ctx context.Context, number *big.Int) (*types.BlockResult, error) { - blockResult := &types.BlockResult{} - return blockResult, ec.c.CallContext(ctx, &blockResult, "scroll_getBlockResultByNumberOrHash", toBlockNumArg(number)) +// GetBlockTraceByNumber returns the BlockTrace given the block number. +func (ec *Client) GetBlockTraceByNumber(ctx context.Context, number *big.Int) (*types.BlockTrace, error) { + blockTrace := &types.BlockTrace{} + return blockTrace, ec.c.CallContext(ctx, &blockTrace, "scroll_getBlockTraceByNumberOrHash", toBlockNumArg(number)) } -// SubscribeNewBlockResult subscribes to block execution trace when a new block is created. -func (ec *Client) SubscribeNewBlockResult(ctx context.Context, ch chan<- *types.BlockResult) (ethereum.Subscription, error) { - return ec.c.EthSubscribe(ctx, ch, "newBlockResult") +// SubscribeNewBlockTrace subscribes to block execution trace when a new block is created. +func (ec *Client) SubscribeNewBlockTrace(ctx context.Context, ch chan<- *types.BlockTrace) (ethereum.Subscription, error) { + return ec.c.EthSubscribe(ctx, ch, "newBlockTrace") } // State Access diff --git a/internal/utesting/zktrie_gen_witness_test.go b/internal/utesting/zktrie_gen_witness_test.go index 9ce1f0b24e..ca622a7bb0 100644 --- a/internal/utesting/zktrie_gen_witness_test.go +++ b/internal/utesting/zktrie_gen_witness_test.go @@ -22,7 +22,7 @@ func init() { } } -func loadStaff(t *testing.T, fname string) *types.BlockResult { +func loadStaff(t *testing.T, fname string) *types.BlockTrace { f, err := os.Open(fname) if err != nil { t.Fatal(err) @@ -33,7 +33,7 @@ func loadStaff(t *testing.T, fname string) *types.BlockResult { t.Fatal(err) } - out := new(types.BlockResult) + out := new(types.BlockTrace) err = json.Unmarshal(bt, out) if err != nil { @@ -84,7 +84,7 @@ func TestGreeterTx(t *testing.T) { } } - traces, err := zkproof.HandleBlockResult(trace) + traces, err := zkproof.HandleBlockTrace(trace) t.Log("traces: ", len(traces)) outObj, _ := json.Marshal(traces) t.Log(string(outObj)) @@ -95,7 +95,7 @@ func TestGreeterTx(t *testing.T) { func TestTokenTx(t *testing.T) { trace := loadStaff(t, "blocktraces/mpt_witness/token.json") - traces, err := zkproof.HandleBlockResult(trace) + traces, err := zkproof.HandleBlockTrace(trace) outObj, _ := json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -106,7 +106,7 @@ func TestTokenTx(t *testing.T) { func TestCallTx(t *testing.T) { trace := loadStaff(t, "blocktraces/mpt_witness/call.json") - traces, err := zkproof.HandleBlockResult(trace) + traces, err := zkproof.HandleBlockTrace(trace) outObj, _ := json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -114,7 +114,7 @@ func TestCallTx(t *testing.T) { } trace = loadStaff(t, "blocktraces/mpt_witness/call_edge.json") - traces, err = zkproof.HandleBlockResult(trace) + traces, err = zkproof.HandleBlockTrace(trace) outObj, _ = json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -124,7 +124,7 @@ func TestCallTx(t *testing.T) { func TestCreateTx(t *testing.T) { trace := loadStaff(t, "blocktraces/mpt_witness/create.json") - traces, err := zkproof.HandleBlockResult(trace) + traces, err := zkproof.HandleBlockTrace(trace) outObj, _ := json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -132,7 +132,7 @@ func TestCreateTx(t *testing.T) { } trace = loadStaff(t, "blocktraces/mpt_witness/deploy.json") - traces, err = zkproof.HandleBlockResult(trace) + traces, err = zkproof.HandleBlockTrace(trace) outObj, _ = json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -143,7 +143,7 @@ func TestCreateTx(t *testing.T) { func TestFailedCallTx(t *testing.T) { trace := loadStaff(t, "blocktraces/mpt_witness/fail_call.json") - traces, err := zkproof.HandleBlockResult(trace) + traces, err := zkproof.HandleBlockTrace(trace) outObj, _ := json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -151,7 +151,7 @@ func TestFailedCallTx(t *testing.T) { } trace = loadStaff(t, "blocktraces/mpt_witness/fail_create.json") - traces, err = zkproof.HandleBlockResult(trace) + traces, err = zkproof.HandleBlockTrace(trace) outObj, _ = json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -163,7 +163,7 @@ func TestFailedCallTx(t *testing.T) { //notice: now only work with OP_ORDER=2 func TestDeleteTx(t *testing.T) { trace := loadStaff(t, "blocktraces/mpt_witness/delete.json") - traces, err := zkproof.HandleBlockResult(trace) + traces, err := zkproof.HandleBlockTrace(trace) outObj, _ := json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -174,7 +174,7 @@ func TestDeleteTx(t *testing.T) { //notice: now only work with OP_ORDER=2 func TestDestructTx(t *testing.T) { trace := loadStaff(t, "blocktraces/mpt_witness/destruct.json") - traces, err := zkproof.HandleBlockResult(trace) + traces, err := zkproof.HandleBlockTrace(trace) outObj, _ := json.Marshal(traces) t.Log(string(outObj)) if err != nil { @@ -184,7 +184,7 @@ func TestDestructTx(t *testing.T) { func TestMutipleTx(t *testing.T) { trace := loadStaff(t, "blocktraces/mpt_witness/multi_txs.json") - traces, err := zkproof.HandleBlockResult(trace) + traces, err := zkproof.HandleBlockTrace(trace) outObj, _ := json.Marshal(traces) t.Log(string(outObj)) if err != nil { diff --git a/trie/zkproof/writer.go b/trie/zkproof/writer.go index 86e0c8a90e..520bce1173 100644 --- a/trie/zkproof/writer.go +++ b/trie/zkproof/writer.go @@ -239,7 +239,7 @@ const ( posCALL = 2 posSTATICCALL = 0 -// posSELFDESTRUCT = 2 + // posSELFDESTRUCT = 2 ) func getAccountState(l *types.StructLogRes, pos int) *types.AccountWrapper { @@ -737,12 +737,12 @@ var usedOrdererScheme = defaultOrdererScheme func SetOrderScheme(t MPTWitnessType) { usedOrdererScheme = t } -// HandleBlockResult only for backward compatibility -func HandleBlockResult(block *types.BlockResult) ([]*StorageTrace, error) { - return HandleBlockResultEx(block, usedOrdererScheme) +// HandleBlockTrace only for backward compatibility +func HandleBlockTrace(block *types.BlockTrace) ([]*StorageTrace, error) { + return HandleBlockTraceEx(block, usedOrdererScheme) } -func HandleBlockResultEx(block *types.BlockResult, ordererScheme MPTWitnessType) ([]*StorageTrace, error) { +func HandleBlockTraceEx(block *types.BlockTrace, ordererScheme MPTWitnessType) ([]*StorageTrace, error) { writer, err := NewZkTrieProofWriter(block.StorageTrace) if err != nil { @@ -767,8 +767,8 @@ func HandleBlockResultEx(block *types.BlockResult, ordererScheme MPTWitnessType) // notice some coinbase addr (like all zero) is in fact not exist and should not be update // TODO: not a good solution, just for patch ... - if coinbaseData := writer.tracingAccounts[block.BlockTrace.Coinbase.Address]; coinbaseData != nil { - od.absorb(block.BlockTrace.Coinbase) + if coinbaseData := writer.tracingAccounts[block.Coinbase.Address]; coinbaseData != nil { + od.absorb(block.Coinbase) } opDisp := od.end_absorb() @@ -791,13 +791,13 @@ func HandleBlockResultEx(block *types.BlockResult, ordererScheme MPTWitnessType) } -func FillBlockResultForMPTWitness(order MPTWitnessType, block *types.BlockResult) error { +func FillBlockTraceForMPTWitness(order MPTWitnessType, block *types.BlockTrace) error { if order == MPTWitnessNothing { return nil } - trace, err := HandleBlockResultEx(block, order) + trace, err := HandleBlockTraceEx(block, order) if err != nil { return err }