state_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 light
  17. import (
  18. "bytes"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/state"
  23. "github.com/ethereum/go-ethereum/ethdb"
  24. "github.com/ethereum/go-ethereum/trie"
  25. "golang.org/x/net/context"
  26. )
  27. type testOdr struct {
  28. OdrBackend
  29. sdb, ldb ethdb.Database
  30. }
  31. func (odr *testOdr) Database() ethdb.Database {
  32. return odr.ldb
  33. }
  34. func (odr *testOdr) Retrieve(ctx context.Context, req OdrRequest) error {
  35. switch req := req.(type) {
  36. case *TrieRequest:
  37. t, _ := trie.New(req.root, odr.sdb)
  38. req.proof = t.Prove(req.key)
  39. case *NodeDataRequest:
  40. req.data, _ = odr.sdb.Get(req.hash[:])
  41. }
  42. req.StoreResult(odr.ldb)
  43. return nil
  44. }
  45. func makeTestState() (common.Hash, ethdb.Database) {
  46. sdb, _ := ethdb.NewMemDatabase()
  47. st, _ := state.New(common.Hash{}, sdb)
  48. for i := byte(0); i < 100; i++ {
  49. addr := common.Address{i}
  50. for j := byte(0); j < 100; j++ {
  51. st.SetState(addr, common.Hash{j}, common.Hash{i, j})
  52. }
  53. st.SetNonce(addr, 100)
  54. st.AddBalance(addr, big.NewInt(int64(i)))
  55. st.SetCode(addr, []byte{i, i, i})
  56. }
  57. root, _ := st.Commit()
  58. return root, sdb
  59. }
  60. func TestLightStateOdr(t *testing.T) {
  61. root, sdb := makeTestState()
  62. ldb, _ := ethdb.NewMemDatabase()
  63. odr := &testOdr{sdb: sdb, ldb: ldb}
  64. ls := NewLightState(root, odr)
  65. ctx := context.Background()
  66. for i := byte(0); i < 100; i++ {
  67. addr := common.Address{i}
  68. err := ls.AddBalance(ctx, addr, big.NewInt(1000))
  69. if err != nil {
  70. t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
  71. }
  72. err = ls.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 100})
  73. if err != nil {
  74. t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
  75. }
  76. }
  77. addr := common.Address{100}
  78. _, err := ls.CreateStateObject(ctx, addr)
  79. if err != nil {
  80. t.Fatalf("Error creating state object: %v", err)
  81. }
  82. err = ls.SetCode(ctx, addr, []byte{100, 100, 100})
  83. if err != nil {
  84. t.Fatalf("Error setting code: %v", err)
  85. }
  86. err = ls.AddBalance(ctx, addr, big.NewInt(1100))
  87. if err != nil {
  88. t.Fatalf("Error adding balance to acc[100]: %v", err)
  89. }
  90. for j := byte(0); j < 101; j++ {
  91. err = ls.SetState(ctx, addr, common.Hash{j}, common.Hash{100, j})
  92. if err != nil {
  93. t.Fatalf("Error setting storage of acc[100]: %v", err)
  94. }
  95. }
  96. err = ls.SetNonce(ctx, addr, 100)
  97. if err != nil {
  98. t.Fatalf("Error setting nonce for acc[100]: %v", err)
  99. }
  100. for i := byte(0); i < 101; i++ {
  101. addr := common.Address{i}
  102. bal, err := ls.GetBalance(ctx, addr)
  103. if err != nil {
  104. t.Fatalf("Error getting balance of acc[%d]: %v", i, err)
  105. }
  106. if bal.Int64() != int64(i)+1000 {
  107. t.Fatalf("Incorrect balance at acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
  108. }
  109. nonce, err := ls.GetNonce(ctx, addr)
  110. if err != nil {
  111. t.Fatalf("Error getting nonce of acc[%d]: %v", i, err)
  112. }
  113. if nonce != 100 {
  114. t.Fatalf("Incorrect nonce at acc[%d]: expected %v, got %v", i, 100, nonce)
  115. }
  116. code, err := ls.GetCode(ctx, addr)
  117. exp := []byte{i, i, i}
  118. if err != nil {
  119. t.Fatalf("Error getting code of acc[%d]: %v", i, err)
  120. }
  121. if !bytes.Equal(code, exp) {
  122. t.Fatalf("Incorrect code at acc[%d]: expected %v, got %v", i, exp, code)
  123. }
  124. for j := byte(0); j < 101; j++ {
  125. exp := common.Hash{i, j}
  126. val, err := ls.GetState(ctx, addr, common.Hash{j})
  127. if err != nil {
  128. t.Fatalf("Error retrieving acc[%d].storage[%d]: %v", i, j, err)
  129. }
  130. if val != exp {
  131. t.Fatalf("Retrieved wrong value from acc[%d].storage[%d]: expected %04x, got %04x", i, j, exp, val)
  132. }
  133. }
  134. }
  135. }
  136. func TestLightStateSetCopy(t *testing.T) {
  137. root, sdb := makeTestState()
  138. ldb, _ := ethdb.NewMemDatabase()
  139. odr := &testOdr{sdb: sdb, ldb: ldb}
  140. ls := NewLightState(root, odr)
  141. ctx := context.Background()
  142. for i := byte(0); i < 100; i++ {
  143. addr := common.Address{i}
  144. err := ls.AddBalance(ctx, addr, big.NewInt(1000))
  145. if err != nil {
  146. t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
  147. }
  148. err = ls.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 100})
  149. if err != nil {
  150. t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
  151. }
  152. }
  153. ls2 := ls.Copy()
  154. for i := byte(0); i < 100; i++ {
  155. addr := common.Address{i}
  156. err := ls2.AddBalance(ctx, addr, big.NewInt(1000))
  157. if err != nil {
  158. t.Fatalf("Error adding balance to acc[%d]: %v", i, err)
  159. }
  160. err = ls2.SetState(ctx, addr, common.Hash{100}, common.Hash{i, 200})
  161. if err != nil {
  162. t.Fatalf("Error setting storage of acc[%d]: %v", i, err)
  163. }
  164. }
  165. lsx := ls.Copy()
  166. ls.Set(ls2)
  167. ls2.Set(lsx)
  168. for i := byte(0); i < 100; i++ {
  169. addr := common.Address{i}
  170. // check balance in ls
  171. bal, err := ls.GetBalance(ctx, addr)
  172. if err != nil {
  173. t.Fatalf("Error getting balance to acc[%d]: %v", i, err)
  174. }
  175. if bal.Int64() != int64(i)+2000 {
  176. t.Fatalf("Incorrect balance at ls.acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
  177. }
  178. // check balance in ls2
  179. bal, err = ls2.GetBalance(ctx, addr)
  180. if err != nil {
  181. t.Fatalf("Error getting balance to acc[%d]: %v", i, err)
  182. }
  183. if bal.Int64() != int64(i)+1000 {
  184. t.Fatalf("Incorrect balance at ls.acc[%d]: expected %v, got %v", i, int64(i)+1000, bal.Int64())
  185. }
  186. // check storage in ls
  187. exp := common.Hash{i, 200}
  188. val, err := ls.GetState(ctx, addr, common.Hash{100})
  189. if err != nil {
  190. t.Fatalf("Error retrieving acc[%d].storage[100]: %v", i, err)
  191. }
  192. if val != exp {
  193. t.Fatalf("Retrieved wrong value from acc[%d].storage[100]: expected %04x, got %04x", i, exp, val)
  194. }
  195. // check storage in ls2
  196. exp = common.Hash{i, 100}
  197. val, err = ls2.GetState(ctx, addr, common.Hash{100})
  198. if err != nil {
  199. t.Fatalf("Error retrieving acc[%d].storage[100]: %v", i, err)
  200. }
  201. if val != exp {
  202. t.Fatalf("Retrieved wrong value from acc[%d].storage[100]: expected %04x, got %04x", i, exp, val)
  203. }
  204. }
  205. }
  206. func TestLightStateDelete(t *testing.T) {
  207. root, sdb := makeTestState()
  208. ldb, _ := ethdb.NewMemDatabase()
  209. odr := &testOdr{sdb: sdb, ldb: ldb}
  210. ls := NewLightState(root, odr)
  211. ctx := context.Background()
  212. addr := common.Address{42}
  213. b, err := ls.HasAccount(ctx, addr)
  214. if err != nil {
  215. t.Fatalf("HasAccount error: %v", err)
  216. }
  217. if !b {
  218. t.Fatalf("HasAccount returned false, expected true")
  219. }
  220. b, err = ls.IsDeleted(ctx, addr)
  221. if err != nil {
  222. t.Fatalf("IsDeleted error: %v", err)
  223. }
  224. if b {
  225. t.Fatalf("IsDeleted returned true, expected false")
  226. }
  227. ls.Delete(ctx, addr)
  228. b, err = ls.IsDeleted(ctx, addr)
  229. if err != nil {
  230. t.Fatalf("IsDeleted error: %v", err)
  231. }
  232. if !b {
  233. t.Fatalf("IsDeleted returned false, expected true")
  234. }
  235. }