@@ -56,7 +56,7 @@ export class SparseMerkleSumTree {
5656 const isRight = Number ( path & 1n ) ;
5757 const branchPromise = isRight ? this . right : this . left ;
5858 const newBranchPromise = branchPromise . then ( ( branch ) =>
59- branch ? this . buildTree ( branch , path , 0 , key , data , value ) : new PendingLeafBranch ( path , key , data , value ) ,
59+ branch ? this . buildTree ( branch , path , key , data , value ) : new PendingLeafBranch ( path , key , data , value ) ,
6060 ) ;
6161
6262 if ( isRight ) {
@@ -89,17 +89,15 @@ export class SparseMerkleSumTree {
8989
9090 private buildTree (
9191 branch : PendingBranch ,
92- remainingPath : bigint ,
93- depth : number ,
92+ keyPath : bigint ,
9493 key : Uint8Array ,
9594 data : Uint8Array ,
9695 value : bigint ,
9796 ) : PendingBranch {
98- const commonPath = calculateCommonPath ( remainingPath , branch . path ) ;
99- const commonPathLength = Number ( commonPath . length ) ;
100- const isRight = Number ( ( remainingPath >> commonPath . length ) & 1n ) ;
97+ const commonPath = calculateCommonPath ( keyPath , branch . path ) ;
98+ const isRight = Number ( ( keyPath >> BigInt ( commonPath . length ) ) & 1n ) ;
10199
102- if ( commonPath . path === remainingPath ) {
100+ if ( commonPath . path === keyPath ) {
103101 throw new LeafInBranchError ( ) ;
104102 }
105103
@@ -109,30 +107,23 @@ export class SparseMerkleSumTree {
109107 throw new LeafOutOfBoundsError ( ) ;
110108 }
111109
112- const oldBranch = new PendingLeafBranch ( branch . path >> commonPath . length , branch . key , branch . data , branch . value ) ;
113- const newBranch = new PendingLeafBranch ( remainingPath >> commonPath . length , key , data , value ) ;
110+ const newBranch = new PendingLeafBranch ( keyPath , key , data , value ) ;
114111 return new PendingNodeBranch (
115112 commonPath . path ,
116- depth + commonPathLength ,
117- isRight ? oldBranch : newBranch ,
118- isRight ? newBranch : oldBranch ,
113+ commonPath . length ,
114+ isRight ? branch : newBranch ,
115+ isRight ? newBranch : branch ,
119116 ) ;
120117 }
121118
122119 // If node branch is split in the middle
123120 if ( commonPath . path < branch . path ) {
124- const newBranch = new PendingLeafBranch ( remainingPath >> commonPath . length , key , data , value ) ;
125- const oldBranch = new PendingNodeBranch (
126- branch . path >> commonPath . length ,
127- branch . depth ,
128- branch . left ,
129- branch . right ,
130- ) ;
121+ const newBranch = new PendingLeafBranch ( keyPath , key , data , value ) ;
131122 return new PendingNodeBranch (
132123 commonPath . path ,
133- depth + commonPathLength ,
134- isRight ? oldBranch : newBranch ,
135- isRight ? newBranch : oldBranch ,
124+ commonPath . length ,
125+ isRight ? branch : newBranch ,
126+ isRight ? newBranch : branch ,
136127 ) ;
137128 }
138129
@@ -141,14 +132,14 @@ export class SparseMerkleSumTree {
141132 branch . path ,
142133 branch . depth ,
143134 branch . left ,
144- this . buildTree ( branch . right , remainingPath >> commonPath . length , depth + commonPathLength , key , data , value ) ,
135+ this . buildTree ( branch . right , keyPath , key , data , value ) ,
145136 ) ;
146137 }
147138
148139 return new PendingNodeBranch (
149140 branch . path ,
150141 branch . depth ,
151- this . buildTree ( branch . left , remainingPath >> commonPath . length , depth + commonPathLength , key , data , value ) ,
142+ this . buildTree ( branch . left , keyPath , key , data , value ) ,
152143 branch . right ,
153144 ) ;
154145 }
0 commit comments