api_test.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. // Copyright 2017 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 eth
  17. import (
  18. "bytes"
  19. "fmt"
  20. "math/big"
  21. "reflect"
  22. "sort"
  23. "testing"
  24. "github.com/davecgh/go-spew/spew"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/core/rawdb"
  27. "github.com/ethereum/go-ethereum/core/state"
  28. "github.com/ethereum/go-ethereum/crypto"
  29. )
  30. var dumper = spew.ConfigState{Indent: " "}
  31. func accountRangeTest(t *testing.T, trie *state.Trie, statedb *state.StateDB, start common.Hash, requestedNum int, expectedNum int) state.IteratorDump {
  32. result := statedb.IteratorDump(true, true, false, start.Bytes(), requestedNum)
  33. if len(result.Accounts) != expectedNum {
  34. t.Fatalf("expected %d results, got %d", expectedNum, len(result.Accounts))
  35. }
  36. for address := range result.Accounts {
  37. if address == (common.Address{}) {
  38. t.Fatalf("empty address returned")
  39. }
  40. if !statedb.Exist(address) {
  41. t.Fatalf("account not found in state %s", address.Hex())
  42. }
  43. }
  44. return result
  45. }
  46. type resultHash []common.Hash
  47. func (h resultHash) Len() int { return len(h) }
  48. func (h resultHash) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  49. func (h resultHash) Less(i, j int) bool { return bytes.Compare(h[i].Bytes(), h[j].Bytes()) < 0 }
  50. func TestAccountRange(t *testing.T) {
  51. t.Parallel()
  52. var (
  53. statedb = state.NewDatabaseWithConfig(rawdb.NewMemoryDatabase(), nil)
  54. state, _ = state.New(common.Hash{}, statedb, nil)
  55. addrs = [AccountRangeMaxResults * 2]common.Address{}
  56. m = map[common.Address]bool{}
  57. )
  58. for i := range addrs {
  59. hash := common.HexToHash(fmt.Sprintf("%x", i))
  60. addr := common.BytesToAddress(crypto.Keccak256Hash(hash.Bytes()).Bytes())
  61. addrs[i] = addr
  62. state.SetBalance(addrs[i], big.NewInt(1))
  63. if _, ok := m[addr]; ok {
  64. t.Fatalf("bad")
  65. } else {
  66. m[addr] = true
  67. }
  68. }
  69. state.Commit(true)
  70. root := state.IntermediateRoot(true)
  71. trie, err := statedb.OpenTrie(root)
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. accountRangeTest(t, &trie, state, common.Hash{}, AccountRangeMaxResults/2, AccountRangeMaxResults/2)
  76. // test pagination
  77. firstResult := accountRangeTest(t, &trie, state, common.Hash{}, AccountRangeMaxResults, AccountRangeMaxResults)
  78. secondResult := accountRangeTest(t, &trie, state, common.BytesToHash(firstResult.Next), AccountRangeMaxResults, AccountRangeMaxResults)
  79. hList := make(resultHash, 0)
  80. for addr1 := range firstResult.Accounts {
  81. // If address is empty, then it makes no sense to compare
  82. // them as they might be two different accounts.
  83. if addr1 == (common.Address{}) {
  84. continue
  85. }
  86. if _, duplicate := secondResult.Accounts[addr1]; duplicate {
  87. t.Fatalf("pagination test failed: results should not overlap")
  88. }
  89. hList = append(hList, crypto.Keccak256Hash(addr1.Bytes()))
  90. }
  91. // Test to see if it's possible to recover from the middle of the previous
  92. // set and get an even split between the first and second sets.
  93. sort.Sort(hList)
  94. middleH := hList[AccountRangeMaxResults/2]
  95. middleResult := accountRangeTest(t, &trie, state, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
  96. missing, infirst, insecond := 0, 0, 0
  97. for h := range middleResult.Accounts {
  98. if _, ok := firstResult.Accounts[h]; ok {
  99. infirst++
  100. } else if _, ok := secondResult.Accounts[h]; ok {
  101. insecond++
  102. } else {
  103. missing++
  104. }
  105. }
  106. if missing != 0 {
  107. t.Fatalf("%d hashes in the 'middle' set were neither in the first not the second set", missing)
  108. }
  109. if infirst != AccountRangeMaxResults/2 {
  110. t.Fatalf("Imbalance in the number of first-test results: %d != %d", infirst, AccountRangeMaxResults/2)
  111. }
  112. if insecond != AccountRangeMaxResults/2 {
  113. t.Fatalf("Imbalance in the number of second-test results: %d != %d", insecond, AccountRangeMaxResults/2)
  114. }
  115. }
  116. func TestEmptyAccountRange(t *testing.T) {
  117. t.Parallel()
  118. var (
  119. statedb = state.NewDatabase(rawdb.NewMemoryDatabase())
  120. state, _ = state.New(common.Hash{}, statedb, nil)
  121. )
  122. state.Commit(true)
  123. state.IntermediateRoot(true)
  124. results := state.IteratorDump(true, true, true, (common.Hash{}).Bytes(), AccountRangeMaxResults)
  125. if bytes.Equal(results.Next, (common.Hash{}).Bytes()) {
  126. t.Fatalf("Empty results should not return a second page")
  127. }
  128. if len(results.Accounts) != 0 {
  129. t.Fatalf("Empty state should not return addresses: %v", results.Accounts)
  130. }
  131. }
  132. func TestStorageRangeAt(t *testing.T) {
  133. t.Parallel()
  134. // Create a state where account 0x010000... has a few storage entries.
  135. var (
  136. state, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()), nil)
  137. addr = common.Address{0x01}
  138. keys = []common.Hash{ // hashes of Keys of storage
  139. common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"),
  140. common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"),
  141. common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"),
  142. common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"),
  143. }
  144. storage = storageMap{
  145. keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}},
  146. keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}},
  147. keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}},
  148. keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}},
  149. }
  150. )
  151. for _, entry := range storage {
  152. state.SetState(addr, *entry.Key, entry.Value)
  153. }
  154. // Check a few combinations of limit and start/end.
  155. tests := []struct {
  156. start []byte
  157. limit int
  158. want StorageRangeResult
  159. }{
  160. {
  161. start: []byte{}, limit: 0,
  162. want: StorageRangeResult{storageMap{}, &keys[0]},
  163. },
  164. {
  165. start: []byte{}, limit: 100,
  166. want: StorageRangeResult{storage, nil},
  167. },
  168. {
  169. start: []byte{}, limit: 2,
  170. want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]},
  171. },
  172. {
  173. start: []byte{0x00}, limit: 4,
  174. want: StorageRangeResult{storage, nil},
  175. },
  176. {
  177. start: []byte{0x40}, limit: 2,
  178. want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]},
  179. },
  180. }
  181. for _, test := range tests {
  182. result, err := storageRangeAt(state.StorageTrie(addr), test.start, test.limit)
  183. if err != nil {
  184. t.Error(err)
  185. }
  186. if !reflect.DeepEqual(result, test.want) {
  187. t.Fatalf("wrong result for range 0x%x.., limit %d:\ngot %s\nwant %s",
  188. test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want))
  189. }
  190. }
  191. }