This repository has been archived by the owner on May 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 346
/
memory_state.go
97 lines (84 loc) · 2.59 KB
/
memory_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
87
88
89
90
91
92
93
94
95
96
97
package acmstate
import (
"github.com/hyperledger/burrow/acm"
"github.com/hyperledger/burrow/binary"
"github.com/hyperledger/burrow/crypto"
"github.com/hyperledger/burrow/execution/errors"
"github.com/hyperledger/burrow/permission"
)
type MemoryState struct {
Accounts map[crypto.Address]*acm.Account
Storage map[crypto.Address]map[binary.Word256][]byte
Metadata map[MetadataHash]string
}
var _ IterableReaderWriter = &MemoryState{}
// Get an in-memory state IterableReader
func NewMemoryState() *MemoryState {
return &MemoryState{
Accounts: map[crypto.Address]*acm.Account{
acm.GlobalPermissionsAddress: {
Permissions: permission.DefaultAccountPermissions,
},
},
Storage: make(map[crypto.Address]map[binary.Word256][]byte),
Metadata: make(map[MetadataHash]string),
}
}
func (ms *MemoryState) GetAccount(address crypto.Address) (*acm.Account, error) {
return ms.Accounts[address], nil
}
func (ms *MemoryState) UpdateAccount(updatedAccount *acm.Account) error {
if updatedAccount == nil {
return errors.Errorf(errors.Codes.IllegalWrite, "UpdateAccount passed nil account in MemoryState")
}
ms.Accounts[updatedAccount.GetAddress()] = updatedAccount
return nil
}
func (ms *MemoryState) GetMetadata(metahash MetadataHash) (string, error) {
return ms.Metadata[metahash], nil
}
func (ms *MemoryState) SetMetadata(metahash MetadataHash, metadata string) error {
ms.Metadata[metahash] = metadata
return nil
}
func (ms *MemoryState) RemoveAccount(address crypto.Address) error {
delete(ms.Accounts, address)
return nil
}
func (ms *MemoryState) GetStorage(address crypto.Address, key binary.Word256) ([]byte, error) {
_, ok := ms.Accounts[address]
if !ok {
return nil, errors.Errorf(errors.Codes.NonExistentAccount,
"could not get storage for non-existent account: %v", address)
}
storage, ok := ms.Storage[address]
if !ok {
return nil, nil
}
return storage[key], nil
}
func (ms *MemoryState) SetStorage(address crypto.Address, key binary.Word256, value []byte) error {
storage, ok := ms.Storage[address]
if !ok {
storage = make(map[binary.Word256][]byte)
ms.Storage[address] = storage
}
storage[key] = value
return nil
}
func (ms *MemoryState) IterateAccounts(consumer func(*acm.Account) error) (err error) {
for _, acc := range ms.Accounts {
if err := consumer(acc); err != nil {
return err
}
}
return nil
}
func (ms *MemoryState) IterateStorage(address crypto.Address, consumer func(key binary.Word256, value []byte) error) (err error) {
for key, value := range ms.Storage[address] {
if err := consumer(key, value); err != nil {
return err
}
}
return nil
}