Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to Cadence v1.2.1, Move Cadence Crypto contract on-chain #6571

Merged
merged 27 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
0d2ad75
update to core contracts with Crypto contract
turbolent Oct 17, 2024
ba5de04
deploy Crypto contract when bootstrapping
turbolent Oct 17, 2024
1f8a42d
update core contract templates
turbolent Oct 17, 2024
2d0bc07
add test case for Crypto contract
turbolent Oct 17, 2024
f027093
update Cadence
turbolent Oct 17, 2024
72e392e
update Cadence to version that support loading contract value from id…
turbolent Oct 17, 2024
dd533f6
translate Crypto identifier location to chain-specific Crypto contract
turbolent Oct 17, 2024
2eb3ce4
Merge branch 'auto-update-onflow-cadence-v1.1.0' into bastian/move-ca…
turbolent Oct 18, 2024
85013e7
update Cadence
turbolent Oct 18, 2024
613e7e7
Merge branch 'master' into bastian/move-cadence-crypto-contract-onchain
turbolent Oct 18, 2024
051202e
use ResolveLocation to rewrite import of Crypto contract to chain-spe…
turbolent Oct 18, 2024
eb7fa2e
pass crypto contract address
turbolent Oct 18, 2024
7740564
clean up imports
turbolent Oct 18, 2024
0abd8be
improve import aliases
turbolent Oct 21, 2024
389cc0e
add test for importing Crypto contract from address
turbolent Oct 21, 2024
6ce3c33
update to flow-core-contracts v1.4.0
turbolent Oct 21, 2024
86ee7b4
lint
turbolent Oct 21, 2024
faf4c66
Merge branch 'master' into bastian/move-cadence-crypto-contract-onchain
turbolent Oct 21, 2024
7e34aa7
adjust new tests
turbolent Oct 21, 2024
d2c051d
adjust state commitments
turbolent Oct 21, 2024
5277537
Merge branch 'master' into bastian/move-cadence-crypto-contract-onchain
turbolent Oct 21, 2024
606746b
update to Cadence v1.2.0
turbolent Oct 21, 2024
af9cd6e
update to Cadence v1.2.1
turbolent Oct 21, 2024
4cb8f01
update to WIP Emulator
turbolent Oct 21, 2024
c61e4aa
add Crypto contract to slice of all system contracts
turbolent Oct 21, 2024
1f2f5e3
Merge branch 'master' into bastian/move-cadence-crypto-contract-onchain
turbolent Oct 21, 2024
27f56e0
update to Go SDK v1.2.2
turbolent Oct 22, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/bootstrap/utils/key_generation.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

sdk "github.com/onflow/flow-go-sdk"
sdkcrypto "github.com/onflow/flow-go-sdk/crypto"

"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/bootstrap"
model "github.com/onflow/flow-go/model/bootstrap"
Expand Down
1 change: 1 addition & 0 deletions cmd/collection/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

client "github.com/onflow/flow-go-sdk/access/grpc"
sdkcrypto "github.com/onflow/flow-go-sdk/crypto"

"github.com/onflow/flow-go/admin/commands"
collectionCommands "github.com/onflow/flow-go/admin/commands/collection"
storageCommands "github.com/onflow/flow-go/admin/commands/storage"
Expand Down
1 change: 1 addition & 0 deletions cmd/consensus/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (

client "github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go-sdk/crypto"

"github.com/onflow/flow-go/cmd"
"github.com/onflow/flow-go/cmd/util/cmd/common"
"github.com/onflow/flow-go/consensus"
Expand Down
3 changes: 2 additions & 1 deletion cmd/util/cmd/common/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"github.com/rs/zerolog"
"github.com/sethvargo/go-retry"

"github.com/onflow/flow-go-sdk/access/grpc"

"github.com/onflow/flow-go/utils/logging"

"github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go/model/flow"
"github.com/onflow/flow-go/state/protocol"
"github.com/onflow/flow-go/state/protocol/inmem"
Expand Down
1 change: 0 additions & 1 deletion cmd/util/ledger/migrations/account_based_migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package migrations
import (
"context"
"fmt"

"testing"

"github.com/onflow/cadence/common"
Expand Down
4 changes: 4 additions & 0 deletions cmd/util/ledger/migrations/migrator_runtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/onflow/flow-go/fvm/evm"
evmStdlib "github.com/onflow/flow-go/fvm/evm/stdlib"
"github.com/onflow/flow-go/fvm/storage/state"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"
)

Expand Down Expand Up @@ -111,8 +112,11 @@ func (c InterpreterMigrationRuntimeConfig) NewRuntimeInterface(
}
}

sc := systemcontracts.SystemContractsForChain(chainID)

return util.NewMigrationRuntimeInterface(
chainID,
common.Address(sc.Crypto.Address),
getCodeFunc,
getContractNames,
getOrLoadProgram,
Expand Down
68 changes: 9 additions & 59 deletions cmd/util/ledger/util/migration_runtime_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type GerOrLoadProgramListenerFunc func(
type MigrationRuntimeInterface struct {
runtime.EmptyRuntimeInterface
chainID flow.ChainID
CryptoContractAddress common.Address
GetContractCodeFunc GetContractCodeFunc
GetContractNamesFunc GetContractNamesFunc
GetOrLoadProgramFunc GetOrLoadProgramFunc
Expand All @@ -48,13 +49,15 @@ var _ runtime.Interface = &MigrationRuntimeInterface{}

func NewMigrationRuntimeInterface(
chainID flow.ChainID,
cryptoContractAddress common.Address,
getCodeFunc GetContractCodeFunc,
getContractNamesFunc GetContractNamesFunc,
getOrLoadProgramFunc GetOrLoadProgramFunc,
getOrLoadProgramListenerFunc GerOrLoadProgramListenerFunc,
) *MigrationRuntimeInterface {
return &MigrationRuntimeInterface{
chainID: chainID,
CryptoContractAddress: cryptoContractAddress,
GetContractCodeFunc: getCodeFunc,
GetContractNamesFunc: getContractNamesFunc,
GetOrLoadProgramFunc: getOrLoadProgramFunc,
Expand All @@ -67,65 +70,12 @@ func (m *MigrationRuntimeInterface) ResolveLocation(
location runtime.Location,
) ([]runtime.ResolvedLocation, error) {

addressLocation, isAddress := location.(common.AddressLocation)
turbolent marked this conversation as resolved.
Show resolved Hide resolved

// if the location is not an address location, e.g. an identifier location (`import Crypto`),
// then return a single resolved location which declares all identifiers.
if !isAddress {
return []runtime.ResolvedLocation{
{
Location: location,
Identifiers: identifiers,
},
}, nil
}

// if the location is an address,
// and no specific identifiers where requested in the import statement,
// then fetch all identifiers at this address
if len(identifiers) == 0 {
address := flow.Address(addressLocation.Address)

getContractNames := m.GetContractNamesFunc
if getContractNames == nil {
return nil, errors.New("GetContractNamesFunc missing")
}

contractNames, err := getContractNames(address)
if err != nil {
return nil, fmt.Errorf("ResolveLocation failed: %w", err)
}

// if there are no contractNames deployed,
// then return no resolved locations
if len(contractNames) == 0 {
return nil, nil
}

identifiers = make([]runtime.Identifier, len(contractNames))

for i := range identifiers {
identifiers[i] = runtime.Identifier{
Identifier: contractNames[i],
}
}
}

// return one resolved location per identifier.
// each resolved location is an address contract location
resolvedLocations := make([]runtime.ResolvedLocation, len(identifiers))
for i := range resolvedLocations {
identifier := identifiers[i]
resolvedLocations[i] = runtime.ResolvedLocation{
Location: common.AddressLocation{
Address: addressLocation.Address,
Name: identifier.Identifier,
},
Identifiers: []runtime.Identifier{identifier},
}
}

return resolvedLocations, nil
return environment.ResolveLocation(
identifiers,
location,
m.GetContractNamesFunc,
m.CryptoContractAddress,
)
}

func (m *MigrationRuntimeInterface) GetCode(location runtime.Location) ([]byte, error) {
Expand Down
3 changes: 1 addition & 2 deletions consensus/hotstuff/verification/common.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package verification

import (
"encoding/binary"
"fmt"

"github.com/onflow/crypto"
"github.com/onflow/crypto/hash"

"github.com/onflow/flow-go/consensus/hotstuff/model"
"github.com/onflow/flow-go/model/flow"

"encoding/binary"
)

// MakeVoteMessage generates the message we have to sign in order to be able
Expand Down
2 changes: 1 addition & 1 deletion engine/execution/state/bootstrap/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestBootstrapLedger(t *testing.T) {
}

func TestBootstrapLedger_ZeroTokenSupply(t *testing.T) {
expectedStateCommitmentBytes, _ := hex.DecodeString("edc0ffd7e797e2383bc68b694645e6b62b0d996498bcff9b492bf4d426798ec5")
expectedStateCommitmentBytes, _ := hex.DecodeString("543fa7112081094b66871692c5a7784f40a9e5cdb9cfda10d1d9b81653966409")
expectedStateCommitment, err := flow.ToStateCommitment(expectedStateCommitmentBytes)
require.NoError(t, err)

Expand Down
17 changes: 17 additions & 0 deletions fvm/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ func (b *bootstrapExecutor) Execute() error {

b.deployViewResolver(service, &env)
b.deployBurner(service, &env)
b.deployCrypto(service, &env)

err = expectAccounts(1)
if err != nil {
Expand Down Expand Up @@ -523,6 +524,22 @@ func (b *bootstrapExecutor) deployBurner(deployTo flow.Address, env *templates.E
panicOnMetaInvokeErrf("failed to deploy burner contract: %s", txError, err)
}

func (b *bootstrapExecutor) deployCrypto(deployTo flow.Address, env *templates.Environment) {
contract := contracts.Crypto()

txError, err := b.invokeMetaTransaction(
b.ctx,
Transaction(
blueprints.DeployContractTransaction(
deployTo,
contract,
"Crypto"),
0),
)
env.CryptoAddress = deployTo.String()
panicOnMetaInvokeErrf("failed to deploy crypto contract: %s", txError, err)
}

func (b *bootstrapExecutor) deployMetadataViews(fungibleToken, nonFungibleToken flow.Address, env *templates.Environment) {

mvContract := contracts.MetadataViews(*env)
Expand Down
3 changes: 1 addition & 2 deletions fvm/crypto/hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ package crypto_test

import (
"crypto/rand"
"testing"

"crypto/sha256"
"crypto/sha512"
"testing"

"github.com/onflow/crypto/hash"
"github.com/stretchr/testify/assert"
Expand Down
52 changes: 42 additions & 10 deletions fvm/environment/contract_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/onflow/cadence/ast"
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/runtime"
"github.com/onflow/cadence/stdlib"

"github.com/onflow/flow-go/fvm/errors"
"github.com/onflow/flow-go/fvm/tracing"
Expand All @@ -15,21 +16,23 @@ import (

// ContractReader provide read access to contracts.
type ContractReader struct {
tracer tracing.TracerSpan
meter Meter

accounts Accounts
tracer tracing.TracerSpan
meter Meter
accounts Accounts
cryptoContractAddress common.Address
}

func NewContractReader(
tracer tracing.TracerSpan,
meter Meter,
accounts Accounts,
cryptoContractAddress common.Address,
) *ContractReader {
return &ContractReader{
tracer: tracer,
meter: meter,
accounts: accounts,
tracer: tracer,
meter: meter,
accounts: accounts,
cryptoContractAddress: cryptoContractAddress,
}
}

Expand Down Expand Up @@ -69,12 +72,37 @@ func (reader *ContractReader) ResolveLocation(
return nil, fmt.Errorf("resolve location failed: %w", err)
}

return ResolveLocation(
identifiers,
location,
reader.accounts.GetContractNames,
reader.cryptoContractAddress,
)
}

func ResolveLocation(
identifiers []ast.Identifier,
location common.Location,
getContractNames func(flow.Address) ([]string, error),
cryptoContractAddress common.Address,
) ([]runtime.ResolvedLocation, error) {

addressLocation, isAddress := location.(common.AddressLocation)

// if the location is not an address location, e.g. an identifier location
// (`import Crypto`), then return a single resolved location which declares
// all identifiers.
// then return a single resolved location which declares all identifiers.
if !isAddress {

// if the location is the Crypto contract,
// translate it to the address of the Crypto contract on the chain

turbolent marked this conversation as resolved.
Show resolved Hide resolved
if location == stdlib.CryptoContractLocation {
location = common.AddressLocation{
Address: cryptoContractAddress,
Name: string(stdlib.CryptoContractLocation),
}
}

return []runtime.ResolvedLocation{
{
Location: location,
Expand All @@ -87,9 +115,13 @@ func (reader *ContractReader) ResolveLocation(
// and no specific identifiers where requested in the import statement,
// then fetch all identifiers at this address
if len(identifiers) == 0 {
if getContractNames == nil {
return nil, fmt.Errorf("no identifiers provided")
}

address := flow.ConvertAddress(addressLocation.Address)

contractNames, err := reader.accounts.GetContractNames(address)
contractNames, err := getContractNames(address)
if err != nil {
return nil, fmt.Errorf("resolving location failed: %w", err)
}
Expand Down
7 changes: 6 additions & 1 deletion fvm/environment/facade_env.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/onflow/flow-go/fvm/storage"
"github.com/onflow/flow-go/fvm/storage/snapshot"
"github.com/onflow/flow-go/fvm/storage/state"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/fvm/tracing"
)

Expand Down Expand Up @@ -63,12 +64,15 @@ func newFacadeEnvironment(
accounts := NewAccounts(txnState)
logger := NewProgramLogger(tracer, params.ProgramLoggerParams)
runtime := NewRuntime(params.RuntimeParams)
chain := params.Chain
systemContracts := NewSystemContracts(
params.Chain,
chain,
tracer,
logger,
runtime)

sc := systemcontracts.SystemContractsForChain(chain.ChainID())

env := &facadeEnvironment{
Runtime: runtime,

Expand Down Expand Up @@ -130,6 +134,7 @@ func newFacadeEnvironment(
tracer,
meter,
accounts,
common.Address(sc.Crypto.Address),
),
ContractUpdater: NoContractUpdater{},
Programs: NewPrograms(
Expand Down
Loading
Loading