-
Notifications
You must be signed in to change notification settings - Fork 27
/
worldAccountsAdapter.go
169 lines (137 loc) · 4.15 KB
/
worldAccountsAdapter.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package worldmock
import (
"bytes"
"context"
"errors"
"fmt"
"github.com/ElrondNetwork/arwen-wasm-vm/arwen"
"github.com/ElrondNetwork/elrond-go/core"
"github.com/ElrondNetwork/elrond-go/data"
"github.com/ElrondNetwork/elrond-go/data/state"
)
// ErrTrieHandlingNotImplemented indicates that no trie-related operations are
// currently implemented.
var ErrTrieHandlingNotImplemented = errors.New("trie handling not implemented")
// MockAccountsAdapter is an implementation of AccountsAdapter based on
// MockWorld and the accounts within it.
type MockAccountsAdapter struct {
World *MockWorld
Snapshots []AccountMap
}
// NewMockAccountsAdapter instantiates a new MockAccountsAdapter.
func NewMockAccountsAdapter(world *MockWorld) *MockAccountsAdapter {
return &MockAccountsAdapter{
World: world,
Snapshots: make([]AccountMap, 0),
}
}
// GetExistingAccount -
func (m *MockAccountsAdapter) GetExistingAccount(address []byte) (state.AccountHandler, error) {
account, exists := m.World.AcctMap[string(address)]
if !exists {
return nil, arwen.ErrInvalidAccount
}
return account, nil
}
// LoadAccount -
func (m *MockAccountsAdapter) LoadAccount(address []byte) (state.AccountHandler, error) {
return m.GetExistingAccount(address)
}
// SaveAccount -
func (m *MockAccountsAdapter) SaveAccount(account state.AccountHandler) error {
mockAccount, ok := account.(*Account)
if !ok {
return errors.New("invalid account to save")
}
m.World.AcctMap.PutAccount(mockAccount)
return nil
}
// RemoveAccount -
func (m *MockAccountsAdapter) RemoveAccount(address []byte) error {
_, exists := m.World.AcctMap[string(address)]
if !exists {
return arwen.ErrInvalidAccount
}
m.World.AcctMap.DeleteAccount(address)
return nil
}
// Commit -
func (m *MockAccountsAdapter) Commit() ([]byte, error) {
m.Snapshots = make([]AccountMap, 0)
return nil, nil
}
// JournalLen -
func (m *MockAccountsAdapter) JournalLen() int {
return 1
}
// RevertToSnapshot -
func (m *MockAccountsAdapter) RevertToSnapshot(snapshotIndex int) error {
if len(m.Snapshots) == 0 {
return errors.New("no snapshots")
}
if snapshotIndex >= len(m.Snapshots) || snapshotIndex < 0 {
return fmt.Errorf(
"snapshot %d out of bounds (min 0, max %d)",
snapshotIndex,
len(m.Snapshots)-1)
}
snapshot := m.Snapshots[snapshotIndex]
m.Snapshots = m.Snapshots[:snapshotIndex]
// TODO should probably set BalanceDelta of all accounts to 0 as well?
return m.World.AcctMap.LoadAccountStorageFrom(snapshot)
}
// GetNumCheckpoints -
func (m *MockAccountsAdapter) GetNumCheckpoints() uint32 {
return uint32(len(m.Snapshots))
}
// GetCode -
func (m *MockAccountsAdapter) GetCode(codeHash []byte) []byte {
for _, account := range m.World.AcctMap {
if bytes.Equal(account.GetCodeHash(), codeHash) {
return account.GetCode()
}
}
return nil
}
// RootHash -
func (m *MockAccountsAdapter) RootHash() ([]byte, error) {
return nil, ErrTrieHandlingNotImplemented
}
// RecreateTrie -
func (m *MockAccountsAdapter) RecreateTrie(_ []byte) error {
return ErrTrieHandlingNotImplemented
}
// PruneTrie -
func (m *MockAccountsAdapter) PruneTrie(_ []byte, _ data.TriePruningIdentifier) {
}
// CancelPrune -
func (m *MockAccountsAdapter) CancelPrune(_ []byte, _ data.TriePruningIdentifier) {
}
// SnapshotState -
func (m *MockAccountsAdapter) SnapshotState(_ []byte, _ context.Context) {
snapshot := m.World.AcctMap.Clone()
m.Snapshots = append(m.Snapshots, snapshot)
}
// SetStateCheckpoint -
func (m *MockAccountsAdapter) SetStateCheckpoint(_ []byte, _ context.Context) {
}
// IsPruningEnabled -
func (m *MockAccountsAdapter) IsPruningEnabled() bool {
return false
}
// GetAllLeaves -
func (m *MockAccountsAdapter) GetAllLeaves(_ []byte, _ context.Context) (chan core.KeyValueHolder, error) {
return nil, ErrTrieHandlingNotImplemented
}
// RecreateAllTries -
func (m *MockAccountsAdapter) RecreateAllTries(_ []byte, _ context.Context) (map[string]data.Trie, error) {
return nil, ErrTrieHandlingNotImplemented
}
// GetTrie -
func (m *MockAccountsAdapter) GetTrie(_ []byte) (data.Trie, error) {
return nil, ErrTrieHandlingNotImplemented
}
// IsInterfaceNil -
func (m *MockAccountsAdapter) IsInterfaceNil() bool {
return m == nil
}