proof.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package trie
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/rawdb"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. "github.com/ethereum/go-ethereum/log"
  25. )
  26. // Prove constructs a merkle proof for key. The result contains all encoded nodes
  27. // on the path to the value at key. The value itself is also included in the last
  28. // node and can be retrieved by verifying the proof.
  29. //
  30. // If the trie does not contain a value for key, the returned proof contains all
  31. // nodes of the longest existing prefix of the key (at least the root node), ending
  32. // with the node that proves the absence of the key.
  33. func (t *Trie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
  34. // Collect all nodes on the path to key.
  35. var (
  36. prefix []byte
  37. nodes []node
  38. tn = t.root
  39. )
  40. key = keybytesToHex(key)
  41. for len(key) > 0 && tn != nil {
  42. switch n := tn.(type) {
  43. case *shortNode:
  44. if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) {
  45. // The trie doesn't contain the key.
  46. tn = nil
  47. } else {
  48. tn = n.Val
  49. prefix = append(prefix, n.Key...)
  50. key = key[len(n.Key):]
  51. }
  52. nodes = append(nodes, n)
  53. case *fullNode:
  54. tn = n.Children[key[0]]
  55. prefix = append(prefix, key[0])
  56. key = key[1:]
  57. nodes = append(nodes, n)
  58. case hashNode:
  59. var err error
  60. tn, err = t.resolveHash(n, prefix)
  61. if err != nil {
  62. log.Error(fmt.Sprintf("Unhandled trie error: %v", err))
  63. return err
  64. }
  65. default:
  66. panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
  67. }
  68. }
  69. hasher := newHasher(false)
  70. defer returnHasherToPool(hasher)
  71. for i, n := range nodes {
  72. if fromLevel > 0 {
  73. fromLevel--
  74. continue
  75. }
  76. var hn node
  77. n, hn = hasher.proofHash(n)
  78. if hash, ok := hn.(hashNode); ok || i == 0 {
  79. // If the node's database encoding is a hash (or is the
  80. // root node), it becomes a proof element.
  81. enc := nodeToBytes(n)
  82. if !ok {
  83. hash = hasher.hashData(enc)
  84. }
  85. proofDb.Put(hash, enc)
  86. }
  87. }
  88. return nil
  89. }
  90. // Prove constructs a merkle proof for key. The result contains all encoded nodes
  91. // on the path to the value at key. The value itself is also included in the last
  92. // node and can be retrieved by verifying the proof.
  93. //
  94. // If the trie does not contain a value for key, the returned proof contains all
  95. // nodes of the longest existing prefix of the key (at least the root node), ending
  96. // with the node that proves the absence of the key.
  97. func (t *StateTrie) Prove(key []byte, fromLevel uint, proofDb ethdb.KeyValueWriter) error {
  98. return t.trie.Prove(key, fromLevel, proofDb)
  99. }
  100. // VerifyProof checks merkle proofs. The given proof must contain the value for
  101. // key in a trie with the given root hash. VerifyProof returns an error if the
  102. // proof contains invalid trie nodes or the wrong value.
  103. func VerifyProof(rootHash common.Hash, key []byte, proofDb ethdb.KeyValueReader) (value []byte, err error) {
  104. key = keybytesToHex(key)
  105. wantHash := rootHash
  106. for i := 0; ; i++ {
  107. buf, _ := proofDb.Get(wantHash[:])
  108. if buf == nil {
  109. return nil, fmt.Errorf("proof node %d (hash %064x) missing", i, wantHash)
  110. }
  111. n, err := decodeNode(wantHash[:], buf)
  112. if err != nil {
  113. return nil, fmt.Errorf("bad proof node %d: %v", i, err)
  114. }
  115. keyrest, cld := get(n, key, true)
  116. switch cld := cld.(type) {
  117. case nil:
  118. // The trie doesn't contain the key.
  119. return nil, nil
  120. case hashNode:
  121. key = keyrest
  122. copy(wantHash[:], cld)
  123. case valueNode:
  124. return cld, nil
  125. }
  126. }
  127. }
  128. // proofToPath converts a merkle proof to trie node path. The main purpose of
  129. // this function is recovering a node path from the merkle proof stream. All
  130. // necessary nodes will be resolved and leave the remaining as hashnode.
  131. //
  132. // The given edge proof is allowed to be an existent or non-existent proof.
  133. func proofToPath(rootHash common.Hash, root node, key []byte, proofDb ethdb.KeyValueReader, allowNonExistent bool) (node, []byte, error) {
  134. // resolveNode retrieves and resolves trie node from merkle proof stream
  135. resolveNode := func(hash common.Hash) (node, error) {
  136. buf, _ := proofDb.Get(hash[:])
  137. if buf == nil {
  138. return nil, fmt.Errorf("proof node (hash %064x) missing", hash)
  139. }
  140. n, err := decodeNode(hash[:], buf)
  141. if err != nil {
  142. return nil, fmt.Errorf("bad proof node %v", err)
  143. }
  144. return n, err
  145. }
  146. // If the root node is empty, resolve it first.
  147. // Root node must be included in the proof.
  148. if root == nil {
  149. n, err := resolveNode(rootHash)
  150. if err != nil {
  151. return nil, nil, err
  152. }
  153. root = n
  154. }
  155. var (
  156. err error
  157. child, parent node
  158. keyrest []byte
  159. valnode []byte
  160. )
  161. key, parent = keybytesToHex(key), root
  162. for {
  163. keyrest, child = get(parent, key, false)
  164. switch cld := child.(type) {
  165. case nil:
  166. // The trie doesn't contain the key. It's possible
  167. // the proof is a non-existing proof, but at least
  168. // we can prove all resolved nodes are correct, it's
  169. // enough for us to prove range.
  170. if allowNonExistent {
  171. return root, nil, nil
  172. }
  173. return nil, nil, errors.New("the node is not contained in trie")
  174. case *shortNode:
  175. key, parent = keyrest, child // Already resolved
  176. continue
  177. case *fullNode:
  178. key, parent = keyrest, child // Already resolved
  179. continue
  180. case hashNode:
  181. child, err = resolveNode(common.BytesToHash(cld))
  182. if err != nil {
  183. return nil, nil, err
  184. }
  185. case valueNode:
  186. valnode = cld
  187. }
  188. // Link the parent and child.
  189. switch pnode := parent.(type) {
  190. case *shortNode:
  191. pnode.Val = child
  192. case *fullNode:
  193. pnode.Children[key[0]] = child
  194. default:
  195. panic(fmt.Sprintf("%T: invalid node: %v", pnode, pnode))
  196. }
  197. if len(valnode) > 0 {
  198. return root, valnode, nil // The whole path is resolved
  199. }
  200. key, parent = keyrest, child
  201. }
  202. }
  203. // unsetInternal removes all internal node references(hashnode, embedded node).
  204. // It should be called after a trie is constructed with two edge paths. Also
  205. // the given boundary keys must be the one used to construct the edge paths.
  206. //
  207. // It's the key step for range proof. All visited nodes should be marked dirty
  208. // since the node content might be modified. Besides it can happen that some
  209. // fullnodes only have one child which is disallowed. But if the proof is valid,
  210. // the missing children will be filled, otherwise it will be thrown anyway.
  211. //
  212. // Note we have the assumption here the given boundary keys are different
  213. // and right is larger than left.
  214. func unsetInternal(n node, left []byte, right []byte) (bool, error) {
  215. left, right = keybytesToHex(left), keybytesToHex(right)
  216. // Step down to the fork point. There are two scenarios can happen:
  217. // - the fork point is a shortnode: either the key of left proof or
  218. // right proof doesn't match with shortnode's key.
  219. // - the fork point is a fullnode: both two edge proofs are allowed
  220. // to point to a non-existent key.
  221. var (
  222. pos = 0
  223. parent node
  224. // fork indicator, 0 means no fork, -1 means proof is less, 1 means proof is greater
  225. shortForkLeft, shortForkRight int
  226. )
  227. findFork:
  228. for {
  229. switch rn := (n).(type) {
  230. case *shortNode:
  231. rn.flags = nodeFlag{dirty: true}
  232. // If either the key of left proof or right proof doesn't match with
  233. // shortnode, stop here and the forkpoint is the shortnode.
  234. if len(left)-pos < len(rn.Key) {
  235. shortForkLeft = bytes.Compare(left[pos:], rn.Key)
  236. } else {
  237. shortForkLeft = bytes.Compare(left[pos:pos+len(rn.Key)], rn.Key)
  238. }
  239. if len(right)-pos < len(rn.Key) {
  240. shortForkRight = bytes.Compare(right[pos:], rn.Key)
  241. } else {
  242. shortForkRight = bytes.Compare(right[pos:pos+len(rn.Key)], rn.Key)
  243. }
  244. if shortForkLeft != 0 || shortForkRight != 0 {
  245. break findFork
  246. }
  247. parent = n
  248. n, pos = rn.Val, pos+len(rn.Key)
  249. case *fullNode:
  250. rn.flags = nodeFlag{dirty: true}
  251. // If either the node pointed by left proof or right proof is nil,
  252. // stop here and the forkpoint is the fullnode.
  253. leftnode, rightnode := rn.Children[left[pos]], rn.Children[right[pos]]
  254. if leftnode == nil || rightnode == nil || leftnode != rightnode {
  255. break findFork
  256. }
  257. parent = n
  258. n, pos = rn.Children[left[pos]], pos+1
  259. default:
  260. panic(fmt.Sprintf("%T: invalid node: %v", n, n))
  261. }
  262. }
  263. switch rn := n.(type) {
  264. case *shortNode:
  265. // There can have these five scenarios:
  266. // - both proofs are less than the trie path => no valid range
  267. // - both proofs are greater than the trie path => no valid range
  268. // - left proof is less and right proof is greater => valid range, unset the shortnode entirely
  269. // - left proof points to the shortnode, but right proof is greater
  270. // - right proof points to the shortnode, but left proof is less
  271. if shortForkLeft == -1 && shortForkRight == -1 {
  272. return false, errors.New("empty range")
  273. }
  274. if shortForkLeft == 1 && shortForkRight == 1 {
  275. return false, errors.New("empty range")
  276. }
  277. if shortForkLeft != 0 && shortForkRight != 0 {
  278. // The fork point is root node, unset the entire trie
  279. if parent == nil {
  280. return true, nil
  281. }
  282. parent.(*fullNode).Children[left[pos-1]] = nil
  283. return false, nil
  284. }
  285. // Only one proof points to non-existent key.
  286. if shortForkRight != 0 {
  287. if _, ok := rn.Val.(valueNode); ok {
  288. // The fork point is root node, unset the entire trie
  289. if parent == nil {
  290. return true, nil
  291. }
  292. parent.(*fullNode).Children[left[pos-1]] = nil
  293. return false, nil
  294. }
  295. return false, unset(rn, rn.Val, left[pos:], len(rn.Key), false)
  296. }
  297. if shortForkLeft != 0 {
  298. if _, ok := rn.Val.(valueNode); ok {
  299. // The fork point is root node, unset the entire trie
  300. if parent == nil {
  301. return true, nil
  302. }
  303. parent.(*fullNode).Children[right[pos-1]] = nil
  304. return false, nil
  305. }
  306. return false, unset(rn, rn.Val, right[pos:], len(rn.Key), true)
  307. }
  308. return false, nil
  309. case *fullNode:
  310. // unset all internal nodes in the forkpoint
  311. for i := left[pos] + 1; i < right[pos]; i++ {
  312. rn.Children[i] = nil
  313. }
  314. if err := unset(rn, rn.Children[left[pos]], left[pos:], 1, false); err != nil {
  315. return false, err
  316. }
  317. if err := unset(rn, rn.Children[right[pos]], right[pos:], 1, true); err != nil {
  318. return false, err
  319. }
  320. return false, nil
  321. default:
  322. panic(fmt.Sprintf("%T: invalid node: %v", n, n))
  323. }
  324. }
  325. // unset removes all internal node references either the left most or right most.
  326. // It can meet these scenarios:
  327. //
  328. // - The given path is existent in the trie, unset the associated nodes with the
  329. // specific direction
  330. // - The given path is non-existent in the trie
  331. // - the fork point is a fullnode, the corresponding child pointed by path
  332. // is nil, return
  333. // - the fork point is a shortnode, the shortnode is included in the range,
  334. // keep the entire branch and return.
  335. // - the fork point is a shortnode, the shortnode is excluded in the range,
  336. // unset the entire branch.
  337. func unset(parent node, child node, key []byte, pos int, removeLeft bool) error {
  338. switch cld := child.(type) {
  339. case *fullNode:
  340. if removeLeft {
  341. for i := 0; i < int(key[pos]); i++ {
  342. cld.Children[i] = nil
  343. }
  344. cld.flags = nodeFlag{dirty: true}
  345. } else {
  346. for i := key[pos] + 1; i < 16; i++ {
  347. cld.Children[i] = nil
  348. }
  349. cld.flags = nodeFlag{dirty: true}
  350. }
  351. return unset(cld, cld.Children[key[pos]], key, pos+1, removeLeft)
  352. case *shortNode:
  353. if len(key[pos:]) < len(cld.Key) || !bytes.Equal(cld.Key, key[pos:pos+len(cld.Key)]) {
  354. // Find the fork point, it's an non-existent branch.
  355. if removeLeft {
  356. if bytes.Compare(cld.Key, key[pos:]) < 0 {
  357. // The key of fork shortnode is less than the path
  358. // (it belongs to the range), unset the entrie
  359. // branch. The parent must be a fullnode.
  360. fn := parent.(*fullNode)
  361. fn.Children[key[pos-1]] = nil
  362. }
  363. //else {
  364. // The key of fork shortnode is greater than the
  365. // path(it doesn't belong to the range), keep
  366. // it with the cached hash available.
  367. //}
  368. } else {
  369. if bytes.Compare(cld.Key, key[pos:]) > 0 {
  370. // The key of fork shortnode is greater than the
  371. // path(it belongs to the range), unset the entrie
  372. // branch. The parent must be a fullnode.
  373. fn := parent.(*fullNode)
  374. fn.Children[key[pos-1]] = nil
  375. }
  376. //else {
  377. // The key of fork shortnode is less than the
  378. // path(it doesn't belong to the range), keep
  379. // it with the cached hash available.
  380. //}
  381. }
  382. return nil
  383. }
  384. if _, ok := cld.Val.(valueNode); ok {
  385. fn := parent.(*fullNode)
  386. fn.Children[key[pos-1]] = nil
  387. return nil
  388. }
  389. cld.flags = nodeFlag{dirty: true}
  390. return unset(cld, cld.Val, key, pos+len(cld.Key), removeLeft)
  391. case nil:
  392. // If the node is nil, then it's a child of the fork point
  393. // fullnode(it's a non-existent branch).
  394. return nil
  395. default:
  396. panic("it shouldn't happen") // hashNode, valueNode
  397. }
  398. }
  399. // hasRightElement returns the indicator whether there exists more elements
  400. // on the right side of the given path. The given path can point to an existent
  401. // key or a non-existent one. This function has the assumption that the whole
  402. // path should already be resolved.
  403. func hasRightElement(node node, key []byte) bool {
  404. pos, key := 0, keybytesToHex(key)
  405. for node != nil {
  406. switch rn := node.(type) {
  407. case *fullNode:
  408. for i := key[pos] + 1; i < 16; i++ {
  409. if rn.Children[i] != nil {
  410. return true
  411. }
  412. }
  413. node, pos = rn.Children[key[pos]], pos+1
  414. case *shortNode:
  415. if len(key)-pos < len(rn.Key) || !bytes.Equal(rn.Key, key[pos:pos+len(rn.Key)]) {
  416. return bytes.Compare(rn.Key, key[pos:]) > 0
  417. }
  418. node, pos = rn.Val, pos+len(rn.Key)
  419. case valueNode:
  420. return false // We have resolved the whole path
  421. default:
  422. panic(fmt.Sprintf("%T: invalid node: %v", node, node)) // hashnode
  423. }
  424. }
  425. return false
  426. }
  427. // VerifyRangeProof checks whether the given leaf nodes and edge proof
  428. // can prove the given trie leaves range is matched with the specific root.
  429. // Besides, the range should be consecutive (no gap inside) and monotonic
  430. // increasing.
  431. //
  432. // Note the given proof actually contains two edge proofs. Both of them can
  433. // be non-existent proofs. For example the first proof is for a non-existent
  434. // key 0x03, the last proof is for a non-existent key 0x10. The given batch
  435. // leaves are [0x04, 0x05, .. 0x09]. It's still feasible to prove the given
  436. // batch is valid.
  437. //
  438. // The firstKey is paired with firstProof, not necessarily the same as keys[0]
  439. // (unless firstProof is an existent proof). Similarly, lastKey and lastProof
  440. // are paired.
  441. //
  442. // Expect the normal case, this function can also be used to verify the following
  443. // range proofs:
  444. //
  445. // - All elements proof. In this case the proof can be nil, but the range should
  446. // be all the leaves in the trie.
  447. //
  448. // - One element proof. In this case no matter the edge proof is a non-existent
  449. // proof or not, we can always verify the correctness of the proof.
  450. //
  451. // - Zero element proof. In this case a single non-existent proof is enough to prove.
  452. // Besides, if there are still some other leaves available on the right side, then
  453. // an error will be returned.
  454. //
  455. // Except returning the error to indicate the proof is valid or not, the function will
  456. // also return a flag to indicate whether there exists more accounts/slots in the trie.
  457. //
  458. // Note: This method does not verify that the proof is of minimal form. If the input
  459. // proofs are 'bloated' with neighbour leaves or random data, aside from the 'useful'
  460. // data, then the proof will still be accepted.
  461. func VerifyRangeProof(rootHash common.Hash, firstKey []byte, lastKey []byte, keys [][]byte, values [][]byte, proof ethdb.KeyValueReader) (bool, error) {
  462. if len(keys) != len(values) {
  463. return false, fmt.Errorf("inconsistent proof data, keys: %d, values: %d", len(keys), len(values))
  464. }
  465. // Ensure the received batch is monotonic increasing and contains no deletions
  466. for i := 0; i < len(keys)-1; i++ {
  467. if bytes.Compare(keys[i], keys[i+1]) >= 0 {
  468. return false, errors.New("range is not monotonically increasing")
  469. }
  470. }
  471. for _, value := range values {
  472. if len(value) == 0 {
  473. return false, errors.New("range contains deletion")
  474. }
  475. }
  476. // Special case, there is no edge proof at all. The given range is expected
  477. // to be the whole leaf-set in the trie.
  478. if proof == nil {
  479. tr := NewStackTrie(nil)
  480. for index, key := range keys {
  481. tr.TryUpdate(key, values[index])
  482. }
  483. if have, want := tr.Hash(), rootHash; have != want {
  484. return false, fmt.Errorf("invalid proof, want hash %x, got %x", want, have)
  485. }
  486. return false, nil // No more elements
  487. }
  488. // Special case, there is a provided edge proof but zero key/value
  489. // pairs, ensure there are no more accounts / slots in the trie.
  490. if len(keys) == 0 {
  491. root, val, err := proofToPath(rootHash, nil, firstKey, proof, true)
  492. if err != nil {
  493. return false, err
  494. }
  495. if val != nil || hasRightElement(root, firstKey) {
  496. return false, errors.New("more entries available")
  497. }
  498. return false, nil
  499. }
  500. // Special case, there is only one element and two edge keys are same.
  501. // In this case, we can't construct two edge paths. So handle it here.
  502. if len(keys) == 1 && bytes.Equal(firstKey, lastKey) {
  503. root, val, err := proofToPath(rootHash, nil, firstKey, proof, false)
  504. if err != nil {
  505. return false, err
  506. }
  507. if !bytes.Equal(firstKey, keys[0]) {
  508. return false, errors.New("correct proof but invalid key")
  509. }
  510. if !bytes.Equal(val, values[0]) {
  511. return false, errors.New("correct proof but invalid data")
  512. }
  513. return hasRightElement(root, firstKey), nil
  514. }
  515. // Ok, in all other cases, we require two edge paths available.
  516. // First check the validity of edge keys.
  517. if bytes.Compare(firstKey, lastKey) >= 0 {
  518. return false, errors.New("invalid edge keys")
  519. }
  520. // todo(rjl493456442) different length edge keys should be supported
  521. if len(firstKey) != len(lastKey) {
  522. return false, errors.New("inconsistent edge keys")
  523. }
  524. // Convert the edge proofs to edge trie paths. Then we can
  525. // have the same tree architecture with the original one.
  526. // For the first edge proof, non-existent proof is allowed.
  527. root, _, err := proofToPath(rootHash, nil, firstKey, proof, true)
  528. if err != nil {
  529. return false, err
  530. }
  531. // Pass the root node here, the second path will be merged
  532. // with the first one. For the last edge proof, non-existent
  533. // proof is also allowed.
  534. root, _, err = proofToPath(rootHash, root, lastKey, proof, true)
  535. if err != nil {
  536. return false, err
  537. }
  538. // Remove all internal references. All the removed parts should
  539. // be re-filled(or re-constructed) by the given leaves range.
  540. empty, err := unsetInternal(root, firstKey, lastKey)
  541. if err != nil {
  542. return false, err
  543. }
  544. // Rebuild the trie with the leaf stream, the shape of trie
  545. // should be same with the original one.
  546. tr := &Trie{root: root, db: NewDatabase(rawdb.NewMemoryDatabase())}
  547. if empty {
  548. tr.root = nil
  549. }
  550. for index, key := range keys {
  551. tr.TryUpdate(key, values[index])
  552. }
  553. if tr.Hash() != rootHash {
  554. return false, fmt.Errorf("invalid proof, want hash %x, got %x", rootHash, tr.Hash())
  555. }
  556. return hasRightElement(tr.root, keys[len(keys)-1]), nil
  557. }
  558. // get returns the child of the given node. Return nil if the
  559. // node with specified key doesn't exist at all.
  560. //
  561. // There is an additional flag `skipResolved`. If it's set then
  562. // all resolved nodes won't be returned.
  563. func get(tn node, key []byte, skipResolved bool) ([]byte, node) {
  564. for {
  565. switch n := tn.(type) {
  566. case *shortNode:
  567. if len(key) < len(n.Key) || !bytes.Equal(n.Key, key[:len(n.Key)]) {
  568. return nil, nil
  569. }
  570. tn = n.Val
  571. key = key[len(n.Key):]
  572. if !skipResolved {
  573. return key, tn
  574. }
  575. case *fullNode:
  576. tn = n.Children[key[0]]
  577. key = key[1:]
  578. if !skipResolved {
  579. return key, tn
  580. }
  581. case hashNode:
  582. return key, n
  583. case nil:
  584. return key, nil
  585. case valueNode:
  586. return nil, n
  587. default:
  588. panic(fmt.Sprintf("%T: invalid node: %v", tn, tn))
  589. }
  590. }
  591. }