managed_state_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package state
  17. import (
  18. "testing"
  19. "github.com/ethereum/go-ethereum/common"
  20. "github.com/ethereum/go-ethereum/ethdb"
  21. )
  22. var addr = common.BytesToAddress([]byte("test"))
  23. func create() (*ManagedState, *account) {
  24. db, _ := ethdb.NewMemDatabase()
  25. statedb := New(common.Hash{}, db)
  26. ms := ManageState(statedb)
  27. so := &StateObject{address: addr, nonce: 100}
  28. ms.StateDB.stateObjects[addr.Str()] = so
  29. ms.accounts[addr.Str()] = newAccount(so)
  30. return ms, ms.accounts[addr.Str()]
  31. }
  32. func TestNewNonce(t *testing.T) {
  33. ms, _ := create()
  34. nonce := ms.NewNonce(addr)
  35. if nonce != 100 {
  36. t.Error("expected nonce 100. got", nonce)
  37. }
  38. nonce = ms.NewNonce(addr)
  39. if nonce != 101 {
  40. t.Error("expected nonce 101. got", nonce)
  41. }
  42. }
  43. func TestRemove(t *testing.T) {
  44. ms, account := create()
  45. nn := make([]bool, 10)
  46. for i, _ := range nn {
  47. nn[i] = true
  48. }
  49. account.nonces = append(account.nonces, nn...)
  50. i := uint64(5)
  51. ms.RemoveNonce(addr, account.nstart+i)
  52. if len(account.nonces) != 5 {
  53. t.Error("expected", i, "'th index to be false")
  54. }
  55. }
  56. func TestReuse(t *testing.T) {
  57. ms, account := create()
  58. nn := make([]bool, 10)
  59. for i, _ := range nn {
  60. nn[i] = true
  61. }
  62. account.nonces = append(account.nonces, nn...)
  63. i := uint64(5)
  64. ms.RemoveNonce(addr, account.nstart+i)
  65. nonce := ms.NewNonce(addr)
  66. if nonce != 105 {
  67. t.Error("expected nonce to be 105. got", nonce)
  68. }
  69. }
  70. func TestRemoteNonceChange(t *testing.T) {
  71. ms, account := create()
  72. nn := make([]bool, 10)
  73. for i, _ := range nn {
  74. nn[i] = true
  75. }
  76. account.nonces = append(account.nonces, nn...)
  77. nonce := ms.NewNonce(addr)
  78. ms.StateDB.stateObjects[addr.Str()].nonce = 200
  79. nonce = ms.NewNonce(addr)
  80. if nonce != 200 {
  81. t.Error("expected nonce after remote update to be", 201, "got", nonce)
  82. }
  83. ms.NewNonce(addr)
  84. ms.NewNonce(addr)
  85. ms.NewNonce(addr)
  86. ms.StateDB.stateObjects[addr.Str()].nonce = 200
  87. nonce = ms.NewNonce(addr)
  88. if nonce != 204 {
  89. t.Error("expected nonce after remote update to be", 201, "got", nonce)
  90. }
  91. }
  92. func TestSetNonce(t *testing.T) {
  93. ms, _ := create()
  94. var addr common.Address
  95. ms.SetNonce(addr, 10)
  96. if ms.GetNonce(addr) != 10 {
  97. t.Error("Expected nonce of 10, got", ms.GetNonce(addr))
  98. }
  99. addr[0] = 1
  100. ms.StateDB.SetNonce(addr, 1)
  101. if ms.GetNonce(addr) != 1 {
  102. t.Error("Expected nonce of 1, got", ms.GetNonce(addr))
  103. }
  104. }