Skip to content

Commit

Permalink
Merge pull request #6571 from onflow/bastian/move-cadence-crypto-cont…
Browse files Browse the repository at this point in the history
…ract-onchain

Update to Cadence v1.2.1, Move Cadence `Crypto` contract on-chain
  • Loading branch information
turbolent authored Oct 22, 2024
2 parents 14c5eee + 27f56e0 commit 6a254ed
Show file tree
Hide file tree
Showing 48 changed files with 438 additions and 279 deletions.
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)

// 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

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

0 comments on commit 6a254ed

Please sign in to comment.