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.

127 lines
2.8 KiB

  1. package main
  2. import (
  3. "fmt"
  4. . "github.com/tendermint/tendermint/common"
  5. . "github.com/tendermint/tendermint/vm"
  6. )
  7. type FakeAppState struct {
  8. accounts map[string]*Account
  9. storage map[string]Word
  10. logs []*Log
  11. }
  12. func (fas *FakeAppState) GetAccount(addr Word) (*Account, error) {
  13. account := fas.accounts[addr.String()]
  14. if account == nil {
  15. return account, nil
  16. } else {
  17. return nil, Errorf("Invalid account addr: %v", addr)
  18. }
  19. }
  20. func (fas *FakeAppState) UpdateAccount(account *Account) error {
  21. _, ok := fas.accounts[account.Address.String()]
  22. if !ok {
  23. return Errorf("Invalid account addr: %v", account.Address.String())
  24. } else {
  25. // Nothing to do
  26. return nil
  27. }
  28. }
  29. func (fas *FakeAppState) DeleteAccount(account *Account) error {
  30. _, ok := fas.accounts[account.Address.String()]
  31. if !ok {
  32. return Errorf("Invalid account addr: %v", account.Address.String())
  33. } else {
  34. // Delete account
  35. delete(fas.accounts, account.Address.String())
  36. return nil
  37. }
  38. }
  39. func (fas *FakeAppState) CreateAccount(addr Word, balance uint64) (*Account, error) {
  40. account := fas.accounts[addr.String()]
  41. if account == nil {
  42. return &Account{
  43. Address: addr,
  44. Balance: balance,
  45. Code: nil,
  46. Nonce: 0,
  47. StateRoot: Zero,
  48. }, nil
  49. } else {
  50. return nil, Errorf("Invalid account addr: %v", addr)
  51. }
  52. }
  53. func (fas *FakeAppState) GetStorage(addr Word, key Word) (Word, error) {
  54. _, ok := fas.accounts[addr.String()]
  55. if !ok {
  56. return Zero, Errorf("Invalid account addr: %v", addr)
  57. }
  58. value, ok := fas.storage[addr.String()+key.String()]
  59. if ok {
  60. return value, nil
  61. } else {
  62. return Zero, nil
  63. }
  64. }
  65. func (fas *FakeAppState) SetStorage(addr Word, key Word, value Word) (bool, error) {
  66. _, ok := fas.accounts[addr.String()]
  67. if !ok {
  68. return false, Errorf("Invalid account addr: %v", addr)
  69. }
  70. _, ok = fas.storage[addr.String()+key.String()]
  71. fas.storage[addr.String()+key.String()] = value
  72. return ok, nil
  73. }
  74. func (fas *FakeAppState) RemoveStorage(addr Word, key Word) error {
  75. _, ok := fas.accounts[addr.String()]
  76. if !ok {
  77. return Errorf("Invalid account addr: %v", addr)
  78. }
  79. delete(fas.storage, addr.String()+key.String())
  80. return nil
  81. }
  82. func (fas *FakeAppState) AddLog(log *Log) {
  83. fas.logs = append(fas.logs, log)
  84. }
  85. func main() {
  86. appState := &FakeAppState{
  87. accounts: make(map[string]*Account),
  88. storage: make(map[string]Word),
  89. logs: nil,
  90. }
  91. params := VMParams{
  92. BlockHeight: 0,
  93. BlockHash: Zero,
  94. BlockTime: 0,
  95. GasLimit: 0,
  96. }
  97. ourVm := NewVM(appState, params)
  98. // Create accounts
  99. account1, err := appState.CreateAccount(Uint64ToWord(100), 0)
  100. if err != nil {
  101. panic(err)
  102. }
  103. account2, err := appState.CreateAccount(Uint64ToWord(101), 0)
  104. if err != nil {
  105. panic(err)
  106. }
  107. var gas uint64 = 1000
  108. output, err := ourVm.Call(account1, account2, []byte{0x60, 0x01, 0x60, 0x01}, []byte{}, 0, &gas)
  109. fmt.Printf("Output: %v Error: %v\n", output, err)
  110. }