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

Fix resource dictionary value deferral removal #129

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
187 changes: 170 additions & 17 deletions runtime/deferral_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/tests/utils"
)

func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
Expand Down Expand Up @@ -146,16 +145,18 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
},
}

nextTransactionLocation := newTransactionLocationGenerator()

clearReadsAndWrites()
err := runtime.ExecuteTransaction(deploy, nil, runtimeInterface, utils.TestLocation)
err := runtime.ExecuteTransaction(deploy, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.NotNil(t, accountCode)

assert.Len(t, writes, 1)

clearReadsAndWrites()
err = runtime.ExecuteTransaction(setupTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(setupTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Len(t, writes, 1)
Expand All @@ -176,7 +177,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
`)

clearReadsAndWrites()
err = runtime.ExecuteTransaction(insertTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(insertTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

cStorageKey := []byte("storage\x1fc")
Expand Down Expand Up @@ -215,7 +216,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
clearReadsAndWrites()
loggedMessages = nil

err = runtime.ExecuteTransaction(readTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(readTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Equal(t, []string{"2", "2"}, loggedMessages)
Expand Down Expand Up @@ -255,7 +256,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
clearReadsAndWrites()
loggedMessages = nil

err = runtime.ExecuteTransaction(updateTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(updateTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Equal(t, []string{"3"}, loggedMessages)
Expand Down Expand Up @@ -308,7 +309,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
clearReadsAndWrites()
loggedMessages = nil

err = runtime.ExecuteTransaction(replaceTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(replaceTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Equal(t, []string{"3", "4"}, loggedMessages)
Expand Down Expand Up @@ -361,7 +362,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
clearReadsAndWrites()
loggedMessages = nil

err = runtime.ExecuteTransaction(removeTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(removeTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Equal(t, []string{"4", "nil"}, loggedMessages)
Expand Down Expand Up @@ -399,7 +400,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
clearReadsAndWrites()
loggedMessages = nil

err = runtime.ExecuteTransaction(readTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(readTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Equal(t, []string{"nil", "nil"}, loggedMessages)
Expand Down Expand Up @@ -440,7 +441,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValues(t *testing.T) {
clearReadsAndWrites()
loggedMessages = nil

err = runtime.ExecuteTransaction(destroyTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(destroyTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Equal(t, []string{"1"}, loggedMessages)
Expand Down Expand Up @@ -613,16 +614,18 @@ func TestRuntimeStorageDeferredResourceDictionaryValuesNested(t *testing.T) {
},
}

nextTransactionLocation := newTransactionLocationGenerator()

clearReadsAndWrites()
err := runtime.ExecuteTransaction(deploy, nil, runtimeInterface, utils.TestLocation)
err := runtime.ExecuteTransaction(deploy, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.NotNil(t, accountCode)

assert.Len(t, writes, 1)

clearReadsAndWrites()
err = runtime.ExecuteTransaction(setupTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(setupTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Len(t, writes, 1)
Expand All @@ -645,7 +648,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValuesNested(t *testing.T) {
`)

clearReadsAndWrites()
err = runtime.ExecuteTransaction(insertTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(insertTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

cStorageKey := []byte("storage\x1fc")
Expand Down Expand Up @@ -687,7 +690,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValuesNested(t *testing.T) {
clearReadsAndWrites()
loggedMessages = nil

err = runtime.ExecuteTransaction(readTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(readTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.Equal(t, []string{"2"}, loggedMessages)
Expand Down Expand Up @@ -848,16 +851,18 @@ func TestRuntimeStorageDeferredResourceDictionaryValuesTransfer(t *testing.T) {
},
}

nextTransactionLocation := newTransactionLocationGenerator()

clearReadsAndWrites()
err := runtime.ExecuteTransaction(deploy, nil, runtimeInterface, utils.TestLocation)
err := runtime.ExecuteTransaction(deploy, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

assert.NotNil(t, accountCode)

assert.Len(t, writes, 1)

clearReadsAndWrites()
err = runtime.ExecuteTransaction(setupTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(setupTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

cStorageKey := []byte("storage\x1fc")
Expand Down Expand Up @@ -896,7 +901,7 @@ func TestRuntimeStorageDeferredResourceDictionaryValuesTransfer(t *testing.T) {
clearReadsAndWrites()
loggedMessages = nil

err = runtime.ExecuteTransaction(transferTx, nil, runtimeInterface, utils.TestLocation)
err = runtime.ExecuteTransaction(transferTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

require.Len(t, writes, 7)
Expand All @@ -917,3 +922,151 @@ func TestRuntimeStorageDeferredResourceDictionaryValuesTransfer(t *testing.T) {
assert.NotEmpty(t, indexedWrites[string(signer2[:])][string(bStorageKey2)])
assert.NotEmpty(t, indexedWrites[string(signer2[:])][string(xStorageKey2)])
}

func TestRuntimeStorageDeferredResourceDictionaryValuesRemoval(t *testing.T) {

// Test that `remove` function correctly loads the potentially deferred value

runtime := NewInterpreterRuntime()

contract := []byte(`
pub contract Test {

pub resource NFT {}

pub resource Collection {
pub var ownedNFTs: @{UInt64: NFT}

init () {
self.ownedNFTs <- {UInt64(1): <- create NFT()}
}

pub fun withdraw(withdrawID: UInt64): @NFT {
let token <- self.ownedNFTs.remove(key: withdrawID) ?? panic("missing NFT")
return <-token
}

pub fun deposit(token: @NFT) {
let token <- token as! @NFT
let oldToken <- self.ownedNFTs[UInt64(1)] <- token
destroy oldToken
}

destroy() {
destroy self.ownedNFTs
}
}

pub fun createCollection(): @Collection {
return <-create Collection()
}
}
`)

deployTx := []byte(fmt.Sprintf(
`
transaction {

prepare(signer: AuthAccount) {
signer.setCode(%s)
}
}
`,
ArrayValueFromBytes(contract).String(),
))

setupTx := []byte(`
import Test from 0x1

transaction {

prepare(signer: AuthAccount) {
let collection <- Test.createCollection()

let token <- collection.withdraw(withdrawID: 1)

collection.deposit(token: <-token)

signer.save(<-collection, to: /storage/NFTCollection)
}
}
`)

testTx := []byte(`
import Test from 0x1

transaction {

prepare(signer: AuthAccount) {
let collection <- signer.load<@Test.Collection>(from:/storage/NFTCollection)!
let nft <- collection.withdraw(withdrawID: 1)
destroy nft
destroy collection
}
}
`)

var accountCode []byte
var events []cadence.Event
var loggedMessages []string
var reads []testRead
var writes []testWrite

onRead := func(controller, owner, key, value []byte) {
reads = append(reads, testRead{
controller,
owner,
key,
})
}

onWrite := func(controller, owner, key, value []byte) {
writes = append(writes, testWrite{
controller,
owner,
key,
value,
})
}

clearReadsAndWrites := func() {
writes = nil
reads = nil
}

signer := common.BytesToAddress([]byte{0x1})

runtimeInterface := &testRuntimeInterface{
resolveImport: func(_ Location) (bytes []byte, err error) {
return accountCode, nil
},
storage: newTestStorage(onRead, onWrite),
getSigningAccounts: func() []Address {
return []Address{signer}
},
updateAccountCode: func(address Address, code []byte, checkPermission bool) (err error) {
accountCode = code
return nil
},
emitEvent: func(event cadence.Event) {
events = append(events, event)
},
log: func(message string) {
loggedMessages = append(loggedMessages, message)
},
}

nextTransactionLocation := newTransactionLocationGenerator()

clearReadsAndWrites()
err := runtime.ExecuteTransaction(deployTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

clearReadsAndWrites()
err = runtime.ExecuteTransaction(setupTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)

clearReadsAndWrites()
err = runtime.ExecuteTransaction(testTx, nil, runtimeInterface, nextTransactionLocation())
require.NoError(t, err)
}
Loading