@@ -61,13 +61,14 @@ func (s *SMT) GenerateFromKVBulk(ctx context.Context, logPrefix string, nodeKeys
61
61
62
62
var buildSmtLoopErr error
63
63
var rootNode * SmtNode
64
+ var maxDepth int
64
65
tempTreeBuildStart := time .Now ()
65
66
leafValueMap := sync.Map {}
66
67
accountValuesReadChan := make (chan * utils.NodeValue8 , 1024 )
67
68
go func () {
68
69
defer wg .Done ()
69
70
defer deletesWorker .Stop ()
70
- rootNode , buildSmtLoopErr = runBuildSmtLoop (s , logPrefix , nodeKeys , & leafValueMap , deletesWorker , accountValuesReadChan )
71
+ rootNode , maxDepth , buildSmtLoopErr = runBuildSmtLoop (s , logPrefix , nodeKeys , & leafValueMap , deletesWorker , accountValuesReadChan )
71
72
}()
72
73
73
74
// startBuildSmtLoopDbCompanionLoop is blocking operation. It continue only when the last result is saved
@@ -124,10 +125,14 @@ func (s *SMT) GenerateFromKVBulk(ctx context.Context, logPrefix string, nodeKeys
124
125
return [4 ]uint64 {}, err
125
126
}
126
127
128
+ if err := s .updateDepth (maxDepth ); err != nil {
129
+ return [4 ]uint64 {}, err
130
+ }
131
+
127
132
return finalRoot , nil
128
133
}
129
134
130
- func runBuildSmtLoop (s * SMT , logPrefix string , nodeKeys []utils.NodeKey , leafValueMap * sync.Map , deletesWorker * utils.Worker , accountValuesReadChan <- chan * utils.NodeValue8 ) (* SmtNode , error ) {
135
+ func runBuildSmtLoop (s * SMT , logPrefix string , nodeKeys []utils.NodeKey , leafValueMap * sync.Map , deletesWorker * utils.Worker , accountValuesReadChan <- chan * utils.NodeValue8 ) (* SmtNode , int , error ) {
131
136
totalKeysCount := len (nodeKeys )
132
137
insertedKeysCount := uint64 (0 )
133
138
maxReachedLevel := 0
@@ -148,7 +153,7 @@ func runBuildSmtLoop(s *SMT, logPrefix string, nodeKeys []utils.NodeKey, leafVal
148
153
keys := k .GetPath ()
149
154
vPointer := <- accountValuesReadChan
150
155
if vPointer == nil {
151
- return nil , fmt .Errorf ("the actual error is returned by main DB thread" )
156
+ return nil , 0 , fmt .Errorf ("the actual error is returned by main DB thread" )
152
157
}
153
158
v := * vPointer
154
159
leafValueMap .Store (k , & v )
@@ -202,7 +207,7 @@ func runBuildSmtLoop(s *SMT, logPrefix string, nodeKeys []utils.NodeKey, leafVal
202
207
//sanity check - new leaf should be on the right side
203
208
//otherwise something went wrong
204
209
if leaf0 .rKey [level2 ] != 0 || keys [level2 + level ] != 1 {
205
- return nil , fmt .Errorf (
210
+ return nil , 0 , fmt .Errorf (
206
211
"leaf insert error. new leaf should be on the right of the old, oldLeaf: %v, newLeaf: %v" ,
207
212
append (keys [:level + 1 ], leaf0 .rKey [level2 :]... ),
208
213
keys ,
@@ -264,7 +269,7 @@ func runBuildSmtLoop(s *SMT, logPrefix string, nodeKeys []utils.NodeKey, leafVal
264
269
// this is case for 1 leaf inserted to the left of the root node
265
270
if len (siblings ) == 0 && keys [0 ] == 0 {
266
271
if upperNode .node0 != nil {
267
- return nil , fmt .Errorf ("tried to override left node" )
272
+ return nil , 0 , fmt .Errorf ("tried to override left node" )
268
273
}
269
274
upperNode .node0 = newNode
270
275
} else {
@@ -273,7 +278,7 @@ func runBuildSmtLoop(s *SMT, logPrefix string, nodeKeys []utils.NodeKey, leafVal
273
278
//the new leaf should be on the right side
274
279
//otherwise something went wrong
275
280
if upperNode .node1 != nil || keys [level ] != 1 {
276
- return nil , fmt .Errorf (
281
+ return nil , 0 , fmt .Errorf (
277
282
"leaf insert error. new should be on the right of the found node, foundNode: %v, newLeafKey: %v" ,
278
283
upperNode .node1 ,
279
284
keys ,
@@ -318,9 +323,7 @@ func runBuildSmtLoop(s *SMT, logPrefix string, nodeKeys []utils.NodeKey, leafVal
318
323
progressChan <- uint64 (totalKeysCount ) + insertedKeysCount
319
324
}
320
325
321
- s .updateDepth (maxReachedLevel )
322
-
323
- return & rootNode , nil
326
+ return & rootNode , maxReachedLevel , nil
324
327
}
325
328
326
329
func startBuildSmtLoopDbCompanionLoop (s * SMT , nodeKeys []utils.NodeKey , jobResultsChannel chan utils.JobResult , accountValuesReadChan chan * utils.NodeValue8 ) error {
0 commit comments