Skip to content

Commit 3c9bdad

Browse files
authored
* wip * linter * docker + genesis * new prover image * docker image v4.0.0-RC2 * Feature/0xPolygonHermez#2840 l1 info root (0xPolygonHermez#2865) * Keep siblings in memory + AddLeaf + tests * l1infotree state and synchronizer * linter * fix: clead db before run the test * index type uint32 * prover image v4.0.0-RC3 * ForcedBatch fields added
1 parent 0a82217 commit 3c9bdad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+2235
-1174
lines changed

cmd/run.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/0xPolygonHermez/zkevm-node/gasprice"
2626
"github.com/0xPolygonHermez/zkevm-node/jsonrpc"
2727
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/client"
28+
"github.com/0xPolygonHermez/zkevm-node/l1infotree"
2829
"github.com/0xPolygonHermez/zkevm-node/log"
2930
"github.com/0xPolygonHermez/zkevm-node/merkletree"
3031
"github.com/0xPolygonHermez/zkevm-node/metrics"
@@ -488,8 +489,20 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDInt
488489
MaxLogsBlockRange: c.RPC.MaxLogsBlockRange,
489490
MaxNativeBlockHashBlockRange: c.RPC.MaxNativeBlockHashBlockRange,
490491
}
492+
allLeaves, err := stateDb.GetAllL1InfoRootEntries(ctx, nil)
493+
if err != nil {
494+
log.Fatal("error getting all leaves. Error: ", err)
495+
}
496+
var leaves [][32]byte
497+
for _, leaf := range allLeaves {
498+
leaves = append(leaves, leaf.Hash())
499+
}
500+
mt, err := l1infotree.NewL1InfoTree(uint8(32), leaves) //nolint:gomnd
501+
if err != nil {
502+
log.Fatal("error creating L1InfoTree. Error: ", err)
503+
}
491504

492-
st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog)
505+
st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog, mt)
493506
return st
494507
}
495508

config/environments/local/local.genesis.config.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
"polTokenAddress": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
77
"polygonZkEVMGlobalExitRootAddress": "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318"
88
},
9-
"genesisBlockNumber": 160,
9+
"genesisBlockNumber": 146,
1010
"root": "0x82a37b85ba3e7b61b14c546d47ff52afbc15e14ba0ddd82642b434f04a58aab8",
1111
"genesis": [
1212
{
@@ -100,7 +100,7 @@
100100
"firstBatchData": {
101101
"transactions": "0xf9010380808401c9c38094aae872c70944d40001755c3eaae53bc4e1a78bd080b8e4f811bff7000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a40d5f56745a118d0906a34e69aec8c0db1cb8fa000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005ca1ab1e0000000000000000000000000000000000000000000000000000000005ca1ab1e1bff",
102102
"globalExitRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
103-
"timestamp": 1700663022,
103+
"timestamp": 1700820478,
104104
"sequencer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
105105
}
106106
}

docker-compose.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ services:
107107
zkevm-prover:
108108
container_name: zkevm-prover
109109
restart: unless-stopped
110-
image: hermeznetwork/zkevm-prover:83c6275
110+
image: hermeznetwork/zkevm-prover:v4.0.0-RC3
111111
depends_on:
112112
zkevm-state-db:
113113
condition: service_healthy

etherman/etherman.go

+145-46
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/0xPolygonHermez/zkevm-node/etherman/ethgasstation"
1818
"github.com/0xPolygonHermez/zkevm-node/etherman/metrics"
1919
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/oldpolygonzkevm"
20+
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/oldpolygonzkevmglobalexitroot"
2021
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/pol"
2122
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonrollupmanager"
2223
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm"
@@ -68,7 +69,7 @@ var (
6869
verifyBatchesSignatureHash = crypto.Keccak256Hash([]byte("VerifyBatches(uint64,bytes32,address)")) // Used in oldZkEvm as well
6970
sequenceForceBatchesSignatureHash = crypto.Keccak256Hash([]byte("SequenceForceBatches(uint64)")) // Used in oldZkEvm as well
7071
forceBatchSignatureHash = crypto.Keccak256Hash([]byte("ForceBatch(uint64,bytes32,address,bytes)")) // Used in oldZkEvm as well
71-
sequenceBatchesSignatureHash = crypto.Keccak256Hash([]byte("SequenceBatches(uint64)")) // Used in oldZkEvm as well
72+
sequenceBatchesSignatureHash = crypto.Keccak256Hash([]byte("SequenceBatches(uint64,bytes32)")) // Used in oldZkEvm as well
7273
initialSequenceBatchesSignatureHash = crypto.Keccak256Hash([]byte("InitialSequenceBatches(bytes,bytes32,address)"))
7374

7475
// Extra RollupManager
@@ -79,16 +80,17 @@ var (
7980
emergencyStateActivatedSignatureHash = crypto.Keccak256Hash([]byte("EmergencyStateActivated()")) // EmergencyManager. Used in oldZkEvm as well
8081
emergencyStateDeactivatedSignatureHash = crypto.Keccak256Hash([]byte("EmergencyStateDeactivated()")) // EmergencyManager. Used in oldZkEvm as well
8182

82-
// New Ger event
83+
// New GER event Etrog
8384
updateL1InfoTreeSignatureHash = crypto.Keccak256Hash([]byte("UpdateL1InfoTree(bytes32,bytes32)"))
8485

85-
// Old zkevm events
86+
// PreLxLy events
8687
updateGlobalExitRootSignatureHash = crypto.Keccak256Hash([]byte("UpdateGlobalExitRoot(bytes32,bytes32)"))
8788
oldVerifyBatchesTrustedAggregatorSignatureHash = crypto.Keccak256Hash([]byte("VerifyBatchesTrustedAggregator(uint64,bytes32,address)"))
8889
transferOwnershipSignatureHash = crypto.Keccak256Hash([]byte("OwnershipTransferred(address,address)"))
8990
updateZkEVMVersionSignatureHash = crypto.Keccak256Hash([]byte("UpdateZkEVMVersion(uint64,uint64,string)"))
9091
oldConsolidatePendingStateSignatureHash = crypto.Keccak256Hash([]byte("ConsolidatePendingState(uint64,bytes32,uint64)"))
9192
oldOverridePendingStateSignatureHash = crypto.Keccak256Hash([]byte("OverridePendingState(uint64,bytes32,address)"))
93+
sequenceBatchesPreEtrogSignatureHash = crypto.Keccak256Hash([]byte("SequenceBatches(uint64)"))
9294

9395
// Proxy events
9496
initializedProxySignatureHash = crypto.Keccak256Hash([]byte("Initialized(uint8)"))
@@ -167,13 +169,14 @@ type externalGasProviders struct {
167169

168170
// Client is a simple implementation of EtherMan.
169171
type Client struct {
170-
EthClient ethereumClient
171-
OldZkEVM *oldpolygonzkevm.Oldpolygonzkevm
172-
ZkEVM *polygonzkevm.Polygonzkevm
173-
RollupManager *polygonrollupmanager.Polygonrollupmanager
174-
GlobalExitRootManager *polygonzkevmglobalexitroot.Polygonzkevmglobalexitroot
175-
Pol *pol.Pol
176-
SCAddresses []common.Address
172+
EthClient ethereumClient
173+
OldZkEVM *oldpolygonzkevm.Oldpolygonzkevm
174+
ZkEVM *polygonzkevm.Polygonzkevm
175+
RollupManager *polygonrollupmanager.Polygonrollupmanager
176+
GlobalExitRootManager *polygonzkevmglobalexitroot.Polygonzkevmglobalexitroot
177+
OldGlobalExitRootManager *oldpolygonzkevmglobalexitroot.Oldpolygonzkevmglobalexitroot
178+
Pol *pol.Pol
179+
SCAddresses []common.Address
177180

178181
RollupID uint32
179182

@@ -453,6 +456,8 @@ func (etherMan *Client) processEvent(ctx context.Context, vLog types.Log, blocks
453456
switch vLog.Topics[0] {
454457
case sequenceBatchesSignatureHash:
455458
return etherMan.sequencedBatchesEvent(ctx, vLog, blocks, blocksOrder)
459+
case sequenceBatchesPreEtrogSignatureHash:
460+
return etherMan.sequencedBatchesPreEtrogEvent(ctx, vLog, blocks, blocksOrder)
456461
case updateGlobalExitRootSignatureHash:
457462
return etherMan.updateGlobalExitRootEvent(ctx, vLog, blocks, blocksOrder)
458463
case updateL1InfoTreeSignatureHash:
@@ -649,17 +654,17 @@ func (etherMan *Client) updateForkId(ctx context.Context, vLog types.Log, blocks
649654
}
650655

651656
func (etherMan *Client) updateL1InfoTreeEvent(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
652-
log.Debug("updateL1InfoTree event detected")
653-
var err error
654-
//TODO: Check that this way os unpacking parameters are right
655-
MainnetExitRoot := vLog.Topics[1]
656-
RollupExitRoot := vLog.Topics[2]
657-
658-
var gExitRoot L1InfoTree
659-
gExitRoot.MainnetExitRoot = MainnetExitRoot
660-
gExitRoot.RollupExitRoot = RollupExitRoot
657+
log.Debug("UpdateL1InfoTree event detected")
658+
globalExitRootL1InfoTree, err := etherMan.GlobalExitRootManager.ParseUpdateL1InfoTree(vLog)
659+
if err != nil {
660+
return err
661+
}
662+
663+
var gExitRoot GlobalExitRoot
664+
gExitRoot.MainnetExitRoot = globalExitRootL1InfoTree.MainnetExitRoot
665+
gExitRoot.RollupExitRoot = globalExitRootL1InfoTree.RollupExitRoot
661666
gExitRoot.BlockNumber = vLog.BlockNumber
662-
gExitRoot.GlobalExitRoot.GlobalExitRoot = hash(MainnetExitRoot, RollupExitRoot)
667+
gExitRoot.GlobalExitRoot = hash(globalExitRootL1InfoTree.MainnetExitRoot, globalExitRootL1InfoTree.RollupExitRoot)
663668
var block *Block
664669
if !isheadBlockInArray(blocks, vLog.BlockHash, vLog.BlockNumber) {
665670
// Need to add the block, doesnt mind if inside the blocks because I have to respect the order so insert at end
@@ -672,7 +677,7 @@ func (etherMan *Client) updateL1InfoTreeEvent(ctx context.Context, vLog types.Lo
672677
// Get the block in the HEAD of the array that contain the current block
673678
block = &(*blocks)[len(*blocks)-1]
674679
gExitRoot.PreviousBlockHash = block.ParentHash
675-
gExitRoot.MinTimestamp = block.ReceivedAt
680+
gExitRoot.Timestamp = block.ReceivedAt
676681
// Add the event to the block
677682
block.L1InfoTree = append(block.L1InfoTree, gExitRoot)
678683
order := Order{
@@ -702,11 +707,11 @@ func isheadBlockInArray(blocks *[]Block, blockHash common.Hash, blockNumber uint
702707

703708
func (etherMan *Client) updateGlobalExitRootEvent(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
704709
log.Debug("UpdateGlobalExitRoot event detected")
705-
globalExitRoot, err := etherMan.GlobalExitRootManager.ParseUpdateGlobalExitRoot(vLog)
710+
oldglobalExitRoot, err := etherMan.OldGlobalExitRootManager.ParseUpdateGlobalExitRoot(vLog)
706711
if err != nil {
707712
return err
708713
}
709-
return etherMan.processUpdateGlobalExitRootEvent(ctx, globalExitRoot.MainnetExitRoot, globalExitRoot.RollupExitRoot, vLog, blocks, blocksOrder)
714+
return etherMan.processUpdateGlobalExitRootEvent(ctx, oldglobalExitRoot.MainnetExitRoot, oldglobalExitRoot.RollupExitRoot, vLog, blocks, blocksOrder)
710715
}
711716

712717
func (etherMan *Client) processUpdateGlobalExitRootEvent(ctx context.Context, mainnetExitRoot, rollupExitRoot common.Hash, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
@@ -790,13 +795,13 @@ func (etherMan *Client) BuildSequenceBatchesTxData(sender common.Address, sequen
790795
}
791796

792797
func (etherMan *Client) sequenceBatches(opts bind.TransactOpts, sequences []ethmanTypes.Sequence, l2Coinbase common.Address) (*types.Transaction, error) {
793-
var batches []polygonzkevm.PolygonRollupBaseBatchData
798+
var batches []polygonzkevm.PolygonRollupBaseEtrogBatchData
794799
for _, seq := range sequences {
795-
batch := polygonzkevm.PolygonRollupBaseBatchData{
796-
Transactions: seq.BatchL2Data,
797-
GlobalExitRoot: seq.GlobalExitRoot,
798-
Timestamp: uint64(seq.Timestamp),
799-
MinForcedTimestamp: uint64(seq.ForcedBatchTimestamp),
800+
batch := polygonzkevm.PolygonRollupBaseEtrogBatchData{
801+
Transactions: seq.BatchL2Data,
802+
ForcedGlobalExitRoot: seq.GlobalExitRoot,
803+
ForcedTimestamp: uint64(seq.ForcedBatchTimestamp),
804+
ForcedBlockHashL1: seq.PrevBlockHash,
800805
}
801806

802807
batches = append(batches, batch)
@@ -1054,6 +1059,53 @@ func (etherMan *Client) sequencedBatchesEvent(ctx context.Context, vLog types.Lo
10541059
return nil
10551060
}
10561061

1062+
func (etherMan *Client) sequencedBatchesPreEtrogEvent(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
1063+
log.Debug("Pre etrog SequenceBatches event detected")
1064+
sb, err := etherMan.OldZkEVM.ParseSequenceBatches(vLog)
1065+
if err != nil {
1066+
return err
1067+
}
1068+
1069+
// Read the tx for this event.
1070+
tx, err := etherMan.EthClient.TransactionInBlock(ctx, vLog.BlockHash, vLog.TxIndex)
1071+
if err != nil {
1072+
return err
1073+
}
1074+
if tx.Hash() != vLog.TxHash {
1075+
return fmt.Errorf("error: tx hash mismatch. want: %s have: %s", vLog.TxHash, tx.Hash().String())
1076+
}
1077+
msg, err := core.TransactionToMessage(tx, types.NewLondonSigner(tx.ChainId()), big.NewInt(0))
1078+
if err != nil {
1079+
return err
1080+
}
1081+
1082+
sequences, err := decodeSequencesPreEtrog(tx.Data(), sb.NumBatch, msg.From, vLog.TxHash, msg.Nonce)
1083+
if err != nil {
1084+
return fmt.Errorf("error decoding the sequences: %v", err)
1085+
}
1086+
1087+
if len(*blocks) == 0 || ((*blocks)[len(*blocks)-1].BlockHash != vLog.BlockHash || (*blocks)[len(*blocks)-1].BlockNumber != vLog.BlockNumber) {
1088+
fullBlock, err := etherMan.EthClient.BlockByHash(ctx, vLog.BlockHash)
1089+
if err != nil {
1090+
return fmt.Errorf("error getting hashParent. BlockNumber: %d. Error: %w", vLog.BlockNumber, err)
1091+
}
1092+
block := prepareBlock(vLog, time.Unix(int64(fullBlock.Time()), 0), fullBlock)
1093+
block.SequencedBatches = append(block.SequencedBatches, sequences)
1094+
*blocks = append(*blocks, block)
1095+
} else if (*blocks)[len(*blocks)-1].BlockHash == vLog.BlockHash && (*blocks)[len(*blocks)-1].BlockNumber == vLog.BlockNumber {
1096+
(*blocks)[len(*blocks)-1].SequencedBatches = append((*blocks)[len(*blocks)-1].SequencedBatches, sequences)
1097+
} else {
1098+
log.Error("Error processing SequencedBatches event. BlockHash:", vLog.BlockHash, ". BlockNumber: ", vLog.BlockNumber)
1099+
return fmt.Errorf("error processing SequencedBatches event")
1100+
}
1101+
or := Order{
1102+
Name: SequenceBatchesOrder,
1103+
Pos: len((*blocks)[len(*blocks)-1].SequencedBatches) - 1,
1104+
}
1105+
(*blocksOrder)[(*blocks)[len(*blocks)-1].BlockHash] = append((*blocksOrder)[(*blocks)[len(*blocks)-1].BlockHash], or)
1106+
return nil
1107+
}
1108+
10571109
func decodeSequences(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64) ([]SequencedBatch, error) {
10581110
// Extract coded txs.
10591111
// Load contract ABI
@@ -1073,7 +1125,52 @@ func decodeSequences(txData []byte, lastBatchNumber uint64, sequencer common.Add
10731125
if err != nil {
10741126
return nil, err
10751127
}
1076-
var sequences []polygonzkevm.PolygonRollupBaseBatchData
1128+
var sequences []polygonzkevm.PolygonRollupBaseEtrogBatchData
1129+
bytedata, err := json.Marshal(data[0])
1130+
if err != nil {
1131+
return nil, err
1132+
}
1133+
err = json.Unmarshal(bytedata, &sequences)
1134+
if err != nil {
1135+
return nil, err
1136+
}
1137+
coinbase := (data[1]).(common.Address)
1138+
sequencedBatches := make([]SequencedBatch, len(sequences))
1139+
for i, seq := range sequences {
1140+
bn := lastBatchNumber - uint64(len(sequences)-(i+1))
1141+
sequencedBatches[i] = SequencedBatch{
1142+
BatchNumber: bn,
1143+
SequencerAddr: sequencer,
1144+
TxHash: txHash,
1145+
Nonce: nonce,
1146+
Coinbase: coinbase,
1147+
PolygonRollupBaseEtrogBatchData: seq,
1148+
}
1149+
}
1150+
1151+
return sequencedBatches, nil
1152+
}
1153+
1154+
func decodeSequencesPreEtrog(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64) ([]SequencedBatch, error) {
1155+
// Extract coded txs.
1156+
// Load contract ABI
1157+
smcAbi, err := abi.JSON(strings.NewReader(oldpolygonzkevm.OldpolygonzkevmABI))
1158+
if err != nil {
1159+
return nil, err
1160+
}
1161+
1162+
// Recover Method from signature and ABI
1163+
method, err := smcAbi.MethodById(txData[:4])
1164+
if err != nil {
1165+
return nil, err
1166+
}
1167+
1168+
// Unpack method inputs
1169+
data, err := method.Inputs.Unpack(txData[4:])
1170+
if err != nil {
1171+
return nil, err
1172+
}
1173+
var sequences []oldpolygonzkevm.PolygonZkEVMBatchData
10771174
bytedata, err := json.Marshal(data[0])
10781175
if err != nil {
10791176
return nil, err
@@ -1087,12 +1184,12 @@ func decodeSequences(txData []byte, lastBatchNumber uint64, sequencer common.Add
10871184
for i, seq := range sequences {
10881185
bn := lastBatchNumber - uint64(len(sequences)-(i+1))
10891186
sequencedBatches[i] = SequencedBatch{
1090-
BatchNumber: bn,
1091-
SequencerAddr: sequencer,
1092-
TxHash: txHash,
1093-
Nonce: nonce,
1094-
Coinbase: coinbase,
1095-
PolygonRollupBaseBatchData: seq,
1187+
BatchNumber: bn,
1188+
SequencerAddr: sequencer,
1189+
TxHash: txHash,
1190+
Nonce: nonce,
1191+
Coinbase: coinbase,
1192+
PolygonZkEVMBatchData: seq,
10961193
}
10971194
}
10981195

@@ -1163,6 +1260,7 @@ func (etherMan *Client) forceSequencedBatchesEvent(ctx context.Context, vLog typ
11631260
if err != nil {
11641261
return err
11651262
}
1263+
// TODO completar los datos de forcedBlockHas, forcedGer y forcedTimestamp
11661264

11671265
// Read the tx for this batch.
11681266
tx, err := etherMan.EthClient.TransactionInBlock(ctx, vLog.BlockHash, vLog.TxIndex)
@@ -1224,7 +1322,7 @@ func decodeSequencedForceBatches(txData []byte, lastBatchNumber uint64, sequence
12241322
return nil, err
12251323
}
12261324

1227-
var forceBatches []polygonzkevm.PolygonRollupBaseForcedBatchData
1325+
var forceBatches []polygonzkevm.PolygonRollupBaseEtrogBatchData
12281326
bytedata, err := json.Marshal(data[0])
12291327
if err != nil {
12301328
return nil, err
@@ -1238,12 +1336,12 @@ func decodeSequencedForceBatches(txData []byte, lastBatchNumber uint64, sequence
12381336
for i, force := range forceBatches {
12391337
bn := lastBatchNumber - uint64(len(forceBatches)-(i+1))
12401338
sequencedForcedBatches[i] = SequencedForceBatch{
1241-
BatchNumber: bn,
1242-
Coinbase: sequencer,
1243-
TxHash: txHash,
1244-
Timestamp: time.Unix(int64(block.Time()), 0),
1245-
Nonce: nonce,
1246-
PolygonRollupBaseForcedBatchData: force,
1339+
BatchNumber: bn,
1340+
Coinbase: sequencer,
1341+
TxHash: txHash,
1342+
Timestamp: time.Unix(int64(block.Time()), 0),
1343+
Nonce: nonce,
1344+
PolygonRollupBaseEtrogBatchData: force,
12471345
}
12481346
}
12491347
return sequencedForcedBatches, nil
@@ -1286,9 +1384,10 @@ func (etherMan *Client) EthBlockByNumber(ctx context.Context, blockNumber uint64
12861384
return block, nil
12871385
}
12881386

1289-
// GetLastBatchTimestamp function allows to retrieve the lastTimestamp value in the smc
1290-
func (etherMan *Client) GetLastBatchTimestamp() (uint64, error) {
1291-
return etherMan.ZkEVM.LastTimestamp(&bind.CallOpts{Pending: false})
1387+
// GetGapLastBatchTimestamp function allows to retrieve the gaplastTimestamp value in the smc
1388+
// TODO: If nobody uses this function delete
1389+
func (etherMan *Client) GetGapLastBatchTimestamp() (uint64, error) {
1390+
return etherMan.ZkEVM.GapLastTimestamp(&bind.CallOpts{Pending: false})
12921391
}
12931392

12941393
// GetLatestBatchNumber function allows to retrieve the latest proposed batch in the smc

0 commit comments

Comments
 (0)