Skip to content

Commit

Permalink
Merge branch 'CDK-382-cdk-erigon-rpc-node-handle-rollbacks' into banana
Browse files Browse the repository at this point in the history
  • Loading branch information
cffls committed Aug 29, 2024
2 parents 04edbf9 + 456262d commit 497ec51
Show file tree
Hide file tree
Showing 15 changed files with 83 additions and 115 deletions.
4 changes: 1 addition & 3 deletions cmd/rpcdaemon/commands/eth_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,9 +422,7 @@ func newRPCTransaction(tx types.Transaction, blockHash common.Hash, blockNumber
case *types.LegacyTx:
chainId = types.DeriveChainId(&t.V)
// if a legacy transaction has an EIP-155 chain id, include it explicitly, otherwise chain id is not included
if !chainId.IsZero() {
result.ChainID = (*hexutil.Big)(chainId.ToBig())
}
result.ChainID = (*hexutil.Big)(chainId.ToBig())
result.GasPrice = (*hexutil.Big)(t.GasPrice.ToBig())
result.V = (*hexutil.Big)(t.V.ToBig())
result.R = (*hexutil.Big)(t.R.ToBig())
Expand Down
10 changes: 7 additions & 3 deletions cmd/rpcdaemon/commands/zkevm_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,10 @@ func (api *ZkEvmAPIImpl) GetBatchByNumber(ctx context.Context, batchNumber rpc.B
}
if _, ok := syncStatus.(bool); !ok {
bn := syncStatus.(map[string]interface{})["currentBlock"]
highestBatchNo, err = hermezDb.GetBatchNoByL2Block(uint64(bn.(hexutil.Uint64)))
if highestBatchNo, err = hermezDb.GetBatchNoByL2Block(uint64(bn.(hexutil.Uint64))); err != nil {
return nil, err
}

}
if batchNumber > rpc.BlockNumber(highestBatchNo) {
return nil, nil
Expand Down Expand Up @@ -1338,9 +1341,10 @@ func convertBlockToRpcBlock(
}

cid := tx.GetChainID()
if cid.Cmp(uint256.NewInt(0)) != 0 {
tran.ChainID = (*types.ArgBig)(cid.ToBig())
if cid == nil {
cid = uint256.NewInt(0)
}
tran.ChainID = (*types.ArgBig)(cid.ToBig())

t := types.TransactionOrHash{Tx: &tran}
result.Transactions = append(result.Transactions, t)
Expand Down
25 changes: 24 additions & 1 deletion core/rawdb/accessors_chain_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/gateway-fm/cdk-erigon-lib/kv"
"github.com/gateway-fm/cdk-erigon-lib/kv/kvcfg"
"github.com/ledgerwatch/erigon/common/dbutils"
"github.com/ledgerwatch/erigon/common/math"
"github.com/ledgerwatch/erigon/core/types"
ethTypes "github.com/ledgerwatch/erigon/core/types"
"github.com/ledgerwatch/erigon/rlp"
Expand Down Expand Up @@ -193,7 +194,29 @@ func ReadReceipts_zkEvm(db kv.Tx, block *types.Block, senders []libcommon.Addres

//[hack] there was a cumulativeGasUsed bug priod to forkid8, so we need to check for it
hermezDb := hermez_db.NewHermezDbReader(db)
forkid8BlockNum, _, _ := hermezDb.GetForkIdBlock(8)
forkBlocks, err := hermezDb.GetAllForkBlocks()
if err != nil {
log.Error("Failed to get fork blocks", "err", err, "stack", dbg.Stack())
return nil
}

forkid8BlockNum := uint64(0)
highestForkId := uint64(0)
for forkId, forkBlock := range forkBlocks {
if forkId > highestForkId {
highestForkId = forkId
}
if forkId == 8 {
forkid8BlockNum = forkBlock
break
}
}

// if we don't have forkid8 and highest saved is lower, then we are lower than forkid
// otherwise we are higher than forkid8 but don't have it saved so it should be treated as if it was 0
if forkid8BlockNum == 0 && highestForkId < 8 {
forkid8BlockNum = math.MaxUint64
}

if err := receipts.DeriveFields_zkEvm(forkid8BlockNum, block.Hash(), block.NumberU64(), block.Transactions(), senders); err != nil {
log.Error("Failed to derive block receipts fields", "hash", block.Hash(), "number", block.NumberU64(), "err", err, "stack", dbg.Stack())
Expand Down
1 change: 0 additions & 1 deletion core/state/intra_block_state_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type ReadOnlyHermezDb interface {
GetBatchGlobalExitRoots(fromBatchNum, toBatchNum uint64) (*[]dstypes.GerUpdate, error)
GetBlockGlobalExitRoot(l2BlockNo uint64) (libcommon.Hash, error)
GetBlockL1BlockHash(l2BlockNo uint64) (libcommon.Hash, error)
GetGerForL1BlockHash(l1BlockHash libcommon.Hash) (libcommon.Hash, error)
GetIntermediateTxStateRoot(blockNum uint64, txhash libcommon.Hash) (libcommon.Hash, error)
GetReusedL1InfoTreeIndex(blockNum uint64) (bool, error)
GetSequenceByBatchNo(batchNo uint64) (*zktypes.L1BatchInfo, error)
Expand Down
5 changes: 2 additions & 3 deletions core/types/receipt_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,8 @@ func (r Receipts) DeriveFields_zkEvm(forkId8BlockNum uint64, hash common.Hash, n
}
// The used gas can be calculated based on previous r
// [hack] there was a cumulativeGasUsed bug priod to forkid8, so we need to check for it
// if the block is before forkId8 or forkid8 is not even on yet
// comuluative is equal to gas used
if i == 0 || number < forkId8BlockNum || forkId8BlockNum == 0 {
// if the block is before forkId8 comuluative is equal to gas used
if i == 0 || number < forkId8BlockNum {
r[i].GasUsed = r[i].CumulativeGasUsed
} else {
r[i].GasUsed = r[i].CumulativeGasUsed - r[i-1].CumulativeGasUsed
Expand Down
5 changes: 3 additions & 2 deletions core/types/receipt_zkevm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@
package types

import (
"math"
"math/big"
"testing"

"github.com/holiman/uint256"
libcommon "github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/holiman/uint256"

"github.com/ledgerwatch/erigon/common/u256"
"github.com/ledgerwatch/erigon/crypto"
Expand Down Expand Up @@ -105,7 +106,7 @@ func TestDeriveFields_zkEvm_preForkId8(t *testing.T) {
hash := libcommon.BytesToHash([]byte{0x03, 0x14})

clearComputedFieldsOnReceipts(t, receipts)
if err := receipts.DeriveFields_zkEvm(0, hash, number.Uint64(), txs, []libcommon.Address{libcommon.BytesToAddress([]byte{0x0}), libcommon.BytesToAddress([]byte{0x0}), libcommon.BytesToAddress([]byte{0x0})}); err != nil {
if err := receipts.DeriveFields_zkEvm(math.MaxUint64, hash, number.Uint64(), txs, []libcommon.Address{libcommon.BytesToAddress([]byte{0x0}), libcommon.BytesToAddress([]byte{0x0}), libcommon.BytesToAddress([]byte{0x0})}); err != nil {
t.Fatalf("DeriveFields_zkEvm(...) = %v, want <nil>", err)
}
// Iterate over all the computed fields and check that they're correct
Expand Down
7 changes: 6 additions & 1 deletion core/vm/jump_table_zkevm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@ func newForkID4InstructionSet() JumpTable {
instructionSet[EXTCODEHASH].execute = opExtCodeHash_zkevm

// SELFDESTRUCT is replaces by SENDALL
instructionSet[SELFDESTRUCT].execute = opSendAll_zkevm
instructionSet[SELFDESTRUCT] = &operation{
execute: opSendAll_zkevm,
dynamicGas: gasSelfdestruct_zkevm,
numPop: 1,
numPush: 0,
}

validateAndFillMaxStack(&instructionSet)
return instructionSet
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ require (
github.com/emicklei/dot v1.0.0
github.com/emirpasic/gods v1.18.1
github.com/fjl/gencodec v0.0.0-20220412091415-8bb9e558978c
github.com/gateway-fm/cdk-erigon-lib v0.0.0-20240819202640-e541bb90668f
github.com/gateway-fm/cdk-erigon-lib v0.0.0-20240829075742-c69f9871e8c9
github.com/gateway-fm/vectorized-poseidon-gold v1.0.0
github.com/gballet/go-verkle v0.0.0-20221121182333-31427a1f2d35
github.com/go-stack/stack v1.8.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,8 @@ github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/garslo/gogen v0.0.0-20170307003452-d6ebae628c7c h1:uYNKzPntb8c6DKvP9EfrBjkLkU7pM4lM+uuHSIa8UtU=
github.com/garslo/gogen v0.0.0-20170307003452-d6ebae628c7c/go.mod h1:Q0X6pkwTILDlzrGEckF6HKjXe48EgsY/l7K7vhY4MW8=
github.com/gateway-fm/cdk-erigon-lib v0.0.0-20240819202640-e541bb90668f h1:QWDcgNitBfcFrNMZYdLMuSSSWBfA1tGpRIXd1CJuJ1g=
github.com/gateway-fm/cdk-erigon-lib v0.0.0-20240819202640-e541bb90668f/go.mod h1:Ky//z32wQOTY/drV858kr8dECBD9OBV9Xzy+aG6TxSk=
github.com/gateway-fm/cdk-erigon-lib v0.0.0-20240829075742-c69f9871e8c9 h1:GLP1WB+Mec7b7vO+4egFlE2zlpFJH9i0o4kW48WT2JI=
github.com/gateway-fm/cdk-erigon-lib v0.0.0-20240829075742-c69f9871e8c9/go.mod h1:Ky//z32wQOTY/drV858kr8dECBD9OBV9Xzy+aG6TxSk=
github.com/gateway-fm/vectorized-poseidon-gold v1.0.0 h1:Du0ZW+fkZhgRNGx/gAkHnMj3/Rl8uJkAEe+ZDPX3PDw=
github.com/gateway-fm/vectorized-poseidon-gold v1.0.0/go.mod h1:VLGQpyjrOg8+FugH/+d8tfYd/c3z4Xqa+zbUBITygaw=
github.com/gballet/go-verkle v0.0.0-20221121182333-31427a1f2d35 h1:I8QswD9gf3VEpr7bpepKKOm7ChxFITIG+oc1I5/S0no=
Expand Down
9 changes: 4 additions & 5 deletions turbo/adapter/ethapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ import (
"fmt"
"math/big"

"github.com/holiman/uint256"
libcommon "github.com/gateway-fm/cdk-erigon-lib/common"
"github.com/gateway-fm/cdk-erigon-lib/common/hexutility"
types2 "github.com/gateway-fm/cdk-erigon-lib/types"
"github.com/holiman/uint256"
"github.com/ledgerwatch/log/v3"

"github.com/ledgerwatch/erigon/accounts/abi"
Expand Down Expand Up @@ -414,10 +414,9 @@ func newRPCTransaction(tx types.Transaction, blockHash libcommon.Hash, blockNumb
switch t := tx.(type) {
case *types.LegacyTx:
chainId = types.DeriveChainId(&t.V)
// if a legacy transaction has an EIP-155 chain id, include it explicitly, otherwise chain id is not included
if !chainId.IsZero() {
result.ChainID = (*hexutil.Big)(chainId.ToBig())
}

result.ChainID = (*hexutil.Big)(chainId.ToBig())

result.GasPrice = (*hexutil.Big)(t.GasPrice.ToBig())
result.V = (*hexutil.Big)(t.V.ToBig())
result.R = (*hexutil.Big)(t.R.ToBig())
Expand Down
74 changes: 23 additions & 51 deletions zk/hermez_db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ const BLOCK_L1_INFO_TREE_INDEX = "block_l1_info_tree_index" // block
const BLOCK_L1_INFO_TREE_INDEX_PROGRESS = "block_l1_info_tree_progress" // block number -> l1 info tree progress
const L1_INJECTED_BATCHES = "l1_injected_batches" // index increasing by 1 -> injected batch for the start of the chain
const BLOCK_INFO_ROOTS = "block_info_roots" // block number -> block info root hash
const L1_BLOCK_HASHES = "l1_block_hashes" // l1 block hash -> true
const BLOCK_L1_BLOCK_HASHES = "block_l1_block_hashes" // block number -> l1 block hash
const L1_BLOCK_HASH_GER = "l1_block_hash_ger" // l1 block hash -> GER
const INTERMEDIATE_TX_STATEROOTS = "hermez_intermediate_tx_stateRoots" // l2blockno -> stateRoot
const BATCH_WITNESSES = "hermez_batch_witnesses" // batch number -> witness
const BATCH_COUNTERS = "hermez_batch_counters" // block number -> counters
Expand Down Expand Up @@ -70,9 +68,7 @@ var HermezDbTables = []string{
BLOCK_L1_INFO_TREE_INDEX_PROGRESS,
L1_INJECTED_BATCHES,
BLOCK_INFO_ROOTS,
L1_BLOCK_HASHES,
BLOCK_L1_BLOCK_HASHES,
L1_BLOCK_HASH_GER,
INTERMEDIATE_TX_STATEROOTS,
BATCH_WITNESSES,
BATCH_COUNTERS,
Expand Down Expand Up @@ -690,53 +686,6 @@ func (db *HermezDb) DeleteReusedL1InfoTreeIndexes(fromBlock, toBlock uint64) err
return nil
}

func (db *HermezDb) WriteGerForL1BlockHash(l1BlockHash common.Hash, ger common.Hash) error {
return db.tx.Put(L1_BLOCK_HASH_GER, l1BlockHash.Bytes(), ger.Bytes())
}

func (db *HermezDbReader) GetGerForL1BlockHash(l1BlockHash common.Hash) (common.Hash, error) {
bytes, err := db.tx.GetOne(L1_BLOCK_HASH_GER, l1BlockHash.Bytes())
if err != nil {
return common.Hash{}, err
}

return common.BytesToHash(bytes), nil
}

func (db *HermezDb) DeleteL1BlockHashGers(l1BlockHashes *[]common.Hash) error {
for _, l1BlockHash := range *l1BlockHashes {
err := db.tx.Delete(L1_BLOCK_HASH_GER, l1BlockHash.Bytes())
if err != nil {
return err
}
}

return nil
}

func (db *HermezDb) WriteL1BlockHash(l1BlockHash common.Hash) error {
return db.tx.Put(L1_BLOCK_HASHES, l1BlockHash.Bytes(), []byte{1})
}

func (db *HermezDbReader) CheckL1BlockHashWritten(l1BlockHash common.Hash) (bool, error) {
bytes, err := db.tx.GetOne(L1_BLOCK_HASHES, l1BlockHash.Bytes())
if err != nil {
return false, err
}
return len(bytes) > 0, nil
}

func (db *HermezDb) DeleteL1BlockHashes(l1BlockHashes *[]common.Hash) error {
for _, l1BlockHash := range *l1BlockHashes {
err := db.tx.Delete(L1_BLOCK_HASHES, l1BlockHash.Bytes())
if err != nil {
return err
}
}

return nil
}

func (db *HermezDb) WriteBlockGlobalExitRoot(l2BlockNo uint64, ger common.Hash) error {
return db.tx.Put(BLOCK_GLOBAL_EXIT_ROOTS, Uint64ToBytes(l2BlockNo), ger.Bytes())
}
Expand Down Expand Up @@ -1080,6 +1029,29 @@ func (db *HermezDbReader) GetForkIdBlock(forkId uint64) (uint64, bool, error) {
return blockNum, found, err
}

func (db *HermezDbReader) GetAllForkBlocks() (map[uint64]uint64, error) {
c, err := db.tx.Cursor(FORKID_BLOCK)
if err != nil {
return nil, err
}
defer c.Close()

forkBlocks := make(map[uint64]uint64)
var k, v []byte

for k, v, err = c.First(); k != nil; k, v, err = c.Next() {
if err != nil {
break
}
currentForkId := BytesToUint64(k)
blockNum := BytesToUint64(v)

forkBlocks[currentForkId] = blockNum
}

return forkBlocks, err
}

func (db *HermezDb) DeleteForkIdBlock(fromBlockNo, toBlockNo uint64) error {
return db.deleteFromBucketWithUintKeysRange(FORKID_BLOCK, fromBlockNo, toBlockNo)
}
Expand Down
43 changes: 5 additions & 38 deletions zk/stages/stage_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,6 @@ type HermezDb interface {
DeleteReusedL1InfoTreeIndexes(fromBlockNum, toBlockNum uint64) error
WriteBlockL1BlockHash(l2BlockNo uint64, l1BlockHash common.Hash) error
DeleteBlockL1BlockHashes(fromBlockNum, toBlockNum uint64) error
WriteL1BlockHash(l1BlockHash common.Hash) error
CheckL1BlockHashWritten(l1BlockHash common.Hash) (bool, error)
DeleteL1BlockHashes(l1BlockHashes *[]common.Hash) error
WriteGerForL1BlockHash(l1BlockHash, ger common.Hash) error
DeleteL1BlockHashGers(l1BlockHashes *[]common.Hash) error
WriteBatchGlobalExitRoot(batchNumber uint64, ger *types.GerUpdate) error
WriteIntermediateTxStateRoot(l2BlockNumber uint64, txHash common.Hash, rpcRoot common.Hash) error
WriteBlockL1InfoTreeIndex(blockNumber uint64, l1Index uint64) error
Expand Down Expand Up @@ -364,13 +359,15 @@ LOOP:
cfg.dsClient.Stop()
return nil
}
continue
}

var dbParentBlockHash common.Hash
if entry.L2BlockNumber > 0 {
dbParentBlockHash, err = eriDb.ReadCanonicalHash(entry.L2BlockNumber - 1)
if err != nil {
return fmt.Errorf("failed to get genesis header: %v", err)
return fmt.Errorf("failed to retrieve parent block hash for datastream block %d: %w",
entry.L2BlockNumber, err)
}
}

Expand Down Expand Up @@ -694,11 +691,6 @@ func UnwindBatchesStage(u *stagedsync.UnwindState, tx kv.RwTx, cfg BatchesCfg, c
return fmt.Errorf("get block global exit roots error: %v", err)
}

l1BlockHashes, err := hermezDb.GetBlockL1BlockHashes(fromBlock, toBlock)
if err != nil {
return fmt.Errorf("get block l1 block hashes error: %v", err)
}

if err := hermezDb.DeleteGlobalExitRoots(&gers); err != nil {
return fmt.Errorf("delete global exit roots error: %v", err)
}
Expand All @@ -711,14 +703,6 @@ func UnwindBatchesStage(u *stagedsync.UnwindState, tx kv.RwTx, cfg BatchesCfg, c
return fmt.Errorf("delete block global exit roots error: %v", err)
}

if err := hermezDb.DeleteL1BlockHashes(&l1BlockHashes); err != nil {
return fmt.Errorf("delete l1 block hashes error: %v", err)
}

if err := hermezDb.DeleteL1BlockHashGers(&l1BlockHashes); err != nil {
return fmt.Errorf("delete l1 block hash gers error: %v", err)
}

if err := hermezDb.DeleteBlockL1BlockHashes(fromBlock, toBlock); err != nil {
return fmt.Errorf("delete block l1 block hashes error: %v", err)
}
Expand Down Expand Up @@ -906,25 +890,8 @@ func writeL2Block(eriDb ErigonDb, hermezDb HermezDb, l2Block *types.FullL2Block,
}

if l2Block.L1BlockHash != emptyHash {
l1BlockHashWritten, err := hermezDb.CheckL1BlockHashWritten(l2Block.L1BlockHash)
if err != nil {
return fmt.Errorf("get global exit root error: %v", err)
}

if !l1BlockHashWritten {
if err := hermezDb.WriteBlockL1BlockHash(l2Block.L2BlockNumber, l2Block.L1BlockHash); err != nil {
return fmt.Errorf("write block global exit root error: %v", err)
}

if err := hermezDb.WriteL1BlockHash(l2Block.L1BlockHash); err != nil {
return fmt.Errorf("write global exit root error: %v", err)
}

if l2Block.GlobalExitRoot != emptyHash {
if err := hermezDb.WriteGerForL1BlockHash(l2Block.L1BlockHash, l2Block.GlobalExitRoot); err != nil {
return fmt.Errorf("write ger for l1 block hash error: %v", err)
}
}
if err := hermezDb.WriteBlockL1BlockHash(l2Block.L2BlockNumber, l2Block.L1BlockHash); err != nil {
return fmt.Errorf("write block global exit root error: %v", err)
}
}

Expand Down
5 changes: 3 additions & 2 deletions zk/stages/stage_sequence_execute.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ func sequencingStageStep(

log.Info(fmt.Sprintf("[%s] Starting batch %d...", logPrefix, batchState.batchNumber))

var allConditionsOK bool
for blockNumber := executionAt + 1; runLoopBlocks; blockNumber++ {
log.Info(fmt.Sprintf("[%s] Starting block %d (forkid %v)...", logPrefix, blockNumber, batchState.forkId))
logTicker.Reset(10 * time.Second)
Expand Down Expand Up @@ -271,7 +270,8 @@ func sequencingStageStep(

ibs := state.New(sdb.stateReader)
getHashFn := core.GetHashFn(header, func(hash common.Hash, number uint64) *types.Header { return rawdb.ReadHeader(sdb.tx, hash, number) })
blockContext := core.NewEVMBlockContext(header, getHashFn, cfg.engine, &cfg.zk.AddressSequencer, parentBlock.ExcessDataGas())
coinbase := batchState.getCoinbase(&cfg)
blockContext := core.NewEVMBlockContext(header, getHashFn, cfg.engine, &coinbase, parentBlock.ExcessDataGas())
batchState.blockState.builtBlockElements.resetBlockBuildingArrays()

parentRoot := parentBlock.Root()
Expand Down Expand Up @@ -307,6 +307,7 @@ func sequencingStageStep(
return err
}
} else if !batchState.isL1Recovery() && !batchState.isResequence() {
var allConditionsOK bool
batchState.blockState.transactionsForInclusion, allConditionsOK, err = getNextPoolTransactions(ctx, cfg, executionAt, batchState.forkId, batchState.yieldedTransactions)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 497ec51

Please sign in to comment.