From 571dcad4be512225bb1209f8008a8577eab29ded Mon Sep 17 00:00:00 2001 From: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Date: Wed, 18 May 2022 05:43:43 +0800 Subject: [PATCH] feat: add `to` AccountProof to trace (#98) * init to * update * fix `to` address * fix * fix * refactor --- core/blockchain.go | 20 +++++++++++++++++--- core/types/l2trace.go | 4 +++- miner/worker.go | 14 +++++++++++++- 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/core/blockchain.go b/core/blockchain.go index 9068a43248a7..4dccd5d5c03a 100644 --- a/core/blockchain.go +++ b/core/blockchain.go @@ -1375,16 +1375,30 @@ func (bc *BlockChain) writeBlockResult(state *state.StateDB, block *types.Block, blockResult.BlockTrace = types.NewTraceBlock(bc.chainConfig, block, &coinbase) for i, tx := range block.Transactions() { evmTrace := blockResult.ExecutionResults[i] - from := evmTrace.Sender.Address + from := evmTrace.From.Address // Get proof proof, err := state.GetProof(from) if err != nil { log.Error("Failed to get proof", "blockNumber", block.NumberU64(), "address", from.String(), "err", err) } else { - evmTrace.Sender.Proof = make([]string, len(proof)) + evmTrace.From.Proof = make([]string, len(proof)) for i := range proof { - evmTrace.Sender.Proof[i] = hexutil.Encode(proof[i]) + evmTrace.From.Proof[i] = hexutil.Encode(proof[i]) + } + } + + if evmTrace.To != nil { + to := evmTrace.To.Address + // Get proof + proof, err = state.GetProof(to) + if err != nil { + log.Error("Failed to get proof", "blockNumber", block.NumberU64(), "address", to.String(), "err", err) + } else { + evmTrace.To.Proof = make([]string, len(proof)) + for i := range proof { + evmTrace.To.Proof[i] = hexutil.Encode(proof[i]) + } } } diff --git a/core/types/l2trace.go b/core/types/l2trace.go index 2f2be6aa3ed0..135d4542ba68 100644 --- a/core/types/l2trace.go +++ b/core/types/l2trace.go @@ -19,7 +19,9 @@ type ExecutionResult struct { Failed bool `json:"failed"` ReturnValue string `json:"returnValue,omitempty"` // Sender's account proof. - Sender *AccountProofWrapper `json:"sender,omitempty"` + From *AccountProofWrapper `json:"from,omitempty"` + // Receiver's account proof. + To *AccountProofWrapper `json:"to,omitempty"` // It's exist only when tx is a contract call. CodeHash *common.Hash `json:"codeHash,omitempty"` // If it is a contract call, the contract code is returned. diff --git a/miner/worker.go b/miner/worker.go index 775fe7c117ab..feabe8604743 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -794,6 +794,17 @@ func (w *worker) commitTransaction(tx *types.Transaction, coinbase common.Addres Balance: (*hexutil.Big)(w.current.state.GetBalance(from)), CodeHash: w.current.state.GetCodeHash(from), } + // Get receiver's address. + var receiver *types.AccountProofWrapper + if tx.To() != nil { + to := *tx.To() + receiver = &types.AccountProofWrapper{ + Address: to, + Nonce: w.current.state.GetNonce(to), + Balance: (*hexutil.Big)(w.current.state.GetBalance(to)), + CodeHash: w.current.state.GetCodeHash(to), + } + } receipt, err := core.ApplyTransaction(w.chainConfig, w.chain, &coinbase, w.current.gasPool, w.current.state, w.current.header, tx, &w.current.header.GasUsed, *w.chain.GetVMConfig()) if err != nil { @@ -805,7 +816,8 @@ func (w *worker) commitTransaction(tx *types.Transaction, coinbase common.Addres w.current.receipts = append(w.current.receipts, receipt) w.current.executionResults = append(w.current.executionResults, &types.ExecutionResult{ Gas: receipt.GasUsed, - Sender: sender, + From: sender, + To: receiver, Failed: receipt.Status != types.ReceiptStatusSuccessful, ReturnValue: fmt.Sprintf("%x", receipt.ReturnValue), StructLogs: vm.FormatLogs(tracer.StructLogs()),