managed_state_test.go 3.0 KB

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