managed_state_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 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}
  28. so.SetNonce(100)
  29. ms.StateDB.stateObjects[addr] = so
  30. ms.accounts[addr] = newAccount(so)
  31. return ms, ms.accounts[addr]
  32. }
  33. func TestNewNonce(t *testing.T) {
  34. ms, _ := create()
  35. nonce := ms.NewNonce(addr)
  36. if nonce != 100 {
  37. t.Error("expected nonce 100. got", nonce)
  38. }
  39. nonce = ms.NewNonce(addr)
  40. if nonce != 101 {
  41. t.Error("expected nonce 101. got", nonce)
  42. }
  43. }
  44. func TestRemove(t *testing.T) {
  45. ms, account := create()
  46. nn := make([]bool, 10)
  47. for i, _ := range nn {
  48. nn[i] = true
  49. }
  50. account.nonces = append(account.nonces, nn...)
  51. i := uint64(5)
  52. ms.RemoveNonce(addr, account.nstart+i)
  53. if len(account.nonces) != 5 {
  54. t.Error("expected", i, "'th index to be false")
  55. }
  56. }
  57. func TestReuse(t *testing.T) {
  58. ms, account := create()
  59. nn := make([]bool, 10)
  60. for i, _ := range nn {
  61. nn[i] = true
  62. }
  63. account.nonces = append(account.nonces, nn...)
  64. i := uint64(5)
  65. ms.RemoveNonce(addr, account.nstart+i)
  66. nonce := ms.NewNonce(addr)
  67. if nonce != 105 {
  68. t.Error("expected nonce to be 105. got", nonce)
  69. }
  70. }
  71. func TestRemoteNonceChange(t *testing.T) {
  72. ms, account := create()
  73. nn := make([]bool, 10)
  74. for i, _ := range nn {
  75. nn[i] = true
  76. }
  77. account.nonces = append(account.nonces, nn...)
  78. nonce := ms.NewNonce(addr)
  79. ms.StateDB.stateObjects[addr].data.Nonce = 200
  80. nonce = ms.NewNonce(addr)
  81. if nonce != 200 {
  82. t.Error("expected nonce after remote update to be", 201, "got", nonce)
  83. }
  84. ms.NewNonce(addr)
  85. ms.NewNonce(addr)
  86. ms.NewNonce(addr)
  87. ms.StateDB.stateObjects[addr].data.Nonce = 200
  88. nonce = ms.NewNonce(addr)
  89. if nonce != 204 {
  90. t.Error("expected nonce after remote update to be", 201, "got", nonce)
  91. }
  92. }
  93. func TestSetNonce(t *testing.T) {
  94. ms, _ := create()
  95. var addr common.Address
  96. ms.SetNonce(addr, 10)
  97. if ms.GetNonce(addr) != 10 {
  98. t.Error("Expected nonce of 10, got", ms.GetNonce(addr))
  99. }
  100. addr[0] = 1
  101. ms.StateDB.SetNonce(addr, 1)
  102. if ms.GetNonce(addr) != 1 {
  103. t.Error("Expected nonce of 1, got", ms.GetNonce(addr))
  104. }
  105. }