-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathremote_view.go
94 lines (75 loc) · 1.98 KB
/
remote_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
package main
import (
"fmt"
"github.com/janezpodhostnik/flow-transaction-info/registers"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
)
type RemoteView struct {
Parent *RemoteView
Delta map[string]flow.RegisterValue
getRemoteRegister registers.RegisterGetRegisterFunc
}
func NewRemoteView(getRemoteRegister registers.RegisterGetRegisterFunc) *RemoteView {
view := &RemoteView{
Delta: make(map[string]flow.RegisterValue),
getRemoteRegister: getRemoteRegister,
}
return view
}
func (v *RemoteView) NewChild() state.View {
return &RemoteView{
Parent: v,
Delta: make(map[string][]byte),
}
}
func (v *RemoteView) MergeView(o state.View) error {
var other *RemoteView
var ok bool
if other, ok = o.(*RemoteView); !ok {
return fmt.Errorf("can not merge: view type mismatch (given: %T, expected:RemoteView)", o)
}
for k, value := range other.Delta {
v.Delta[k] = value
}
return nil
}
func (v *RemoteView) DropDelta() {
v.Delta = make(map[string]flow.RegisterValue)
}
func (v *RemoteView) Set(owner, key string, value flow.RegisterValue) error {
v.Delta[owner+"~"+key] = value
return nil
}
func (v *RemoteView) Get(owner, key string) (flow.RegisterValue, error) {
// first check the delta
value, found := v.Delta[owner+"~"+key]
if found {
return value, nil
}
// then call the parent (if exist)
if v.Parent != nil {
return v.Parent.Get(owner, key)
}
// last use the getRemoteRegister
resp, err := v.getRemoteRegister(owner, key)
if err != nil {
return nil, err
}
return resp, nil
}
// returns all the registers that has been touched
func (v *RemoteView) AllRegisters() []flow.RegisterID {
panic("Not implemented yet")
}
func (v *RemoteView) RegisterUpdates() ([]flow.RegisterID, []flow.RegisterValue) {
panic("Not implemented yet")
}
func (v *RemoteView) Touch(owner, key string) error {
// no-op for now
return nil
}
func (v *RemoteView) Delete(owner, key string) error {
v.Delta[owner+"~"+key] = nil
return nil
}