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.

79 lines
1.8 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. }
  11. func (fas *FakeAppState) GetAccount(addr Word256) *Account {
  12. account := fas.accounts[addr.String()]
  13. return account
  14. }
  15. func (fas *FakeAppState) UpdateAccount(account *Account) {
  16. fas.accounts[account.Address.String()] = account
  17. }
  18. func (fas *FakeAppState) RemoveAccount(account *Account) {
  19. _, ok := fas.accounts[account.Address.String()]
  20. if !ok {
  21. panic(Fmt("Invalid account addr: %X", account.Address))
  22. } else {
  23. // Remove account
  24. delete(fas.accounts, account.Address.String())
  25. }
  26. }
  27. func (fas *FakeAppState) CreateAccount(creator *Account) *Account {
  28. addr := createAddress(creator)
  29. account := fas.accounts[addr.String()]
  30. if account == nil {
  31. return &Account{
  32. Address: addr,
  33. Balance: 0,
  34. Code: nil,
  35. Nonce: 0,
  36. }
  37. } else {
  38. panic(Fmt("Invalid account addr: %X", addr))
  39. }
  40. }
  41. func (fas *FakeAppState) GetStorage(addr Word256, key Word256) Word256 {
  42. _, ok := fas.accounts[addr.String()]
  43. if !ok {
  44. panic(Fmt("Invalid account addr: %X", addr))
  45. }
  46. value, ok := fas.storage[addr.String()+key.String()]
  47. if ok {
  48. return value
  49. } else {
  50. return Zero256
  51. }
  52. }
  53. func (fas *FakeAppState) SetStorage(addr Word256, key Word256, value Word256) {
  54. _, ok := fas.accounts[addr.String()]
  55. if !ok {
  56. panic(Fmt("Invalid account addr: %X", addr))
  57. }
  58. fas.storage[addr.String()+key.String()] = value
  59. }
  60. // Creates a 20 byte address and bumps the nonce.
  61. func createAddress(creator *Account) Word256 {
  62. nonce := creator.Nonce
  63. creator.Nonce += 1
  64. temp := make([]byte, 32+8)
  65. copy(temp, creator.Address[:])
  66. PutInt64BE(temp[32:], nonce)
  67. return LeftPadWord256(sha3.Sha3(temp)[:20])
  68. }