Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/zkevm' into zkevm-2.60
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Jul 23, 2024
2 parents 8ffbaba + 859d373 commit 269c9ec
Show file tree
Hide file tree
Showing 69 changed files with 1,047 additions and 969 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ Resource Utilisation config:

Useful config entries:
- `zkevm.sync-limit`: This will ensure the network only syncs to a given block height.
- `debug.timers`: This will enable debug timers in the logs to help with performance tuning. Recording timings of witness generation, etc. at INFO level.

***

Expand Down
1 change: 1 addition & 0 deletions cmd/hack/hack.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ func readAccountAtVersion(chaindata string, account string, block uint64) error
defer tx.Rollback()

ps := state.NewPlainState(tx, block, nil)
defer ps.Close()

addr := libcommon.HexToAddress(account)
acc, err := ps.ReadAccountData(addr)
Expand Down
7 changes: 5 additions & 2 deletions cmd/hack/rpc_checker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"io/ioutil"
"log"
"net/http"
"strconv"
"sync"
)

Expand Down Expand Up @@ -65,7 +66,7 @@ func getBlockHash(nodeURL string, blockNumber int, wg *sync.WaitGroup, resultCha
request := JSONRPCRequest{
Jsonrpc: "2.0",
Method: "eth_getBlockByNumber",
Params: []interface{}{fmt.Sprintf("0x%x", blockNumber), true},
Params: []interface{}{"0x" + strconv.FormatInt(int64(blockNumber), 16), true},
ID: 1,
}

Expand Down Expand Up @@ -183,7 +184,9 @@ func compareReceipts(nodeURL1, nodeURL2 string, txHashes []string) (bool, error)
return true, nil
}

/* EXAMPLE USAGE:
/*
EXAMPLE USAGE:
go run cmd/hack/rpc_checker/main.go -node1=http://your-node1-url -node2=http://your-node2-url -fromBlock=3000000 -step=1000 -compare-receipts=true
*/
func main() {
Expand Down
7 changes: 4 additions & 3 deletions cmd/rpcdaemon/cli/httpcfg/http_cfg.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ type HttpCfg struct {
RPCSlowLogThreshold time.Duration

// zkevm
DataStreamPort int
DataStreamHost string
L2RpcUrl string
DataStreamPort int
DataStreamHost string
DataStreamWriteTimeout time.Duration
L2RpcUrl string
}
6 changes: 4 additions & 2 deletions cmd/state/commands/check_change_sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,10 @@ func CheckChangeSets(ctx context.Context, genesis *types.Genesis, blockNum uint6

engine := initConsensusEngine(ctx, chainConfig, allSnapshots, blockReader, logger)

for !interrupt {
reader := state.NewPlainState(historyTx, blockNum, systemcontracts.SystemContractCodeLookup[chainConfig.ChainName])
defer reader.Close()

for !interrupt {
if blockNum > execAt {
log.Warn(fmt.Sprintf("Force stop: because trying to check blockNumber=%d higher than Exec stage=%d", blockNum, execAt))
break
Expand All @@ -148,7 +150,7 @@ func CheckChangeSets(ctx context.Context, genesis *types.Genesis, blockNum uint6
if b == nil {
break
}
reader := state.NewPlainState(historyTx, blockNum, systemcontracts.SystemContractCodeLookup[chainConfig.ChainName])
reader.SetBlockNr(blockNum)
//reader.SetTrace(blockNum == uint64(block))
intraBlockState := state.New(reader)
csw := state.NewChangeSetWriterPlain(nil /* db */, blockNum)
Expand Down
5 changes: 5 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,11 @@ var (
Usage: "Define the host used for the zkevm data stream",
Value: "",
}
DataStreamWriteTimeout = cli.DurationFlag{
Name: "zkevm.data-stream-writeTimeout",
Usage: "Define the TCP write timeout when sending data to a datastream client",
Value: 5 * time.Second,
}
Limbo = cli.BoolFlag{
Name: "zkevm.limbo",
Usage: "Enable limbo processing on batches that failed verification",
Expand Down
2 changes: 1 addition & 1 deletion core/blockchain_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func ExecuteBlockEphemerallyZk(
return nil, err
}

receipt, execResult, err := ApplyTransaction_zkevm(chainConfig, engine, evm, gp, ibs, state.NewNoopWriter(), header, tx, usedGas, effectiveGasPricePercentage)
receipt, execResult, err := ApplyTransaction_zkevm(chainConfig, engine, evm, gp, ibs, state.NewNoopWriter(), header, tx, usedGas, effectiveGasPricePercentage, true)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion core/genesis_write.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func GenesisToBlock(g *types.Genesis, tmpDir string, logger log.Logger) (*types.

var err error
sparseDb := eridb.NewMemDb()
sparseTree := smt.NewSMT(sparseDb)
sparseTree := smt.NewSMT(sparseDb, false)
go func() { // we may run inside write tx, can't open 2nd write tx in same goroutine
// TODO(yperbasis): use memdb.MemoryMutation instead
defer wg.Done()
Expand Down
85 changes: 85 additions & 0 deletions core/state/plain_readonly_storage_zkevm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
// Copyright 2019 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.

package state

import (
"fmt"

libcommon "github.com/ledgerwatch/erigon-lib/common"
"github.com/ledgerwatch/erigon-lib/kv"
"github.com/ledgerwatch/erigon-lib/kv/dbutils"
"github.com/ledgerwatch/erigon-lib/kv/kvcfg"

"github.com/ledgerwatch/erigon/core/state/historyv2read"
)

// State at the beginning of blockNr
type PlainStateReadAccountStorage struct {
storageHistoryC kv.Cursor
storageChangesC kv.CursorDupSort
tx kv.Tx
blockNr uint64
trace bool
}

func NewPlainStateReadAccountStorage(tx kv.Tx, blockNr uint64) *PlainStateReadAccountStorage {
histV3, _ := kvcfg.HistoryV3.Enabled(tx)
if histV3 {
panic("Please use HistoryStateReaderV3 with HistoryV3")
}
ps := &PlainStateReadAccountStorage{
tx: tx,
blockNr: blockNr,
}

c2, _ := tx.Cursor(kv.E2StorageHistory)
c4, _ := tx.CursorDupSort(kv.StorageChangeSet)

ps.storageHistoryC = c2
ps.storageChangesC = c4
return ps
}

func (s *PlainStateReadAccountStorage) Close() {
s.storageHistoryC.Close()
s.storageChangesC.Close()
}

func (s *PlainStateReadAccountStorage) SetBlockNr(blockNr uint64) {
s.blockNr = blockNr
s.storageChangesC.First()
s.storageHistoryC.First()
}

func (s *PlainStateReadAccountStorage) SetTrace(trace bool) {
s.trace = trace
}

func (s *PlainStateReadAccountStorage) ReadAccountStorage(address libcommon.Address, incarnation uint64, key *libcommon.Hash) ([]byte, error) {
compositeKey := dbutils.PlainGenerateCompositeStorageKey(address.Bytes(), incarnation, key.Bytes())
enc, _, err := historyv2read.GetAsOf(s.tx, s.storageHistoryC, s.storageChangesC, true /* storage */, compositeKey, s.blockNr)
if err != nil {
return nil, err
}
if s.trace {
fmt.Printf("ReadAccountStorage [%x] [%x] => [%x]\n", address, *key, enc)
}
if len(enc) == 0 {
return nil, nil
}
return enc, nil
}
12 changes: 7 additions & 5 deletions core/state_processor_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func GetTxContext(config *chain.Config, engine consensus.EngineReader, ibs *stat
// and uses the input parameters for its environment. It returns the receipt
// for the transaction, gas used and an error if the transaction failed,
// indicating the block was invalid.
func ApplyMessageWithTxContext(msg types.Message, txContext evmtypes.TxContext, gp *GasPool, ibs *state.IntraBlockState, stateWriter state.StateWriter, blockNumber *big.Int, tx types.Transaction, usedGas *uint64, evm *vm.EVM) (*types.Receipt, *ExecutionResult, error) {
func ApplyMessageWithTxContext(msg types.Message, txContext evmtypes.TxContext, gp *GasPool, ibs *state.IntraBlockState, stateWriter state.StateWriter, blockNumber *big.Int, tx types.Transaction, usedGas *uint64, evm *vm.EVM, shouldFinalizeIbs bool) (*types.Receipt, *ExecutionResult, error) {
rules := evm.ChainRules()

if evm.Config().TraceJumpDest {
Expand All @@ -81,10 +81,11 @@ func ApplyMessageWithTxContext(msg types.Message, txContext evmtypes.TxContext,
}

// Update the state with pending changes
if err = ibs.FinalizeTx(rules, stateWriter); err != nil {
return nil, nil, err
if shouldFinalizeIbs {
if err = ibs.FinalizeTx(rules, stateWriter); err != nil {
return nil, nil, err
}
}

if usedGas != nil {
*usedGas += result.UsedGas
}
Expand Down Expand Up @@ -167,11 +168,12 @@ func ApplyTransaction_zkevm(
tx types.Transaction,
usedGas *uint64,
effectiveGasPricePercentage uint8,
shouldFinalizeIbs bool,
) (*types.Receipt, *ExecutionResult, error) {
// Create a new context to be used in the EVM environment
msg, txContext, err := GetTxContext(config, engine, ibs, header, tx, evm, effectiveGasPricePercentage)
if err != nil {
return nil, nil, err
}
return ApplyMessageWithTxContext(msg, txContext, gp, ibs, stateWriter, header.Number, tx, usedGas, evm)
return ApplyMessageWithTxContext(msg, txContext, gp, ibs, stateWriter, header.Number, tx, usedGas, evm, shouldFinalizeIbs)
}
2 changes: 1 addition & 1 deletion eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ func New(ctx context.Context, stack *node.Node, config *ethconfig.Config, logger
Outputs: nil,
}
// todo [zkevm] read the stream version from config and figure out what system id is used for
backend.dataStream, err = datastreamer.NewServer(uint16(httpCfg.DataStreamPort), uint8(backend.config.DatastreamVersion), 1, datastreamer.StreamType(1), file, logConfig)
backend.dataStream, err = datastreamer.NewServer(uint16(httpCfg.DataStreamPort), uint8(backend.config.DatastreamVersion), 1, datastreamer.StreamType(1), file, httpCfg.DataStreamWriteTimeout, logConfig)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions eth/ethconfig/config_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ type Zk struct {
DAUrl string
DataStreamHost string
DataStreamPort uint
DataStreamWriteTimeout time.Duration

RebuildTreeAfter uint64
IncrementTreeAlways bool
Expand Down
5 changes: 5 additions & 0 deletions eth/stagedsync/stage_execute_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,11 @@ func getExecRange(cfg ExecuteBlockCfg, tx kv.RwTx, stageProgress, toBlock uint64
return 0, 0, err
}

// skip if no progress
if prevStageProgress == 0 && toBlock == 0 {
return 0, 0, nil
}

to := prevStageProgress
if toBlock > 0 {
to = cmp.Min(prevStageProgress, toBlock)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ replace github.com/ledgerwatch/erigon-lib => ./erigon-lib

require (
gfx.cafe/util/go/generic v0.0.0-20230721185457-c559e86c829c
github.com/0xPolygonHermez/zkevm-data-streamer v0.2.2
github.com/0xPolygonHermez/zkevm-data-streamer v0.2.3-RC4
github.com/99designs/gqlgen v0.17.40
github.com/Giulio2002/bls v0.0.0-20240315151443-652e18a3d188
github.com/Masterminds/sprig/v3 v3.2.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ filippo.io/edwards25519 v1.0.0-rc.1/go.mod h1:N1IkdkCkiLB6tki+MYJoSx2JTY9NUlxZE7
gfx.cafe/util/go/generic v0.0.0-20230721185457-c559e86c829c h1:alCfDKmPC0EC0KGlZWrNF0hilVWBkzMz+aAYTJ/2hY4=
gfx.cafe/util/go/generic v0.0.0-20230721185457-c559e86c829c/go.mod h1:WvSX4JsCRBuIXj0FRBFX9YLg+2SoL3w8Ww19uZO9yNE=
git.apache.org/thrift.git v0.0.0-20180902110319-2566ecd5d999/go.mod h1:fPE2ZNJGynbRyZ4dJvy6G277gSllfV2HJqblrnkyeyg=
github.com/0xPolygonHermez/zkevm-data-streamer v0.2.2 h1:XRMTk+W6vtJVGVjuEznfWyNt7HkRkkuSmlN5Y6p60Sc=
github.com/0xPolygonHermez/zkevm-data-streamer v0.2.2/go.mod h1:0QkAXcFa92mFJrCbN3UPUJGJYes851yEgYHLONnaosE=
github.com/0xPolygonHermez/zkevm-data-streamer v0.2.3-RC4 h1:+4K+xSzv0ImbK30B/T9FauNTrTFUmWcNKYhIgwsE4C4=
github.com/0xPolygonHermez/zkevm-data-streamer v0.2.3-RC4/go.mod h1:0QkAXcFa92mFJrCbN3UPUJGJYes851yEgYHLONnaosE=
github.com/99designs/gqlgen v0.17.40 h1:/l8JcEVQ93wqIfmH9VS1jsAkwm6eAF1NwQn3N+SDqBY=
github.com/99designs/gqlgen v0.17.40/go.mod h1:b62q1USk82GYIVjC60h02YguAZLqYZtvWml8KkhJps4=
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
Expand Down
3 changes: 3 additions & 0 deletions hermezconfig-bali.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ txpool.disable: true
torrent.port: 42070
zkevm.datastream-version: 2

# debug.timers: true # Uncomment to enable timers

externalcl: true
http.api: [eth, debug, net, trace, web3, erigon, zkevm]
http.addr: 0.0.0.0
http.vhosts: any
Expand Down
3 changes: 3 additions & 0 deletions hermezconfig-cardona.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ txpool.disable: true
torrent.port: 42070
zkevm.datastream-version: 2

# debug.timers: true # Uncomment to enable timers

externalcl: true
http.api: [eth, debug, net, trace, web3, erigon, zkevm]
http.addr: 0.0.0.0
http.vhosts: any
Expand Down
3 changes: 3 additions & 0 deletions hermezconfig-mainnet.yaml.example
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ zkevm.l1-first-block: 16896700
zkevm.rpc-ratelimit: 250
zkevm.datastream-version: 2

# debug.timers: true # Uncomment to enable timers

externalcl: true
http.api: [eth, debug, net, trace, web3, erigon, zkevm]
http.addr: 0.0.0.0
http.vhosts: any
Expand Down
Loading

0 comments on commit 269c9ec

Please sign in to comment.