From 8a78c736a5e13cc020252a43af409c592b3cbca8 Mon Sep 17 00:00:00 2001 From: HAOYUatHZ <37070449+HAOYUatHZ@users.noreply.github.com> Date: Thu, 16 Feb 2023 10:18:47 +0800 Subject: [PATCH] feat(trace): add withdraw trie root (#217) * init rollup/rcfg/config.go minor * update traces struct * init withdraw_trie.go * finish * fix `L2MessageQueueAddress` * refactor(rollup): add `UsingScroll` into `ChainConfig` (#218) * update comments * update comments --- core/types/l2trace.go | 1 + eth/tracers/api_blocktrace.go | 6 ++++++ params/config.go | 10 +++++++--- rollup/rcfg/config.go | 18 ++++++++++++++++++ rollup/withdrawtrie/withdraw_trie.go | 18 ++++++++++++++++++ 5 files changed, 50 insertions(+), 3 deletions(-) create mode 100644 rollup/rcfg/config.go create mode 100644 rollup/withdrawtrie/withdraw_trie.go diff --git a/core/types/l2trace.go b/core/types/l2trace.go index dacc9ac8de80..727352ad6476 100644 --- a/core/types/l2trace.go +++ b/core/types/l2trace.go @@ -31,6 +31,7 @@ type BlockTrace struct { StorageTrace *StorageTrace `json:"storageTrace"` ExecutionResults []*ExecutionResult `json:"executionResults"` MPTWitness *json.RawMessage `json:"mptwitness,omitempty"` + WithdrawTrieRoot common.Hash `json:"withdraw_trie_root,omitempty"` } // StorageTrace stores proofs of storage needed by storage circuit diff --git a/eth/tracers/api_blocktrace.go b/eth/tracers/api_blocktrace.go index 0eed2d0d1341..19c4f1840696 100644 --- a/eth/tracers/api_blocktrace.go +++ b/eth/tracers/api_blocktrace.go @@ -14,6 +14,8 @@ import ( "github.com/scroll-tech/go-ethereum/core/types" "github.com/scroll-tech/go-ethereum/core/vm" "github.com/scroll-tech/go-ethereum/log" + "github.com/scroll-tech/go-ethereum/rollup/rcfg" + "github.com/scroll-tech/go-ethereum/rollup/withdrawtrie" "github.com/scroll-tech/go-ethereum/rpc" "github.com/scroll-tech/go-ethereum/trie/zkproof" ) @@ -384,5 +386,9 @@ func (api *API) fillBlockTrace(env *traceEnv, block *types.Block) (*types.BlockT } } + if api.backend.ChainConfig().UsingScroll { + blockTrace.WithdrawTrieRoot = withdrawtrie.ReadWTRSlot(rcfg.L2MessageQueueAddress, env.state) + } + return blockTrace, nil } diff --git a/params/config.go b/params/config.go index 65a0ae85362a..d99f3053813a 100644 --- a/params/config.go +++ b/params/config.go @@ -258,16 +258,16 @@ var ( // // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. - AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, false, nil, true, true, nil} + AllEthashProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, false, nil, true, true, nil, true} // AllCliqueProtocolChanges contains every protocol change (EIPs) introduced // and accepted by the Ethereum core developers into the Clique consensus. // // This configuration is intentionally not using keyed fields to force anyone // adding flags to the config to also have to set these fields. - AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, false, nil, true, true, nil} + AllCliqueProtocolChanges = &ChainConfig{big.NewInt(1337), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, nil, nil, &CliqueConfig{Period: 0, Epoch: 30000}, false, nil, true, true, nil, true} - TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, false, &common.Address{123}, true, true, nil} + TestChainConfig = &ChainConfig{big.NewInt(1), big.NewInt(0), nil, false, big.NewInt(0), common.Hash{}, big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), big.NewInt(0), nil, new(EthashConfig), nil, false, &common.Address{123}, true, true, nil, true} TestRules = TestChainConfig.Rules(new(big.Int)) ) @@ -370,6 +370,10 @@ type ChainConfig struct { // Scroll genesis extension: Maximum number of transactions per block [optional] MaxTxPerBlock *int `json:"maxTxPerBlock,omitempty"` + + // Scroll genesis extension: enable scroll rollup-related traces & state transition + // TODO: merge with these config: Zktrie, FeeVaultAddress, EnableEIP2718, EnableEIP1559 & MaxTxPerBlock + UsingScroll bool `json:"usingScroll,omitempty"` } // EthashConfig is the consensus engine configs for proof-of-work based sealing. diff --git a/rollup/rcfg/config.go b/rollup/rcfg/config.go new file mode 100644 index 000000000000..74ed809efcd9 --- /dev/null +++ b/rollup/rcfg/config.go @@ -0,0 +1,18 @@ +package rcfg + +import ( + "math/big" + + "github.com/scroll-tech/go-ethereum/common" +) + +// TODO: +// verify in consensus layer when decentralizing sequencer + +var ( + // L2MessageQueueAddress is the address of the L2MessageQueue + // predeploy + // see contracts/src/L2/predeploys/L2MessageQueue.sol + L2MessageQueueAddress = common.HexToAddress("0x5300000000000000000000000000000000000000") + WithdrawTrieRootSlot = common.BigToHash(big.NewInt(0)) +) diff --git a/rollup/withdrawtrie/withdraw_trie.go b/rollup/withdrawtrie/withdraw_trie.go new file mode 100644 index 000000000000..85f034730958 --- /dev/null +++ b/rollup/withdrawtrie/withdraw_trie.go @@ -0,0 +1,18 @@ +package withdrawtrie + +import ( + "github.com/scroll-tech/go-ethereum/common" + "github.com/scroll-tech/go-ethereum/rollup/rcfg" +) + +// StateDB represents the StateDB interface +// required to get withdraw trie root +type StateDB interface { + GetState(common.Address, common.Hash) common.Hash +} + +// ReadWTRSlot reads WithdrawTrieRoot slot in L2MessageQueue predeploy, i.e., `messageRoot` +// in contracts/src/libraries/common/AppendOnlyMerkleTree.sol +func ReadWTRSlot(addr common.Address, state StateDB) common.Hash { + return state.GetState(addr, rcfg.WithdrawTrieRootSlot) +}