Skip to content

Commit 7745fd5

Browse files
mask-ppicemelon0xmountaintop
authored
Extract execution trace from EVM execution and provide subscription API (ethereum#19)
* Create go.yml * Merge from zkrollup and fix conflict * go mod tidy * fix worker_test test case * fix worker_test test case * Delete go.yml * add log content, enable memory trace * add tracer pool handler * Add comments and format code * add evmTrace subscribe api * Move the evmTrace struct. * Fix miner bug. * upgrade evmTrace api * fix bug about evmTracesByHash api * Fix the bug about block.timestamp and remove unnecessary copy. * Update eth/filters/api.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Upgrade comments. * Delete useless code in test file * Update miner/worker.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Change the return result to BlockResult. * Change return type. * Change blockResult to blockResults. * Add ReturnValue. * Update core/rawdb/l2trace.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Update core/rawdb/l2trace.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Update core/types/l2trace.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Add indent to the comment and rm json encoding flag. * Rm json encoding flag. * Update core/rawdb/l2trace.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Rm json encoding flag. * Update ethclient/ethclient.go Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> * Update eth/filters/api.go Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> * Use as the blockResult prefix flag. * Update eth/filters/filter_system.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Update eth/filters/filter_system.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Update ethclient/ethclient.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> * Update eth/filters/api.go Co-authored-by: Haichen Shen <shenhaichen@gmail.com> Co-authored-by: Haichen Shen <shenhaichen@gmail.com> Co-authored-by: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com>
1 parent b4d6088 commit 7745fd5

File tree

21 files changed

+770
-108
lines changed

21 files changed

+770
-108
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,4 @@ profile.cov
4747
/dashboard/assets/package-lock.json
4848

4949
**/yarn-error.log
50+

core/blockchain.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1182,17 +1182,17 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
11821182
}
11831183

11841184
// WriteBlockWithState writes the block and all associated state to the database.
1185-
func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
1185+
func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, blockResult *types.BlockResult, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
11861186
if !bc.chainmu.TryLock() {
11871187
return NonStatTy, errInsertionInterrupted
11881188
}
11891189
defer bc.chainmu.Unlock()
1190-
return bc.writeBlockWithState(block, receipts, logs, state, emitHeadEvent)
1190+
return bc.writeBlockWithState(block, receipts, logs, blockResult, state, emitHeadEvent)
11911191
}
11921192

11931193
// writeBlockWithState writes the block and all associated state to the database,
11941194
// but is expects the chain mutex to be held.
1195-
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
1195+
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, blockResult *types.BlockResult, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
11961196
if bc.insertStopped() {
11971197
return NonStatTy, errInsertionInterrupted
11981198
}
@@ -1215,6 +1215,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
12151215
rawdb.WriteTd(blockBatch, block.Hash(), block.NumberU64(), externTd)
12161216
rawdb.WriteBlock(blockBatch, block)
12171217
rawdb.WriteReceipts(blockBatch, block.Hash(), block.NumberU64(), receipts)
1218+
rawdb.WriteBlockResult(blockBatch, block.Hash(), blockResult)
12181219
rawdb.WritePreimages(blockBatch, state.Preimages())
12191220
if err := blockBatch.Write(); err != nil {
12201221
log.Crit("Failed to write block into disk", "err", err)
@@ -1314,7 +1315,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
13141315
bc.futureBlocks.Remove(block.Hash())
13151316

13161317
if status == CanonStatTy {
1317-
bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs})
1318+
bc.chainFeed.Send(ChainEvent{Block: block, Hash: block.Hash(), Logs: logs, BlockResult: blockResult})
13181319
if len(logs) > 0 {
13191320
bc.logsFeed.Send(logs)
13201321
}
@@ -1644,7 +1645,8 @@ func (bc *BlockChain) insertChain(chain types.Blocks, verifySeals bool) (int, er
16441645

16451646
// Write the block to the chain and get the status.
16461647
substart = time.Now()
1647-
status, err := bc.writeBlockWithState(block, receipts, logs, statedb, false)
1648+
// EvmTraces is nil is safe because l2geth's p2p server is stoped and the code will not execute there.
1649+
status, err := bc.writeBlockWithState(block, receipts, logs, nil, statedb, false)
16481650
atomic.StoreUint32(&followupInterrupt, 1)
16491651
if err != nil {
16501652
return it.index, err

core/events.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ type NewMinedBlockEvent struct{ Block *types.Block }
3131
type RemovedLogsEvent struct{ Logs []*types.Log }
3232

3333
type ChainEvent struct {
34-
Block *types.Block
35-
Hash common.Hash
36-
Logs []*types.Log
34+
Block *types.Block
35+
Hash common.Hash
36+
Logs []*types.Log
37+
BlockResult *types.BlockResult
3738
}
3839

3940
type ChainSideEvent struct {

core/rawdb/l2trace.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package rawdb
2+
3+
import (
4+
"github.com/scroll-tech/go-ethereum/common"
5+
"github.com/scroll-tech/go-ethereum/core/types"
6+
"github.com/scroll-tech/go-ethereum/ethdb"
7+
"github.com/scroll-tech/go-ethereum/log"
8+
"github.com/scroll-tech/go-ethereum/rlp"
9+
)
10+
11+
// ReadBlockResult retrieves all data required by roller.
12+
func ReadBlockResult(db ethdb.Reader, hash common.Hash) *types.BlockResult {
13+
data, _ := db.Get(blockResultKey(hash))
14+
if len(data) == 0 {
15+
return nil
16+
}
17+
var blockResult types.BlockResult
18+
if err := rlp.DecodeBytes(data, &blockResult); err != nil {
19+
log.Error("Failed to decode BlockResult", "err", err)
20+
return nil
21+
}
22+
return &blockResult
23+
}
24+
25+
// WriteBlockResult stores blockResult into leveldb.
26+
func WriteBlockResult(db ethdb.KeyValueWriter, hash common.Hash, blockResult *types.BlockResult) {
27+
bytes, err := rlp.EncodeToBytes(blockResult)
28+
if err != nil {
29+
log.Crit("Failed to RLP encode BlockResult", "err", err)
30+
}
31+
db.Put(blockResultKey(hash), bytes)
32+
}
33+
34+
// DeleteBlockResult removes blockResult with a block hash.
35+
func DeleteBlockResult(db ethdb.KeyValueWriter, hash common.Hash) {
36+
if err := db.Delete(blockResultKey(hash)); err != nil {
37+
log.Crit("Failed to delete BlockResult", "err", err)
38+
}
39+
}

0 commit comments

Comments
 (0)