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.

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