-
Notifications
You must be signed in to change notification settings - Fork 175
/
view.go
263 lines (218 loc) · 7.18 KB
/
view.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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
package delta
import (
"fmt"
"sync"
"github.com/onflow/flow-go/crypto/hash"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
)
// GetRegisterFunc is a function that returns the value for a register.
type GetRegisterFunc func(owner, controller, key string) (flow.RegisterValue, error)
// A View is a read-only view into a ledger stored in an underlying data source.
//
// A ledger view records writes to a delta that can be used to update the
// underlying data source.
type View struct {
delta Delta
regTouchSet map[string]flow.RegisterID // contains all the registers that have been touched (either read or written to)
readsCount uint64 // contains the total number of reads
// spockSecret keeps the secret used for SPoCKs
// TODO we can add a flag to disable capturing spockSecret
// for views other than collection views to improve performance
spockSecret []byte
spockSecretLock sync.Mutex
spockSecretHasher hash.Hasher
readFunc GetRegisterFunc
}
type Snapshot struct {
Delta Delta
Reads map[string]flow.RegisterID
}
// Snapshot is state of interactions with the register
type SpockSnapshot struct {
Snapshot
SpockSecret []byte
}
func AlwaysEmptyGetRegisterFunc(owner, controller, key string) (flow.RegisterValue, error) {
return nil, nil
}
// NewView instantiates a new ledger view with the provided read function.
func NewView(readFunc GetRegisterFunc) *View {
return &View{
delta: NewDelta(),
regTouchSet: make(map[string]flow.RegisterID),
readFunc: readFunc,
spockSecretHasher: hash.NewSHA3_256(),
}
}
// Snapshot returns copy of current state of interactions with a View
func (v *View) Interactions() *SpockSnapshot {
var delta = Delta{
Data: make(map[string]flow.RegisterEntry, len(v.delta.Data)),
}
var reads = make(map[string]flow.RegisterID, len(v.regTouchSet))
//copy data
for s, value := range v.delta.Data {
delta.Data[s] = value
}
for i, id := range v.regTouchSet {
reads[i] = id
}
return &SpockSnapshot{
Snapshot: Snapshot{
Delta: delta,
Reads: reads,
},
SpockSecret: v.SpockSecret(),
}
}
// AllRegisters returns all the register IDs either in read or delta
func (r *Snapshot) AllRegisters() []flow.RegisterID {
set := make(map[string]flow.RegisterID, len(r.Reads)+len(r.Delta.Data))
for _, reg := range r.Reads {
set[reg.String()] = reg
}
for _, reg := range r.Delta.RegisterIDs() {
set[reg.String()] = reg
}
ret := make([]flow.RegisterID, 0, len(set))
for _, r := range set {
ret = append(ret, r)
}
return ret
}
// NewChild generates a new child view, with the current view as the base, sharing the Get function
func (v *View) NewChild() state.View {
return NewView(v.Peek)
}
func (v *View) DropDelta() {
v.delta = NewDelta()
}
func (v *View) AllRegisters() []flow.RegisterID {
return v.Interactions().AllRegisters()
}
func (v *View) RegisterUpdates() ([]flow.RegisterID, []flow.RegisterValue) {
return v.Delta().RegisterUpdates()
}
// Get gets a register value from this view.
//
// This function will return an error if it fails to read from the underlying
// data source for this view.
func (v *View) Get(owner, controller, key string) (flow.RegisterValue, error) {
var err error
registerID := flow.NewRegisterID(owner, controller, key)
value, exists := v.delta.Get(owner, controller, key)
if !exists {
value, err = v.readFunc(owner, controller, key)
if err != nil {
return nil, fmt.Errorf("get register failed: %w", err)
}
// capture register touch
v.regTouchSet[registerID.String()] = registerID
// increase reads
v.readsCount++
}
// every time we read a value (order preserving) we update the secret
// with the registerID only (value is not required)
_, err = v.spockSecretHasher.Write(registerID.Bytes())
if err != nil {
return nil, fmt.Errorf("get register failed: %w", err)
}
return value, nil
}
// Peek reads the value without registering the read, as when used as parent read function
func (v *View) Peek(owner, controller, key string) (flow.RegisterValue, error) {
value, exists := v.delta.Get(owner, controller, key)
if exists {
return value, nil
}
return v.readFunc(owner, controller, key)
}
// Set sets a register value in this view.
func (v *View) Set(owner, controller, key string, value flow.RegisterValue) error {
registerID := flow.NewRegisterID(owner, controller, key)
// every time we write something to delta (order preserving) we update
// the spock secret with both the register ID and value.
_, err := v.spockSecretHasher.Write(registerID.Bytes())
if err != nil {
return fmt.Errorf("set register failed: %w", err)
}
_, err = v.spockSecretHasher.Write(value)
if err != nil {
return fmt.Errorf("set register failed: %w", err)
}
// capture register touch
v.regTouchSet[registerID.String()] = registerID
// add key value to delta
v.delta.Set(owner, controller, key, value)
return nil
}
// Touch explicitly adds a register to the touched registers set.
func (v *View) Touch(owner, controller, key string) error {
k := flow.NewRegisterID(owner, controller, key)
// capture register touch
v.regTouchSet[k.String()] = k
// increase reads
v.readsCount++
return nil
}
// Delete removes a register in this view.
func (v *View) Delete(owner, controller, key string) error {
return v.Set(owner, controller, key, nil)
}
// Delta returns a record of the registers that were mutated in this view.
func (v *View) Delta() Delta {
return v.delta
}
// MergeView applies the changes from a the given view to this view.
// TODO rename this, this is not actually a merge as we can't merge
// readFunc s.
func (v *View) MergeView(ch state.View) error {
child, ok := ch.(*View)
if !ok {
return fmt.Errorf("can not merge view: view type mismatch (given: %T, expected:delta.View)", ch)
}
for _, id := range child.Interactions().RegisterTouches() {
v.regTouchSet[id.String()] = id
}
// SpockSecret is order aware
// TODO return the error and handle it properly on other places
spockSecret := child.SpockSecret()
_, err := v.spockSecretHasher.Write(spockSecret)
if err != nil {
return fmt.Errorf("merging SPoCK secrets failed: %w", err)
}
v.delta.MergeWith(child.delta)
v.readsCount += child.readsCount
return nil
}
// RegisterTouches returns the register IDs touched by this view (either read or write)
func (r *Snapshot) RegisterTouches() map[string]flow.RegisterID {
ret := make(map[string]flow.RegisterID, len(r.Reads))
for k, v := range r.Reads {
ret[k] = v
}
return ret
}
// ReadsCount returns the total number of reads performed on this view including all child views
func (v *View) ReadsCount() uint64 {
return v.readsCount
}
// SpockSecret returns the secret value for SPoCK
//
// This function modifies the internal state of the SPoCK secret hasher.
// Once called, it doesn't allow writing more data into the SPoCK secret.
func (v *View) SpockSecret() []byte {
// check if spockSecret has been already computed
v.spockSecretLock.Lock()
if v.spockSecret == nil {
v.spockSecret = v.spockSecretHasher.SumHash()
}
v.spockSecretLock.Unlock()
return v.spockSecret
}
// Detach detaches view from parent, by setting readFunc to
// default, empty one
func (v *View) Detach() {
v.readFunc = AlwaysEmptyGetRegisterFunc
}