@@ -229,8 +229,10 @@ pub struct CodedBlock<B: Block, S: Scheme> {
229229 config : CodingConfig ,
230230 /// The erasure coding commitment.
231231 commitment : S :: Commitment ,
232- /// The coded shards, along with corresponding coding proofs.
233- shards : Vec < S :: Shard > ,
232+ /// The coded shards.
233+ ///
234+ /// These shards are optional to enable lazy construction.
235+ shards : Option < Vec < S :: Shard > > ,
234236}
235237
236238impl < B : Block < Commitment = CodingCommitment > , S : Scheme > CodedBlock < B , S > {
@@ -250,7 +252,17 @@ impl<B: Block<Commitment = CodingCommitment>, S: Scheme> CodedBlock<B, S> {
250252 inner,
251253 config,
252254 commitment,
253- shards,
255+ shards : Some ( shards) ,
256+ }
257+ }
258+
259+ /// Create a new [CodedBlock] from a [Block] and trusted [CodingCommitment].
260+ pub fn new_trusted ( inner : B , commitment : CodingCommitment ) -> Self {
261+ Self {
262+ inner,
263+ config : commitment. config ( ) ,
264+ commitment : commitment. inner ( ) ,
265+ shards : None ,
254266 }
255267 }
256268
@@ -260,16 +272,26 @@ impl<B: Block<Commitment = CodingCommitment>, S: Scheme> CodedBlock<B, S> {
260272 }
261273
262274 /// Returns a refernce to the shards in this coded block.
263- pub fn shards ( & self ) -> & [ S :: Shard ] {
264- & self . shards
275+ pub fn shards ( & mut self ) -> & [ S :: Shard ] {
276+ match self . shards {
277+ Some ( ref shards) => shards,
278+ None => {
279+ let ( commitment, shards) = Self :: encode ( & self . inner , self . config ) ;
280+
281+ assert_eq ! ( commitment, self . commitment) ;
282+
283+ self . shards = Some ( shards) ;
284+ self . shards . as_ref ( ) . unwrap ( )
285+ }
286+ }
265287 }
266288
267289 /// Returns a [Shard] at the given index, if the index is valid.
268290 pub fn shard < H : Hasher > ( & self , index : usize ) -> Option < Shard < S , H > > {
269291 Some ( Shard :: new (
270292 self . commitment ( ) ,
271293 index,
272- DistributionShard :: Strong ( self . shards . get ( index) ?. clone ( ) ) ,
294+ DistributionShard :: Strong ( self . shards . as_ref ( ) ? . get ( index) ?. clone ( ) ) ,
273295 ) )
274296 }
275297
@@ -345,7 +367,7 @@ impl<B: Block, S: Scheme> Read for CodedBlock<B, S> {
345367 inner,
346368 config,
347369 commitment,
348- shards,
370+ shards : Some ( shards ) ,
349371 } )
350372 }
351373}
0 commit comments