Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* make, docker compose and genesis

* fix init tx

* fix event

* Fix genesis

* fix process forced batch
  • Loading branch information
ARR552 authored Dec 20, 2023
1 parent 14aefd9 commit 91ceee5
Show file tree
Hide file tree
Showing 11 changed files with 193 additions and 78 deletions.
4 changes: 1 addition & 3 deletions etherman/etherman.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ var (
rollupManagerVerifyBatchesSignatureHash = crypto.Keccak256Hash([]byte("VerifyBatches(uint32,uint64,bytes32,bytes32,address)"))
onSequenceBatchesSignatureHash = crypto.Keccak256Hash([]byte("OnSequenceBatches(uint32,uint64)"))
updateRollupSignatureHash = crypto.Keccak256Hash([]byte("UpdateRollup(uint32,uint32,uint64)"))
addExistingRollupSignatureHash = crypto.Keccak256Hash([]byte("AddExistingRollup(uint32,uint64,address,uint64,uint8)"))
addExistingRollupSignatureHash = crypto.Keccak256Hash([]byte("AddExistingRollup(uint32,uint64,address,uint64,uint8,uint64)"))
createNewRollupSignatureHash = crypto.Keccak256Hash([]byte("CreateNewRollup(uint32,uint32,address,uint64,address)"))
obsoleteRollupTypeSignatureHash = crypto.Keccak256Hash([]byte("ObsoleteRollupType(uint32)"))
addNewRollupTypeSignatureHash = crypto.Keccak256Hash([]byte("AddNewRollupType(uint32,address,address,uint64,uint8,bytes32,string)"))
Expand Down Expand Up @@ -694,12 +694,10 @@ func (etherMan *Client) initialSequenceBatches(ctx context.Context, vLog types.L
})

if len(*blocks) == 0 || ((*blocks)[len(*blocks)-1].BlockHash != vLog.BlockHash || (*blocks)[len(*blocks)-1].BlockNumber != vLog.BlockNumber) {
log.Debug("1 init")
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 {
log.Debug("2 init. Block: ", fullBlock.Number())
(*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)
Expand Down
2 changes: 1 addition & 1 deletion sequencer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type stateInterface interface {
CloseBatch(ctx context.Context, receipt state.ProcessingReceipt, dbTx pgx.Tx) error
CloseWIPBatch(ctx context.Context, receipt state.ProcessingReceipt, dbTx pgx.Tx) error
ExecuteBatch(ctx context.Context, batch state.Batch, updateMerkleTree bool, dbTx pgx.Tx) (*executor.ProcessBatchResponse, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
GetForcedBatch(ctx context.Context, forcedBatchNumber uint64, dbTx pgx.Tx) (*state.ForcedBatch, error)
GetLastBatch(ctx context.Context, dbTx pgx.Tx) (*state.Batch, error)
GetLastBatchNumber(ctx context.Context, dbTx pgx.Tx) (uint64, error)
Expand Down
18 changes: 9 additions & 9 deletions sequencer/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 18 additions & 11 deletions state/batchV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (s *State) ProcessBatchV2(ctx context.Context, request ProcessRequest, upda
}

// ExecuteBatchV2 is used by the synchronizer to reprocess batches to compare generated state root vs stored one
func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoTree L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error) {
func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoTree L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error) {
if dbTx == nil {
return nil, ErrDBTxNil
}
Expand Down Expand Up @@ -153,11 +153,16 @@ func (s *State) ExecuteBatchV2(ctx context.Context, batch Batch, l1InfoTree L1In
ContextId: uuid.NewString(),
SkipVerifyL1InfoRoot: skipVerifyL1InfoRoot,
}
processBatchRequest.L1InfoTreeData = map[uint32]*executor.L1DataV2{l1InfoTree.L1InfoTreeIndex: {
GlobalExitRoot: l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.GlobalExitRoot.Bytes(),
BlockHashL1: l1InfoTree.L1InfoTreeLeaf.PreviousBlockHash.Bytes(),
MinTimestamp: uint64(l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.Timestamp.Unix()),
}}

if forcedBlockHashL1 != nil {
processBatchRequest.ForcedBlockhashL1 = forcedBlockHashL1.Bytes()
} else {
processBatchRequest.L1InfoTreeData = map[uint32]*executor.L1DataV2{l1InfoTree.L1InfoTreeIndex: {
GlobalExitRoot: l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.GlobalExitRoot.Bytes(),
BlockHashL1: l1InfoTree.L1InfoTreeLeaf.PreviousBlockHash.Bytes(),
MinTimestamp: uint64(l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.Timestamp.Unix()),
}}
}

// Send Batch to the Executor
log.Debugf("ExecuteBatchV2[processBatchRequest.OldBatchNum]: %v", processBatchRequest.OldBatchNum)
Expand Down Expand Up @@ -244,16 +249,18 @@ func (s *State) processBatchV2(ctx context.Context, batchNumber uint64, batchL2D
ForkId: forkID,
ContextId: uuid.NewString(),
SkipVerifyL1InfoRoot: skipVerifyL1InfoRoot,
L1InfoRoot: l1InfoTree.L1InfoTreeRoot.Bytes(),
}

processBatchRequest.L1InfoTreeData = map[uint32]*executor.L1DataV2{l1InfoTree.L1InfoTreeIndex: {
GlobalExitRoot: l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.GlobalExitRoot.Bytes(),
BlockHashL1: l1InfoTree.L1InfoTreeLeaf.PreviousBlockHash.Bytes(),
MinTimestamp: uint64(l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.Timestamp.Unix()),
}}
if forcedBlockHashL1 != nil {
log.Debug("Setting ForcedBlockhashL1: ", forcedBlockHashL1)
processBatchRequest.ForcedBlockhashL1 = forcedBlockHashL1.Bytes()
} else {
processBatchRequest.L1InfoTreeData = map[uint32]*executor.L1DataV2{l1InfoTree.L1InfoTreeIndex: {
GlobalExitRoot: l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.GlobalExitRoot.Bytes(),
BlockHashL1: l1InfoTree.L1InfoTreeLeaf.PreviousBlockHash.Bytes(),
MinTimestamp: uint64(l1InfoTree.L1InfoTreeLeaf.GlobalExitRoot.Timestamp.Unix()),
}}
}

if l1InfoTree.L1InfoTreeRoot != (common.Hash{}) {
Expand Down
55 changes: 36 additions & 19 deletions synchronizer/actions/etrog/processor_l1_sequence_batches.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type stateProcessSequenceBatches interface {
GetNextForcedBatches(ctx context.Context, nextForcedBatches int, dbTx pgx.Tx) ([]state.ForcedBatch, error)
GetBatchByNumber(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) (*state.Batch, error)
ProcessAndStoreClosedBatchV2(ctx context.Context, processingCtx state.ProcessingContextV2, dbTx pgx.Tx, caller metrics.CallerLabel) (common.Hash, uint64, string, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
AddAccumulatedInputHash(ctx context.Context, batchNum uint64, accInputHash common.Hash, dbTx pgx.Tx) error
ResetTrustedState(ctx context.Context, batchNumber uint64, dbTx pgx.Tx) error
AddSequence(ctx context.Context, sequence state.Sequence, dbTx pgx.Tx) error
Expand Down Expand Up @@ -117,6 +117,12 @@ func (g *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
Coinbase: sbatch.Coinbase,
BatchL2Data: sbatch.PolygonRollupBaseEtrogBatchData.Transactions,
}
var (
processCtx state.ProcessingContextV2
currentL1InfoTree state.L1InfoTreeExitRootStorageEntry
forcedBlockHashL1 *common.Hash
err error
)
// ForcedBatch must be processed
if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber != 1 { // If this is true means that the batch is forced
log.Debug("FORCED BATCH SEQUENCED!")
Expand Down Expand Up @@ -155,27 +161,38 @@ func (g *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
}
log.Debug("Setting forcedBatchNum: ", forcedBatches[0].ForcedBatchNumber)
batch.ForcedBatchNum = &forcedBatches[0].ForcedBatchNumber
}
// Now we need to check the batch. ForcedBatches should be already stored in the batch table because this is done by the sequencer
var (
processCtx state.ProcessingContextV2
currentL1InfoTree state.L1InfoTreeExitRootStorageEntry
err error
)
if sbatch.BatchNumber == 1 {
currentL1InfoTree = state.L1InfoTreeExitRootStorageEntry{
L1InfoTreeLeaf: state.L1InfoTreeLeaf{
PreviousBlockHash: sbatch.PolygonRollupBaseEtrogBatchData.ForcedBlockHashL1,
GlobalExitRoot: state.GlobalExitRoot{
Timestamp: time.Unix(int64(sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp), 0),
GlobalExitRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
},
},
L1InfoTreeRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
}
} else if sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp > 0 && sbatch.BatchNumber == 1 {
log.Debug("Processing initial batch")
l1InfoRoot := g.state.GetCurrentL1InfoRoot()
var forcedBlockHashL1 common.Hash = sbatch.PolygonRollupBaseEtrogBatchData.ForcedBlockHashL1
var fBHL1 common.Hash = sbatch.PolygonRollupBaseEtrogBatchData.ForcedBlockHashL1
forcedBlockHashL1 = &fBHL1
txs := sbatch.PolygonRollupBaseEtrogBatchData.Transactions
processCtx = state.ProcessingContextV2{
BatchNumber: 1,
Coinbase: sbatch.SequencerAddr,
Timestamp: time.Unix(int64(sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp), 0),
L1InfoRoot: state.L1InfoTreeExitRootStorageEntry{
L1InfoTreeRoot: l1InfoRoot,
currentL1InfoTree = state.L1InfoTreeExitRootStorageEntry{
L1InfoTreeLeaf: state.L1InfoTreeLeaf{
PreviousBlockHash: fBHL1,
GlobalExitRoot: state.GlobalExitRoot{
Timestamp: time.Unix(int64(sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp), 0),
GlobalExitRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
},
},
L1InfoTreeRoot: sbatch.PolygonRollupBaseEtrogBatchData.ForcedGlobalExitRoot,
}
processCtx = state.ProcessingContextV2{
BatchNumber: 1,
Coinbase: sbatch.SequencerAddr,
Timestamp: time.Unix(int64(sbatch.PolygonRollupBaseEtrogBatchData.ForcedTimestamp), 0),
L1InfoRoot: currentL1InfoTree,
BatchL2Data: &txs,
ForcedBlockHashL1: &forcedBlockHashL1,
ForcedBlockHashL1: forcedBlockHashL1,
SkipVerifyL1InfoRoot: 1,
}
} else {
Expand Down Expand Up @@ -237,7 +254,7 @@ func (g *ProcessorL1SequenceBatchesEtrog) processSequenceBatches(ctx context.Con
} else {
// Reprocess batch to compare the stateRoot with tBatch.StateRoot and get accInputHash
var skipVerifyL1InfoRoot uint32 = 0 // false
p, err := g.state.ExecuteBatchV2(ctx, batch, currentL1InfoTree, now, false, skipVerifyL1InfoRoot, dbTx)
p, err := g.state.ExecuteBatchV2(ctx, batch, currentL1InfoTree, processCtx.Timestamp, false, skipVerifyL1InfoRoot, processCtx.ForcedBlockHashL1, dbTx)
if err != nil {
log.Errorf("error executing L1 batch: %+v, error: %v", batch, err)
rollbackErr := dbTx.Rollback(ctx)
Expand Down
2 changes: 1 addition & 1 deletion synchronizer/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ type stateInterface interface {
StoreTransaction(ctx context.Context, batchNumber uint64, processedTx *state.ProcessTransactionResponse, coinbase common.Address, timestamp uint64, egpLog *state.EffectiveGasPriceLog, dbTx pgx.Tx) (*state.L2Header, error)
GetStateRootByBatchNumber(ctx context.Context, batchNum uint64, dbTx pgx.Tx) (common.Hash, error)
ExecuteBatch(ctx context.Context, batch state.Batch, updateMerkleTree bool, dbTx pgx.Tx) (*executor.ProcessBatchResponse, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
ExecuteBatchV2(ctx context.Context, batch state.Batch, l1InfoTree state.L1InfoTreeExitRootStorageEntry, timestampLimit time.Time, updateMerkleTree bool, skipVerifyL1InfoRoot uint32, forcedBlockHashL1 *common.Hash, dbTx pgx.Tx) (*executor.ProcessBatchResponseV2, error)
GetLastVerifiedBatch(ctx context.Context, dbTx pgx.Tx) (*state.VerifiedBatch, error)
GetLastVirtualBatchNum(ctx context.Context, dbTx pgx.Tx) (uint64, error)
AddSequence(ctx context.Context, sequence state.Sequence, dbTx pgx.Tx) error
Expand Down
18 changes: 9 additions & 9 deletions synchronizer/mock_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 10 additions & 8 deletions synchronizer/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,15 +262,17 @@ func (s *ClientSynchronizer) Sync() error {
}
return err
}
err = s.l1EventProcessors.Process(s.ctx, actions.ForkIdType(blocks[0].ForkIDs[0].ForkID), etherman.Order{Name: etherman.SequenceBatchesOrder, Pos: 0}, &blocks[0], dbTx)
if err != nil {
log.Error("error storing initial tx (batch 1): ", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
if len(blocks[0].SequencedBatches) != 0 {
err = s.l1EventProcessors.Process(s.ctx, actions.ForkIdType(blocks[0].ForkIDs[0].ForkID), etherman.Order{Name: etherman.SequenceBatchesOrder, Pos: 0}, &blocks[0], dbTx)
if err != nil {
log.Error("error storing initial tx (batch 1): ", err)
rollbackErr := dbTx.Rollback(s.ctx)
if rollbackErr != nil {
log.Errorf("error rolling back state. RollbackErr: %v, err: %s", rollbackErr, err.Error())
return rollbackErr
}
return err
}
return err
}

if genesisRoot != s.genesis.Root {
Expand Down
Loading

0 comments on commit 91ceee5

Please sign in to comment.