-
Notifications
You must be signed in to change notification settings - Fork 2
/
fake_app_state.go
86 lines (70 loc) · 2.01 KB
/
fake_app_state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package bvm
import (
"bytes"
"fmt"
"github.com/bcbchain/bcbchain/hyperledger/burrow/crypto"
goCrypto "github.com/bcbchain/bclib/tendermint/go-crypto"
"github.com/bcbchain/bcbchain/hyperledger/burrow/acm"
"github.com/bcbchain/bcbchain/hyperledger/burrow/acm/acmstate"
. "github.com/bcbchain/bcbchain/hyperledger/burrow/binary"
)
type FakeAppState struct {
Accounts map[crypto.BVMAddress]*acm.Account
Storage map[string][]byte
tokenAddr goCrypto.Address
}
func (fas *FakeAppState) GetToken() goCrypto.Address {
return fas.tokenAddr
}
func (fas *FakeAppState) SetToken(token goCrypto.Address) {
fas.tokenAddr = token
}
var _ acmstate.ReaderWriter = &FakeAppState{}
func (fas *FakeAppState) GetAccount(addr crypto.BVMAddress) (*acm.Account, error) {
account := fas.Accounts[addr]
return account, nil
}
func (fas *FakeAppState) UpdateAccount(account *acm.Account) error {
fas.Accounts[account.Address] = account
return nil
}
func (fas *FakeAppState) RemoveAccount(address crypto.BVMAddress) error {
_, ok := fas.Accounts[address]
if !ok {
panic(fmt.Sprintf("Invalid account addr: %s", address))
} else {
// Remove account
delete(fas.Accounts, address)
}
return nil
}
func (fas *FakeAppState) GetStorage(addr crypto.BVMAddress, key Word256) ([]byte, error) {
_, ok := fas.Accounts[addr]
if !ok {
return []byte{}, nil
// panic(fmt.Sprintf("Invalid account addr: %s", addr))
}
value, ok := fas.Storage[addr.String()+key.String()]
if ok {
return value, nil
} else {
return []byte{}, nil
}
}
func (fas *FakeAppState) SetStorage(addr crypto.BVMAddress, key Word256, value []byte) error {
_, ok := fas.Accounts[addr]
if !ok {
fmt.Println("\n\n", fas.accountsDump())
panic(fmt.Sprintf("Invalid account addr: %s", addr))
}
fas.Storage[addr.String()+key.String()] = value
return nil
}
func (fas *FakeAppState) accountsDump() string {
buf := new(bytes.Buffer)
fmt.Fprint(buf, "Dumping Accounts...", "\n")
for _, acc := range fas.Accounts {
fmt.Fprint(buf, acc.Address, "\n")
}
return buf.String()
}