@@ -219,54 +219,69 @@ func unsetInternal(n node, left []byte, right []byte) error {
219219 if len (left ) != len (right ) {
220220 return errors .New ("inconsistent edge path" )
221221 }
222- // Step down to the fork point
223- prefix , pos := prefixLen (left , right ), 0
224- var parent node
222+ // Step down to the fork point. There are two scenarios can happen:
223+ // - the fork point is a shortnode: the left proof MUST point to a
224+ // non-existent key and the key doesn't match with the shortnode
225+ // - the fork point is a fullnode: the left proof can point to an
226+ // existent key or not.
227+ var (
228+ pos = 0
229+ parent node
230+ )
231+ findFork:
225232 for {
226- if pos >= prefix {
227- break
228- }
229233 switch rn := (n ).(type ) {
230234 case * shortNode :
235+ // The right proof must point to an existent key.
231236 if len (right )- pos < len (rn .Key ) || ! bytes .Equal (rn .Key , right [pos :pos + len (rn .Key )]) {
232237 return errors .New ("invalid edge path" )
233238 }
239+ rn .flags = nodeFlag {dirty : true }
234240 // Special case, the non-existent proof points to the same path
235241 // as the existent proof, but the path of existent proof is longer.
236- // In this case, truncate the extra path(it should be recovered
237- // by node insertion).
242+ // In this case, the fork point is this shortnode.
238243 if len (left )- pos < len (rn .Key ) || ! bytes .Equal (rn .Key , left [pos :pos + len (rn .Key )]) {
239- fn := parent .(* fullNode )
240- fn .Children [left [pos - 1 ]] = nil
241- return nil
244+ break findFork
242245 }
243- rn .flags = nodeFlag {dirty : true }
244246 parent = n
245247 n , pos = rn .Val , pos + len (rn .Key )
246248 case * fullNode :
249+ leftnode , rightnode := rn .Children [left [pos ]], rn .Children [right [pos ]]
250+ // The right proof must point to an existent key.
251+ if rightnode == nil {
252+ return errors .New ("invalid edge path" )
253+ }
247254 rn .flags = nodeFlag {dirty : true }
255+ if leftnode != rightnode {
256+ break findFork
257+ }
248258 parent = n
249- n , pos = rn .Children [right [pos ]], pos + 1
259+ n , pos = rn .Children [left [pos ]], pos + 1
250260 default :
251261 panic (fmt .Sprintf ("%T: invalid node: %v" , n , n ))
252262 }
253263 }
254- fn , ok := n .(* fullNode )
255- if ! ok {
256- return errors .New ("the fork point must be a fullnode" )
257- }
258- // Find the fork point! Unset all intermediate references
259- for i := left [prefix ] + 1 ; i < right [prefix ]; i ++ {
260- fn .Children [i ] = nil
261- }
262- fn .flags = nodeFlag {dirty : true }
263- if err := unset (fn , fn .Children [left [prefix ]], left [prefix :], 1 , false ); err != nil {
264- return err
265- }
266- if err := unset (fn , fn .Children [right [prefix ]], right [prefix :], 1 , true ); err != nil {
267- return err
264+ switch rn := n .(type ) {
265+ case * shortNode :
266+ if _ , ok := rn .Val .(valueNode ); ok {
267+ parent .(* fullNode ).Children [right [pos - 1 ]] = nil
268+ return nil
269+ }
270+ return unset (rn , rn .Val , right [pos :], len (rn .Key ), true )
271+ case * fullNode :
272+ for i := left [pos ] + 1 ; i < right [pos ]; i ++ {
273+ rn .Children [i ] = nil
274+ }
275+ if err := unset (rn , rn .Children [left [pos ]], left [pos :], 1 , false ); err != nil {
276+ return err
277+ }
278+ if err := unset (rn , rn .Children [right [pos ]], right [pos :], 1 , true ); err != nil {
279+ return err
280+ }
281+ return nil
282+ default :
283+ panic (fmt .Sprintf ("%T: invalid node: %v" , n , n ))
268284 }
269- return nil
270285}
271286
272287// unset removes all internal node references either the left most or right most.
@@ -314,8 +329,8 @@ func unset(parent node, child node, key []byte, pos int, removeLeft bool) error
314329 // The key of fork shortnode is less than the
315330 // path(it doesn't belong to the range), keep
316331 // it with the cached hash available.
317- return nil
318332 }
333+ return nil
319334 }
320335 if _ , ok := cld .Val .(valueNode ); ok {
321336 fn := parent .(* fullNode )
0 commit comments