api_test.go 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. "reflect"
  19. "testing"
  20. "github.com/davecgh/go-spew/spew"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/state"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. )
  25. var dumper = spew.ConfigState{Indent: " "}
  26. func TestStorageRangeAt(t *testing.T) {
  27. // Create a state where account 0x010000... has a few storage entries.
  28. var (
  29. state, _ = state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
  30. addr = common.Address{0x01}
  31. keys = []common.Hash{ // hashes of Keys of storage
  32. common.HexToHash("340dd630ad21bf010b4e676dbfa9ba9a02175262d1fa356232cfde6cb5b47ef2"),
  33. common.HexToHash("426fcb404ab2d5d8e61a3d918108006bbb0a9be65e92235bb10eefbdb6dcd053"),
  34. common.HexToHash("48078cfed56339ea54962e72c37c7f588fc4f8e5bc173827ba75cb10a63a96a5"),
  35. common.HexToHash("5723d2c3a83af9b735e3b7f21531e5623d183a9095a56604ead41f3582fdfb75"),
  36. }
  37. storage = storageMap{
  38. keys[0]: {Key: &common.Hash{0x02}, Value: common.Hash{0x01}},
  39. keys[1]: {Key: &common.Hash{0x04}, Value: common.Hash{0x02}},
  40. keys[2]: {Key: &common.Hash{0x01}, Value: common.Hash{0x03}},
  41. keys[3]: {Key: &common.Hash{0x03}, Value: common.Hash{0x04}},
  42. }
  43. )
  44. for _, entry := range storage {
  45. state.SetState(addr, *entry.Key, entry.Value)
  46. }
  47. // Check a few combinations of limit and start/end.
  48. tests := []struct {
  49. start []byte
  50. limit int
  51. want StorageRangeResult
  52. }{
  53. {
  54. start: []byte{}, limit: 0,
  55. want: StorageRangeResult{storageMap{}, &keys[0]},
  56. },
  57. {
  58. start: []byte{}, limit: 100,
  59. want: StorageRangeResult{storage, nil},
  60. },
  61. {
  62. start: []byte{}, limit: 2,
  63. want: StorageRangeResult{storageMap{keys[0]: storage[keys[0]], keys[1]: storage[keys[1]]}, &keys[2]},
  64. },
  65. {
  66. start: []byte{0x00}, limit: 4,
  67. want: StorageRangeResult{storage, nil},
  68. },
  69. {
  70. start: []byte{0x40}, limit: 2,
  71. want: StorageRangeResult{storageMap{keys[1]: storage[keys[1]], keys[2]: storage[keys[2]]}, &keys[3]},
  72. },
  73. }
  74. for _, test := range tests {
  75. result, err := storageRangeAt(state.StorageTrie(addr), test.start, test.limit)
  76. if err != nil {
  77. t.Error(err)
  78. }
  79. if !reflect.DeepEqual(result, test.want) {
  80. t.Fatalf("wrong result for range 0x%x.., limit %d:\ngot %s\nwant %s",
  81. test.start, test.limit, dumper.Sdump(result), dumper.Sdump(&test.want))
  82. }
  83. }
  84. }