2015-03-26 10:58:20 -07:00
|
|
|
package vm
|
2015-03-18 23:13:16 -07:00
|
|
|
|
|
|
|
import (
|
2015-04-01 17:30:16 -07:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2015-08-10 20:38:45 -07:00
|
|
|
"github.com/tendermint/tendermint/types"
|
2015-04-01 17:30:16 -07:00
|
|
|
. "github.com/tendermint/tendermint/vm"
|
|
|
|
"github.com/tendermint/tendermint/vm/sha3"
|
2015-03-18 23:13:16 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type FakeAppState struct {
|
|
|
|
accounts map[string]*Account
|
2015-03-28 23:44:07 -07:00
|
|
|
storage map[string]Word256
|
2015-08-10 20:38:45 -07:00
|
|
|
logs []types.EventDataLog
|
2015-03-18 23:13:16 -07:00
|
|
|
}
|
|
|
|
|
2015-03-28 23:44:07 -07:00
|
|
|
func (fas *FakeAppState) GetAccount(addr Word256) *Account {
|
2015-03-18 23:13:16 -07:00
|
|
|
account := fas.accounts[addr.String()]
|
2015-05-14 20:23:36 -04:00
|
|
|
return account
|
2015-03-18 23:13:16 -07:00
|
|
|
}
|
|
|
|
|
2015-03-28 23:44:07 -07:00
|
|
|
func (fas *FakeAppState) UpdateAccount(account *Account) {
|
2015-05-14 20:58:24 -04:00
|
|
|
fas.accounts[account.Address.String()] = account
|
2015-03-18 23:13:16 -07:00
|
|
|
}
|
|
|
|
|
2015-03-28 23:44:07 -07:00
|
|
|
func (fas *FakeAppState) RemoveAccount(account *Account) {
|
2015-03-18 23:13:16 -07:00
|
|
|
_, ok := fas.accounts[account.Address.String()]
|
|
|
|
if !ok {
|
2015-03-28 23:44:07 -07:00
|
|
|
panic(Fmt("Invalid account addr: %X", account.Address))
|
2015-03-18 23:13:16 -07:00
|
|
|
} else {
|
2015-03-28 23:44:07 -07:00
|
|
|
// Remove account
|
2015-03-18 23:13:16 -07:00
|
|
|
delete(fas.accounts, account.Address.String())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 23:44:07 -07:00
|
|
|
func (fas *FakeAppState) CreateAccount(creator *Account) *Account {
|
2015-03-20 19:59:42 -07:00
|
|
|
addr := createAddress(creator)
|
2015-03-18 23:13:16 -07:00
|
|
|
account := fas.accounts[addr.String()]
|
|
|
|
if account == nil {
|
|
|
|
return &Account{
|
2015-07-28 12:39:10 -07:00
|
|
|
Address: addr,
|
|
|
|
Balance: 0,
|
|
|
|
Code: nil,
|
|
|
|
Nonce: 0,
|
2015-03-28 23:44:07 -07:00
|
|
|
}
|
2015-03-18 23:13:16 -07:00
|
|
|
} else {
|
2015-03-28 23:44:07 -07:00
|
|
|
panic(Fmt("Invalid account addr: %X", addr))
|
2015-03-18 23:13:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 23:44:07 -07:00
|
|
|
func (fas *FakeAppState) GetStorage(addr Word256, key Word256) Word256 {
|
2015-03-18 23:13:16 -07:00
|
|
|
_, ok := fas.accounts[addr.String()]
|
|
|
|
if !ok {
|
2015-03-28 23:44:07 -07:00
|
|
|
panic(Fmt("Invalid account addr: %X", addr))
|
2015-03-18 23:13:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
value, ok := fas.storage[addr.String()+key.String()]
|
|
|
|
if ok {
|
2015-03-28 23:44:07 -07:00
|
|
|
return value
|
2015-03-18 23:13:16 -07:00
|
|
|
} else {
|
2015-03-28 23:44:07 -07:00
|
|
|
return Zero256
|
2015-03-18 23:13:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-28 23:44:07 -07:00
|
|
|
func (fas *FakeAppState) SetStorage(addr Word256, key Word256, value Word256) {
|
2015-03-18 23:13:16 -07:00
|
|
|
_, ok := fas.accounts[addr.String()]
|
|
|
|
if !ok {
|
2015-03-28 23:44:07 -07:00
|
|
|
panic(Fmt("Invalid account addr: %X", addr))
|
2015-03-18 23:13:16 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fas.storage[addr.String()+key.String()] = value
|
|
|
|
}
|
|
|
|
|
2015-08-10 20:38:45 -07:00
|
|
|
func (fas *FakeAppState) AddLog(log types.EventDataLog) {
|
2015-03-18 23:13:16 -07:00
|
|
|
fas.logs = append(fas.logs, log)
|
|
|
|
}
|
|
|
|
|
2015-03-20 19:59:42 -07:00
|
|
|
// Creates a 20 byte address and bumps the nonce.
|
2015-03-28 23:44:07 -07:00
|
|
|
func createAddress(creator *Account) Word256 {
|
2015-03-20 19:59:42 -07:00
|
|
|
nonce := creator.Nonce
|
|
|
|
creator.Nonce += 1
|
|
|
|
temp := make([]byte, 32+8)
|
|
|
|
copy(temp, creator.Address[:])
|
2015-06-25 20:28:34 -07:00
|
|
|
PutInt64BE(temp[32:], nonce)
|
2015-04-17 17:39:50 -07:00
|
|
|
return LeftPadWord256(sha3.Sha3(temp)[:20])
|
2015-03-20 19:59:42 -07:00
|
|
|
}
|