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.9 KiB

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