-
Notifications
You must be signed in to change notification settings - Fork 175
/
test.go
156 lines (132 loc) · 4.06 KB
/
test.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
package utils
import (
"fmt"
"strings"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
)
// SimpleView provides a simple view for testing purposes.
type SimpleView struct {
Parent *SimpleView
Ledger *MapLedger
}
func NewSimpleView() *SimpleView {
return &SimpleView{
Ledger: &MapLedger{
Registers: make(map[string]flow.RegisterEntry),
RegisterTouches: make(map[string]bool),
RegisterUpdated: make(map[string]bool),
},
}
}
func (v *SimpleView) NewChild() state.View {
ch := NewSimpleView()
ch.Parent = v
return ch
}
func (v *SimpleView) MergeView(o state.View) error {
var other *SimpleView
var ok bool
if other, ok = o.(*SimpleView); !ok {
return fmt.Errorf("can not merge: view type mismatch (given: %T, expected:SimpleView)", o)
}
for _, item := range other.Ledger.Registers {
err := v.Ledger.Set(item.Key.Owner, item.Key.Controller, item.Key.Key, item.Value)
if err != nil {
return fmt.Errorf("can not merge: %w", err)
}
}
for k := range other.Ledger.RegisterTouches {
v.Ledger.RegisterTouches[k] = true
}
return nil
}
func (v *SimpleView) DropDelta() {
v.Ledger.Registers = make(map[string]flow.RegisterEntry)
}
func (v *SimpleView) Set(owner, controller, key string, value flow.RegisterValue) error {
return v.Ledger.Set(owner, controller, key, value)
}
func (v *SimpleView) Get(owner, controller, key string) (flow.RegisterValue, error) {
value, err := v.Ledger.Get(owner, controller, key)
if err != nil {
return nil, err
}
if len(value) > 0 {
return value, nil
}
if v.Parent != nil {
return v.Parent.Get(owner, controller, key)
}
return nil, nil
}
// returns all the registers that has been touched
func (v *SimpleView) AllRegisters() []flow.RegisterID {
res := make([]flow.RegisterID, 0, len(v.Ledger.RegisterTouches))
for k := range v.Ledger.RegisterTouches {
res = append(res, v.Ledger.Registers[k].Key)
}
return res
}
func (v *SimpleView) RegisterUpdates() ([]flow.RegisterID, []flow.RegisterValue) {
ids := make([]flow.RegisterID, 0, len(v.Ledger.RegisterUpdated))
values := make([]flow.RegisterValue, 0, len(v.Ledger.RegisterUpdated))
for k := range v.Ledger.RegisterUpdated {
ids = append(ids, v.Ledger.Registers[k].Key)
values = append(values, v.Ledger.Registers[k].Value)
}
return ids, values
}
func (v *SimpleView) Touch(owner, controller, key string) error {
return v.Ledger.Touch(owner, controller, key)
}
func (v *SimpleView) Delete(owner, controller, key string) error {
return v.Ledger.Delete(owner, controller, key)
}
// A MapLedger is a naive ledger storage implementation backed by a simple map.
//
// This implementation is designed for testing purposes.
type MapLedger struct {
Registers map[string]flow.RegisterEntry
RegisterTouches map[string]bool
RegisterUpdated map[string]bool
}
// NewMapLedger returns an instance of map ledger (should only be used for testing)
func NewMapLedger() *MapLedger {
return &MapLedger{
Registers: make(map[string]flow.RegisterEntry),
RegisterTouches: make(map[string]bool),
RegisterUpdated: make(map[string]bool),
}
}
func (m *MapLedger) Set(owner, controller, key string, value flow.RegisterValue) error {
k := fullKey(owner, controller, key)
m.RegisterTouches[k] = true
m.RegisterUpdated[k] = true
m.Registers[k] = flow.RegisterEntry{
Key: flow.RegisterID{
Owner: owner,
Controller: controller,
Key: key,
},
Value: value,
}
return nil
}
func (m *MapLedger) Get(owner, controller, key string) (flow.RegisterValue, error) {
k := fullKey(owner, controller, key)
m.RegisterTouches[k] = true
return m.Registers[k].Value, nil
}
func (m *MapLedger) Touch(owner, controller, key string) error {
m.RegisterTouches[fullKey(owner, controller, key)] = true
return nil
}
func (m *MapLedger) Delete(owner, controller, key string) error {
delete(m.RegisterTouches, fullKey(owner, controller, key))
return nil
}
func fullKey(owner, controller, key string) string {
// https://en.wikipedia.org/wiki/C0_and_C1_control_codes#Field_separators
return strings.Join([]string{owner, controller, key}, "\x1F")
}