From 78780d547e640ca4c982d4078843ef7030c54bcc Mon Sep 17 00:00:00 2001 From: Supun Setunga Date: Mon, 28 Oct 2024 15:45:59 -0700 Subject: [PATCH] Fix tests --- bbq/compiler/compiler_test.go | 60 ++++++++++++++++--------------- bbq/vm/value_array.go | 3 -- tests/ft_test.go | 66 ++++++++++++++++++----------------- 3 files changed, 65 insertions(+), 64 deletions(-) diff --git a/bbq/compiler/compiler_test.go b/bbq/compiler/compiler_test.go index 6938023812..ce4d20c2a4 100644 --- a/bbq/compiler/compiler_test.go +++ b/bbq/compiler/compiler_test.go @@ -61,14 +61,16 @@ func TestCompileRecursionFib(t *testing.T) { byte(opcode.GetLocal), 0, 0, byte(opcode.GetConstant), 0, 1, byte(opcode.IntSubtract), + byte(opcode.Transfer), 0, 0, byte(opcode.GetGlobal), 0, 0, - byte(opcode.Invoke), + byte(opcode.Invoke), 0, 0, // fib(n - 2) byte(opcode.GetLocal), 0, 0, - byte(opcode.GetConstant), 0, 2, + byte(opcode.GetConstant), 0, 0, byte(opcode.IntSubtract), + byte(opcode.Transfer), 0, 0, byte(opcode.GetGlobal), 0, 0, - byte(opcode.Invoke), + byte(opcode.Invoke), 0, 0, // return sum byte(opcode.IntAdd), byte(opcode.ReturnValue), @@ -86,10 +88,6 @@ func TestCompileRecursionFib(t *testing.T) { Data: []byte{0x1}, Kind: constantkind.Int, }, - { - Data: []byte{0x2}, - Kind: constantkind.Int, - }, }, program.Constants, ) @@ -124,39 +122,47 @@ func TestCompileImperativeFib(t *testing.T) { []byte{ // var fib1 = 1 byte(opcode.GetConstant), 0, 0, + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 1, // var fib2 = 1 - byte(opcode.GetConstant), 0, 1, + byte(opcode.GetConstant), 0, 0, + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 2, // var fibonacci = fib1 byte(opcode.GetLocal), 0, 1, + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 3, // var i = 2 - byte(opcode.GetConstant), 0, 2, + byte(opcode.GetConstant), 0, 1, + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 4, // while i < n byte(opcode.GetLocal), 0, 4, byte(opcode.GetLocal), 0, 0, byte(opcode.IntLess), - byte(opcode.JumpIfFalse), 0, 69, + byte(opcode.JumpIfFalse), 0, 93, // fibonacci = fib1 + fib2 byte(opcode.GetLocal), 0, 1, byte(opcode.GetLocal), 0, 2, byte(opcode.IntAdd), + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 3, // fib1 = fib2 byte(opcode.GetLocal), 0, 2, + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 1, // fib2 = fibonacci byte(opcode.GetLocal), 0, 3, + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 2, // i = i + 1 byte(opcode.GetLocal), 0, 4, - byte(opcode.GetConstant), 0, 3, + byte(opcode.GetConstant), 0, 0, byte(opcode.IntAdd), + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 4, // continue loop - byte(opcode.Jump), 0, 24, + byte(opcode.Jump), 0, 36, // return fibonacci byte(opcode.GetLocal), 0, 3, byte(opcode.ReturnValue), @@ -166,10 +172,6 @@ func TestCompileImperativeFib(t *testing.T) { require.Equal(t, []*bbq.Constant{ - { - Data: []byte{0x1}, - Kind: constantkind.Int, - }, { Data: []byte{0x1}, Kind: constantkind.Int, @@ -178,10 +180,6 @@ func TestCompileImperativeFib(t *testing.T) { Data: []byte{0x2}, Kind: constantkind.Int, }, - { - Data: []byte{0x1}, - Kind: constantkind.Int, - }, }, program.Constants, ) @@ -213,24 +211,26 @@ func TestCompileBreak(t *testing.T) { []byte{ // var i = 0 byte(opcode.GetConstant), 0, 0, + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 0, // while true byte(opcode.True), - byte(opcode.JumpIfFalse), 0, 36, + byte(opcode.JumpIfFalse), 0, 42, // if i > 3 byte(opcode.GetLocal), 0, 0, byte(opcode.GetConstant), 0, 1, byte(opcode.IntGreater), - byte(opcode.JumpIfFalse), 0, 23, + byte(opcode.JumpIfFalse), 0, 26, // break - byte(opcode.Jump), 0, 36, + byte(opcode.Jump), 0, 42, // i = i + 1 byte(opcode.GetLocal), 0, 0, byte(opcode.GetConstant), 0, 2, byte(opcode.IntAdd), + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 0, // repeat - byte(opcode.Jump), 0, 6, + byte(opcode.Jump), 0, 9, // return i byte(opcode.GetLocal), 0, 0, byte(opcode.ReturnValue), @@ -284,26 +284,28 @@ func TestCompileContinue(t *testing.T) { []byte{ // var i = 0 byte(opcode.GetConstant), 0, 0, + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 0, // while true byte(opcode.True), - byte(opcode.JumpIfFalse), 0, 39, + byte(opcode.JumpIfFalse), 0, 45, // i = i + 1 byte(opcode.GetLocal), 0, 0, byte(opcode.GetConstant), 0, 1, byte(opcode.IntAdd), + byte(opcode.Transfer), 0, 0, byte(opcode.SetLocal), 0, 0, // if i < 3 byte(opcode.GetLocal), 0, 0, byte(opcode.GetConstant), 0, 2, byte(opcode.IntLess), - byte(opcode.JumpIfFalse), 0, 33, + byte(opcode.JumpIfFalse), 0, 39, // continue - byte(opcode.Jump), 0, 6, + byte(opcode.Jump), 0, 9, // break - byte(opcode.Jump), 0, 39, + byte(opcode.Jump), 0, 45, // repeat - byte(opcode.Jump), 0, 6, + byte(opcode.Jump), 0, 9, // return i byte(opcode.GetLocal), 0, 0, byte(opcode.ReturnValue), diff --git a/bbq/vm/value_array.go b/bbq/vm/value_array.go index 29dfb82422..708ea33171 100644 --- a/bbq/vm/value_array.go +++ b/bbq/vm/value_array.go @@ -24,12 +24,10 @@ import ( "github.com/onflow/cadence/common" "github.com/onflow/cadence/errors" "github.com/onflow/cadence/interpreter" - "github.com/onflow/cadence/sema" ) type ArrayValue struct { Type interpreter.ArrayStaticType - semaType sema.ArrayType array *atree.Array isResourceKinded bool elementSize uint @@ -242,7 +240,6 @@ func (v *ArrayValue) Transfer(config *Config, address atree.Address, remove bool array, ) - res.semaType = v.semaType res.isResourceKinded = v.isResourceKinded res.isDestroyed = v.isDestroyed diff --git a/tests/ft_test.go b/tests/ft_test.go index 4e5c0422ce..2efce854de 100644 --- a/tests/ft_test.go +++ b/tests/ft_test.go @@ -233,9 +233,9 @@ access(all) contract interface FungibleToken { /// createEmptyVault allows any user to create a new Vault that has a zero balance /// - access(all) fun createEmptyVault(): @{FungibleToken.Vault} { + access(all) fun createEmptyVault(vaultType: Type): @{FungibleToken.Vault} { post { - // result.getType() == vaultType: "The returned vault does not match the desired type" + result.getType() == vaultType: "The returned vault does not match the desired type" result.balance == 0.0: "The newly created Vault must have zero balance" } } @@ -387,7 +387,7 @@ access(all) contract FlowToken: FungibleToken { // and store the returned Vault in their storage in order to allow their // account to be able to receive deposits of this token type. // - access(all) fun createEmptyVault(): @FlowToken.Vault { + access(all) fun createEmptyVault(vaultType: Type): @FlowToken.Vault { return <-create Vault(balance: 0.0) } @@ -470,34 +470,35 @@ import FlowToken from 0x1 transaction { - prepare(signer: auth(BorrowValue, IssueStorageCapabilityController, PublishCapability, SaveValue) &Account) { - - var storagePath = /storage/flowTokenVault - - if signer.storage.borrow<&FlowToken.Vault>(from: storagePath) != nil { - return + prepare(signer: auth(Storage, Capabilities) &Account) { + + if signer.storage.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault) == nil { + // Create a new flowToken Vault and put it in storage + var vault <- FlowToken.createEmptyVault(vaultType: Type<@FlowToken.Vault>()) + signer.storage.save(<- vault, to: /storage/flowTokenVault) + + // Create a public capability to the Vault that only exposes + // the deposit function through the Receiver interface + let vaultCap = signer.capabilities.storage.issue<&FlowToken.Vault>( + /storage/flowTokenVault + ) + + signer.capabilities.publish( + vaultCap, + at: /public/flowTokenReceiver + ) + + // Create a public capability to the Vault that only exposes + // the balance field through the Balance interface + let balanceCap = signer.capabilities.storage.issue<&FlowToken.Vault>( + /storage/flowTokenVault + ) + + signer.capabilities.publish( + balanceCap, + at: /public/flowTokenBalance + ) } - - // Create a new flowToken Vault and put it in storage - signer.storage.save(<-FlowToken.createEmptyVault(), to: storagePath) - - // Create a public capability to the Vault that only exposes - // the deposit function through the Receiver interface - let vaultCap = signer.capabilities.storage.issue<&FlowToken.Vault>(storagePath) - - signer.capabilities.publish( - vaultCap, - at: /public/flowTokenReceiver - ) - - // Create a public capability to the Vault that only exposes - // the balance field through the Balance interface - let balanceCap = signer.capabilities.storage.issue<&FlowToken.Vault>(storagePath) - - signer.capabilities.publish( - balanceCap, - at: /public/flowTokenBalance - ) } } ` @@ -509,8 +510,6 @@ import FlowToken from 0x1 transaction(recipient: Address, amount: UFix64) { let tokenAdmin: &FlowToken.Administrator - - /// Reference to the Fungible Token Receiver of the recipient let tokenReceiver: &{FungibleToken.Receiver} prepare(signer: auth(BorrowValue) &Account) { @@ -540,6 +539,8 @@ import FungibleToken from 0x1 import FlowToken from 0x1 transaction(amount: UFix64, to: Address) { + + // The Vault resource that holds the tokens that are being transferred let sentVault: @{FungibleToken.Vault} prepare(signer: auth(BorrowValue) &Account) { @@ -553,6 +554,7 @@ transaction(amount: UFix64, to: Address) { } execute { + // Get a reference to the recipient's Receiver let receiverRef = getAccount(to) .capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)