managed_state_test.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package state
  2. import (
  3. "testing"
  4. "github.com/ethereum/go-ethereum/common"
  5. )
  6. var addr = common.BytesToAddress([]byte("test"))
  7. func create() (*ManagedState, *account) {
  8. ms := ManageState(&StateDB{stateObjects: make(map[string]*StateObject)})
  9. so := &StateObject{address: addr, nonce: 100}
  10. ms.StateDB.stateObjects[addr.Str()] = so
  11. ms.accounts[addr.Str()] = newAccount(so)
  12. return ms, ms.accounts[addr.Str()]
  13. }
  14. func TestNewNonce(t *testing.T) {
  15. ms, _ := create()
  16. nonce := ms.NewNonce(addr)
  17. if nonce != 100 {
  18. t.Error("expected nonce 100. got", nonce)
  19. }
  20. nonce = ms.NewNonce(addr)
  21. if nonce != 101 {
  22. t.Error("expected nonce 101. got", nonce)
  23. }
  24. }
  25. func TestRemove(t *testing.T) {
  26. ms, account := create()
  27. nn := make([]bool, 10)
  28. for i, _ := range nn {
  29. nn[i] = true
  30. }
  31. account.nonces = append(account.nonces, nn...)
  32. i := uint64(5)
  33. ms.RemoveNonce(addr, account.nstart+i)
  34. if len(account.nonces) != 5 {
  35. t.Error("expected", i, "'th index to be false")
  36. }
  37. }
  38. func TestReuse(t *testing.T) {
  39. ms, account := create()
  40. nn := make([]bool, 10)
  41. for i, _ := range nn {
  42. nn[i] = true
  43. }
  44. account.nonces = append(account.nonces, nn...)
  45. i := uint64(5)
  46. ms.RemoveNonce(addr, account.nstart+i)
  47. nonce := ms.NewNonce(addr)
  48. if nonce != 105 {
  49. t.Error("expected nonce to be 105. got", nonce)
  50. }
  51. }
  52. func TestRemoteNonceChange(t *testing.T) {
  53. ms, account := create()
  54. nn := make([]bool, 10)
  55. for i, _ := range nn {
  56. nn[i] = true
  57. }
  58. account.nonces = append(account.nonces, nn...)
  59. nonce := ms.NewNonce(addr)
  60. ms.StateDB.stateObjects[addr.Str()].nonce = 200
  61. nonce = ms.NewNonce(addr)
  62. if nonce != 200 {
  63. t.Error("expected nonce after remote update to be", 201, "got", nonce)
  64. }
  65. ms.NewNonce(addr)
  66. ms.NewNonce(addr)
  67. ms.NewNonce(addr)
  68. ms.StateDB.stateObjects[addr.Str()].nonce = 200
  69. nonce = ms.NewNonce(addr)
  70. if nonce != 204 {
  71. t.Error("expected nonce after remote update to be", 201, "got", nonce)
  72. }
  73. }
  74. func TestSetNonce(t *testing.T) {
  75. ms, _ := create()
  76. var addr common.Address
  77. ms.SetNonce(addr, 10)
  78. if ms.GetNonce(addr) != 10 {
  79. t.Errorf("Expected nonce of 10, got", ms.GetNonce(addr))
  80. }
  81. addr[0] = 1
  82. ms.StateDB.SetNonce(addr, 1)
  83. if ms.GetNonce(addr) != 1 {
  84. t.Errorf("Expected nonce of 1, got", ms.GetNonce(addr))
  85. }
  86. }