Skip to content

Commit

Permalink
Cherry-pick: Feature/3640 synchronizer choose to sync from l2 (#3641) (
Browse files Browse the repository at this point in the history
…#3649)

* Feature/3640 synchronizer choose to sync from l2 (#3641)
* change synchronization config params 'Enable' to 'Enabled' (#3656)
  • Loading branch information
joanestebanr authored May 27, 2024
1 parent a5f9558 commit a59d86f
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 34 deletions.
12 changes: 12 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,18 @@ func Test_Defaults(t *testing.T) {
path: "Synchronizer.L2Synchronization.CheckLastL2BlockHashOnCloseBatch",
expectedValue: true,
},
{
path: "Synchronizer.L1BlockCheck.Enabled",
expectedValue: true,
},
{
path: "Synchronizer.L1BlockCheck.PreCheckEnabled",
expectedValue: true,
},
{
path: "Synchronizer.L2Synchronization.Enabled",
expectedValue: true,
},

{
path: "Sequencer.DeletePoolTxsL1BlockConfirmations",
Expand Down
5 changes: 3 additions & 2 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ L1SynchronizationMode = "sequential"
L1SyncCheckL2BlockHash = true
L1SyncCheckL2BlockNumberModulus = 600
[Synchronizer.L1BlockCheck]
Enable = true
Enabled = true
L1SafeBlockPoint = "finalized"
L1SafeBlockOffset = 0
ForceCheckBeforeStart = true
PreCheckEnable = true
PreCheckEnabled = true
L1PreSafeBlockPoint = "safe"
L1PreSafeBlockOffset = 0
[Synchronizer.L1ParallelSynchronization]
Expand All @@ -128,6 +128,7 @@ L1SyncCheckL2BlockNumberModulus = 600
AceptableInacctivityTime = "5s"
ApplyAfterNumRollupReceived = 10
[Synchronizer.L2Synchronization]
Enabled = true
AcceptEmptyClosedBatches = false
ReprocessFullBatchOnClose = true
CheckLastL2BlockHashOnCloseBatch = true
Expand Down
4 changes: 2 additions & 2 deletions docs/config-file/node-config-doc.html

Large diffs are not rendered by default.

37 changes: 26 additions & 11 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -1465,26 +1465,26 @@ L1SyncCheckL2BlockNumberModulus=600

| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ---------------------------------------------------------------------------- | ------- | ---------------- | ---------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| - [Enable](#Synchronizer_L1BlockCheck_Enable ) | No | boolean | No | - | Enable if is true then the check l1 Block Hash is active |
| - [Enabled](#Synchronizer_L1BlockCheck_Enabled ) | No | boolean | No | - | If enabled then the check l1 Block Hash is active |
| - [L1SafeBlockPoint](#Synchronizer_L1BlockCheck_L1SafeBlockPoint ) | No | enum (of string) | No | - | L1SafeBlockPoint is the point that a block is considered safe enough to be checked<br />it can be: finalized, safe,pending or latest |
| - [L1SafeBlockOffset](#Synchronizer_L1BlockCheck_L1SafeBlockOffset ) | No | integer | No | - | L1SafeBlockOffset is the offset to add to L1SafeBlockPoint as a safe point<br />it can be positive or negative<br />Example: L1SafeBlockPoint= finalized, L1SafeBlockOffset= -10, then the safe block ten blocks before the finalized block |
| - [ForceCheckBeforeStart](#Synchronizer_L1BlockCheck_ForceCheckBeforeStart ) | No | boolean | No | - | ForceCheckBeforeStart if is true then the first time the system is started it will force to check all pending blocks |
| - [PreCheckEnable](#Synchronizer_L1BlockCheck_PreCheckEnable ) | No | boolean | No | - | PreCheckEnable if is true then the pre-check is active, will check blocks between L1SafeBlock and L1PreSafeBlock |
| - [PreCheckEnabled](#Synchronizer_L1BlockCheck_PreCheckEnabled ) | No | boolean | No | - | If enabled then the pre-check is active, will check blocks between L1SafeBlock and L1PreSafeBlock |
| - [L1PreSafeBlockPoint](#Synchronizer_L1BlockCheck_L1PreSafeBlockPoint ) | No | enum (of string) | No | - | L1PreSafeBlockPoint is the point that a block is considered safe enough to be checked<br />it can be: finalized, safe,pending or latest |
| - [L1PreSafeBlockOffset](#Synchronizer_L1BlockCheck_L1PreSafeBlockOffset ) | No | integer | No | - | L1PreSafeBlockOffset is the offset to add to L1PreSafeBlockPoint as a safe point<br />it can be positive or negative<br />Example: L1PreSafeBlockPoint= finalized, L1PreSafeBlockOffset= -10, then the safe block ten blocks before the finalized block |

#### <a name="Synchronizer_L1BlockCheck_Enable"></a>9.7.1. `Synchronizer.L1BlockCheck.Enable`
#### <a name="Synchronizer_L1BlockCheck_Enabled"></a>9.7.1. `Synchronizer.L1BlockCheck.Enabled`

**Type:** : `boolean`

**Default:** `true`

**Description:** Enable if is true then the check l1 Block Hash is active
**Description:** If enabled then the check l1 Block Hash is active

**Example setting the default value** (true):
```
[Synchronizer.L1BlockCheck]
Enable=true
Enabled=true
```

#### <a name="Synchronizer_L1BlockCheck_L1SafeBlockPoint"></a>9.7.2. `Synchronizer.L1BlockCheck.L1SafeBlockPoint`
Expand Down Expand Up @@ -1537,18 +1537,18 @@ L1SafeBlockOffset=0
ForceCheckBeforeStart=true
```

#### <a name="Synchronizer_L1BlockCheck_PreCheckEnable"></a>9.7.5. `Synchronizer.L1BlockCheck.PreCheckEnable`
#### <a name="Synchronizer_L1BlockCheck_PreCheckEnabled"></a>9.7.5. `Synchronizer.L1BlockCheck.PreCheckEnabled`

**Type:** : `boolean`

**Default:** `true`

**Description:** PreCheckEnable if is true then the pre-check is active, will check blocks between L1SafeBlock and L1PreSafeBlock
**Description:** If enabled then the pre-check is active, will check blocks between L1SafeBlock and L1PreSafeBlock

**Example setting the default value** (true):
```
[Synchronizer.L1BlockCheck]
PreCheckEnable=true
PreCheckEnabled=true
```

#### <a name="Synchronizer_L1BlockCheck_L1PreSafeBlockPoint"></a>9.7.6. `Synchronizer.L1BlockCheck.L1PreSafeBlockPoint`
Expand Down Expand Up @@ -1875,11 +1875,26 @@ FallbackToSequentialModeOnSynchronized=false

| Property | Pattern | Type | Deprecated | Definition | Title/Description |
| ------------------------------------------------------------------------------------------------------- | ------- | ------- | ---------- | ---------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| - [Enabled](#Synchronizer_L2Synchronization_Enabled ) | No | boolean | No | - | If enabled then the L2 sync process is permitted (only for permissionless) |
| - [AcceptEmptyClosedBatches](#Synchronizer_L2Synchronization_AcceptEmptyClosedBatches ) | No | boolean | No | - | AcceptEmptyClosedBatches is a flag to enable or disable the acceptance of empty batches.<br />if true, the synchronizer will accept empty batches and process them. |
| - [ReprocessFullBatchOnClose](#Synchronizer_L2Synchronization_ReprocessFullBatchOnClose ) | No | boolean | No | - | ReprocessFullBatchOnClose if is true when a batch is closed is force to reprocess again |
| - [CheckLastL2BlockHashOnCloseBatch](#Synchronizer_L2Synchronization_CheckLastL2BlockHashOnCloseBatch ) | No | boolean | No | - | CheckLastL2BlockHashOnCloseBatch if is true when a batch is closed is force to check the last L2Block hash |

#### <a name="Synchronizer_L2Synchronization_AcceptEmptyClosedBatches"></a>9.10.1. `Synchronizer.L2Synchronization.AcceptEmptyClosedBatches`
#### <a name="Synchronizer_L2Synchronization_Enabled"></a>9.10.1. `Synchronizer.L2Synchronization.Enabled`

**Type:** : `boolean`

**Default:** `true`

**Description:** If enabled then the L2 sync process is permitted (only for permissionless)

**Example setting the default value** (true):
```
[Synchronizer.L2Synchronization]
Enabled=true
```

#### <a name="Synchronizer_L2Synchronization_AcceptEmptyClosedBatches"></a>9.10.2. `Synchronizer.L2Synchronization.AcceptEmptyClosedBatches`

**Type:** : `boolean`

Expand All @@ -1894,7 +1909,7 @@ if true, the synchronizer will accept empty batches and process them.
AcceptEmptyClosedBatches=false
```

#### <a name="Synchronizer_L2Synchronization_ReprocessFullBatchOnClose"></a>9.10.2. `Synchronizer.L2Synchronization.ReprocessFullBatchOnClose`
#### <a name="Synchronizer_L2Synchronization_ReprocessFullBatchOnClose"></a>9.10.3. `Synchronizer.L2Synchronization.ReprocessFullBatchOnClose`

**Type:** : `boolean`

Expand All @@ -1908,7 +1923,7 @@ AcceptEmptyClosedBatches=false
ReprocessFullBatchOnClose=true
```

#### <a name="Synchronizer_L2Synchronization_CheckLastL2BlockHashOnCloseBatch"></a>9.10.3. `Synchronizer.L2Synchronization.CheckLastL2BlockHashOnCloseBatch`
#### <a name="Synchronizer_L2Synchronization_CheckLastL2BlockHashOnCloseBatch"></a>9.10.4. `Synchronizer.L2Synchronization.CheckLastL2BlockHashOnCloseBatch`

**Type:** : `boolean`

Expand Down
13 changes: 9 additions & 4 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -539,9 +539,9 @@
},
"L1BlockCheck": {
"properties": {
"Enable": {
"Enabled": {
"type": "boolean",
"description": "Enable if is true then the check l1 Block Hash is active",
"description": "If enabled then the check l1 Block Hash is active",
"default": true
},
"L1SafeBlockPoint": {
Expand All @@ -564,9 +564,9 @@
"description": "ForceCheckBeforeStart if is true then the first time the system is started it will force to check all pending blocks",
"default": true
},
"PreCheckEnable": {
"PreCheckEnabled": {
"type": "boolean",
"description": "PreCheckEnable if is true then the pre-check is active, will check blocks between L1SafeBlock and L1PreSafeBlock",
"description": "If enabled then the pre-check is active, will check blocks between L1SafeBlock and L1PreSafeBlock",
"default": true
},
"L1PreSafeBlockPoint": {
Expand Down Expand Up @@ -698,6 +698,11 @@
},
"L2Synchronization": {
"properties": {
"Enabled": {
"type": "boolean",
"description": "If enabled then the L2 sync process is permitted (only for permissionless)",
"default": true
},
"AcceptEmptyClosedBatches": {
"type": "boolean",
"description": "AcceptEmptyClosedBatches is a flag to enable or disable the acceptance of empty batches.\nif true, the synchronizer will accept empty batches and process them.",
Expand Down
10 changes: 5 additions & 5 deletions synchronizer/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ type Config struct {

// L1BlockCheckConfig Configuration for L1 Block Checker
type L1BlockCheckConfig struct {
// Enable if is true then the check l1 Block Hash is active
Enable bool `mapstructure:"Enable"`
// If enabled then the check l1 Block Hash is active
Enabled bool `mapstructure:"Enabled"`
// L1SafeBlockPoint is the point that a block is considered safe enough to be checked
// it can be: finalized, safe,pending or latest
L1SafeBlockPoint string `mapstructure:"L1SafeBlockPoint" jsonschema:"enum=finalized,enum=safe, enum=pending,enum=latest"`
Expand All @@ -49,8 +49,8 @@ type L1BlockCheckConfig struct {
// ForceCheckBeforeStart if is true then the first time the system is started it will force to check all pending blocks
ForceCheckBeforeStart bool `mapstructure:"ForceCheckBeforeStart"`

// PreCheckEnable if is true then the pre-check is active, will check blocks between L1SafeBlock and L1PreSafeBlock
PreCheckEnable bool `mapstructure:"PreCheckEnable"`
// If enabled then the pre-check is active, will check blocks between L1SafeBlock and L1PreSafeBlock
PreCheckEnabled bool `mapstructure:"PreCheckEnabled"`
// L1PreSafeBlockPoint is the point that a block is considered safe enough to be checked
// it can be: finalized, safe,pending or latest
L1PreSafeBlockPoint string `mapstructure:"L1PreSafeBlockPoint" jsonschema:"enum=finalized,enum=safe, enum=pending,enum=latest"`
Expand All @@ -61,7 +61,7 @@ type L1BlockCheckConfig struct {
}

func (c *L1BlockCheckConfig) String() string {
return fmt.Sprintf("Enable: %v, L1SafeBlockPoint: %s, L1SafeBlockOffset: %d, ForceCheckBeforeStart: %v", c.Enable, c.L1SafeBlockPoint, c.L1SafeBlockOffset, c.ForceCheckBeforeStart)
return fmt.Sprintf("Enable: %v, L1SafeBlockPoint: %s, L1SafeBlockOffset: %d, ForceCheckBeforeStart: %v", c.Enabled, c.L1SafeBlockPoint, c.L1SafeBlockOffset, c.ForceCheckBeforeStart)
}

// L1ParallelSynchronizationConfig Configuration for parallel mode (if UL1SynchronizationMode equal to 'parallel')
Expand Down
2 changes: 2 additions & 0 deletions synchronizer/l2_sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package l2_sync

// Config configuration of L2 sync process
type Config struct {
// If enabled then the L2 sync process is permitted (only for permissionless)
Enabled bool `mapstructure:"Enabled"`
// AcceptEmptyClosedBatches is a flag to enable or disable the acceptance of empty batches.
// if true, the synchronizer will accept empty batches and process them.
AcceptEmptyClosedBatches bool `mapstructure:"AcceptEmptyClosedBatches"`
Expand Down
13 changes: 8 additions & 5 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,13 @@ func NewSynchronizer(
syncBlockProtection: syncBlockProtection,
halter: syncCommon.NewCriticalErrorHalt(eventLog, 5*time.Second), //nolint:gomnd
}
if cfg.L1BlockCheck.Enable {
if cfg.L1BlockCheck.Enabled {
log.Infof("L1BlockChecker enabled: %s", cfg.L1BlockCheck.String())
l1BlockChecker := l1_check_block.NewCheckL1BlockHash(ethMan, res.state,
l1_check_block.NewSafeL1BlockNumberFetch(l1_check_block.StringToL1BlockPoint(cfg.L1BlockCheck.L1SafeBlockPoint), cfg.L1BlockCheck.L1SafeBlockOffset))

var preCheckAsync syncinterfaces.AsyncL1BlockChecker
if cfg.L1BlockCheck.PreCheckEnable {
if cfg.L1BlockCheck.PreCheckEnabled {
log.Infof("L1BlockChecker enabled precheck from: %s/%d to: %s/%d",
cfg.L1BlockCheck.L1SafeBlockPoint, cfg.L1BlockCheck.L1SafeBlockOffset,
cfg.L1BlockCheck.L1PreSafeBlockPoint, cfg.L1BlockCheck.L1PreSafeBlockOffset)
Expand All @@ -153,7 +153,7 @@ func NewSynchronizer(
time.Second)
}

if !isTrustedSequencer {
if !isTrustedSequencer && cfg.L2Synchronization.Enabled {
log.Info("Permissionless: creating and Initializing L2 synchronization components")
L1SyncChecker := l2_sync_etrog.NewCheckSyncStatusToProcessBatch(res.zkEVMClient, res.state)
sync := &res
Expand All @@ -172,7 +172,10 @@ func NewSynchronizer(
uint64(state.FORKID_ELDERBERRY): syncTrustedStateEtrog,
uint64(state.FORKID_ELDERBERRY_2): syncTrustedStateEtrog,
}, res.state)
} else {
log.Info("L2 synchronization disabled or running in trusted sequencer mode")
}

var l1checkerL2Blocks *actions.CheckL2BlockHash
if cfg.L1SyncCheckL2BlockHash {
if !isTrustedSequencer {
Expand Down Expand Up @@ -457,7 +460,7 @@ func (s *ClientSynchronizer) Sync() error {
// latestSequencedBatchNumber -> last batch on SMC
if latestSyncedBatch >= latestSequencedBatchNumber {
startTrusted := time.Now()
if s.syncTrustedStateExecutor != nil && !s.isTrustedSequencer {
if s.syncTrustedStateExecutor != nil {
log.Info("Syncing trusted state (permissionless)")
//Sync Trusted State
log.Debug("Doing reorg check before L2 sync")
Expand Down Expand Up @@ -815,7 +818,7 @@ func (s *ClientSynchronizer) ProcessBlockRange(blocks []etherman.Block, order ma
}

func (s *ClientSynchronizer) syncTrustedState(latestSyncedBatch uint64) error {
if s.syncTrustedStateExecutor == nil || s.isTrustedSequencer {
if s.syncTrustedStateExecutor == nil {
return nil
}

Expand Down
35 changes: 30 additions & 5 deletions synchronizer/synchronizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/0xPolygonHermez/zkevm-node/state/runtime/executor"
"github.com/0xPolygonHermez/zkevm-node/synchronizer/common/syncinterfaces"
mock_syncinterfaces "github.com/0xPolygonHermez/zkevm-node/synchronizer/common/syncinterfaces/mocks"
"github.com/0xPolygonHermez/zkevm-node/synchronizer/l2_sync"
syncMocks "github.com/0xPolygonHermez/zkevm-node/synchronizer/mocks"
"github.com/ethereum/go-ethereum/common"
ethTypes "github.com/ethereum/go-ethereum/core/types"
Expand Down Expand Up @@ -131,7 +132,10 @@ func TestForcedBatchEtrog(t *testing.T) {
L1SynchronizationMode: SequentialMode,
SyncBlockProtection: "latest",
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
Enabled: false,
},
L2Synchronization: l2_sync.Config{
Enabled: true,
},
}

Expand Down Expand Up @@ -677,6 +681,9 @@ func setupGenericTest(t *testing.T) (*state.Genesis, *Config, *mocks) {
RollupInfoRetriesSpacing: cfgTypes.Duration{Duration: 1 * time.Second},
FallbackToSequentialModeOnSynchronized: false,
},
L2Synchronization: l2_sync.Config{
Enabled: true,
},
}

m := mocks{
Expand Down Expand Up @@ -933,7 +940,10 @@ func TestReorg(t *testing.T) {
L1SynchronizationMode: SequentialMode,
SyncBlockProtection: "latest",
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
Enabled: false,
},
L2Synchronization: l2_sync.Config{
Enabled: true,
},
}

Expand Down Expand Up @@ -1253,7 +1263,10 @@ func TestLatestSyncedBlockEmpty(t *testing.T) {
L1SynchronizationMode: SequentialMode,
SyncBlockProtection: "latest",
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
Enabled: false,
},
L2Synchronization: l2_sync.Config{
Enabled: true,
},
}

Expand Down Expand Up @@ -1467,7 +1480,10 @@ func TestRegularReorg(t *testing.T) {
L1SynchronizationMode: SequentialMode,
SyncBlockProtection: "latest",
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
Enabled: false,
},
L2Synchronization: l2_sync.Config{
Enabled: true,
},
}

Expand Down Expand Up @@ -1749,7 +1765,10 @@ func TestLatestSyncedBlockEmptyWithExtraReorg(t *testing.T) {
L1SynchronizationMode: SequentialMode,
SyncBlockProtection: "latest",
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
Enabled: false,
},
L2Synchronization: l2_sync.Config{
Enabled: true,
},
}

Expand Down Expand Up @@ -2024,6 +2043,12 @@ func TestCallFromEmptyBlockAndReorg(t *testing.T) {
SyncChunkSize: 3,
L1SynchronizationMode: SequentialMode,
SyncBlockProtection: "latest",
L1BlockCheck: L1BlockCheckConfig{
Enabled: false,
},
L2Synchronization: l2_sync.Config{
Enabled: true,
},
}

m := mocks{
Expand Down

0 comments on commit a59d86f

Please sign in to comment.