Skip to content

Commit

Permalink
Extract execution trace from EVM execution and provide subscription A…
Browse files Browse the repository at this point in the history
…PI (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 <[email protected]>

* Upgrade comments.

* Delete useless code in test file

* Update miner/worker.go

Co-authored-by: Haichen Shen <[email protected]>

* 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 <[email protected]>

* Update core/rawdb/l2trace.go

Co-authored-by: Haichen Shen <[email protected]>

* Update core/types/l2trace.go

Co-authored-by: Haichen Shen <[email protected]>

* Add indent to the comment and rm json encoding flag.

* Rm json encoding flag.

* Update core/rawdb/l2trace.go

Co-authored-by: Haichen Shen <[email protected]>

* Rm json encoding flag.

* Update ethclient/ethclient.go

Co-authored-by: HAOYUatHZ <[email protected]>

* Update eth/filters/api.go

Co-authored-by: HAOYUatHZ <[email protected]>

* Use  as the blockResult prefix flag.

* Update eth/filters/filter_system.go

Co-authored-by: Haichen Shen <[email protected]>

* Update eth/filters/filter_system.go

Co-authored-by: Haichen Shen <[email protected]>

* Update ethclient/ethclient.go

Co-authored-by: Haichen Shen <[email protected]>

* Update eth/filters/api.go

Co-authored-by: Haichen Shen <[email protected]>

Co-authored-by: Haichen Shen <[email protected]>
Co-authored-by: HAOYUatHZ <[email protected]>
  • Loading branch information
3 people authored Jan 23, 2022
1 parent b4d6088 commit 7745fd5
Show file tree
Hide file tree
Showing 21 changed files with 770 additions and 108 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ profile.cov
/dashboard/assets/package-lock.json

**/yarn-error.log

12 changes: 7 additions & 5 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -1182,17 +1182,17 @@ func (bc *BlockChain) writeKnownBlock(block *types.Block) error {
}

// WriteBlockWithState writes the block and all associated state to the database.
func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
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) {
if !bc.chainmu.TryLock() {
return NonStatTy, errInsertionInterrupted
}
defer bc.chainmu.Unlock()
return bc.writeBlockWithState(block, receipts, logs, state, emitHeadEvent)
return bc.writeBlockWithState(block, receipts, logs, blockResult, state, emitHeadEvent)
}

// writeBlockWithState writes the block and all associated state to the database,
// but is expects the chain mutex to be held.
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
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) {
if bc.insertStopped() {
return NonStatTy, errInsertionInterrupted
}
Expand All @@ -1215,6 +1215,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
rawdb.WriteTd(blockBatch, block.Hash(), block.NumberU64(), externTd)
rawdb.WriteBlock(blockBatch, block)
rawdb.WriteReceipts(blockBatch, block.Hash(), block.NumberU64(), receipts)
rawdb.WriteBlockResult(blockBatch, block.Hash(), blockResult)
rawdb.WritePreimages(blockBatch, state.Preimages())
if err := blockBatch.Write(); err != nil {
log.Crit("Failed to write block into disk", "err", err)
Expand Down Expand Up @@ -1314,7 +1315,7 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
bc.futureBlocks.Remove(block.Hash())

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

// Write the block to the chain and get the status.
substart = time.Now()
status, err := bc.writeBlockWithState(block, receipts, logs, statedb, false)
// EvmTraces is nil is safe because l2geth's p2p server is stoped and the code will not execute there.
status, err := bc.writeBlockWithState(block, receipts, logs, nil, statedb, false)
atomic.StoreUint32(&followupInterrupt, 1)
if err != nil {
return it.index, err
Expand Down
7 changes: 4 additions & 3 deletions core/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ type NewMinedBlockEvent struct{ Block *types.Block }
type RemovedLogsEvent struct{ Logs []*types.Log }

type ChainEvent struct {
Block *types.Block
Hash common.Hash
Logs []*types.Log
Block *types.Block
Hash common.Hash
Logs []*types.Log
BlockResult *types.BlockResult
}

type ChainSideEvent struct {
Expand Down
39 changes: 39 additions & 0 deletions core/rawdb/l2trace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package rawdb

import (
"github.com/scroll-tech/go-ethereum/common"
"github.com/scroll-tech/go-ethereum/core/types"
"github.com/scroll-tech/go-ethereum/ethdb"
"github.com/scroll-tech/go-ethereum/log"
"github.com/scroll-tech/go-ethereum/rlp"
)

// ReadBlockResult retrieves all data required by roller.
func ReadBlockResult(db ethdb.Reader, hash common.Hash) *types.BlockResult {
data, _ := db.Get(blockResultKey(hash))
if len(data) == 0 {
return nil
}
var blockResult types.BlockResult
if err := rlp.DecodeBytes(data, &blockResult); err != nil {
log.Error("Failed to decode BlockResult", "err", err)
return nil
}
return &blockResult
}

// WriteBlockResult stores blockResult into leveldb.
func WriteBlockResult(db ethdb.KeyValueWriter, hash common.Hash, blockResult *types.BlockResult) {
bytes, err := rlp.EncodeToBytes(blockResult)
if err != nil {
log.Crit("Failed to RLP encode BlockResult", "err", err)
}
db.Put(blockResultKey(hash), bytes)
}

// DeleteBlockResult removes blockResult with a block hash.
func DeleteBlockResult(db ethdb.KeyValueWriter, hash common.Hash) {
if err := db.Delete(blockResultKey(hash)); err != nil {
log.Crit("Failed to delete BlockResult", "err", err)
}
}
Loading

0 comments on commit 7745fd5

Please sign in to comment.