Skip to content

Commit

Permalink
Merge pull request #440 from onflow/auto-update-onflow-cadence-v1.2.1
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Oct 22, 2024
2 parents e126414 + fce173b commit 77087b2
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 2,140 deletions.
3 changes: 1 addition & 2 deletions languageserver/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ SCRIPTPATH=$(dirname "$0")

if [ "$1" = "cadence" ] && [ "$2" = "language-server" ] ; then
(cd "$SCRIPTPATH" && \
go build -gcflags="all=-N -l" ./cmd/languageserver && \
dlv --log-dest 2 --continue --listen=:2345 --headless=true --api-version=2 --accept-multiclient exec ./languageserver -- "$@");
go run -gcflags="all=-N -l" ./cmd/languageserver --enable-flow-client=false);
else
flow "$@"
fi
73 changes: 59 additions & 14 deletions test/emulator_backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,20 @@ import (
"context"
"encoding/hex"
"fmt"
"sort"
"strings"
"time"

"github.com/onflow/cadence"
"github.com/onflow/cadence/ast"
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/errors"
"github.com/onflow/cadence/interpreter"
"github.com/onflow/cadence/parser"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/errors"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/parser"
"github.com/onflow/cadence/runtime/stdlib"
"github.com/onflow/cadence/sema"
"github.com/onflow/cadence/stdlib"
"github.com/onflow/flow-emulator/adapters"
"github.com/onflow/flow-emulator/convert"
"github.com/onflow/flow-emulator/emulator"
Expand All @@ -41,6 +44,7 @@ import (
"github.com/onflow/flow-go-sdk/crypto"
sdkTest "github.com/onflow/flow-go-sdk/test"
fvmCrypto "github.com/onflow/flow-go/fvm/crypto"
"github.com/onflow/flow-go/fvm/environment"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"
"github.com/rs/zerolog"
Expand Down Expand Up @@ -100,6 +104,9 @@ type EmulatorBackend struct {
// contracts is a mapping of contract identifiers to their
// deployed account address.
contracts map[string]common.Address

// locationHandler is used for resolving locations
locationHandler sema.LocationHandlerFunc
}

type keyInfo struct {
Expand All @@ -108,11 +115,12 @@ type keyInfo struct {
}

var chain = flow.MonotonicEmulator.Chain()
var chainContracts = systemcontracts.SystemContractsForChain(chain.ChainID())

var commonContracts = emulator.NewCommonContracts(chain)

// TODO: refactor, use chainContracts.All instead
var systemContracts = func() []common.AddressLocation {
chainContracts := systemcontracts.SystemContractsForChain(chain.ChainID())
serviceAddress := chain.ServiceAddress().HexWithPrefix()
contracts := map[string]string{
"FlowServiceAccount": serviceAddress,
Expand All @@ -136,6 +144,7 @@ var systemContracts = func() []common.AddressLocation {
"EVM": serviceAddress,
"FungibleTokenSwitchboard": chainContracts.FungibleToken.Address.HexWithPrefix(),
"Burner": serviceAddress,
"Crypto": serviceAddress,
}

locations := make([]common.AddressLocation, 0)
Expand Down Expand Up @@ -173,21 +182,55 @@ func NewEmulatorBackend(
clock := newSystemClock()
blockchain.SetClock(clock)

sc := systemcontracts.SystemContractsForChain(chain.ChainID())
cryptoContractAddress := common.Address(sc.Crypto.Address)

locationHandler := func(
identifiers []ast.Identifier,
location common.Location,
) ([]sema.ResolvedLocation, error) {
return environment.ResolveLocation(
identifiers,
location,
func(address flow.Address) ([]string, error) {
return accountContractNames(blockchain, address)
},
cryptoContractAddress,
)
}

emulatorBackend := &EmulatorBackend{
blockchain: blockchain,
blockOffset: 0,
accountKeys: map[common.Address]map[string]keyInfo{},
stdlibHandler: stdlibHandler,
logCollection: logCollectionHook,
clock: clock,
contracts: map[string]common.Address{},
accounts: map[common.Address]*stdlib.Account{},
blockchain: blockchain,
blockOffset: 0,
accountKeys: map[common.Address]map[string]keyInfo{},
stdlibHandler: stdlibHandler,
logCollection: logCollectionHook,
clock: clock,
contracts: map[string]common.Address{},
accounts: map[common.Address]*stdlib.Account{},
locationHandler: locationHandler,
}
emulatorBackend.bootstrapAccounts()

return emulatorBackend
}

func accountContractNames(blockchain *emulator.Blockchain, address flow.Address) ([]string, error) {
account, err := blockchain.GetAccount(address)
if err != nil {
return nil, err
}

contractNames := make([]string, 0, len(account.Contracts))

for name := range account.Contracts {
contractNames = append(contractNames, name)
}
sort.Strings(contractNames)

return contractNames, nil
}

func (e *EmulatorBackend) RunScript(
inter *interpreter.Interpreter,
code string,
Expand Down Expand Up @@ -239,6 +282,7 @@ func (e *EmulatorBackend) RunScript(
inter,
interpreter.EmptyLocationRange,
e.stdlibHandler,
e.locationHandler,
result.Value,
expectedType,
)
Expand Down Expand Up @@ -557,6 +601,7 @@ func (e *EmulatorBackend) Events(
inter,
interpreter.EmptyLocationRange,
e.stdlibHandler,
e.locationHandler,
event.Value,
nil,
)
Expand Down
23 changes: 12 additions & 11 deletions test/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ go 1.22

require (
github.com/btcsuite/btcd/chaincfg/chainhash v1.0.2
github.com/onflow/atree v0.8.0-rc.6
github.com/onflow/cadence v1.0.0
github.com/onflow/flow-emulator v1.0.1
github.com/onflow/flow-go v0.38.0-preview.0.0.20241002124048-21cfe5aea5a8
github.com/onflow/flow-go-sdk v1.0.0
github.com/onflow/atree v0.8.0
github.com/onflow/cadence v1.2.1
github.com/onflow/flow-emulator v1.1.0
github.com/onflow/flow-go v0.38.0-preview.0.0.20241022154145-6a254edbec23
github.com/onflow/flow-go-sdk v1.2.2
github.com/rs/zerolog v1.29.0
github.com/stretchr/testify v1.9.0
)
Expand Down Expand Up @@ -134,14 +134,15 @@ require (
github.com/multiformats/go-varint v0.0.7 // indirect
github.com/olekukonko/tablewriter v0.0.5 // indirect
github.com/onflow/crypto v0.25.2 // indirect
github.com/onflow/flow-core-contracts/lib/go/contracts v1.3.1 // indirect
github.com/onflow/flow-core-contracts/lib/go/templates v1.3.1 // indirect
github.com/onflow/flow-ft/lib/go/contracts v1.0.0 // indirect
github.com/onflow/flow-ft/lib/go/templates v1.0.0 // indirect
github.com/onflow/flow-nft/lib/go/contracts v1.2.1 // indirect
github.com/onflow/flow-nft/lib/go/templates v1.2.0 // indirect
github.com/onflow/flow-core-contracts/lib/go/contracts v1.4.0 // indirect
github.com/onflow/flow-core-contracts/lib/go/templates v1.4.0 // indirect
github.com/onflow/flow-ft/lib/go/contracts v1.0.1 // indirect
github.com/onflow/flow-ft/lib/go/templates v1.0.1 // indirect
github.com/onflow/flow-nft/lib/go/contracts v1.2.2 // indirect
github.com/onflow/flow-nft/lib/go/templates v1.2.1 // indirect
github.com/onflow/flow/protobuf/go/flow v0.4.7 // indirect
github.com/onflow/go-ethereum v1.14.7 // indirect
github.com/onflow/nft-storefront/lib/go/contracts v1.0.0 // indirect
github.com/onflow/sdks v0.6.0-preview.1 // indirect
github.com/onflow/wal v1.0.2 // indirect
github.com/pbnjay/memory v0.0.0-20210728143218-7b4eea64cf58 // indirect
Expand Down
Loading

0 comments on commit 77087b2

Please sign in to comment.