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