Skip to content

Commit

Permalink
Feature/#3081 smc (#3097)
Browse files Browse the repository at this point in the history
* new contracts

* Event etherman implementation

* smc

* proxy + etherman unit test

* new event implemented in synchronizer

* Fix setforcedBatch event

* smc update

* updateEtrogSequence working

* fix ger mismatch

* add comment suggested
  • Loading branch information
ARR552 authored Jan 18, 2024
1 parent 1690310 commit f28d4f5
Show file tree
Hide file tree
Showing 43 changed files with 1,984 additions and 515 deletions.
76 changes: 66 additions & 10 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ var (
// Events new ZkEvm/RollupBase
acceptAdminRoleSignatureHash = crypto.Keccak256Hash([]byte("AcceptAdminRole(address)")) // Used in oldZkEvm as well
transferAdminRoleSignatureHash = crypto.Keccak256Hash([]byte("TransferAdminRole(address)")) // Used in oldZkEvm as well
activateForceBatchesSignatureHash = crypto.Keccak256Hash([]byte("ActivateForceBatches()")) // Used in oldZkEvm as well
setForceBatchAddressSignatureHash = crypto.Keccak256Hash([]byte("SetForceBatchAddress(address)")) // Used in oldZkEvm as well
setForceBatchTimeoutSignatureHash = crypto.Keccak256Hash([]byte("SetForceBatchTimeout(uint64)")) // Used in oldZkEvm as well
setTrustedSequencerURLSignatureHash = crypto.Keccak256Hash([]byte("SetTrustedSequencerURL(string)")) // Used in oldZkEvm as well
setTrustedSequencerSignatureHash = crypto.Keccak256Hash([]byte("SetTrustedSequencer(address)")) // Used in oldZkEvm as well
Expand All @@ -71,6 +71,7 @@ var (
forceBatchSignatureHash = crypto.Keccak256Hash([]byte("ForceBatch(uint64,bytes32,address,bytes)")) // 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)"))
updateEtrogSequenceSignatureHash = crypto.Keccak256Hash([]byte("UpdateEtrogSequence(uint64,bytes,bytes32,address)"))

// Extra RollupManager
initializedSignatureHash = crypto.Keccak256Hash([]byte("Initialized(uint64)")) // Initializable. Used in RollupBase as well
Expand Down Expand Up @@ -123,6 +124,8 @@ const (
L1InfoTreeOrder EventOrder = "L1InfoTreeOrder"
// SequenceBatchesOrder identifies a VerifyBatch event
SequenceBatchesOrder EventOrder = "SequenceBatches"
// UpdateEtrogSequenceOrder identifies a VerifyBatch event
UpdateEtrogSequenceOrder EventOrder = "UpdateEtrogSequence"
// ForcedBatchesOrder identifies a ForcedBatches event
ForcedBatchesOrder EventOrder = "ForcedBatches"
// TrustedVerifyBatchOrder identifies a TrustedVerifyBatch event
Expand Down Expand Up @@ -484,6 +487,8 @@ func (etherMan *Client) processEvent(ctx context.Context, vLog types.Log, blocks
return etherMan.forcedBatchEvent(ctx, vLog, blocks, blocksOrder)
case initialSequenceBatchesSignatureHash:
return etherMan.initialSequenceBatches(ctx, vLog, blocks, blocksOrder)
case updateEtrogSequenceSignatureHash:
return etherMan.updateEtrogSequence(ctx, vLog, blocks, blocksOrder)
case verifyBatchesTrustedAggregatorSignatureHash:
log.Debug("VerifyBatchesTrustedAggregator event detected. Ignoring...")
return nil
Expand Down Expand Up @@ -552,8 +557,8 @@ func (etherMan *Client) processEvent(ctx context.Context, vLog types.Log, blocks
case setForceBatchTimeoutSignatureHash:
log.Debug("SetForceBatchTimeout event detected. Ignoring...")
return nil
case activateForceBatchesSignatureHash:
log.Debug("ActivateForceBatches event detected. Ignoring...")
case setForceBatchAddressSignatureHash:
log.Debug("SetForceBatchAddress event detected. Ignoring...")
return nil
case transferAdminRoleSignatureHash:
log.Debug("TransferAdminRole event detected. Ignoring...")
Expand Down Expand Up @@ -653,11 +658,68 @@ func (etherMan *Client) addExistingRollup(ctx context.Context, vLog types.Log, b
return etherMan.updateForkId(ctx, vLog, blocks, blocksOrder, addExistingRollup.LastVerifiedBatchBeforeUpgrade, addExistingRollup.ForkID, "")
}

func (etherMan *Client) updateEtrogSequence(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
log.Debug("updateEtrogSequence event detected")
updateEtrogSequence, err := etherMan.ZkEVM.ParseUpdateEtrogSequence(vLog)
if err != nil {
log.Error("error parsing updateEtrogSequence event. Error: ", err)
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
}
fullBlock, err := etherMan.EthClient.BlockByHash(ctx, vLog.BlockHash)
if err != nil {
return fmt.Errorf("error getting fullBlockInfo. BlockNumber: %d. Error: %w", vLog.BlockNumber, err)
}

log.Info("update Etrog transaction sequence...")
sequence := UpdateEtrogSequence{
BatchNumber: updateEtrogSequence.NumBatch,
SequencerAddr: updateEtrogSequence.Sequencer,
TxHash: vLog.TxHash,
Nonce: msg.Nonce,
PolygonRollupBaseEtrogBatchData: &polygonzkevm.PolygonRollupBaseEtrogBatchData{
Transactions: updateEtrogSequence.Transactions,
ForcedGlobalExitRoot: updateEtrogSequence.LastGlobalExitRoot,
ForcedTimestamp: fullBlock.Time(),
ForcedBlockHashL1: fullBlock.ParentHash(),
},
}

if len(*blocks) == 0 || ((*blocks)[len(*blocks)-1].BlockHash != vLog.BlockHash || (*blocks)[len(*blocks)-1].BlockNumber != vLog.BlockNumber) {
block := prepareBlock(vLog, time.Unix(int64(fullBlock.Time()), 0), fullBlock)
block.UpdateEtrogSequence = sequence
*blocks = append(*blocks, block)
} else if (*blocks)[len(*blocks)-1].BlockHash == vLog.BlockHash && (*blocks)[len(*blocks)-1].BlockNumber == vLog.BlockNumber {
(*blocks)[len(*blocks)-1].UpdateEtrogSequence = sequence
} else {
log.Error("Error processing UpdateEtrogSequence event. BlockHash:", vLog.BlockHash, ". BlockNumber: ", vLog.BlockNumber)
return fmt.Errorf("error processing UpdateEtrogSequence event")
}
or := Order{
Name: UpdateEtrogSequenceOrder,
Pos: 0,
}
(*blocksOrder)[(*blocks)[len(*blocks)-1].BlockHash] = append((*blocksOrder)[(*blocks)[len(*blocks)-1].BlockHash], or)
return nil
}

func (etherMan *Client) initialSequenceBatches(ctx context.Context, vLog types.Log, blocks *[]Block, blocksOrder *map[common.Hash][]Order) error {
log.Debug("initialSequenceBatches event detected")
initialSequenceBatches, err := etherMan.ZkEVM.ParseInitialSequenceBatches(vLog)
if err != nil {
log.Error("error parsing createNewRollup event. Error: ", err)
log.Error("error parsing initialSequenceBatches event. Error: ", err)
return err
}

Expand Down Expand Up @@ -1477,12 +1539,6 @@ func (etherMan *Client) EthBlockByNumber(ctx context.Context, blockNumber uint64
return block, nil
}

// 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
func (etherMan *Client) GetLatestBatchNumber() (uint64, error) {
rollupData, err := etherMan.RollupManager.RollupIDToRollupData(&bind.CallOpts{Pending: false}, etherMan.RollupID)
Expand Down
37 changes: 27 additions & 10 deletions etherman/simulated.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevm"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevmbridge"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/polygonzkevmglobalexitroot"
"github.com/0xPolygonHermez/zkevm-node/etherman/smartcontracts/proxy"
"github.com/0xPolygonHermez/zkevm-node/log"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/accounts/abi/bind/backends"
Expand Down Expand Up @@ -55,23 +56,33 @@ func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) (etherman *Client
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
const posBridge = 1
const posBridge = 3
calculatedBridgeAddr := crypto.CreateAddress(auth.From, nonce+posBridge)
const posRollupManager = 2
const posRollupManager = 4
calculatedRollupManagerAddr := crypto.CreateAddress(auth.From, nonce+posRollupManager)
genesis := common.HexToHash("0xfd3434cd8f67e59d73488a2b8da242dd1f02849ea5dd99f0ca22c836c3d5b4a9") // Random value. Needs to be different to 0x0
exitManagerAddr, _, globalExitRoot, err := polygonzkevmglobalexitroot.DeployPolygonzkevmglobalexitroot(auth, client, calculatedRollupManagerAddr, calculatedBridgeAddr)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
bridgeAddr, _, br, err := polygonzkevmbridge.DeployPolygonzkevmbridge(auth, client)
implementationBridgeAddr, _, _, err := polygonzkevmbridge.DeployPolygonzkevmbridge(auth, client)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}

mockRollupManagerAddr, _, mockRollupManager, err := mockpolygonrollupmanager.DeployMockpolygonrollupmanager(auth, client, exitManagerAddr, polAddr, bridgeAddr)
implementationMockRollupManagerAddr, _, _, err := mockpolygonrollupmanager.DeployMockpolygonrollupmanager(auth, client, exitManagerAddr, polAddr, calculatedBridgeAddr)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
bridgeAddr, _, _, err := proxy.DeployProxy(auth, client, implementationBridgeAddr, implementationBridgeAddr, []byte{})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
mockRollupManagerAddr, _, _, err := proxy.DeployProxy(auth, client, implementationMockRollupManagerAddr, implementationMockRollupManagerAddr, []byte{})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
Expand All @@ -85,13 +96,22 @@ func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) (etherman *Client
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
mockRollupManager, err := mockpolygonrollupmanager.NewMockpolygonrollupmanager(mockRollupManagerAddr, client)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
br, err = polygonzkevmbridge.NewPolygonzkevmbridge(bridgeAddr, client)
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
_, err = br.Initialize(auth, 0, common.Address{}, 0, exitManagerAddr, mockRollupManagerAddr, []byte{})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}

_, err = mockRollupManager.InitializeMock(auth, auth.From, 10000, 10000, auth.From, auth.From, auth.From) //nolint:gomnd
_, err = mockRollupManager.Initialize(auth, auth.From, 10000, 10000, auth.From, auth.From, auth.From, common.Address{}, common.Address{}, 0, 0) //nolint:gomnd
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
Expand Down Expand Up @@ -158,16 +178,13 @@ func NewSimulatedEtherman(cfg Config, auth *bind.TransactOpts) (etherman *Client
return nil, nil, common.Address{}, nil, err
}

_, err = trueZkevm.ActivateForceBatches(auth)
_, err = trueZkevm.SetForceBatchAddress(auth, common.Address{})
if err != nil {
log.Error("error: ", err)
return nil, nil, common.Address{}, nil, err
}
client.Commit()

r, _ := trueZkevm.IsForcedBatchAllowed(&bind.CallOpts{Pending: false})
log.Debug("IsforcedBatch: ", r)

client.Commit()
c := &Client{
EthClient: client,
Expand Down
62 changes: 21 additions & 41 deletions etherman/smartcontracts/abi/mockpolygonrollupmanager.abi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"inputs": [
{
"internalType": "contract IPolygonZkEVMGlobalExitRoot",
"internalType": "contract IPolygonZkEVMGlobalExitRootV2",
"name": "_globalExitRootManager",
"type": "address"
},
Expand Down Expand Up @@ -160,6 +160,11 @@
"name": "PendingStateNotConsolidable",
"type": "error"
},
{
"inputs": [],
"name": "RollupAddressAlreadyExist",
"type": "error"
},
{
"inputs": [],
"name": "RollupMustExist",
Expand Down Expand Up @@ -1177,7 +1182,7 @@
"name": "globalExitRootManager",
"outputs": [
{
"internalType": "contract IPolygonZkEVMGlobalExitRoot",
"internalType": "contract IPolygonZkEVMGlobalExitRootV2",
"name": "",
"type": "address"
}
Expand Down Expand Up @@ -1260,7 +1265,7 @@
"type": "address"
},
{
"internalType": "contract PolygonZkEVMV2Existent",
"internalType": "contract PolygonZkEVMExistentEtrog",
"name": "polygonZkEVM",
"type": "address"
},
Expand All @@ -1285,44 +1290,6 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "address",
"name": "trustedAggregator",
"type": "address"
},
{
"internalType": "uint64",
"name": "_pendingStateTimeout",
"type": "uint64"
},
{
"internalType": "uint64",
"name": "_trustedAggregatorTimeout",
"type": "uint64"
},
{
"internalType": "address",
"name": "admin",
"type": "address"
},
{
"internalType": "address",
"name": "timelock",
"type": "address"
},
{
"internalType": "address",
"name": "emergencyCouncil",
"type": "address"
}
],
"name": "initializeMock",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "isEmergencyState",
Expand Down Expand Up @@ -1373,6 +1340,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "lastDeactivatedEmergencyStateTimestamp",
"outputs": [
{
"internalType": "uint64",
"name": "",
"type": "uint64"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "multiplierBatchFee",
Expand Down
24 changes: 21 additions & 3 deletions etherman/smartcontracts/abi/polygonrollupmanager.abi
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
"inputs": [
{
"internalType": "contract IPolygonZkEVMGlobalExitRoot",
"internalType": "contract IPolygonZkEVMGlobalExitRootV2",
"name": "_globalExitRootManager",
"type": "address"
},
Expand Down Expand Up @@ -160,6 +160,11 @@
"name": "PendingStateNotConsolidable",
"type": "error"
},
{
"inputs": [],
"name": "RollupAddressAlreadyExist",
"type": "error"
},
{
"inputs": [],
"name": "RollupMustExist",
Expand Down Expand Up @@ -1177,7 +1182,7 @@
"name": "globalExitRootManager",
"outputs": [
{
"internalType": "contract IPolygonZkEVMGlobalExitRoot",
"internalType": "contract IPolygonZkEVMGlobalExitRootV2",
"name": "",
"type": "address"
}
Expand Down Expand Up @@ -1260,7 +1265,7 @@
"type": "address"
},
{
"internalType": "contract PolygonZkEVMV2Existent",
"internalType": "contract PolygonZkEVMExistentEtrog",
"name": "polygonZkEVM",
"type": "address"
},
Expand Down Expand Up @@ -1335,6 +1340,19 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "lastDeactivatedEmergencyStateTimestamp",
"outputs": [
{
"internalType": "uint64",
"name": "",
"type": "uint64"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "multiplierBatchFee",
Expand Down
Loading

0 comments on commit f28d4f5

Please sign in to comment.