Skip to content

(depreciated) Improvment for the first phase of zktrie #100

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

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions CHANGELOGS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# CHANGELOGS

## 2022-05-04

Tag: None.

Current rev: a79e72f69701695185f2f71788d17998bdd5a5a8.

Based on https://github.com/ethereum/go-ethereum v1.10.13.

**Notable changes:**

### 1. Disable consensus and p2p service.

Related commits:

+ [a4eb31f4d2959a4ea97edf38aa13e4f87a81b1a1](https://github.com/scroll-tech/go-ethereum/commit/a4eb31f4d2959a4ea97edf38aa13e4f87a81b1a1) (PR [#8](https://github.com/scroll-tech/go-ethereum/pull/8))
+ [8b16b4cefae82f7d3f73a37df9e04d7d356a7a23](https://github.com/scroll-tech/go-ethereum/commit/8b16b4cefae82f7d3f73a37df9e04d7d356a7a23) (PR [#29](https://github.com/scroll-tech/go-ethereum/pull/29))

### 2. Add more detailed execution trace for zkevm-circuits proving.

Related commits:

+ [7745fd584018eb0ef63db1b17e27b968c9ae5dca](https://github.com/scroll-tech/go-ethereum/commit/7745fd584018eb0ef63db1b17e27b968c9ae5dca) (PR [#19](https://github.com/scroll-tech/go-ethereum/pull/19))
+ [69c291cf7ac43be7e10979457e4137af0c63ce0e](https://github.com/scroll-tech/go-ethereum/commit/69c291cf7ac43be7e10979457e4137af0c63ce0e) (PR [#20](https://github.com/scroll-tech/go-ethereum/pull/20))
+ [a5999cee905c16a24abb4580f43dca30ac9441d5](https://github.com/scroll-tech/go-ethereum/commit/a5999cee905c16a24abb4580f43dca30ac9441d5) (PR [#44](https://github.com/scroll-tech/go-ethereum/pull/44))
+ [13ea5c234b359e9eebe92b816ae6f02e876d6add](https://github.com/scroll-tech/go-ethereum/commit/13ea5c234b359e9eebe92b816ae6f02e876d6add) (PR [#46](https://github.com/scroll-tech/go-ethereum/pull/46))
+ [51281549254bfbfd3ad1dd87ffc9604c2b2e5a77](https://github.com/scroll-tech/go-ethereum/commit/51281549254bfbfd3ad1dd87ffc9604c2b2e5a77) (PR [#56](https://github.com/scroll-tech/go-ethereum/pull/56))
+ [8ccc10541dd49d52c70230cc0f8210d5da64248b](https://github.com/scroll-tech/go-ethereum/commit/8ccc10541dd49d52c70230cc0f8210d5da64248b) (PR [#58](https://github.com/scroll-tech/go-ethereum/pull/58))
+ [360115e61fb33aee60e02d7a907a2f0e79935ee2](https://github.com/scroll-tech/go-ethereum/commit/360115e61fb33aee60e02d7a907a2f0e79935ee2)
+ [9f1d8552e4d0abb30096ab3a75d9ab4616ac23b6](https://github.com/scroll-tech/go-ethereum/commit/9f1d8552e4d0abb30096ab3a75d9ab4616ac23b6) (PR [#66](https://github.com/scroll-tech/go-ethereum/pull/66))
+ [2329d324098f799e4d547d6ef14a77f52fc469cf](https://github.com/scroll-tech/go-ethereum/commit/2329d324098f799e4d547d6ef14a77f52fc469cf) (PR [#71](https://github.com/scroll-tech/go-ethereum/pull/71))

(

And some fixes regarding encoding:

+ [33fcd2bf6d4fa467bd8207bb1dc9c55bbed6be9b](https://github.com/scroll-tech/go-ethereum/commit/33fcd2bf6d4fa467bd8207bb1dc9c55bbed6be9b) (PR [#72](https://github.com/scroll-tech/go-ethereum/pull/72))
+ [3d3c9d3edff7cc6c3445f4fc9cf072df7853ee7d](https://github.com/scroll-tech/go-ethereum/commit/3d3c9d3edff7cc6c3445f4fc9cf072df7853ee7d) (PR [#74](https://github.com/scroll-tech/go-ethereum/pull/74))
+ [06190d0642afe076b257d67ad40b6b85e0a6e087](https://github.com/scroll-tech/go-ethereum/commit/06190d0642afe076b257d67ad40b6b85e0a6e087) (PR [#75](https://github.com/scroll-tech/go-ethereum/pull/75))

)


### 3. Optimization to reduce GC pressure

Related commits:

+ [09a31ccc66bcf676f71451bb1f3fde2e44849da3](https://github.com/scroll-tech/go-ethereum/commit/09a31ccc66bcf676f71451bb1f3fde2e44849da3) (PR [#43](https://github.com/scroll-tech/go-ethereum/pull/43))
+ [a79e72f69701695185f2f71788d17998bdd5a5a8](https://github.com/scroll-tech/go-ethereum/commit/a79e72f69701695185f2f71788d17998bdd5a5a8) (PR [#83](https://github.com/scroll-tech/go-ethereum/pull/83))

### 4. Misc

4.1 enable London fork rules from the beginning

Related commits:

+ [c180aa2e75d80dda90719b58690111b0d5b69f21](https://github.com/scroll-tech/go-ethereum/commit/c180aa2e75d80dda90719b58690111b0d5b69f21) (PR [#76](https://github.com/scroll-tech/go-ethereum/pull/76))
1 change: 0 additions & 1 deletion cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ var (
utils.MiningEnabledFlag,
utils.MinerThreadsFlag,
utils.MinerNotifyFlag,
utils.MinerSMTTraceFlag,
utils.LegacyMinerGasTargetFlag,
utils.MinerGasLimitFlag,
utils.MinerGasPriceFlag,
Expand Down
7 changes: 6 additions & 1 deletion cmd/geth/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ var AppHelpFlagGroups = []flags.FlagGroup{
utils.WhitelistFlag,
},
},
{
Name: "NOTRACE CLIENT",
Flags: []cli.Flag{
utils.TraceCacheLimit,
},
},
{
Name: "LIGHT CLIENT",
Flags: []cli.Flag{
Expand Down Expand Up @@ -183,7 +189,6 @@ var AppHelpFlagGroups = []flags.FlagGroup{
utils.MiningEnabledFlag,
utils.MinerThreadsFlag,
utils.MinerNotifyFlag,
utils.MinerSMTTraceFlag,
utils.MinerNotifyFullFlag,
utils.MinerGasPriceFlag,
utils.MinerGasLimitFlag,
Expand Down
21 changes: 14 additions & 7 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,12 @@ var (
Name: "override.arrowglacier",
Usage: "Manually specify Arrow Glacier fork-block, overriding the bundled setting",
}
// NoTrace settings
TraceCacheLimit = cli.IntFlag{
Name: "trace.limit",
Usage: "Handle the latest several blockResults",
Value: ethconfig.Defaults.TraceCacheLimit,
}
// Light server and client settings
LightServeFlag = cli.IntFlag{
Name: "light.serve",
Expand Down Expand Up @@ -433,10 +439,6 @@ var (
Name: "mine",
Usage: "Enable mining",
}
MinerSMTTraceFlag = cli.BoolFlag{
Name: "miner.SMTTrace",
Usage: "Enable worker to output SMTTrace in blockResult",
}
MinerThreadsFlag = cli.IntFlag{
Name: "miner.threads",
Usage: "Number of CPU threads to use for mining",
Expand Down Expand Up @@ -1098,6 +1100,13 @@ func MakeAddress(ks *keystore.KeyStore, account string) (accounts.Account, error
return accs[index], nil
}

func setTrace(ctx *cli.Context, cfg *ethconfig.Config) {
// NoTrace flag
if ctx.GlobalIsSet(TraceCacheLimit.Name) {
cfg.TraceCacheLimit = ctx.GlobalInt(TraceCacheLimit.Name)
}
}

// setEtherbase retrieves the etherbase either from the directly specified
// command line flags or from the keystore if CLI indexed.
func setEtherbase(ctx *cli.Context, ks *keystore.KeyStore, cfg *ethconfig.Config) {
Expand Down Expand Up @@ -1403,9 +1412,6 @@ func setMiner(ctx *cli.Context, cfg *miner.Config) {
if ctx.GlobalIsSet(MinerNoVerifyFlag.Name) {
cfg.Noverify = ctx.GlobalBool(MinerNoVerifyFlag.Name)
}
if ctx.GlobalIsSet(MinerSMTTraceFlag.Name) {
cfg.SMTTrace = ctx.GlobalBool(MinerSMTTraceFlag.Name)
}
if ctx.GlobalIsSet(LegacyMinerGasTargetFlag.Name) {
log.Warn("The generic --miner.gastarget flag is deprecated and will be removed in the future!")
}
Expand Down Expand Up @@ -1492,6 +1498,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if keystores := stack.AccountManager().Backends(keystore.KeyStoreType); len(keystores) > 0 {
ks = keystores[0].(*keystore.KeyStore)
}
setTrace(ctx, cfg)
setEtherbase(ctx, ks, cfg)
setGPO(ctx, &cfg.GPO, ctx.GlobalString(SyncModeFlag.Name) == "light")
setTxPool(ctx, &cfg.TxPool)
Expand Down
107 changes: 42 additions & 65 deletions core/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,20 @@ type CacheConfig struct {
TrieTimeLimit time.Duration // Time limit after which to flush the current in-memory trie to disk
SnapshotLimit int // Memory allowance (MB) to use for caching snapshot entries in memory
Preimages bool // Whether to store preimage of trie key to the disk
TraceCacheLimit int

SnapshotWait bool // Wait for snapshot construction on startup. TODO(karalabe): This is a dirty hack for testing, nuke it
}

// defaultCacheConfig are the default caching values if none are specified by the
// user (also used during testing).
var defaultCacheConfig = &CacheConfig{
TrieCleanLimit: 256,
TrieDirtyLimit: 256,
TrieTimeLimit: 5 * time.Minute,
SnapshotLimit: 256,
SnapshotWait: true,
TrieCleanLimit: 256,
TrieDirtyLimit: 256,
TrieTimeLimit: 5 * time.Minute,
SnapshotLimit: 256,
SnapshotWait: true,
TraceCacheLimit: 32,
}

// BlockChain represents the canonical chain given a database with a genesis
Expand Down Expand Up @@ -231,6 +233,9 @@ func NewBlockChain(db ethdb.Database, cacheConfig *CacheConfig, chainConfig *par
txLookupCache, _ := lru.New(txLookupCacheLimit)
futureBlocks, _ := lru.New(maxFutureBlocks)
blockResultCache, _ := lru.New(blockResultCacheLimit)
if cacheConfig.TraceCacheLimit != 0 {
blockResultCache, _ = lru.New(cacheConfig.TraceCacheLimit)
}

bc := &BlockChain{
chainConfig: chainConfig,
Expand Down Expand Up @@ -1189,7 +1194,7 @@ 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, evmTraces []*types.ExecutionResult, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, evmTraces *types.EvmTxTraces, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
if !bc.chainmu.TryLock() {
return NonStatTy, errInsertionInterrupted
}
Expand All @@ -1199,7 +1204,7 @@ func (bc *BlockChain) WriteBlockWithState(block *types.Block, receipts []*types.

// 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, evmTraces []*types.ExecutionResult, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.Receipt, logs []*types.Log, evmTraces *types.EvmTxTraces, state *state.StateDB, emitHeadEvent bool) (status WriteStatus, err error) {
if bc.insertStopped() {
return NonStatTy, errInsertionInterrupted
}
Expand Down Expand Up @@ -1347,43 +1352,48 @@ func (bc *BlockChain) writeBlockWithState(block *types.Block, receipts []*types.
}

// Fill blockResult content
func (bc *BlockChain) writeBlockResult(state *state.StateDB, block *types.Block, evmTraces []*types.ExecutionResult) *types.BlockResult {
func (bc *BlockChain) writeBlockResult(state *state.StateDB, block *types.Block, evmTraces *types.EvmTxTraces) *types.BlockResult {
blockResult := &types.BlockResult{
ExecutionResults: evmTraces,
ExecutionResults: evmTraces.TxResults,
StorageTrace: evmTraces.Storage,
}
coinbase := types.AccountProofWrapper{
coinbase := types.AccountWrapper{
Address: block.Coinbase(),
Nonce: state.GetNonce(block.Coinbase()),
Balance: (*hexutil.Big)(state.GetBalance(block.Coinbase())),
CodeHash: state.GetCodeHash(block.Coinbase()),
}
// Get coinbase address's account proof.
proof, err := state.GetProof(block.Coinbase())
if err != nil {
log.Error("Failed to get proof", "blockNumber", block.NumberU64(), "address", block.Coinbase().String(), "err", err)
} else {
coinbase.Proof = make([]string, len(proof))
for i := range proof {
coinbase.Proof[i] = hexutil.Encode(proof[i])
}
}

blockResult.BlockTrace = types.NewTraceBlock(bc.chainConfig, block, &coinbase)
blockResult.StorageTrace.RootAfter = state.GetRootHash()
for i, tx := range block.Transactions() {
evmTrace := blockResult.ExecutionResults[i]
from := evmTrace.Sender.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))
for i := range proof {
evmTrace.Sender.Proof[i] = hexutil.Encode(proof[i])
/*
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.From.Proof = make([]string, len(proof))
for i := range proof {
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])
}
}
*/
// Contract is called
if len(tx.Data()) != 0 && tx.To() != nil {
evmTrace.ByteCode = hexutil.Encode(state.GetCode(*tx.To()))
Expand All @@ -1393,41 +1403,8 @@ func (bc *BlockChain) writeBlockResult(state *state.StateDB, block *types.Block,
} else if tx.To() == nil { // Contract is created.
evmTrace.ByteCode = hexutil.Encode(tx.Data())
}

if evmTrace.Storage == nil {
log.Info("no storage in trace")
} else if bc.chainConfig.Zktrie {

smtWriter, err := newZkTrieProofWriter(evmTrace.Storage)
var postTxStatus map[string]hexutil.Bytes
if err != nil {
log.Error("build smt writer fail", "error", err)
} else if evmTrace.Failed {
//only need to handle the status change of From addr
fromAddr := evmTrace.Sender.Address.String()
postTxStatus = map[string]hexutil.Bytes{
fromAddr: evmTrace.Storage.AccountsAfter[fromAddr],
}
} else if err = smtWriter.handleAccountCreate(evmTrace.Storage.AccountCreated); err != nil {
log.Error("handle contract create for SMT fail", "error", err)
} else if err = smtWriter.handleLogs(evmTrace.StructLogs); err != nil {
log.Error("handle logs for SMT fail", "error", err)
} else {
postTxStatus = evmTrace.Storage.AccountsAfter
}

if err == nil {
if err = smtWriter.handlePostTx(postTxStatus); err != nil {
log.Error("handle post tx account state for SMT fail", "error", err)
} else if err = smtWriter.txFinal(evmTrace.Storage.RootAfter); err != nil {
log.Error("final tx verify fail", "error", err)
} else {
log.Info("write SMTTrace", "tx", i, "records", len(smtWriter.outTrace))
evmTrace.Storage.SMTTrace = smtWriter.outTrace
}
}
}
}

return blockResult
}

Expand Down
Loading