You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.1 KiB

  1. package vm
  2. import (
  3. . "github.com/tendermint/tendermint/common"
  4. . "github.com/tendermint/tendermint/vm"
  5. "github.com/tendermint/tendermint/vm/sha3"
  6. )
  7. type FakeAppState struct {
  8. accounts map[string]*Account
  9. storage map[string]Word256
  10. logs []*Log
  11. }
  12. func (fas *FakeAppState) GetAccount(addr Word256) *Account {
  13. account := fas.accounts[addr.String()]
  14. if account != nil {
  15. return account
  16. } else {
  17. panic(Fmt("Invalid account addr: %X", addr))
  18. }
  19. }
  20. func (fas *FakeAppState) UpdateAccount(account *Account) {
  21. _, ok := fas.accounts[account.Address.String()]
  22. if !ok {
  23. panic(Fmt("Invalid account addr: %X", account.Address))
  24. } else {
  25. // Nothing to do
  26. }
  27. }
  28. func (fas *FakeAppState) RemoveAccount(account *Account) {
  29. _, ok := fas.accounts[account.Address.String()]
  30. if !ok {
  31. panic(Fmt("Invalid account addr: %X", account.Address))
  32. } else {
  33. // Remove account
  34. delete(fas.accounts, account.Address.String())
  35. }
  36. }
  37. func (fas *FakeAppState) CreateAccount(creator *Account) *Account {
  38. addr := createAddress(creator)
  39. account := fas.accounts[addr.String()]
  40. if account == nil {
  41. return &Account{
  42. Address: addr,
  43. Balance: 0,
  44. Code: nil,
  45. Nonce: 0,
  46. StorageRoot: Zero256,
  47. }
  48. } else {
  49. panic(Fmt("Invalid account addr: %X", addr))
  50. }
  51. }
  52. func (fas *FakeAppState) GetStorage(addr Word256, key Word256) Word256 {
  53. _, ok := fas.accounts[addr.String()]
  54. if !ok {
  55. panic(Fmt("Invalid account addr: %X", addr))
  56. }
  57. value, ok := fas.storage[addr.String()+key.String()]
  58. if ok {
  59. return value
  60. } else {
  61. return Zero256
  62. }
  63. }
  64. func (fas *FakeAppState) SetStorage(addr Word256, key Word256, value Word256) {
  65. _, ok := fas.accounts[addr.String()]
  66. if !ok {
  67. panic(Fmt("Invalid account addr: %X", addr))
  68. }
  69. fas.storage[addr.String()+key.String()] = value
  70. }
  71. func (fas *FakeAppState) AddLog(log *Log) {
  72. fas.logs = append(fas.logs, log)
  73. }
  74. // Creates a 20 byte address and bumps the nonce.
  75. func createAddress(creator *Account) Word256 {
  76. nonce := creator.Nonce
  77. creator.Nonce += 1
  78. temp := make([]byte, 32+8)
  79. copy(temp, creator.Address[:])
  80. PutUint64(temp[32:], nonce)
  81. return LeftPadWord256(sha3.Sha3(temp)[:20])
  82. }