Skip to content

Commit 2de2a1a

Browse files
authored
Merge pull request #603 from onflow/petera/fix-forking-master
Reenable forked mainnet
2 parents fec4774 + 6997b7f commit 2de2a1a

File tree

19 files changed

+542
-110
lines changed

19 files changed

+542
-110
lines changed

Makefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ GOPATH ?= $(HOME)/go
1515
install-tools:
1616
mkdir -p ${GOPATH}; \
1717
cd ${GOPATH}; \
18-
GO111MODULE=on go install github.com/golang/mock/mockgen@v1.6.0; \
18+
GO111MODULE=on go install go.uber.org/mock/mockgen@latest; \
1919
GO111MODULE=on go install github.com/axw/gocov/gocov@latest; \
2020
GO111MODULE=on go install github.com/matm/gocov-html@latest; \
2121
GO111MODULE=on go install github.com/sanderhahn/gozip/cmd/gozip@latest;
@@ -46,6 +46,8 @@ generate: generate-mocks
4646
generate-mocks:
4747
GO111MODULE=on ${GOPATH}/bin/mockgen -destination=emulator/mocks/emulator.go -package=mocks github.com/onflow/flow-emulator/emulator Emulator
4848
GO111MODULE=on ${GOPATH}/bin/mockgen -destination=storage/mocks/store.go -package=mocks github.com/onflow/flow-emulator/storage Store
49+
GO111MODULE=on ${GOPATH}/bin/mockgen -destination=internal/mocks/access.go -package=mocks github.com/onflow/flow-emulator/internal AccessAPIClient
50+
GO111MODULE=on ${GOPATH}/bin/mockgen -destination=internal/mocks/executiondata.go -package=mocks github.com/onflow/flow-emulator/internal ExecutionDataAPIClient
4951

5052
.PHONY: ci
5153
ci: install-tools test check-tidy test coverage check-headers

README.md

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ and if you plan to run the emulator with Docker you must use the environment var
6868
| `--contract-removal` | `FLOW_CONTRACTREMOVAL` | `true` | Allow removal of already deployed contracts, used for updating during development |
6969
| `--skip-tx-validation` | `FLOW_SKIPTRANSACTIONVALIDATION` | `false` | Skip verification of transaction signatures and sequence numbers |
7070
| `--host` | `FLOW_HOST` | ` ` | Host to listen on for emulator GRPC/REST/Admin servers (default: All Interfaces) |
71-
| `--chain-id` | `FLOW_CHAINID` | `emulator` | Chain to simulate, if 'mainnet' or 'testnet' values are used, you will be able to run transactions against that network and a local fork will be created.. Valid values are: 'emulator', 'testnet', 'mainnet' |
71+
| `--chain-id` | `FLOW_CHAINID` | `emulator` | Chain to simulate, if 'mainnet' or 'testnet' values are used, you will be able to run transactions against that network and a local fork will be created. Valid values are: 'emulator', 'testnet', 'mainnet' |
7272
| `--redis-url` | `FLOW_REDIS_URL` | '' | Redis-server URL for persisting redis storage backend ( `redis://[[username:]password@]host[:port][/database]` ) |
7373
| `--start-block-height` | `FLOW_STARTBLOCKHEIGHT` | `0` | Start block height to use when starting the network using 'testnet' or 'mainnet' as the chain-id |
74-
74+
| `--rpc-host` | `FLOW_RPCHOST` | '' | RPC host (access node) to query for previous state when starting the network using 'testnet' or 'mainnet' as the chain-id |
7575
| `--legacy-upgrade` | `FLOW_LEGACYUPGRADE` | `false` | Enable upgrading of legacy contracts |
7676

7777
## Running the emulator with the Flow CLI
@@ -225,20 +225,27 @@ flow keys generate
225225
```
226226

227227
## Emulating mainnet and testnet transactions
228-
The emulator allows you to simulate the execution of transactions as if they were
229-
performed on the mainnet or testnet. In order to activate this feature,
230-
you must specify the network name for the chain ID flag in the following manner:
228+
The emulator allows you to simulate the execution of transactions as if they were
229+
performed on the Mainnet or Testnet. In order to activate this feature,
230+
you must specify the network name for the chain ID flag as well as the RPC host
231+
to connect to.
232+
231233
```
232-
flow emulator --chain-id mainnet
234+
flow emulator --chain-id mainnet --rpc-host access-008.mainnet24.nodes.onflow.org:9000
235+
flow emulator --chain-id mainnet --rpc-host access-002.devnet49.nodes.onflow.org:9000
233236
```
234-
Please note, the actual execution on the real network may differ.
237+
238+
Please note, the actual execution on the real network may differ depending on the exact state when the transaction is executed.
235239

236240
By default, the forked network will start from the latest sealed block when the emulator
237241
is started. You can specify a different starting block height by using the `--start-block-height` flag.
238242

239-
You can also store all of your changes and cached registers to a persistent db using the `--persist` flag,
243+
You can also store all of your changes and cached registers to a persistent db by using the `--persist` flag,
240244
along with the other sqlite settings.
241245

246+
To submit transactions as a different account, you can use the `--skip-tx-validation` flag to disable transaction signature
247+
verification. Then submit transactions from any account using any valid private key.
248+
242249
## Debugging
243250
To debug any transactions sent via VSCode or Flow CLI, you can use the `debugger` pragma.
244251
This will cause execution to pause at the debugger for any transaction or script which includes that pragma.

adapters/access.go

Lines changed: 37 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -53,22 +53,22 @@ func NewAccessAdapter(logger *zerolog.Logger, emulator emulator.Emulator) *Acces
5353
}
5454
}
5555

56-
func convertError(err error) error {
56+
func convertError(err error, defaultStatusCode codes.Code) error {
5757
if err != nil {
5858
switch err.(type) {
5959
case types.InvalidArgumentError:
6060
return status.Error(codes.InvalidArgument, err.Error())
6161
case types.NotFoundError:
6262
return status.Error(codes.NotFound, err.Error())
6363
default:
64-
return status.Error(codes.Internal, err.Error())
64+
return status.Error(defaultStatusCode, err.Error())
6565
}
6666
}
6767
return nil
6868
}
6969

7070
func (a *AccessAdapter) Ping(_ context.Context) error {
71-
return convertError(a.emulator.Ping())
71+
return convertError(a.emulator.Ping(), codes.Internal)
7272
}
7373

7474
func (a *AccessAdapter) GetNetworkParameters(_ context.Context) access.NetworkParameters {
@@ -78,7 +78,7 @@ func (a *AccessAdapter) GetNetworkParameters(_ context.Context) access.NetworkPa
7878
func (a *AccessAdapter) GetLatestBlockHeader(_ context.Context, _ bool) (*flowgo.Header, flowgo.BlockStatus, error) {
7979
block, err := a.emulator.GetLatestBlock()
8080
if err != nil {
81-
return nil, flowgo.BlockStatusUnknown, convertError(err)
81+
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
8282
}
8383

8484
a.logger.Debug().Fields(map[string]any{
@@ -92,7 +92,7 @@ func (a *AccessAdapter) GetLatestBlockHeader(_ context.Context, _ bool) (*flowgo
9292
func (a *AccessAdapter) GetBlockHeaderByHeight(_ context.Context, height uint64) (*flowgo.Header, flowgo.BlockStatus, error) {
9393
block, err := a.emulator.GetBlockByHeight(height)
9494
if err != nil {
95-
return nil, flowgo.BlockStatusUnknown, convertError(err)
95+
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
9696
}
9797

9898
a.logger.Debug().Fields(map[string]any{
@@ -106,7 +106,7 @@ func (a *AccessAdapter) GetBlockHeaderByHeight(_ context.Context, height uint64)
106106
func (a *AccessAdapter) GetBlockHeaderByID(_ context.Context, id flowgo.Identifier) (*flowgo.Header, flowgo.BlockStatus, error) {
107107
block, err := a.emulator.GetBlockByID(id)
108108
if err != nil {
109-
return nil, flowgo.BlockStatusUnknown, convertError(err)
109+
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
110110
}
111111

112112
a.logger.Debug().Fields(map[string]any{
@@ -120,7 +120,7 @@ func (a *AccessAdapter) GetBlockHeaderByID(_ context.Context, id flowgo.Identifi
120120
func (a *AccessAdapter) GetLatestBlock(_ context.Context, _ bool) (*flowgo.Block, flowgo.BlockStatus, error) {
121121
block, err := a.emulator.GetLatestBlock()
122122
if err != nil {
123-
return nil, flowgo.BlockStatusUnknown, convertError(err)
123+
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
124124
}
125125

126126
a.logger.Debug().Fields(map[string]any{
@@ -134,7 +134,7 @@ func (a *AccessAdapter) GetLatestBlock(_ context.Context, _ bool) (*flowgo.Block
134134
func (a *AccessAdapter) GetBlockByHeight(_ context.Context, height uint64) (*flowgo.Block, flowgo.BlockStatus, error) {
135135
block, err := a.emulator.GetBlockByHeight(height)
136136
if err != nil {
137-
return nil, flowgo.BlockStatusUnknown, convertError(err)
137+
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
138138
}
139139

140140
a.logger.Debug().Fields(map[string]any{
@@ -148,7 +148,7 @@ func (a *AccessAdapter) GetBlockByHeight(_ context.Context, height uint64) (*flo
148148
func (a *AccessAdapter) GetBlockByID(_ context.Context, id flowgo.Identifier) (*flowgo.Block, flowgo.BlockStatus, error) {
149149
block, err := a.emulator.GetBlockByID(id)
150150
if err != nil {
151-
return nil, flowgo.BlockStatusUnknown, convertError(err)
151+
return nil, flowgo.BlockStatusUnknown, convertError(err, codes.Internal)
152152
}
153153

154154
a.logger.Debug().Fields(map[string]any{
@@ -162,7 +162,7 @@ func (a *AccessAdapter) GetBlockByID(_ context.Context, id flowgo.Identifier) (*
162162
func (a *AccessAdapter) GetCollectionByID(_ context.Context, id flowgo.Identifier) (*flowgo.LightCollection, error) {
163163
collection, err := a.emulator.GetCollectionByID(id)
164164
if err != nil {
165-
return nil, convertError(err)
165+
return nil, convertError(err, codes.Internal)
166166
}
167167

168168
a.logger.Debug().
@@ -175,7 +175,7 @@ func (a *AccessAdapter) GetCollectionByID(_ context.Context, id flowgo.Identifie
175175
func (a *AccessAdapter) GetTransaction(_ context.Context, id flowgo.Identifier) (*flowgo.TransactionBody, error) {
176176
tx, err := a.emulator.GetTransaction(id)
177177
if err != nil {
178-
return nil, convertError(err)
178+
return nil, convertError(err, codes.Internal)
179179
}
180180

181181
a.logger.Debug().
@@ -197,14 +197,14 @@ func (a *AccessAdapter) GetTransactionResult(
197197
) {
198198
result, err := a.emulator.GetTransactionResult(id)
199199
if err != nil {
200-
return nil, convertError(err)
200+
return nil, convertError(err, codes.Internal)
201201
}
202202

203203
// Convert CCF events to JSON events, else return CCF encoded version
204204
if requiredEventEncodingVersion == entities.EventEncodingVersion_JSON_CDC_V0 {
205205
result.Events, err = ConvertCCFEventsToJsonEvents(result.Events)
206206
if err != nil {
207-
return nil, convertError(err)
207+
return nil, convertError(err, codes.Internal)
208208
}
209209
}
210210
a.logger.Debug().
@@ -217,7 +217,7 @@ func (a *AccessAdapter) GetTransactionResult(
217217
func (a *AccessAdapter) GetAccount(_ context.Context, address flowgo.Address) (*flowgo.Account, error) {
218218
account, err := a.emulator.GetAccount(address)
219219
if err != nil {
220-
return nil, convertError(err)
220+
return nil, convertError(err, codes.Internal)
221221
}
222222

223223
a.logger.Debug().
@@ -230,7 +230,7 @@ func (a *AccessAdapter) GetAccount(_ context.Context, address flowgo.Address) (*
230230
func (a *AccessAdapter) GetAccountAtLatestBlock(ctx context.Context, address flowgo.Address) (*flowgo.Account, error) {
231231
account, err := a.GetAccount(ctx, address)
232232
if err != nil {
233-
return nil, convertError(err)
233+
return nil, convertError(err, codes.Internal)
234234
}
235235

236236
a.logger.Debug().
@@ -253,7 +253,7 @@ func (a *AccessAdapter) GetAccountAtBlockHeight(
253253

254254
account, err := a.emulator.GetAccountAtBlockHeight(address, height)
255255
if err != nil {
256-
return nil, convertError(err)
256+
return nil, convertError(err, codes.Internal)
257257
}
258258
return account, nil
259259
}
@@ -264,12 +264,12 @@ func convertScriptResult(result *types.ScriptResult, err error) ([]byte, error)
264264
}
265265

266266
if !result.Succeeded() {
267-
return nil, result.Error
267+
return nil, status.Error(codes.InvalidArgument, result.Error.Error())
268268
}
269269

270270
valueBytes, err := jsoncdc.Encode(result.Value)
271271
if err != nil {
272-
return nil, status.Error(codes.Internal, err.Error())
272+
return nil, convertError(err, codes.InvalidArgument)
273273
}
274274

275275
return valueBytes, nil
@@ -280,7 +280,14 @@ func (a *AccessAdapter) ExecuteScriptAtLatestBlock(
280280
script []byte,
281281
arguments [][]byte,
282282
) ([]byte, error) {
283-
a.logger.Debug().Msg("👤 ExecuteScriptAtLatestBlock called")
283+
latestBlock, err := a.emulator.GetLatestBlock()
284+
if err != nil {
285+
return nil, err
286+
}
287+
a.logger.Debug().
288+
Uint64("blockHeight", latestBlock.Header.Height).
289+
Msg("👤 ExecuteScriptAtLatestBlock called")
290+
284291
result, err := a.emulator.ExecuteScript(script, arguments)
285292
if err == nil {
286293
utils.PrintScriptResult(a.logger, result)
@@ -332,7 +339,7 @@ func (a *AccessAdapter) GetEventsForHeightRange(
332339
) ([]flowgo.BlockEvents, error) {
333340
events, err := a.emulator.GetEventsForHeightRange(eventType, startHeight, endHeight)
334341
if err != nil {
335-
return nil, convertError(err)
342+
return nil, convertError(err, codes.Internal)
336343
}
337344

338345
eventCount := 0
@@ -343,7 +350,7 @@ func (a *AccessAdapter) GetEventsForHeightRange(
343350
events[i].Events, err = ConvertCCFEventsToJsonEvents(events[i].Events)
344351
eventCount = eventCount + len(events[i].Events)
345352
if err != nil {
346-
return nil, convertError(err)
353+
return nil, convertError(err, codes.Internal)
347354
}
348355
}
349356
}
@@ -366,7 +373,7 @@ func (a *AccessAdapter) GetEventsForBlockIDs(
366373
) ([]flowgo.BlockEvents, error) {
367374
events, err := a.emulator.GetEventsForBlockIDs(eventType, blockIDs)
368375
if err != nil {
369-
return nil, convertError(err)
376+
return nil, convertError(err, codes.Internal)
370377
}
371378

372379
eventCount := 0
@@ -377,7 +384,7 @@ func (a *AccessAdapter) GetEventsForBlockIDs(
377384
events[i].Events, err = ConvertCCFEventsToJsonEvents(events[i].Events)
378385
eventCount = eventCount + len(events[i].Events)
379386
if err != nil {
380-
return nil, convertError(err)
387+
return nil, convertError(err, codes.Internal)
381388
}
382389
}
383390
}
@@ -426,18 +433,18 @@ func (a *AccessAdapter) GetTransactionResultByIndex(
426433
) (*access.TransactionResult, error) {
427434
results, err := a.emulator.GetTransactionResultsByBlockID(blockID)
428435
if err != nil {
429-
return nil, convertError(err)
436+
return nil, convertError(err, codes.Internal)
430437
}
431438
if len(results) <= int(index) {
432-
return nil, convertError(&types.TransactionNotFoundError{ID: flowgo.Identifier{}})
439+
return nil, convertError(&types.TransactionNotFoundError{ID: flowgo.Identifier{}}, codes.Internal)
433440
}
434441

435442
// Convert CCF events to JSON events, else return CCF encoded version
436443
if requiredEventEncodingVersion == entities.EventEncodingVersion_JSON_CDC_V0 {
437444
for i := range results {
438445
results[i].Events, err = ConvertCCFEventsToJsonEvents(results[i].Events)
439446
if err != nil {
440-
return nil, convertError(err)
447+
return nil, convertError(err, codes.Internal)
441448
}
442449
}
443450
}
@@ -448,7 +455,7 @@ func (a *AccessAdapter) GetTransactionResultByIndex(
448455
func (a *AccessAdapter) GetTransactionsByBlockID(_ context.Context, blockID flowgo.Identifier) ([]*flowgo.TransactionBody, error) {
449456
result, err := a.emulator.GetTransactionsByBlockID(blockID)
450457
if err != nil {
451-
return nil, convertError(err)
458+
return nil, convertError(err, codes.Internal)
452459
}
453460
return result, nil
454461
}
@@ -460,15 +467,15 @@ func (a *AccessAdapter) GetTransactionResultsByBlockID(
460467
) ([]*access.TransactionResult, error) {
461468
result, err := a.emulator.GetTransactionResultsByBlockID(blockID)
462469
if err != nil {
463-
return nil, convertError(err)
470+
return nil, convertError(err, codes.Internal)
464471
}
465472

466473
// Convert CCF events to JSON events, else return CCF encoded version
467474
if requiredEventEncodingVersion == entities.EventEncodingVersion_JSON_CDC_V0 {
468475
for i := range result {
469476
result[i].Events, err = ConvertCCFEventsToJsonEvents(result[i].Events)
470477
if err != nil {
471-
return nil, convertError(err)
478+
return nil, convertError(err, codes.Internal)
472479
}
473480
}
474481
}
@@ -481,7 +488,7 @@ func (a *AccessAdapter) SendTransaction(_ context.Context, tx *flowgo.Transactio
481488
Str("txID", tx.ID().String()).
482489
Msg(`✉️ Transaction submitted`)
483490

484-
return convertError(a.emulator.SendTransaction(tx))
491+
return convertError(a.emulator.SendTransaction(tx), codes.Internal)
485492
}
486493

487494
func (a *AccessAdapter) GetNodeVersionInfo(

adapters/access_test.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,20 @@ import (
2323
"fmt"
2424
"testing"
2525

26-
"github.com/onflow/flow/protobuf/go/flow/entities"
27-
26+
"github.com/rs/zerolog"
27+
"github.com/stretchr/testify/assert"
2828
"github.com/stretchr/testify/require"
29+
"go.uber.org/mock/gomock"
2930

30-
"github.com/golang/mock/gomock"
3131
"github.com/onflow/cadence"
3232
"github.com/onflow/cadence/encoding/ccf"
33-
"github.com/onflow/flow-emulator/emulator/mocks"
34-
"github.com/onflow/flow-emulator/types"
3533
"github.com/onflow/flow-go/access"
3634
"github.com/onflow/flow-go/engine/common/rpc/convert"
3735
flowgo "github.com/onflow/flow-go/model/flow"
38-
"github.com/rs/zerolog"
39-
"github.com/stretchr/testify/assert"
36+
"github.com/onflow/flow/protobuf/go/flow/entities"
37+
38+
"github.com/onflow/flow-emulator/emulator/mocks"
39+
"github.com/onflow/flow-emulator/types"
4040
)
4141

4242
func accessTest(f func(t *testing.T, adapter *AccessAdapter, emu *mocks.MockEmulator)) func(t *testing.T) {
@@ -451,6 +451,12 @@ func TestAccess(t *testing.T) {
451451
emulatorResult := types.ScriptResult{Value: stringValue}
452452
expected, _ := convertScriptResult(&emulatorResult, nil)
453453

454+
// called once for each script execution
455+
emu.EXPECT().
456+
GetLatestBlock().
457+
Return(&flowgo.Block{Header: &flowgo.Header{}}, nil).
458+
Times(2)
459+
454460
//success
455461
emu.EXPECT().
456462
ExecuteScript(script, arguments).

adapters/sdk_test.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,18 @@ import (
2323
"fmt"
2424
"testing"
2525

26-
"github.com/golang/mock/gomock"
26+
"github.com/rs/zerolog"
27+
"github.com/stretchr/testify/assert"
28+
"go.uber.org/mock/gomock"
29+
2730
"github.com/onflow/cadence"
28-
"github.com/onflow/flow-emulator/convert"
29-
"github.com/onflow/flow-emulator/emulator/mocks"
30-
"github.com/onflow/flow-emulator/types"
3131
flowgosdk "github.com/onflow/flow-go-sdk"
3232
"github.com/onflow/flow-go/access"
3333
flowgo "github.com/onflow/flow-go/model/flow"
34-
"github.com/rs/zerolog"
35-
"github.com/stretchr/testify/assert"
34+
35+
"github.com/onflow/flow-emulator/convert"
36+
"github.com/onflow/flow-emulator/emulator/mocks"
37+
"github.com/onflow/flow-emulator/types"
3638
)
3739

3840
func sdkTest(f func(t *testing.T, adapter *SDKAdapter, emu *mocks.MockEmulator)) func(t *testing.T) {

0 commit comments

Comments
 (0)