Skip to content

Commit

Permalink
import from improvements zkevm 0.7.4
Browse files Browse the repository at this point in the history
  • Loading branch information
joanestebanr committed Sep 3, 2024
1 parent 3b65eca commit 79f9277
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 16 deletions.
16 changes: 4 additions & 12 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ import (
"golang.org/x/crypto/sha3"
)

const (
// ETrogUpgradeVersion is the version of the LxLy upgrade
ETrogUpgradeVersion = 2
)

var (
// Events RollupManager
setBatchFeeSignatureHash = crypto.Keccak256Hash([]byte("SetBatchFee(uint256)"))
Expand Down Expand Up @@ -370,20 +365,17 @@ func (etherMan *Client) VerifyGenBlockNumber(ctx context.Context, genBlockNumber

// GetL1BlockUpgradeLxLy It returns the block genesis for LxLy before genesisBlock or error
func (etherMan *Client) GetL1BlockUpgradeLxLy(ctx context.Context, genesisBlock uint64) (uint64, error) {
it, err := etherMan.RollupManager.FilterInitialized(&bind.FilterOpts{
it, err := etherMan.GlobalExitRootManager.FilterUpdateL1InfoTree(&bind.FilterOpts{
Start: 1,
End: &genesisBlock,
Context: ctx,
})
}, nil, nil)
if err != nil {
return uint64(0), err
}
for it.Next() {
log.Debugf("BlockNumber: %d Topics:Initialized(%d)", it.Event.Raw.BlockNumber, it.Event.Version)
if it.Event.Version == ETrogUpgradeVersion { // 2 is ETROG (LxLy upgrade)
log.Infof("LxLy upgrade found at blockNumber: %d", it.Event.Raw.BlockNumber)
return it.Event.Raw.BlockNumber, nil
}
log.Debugf("BlockNumber: %d Topics:L1InfoTree", it.Event.Raw.BlockNumber)
return it.Event.Raw.BlockNumber, nil
}
return uint64(0), ErrNotFound
}
Expand Down
5 changes: 5 additions & 0 deletions state/genesis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ import (
"github.com/jackc/pgx/v4"
)

const (
// AutoDiscoverRollupManagerBlockNumber is the value to auto-discover the RollupManager creation block number
AutoDiscoverRollupManagerBlockNumber = uint64(0)
)

// Genesis contains the information to populate state on creation
type Genesis struct {
// RollupBlockNumber is the block number where the polygonZKEVM smc was deployed on L1
Expand Down
9 changes: 5 additions & 4 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,20 +294,21 @@ func (s *ClientSynchronizer) isGenesisProcessed(ctx context.Context, dbTx pgx.Tx
// bool -> need to process blocks
// uint64 -> first block to synchronize
// error -> error
// 1. First try to get last block on DB, if there are could be fully synced or pending blocks
// 2. If DB is empty the LxLy upgrade block as starting point
// 1. Check last synced block on DB, if there are any could be fully synced (>=genesis) or syncing pre-genesis events (<genesis)
// 2. If no block DB then get the LxLy upgrade block as starting point for pre-genesis or use the genesis block as starting point
func (s *ClientSynchronizer) getStartingL1Block(ctx context.Context, genesisBlockNumber, rollupManagerBlockNumber uint64, dbTx pgx.Tx) (bool, uint64, error) {
lastBlock, err := s.state.GetLastBlock(ctx, dbTx)
if err != nil && errors.Is(err, state.ErrStateNotSynchronized) {
// No block on DB
upgradeLxLyBlockNumber := rollupManagerBlockNumber
if upgradeLxLyBlockNumber == 0 {
if upgradeLxLyBlockNumber == state.AutoDiscoverRollupManagerBlockNumber {
upgradeLxLyBlockNumber, err = s.etherMan.GetL1BlockUpgradeLxLy(ctx, genesisBlockNumber)
if err != nil && errors.Is(err, etherman.ErrNotFound) {
log.Infof("sync pregenesis: LxLy upgrade not detected before genesis block %d, it'll be sync as usual. Nothing to do yet", genesisBlockNumber)
return false, 0, nil
} else if err != nil {
log.Errorf("sync pregenesis: error getting LxLy upgrade block. Error: %v", err)
log.Errorf("sync pre-genesis: error getting LxLy upgrade block. Maybe your provider doesnt support eth_getLogs big block range,"+
".Suggestion: inform the field rollupManagerCreationBlockNumber in genesis file to avoid auto-discover. Error: %v", err)
return false, 0, err
}
}
Expand Down
36 changes: 36 additions & 0 deletions synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package synchronizer

import (
context "context"
"fmt"
"math"
"math/big"
"testing"
Expand Down Expand Up @@ -44,6 +45,41 @@ type mocks struct {
//EventLog *eventLogMock
}

func TestGetStartingL1BlockAutodiscover(t *testing.T) {
genesis, cfg, m := setupGenericTest(t)
ethermanForL1 := []syncinterfaces.EthermanFullInterface{m.Etherman}
syncInterface, err := NewSynchronizer(false, m.Etherman, ethermanForL1, m.State, m.Pool, m.EthTxManager, m.ZKEVMClient, m.zkEVMClientEthereumCompatible, nil, *genesis, *cfg, false)
require.NoError(t, err)
sync, ok := syncInterface.(*ClientSynchronizer)
require.True(t, ok)
ctx := context.TODO()
t.Run("getStartingL1Block autodiscover OK", func(t *testing.T) {
m.State.EXPECT().GetLastBlock(ctx, nil).Return(nil, state.ErrStateNotSynchronized).Once()
m.Etherman.EXPECT().GetL1BlockUpgradeLxLy(mock.Anything, mock.Anything).Return(uint64(100), nil).Once()
needProcess, firstBlock, err := sync.getStartingL1Block(ctx, 123, 0, nil)
require.NoError(t, err)
require.True(t, needProcess)
require.Equal(t, uint64(100), firstBlock)
})

t.Run("getStartingL1Block autodiscover Fails", func(t *testing.T) {
m.State.EXPECT().GetLastBlock(ctx, nil).Return(nil, state.ErrStateNotSynchronized).Once()
m.Etherman.EXPECT().GetL1BlockUpgradeLxLy(mock.Anything, mock.Anything).Return(uint64(0), fmt.Errorf("error")).Once()
_, _, err = sync.getStartingL1Block(ctx, 123, 0, nil)
require.Error(t, err)
})

t.Run("getStartingL1Block have already started sync", func(t *testing.T) {
m.State.EXPECT().GetLastBlock(ctx, nil).Return(&state.Block{BlockNumber: 100}, nil).Once()

needProcess, firstBlock, err := sync.getStartingL1Block(ctx, 123, 0, nil)
require.NoError(t, err)
require.True(t, needProcess)
require.Equal(t, uint64(101), firstBlock)
})

}

// Feature #2220 and #2239: Optimize Trusted state synchronization
//
// this Check partially point 2: Use previous batch stored in memory to avoid getting from database
Expand Down

0 comments on commit 79f9277

Please sign in to comment.