Skip to content

Commit

Permalink
Feature/0xPolygonHermez#2831 new smc etrog (0xPolygonHermez#2837)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ARR552 authored Dec 1, 2023
1 parent 0a82217 commit 3c9bdad
Show file tree
Hide file tree
Showing 48 changed files with 2,235 additions and 1,174 deletions.
15 changes: 14 additions & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/gasprice"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc"
"github.com/0xPolygonHermez/zkevm-node/jsonrpc/client"
"github.com/0xPolygonHermez/zkevm-node/l1infotree"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/0xPolygonHermez/zkevm-node/merkletree"
"github.com/0xPolygonHermez/zkevm-node/metrics"
Expand Down Expand Up @@ -488,8 +489,20 @@ func newState(ctx context.Context, c *config.Config, l2ChainID uint64, forkIDInt
MaxLogsBlockRange: c.RPC.MaxLogsBlockRange,
MaxNativeBlockHashBlockRange: c.RPC.MaxNativeBlockHashBlockRange,
}
allLeaves, err := stateDb.GetAllL1InfoRootEntries(ctx, nil)
if err != nil {
log.Fatal("error getting all leaves. Error: ", err)
}
var leaves [][32]byte
for _, leaf := range allLeaves {
leaves = append(leaves, leaf.Hash())
}
mt, err := l1infotree.NewL1InfoTree(uint8(32), leaves) //nolint:gomnd
if err != nil {
log.Fatal("error creating L1InfoTree. Error: ", err)
}

st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog)
st := state.NewState(stateCfg, stateDb, executorClient, stateTree, eventLog, mt)
return st
}

Expand Down
4 changes: 2 additions & 2 deletions config/environments/local/local.genesis.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"polTokenAddress": "0x5FbDB2315678afecb367f032d93F642f64180aa3",
"polygonZkEVMGlobalExitRootAddress": "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318"
},
"genesisBlockNumber": 160,
"genesisBlockNumber": 146,
"root": "0x82a37b85ba3e7b61b14c546d47ff52afbc15e14ba0ddd82642b434f04a58aab8",
"genesis": [
{
Expand Down Expand Up @@ -100,7 +100,7 @@
"firstBatchData": {
"transactions": "0xf9010380808401c9c38094aae872c70944d40001755c3eaae53bc4e1a78bd080b8e4f811bff7000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000a40d5f56745a118d0906a34e69aec8c0db1cb8fa000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000005ca1ab1e0000000000000000000000000000000000000000000000000000000005ca1ab1e1bff",
"globalExitRoot": "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp": 1700663022,
"timestamp": 1700820478,
"sequencer": "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
}
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ services:
zkevm-prover:
container_name: zkevm-prover
restart: unless-stopped
image: hermeznetwork/zkevm-prover:83c6275
image: hermeznetwork/zkevm-prover:v4.0.0-RC3
depends_on:
zkevm-state-db:
condition: service_healthy
Expand Down
191 changes: 145 additions & 46 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/etherman/ethgasstation"
"github.com/0xPolygonHermez/zkevm-node/etherman/metrics"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/oldpolygonzkevm"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/oldpolygonzkevmglobalexitroot"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/pol"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonrollupmanager"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm"
Expand Down Expand Up @@ -68,7 +69,7 @@ var (
verifyBatchesSignatureHash = crypto.Keccak256Hash([]byte("VerifyBatches(uint64,bytes32,address)")) // Used in oldZkEvm as well
sequenceForceBatchesSignatureHash = crypto.Keccak256Hash([]byte("SequenceForceBatches(uint64)")) // Used in oldZkEvm as well
forceBatchSignatureHash = crypto.Keccak256Hash([]byte("ForceBatch(uint64,bytes32,address,bytes)")) // Used in oldZkEvm as well
sequenceBatchesSignatureHash = crypto.Keccak256Hash([]byte("SequenceBatches(uint64)")) // Used in oldZkEvm as well
sequenceBatchesSignatureHash = crypto.Keccak256Hash([]byte("SequenceBatches(uint64,bytes32)")) // Used in oldZkEvm as well
initialSequenceBatchesSignatureHash = crypto.Keccak256Hash([]byte("InitialSequenceBatches(bytes,bytes32,address)"))

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

// New Ger event
// New GER event Etrog
updateL1InfoTreeSignatureHash = crypto.Keccak256Hash([]byte("UpdateL1InfoTree(bytes32,bytes32)"))

// Old zkevm events
// PreLxLy events
updateGlobalExitRootSignatureHash = crypto.Keccak256Hash([]byte("UpdateGlobalExitRoot(bytes32,bytes32)"))
oldVerifyBatchesTrustedAggregatorSignatureHash = crypto.Keccak256Hash([]byte("VerifyBatchesTrustedAggregator(uint64,bytes32,address)"))
transferOwnershipSignatureHash = crypto.Keccak256Hash([]byte("OwnershipTransferred(address,address)"))
updateZkEVMVersionSignatureHash = crypto.Keccak256Hash([]byte("UpdateZkEVMVersion(uint64,uint64,string)"))
oldConsolidatePendingStateSignatureHash = crypto.Keccak256Hash([]byte("ConsolidatePendingState(uint64,bytes32,uint64)"))
oldOverridePendingStateSignatureHash = crypto.Keccak256Hash([]byte("OverridePendingState(uint64,bytes32,address)"))
sequenceBatchesPreEtrogSignatureHash = crypto.Keccak256Hash([]byte("SequenceBatches(uint64)"))

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

// Client is a simple implementation of EtherMan.
type Client struct {
EthClient ethereumClient
OldZkEVM *oldpolygonzkevm.Oldpolygonzkevm
ZkEVM *polygonzkevm.Polygonzkevm
RollupManager *polygonrollupmanager.Polygonrollupmanager
GlobalExitRootManager *polygonzkevmglobalexitroot.Polygonzkevmglobalexitroot
Pol *pol.Pol
SCAddresses []common.Address
EthClient ethereumClient
OldZkEVM *oldpolygonzkevm.Oldpolygonzkevm
ZkEVM *polygonzkevm.Polygonzkevm
RollupManager *polygonrollupmanager.Polygonrollupmanager
GlobalExitRootManager *polygonzkevmglobalexitroot.Polygonzkevmglobalexitroot
OldGlobalExitRootManager *oldpolygonzkevmglobalexitroot.Oldpolygonzkevmglobalexitroot
Pol *pol.Pol
SCAddresses []common.Address

RollupID uint32

Expand Down Expand Up @@ -453,6 +456,8 @@ func (etherMan *Client) processEvent(ctx context.Context, vLog types.Log, blocks
switch vLog.Topics[0] {
case sequenceBatchesSignatureHash:
return etherMan.sequencedBatchesEvent(ctx, vLog, blocks, blocksOrder)
case sequenceBatchesPreEtrogSignatureHash:
return etherMan.sequencedBatchesPreEtrogEvent(ctx, vLog, blocks, blocksOrder)
case updateGlobalExitRootSignatureHash:
return etherMan.updateGlobalExitRootEvent(ctx, vLog, blocks, blocksOrder)
case updateL1InfoTreeSignatureHash:
Expand Down Expand Up @@ -649,17 +654,17 @@ func (etherMan *Client) updateForkId(ctx context.Context, vLog types.Log, blocks
}

func (etherMan *Client) updateL1InfoTreeEvent(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
log.Debug("updateL1InfoTree event detected")
var err error
//TODO: Check that this way os unpacking parameters are right
MainnetExitRoot := vLog.Topics[1]
RollupExitRoot := vLog.Topics[2]

var gExitRoot L1InfoTree
gExitRoot.MainnetExitRoot = MainnetExitRoot
gExitRoot.RollupExitRoot = RollupExitRoot
log.Debug("UpdateL1InfoTree event detected")
globalExitRootL1InfoTree, err := etherMan.GlobalExitRootManager.ParseUpdateL1InfoTree(vLog)
if err != nil {
return err
}

var gExitRoot GlobalExitRoot
gExitRoot.MainnetExitRoot = globalExitRootL1InfoTree.MainnetExitRoot
gExitRoot.RollupExitRoot = globalExitRootL1InfoTree.RollupExitRoot
gExitRoot.BlockNumber = vLog.BlockNumber
gExitRoot.GlobalExitRoot.GlobalExitRoot = hash(MainnetExitRoot, RollupExitRoot)
gExitRoot.GlobalExitRoot = hash(globalExitRootL1InfoTree.MainnetExitRoot, globalExitRootL1InfoTree.RollupExitRoot)
var block *Block
if !isheadBlockInArray(blocks, vLog.BlockHash, vLog.BlockNumber) {
// Need to add the block, doesnt mind if inside the blocks because I have to respect the order so insert at end
Expand All @@ -672,7 +677,7 @@ func (etherMan *Client) updateL1InfoTreeEvent(ctx context.Context, vLog types.Lo
// Get the block in the HEAD of the array that contain the current block
block = &(*blocks)[len(*blocks)-1]
gExitRoot.PreviousBlockHash = block.ParentHash
gExitRoot.MinTimestamp = block.ReceivedAt
gExitRoot.Timestamp = block.ReceivedAt
// Add the event to the block
block.L1InfoTree = append(block.L1InfoTree, gExitRoot)
order := Order{
Expand Down Expand Up @@ -702,11 +707,11 @@ func isheadBlockInArray(blocks *[]Block, blockHash common.Hash, blockNumber uint

func (etherMan *Client) updateGlobalExitRootEvent(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
log.Debug("UpdateGlobalExitRoot event detected")
globalExitRoot, err := etherMan.GlobalExitRootManager.ParseUpdateGlobalExitRoot(vLog)
oldglobalExitRoot, err := etherMan.OldGlobalExitRootManager.ParseUpdateGlobalExitRoot(vLog)
if err != nil {
return err
}
return etherMan.processUpdateGlobalExitRootEvent(ctx, globalExitRoot.MainnetExitRoot, globalExitRoot.RollupExitRoot, vLog, blocks, blocksOrder)
return etherMan.processUpdateGlobalExitRootEvent(ctx, oldglobalExitRoot.MainnetExitRoot, oldglobalExitRoot.RollupExitRoot, vLog, blocks, blocksOrder)
}

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

func (etherMan *Client) sequenceBatches(opts bind.TransactOpts, sequences []ethmanTypes.Sequence, l2Coinbase common.Address) (*types.Transaction, error) {
var batches []polygonzkevm.PolygonRollupBaseBatchData
var batches []polygonzkevm.PolygonRollupBaseEtrogBatchData
for _, seq := range sequences {
batch := polygonzkevm.PolygonRollupBaseBatchData{
Transactions: seq.BatchL2Data,
GlobalExitRoot: seq.GlobalExitRoot,
Timestamp: uint64(seq.Timestamp),
MinForcedTimestamp: uint64(seq.ForcedBatchTimestamp),
batch := polygonzkevm.PolygonRollupBaseEtrogBatchData{
Transactions: seq.BatchL2Data,
ForcedGlobalExitRoot: seq.GlobalExitRoot,
ForcedTimestamp: uint64(seq.ForcedBatchTimestamp),
ForcedBlockHashL1: seq.PrevBlockHash,
}

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

func (etherMan *Client) sequencedBatchesPreEtrogEvent(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
log.Debug("Pre etrog SequenceBatches event detected")
sb, err := etherMan.OldZkEVM.ParseSequenceBatches(vLog)
if err != nil {
return err
}

// Read the tx for this event.
tx, err := etherMan.EthClient.TransactionInBlock(ctx, vLog.BlockHash, vLog.TxIndex)
if err != nil {
return err
}
if tx.Hash() != vLog.TxHash {
return fmt.Errorf("error: tx hash mismatch. want: %s have: %s", vLog.TxHash, tx.Hash().String())
}
msg, err := core.TransactionToMessage(tx, types.NewLondonSigner(tx.ChainId()), big.NewInt(0))
if err != nil {
return err
}

sequences, err := decodeSequencesPreEtrog(tx.Data(), sb.NumBatch, msg.From, vLog.TxHash, msg.Nonce)
if err != nil {
return fmt.Errorf("error decoding the sequences: %v", err)
}

if len(*blocks) == 0 || ((*blocks)[len(*blocks)-1].BlockHash != vLog.BlockHash || (*blocks)[len(*blocks)-1].BlockNumber != vLog.BlockNumber) {
fullBlock, err := etherMan.EthClient.BlockByHash(ctx, vLog.BlockHash)
if err != nil {
return fmt.Errorf("error getting hashParent. BlockNumber: %d. Error: %w", vLog.BlockNumber, err)
}
block := prepareBlock(vLog, time.Unix(int64(fullBlock.Time()), 0), fullBlock)
block.SequencedBatches = append(block.SequencedBatches, sequences)
*blocks = append(*blocks, block)
} else if (*blocks)[len(*blocks)-1].BlockHash == vLog.BlockHash && (*blocks)[len(*blocks)-1].BlockNumber == vLog.BlockNumber {
(*blocks)[len(*blocks)-1].SequencedBatches = append((*blocks)[len(*blocks)-1].SequencedBatches, sequences)
} else {
log.Error("Error processing SequencedBatches event. BlockHash:", vLog.BlockHash, ". BlockNumber: ", vLog.BlockNumber)
return fmt.Errorf("error processing SequencedBatches event")
}
or := Order{
Name: SequenceBatchesOrder,
Pos: len((*blocks)[len(*blocks)-1].SequencedBatches) - 1,
}
(*blocksOrder)[(*blocks)[len(*blocks)-1].BlockHash] = append((*blocksOrder)[(*blocks)[len(*blocks)-1].BlockHash], or)
return nil
}

func decodeSequences(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64) ([]SequencedBatch, error) {
// Extract coded txs.
// Load contract ABI
Expand All @@ -1073,7 +1125,52 @@ func decodeSequences(txData []byte, lastBatchNumber uint64, sequencer common.Add
if err != nil {
return nil, err
}
var sequences []polygonzkevm.PolygonRollupBaseBatchData
var sequences []polygonzkevm.PolygonRollupBaseEtrogBatchData
bytedata, err := json.Marshal(data[0])
if err != nil {
return nil, err
}
err = json.Unmarshal(bytedata, &sequences)
if err != nil {
return nil, err
}
coinbase := (data[1]).(common.Address)
sequencedBatches := make([]SequencedBatch, len(sequences))
for i, seq := range sequences {
bn := lastBatchNumber - uint64(len(sequences)-(i+1))
sequencedBatches[i] = SequencedBatch{
BatchNumber: bn,
SequencerAddr: sequencer,
TxHash: txHash,
Nonce: nonce,
Coinbase: coinbase,
PolygonRollupBaseEtrogBatchData: seq,
}
}

return sequencedBatches, nil
}

func decodeSequencesPreEtrog(txData []byte, lastBatchNumber uint64, sequencer common.Address, txHash common.Hash, nonce uint64) ([]SequencedBatch, error) {
// Extract coded txs.
// Load contract ABI
smcAbi, err := abi.JSON(strings.NewReader(oldpolygonzkevm.OldpolygonzkevmABI))
if err != nil {
return nil, err
}

// Recover Method from signature and ABI
method, err := smcAbi.MethodById(txData[:4])
if err != nil {
return nil, err
}

// Unpack method inputs
data, err := method.Inputs.Unpack(txData[4:])
if err != nil {
return nil, err
}
var sequences []oldpolygonzkevm.PolygonZkEVMBatchData
bytedata, err := json.Marshal(data[0])
if err != nil {
return nil, err
Expand All @@ -1087,12 +1184,12 @@ func decodeSequences(txData []byte, lastBatchNumber uint64, sequencer common.Add
for i, seq := range sequences {
bn := lastBatchNumber - uint64(len(sequences)-(i+1))
sequencedBatches[i] = SequencedBatch{
BatchNumber: bn,
SequencerAddr: sequencer,
TxHash: txHash,
Nonce: nonce,
Coinbase: coinbase,
PolygonRollupBaseBatchData: seq,
BatchNumber: bn,
SequencerAddr: sequencer,
TxHash: txHash,
Nonce: nonce,
Coinbase: coinbase,
PolygonZkEVMBatchData: seq,
}
}

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

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

var forceBatches []polygonzkevm.PolygonRollupBaseForcedBatchData
var forceBatches []polygonzkevm.PolygonRollupBaseEtrogBatchData
bytedata, err := json.Marshal(data[0])
if err != nil {
return nil, err
Expand All @@ -1238,12 +1336,12 @@ func decodeSequencedForceBatches(txData []byte, lastBatchNumber uint64, sequence
for i, force := range forceBatches {
bn := lastBatchNumber - uint64(len(forceBatches)-(i+1))
sequencedForcedBatches[i] = SequencedForceBatch{
BatchNumber: bn,
Coinbase: sequencer,
TxHash: txHash,
Timestamp: time.Unix(int64(block.Time()), 0),
Nonce: nonce,
PolygonRollupBaseForcedBatchData: force,
BatchNumber: bn,
Coinbase: sequencer,
TxHash: txHash,
Timestamp: time.Unix(int64(block.Time()), 0),
Nonce: nonce,
PolygonRollupBaseEtrogBatchData: force,
}
}
return sequencedForcedBatches, nil
Expand Down Expand Up @@ -1286,9 +1384,10 @@ func (etherMan *Client) EthBlockByNumber(ctx context.Context, blockNumber uint64
return block, nil
}

// GetLastBatchTimestamp function allows to retrieve the lastTimestamp value in the smc
func (etherMan *Client) GetLastBatchTimestamp() (uint64, error) {
return etherMan.ZkEVM.LastTimestamp(&bind.CallOpts{Pending: false})
// GetGapLastBatchTimestamp function allows to retrieve the gaplastTimestamp value in the smc
// TODO: If nobody uses this function delete
func (etherMan *Client) GetGapLastBatchTimestamp() (uint64, error) {
return etherMan.ZkEVM.GapLastTimestamp(&bind.CallOpts{Pending: false})
}

// GetLatestBatchNumber function allows to retrieve the latest proposed batch in the smc
Expand Down
Loading

0 comments on commit 3c9bdad

Please sign in to comment.