@@ -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 )
0 commit comments