Skip to content

Commit

Permalink
Merge branch 'master' into dependabot/go_modules/github.com/prometheu…
Browse files Browse the repository at this point in the history
…s/client_golang-1.16.0
  • Loading branch information
bluesign authored Jul 27, 2023
2 parents 78722a0 + 700b2b7 commit 6909b73
Show file tree
Hide file tree
Showing 29 changed files with 1,000 additions and 132 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/nightly-builds.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
steps:
- uses: actions/checkout@v3
- name: Setup Go
uses: actions/setup-go@v3
uses: actions/setup-go@v4
with:
go-version: ${{ env.GO_VERSION }}
cache: true
Expand Down
80 changes: 47 additions & 33 deletions README.md

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions adapters/access_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Flow Emulator
*
* Copyright 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 adapters

import (
Expand Down
18 changes: 18 additions & 0 deletions adapters/sdk_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Flow Emulator
*
* Copyright 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 adapters

import (
Expand Down
2 changes: 1 addition & 1 deletion check-headers.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/sh

files=$(find . -name \*.go -type f -print0 | xargs -0 egrep -L '(Licensed under the Apache License)|(Code generated from|by)')
files=$(find . -name \*.go -type f -print0 | xargs -0 egrep -L '(Licensed under the Apache License)|(Code generated (from|by))')
if [ -n "$files" ]; then
echo "Missing license header in:"
echo "$files"
Expand Down
18 changes: 18 additions & 0 deletions convert/flow_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Flow Emulator
*
* Copyright 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 convert

import (
Expand Down
128 changes: 81 additions & 47 deletions docs/overview.md

Large diffs are not rendered by default.

117 changes: 92 additions & 25 deletions emulator/blockchain.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Flow Emulator
*
* Copyright 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 emulator provides an emulated version of the Flow emulator that can be used
// for development purposes.
//
Expand All @@ -15,6 +33,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"math"
"strings"
"sync"
"time"
Expand All @@ -38,6 +57,7 @@ import (
fvmcrypto "github.com/onflow/flow-go/fvm/crypto"
"github.com/onflow/flow-go/fvm/environment"
fvmerrors "github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/meter"
reusableRuntime "github.com/onflow/flow-go/fvm/runtime"
"github.com/onflow/flow-go/fvm/storage/snapshot"
flowgo "github.com/onflow/flow-go/model/flow"
Expand All @@ -62,6 +82,7 @@ func New(opts ...Option) (*Blockchain, error) {
activeDebuggingSession: false,
conf: conf,
clock: NewSystemClock(),
sourceFileMap: make(map[common.Location]string),
}
err := b.ReloadBlockchain()
if err != nil {
Expand Down Expand Up @@ -263,7 +284,7 @@ func WithCoverageReport(coverageReport *runtime.CoverageReport) Option {
// Contracts allows users to deploy the given contracts.
// Some default common contracts are pre-configured in the `CommonContracts`
// global variable. It includes contracts such as:
// NonFungibleToken, FUSD, MetadataViews, NFTStorefront, NFTStorefrontV2, ExampleNFT
// NonFungibleToken, MetadataViews, NFTStorefront, NFTStorefrontV2, ExampleNFT
// The default value is []ContractDescription{}.
func Contracts(contracts []ContractDescription) Option {
return func(c *config) {
Expand Down Expand Up @@ -299,6 +320,8 @@ type Blockchain struct {
conf config

coverageReportedRuntime *CoverageReportedRuntime

sourceFileMap map[common.Location]string
}

// config is a set of configuration options for an emulated emulator.
Expand Down Expand Up @@ -538,7 +561,7 @@ func configureFVM(blockchain *Blockchain, conf config, blocks *blocks) (*fvm.Vir
fvm.WithBlocks(blocks),
fvm.WithContractDeploymentRestricted(false),
fvm.WithContractRemovalRestricted(!conf.ContractRemovalEnabled),
fvm.WithGasLimit(conf.ScriptGasLimit),
fvm.WithComputationLimit(conf.ScriptGasLimit),
fvm.WithCadenceLogging(true),
fvm.WithAccountStorageLimit(conf.StorageLimitEnabled),
fvm.WithTransactionFeesEnabled(conf.TransactionFeesEnabled),
Expand Down Expand Up @@ -692,6 +715,17 @@ func configureBootstrapProcedure(conf config, flowAccountKey flowgo.AccountPubli
options = append(options,
fvm.WithInitialTokenSupply(supply),
fvm.WithRestrictedAccountCreationEnabled(false),
fvm.WithTransactionFee(fvm.DefaultTransactionFees),
fvm.WithExecutionMemoryLimit(math.MaxUint32),
fvm.WithExecutionMemoryWeights(meter.DefaultMemoryWeights),
fvm.WithExecutionEffortWeights(map[common.ComputationKind]uint64{
common.ComputationKindStatement: 1569,
common.ComputationKindLoop: 1569,
common.ComputationKindFunctionInvocation: 1569,
environment.ComputationKindGetValue: 808,
environment.ComputationKindCreateAccount: 2837670,
environment.ComputationKindSetValue: 765,
}),
)
if conf.StorageLimitEnabled {
options = append(options,
Expand All @@ -700,27 +734,6 @@ func configureBootstrapProcedure(conf config, flowAccountKey flowgo.AccountPubli
fvm.WithStorageMBPerFLOW(conf.StorageMBPerFLOW),
)
}
if conf.TransactionFeesEnabled {
// This enables variable transaction fees AND execution effort metering
// as described in Variable Transaction Fees: Execution Effort FLIP: https://github.com/onflow/flow/pull/753)
// TODO: In the future this should be an injectable parameter. For now this is hard coded
// as this is the first iteration of variable execution fees.
options = append(options,
fvm.WithTransactionFee(fvm.BootstrapProcedureFeeParameters{
SurgeFactor: cadence.UFix64(100_000_000), // 1.0
InclusionEffortCost: cadence.UFix64(100), // 1E-6
ExecutionEffortCost: cadence.UFix64(499_000_000), // 4.99
}),
fvm.WithExecutionEffortWeights(map[common.ComputationKind]uint64{
common.ComputationKindStatement: 1569,
common.ComputationKindLoop: 1569,
common.ComputationKindFunctionInvocation: 1569,
environment.ComputationKindGetValue: 808,
environment.ComputationKindCreateAccount: 2837670,
environment.ComputationKindSetValue: 765,
}),
)
}
return fvm.Bootstrap(
flowAccountKey,
options...,
Expand Down Expand Up @@ -1182,7 +1195,10 @@ func (b *Blockchain) executeNextTransaction(ctx fvm.Context) (*types.Transaction

b.currentCode = string(txnBody.Script)
b.currentScriptID = txnId.String()
if b.activeDebuggingSession {

pragmas := ExtractPragmas(b.currentCode)

if b.activeDebuggingSession && pragmas.Contains(PragmaDebug) {
b.debugger.RequestPause()
}

Expand All @@ -1204,6 +1220,13 @@ func (b *Blockchain) executeNextTransaction(ctx fvm.Context) (*types.Transaction
tr.Debug = b.debugSignatureError(tr.Error, txnBody)
}

//add to source map if any pragma
if pragmas.Contains(PragmaSourceFile) {
location := common.NewTransactionLocation(nil, tr.TransactionID.Bytes())
sourceFile := pragmas.FilterByName(PragmaSourceFile).First().Argument()
b.sourceFileMap[location] = sourceFile
}

return tr, nil
}

Expand Down Expand Up @@ -1371,7 +1394,10 @@ func (b *Blockchain) executeScriptAtBlockID(script []byte, arguments [][]byte, i
scriptProc := fvm.Script(script).WithArguments(arguments...)
b.currentCode = string(script)
b.currentScriptID = scriptProc.ID.String()
if b.activeDebuggingSession {

pragmas := ExtractPragmas(b.currentCode)

if b.activeDebuggingSession && pragmas.Contains(PragmaDebug) {
b.debugger.RequestPause()
}
_, output, err := b.vm.Run(
Expand All @@ -1398,6 +1424,13 @@ func (b *Blockchain) executeScriptAtBlockID(script []byte, arguments [][]byte, i
scriptError = convert.VMErrorToEmulator(output.Err)
}

//add to source map if any pragma
if pragmas.Contains(PragmaSourceFile) {
location := common.NewScriptLocation(nil, scriptID.Bytes())
sourceFile := pragmas.FilterByName(PragmaSourceFile).First().Argument()
b.sourceFileMap[location] = sourceFile
}

return &types.ScriptResult{
ScriptID: scriptID,
Value: convertedValue,
Expand Down Expand Up @@ -1578,3 +1611,37 @@ func (b *Blockchain) SetClock(clock Clock) {
b.clock = clock
b.pendingBlock.SetClock(clock)
}

func (b *Blockchain) GetSourceFile(location common.Location) string {

value, exists := b.sourceFileMap[location]
if exists {
return value
}

addressLocation, isAddressLocation := location.(common.AddressLocation)
if !isAddressLocation {
return location.ID()
}
view := b.pendingBlock.ledgerState.NewChild()

env := environment.NewScriptEnvironmentFromStorageSnapshot(
b.vmCtx.EnvironmentParams,
view)

r := b.vmCtx.Borrow(env)
defer b.vmCtx.Return(r)

code, err := r.GetAccountContractCode(addressLocation)

if err != nil {
return location.ID()
}
pragmas := ExtractPragmas(string(code))
if pragmas.Contains(PragmaSourceFile) {
return pragmas.FilterByName(PragmaSourceFile).First().Argument()
}

return location.ID()

}
17 changes: 17 additions & 0 deletions emulator/collection_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Flow Emulator
*
* Copyright 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 emulator_test

import (
Expand Down
25 changes: 18 additions & 7 deletions emulator/contracts.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Flow Emulator
*
* Copyright 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 emulator

import (
Expand All @@ -10,20 +28,13 @@ import (
"github.com/onflow/flow-go/fvm"
flowgo "github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-nft/lib/go/contracts"
fusd "github.com/onflow/fusd/lib/go/contracts"
nftstorefront "github.com/onflow/nft-storefront/lib/go/contracts"
)

func NewCommonContracts(chain flowgo.Chain) []ContractDescription {
ftAddress := flowsdk.HexToAddress(fvm.FungibleTokenAddress(chain).HexWithPrefix())
serviceAddress := flowsdk.HexToAddress(chain.ServiceAddress().HexWithPrefix())
return []ContractDescription{
{
Name: "FUSD",
Address: serviceAddress,
Description: "💵 FUSD contract",
Source: fusd.FUSD(ftAddress.String()),
},
{
Name: "ExampleNFT",
Address: serviceAddress,
Expand Down
17 changes: 17 additions & 0 deletions emulator/contracts_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/*
* Flow Emulator
*
* Copyright 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 emulator_test

import (
Expand Down
18 changes: 18 additions & 0 deletions emulator/coverage_report_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,21 @@
/*
* Flow Emulator
*
* Copyright 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 emulator_test

import (
Expand Down
Loading

0 comments on commit 6909b73

Please sign in to comment.