stacktrie_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. package trie
  2. import (
  3. "testing"
  4. "github.com/ethereum/go-ethereum/common"
  5. "github.com/ethereum/go-ethereum/ethdb/memorydb"
  6. )
  7. func TestSizeBug(t *testing.T) {
  8. st := NewStackTrie(nil)
  9. nt, _ := New(common.Hash{}, NewDatabase(memorydb.New()))
  10. leaf := common.FromHex("290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")
  11. value := common.FromHex("94cf40d0d2b44f2b66e07cace1372ca42b73cf21a3")
  12. nt.TryUpdate(leaf, value)
  13. st.TryUpdate(leaf, value)
  14. if nt.Hash() != st.Hash() {
  15. t.Fatalf("error %x != %x", st.Hash(), nt.Hash())
  16. }
  17. }
  18. func TestEmptyBug(t *testing.T) {
  19. st := NewStackTrie(nil)
  20. nt, _ := New(common.Hash{}, NewDatabase(memorydb.New()))
  21. //leaf := common.FromHex("290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")
  22. //value := common.FromHex("94cf40d0d2b44f2b66e07cace1372ca42b73cf21a3")
  23. kvs := []struct {
  24. K string
  25. V string
  26. }{
  27. {K: "405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace", V: "9496f4ec2bf9dab484cac6be589e8417d84781be08"},
  28. {K: "40edb63a35fcf86c08022722aa3287cdd36440d671b4918131b2514795fefa9c", V: "01"},
  29. {K: "b10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf6", V: "947a30f7736e48d6599356464ba4c150d8da0302ff"},
  30. {K: "c2575a0e9e593c00f959f8c92f12db2869c3395a3b0502d05e2516446f71f85b", V: "02"},
  31. }
  32. for _, kv := range kvs {
  33. nt.TryUpdate(common.FromHex(kv.K), common.FromHex(kv.V))
  34. st.TryUpdate(common.FromHex(kv.K), common.FromHex(kv.V))
  35. }
  36. if nt.Hash() != st.Hash() {
  37. t.Fatalf("error %x != %x", st.Hash(), nt.Hash())
  38. }
  39. }
  40. func TestValLength56(t *testing.T) {
  41. st := NewStackTrie(nil)
  42. nt, _ := New(common.Hash{}, NewDatabase(memorydb.New()))
  43. //leaf := common.FromHex("290decd9548b62a8d60345a988386fc84ba6bc95484008f6362f93160ef3e563")
  44. //value := common.FromHex("94cf40d0d2b44f2b66e07cace1372ca42b73cf21a3")
  45. kvs := []struct {
  46. K string
  47. V string
  48. }{
  49. {K: "405787fa12a823e0f2b7631cc41b3ba8828b3321ca811111fa75cd3aa3bb5ace", V: "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"},
  50. }
  51. for _, kv := range kvs {
  52. nt.TryUpdate(common.FromHex(kv.K), common.FromHex(kv.V))
  53. st.TryUpdate(common.FromHex(kv.K), common.FromHex(kv.V))
  54. }
  55. if nt.Hash() != st.Hash() {
  56. t.Fatalf("error %x != %x", st.Hash(), nt.Hash())
  57. }
  58. }
  59. // TestUpdateSmallNodes tests a case where the leaves are small (both key and value),
  60. // which causes a lot of node-within-node. This case was found via fuzzing.
  61. func TestUpdateSmallNodes(t *testing.T) {
  62. st := NewStackTrie(nil)
  63. nt, _ := New(common.Hash{}, NewDatabase(memorydb.New()))
  64. kvs := []struct {
  65. K string
  66. V string
  67. }{
  68. {"63303030", "3041"}, // stacktrie.Update
  69. {"65", "3000"}, // stacktrie.Update
  70. }
  71. for _, kv := range kvs {
  72. nt.TryUpdate(common.FromHex(kv.K), common.FromHex(kv.V))
  73. st.TryUpdate(common.FromHex(kv.K), common.FromHex(kv.V))
  74. }
  75. if nt.Hash() != st.Hash() {
  76. t.Fatalf("error %x != %x", st.Hash(), nt.Hash())
  77. }
  78. }
  79. // TestUpdateVariableKeys contains a case which stacktrie fails: when keys of different
  80. // sizes are used, and the second one has the same prefix as the first, then the
  81. // stacktrie fails, since it's unable to 'expand' on an already added leaf.
  82. // For all practical purposes, this is fine, since keys are fixed-size length
  83. // in account and storage tries.
  84. //
  85. // The test is marked as 'skipped', and exists just to have the behaviour documented.
  86. // This case was found via fuzzing.
  87. func TestUpdateVariableKeys(t *testing.T) {
  88. t.SkipNow()
  89. st := NewStackTrie(nil)
  90. nt, _ := New(common.Hash{}, NewDatabase(memorydb.New()))
  91. kvs := []struct {
  92. K string
  93. V string
  94. }{
  95. {"0x33303534636532393561313031676174", "303030"},
  96. {"0x3330353463653239356131303167617430", "313131"},
  97. }
  98. for _, kv := range kvs {
  99. nt.TryUpdate(common.FromHex(kv.K), common.FromHex(kv.V))
  100. st.TryUpdate(common.FromHex(kv.K), common.FromHex(kv.V))
  101. }
  102. if nt.Hash() != st.Hash() {
  103. t.Fatalf("error %x != %x", st.Hash(), nt.Hash())
  104. }
  105. }