Skip to content

Commit f4d6e67

Browse files
author
colinlyguo
committed
address comments
1 parent bd00cb4 commit f4d6e67

File tree

9 files changed

+31
-31
lines changed

9 files changed

+31
-31
lines changed

rollup/abi/validium_abi.go

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

rollup/cmd/rollup_relayer/app/app.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,7 @@ func action(ctx *cli.Context) error {
107107
}
108108

109109
chunkProposer := watcher.NewChunkProposer(subCtx, cfg.L2Config.ChunkProposerConfig, minCodecVersion, genesis.Config, db, registry)
110-
batchProposer := watcher.NewBatchProposer(subCtx, cfg.L2Config.BatchProposerConfig, minCodecVersion, genesis.Config, db, registry)
111-
batchProposer.SetValidiumMode(cfg.L2Config.RelayerConfig.ValidiumMode)
110+
batchProposer := watcher.NewBatchProposer(subCtx, cfg.L2Config.BatchProposerConfig, minCodecVersion, genesis.Config, db, cfg.L2Config.RelayerConfig.ValidiumMode, registry)
112111
bundleProposer := watcher.NewBundleProposer(subCtx, cfg.L2Config.BundleProposerConfig, minCodecVersion, genesis.Config, db, registry)
113112

114113
l2watcher := watcher.NewL2WatcherClient(subCtx, l2client, cfg.L2Config.Confirmations, cfg.L2Config.L2MessageQueueAddress, cfg.L2Config.WithdrawTrieRootSlot, genesis.Config, db, registry)

rollup/internal/controller/relayer/l2_relayer.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,6 +1005,7 @@ func (r *Layer2Relayer) constructCommitBatchPayloadValidium(batchesToSubmit []*d
10051005

10061006
// Get the commitment from the batch data: for validium mode, we use the last L2 block hash as the commitment to the off-chain data
10071007
// Get the last chunk from the last batch to find the end block hash
1008+
// TODO: This is a temporary solution, we might use a larger commitment in the future
10081009
if len(batchesToSubmit) == 0 {
10091010
return nil, 0, 0, fmt.Errorf("no batches to submit")
10101011
}

rollup/internal/controller/watcher/batch_proposer.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type BatchProposer struct {
5454
}
5555

5656
// NewBatchProposer creates a new BatchProposer instance.
57-
func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, minCodecVersion encoding.CodecVersion, chainCfg *params.ChainConfig, db *gorm.DB, reg prometheus.Registerer) *BatchProposer {
57+
func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, minCodecVersion encoding.CodecVersion, chainCfg *params.ChainConfig, db *gorm.DB, validiumMode bool, reg prometheus.Registerer) *BatchProposer {
5858
log.Info("new batch proposer", "batchTimeoutSec", cfg.BatchTimeoutSec, "maxBlobSize", maxBlobSize, "maxUncompressedBatchBytesSize", cfg.MaxUncompressedBatchBytesSize)
5959

6060
p := &BatchProposer{
@@ -65,7 +65,7 @@ func NewBatchProposer(ctx context.Context, cfg *config.BatchProposerConfig, minC
6565
l2BlockOrm: orm.NewL2Block(db),
6666
cfg: cfg,
6767
replayMode: false, // default is false, set to true when using proposer tool
68-
validiumMode: false, // default is false, set to true when using validium mode
68+
validiumMode: validiumMode,
6969
minCodecVersion: minCodecVersion,
7070
chainCfg: chainCfg,
7171

@@ -130,11 +130,6 @@ func (p *BatchProposer) SetReplayDB(replayDB *gorm.DB) {
130130
p.replayMode = true
131131
}
132132

133-
// SetValidiumMode sets the validium mode for the BatchProposer.
134-
func (p *BatchProposer) SetValidiumMode(validiumMode bool) {
135-
p.validiumMode = validiumMode
136-
}
137-
138133
// TryProposeBatch tries to propose a new batches.
139134
func (p *BatchProposer) TryProposeBatch() {
140135
p.batchProposerCircleTotal.Inc()

rollup/internal/controller/watcher/batch_proposer_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func testBatchProposerLimitsCodecV7(t *testing.T) {
100100
DarwinV2Time: new(uint64),
101101
EuclidTime: new(uint64),
102102
EuclidV2Time: new(uint64),
103-
}, db, nil)
103+
}, db, false /* rollup mode */, nil)
104104
bp.TryProposeBatch()
105105

106106
batches, err := batchOrm.GetBatches(context.Background(), map[string]interface{}{}, []string{}, 0)
@@ -178,7 +178,7 @@ func testBatchProposerBlobSizeLimitCodecV7(t *testing.T) {
178178
MaxChunksPerBatch: math.MaxInt32,
179179
BatchTimeoutSec: math.MaxUint32,
180180
MaxUncompressedBatchBytesSize: math.MaxUint64,
181-
}, encoding.CodecV7, chainConfig, db, nil)
181+
}, encoding.CodecV7, chainConfig, db, false /* rollup mode */, nil)
182182

183183
for i := 0; i < 2; i++ {
184184
bp.TryProposeBatch()
@@ -246,7 +246,7 @@ func testBatchProposerMaxChunkNumPerBatchLimitCodecV7(t *testing.T) {
246246
MaxChunksPerBatch: 45,
247247
BatchTimeoutSec: math.MaxUint32,
248248
MaxUncompressedBatchBytesSize: math.MaxUint64,
249-
}, encoding.CodecV7, chainConfig, db, nil)
249+
}, encoding.CodecV7, chainConfig, db, false /* rollup mode */, nil)
250250
bp.TryProposeBatch()
251251

252252
batches, err := batchOrm.GetBatches(context.Background(), map[string]interface{}{}, []string{}, 0)
@@ -335,7 +335,7 @@ func testBatchProposerUncompressedBatchBytesLimitCodecV8(t *testing.T) {
335335
MaxChunksPerBatch: math.MaxInt32, // No chunk count limit
336336
BatchTimeoutSec: math.MaxUint32, // No timeout limit
337337
MaxUncompressedBatchBytesSize: 4 * 1024, // 4KiB limit
338-
}, encoding.CodecV8, chainConfig, db, nil)
338+
}, encoding.CodecV8, chainConfig, db, false /* rollup mode */, nil)
339339

340340
bp.TryProposeBatch()
341341

rollup/internal/controller/watcher/bundle_proposer_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func testBundleProposerLimitsCodecV7(t *testing.T) {
103103
MaxChunksPerBatch: math.MaxInt32,
104104
BatchTimeoutSec: 0,
105105
MaxUncompressedBatchBytesSize: math.MaxUint64,
106-
}, encoding.CodecV7, chainConfig, db, nil)
106+
}, encoding.CodecV7, chainConfig, db, false /* rollup mode */, nil)
107107

108108
cp.TryProposeChunk() // chunk1 contains block1
109109
bap.TryProposeBatch() // batch1 contains chunk1

rollup/internal/controller/watcher/proposer_tool.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func NewProposerTool(ctx context.Context, cancel context.CancelFunc, cfg *config
125125

126126
chunkProposer := NewChunkProposer(ctx, cfg.L2Config.ChunkProposerConfig, minCodecVersion, chainCfg, db, nil)
127127
chunkProposer.SetReplayDB(dbForReplay)
128-
batchProposer := NewBatchProposer(ctx, cfg.L2Config.BatchProposerConfig, minCodecVersion, chainCfg, db, nil)
128+
batchProposer := NewBatchProposer(ctx, cfg.L2Config.BatchProposerConfig, minCodecVersion, chainCfg, db, false /* rollup mode */, nil)
129129
batchProposer.SetReplayDB(dbForReplay)
130130
bundleProposer := NewBundleProposer(ctx, cfg.L2Config.BundleProposerConfig, minCodecVersion, chainCfg, db, nil)
131131

rollup/internal/utils/utils.go

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -125,19 +125,28 @@ type BatchMetadata struct {
125125
}
126126

127127
// encodeBatchHeaderValidium encodes batch header for validium mode and returns both encoded bytes and hash
128-
func encodeBatchHeaderValidium(b *encoding.Batch, codecVersion encoding.CodecVersion) ([]byte, common.Hash) {
128+
func encodeBatchHeaderValidium(b *encoding.Batch, codecVersion encoding.CodecVersion) ([]byte, common.Hash, error) {
129129
if b == nil {
130-
return nil, common.Hash{}
130+
return nil, common.Hash{}, fmt.Errorf("batch is nil, version: %v, index: %v", codecVersion, b.Index)
131131
}
132132

133+
if len(b.Blocks) == 0 {
134+
return nil, common.Hash{}, fmt.Errorf("batch contains no blocks, version: %v, index: %v", codecVersion, b.Index)
135+
}
136+
137+
// For validium mode, use the last block hash as commitment to the off-chain data
138+
// TODO: This is a temporary solution, we might use a larger commitment in the future
139+
lastBlock := b.Blocks[len(b.Blocks)-1]
140+
commitment := lastBlock.Header.Hash()
141+
133142
// Batch header field sizes
134143
const (
135144
versionSize = 1
136145
indexSize = 8
137146
parentHashSize = 32
138147
stateRootSize = 32
139148
withdrawRootSize = 32
140-
commitmentSize = 32
149+
commitmentSize = 32 // TODO: 32 bytes for now, might use larger commitment in the future
141150

142151
// Total size of validium batch header
143152
validiumBatchHeaderSize = versionSize + indexSize + parentHashSize + stateRootSize + withdrawRootSize + commitmentSize
@@ -160,17 +169,10 @@ func encodeBatchHeaderValidium(b *encoding.Batch, codecVersion encoding.CodecVer
160169
copy(batchBytes[parentHashOffset:parentHashOffset+parentHashSize], b.ParentBatchHash[0:parentHashSize]) // parentBatchHash
161170
copy(batchBytes[stateRootOffset:stateRootOffset+stateRootSize], b.StateRoot().Bytes()[0:stateRootSize]) // postStateRoot
162171
copy(batchBytes[withdrawRootOffset:withdrawRootOffset+withdrawRootSize], b.WithdrawRoot().Bytes()[0:withdrawRootSize]) // postWithdrawRoot
163-
164-
// For validium mode, use the last block hash as commitment to the off-chain data
165-
var commitment common.Hash
166-
if len(b.Blocks) > 0 {
167-
lastBlock := b.Blocks[len(b.Blocks)-1]
168-
commitment = lastBlock.Header.Hash()
169-
}
170-
copy(batchBytes[commitmentOffset:commitmentOffset+commitmentSize], commitment[0:commitmentSize]) // data commitment
172+
copy(batchBytes[commitmentOffset:commitmentOffset+commitmentSize], commitment[0:commitmentSize]) // data commitment
171173

172174
hash := crypto.Keccak256Hash(batchBytes)
173-
return batchBytes, hash
175+
return batchBytes, hash, nil
174176
}
175177

176178
// GetBatchMetadata retrieves the metadata of a batch.
@@ -195,12 +197,15 @@ func GetBatchMetadata(batch *encoding.Batch, codecVersion encoding.CodecVersion,
195197

196198
// If this function is used in Validium, we encode the batch header differently.
197199
if validiumMode {
198-
batchMeta.BatchBytes, batchMeta.BatchHash = encodeBatchHeaderValidium(batch, codecVersion)
200+
batchMeta.BatchBytes, batchMeta.BatchHash, err = encodeBatchHeaderValidium(batch, codecVersion)
201+
if err != nil {
202+
return nil, fmt.Errorf("failed to encode batch header for validium, version: %v, index: %v, err: %w", codecVersion, batch.Index, err)
203+
}
199204
}
200205

201206
batchMeta.BatchBlobDataProof, err = daBatch.BlobDataProofForPointEvaluation()
202207
if err != nil {
203-
return nil, fmt.Errorf("failed to get blob data proof, version: %v, err: %w", codecVersion, err)
208+
return nil, fmt.Errorf("failed to get blob data proof, version: %v, index: %v, err: %w", codecVersion, batch.Index, err)
204209
}
205210

206211
numChunks := len(batch.Chunks)

rollup/tests/rollup_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func testCommitBatchAndFinalizeBundleCodecV7(t *testing.T) {
128128
MaxChunksPerBatch: math.MaxInt32,
129129
BatchTimeoutSec: 300,
130130
MaxUncompressedBatchBytesSize: math.MaxUint64,
131-
}, encoding.CodecV7, chainConfig, db, nil)
131+
}, encoding.CodecV7, chainConfig, db, false /* rollup mode */, nil)
132132

133133
bup := watcher.NewBundleProposer(context.Background(), &config.BundleProposerConfig{
134134
MaxBatchNumPerBundle: 2,

0 commit comments

Comments
 (0)