Skip to content

Commit

Permalink
add back test for exporting attachments
Browse files Browse the repository at this point in the history
  • Loading branch information
dsainati1 committed Mar 2, 2023
1 parent 7f83d27 commit 9c7b2b8
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 2 deletions.
94 changes: 93 additions & 1 deletion runtime/attachments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ func TestAccountAttachmentSaveAndLoad(t *testing.T) {
require.Equal(t, logs[0], "3")
}

func TestAccountAttachmentExport(t *testing.T) {
func TestAccountAttachmentExportFailure(t *testing.T) {
t.Parallel()

storage := newTestLedger(nil, nil)
Expand Down Expand Up @@ -242,6 +242,98 @@ func TestAccountAttachmentExport(t *testing.T) {
require.ErrorAs(t, err, &interpreter.InvalidatedResourceError{})
}

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

storage := newTestLedger(nil, nil)
rt := newTestInterpreterRuntimeWithAttachments()

logs := make([]string, 0)
events := make([]string, 0)
accountCodes := map[Location][]byte{}

deployTx := DeploymentTransaction("Test", []byte(`
pub contract Test {
pub resource R {}
pub attachment A for R {}
pub fun makeRWithA(): @R {
return <- attach A() to <-create R()
}
}
`))

script := []byte(`
import Test from 0x1
pub fun main(): &Test.A? {
let r <- Test.makeRWithA()
let authAccount = getAuthAccount(0x1)
authAccount.save(<-r, to: /storage/foo)
let ref = authAccount.borrow<&Test.R>(from: /storage/foo)!
let a = ref[Test.A]
return a
}
`)

runtimeInterface1 := &testRuntimeInterface{
storage: storage,
log: func(message string) {
logs = append(logs, message)
},
emitEvent: func(event cadence.Event) error {
events = append(events, event.String())
return nil
},
resolveLocation: singleIdentifierLocationResolver(t),
getSigningAccounts: func() ([]Address, error) {
return []Address{[8]byte{0, 0, 0, 0, 0, 0, 0, 1}}, nil
},
updateAccountContractCode: func(address Address, name string, code []byte) error {
location := common.AddressLocation{
Address: address,
Name: name,
}
accountCodes[location] = code
return nil
},
getAccountContractCode: func(address Address, name string) (code []byte, err error) {
location := common.AddressLocation{
Address: address,
Name: name,
}
code = accountCodes[location]
return code, nil
},
}

nextTransactionLocation := newTransactionLocationGenerator()
nextScriptLocation := newScriptLocationGenerator()

err := rt.ExecuteTransaction(
Script{
Source: deployTx,
},
Context{
Interface: runtimeInterface1,
Location: nextTransactionLocation(),
},
)
require.NoError(t, err)

v, err := rt.ExecuteScript(
Script{
Source: script,
},
Context{
Interface: runtimeInterface1,
Location: nextScriptLocation(),
},
)
require.NoError(t, err)
require.IsType(t, cadence.Optional{}, v)
require.IsType(t, cadence.Attachment{}, v.(cadence.Optional).Value)
require.Equal(t, "A.0000000000000001.Test.A()", v.(cadence.Optional).Value.String())
}

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

Expand Down
11 changes: 11 additions & 0 deletions runtime/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -4403,6 +4403,17 @@ func (interpreter *Interpreter) checkContainerMutation(
}
}

func (interpreter *Interpreter) checkReferencedResourceNotDestroyed(value Value, locationRange LocationRange) {
resourceKindedValue, ok := value.(ResourceKindedValue)
if !ok || !resourceKindedValue.IsDestroyed() {
return
}

panic(DestroyedResourceError{
LocationRange: locationRange,
})
}

func (interpreter *Interpreter) checkReferencedResourceNotMovedOrDestroyed(
referencedValue Value,
locationRange LocationRange,
Expand Down
2 changes: 1 addition & 1 deletion runtime/interpreter/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -17110,7 +17110,7 @@ func (v *StorageReferenceValue) mustReferencedValue(

self := *referencedValue

interpreter.checkReferencedResourceNotMovedOrDestroyed(self, locationRange)
interpreter.checkReferencedResourceNotDestroyed(self, locationRange)

return self
}
Expand Down

0 comments on commit 9c7b2b8

Please sign in to comment.