-
Notifications
You must be signed in to change notification settings - Fork 4
/
emulator.go
323 lines (273 loc) · 8.49 KB
/
emulator.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
* Flow CLI
*
* Copyright 2019 Dapper Labs, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package gateway
import (
"context"
"fmt"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime"
"github.com/onflow/flow-emulator/adapters"
"github.com/onflow/flow-emulator/emulator"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/rs/zerolog"
"google.golang.org/grpc/status"
)
type EmulatorKey struct {
PublicKey crypto.PublicKey
SigAlgo crypto.SignatureAlgorithm
HashAlgo crypto.HashAlgorithm
}
type EmulatorGateway struct {
emulator *emulator.Blockchain
adapter *adapters.SDKAdapter
accessAdapter *adapters.AccessAdapter
logger *zerolog.Logger
emulatorOptions []emulator.Option
}
func UnwrapStatusError(err error) error {
return fmt.Errorf(status.Convert(err).Message())
}
func NewEmulatorGateway(key *EmulatorKey) *EmulatorGateway {
return NewEmulatorGatewayWithOpts(key)
}
func NewEmulatorGatewayWithOpts(key *EmulatorKey, opts ...func(*EmulatorGateway)) *EmulatorGateway {
noopLogger := zerolog.Nop()
gateway := &EmulatorGateway{
logger: &noopLogger,
emulatorOptions: []emulator.Option{},
}
for _, opt := range opts {
opt(gateway)
}
gateway.emulator = newEmulator(key, gateway.emulatorOptions...)
gateway.adapter = adapters.NewSDKAdapter(gateway.logger, gateway.emulator)
gateway.accessAdapter = adapters.NewAccessAdapter(gateway.logger, gateway.emulator)
gateway.emulator.EnableAutoMine()
return gateway
}
func WithLogger(logger *zerolog.Logger) func(g *EmulatorGateway) {
return func(g *EmulatorGateway) {
g.logger = logger
}
}
func WithEmulatorOptions(options ...emulator.Option) func(g *EmulatorGateway) {
return func(g *EmulatorGateway) {
g.emulatorOptions = append(g.emulatorOptions, options...)
}
}
func newEmulator(key *EmulatorKey, emulatorOptions ...emulator.Option) *emulator.Blockchain {
var opts []emulator.Option
if key != nil {
opts = append(opts, emulator.WithServicePublicKey(key.PublicKey, key.SigAlgo, key.HashAlgo))
}
opts = append(opts, emulatorOptions...)
b, err := emulator.New(opts...)
if err != nil {
panic(err)
}
return b
}
func (g *EmulatorGateway) GetAccount(ctx context.Context, address flow.Address) (*flow.Account, error) {
account, err := g.adapter.GetAccount(ctx, address)
if err != nil {
return nil, UnwrapStatusError(err)
}
return account, nil
}
func (g *EmulatorGateway) SendSignedTransaction(ctx context.Context, tx *flow.Transaction) (*flow.Transaction, error) {
err := g.adapter.SendTransaction(ctx, *tx)
if err != nil {
return nil, UnwrapStatusError(err)
}
return tx, nil
}
func (g *EmulatorGateway) GetTransactionResult(ctx context.Context, ID flow.Identifier, _ bool) (*flow.TransactionResult, error) {
result, err := g.adapter.GetTransactionResult(ctx, ID)
if err != nil {
return nil, UnwrapStatusError(err)
}
return result, nil
}
func (g *EmulatorGateway) GetTransaction(ctx context.Context, id flow.Identifier) (*flow.Transaction, error) {
transaction, err := g.adapter.GetTransaction(ctx, id)
if err != nil {
return nil, UnwrapStatusError(err)
}
return transaction, nil
}
func (g *EmulatorGateway) GetTransactionResultsByBlockID(ctx context.Context, id flow.Identifier) ([]*flow.TransactionResult, error) {
txr, err := g.adapter.GetTransactionResultsByBlockID(ctx, id)
if err != nil {
return nil, UnwrapStatusError(err)
}
return txr, nil
}
func (g *EmulatorGateway) GetTransactionsByBlockID(ctx context.Context, id flow.Identifier) ([]*flow.Transaction, error) {
txr, err := g.adapter.GetTransactionsByBlockID(ctx, id)
if err != nil {
return nil, UnwrapStatusError(err)
}
return txr, nil
}
func (g *EmulatorGateway) Ping() error {
ctx := context.Background()
err := g.adapter.Ping(ctx)
if err != nil {
return UnwrapStatusError(err)
}
return nil
}
func (g *EmulatorGateway) WaitServer(ctx context.Context) error {
return nil
}
type scriptQuery struct {
id flow.Identifier
height uint64
latest bool
}
func (g *EmulatorGateway) executeScriptQuery(
ctx context.Context,
script []byte,
arguments []cadence.Value,
query scriptQuery,
) (cadence.Value, error) {
args, err := cadenceValuesToMessages(arguments)
if err != nil {
return nil, UnwrapStatusError(err)
}
var result []byte
if query.id != flow.EmptyID {
result, err = g.adapter.ExecuteScriptAtBlockID(ctx, query.id, script, args)
} else if query.height > 0 {
result, err = g.adapter.ExecuteScriptAtBlockHeight(ctx, query.height, script, args)
} else {
result, err = g.adapter.ExecuteScriptAtLatestBlock(ctx, script, args)
}
if err != nil {
return nil, UnwrapStatusError(err)
}
value, err := messageToCadenceValue(result)
if err != nil {
return nil, UnwrapStatusError(err)
}
return value, nil
}
func (g *EmulatorGateway) ExecuteScript(
ctx context.Context,
script []byte,
arguments []cadence.Value,
) (cadence.Value, error) {
return g.executeScriptQuery(ctx, script, arguments, scriptQuery{latest: true})
}
func (g *EmulatorGateway) ExecuteScriptAtHeight(
ctx context.Context,
script []byte,
arguments []cadence.Value,
height uint64,
) (cadence.Value, error) {
return g.executeScriptQuery(ctx, script, arguments, scriptQuery{height: height})
}
func (g *EmulatorGateway) ExecuteScriptAtID(
ctx context.Context,
script []byte,
arguments []cadence.Value,
id flow.Identifier,
) (cadence.Value, error) {
return g.executeScriptQuery(ctx, script, arguments, scriptQuery{id: id})
}
func (g *EmulatorGateway) GetLatestBlock(ctx context.Context) (*flow.Block, error) {
block, _, err := g.adapter.GetLatestBlock(ctx, true)
if err != nil {
return nil, UnwrapStatusError(err)
}
return block, nil
}
func cadenceValuesToMessages(values []cadence.Value) ([][]byte, error) {
msgs := make([][]byte, len(values))
for i, val := range values {
msg, err := jsoncdc.Encode(val)
if err != nil {
return nil, fmt.Errorf("convert: %w", err)
}
msgs[i] = msg
}
return msgs, nil
}
func messageToCadenceValue(m []byte) (cadence.Value, error) {
v, err := jsoncdc.Decode(nil, m)
if err != nil {
return nil, fmt.Errorf("convert: %w", err)
}
return v, nil
}
func (g *EmulatorGateway) GetEvents(
ctx context.Context,
eventType string,
startHeight uint64,
endHeight uint64,
) ([]flow.BlockEvents, error) {
events := make([]flow.BlockEvents, 0)
for height := startHeight; height <= endHeight; height++ {
events = append(events, g.getBlockEvent(ctx, height, eventType))
}
return events, nil
}
func (g *EmulatorGateway) getBlockEvent(ctx context.Context, height uint64, eventType string) flow.BlockEvents {
events, _ := g.adapter.GetEventsForHeightRange(ctx, eventType, height, height)
return *events[0]
}
func (g *EmulatorGateway) GetCollection(ctx context.Context, id flow.Identifier) (*flow.Collection, error) {
collection, err := g.adapter.GetCollectionByID(ctx, id)
if err != nil {
return nil, UnwrapStatusError(err)
}
return collection, nil
}
func (g *EmulatorGateway) GetBlockByID(ctx context.Context, id flow.Identifier) (*flow.Block, error) {
block, _, err := g.adapter.GetBlockByID(ctx, id)
if err != nil {
return nil, UnwrapStatusError(err)
}
return block, nil
}
func (g *EmulatorGateway) GetBlockByHeight(ctx context.Context, height uint64) (*flow.Block, error) {
block, _, err := g.adapter.GetBlockByHeight(ctx, height)
if err != nil {
return nil, UnwrapStatusError(err)
}
return block, nil
}
func (g *EmulatorGateway) GetLatestProtocolStateSnapshot(ctx context.Context) ([]byte, error) {
snapshot, err := g.adapter.GetLatestProtocolStateSnapshot(ctx)
if err != nil {
return nil, UnwrapStatusError(err)
}
return snapshot, nil
}
// SecureConnection placeholder func to complete gateway interface implementation
func (g *EmulatorGateway) SecureConnection() bool {
return false
}
func (g *EmulatorGateway) CoverageReport() *runtime.CoverageReport {
return g.emulator.CoverageReport()
}
func (g *EmulatorGateway) RollbackToBlockHeight(height uint64) error {
return g.emulator.RollbackToBlockHeight(height)
}