@@ -69,13 +69,11 @@ proc mergePayloadImpl[LeafType, T](
69
69
70
70
template resetKeys () =
71
71
# Reset cached hashes of touched verticies
72
- for i in 2 .. vids.len:
72
+ for i in 1 .. vids.len:
73
73
db.layersResKey ((root, vids[^ i]), vtxs[^ i])
74
74
75
75
while pos < path.len:
76
76
# Clear existing merkle keys along the traversal path
77
- vids.add cur
78
- vtxs.add vtx
79
77
var psuffix = path.slice (pos)
80
78
let n = psuffix.sharedPrefixLen (vtx.pfx)
81
79
case vtx.vType
@@ -87,13 +85,15 @@ proc mergePayloadImpl[LeafType, T](
87
85
when payload is AristoAccount :
88
86
if AccLeafRef (vtx).account == payload:
89
87
return err (MergeNoAction )
90
- let leafVtx = db.layersPutLeaf ((root, cur), psuffix, payload)
88
+ let leafVtx = db.layersPutDup ((root, cur), AccLeafRef (vtx))
89
+ leafVtx.account = payload
91
90
leafVtx.stoID = AccLeafRef (vtx).stoID
92
91
93
92
else :
94
93
if StoLeafRef (vtx).stoData == payload:
95
94
return err (MergeNoAction )
96
- let leafVtx = db.layersPutLeaf ((root, cur), psuffix, payload)
95
+ let leafVtx = db.layersPutDup ((root, cur), StoLeafRef (vtx))
96
+ leafVtx.stoData = payload
97
97
(leafVtx, nil , nil )
98
98
else :
99
99
# Turn leaf into a branch (or extension) then insert the two leaves
@@ -141,6 +141,8 @@ proc mergePayloadImpl[LeafType, T](
141
141
next = BranchRef (vtx).bVid (nibble)
142
142
143
143
if next.isValid:
144
+ vids.add cur
145
+ vtxs.add vtx
144
146
cur = next
145
147
psuffix = psuffix.slice (n + 1 )
146
148
pos += n + 1
@@ -153,11 +155,10 @@ proc mergePayloadImpl[LeafType, T](
153
155
# There's no vertex at the branch point - insert the payload as a new
154
156
# leaf and update the existing branch
155
157
156
- let brDup = vtx.dup ()
157
- let local = BranchRef (brDup).setUsed (nibble, true )
158
- db.layersPutVtx ((root, cur), brDup)
159
-
160
- let leafVtx = db.layersPutLeaf ((root, local), psuffix.slice (n + 1 ), payload)
158
+ let
159
+ brDup = db.layersPutDup ((root, cur), BranchRef (vtx))
160
+ local = BranchRef (brDup).setUsed (nibble, true )
161
+ leafVtx = db.layersPutLeaf ((root, local), psuffix.slice (n + 1 ), payload)
161
162
162
163
resetKeys ()
163
164
return ok ((leafVtx, nil , nil ))
@@ -238,7 +239,8 @@ proc mergeStorageData*(
238
239
return err (MergeStoAccMissing )
239
240
240
241
let
241
- stoID = AccLeafRef (accHike.legs[^ 1 ].wp.vtx).stoID
242
+ accVtx = AccLeafRef (accHike.legs[^ 1 ].wp.vtx)
243
+ stoID = accVtx.stoID
242
244
243
245
# Provide new storage ID when needed
244
246
useID =
@@ -270,9 +272,8 @@ proc mergeStorageData*(
270
272
271
273
if not stoID.isValid:
272
274
# Make sure that there is an account that refers to that storage trie
273
- let leaf = AccLeafRef ( accHike.legs[^ 1 ].wp.vtx).dup # Dup on modify
275
+ let leaf = db. layersPutDup (( STATE_ROOT_VID , accHike.legs[^ 1 ].wp.vid), accVtx) # Dup on modify
274
276
leaf.stoID = useID
275
- db.layersPutVtx ((STATE_ROOT_VID , accHike.legs[^ 1 ].wp.vid), leaf)
276
277
277
278
ok ()
278
279
0 commit comments