cadence icon indicating copy to clipboard operation
cadence copied to clipboard

Throw error if you link to a path already linked

Open bjartek opened this issue 3 years ago • 6 comments

Currently if you link storage to a path and the target is already linked it will overwrite the old link.

I think it should throw an error in this case. It is very easy to overwrite something by accident. Especially if you can create paths dynamically.

suggested solution

throw error if path linked or create a new method that does this

bjartek avatar Nov 19 '21 06:11 bjartek

Yes. A simple solution would be to require the user to simply unlink the Capability and then allow them to link afterwards. Overwriting without warning is an easy accident to happen.

jacob-tucker avatar Nov 19 '21 07:11 jacob-tucker

+1

brianross93 avatar Nov 19 '21 07:11 brianross93

Hi @bjartek, I had a quick look at this and have a question to help me clarify the problem.

Does the below example correctly reflect the issue, or have I misunderstood problematic case?

let foo = Foo()
account.save(foo, to: /storage/foo)

let bar = Bar()
account.save(bar, to: /storage/bar)

// Link

account.link<&Foo>(/public/foo, target: /storage/foo)

// Link another storage value to the same path in account.
account.link<&Bar>(/public/foo, target: /storage/bar)

SupunS avatar Feb 15 '22 16:02 SupunS

That explains it fully. The last line should throw an error IMHO. If you do not unlink it first.

bjartek avatar Feb 15 '22 16:02 bjartek

Excellent! Thanks for clarifying.

Currently the second link doesn't overwrite, but rather returns with a nil. After the last line, if I then try to get the value at the account path /public/foo, it returns the first value foo

account.getCapability<&Bar>(/public/foo)   // returns a capability to 'foo'

Added some test-cases to verify the current behaviour: https://github.com/onflow/cadence/pull/1416

I guess the suggestion is to make it (the failure at second linking) a bit more explicit, by panicing the runtime, rather than returning a nil?

SupunS avatar Feb 15 '22 17:02 SupunS

Yes

bjartek avatar Feb 15 '22 17:02 bjartek

Might not be needed if the new Capability Controllers API is approved

j1010001 avatar Dec 02 '22 19:12 j1010001

The new Capability Controller API has been released

turbolent avatar Jun 12 '23 22:06 turbolent