api_test.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  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) AccountRangeResult {
  32. result, err := accountRange(*trie, start, requestedNum)
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if len(result.Accounts) != expectedNum {
  37. t.Fatalf("expected %d results. Got %d", expectedNum, len(result.Accounts))
  38. }
  39. for _, address := range result.Accounts {
  40. if address == nil {
  41. t.Fatalf("null address returned")
  42. }
  43. if !statedb.Exist(*address) {
  44. t.Fatalf("account not found in state %s", address.Hex())
  45. }
  46. }
  47. return result
  48. }
  49. type resultHash []*common.Hash
  50. func (h resultHash) Len() int { return len(h) }
  51. func (h resultHash) Swap(i, j int) { h[i], h[j] = h[j], h[i] }
  52. func (h resultHash) Less(i, j int) bool { return bytes.Compare(h[i].Bytes(), h[j].Bytes()) < 0 }
  53. func TestAccountRange(t *testing.T) {
  54. var (
  55. statedb = state.NewDatabase(rawdb.NewMemoryDatabase())
  56. state, _ = state.New(common.Hash{}, statedb)
  57. addrs = [AccountRangeMaxResults * 2]common.Address{}
  58. m = map[common.Address]bool{}
  59. )
  60. for i := range addrs {
  61. hash := common.HexToHash(fmt.Sprintf("%x", i))
  62. addr := common.BytesToAddress(crypto.Keccak256Hash(hash.Bytes()).Bytes())
  63. addrs[i] = addr
  64. state.SetBalance(addrs[i], big.NewInt(1))
  65. if _, ok := m[addr]; ok {
  66. t.Fatalf("bad")
  67. } else {
  68. m[addr] = true
  69. }
  70. }
  71. state.Commit(true)
  72. root := state.IntermediateRoot(true)
  73. trie, err := statedb.OpenTrie(root)
  74. if err != nil {
  75. t.Fatal(err)
  76. }
  77. t.Logf("test getting number of results less than max")
  78. accountRangeTest(t, &trie, state, &common.Hash{0x0}, AccountRangeMaxResults/2, AccountRangeMaxResults/2)
  79. t.Logf("test getting number of results greater than max %d", AccountRangeMaxResults)
  80. accountRangeTest(t, &trie, state, &common.Hash{0x0}, AccountRangeMaxResults*2, AccountRangeMaxResults)
  81. t.Logf("test with empty 'start' hash")
  82. accountRangeTest(t, &trie, state, nil, AccountRangeMaxResults, AccountRangeMaxResults)
  83. t.Logf("test pagination")
  84. // test pagination
  85. firstResult := accountRangeTest(t, &trie, state, &common.Hash{0x0}, AccountRangeMaxResults, AccountRangeMaxResults)
  86. t.Logf("test pagination 2")
  87. secondResult := accountRangeTest(t, &trie, state, &firstResult.Next, AccountRangeMaxResults, AccountRangeMaxResults)
  88. hList := make(resultHash, 0)
  89. for h1, addr1 := range firstResult.Accounts {
  90. h := &common.Hash{}
  91. h.SetBytes(h1.Bytes())
  92. hList = append(hList, h)
  93. for h2, addr2 := range secondResult.Accounts {
  94. // Make sure that the hashes aren't the same
  95. if bytes.Equal(h1.Bytes(), h2.Bytes()) {
  96. t.Fatalf("pagination test failed: results should not overlap")
  97. }
  98. // If either address is nil, then it makes no sense to compare
  99. // them as they might be two different accounts.
  100. if addr1 == nil || addr2 == nil {
  101. continue
  102. }
  103. // Since the two hashes are different, they should not have
  104. // the same preimage, but let's check anyway in case there
  105. // is a bug in the (hash, addr) map generation code.
  106. if bytes.Equal(addr1.Bytes(), addr2.Bytes()) {
  107. t.Fatalf("pagination test failed: addresses should not repeat")
  108. }
  109. }
  110. }
  111. // Test to see if it's possible to recover from the middle of the previous
  112. // set and get an even split between the first and second sets.
  113. t.Logf("test random access pagination")
  114. sort.Sort(hList)
  115. middleH := hList[AccountRangeMaxResults/2]
  116. middleResult := accountRangeTest(t, &trie, state, middleH, AccountRangeMaxResults, AccountRangeMaxResults)
  117. innone, infirst, insecond := 0, 0, 0
  118. for h := range middleResult.Accounts {
  119. if _, ok := firstResult.Accounts[h]; ok {
  120. infirst++
  121. } else if _, ok := secondResult.Accounts[h]; ok {
  122. insecond++
  123. } else {
  124. innone++
  125. }
  126. }
  127. if innone != 0 {
  128. t.Fatalf("%d hashes in the 'middle' set were neither in the first not the second set", innone)
  129. }
  130. if infirst != AccountRangeMaxResults/2 {
  131. t.Fatalf("Imbalance in the number of first-test results: %d != %d", infirst, AccountRangeMaxResults/2)
  132. }
  133. if insecond != AccountRangeMaxResults/2 {
  134. t.Fatalf("Imbalance in the number of second-test results: %d != %d", insecond, AccountRangeMaxResults/2)
  135. }
  136. }
  137. func TestEmptyAccountRange(t *testing.T) {
  138. var (
  139. statedb = state.NewDatabase(rawdb.NewMemoryDatabase())
  140. state, _ = state.New(common.Hash{}, statedb)
  141. )
  142. state.Commit(true)
  143. root := state.IntermediateRoot(true)
  144. trie, err := statedb.OpenTrie(root)
  145. if err != nil {
  146. t.Fatal(err)
  147. }
  148. results, err := accountRange(trie, &common.Hash{0x0}, AccountRangeMaxResults)
  149. if err != nil {
  150. t.Fatalf("Empty results should not trigger an error: %v", err)
  151. }
  152. if results.Next != common.HexToHash("0") {
  153. t.Fatalf("Empty results should not return a second page")
  154. }
  155. if len(results.Accounts) != 0 {
  156. t.Fatalf("Empty state should not return addresses: %v", results.Accounts)
  157. }
  158. }
  159. func TestStorageRangeAt(t *testing.T) {
  160. // Create a state where account 0x010000... has a few storage entries.
  161. var (
  162. state, _ = state.New(common.Hash{}, state.NewDatabase(rawdb.NewMemoryDatabase()))
  163. addr = common.Address{0x01}
  164. keys = []common.Hash{ // hashes of Keys of storage
  165. common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"),
  166. common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"),
  167. common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"),
  168. common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"),
  169. }
  170. storage = storageMap{
  171. keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}},
  172. keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}},
  173. keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}},
  174. keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}},
  175. }
  176. )
  177. for _, entry := range storage {
  178. state.SetState(addr, *entry.Key, entry.Value)
  179. }
  180. // Check a few combinations of limit and start/end.
  181. tests := []struct {
  182. start []byte
  183. limit int
  184. want StorageRangeResult
  185. }{
  186. {
  187. start: []byte{}, limit: 0,
  188. want: StorageRangeResult{storageMap{}, &keys[0]},
  189. },
  190. {
  191. start: []byte{}, limit: 100,
  192. want: StorageRangeResult{storage, nil},
  193. },
  194. {
  195. start: []byte{}, limit: 2,
  196. want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]},
  197. },
  198. {
  199. start: []byte{0x00}, limit: 4,
  200. want: StorageRangeResult{storage, nil},
  201. },
  202. {
  203. start: []byte{0x40}, limit: 2,
  204. want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]},
  205. },
  206. }
  207. for _, test := range tests {
  208. result, err := storageRangeAt(state.StorageTrie(addr), test.start, test.limit)
  209. if err != nil {
  210. t.Error(err)
  211. }
  212. if !reflect.DeepEqual(result, test.want) {
  213. t.Fatalf("wrong result for range 0x%x.., limit %d:\ngot %s\nwant %s",
  214. test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want))
  215. }
  216. }
  217. }