From 8da3ba05c01d5f038912c07f508982d079574690 Mon Sep 17 00:00:00 2001 From: Jae Kwon Date: Wed, 18 Mar 2015 23:13:16 -0700 Subject: [PATCH] Creating fake_app_state test --- vm/test/fake_app_state.go | 127 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) create mode 100644 vm/test/fake_app_state.go diff --git a/vm/test/fake_app_state.go b/vm/test/fake_app_state.go new file mode 100644 index 000000000..71b124cce --- /dev/null +++ b/vm/test/fake_app_state.go @@ -0,0 +1,127 @@ +package main + +import ( + "fmt" + + . "github.com/tendermint/tendermint/common" + . "github.com/tendermint/tendermint/vm" +) + +type FakeAppState struct { + accounts map[string]*Account + storage map[string]Word + logs []*Log +} + +func (fas *FakeAppState) GetAccount(addr Word) (*Account, error) { + account := fas.accounts[addr.String()] + if account == nil { + return account, nil + } else { + return nil, Errorf("Invalid account addr: %v", addr) + } +} + +func (fas *FakeAppState) UpdateAccount(account *Account) error { + _, ok := fas.accounts[account.Address.String()] + if !ok { + return Errorf("Invalid account addr: %v", account.Address.String()) + } else { + // Nothing to do + return nil + } +} + +func (fas *FakeAppState) DeleteAccount(account *Account) error { + _, ok := fas.accounts[account.Address.String()] + if !ok { + return Errorf("Invalid account addr: %v", account.Address.String()) + } else { + // Delete account + delete(fas.accounts, account.Address.String()) + return nil + } +} + +func (fas *FakeAppState) CreateAccount(addr Word, balance uint64) (*Account, error) { + account := fas.accounts[addr.String()] + if account == nil { + return &Account{ + Address: addr, + Balance: balance, + Code: nil, + Nonce: 0, + StateRoot: Zero, + }, nil + } else { + return nil, Errorf("Invalid account addr: %v", addr) + } +} + +func (fas *FakeAppState) GetStorage(addr Word, key Word) (Word, error) { + _, ok := fas.accounts[addr.String()] + if !ok { + return Zero, Errorf("Invalid account addr: %v", addr) + } + + value, ok := fas.storage[addr.String()+key.String()] + if ok { + return value, nil + } else { + return Zero, nil + } +} + +func (fas *FakeAppState) SetStorage(addr Word, key Word, value Word) (bool, error) { + _, ok := fas.accounts[addr.String()] + if !ok { + return false, Errorf("Invalid account addr: %v", addr) + } + + _, ok = fas.storage[addr.String()+key.String()] + fas.storage[addr.String()+key.String()] = value + return ok, nil +} + +func (fas *FakeAppState) RemoveStorage(addr Word, key Word) error { + _, ok := fas.accounts[addr.String()] + if !ok { + return Errorf("Invalid account addr: %v", addr) + } + + delete(fas.storage, addr.String()+key.String()) + return nil +} + +func (fas *FakeAppState) AddLog(log *Log) { + fas.logs = append(fas.logs, log) +} + +func main() { + appState := &FakeAppState{ + accounts: make(map[string]*Account), + storage: make(map[string]Word), + logs: nil, + } + params := VMParams{ + BlockHeight: 0, + BlockHash: Zero, + BlockTime: 0, + GasLimit: 0, + } + ourVm := NewVM(appState, params) + + // Create accounts + account1, err := appState.CreateAccount(Uint64ToWord(100), 0) + if err != nil { + panic(err) + } + account2, err := appState.CreateAccount(Uint64ToWord(101), 0) + if err != nil { + panic(err) + } + + var gas uint64 = 1000 + output, err := ourVm.Call(account1, account2, []byte{0x60, 0x01, 0x60, 0x01}, []byte{}, 0, &gas) + fmt.Printf("Output: %v Error: %v\n", output, err) +}