@@ -423,7 +423,7 @@ impl TaprootBuilder {
423
423
/// are not provided in DFS walk order. The depth of the root node is 0.
424
424
pub fn add_leaf_with_ver (
425
425
self ,
426
- depth : usize ,
426
+ depth : u8 ,
427
427
script : Script ,
428
428
ver : LeafVersion ,
429
429
) -> Result < Self , TaprootBuilderError > {
@@ -435,13 +435,13 @@ impl TaprootBuilder {
435
435
/// leaves are not provided in DFS walk order. The depth of the root node is 0.
436
436
///
437
437
/// See [`TaprootBuilder::add_leaf_with_ver`] for adding a leaf with specific version.
438
- pub fn add_leaf ( self , depth : usize , script : Script ) -> Result < Self , TaprootBuilderError > {
438
+ pub fn add_leaf ( self , depth : u8 , script : Script ) -> Result < Self , TaprootBuilderError > {
439
439
self . add_leaf_with_ver ( depth, script, LeafVersion :: TapScript )
440
440
}
441
441
442
442
/// Adds a hidden/omitted node at `depth` to the builder. Errors if the leaves are not provided
443
443
/// in DFS walk order. The depth of the root node is 0.
444
- pub fn add_hidden ( self , depth : usize , hash : sha256:: Hash ) -> Result < Self , TaprootBuilderError > {
444
+ pub fn add_hidden ( self , depth : u8 , hash : sha256:: Hash ) -> Result < Self , TaprootBuilderError > {
445
445
let node = NodeInfo :: new_hidden ( hash) ;
446
446
self . insert ( node, depth)
447
447
}
@@ -473,20 +473,20 @@ impl TaprootBuilder {
473
473
}
474
474
475
475
/// Inserts a leaf at `depth`.
476
- fn insert ( mut self , mut node : NodeInfo , mut depth : usize ) -> Result < Self , TaprootBuilderError > {
476
+ fn insert ( mut self , mut node : NodeInfo , mut depth : u8 ) -> Result < Self , TaprootBuilderError > {
477
477
// early error on invalid depth. Though this will be checked later
478
478
// while constructing TaprootMerkelBranch
479
- if depth > TAPROOT_CONTROL_MAX_NODE_COUNT {
480
- return Err ( TaprootBuilderError :: InvalidMerkleTreeDepth ( depth) ) ;
479
+ if depth as usize > TAPROOT_CONTROL_MAX_NODE_COUNT {
480
+ return Err ( TaprootBuilderError :: InvalidMerkleTreeDepth ( depth as usize ) ) ;
481
481
}
482
482
// We cannot insert a leaf at a lower depth while a deeper branch is unfinished. Doing
483
483
// so would mean the add_leaf/add_hidden invocations do not correspond to a DFS traversal of a
484
484
// binary tree.
485
- if depth + 1 < self . branch . len ( ) {
485
+ if depth as usize + 1 < self . branch . len ( ) {
486
486
return Err ( TaprootBuilderError :: NodeNotInDfsOrder ) ;
487
487
}
488
488
489
- while self . branch . len ( ) == depth + 1 {
489
+ while self . branch . len ( ) == depth as usize + 1 {
490
490
let child = match self . branch . pop ( ) {
491
491
None => unreachable ! ( "Len of branch checked to be >= 1" ) ,
492
492
Some ( Some ( child) ) => child,
@@ -507,14 +507,14 @@ impl TaprootBuilder {
507
507
depth -= 1 ;
508
508
}
509
509
510
- if self . branch . len ( ) < depth + 1 {
510
+ if self . branch . len ( ) < depth as usize + 1 {
511
511
// add enough nodes so that we can insert node at depth `depth`
512
- let num_extra_nodes = depth + 1 - self . branch . len ( ) ;
512
+ let num_extra_nodes = depth as usize + 1 - self . branch . len ( ) ;
513
513
self . branch
514
514
. extend ( ( 0 ..num_extra_nodes) . into_iter ( ) . map ( |_| None ) ) ;
515
515
}
516
516
// Push the last node to the branch
517
- self . branch [ depth] = Some ( node) ;
517
+ self . branch [ depth as usize ] = Some ( node) ;
518
518
Ok ( self )
519
519
}
520
520
}
@@ -1270,7 +1270,7 @@ mod test {
1270
1270
v : & serde_json:: Value ,
1271
1271
mut builder : TaprootBuilder ,
1272
1272
leaves : & mut Vec < ( Script , LeafVersion ) > ,
1273
- depth : usize ,
1273
+ depth : u8 ,
1274
1274
) -> TaprootBuilder {
1275
1275
if v. is_null ( ) {
1276
1276
// nothing to push
0 commit comments