Skip to content

Commit

Permalink
Merge branch 'master' into ramtin/fvm-remove-legacy-controller-from-s…
Browse files Browse the repository at this point in the history
…torage
  • Loading branch information
ramtinms authored Jul 15, 2022
2 parents a1fdcb7 + 45b6ede commit 652aac3
Show file tree
Hide file tree
Showing 6 changed files with 161 additions and 143 deletions.
241 changes: 125 additions & 116 deletions fvm/contractFunctionInvocations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,167 +18,176 @@ var deductTransactionFeesInvocationArgumentTypes = []sema.Type{
sema.UInt64Type,
}

// DeductTransactionFeesInvocation prepares a function that calls fee deduction on the service account
func DeductTransactionFeesInvocation(
// InvokeDeductTransactionFeesContract executes the fee deduction contract on
// the service account.
func InvokeDeductTransactionFeesContract(
env Environment,
traceSpan opentracing.Span,
) func(payer flow.Address, inclusionEffort uint64, executionEffort uint64) (cadence.Value, error) {
payer flow.Address,
inclusionEffort uint64,
executionEffort uint64,
) (cadence.Value, error) {

feesAddress := FlowFeesAddress(env.Context().Chain)

return func(payer flow.Address, inclusionEffort uint64, executionEffort uint64) (cadence.Value, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(feesAddress),
Name: systemcontracts.ContractNameFlowFees,
},
systemcontracts.ContractServiceAccountFunction_deductTransactionFee,
[]interpreter.Value{
interpreter.NewUnmeteredAddressValueFromBytes(payer.Bytes()),
interpreter.UFix64Value(inclusionEffort),
interpreter.UFix64Value(executionEffort),
},
deductTransactionFeesInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(feesAddress),
Name: systemcontracts.ContractNameFlowFees,
},
systemcontracts.ContractServiceAccountFunction_deductTransactionFee,
[]interpreter.Value{
interpreter.NewUnmeteredAddressValueFromBytes(payer.Bytes()),
interpreter.UFix64Value(inclusionEffort),
interpreter.UFix64Value(executionEffort),
},
deductTransactionFeesInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}

var setupNewAccountInvocationArgumentTypes = []sema.Type{
sema.AuthAccountType,
sema.AuthAccountType,
}

// SetupNewAccountInvocation prepares a function that calls new account setup on the service account
func SetupNewAccountInvocation(
// InvokeSetupNewAccountContract executes the new account setup contract on
// the service account.
func InvokeSetupNewAccountContract(
env Environment,
traceSpan opentracing.Span,
) func(flowAddress flow.Address, payer common.Address) (cadence.Value, error) {
return func(flowAddress flow.Address, payer common.Address) (cadence.Value, error) {
// uses `FlowServiceAccount.setupNewAccount` from https://github.com/onflow/flow-core-contracts/blob/master/contracts/FlowServiceAccount.cdc
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractServiceAccount,
},
systemcontracts.ContractServiceAccountFunction_setupNewAccount,
[]interpreter.Value{
interpreter.NewAddressValue(env, common.Address(flowAddress)),
interpreter.NewAddressValue(env, payer),
},
setupNewAccountInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
flowAddress flow.Address,
payer common.Address,
) (cadence.Value, error) {

// uses `FlowServiceAccount.setupNewAccount` from https://github.com/onflow/flow-core-contracts/blob/master/contracts/FlowServiceAccount.cdc
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractServiceAccount,
},
systemcontracts.ContractServiceAccountFunction_setupNewAccount,
[]interpreter.Value{
interpreter.NewAddressValue(env, common.Address(flowAddress)),
interpreter.NewAddressValue(env, payer),
},
setupNewAccountInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}

var accountAvailableBalanceInvocationArgumentTypes = []sema.Type{
&sema.AddressType{},
}

// AccountAvailableBalanceInvocation prepares a function that calls get available balance on the storage fees contract
func AccountAvailableBalanceInvocation(
// InvokeAccountAvailableBalanceContract executes the get available balance
// contract on the storage fees contract.
func InvokeAccountAvailableBalanceContract(
env Environment,
traceSpan opentracing.Span,
) func(address common.Address) (cadence.Value, error) {
return func(address common.Address) (cadence.Value, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractStorageFees,
},
systemcontracts.ContractStorageFeesFunction_defaultTokenAvailableBalance,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountAvailableBalanceInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
address common.Address,
) (cadence.Value, error) {

invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractStorageFees,
},
systemcontracts.ContractStorageFeesFunction_defaultTokenAvailableBalance,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountAvailableBalanceInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}

var accountBalanceInvocationArgumentTypes = []sema.Type{
sema.PublicAccountType,
}

// AccountBalanceInvocation prepares a function that calls get available balance on the service account
func AccountBalanceInvocation(
// InvokeAccountBalanceContract executes the get available balance contract
// on the service account.
func InvokeAccountBalanceContract(
env Environment,
traceSpan opentracing.Span,
) func(address common.Address) (cadence.Value, error) {
return func(address common.Address) (cadence.Value, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractServiceAccount},
systemcontracts.ContractServiceAccountFunction_defaultTokenBalance,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountBalanceInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
address common.Address,
) (cadence.Value, error) {

invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractServiceAccount},
systemcontracts.ContractServiceAccountFunction_defaultTokenBalance,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountBalanceInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}

var accountStorageCapacityInvocationArgumentTypes = []sema.Type{
&sema.AddressType{},
}

// AccountStorageCapacityInvocation prepares a function that calls get storage capacity on the storage fees contract
func AccountStorageCapacityInvocation(
// InvokeAccountStorageCapacityContract executes the get storage capacity
// contract on the storage fees contract.
func InvokeAccountStorageCapacityContract(
env Environment,
traceSpan opentracing.Span,
) func(address common.Address) (cadence.Value, error) {
return func(address common.Address) (cadence.Value, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractStorageFees,
},
systemcontracts.ContractStorageFeesFunction_calculateAccountCapacity,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountStorageCapacityInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
address common.Address,
) (cadence.Value, error) {

invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractStorageFees,
},
systemcontracts.ContractStorageFeesFunction_calculateAccountCapacity,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountStorageCapacityInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}

var useContractAuditVoucherInvocationArgumentTypes = []sema.Type{
&sema.AddressType{},
sema.StringType,
}

// UseContractAuditVoucherInvocation prepares a function that can use a contract deployment audit voucher
func UseContractAuditVoucherInvocation(
// InvokeUseContractAuditVoucherContract executes the use a contract
// deployment audit voucher contract.
func InvokeUseContractAuditVoucherContract(
env Environment,
traceSpan opentracing.Span,
) func(address common.Address, code string) (bool, error) {
return func(address common.Address, code string) (bool, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractDeploymentAudits,
},
systemcontracts.ContractDeploymentAuditsFunction_useVoucherForDeploy,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
interpreter.NewUnmeteredStringValue(code),
},
useContractAuditVoucherInvocationArgumentTypes,
env.Context().Logger,
)
resultCdc, err := invoker.Invoke(env, traceSpan)
if err != nil {
return false, err
}
result := resultCdc.(cadence.Bool).ToGoValue().(bool)
return result, nil
address common.Address,
code string) (bool, error) {

invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractDeploymentAudits,
},
systemcontracts.ContractDeploymentAuditsFunction_useVoucherForDeploy,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
interpreter.NewUnmeteredStringValue(code),
},
useContractAuditVoucherInvocationArgumentTypes,
env.Context().Logger,
)
resultCdc, err := invoker.Invoke(env, traceSpan)
if err != nil {
return false, err
}
result := resultCdc.(cadence.Bool).ToGoValue().(bool)
return result, nil
}
17 changes: 9 additions & 8 deletions fvm/scriptEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -288,9 +288,10 @@ func (e *ScriptEnv) GetStorageCapacity(address common.Address) (value uint64, er
return 0, fmt.Errorf("get storage capacity failed: %w", err)
}

accountStorageCapacity := AccountStorageCapacityInvocation(e, e.traceSpan)
result, invokeErr := accountStorageCapacity(address)

result, invokeErr := InvokeAccountStorageCapacityContract(
e,
e.traceSpan,
address)
if invokeErr != nil {
return 0, errors.HandleRuntimeError(invokeErr)
}
Expand All @@ -311,9 +312,7 @@ func (e *ScriptEnv) GetAccountBalance(address common.Address) (value uint64, err
return 0, fmt.Errorf("get account balance failed: %w", err)
}

accountBalance := AccountBalanceInvocation(e, e.traceSpan)
result, invokeErr := accountBalance(address)

result, invokeErr := InvokeAccountBalanceContract(e, e.traceSpan, address)
if invokeErr != nil {
return 0, errors.HandleRuntimeError(invokeErr)
}
Expand All @@ -331,8 +330,10 @@ func (e *ScriptEnv) GetAccountAvailableBalance(address common.Address) (value ui
return 0, fmt.Errorf("get account available balance failed: %w", err)
}

accountAvailableBalance := AccountAvailableBalanceInvocation(e, e.traceSpan)
result, invokeErr := accountAvailableBalance(address)
result, invokeErr := InvokeAccountAvailableBalanceContract(
e,
e.traceSpan,
address)

if invokeErr != nil {
return 0, errors.HandleRuntimeError(invokeErr)
Expand Down
32 changes: 19 additions & 13 deletions fvm/transactionEnv.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,11 @@ func (e *TransactionEnv) GetIsContractDeploymentRestricted() (restricted bool, d
}

func (e *TransactionEnv) useContractAuditVoucher(address runtime.Address, code []byte) (bool, error) {
useVoucher := UseContractAuditVoucherInvocation(e, e.traceSpan)
return useVoucher(address, string(code[:]))
return InvokeUseContractAuditVoucherContract(
e,
e.traceSpan,
address,
string(code[:]))
}

func (e *TransactionEnv) isAuthorizerServiceAccount() bool {
Expand Down Expand Up @@ -443,9 +446,10 @@ func (e *TransactionEnv) GetStorageCapacity(address common.Address) (value uint6
return value, fmt.Errorf("get storage capacity failed: %w", err)
}

accountStorageCapacity := AccountStorageCapacityInvocation(e, e.traceSpan)
result, invokeErr := accountStorageCapacity(address)

result, invokeErr := InvokeAccountStorageCapacityContract(
e,
e.traceSpan,
address)
if invokeErr != nil {
return 0, errors.HandleRuntimeError(invokeErr)
}
Expand All @@ -471,9 +475,7 @@ func (e *TransactionEnv) GetAccountBalance(address common.Address) (value uint64
return value, fmt.Errorf("get account balance failed: %w", err)
}

accountBalance := AccountBalanceInvocation(e, e.traceSpan)
result, invokeErr := accountBalance(address)

result, invokeErr := InvokeAccountBalanceContract(e, e.traceSpan, address)
if invokeErr != nil {
return 0, errors.HandleRuntimeError(invokeErr)
}
Expand All @@ -491,8 +493,10 @@ func (e *TransactionEnv) GetAccountAvailableBalance(address common.Address) (val
return value, fmt.Errorf("get account available balance failed: %w", err)
}

accountAvailableBalance := AccountAvailableBalanceInvocation(e, e.traceSpan)
result, invokeErr := accountAvailableBalance(address)
result, invokeErr := InvokeAccountAvailableBalanceContract(
e,
e.traceSpan,
address)

if invokeErr != nil {
return 0, errors.HandleRuntimeError(invokeErr)
Expand Down Expand Up @@ -957,9 +961,11 @@ func (e *TransactionEnv) CreateAccount(payer runtime.Address) (address runtime.A
}

if e.ctx.ServiceAccountEnabled {
setupNewAccount := SetupNewAccountInvocation(e, e.traceSpan)
_, invokeErr := setupNewAccount(flowAddress, payer)

_, invokeErr := InvokeSetupNewAccountContract(
e,
e.traceSpan,
flowAddress,
payer)
if invokeErr != nil {
return address, errors.HandleRuntimeError(invokeErr)
}
Expand Down
Loading

0 comments on commit 652aac3

Please sign in to comment.