Skip to content

Commit

Permalink
Feature/3640 synchronizer choose to sync from l2 (0xPolygonHermez#3641)
Browse files Browse the repository at this point in the history
* 0xPolygonHermez#3640. New Param Synchronizer.L2Synchronization.Enable to choose if sync from L2
  • Loading branch information
joanestebanr authored and Stefan-Ethernal committed Jun 26, 2024
1 parent aafc32f commit 7bbad75
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 6 deletions.
4 changes: 4 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ func Test_Defaults(t *testing.T) {
path: "Synchronizer.L2Synchronization.DataSourcePriority",
expectedValue: []dataavailability.DataSourcePriority{"local", "trusted", "external"},
},
{
path: "Synchronizer.L2Synchronization.Enable",
expectedValue: true,
},
{
path: "Sequencer.DeletePoolTxsL1BlockConfirmations",
expectedValue: uint64(100),
Expand Down
1 change: 1 addition & 0 deletions config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ L1SyncCheckL2BlockNumberhModulus = 600
AceptableInacctivityTime = "5s"
ApplyAfterNumRollupReceived = 10
[Synchronizer.L2Synchronization]
Enable = true
AcceptEmptyClosedBatches = false
ReprocessFullBatchOnClose = false
CheckLastL2BlockHashOnCloseBatch = true
Expand Down
20 changes: 17 additions & 3 deletions docs/config-file/node-config-doc.md
Original file line number Diff line number Diff line change
Expand Up @@ -1881,7 +1881,21 @@ FallbackToSequentialModeOnSynchronized=false
| - [CheckLastL2BlockHashOnCloseBatch](#Synchronizer_L2Synchronization_CheckLastL2BlockHashOnCloseBatch ) | No | boolean | No | - | CheckLastL2BlockHashOnCloseBatch if is true when a batch is closed is force to check the last L2Block hash |
| - [DataSourcePriority](#Synchronizer_L2Synchronization_DataSourcePriority ) | No | array of string | No | - | DataSourcePriority defines the order in which L2 batch should be retrieved: local, trusted, external |

#### <a name="Synchronizer_L2Synchronization_AcceptEmptyClosedBatches"></a>9.10.1. `Synchronizer.L2Synchronization.AcceptEmptyClosedBatches`
#### <a name="Synchronizer_L2Synchronization_Enable"></a>9.10.1. `Synchronizer.L2Synchronization.Enable`

**Type:** : `boolean`

**Default:** `true`

**Description:** Enable if is true then the L2 sync process is permitted (only for permissionless)

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

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

**Type:** : `boolean`

Expand All @@ -1896,7 +1910,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 @@ -1910,7 +1924,7 @@ AcceptEmptyClosedBatches=false
ReprocessFullBatchOnClose=false
```

#### <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
5 changes: 5 additions & 0 deletions docs/config-file/node-config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,11 @@
},
"L2Synchronization": {
"properties": {
"Enable": {
"type": "boolean",
"description": "Enable if is true 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
2 changes: 2 additions & 0 deletions synchronizer/l2_sync/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import "github.com/0xPolygonHermez/zkevm-node/dataavailability"

// Config configuration of L2 sync process
type Config struct {
// Enable if is true then the L2 sync process is permitted (only for permissionless)
Enable bool `mapstructure:"Enable"`
// 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
9 changes: 6 additions & 3 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func NewSynchronizer(
time.Second)
}

if !isTrustedSequencer {
if !isTrustedSequencer && cfg.L2Synchronization.Enable {
log.Info("Permissionless: creating and Initializing L2 synchronization components")
L1SyncChecker := l2_sync_etrog.NewCheckSyncStatusToProcessBatch(res.zkEVMClient, res.state)
sync := &res
Expand All @@ -170,7 +170,10 @@ func NewSynchronizer(
uint64(state.FORKID_ELDERBERRY): syncTrustedStateEtrog,
uint64(state.FORKID_9): 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 @@ -443,7 +446,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 @@ -801,7 +804,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
22 changes: 22 additions & 0 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 @@ -132,6 +133,9 @@ func TestForcedBatchEtrog(t *testing.T) {
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
},
L2Synchronization: l2_sync.Config{
Enable: true,
},
}

m := mocks{
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{
Enable: true,
},
}

m := mocks{
Expand Down Expand Up @@ -933,6 +940,9 @@ func TestReorg(t *testing.T) {
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
},
L2Synchronization: l2_sync.Config{
Enable: true,
},
}

m := mocks{
Expand Down Expand Up @@ -1253,6 +1263,9 @@ func TestLatestSyncedBlockEmpty(t *testing.T) {
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
},
L2Synchronization: l2_sync.Config{
Enable: true,
},
}

m := mocks{
Expand Down Expand Up @@ -1467,6 +1480,9 @@ func TestRegularReorg(t *testing.T) {
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
},
L2Synchronization: l2_sync.Config{
Enable: true,
},
}

m := mocks{
Expand Down Expand Up @@ -1749,6 +1765,9 @@ func TestLatestSyncedBlockEmptyWithExtraReorg(t *testing.T) {
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
},
L2Synchronization: l2_sync.Config{
Enable: true,
},
}

m := mocks{
Expand Down Expand Up @@ -2025,6 +2044,9 @@ func TestCallFromEmptyBlockAndReorg(t *testing.T) {
L1BlockCheck: L1BlockCheckConfig{
Enable: false,
},
L2Synchronization: l2_sync.Config{
Enable: true,
},
}

m := mocks{
Expand Down

0 comments on commit 7bbad75

Please sign in to comment.