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

Test storage capacity update on FLOW transfer #2193

Merged
merged 6 commits into from
Sep 3, 2022
Merged
Changes from all commits
Commits
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
91 changes: 91 additions & 0 deletions fvm/fvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,97 @@ func TestEnforcingComputationLimit(t *testing.T) {
}
}

func TestStorageCapacity(t *testing.T) {
t.Run("Storage capacity updates on FLOW transfer", newVMTest().
withContextOptions(
fvm.WithTransactionProcessors(fvm.NewTransactionInvoker()),
fvm.WithCadenceLogging(true),
).
withBootstrapProcedureOptions(
fvm.WithStorageMBPerFLOW(10_0000_0000),
fvm.WithAccountCreationFee(fvm.DefaultAccountCreationFee),
).
run(func(
t *testing.T,
vm *fvm.VirtualMachine,
chain flow.Chain,
ctx fvm.Context,
view state.View,
programs *programs.Programs,
) {
service := chain.ServiceAddress()
signer := createAccount(t, vm, chain, ctx, view, programs)
target := createAccount(t, vm, chain, ctx, view, programs)

// Transfer FLOW from service account to test accounts

transferTxBody := transferTokensTx(chain).
AddAuthorizer(service).
AddArgument(jsoncdc.MustEncode(cadence.UFix64(1_000_000))).
AddArgument(jsoncdc.MustEncode(cadence.NewAddress(signer))).
SetProposalKey(service, 0, 0).
SetPayer(service)
tx := fvm.Transaction(transferTxBody, 0)
err := vm.Run(ctx, tx, view, programs)
require.NoError(t, err)
require.NoError(t, tx.Err)

transferTxBody = transferTokensTx(chain).
AddAuthorizer(service).
AddArgument(jsoncdc.MustEncode(cadence.UFix64(1_000_000))).
AddArgument(jsoncdc.MustEncode(cadence.NewAddress(target))).
SetProposalKey(service, 0, 0).
SetPayer(service)
tx = fvm.Transaction(transferTxBody, 0)
err = vm.Run(ctx, tx, view, programs)
require.NoError(t, err)
require.NoError(t, tx.Err)

// Perform test

txBody := flow.NewTransactionBody().
SetScript([]byte(fmt.Sprintf(`
import FungibleToken from 0x%s
import FlowToken from 0x%s

transaction(target: Address) {
prepare(signer: AuthAccount) {
let receiverRef = getAccount(target)
.getCapability(/public/flowTokenReceiver)
.borrow<&{FungibleToken.Receiver}>()
?? panic("Could not borrow receiver reference to the recipient''s Vault")

let vaultRef = signer
.borrow<&{FungibleToken.Provider}>(from: /storage/flowTokenVault)
?? panic("Could not borrow reference to the owner''s Vault!")

var cap0: UInt64 = signer.storageCapacity

receiverRef.deposit(from: <- vaultRef.withdraw(amount: 0.0000001))

var cap1: UInt64 = signer.storageCapacity

log(cap0 - cap1)
}
}`,
fvm.FungibleTokenAddress(chain),
fvm.FlowTokenAddress(chain),
))).
AddArgument(jsoncdc.MustEncode(cadence.NewAddress(target))).
AddAuthorizer(signer)

tx = fvm.Transaction(txBody, 0)

err = vm.Run(ctx, tx, view, programs)
require.NoError(t, err)
require.NoError(t, tx.Err)

require.Len(t, tx.Logs, 1)
assert.Equal(t, tx.Logs[0], "1")
}),
)
}

func TestScriptContractMutationsFailure(t *testing.T) {
t.Parallel()

Expand Down