-
Notifications
You must be signed in to change notification settings - Fork 179
/
remoteView.go
211 lines (172 loc) · 5.09 KB
/
remoteView.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
package debug
import (
"context"
"fmt"
"google.golang.org/grpc"
"github.com/onflow/flow/protobuf/go/flow/execution"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
)
// RemoteView provides a view connected to a live execution node to read the registers
// writen values are kept inside a map
//
// TODO implement register touches
type RemoteView struct {
Parent *RemoteView
Delta map[string]flow.RegisterValue
Cache registerCache
BlockID []byte
BlockHeader *flow.Header
connection *grpc.ClientConn
executionAPIclient execution.ExecutionAPIClient
}
// A RemoteViewOption sets a configuration parameter for the remote view
type RemoteViewOption func(view *RemoteView) *RemoteView
// WithFileCache sets the output path to store
// register values so can be fetched from a file cache
// it loads the values from the cache upon object construction
func WithCache(cache registerCache) RemoteViewOption {
return func(view *RemoteView) *RemoteView {
view.Cache = cache
return view
}
}
// WithBlockID sets the blockID for the remote view, if not used
// remote view will use the latest sealed block
func WithBlockID(blockID flow.Identifier) RemoteViewOption {
return func(view *RemoteView) *RemoteView {
view.BlockID = blockID[:]
var err error
view.BlockHeader, err = view.getBlockHeader(blockID)
if err != nil {
panic(err)
}
return view
}
}
func NewRemoteView(grpcAddress string, opts ...RemoteViewOption) *RemoteView {
conn, err := grpc.Dial(grpcAddress, grpc.WithInsecure())
if err != nil {
panic(err)
}
view := &RemoteView{
connection: conn,
executionAPIclient: execution.NewExecutionAPIClient(conn),
Delta: make(map[string]flow.RegisterValue),
Cache: newMemRegisterCache(),
}
view.BlockID, view.BlockHeader, err = view.getLatestBlockID()
if err != nil {
panic(err)
}
for _, applyOption := range opts {
view = applyOption(view)
}
return view
}
func (v *RemoteView) Done() {
v.connection.Close()
}
func (v *RemoteView) getLatestBlockID() ([]byte, *flow.Header, error) {
req := &execution.GetLatestBlockHeaderRequest{
IsSealed: true,
}
resp, err := v.executionAPIclient.GetLatestBlockHeader(context.Background(), req)
if err != nil {
return nil, nil, err
}
// TODO set chainID and parentID
header := &flow.Header{
Height: resp.Block.Height,
Timestamp: resp.Block.Timestamp.AsTime(),
}
return resp.Block.Id, header, nil
}
func (v *RemoteView) getBlockHeader(blockID flow.Identifier) (*flow.Header, error) {
req := &execution.GetBlockHeaderByIDRequest{
Id: blockID[:],
}
resp, err := v.executionAPIclient.GetBlockHeaderByID(context.Background(), req)
if err != nil {
return nil, err
}
// TODO set chainID and parentID
header := &flow.Header{
Height: resp.Block.Height,
Timestamp: resp.Block.Timestamp.AsTime(),
}
return header, nil
}
func (v *RemoteView) NewChild() state.View {
return &RemoteView{
Parent: v,
executionAPIclient: v.executionAPIclient,
connection: v.connection,
Cache: newMemRegisterCache(),
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, controller, key string, value flow.RegisterValue) error {
v.Delta[owner+"~"+controller+"~"+key] = value
return nil
}
func (v *RemoteView) Get(owner, controller, key string) (flow.RegisterValue, error) {
// first check the delta
value, found := v.Delta[owner+"~"+controller+"~"+key]
if found {
return value, nil
}
// then check the read cache
value, found = v.Cache.Get(owner, controller, key)
if found {
return value, nil
}
// then call the parent (if exist)
if v.Parent != nil {
return v.Parent.Get(owner, controller, key)
}
// last use the grpc api the
req := &execution.GetRegisterAtBlockIDRequest{
BlockId: []byte(v.BlockID),
RegisterOwner: []byte(owner),
RegisterController: []byte(controller),
RegisterKey: []byte(key),
}
// TODO use a proper context for timeouts
resp, err := v.executionAPIclient.GetRegisterAtBlockID(context.Background(), req)
if err != nil {
return nil, err
}
v.Cache.Set(owner, controller, key, resp.Value)
// append value to the file cache
return resp.Value, 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, controller, key string) error {
// no-op for now
return nil
}
func (v *RemoteView) Delete(owner, controller, key string) error {
v.Delta[owner+"~"+controller+"~"+key] = nil
return nil
}