tendermint/vm/test/fake_app_state.go

80 lines
1.8 KiB
Go
Raw Normal View History

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"
. "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
storage map[string]Word256
2015-03-18 23:13:16 -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 17:23:36 -07:00
return account
2015-03-18 23:13:16 -07:00
}
func (fas *FakeAppState) UpdateAccount(account *Account) {
fas.accounts[account.Address.String()] = account
2015-03-18 23:13:16 -07:00
}
func (fas *FakeAppState) RemoveAccount(account *Account) {
2015-03-18 23:13:16 -07:00
_, ok := fas.accounts[account.Address.String()]
if !ok {
panic(Fmt("Invalid account addr: %X", account.Address))
2015-03-18 23:13:16 -07:00
} else {
// Remove account
2015-03-18 23:13:16 -07:00
delete(fas.accounts, account.Address.String())
}
}
func (fas *FakeAppState) CreateAccount(creator *Account) *Account {
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-18 23:13:16 -07:00
} else {
panic(Fmt("Invalid account addr: %X", addr))
2015-03-18 23:13:16 -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 {
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 {
return value
2015-03-18 23:13:16 -07:00
} else {
return Zero256
2015-03-18 23:13:16 -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 {
panic(Fmt("Invalid account addr: %X", addr))
2015-03-18 23:13:16 -07:00
}
fas.storage[addr.String()+key.String()] = value
}
// Creates a 20 byte address and bumps the nonce.
func createAddress(creator *Account) Word256 {
nonce := creator.Nonce
creator.Nonce += 1
temp := make([]byte, 32+8)
copy(temp, creator.Address[:])
PutInt64BE(temp[32:], nonce)
2015-04-17 17:39:50 -07:00
return LeftPadWord256(sha3.Sha3(temp)[:20])
}