File tree Expand file tree Collapse file tree 1 file changed +8
-4
lines changed
twenty-first/src/util_types Expand file tree Collapse file tree 1 file changed +8
-4
lines changed Original file line number Diff line number Diff line change @@ -316,10 +316,14 @@ impl MerkleTree {
316316
317317 /// All leafs of the Merkle tree.
318318 pub fn leafs ( & self ) -> impl Iterator < Item = & Digest > {
319- self . nodes . iter ( ) . skip (
320- ( self . num_nodes ( ) / 2 )
321- . try_into ( )
322- . expect ( "MerkleTreeNodeIndex to usize conversion error" ) ,
319+ // This conversion can only fail if the number of leafs is larger than
320+ // usize::MAX. This implies that the number of nodes is larger than
321+ // usize::MAX. Since the nodes are stored in a Vec, the number of nodes
322+ // can never exceed usize::MAX.
323+ // This proof by contradiction shows that unwrapping is fine.
324+ let num_leafs = usize:: try_from ( self . num_leafs ( ) ) . unwrap ( ) ;
325+
326+ self . nodes . iter ( ) . skip ( num_leafs)
323327 )
324328 }
325329
You can’t perform that action at this time.
0 commit comments