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

Refactor EVM.BridgeRouter from interface to resource & impl #61

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
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
30 changes: 0 additions & 30 deletions cadence/contracts/bridge/FlowEVMBridgeAccessor.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -141,36 +141,6 @@ contract FlowEVMBridgeAccessor {
protectedTransferCall: callback
)
}

/// Returns a BridgeRouter resource so a Capability on this BridgeAccessor can be stored in the BridgeRouter
///
access(EVM.Bridge) fun createBridgeRouter(): @BridgeRouter {
return <-create BridgeRouter()
}
}

/// BridgeRouter implementation used by the EVM contract to capture a BridgeAccessor Capability and route bridge
/// calls from COA resources to the FlowEVMBridge contract
///
access(all) resource BridgeRouter : EVM.BridgeRouter {
/// Capability to the BridgeAccessor resource, initialized to nil
access(self) var bridgeAccessorCap: Capability<auth(EVM.Bridge) &{EVM.BridgeAccessor}>?

init() {
self.bridgeAccessorCap = nil
}

/// Returns an EVM.Bridge entitled reference to the underlying BridgeAccessor resource
///
access(EVM.Bridge) view fun borrowBridgeAccessor(): auth(EVM.Bridge) &{EVM.BridgeAccessor} {
let cap = self.bridgeAccessorCap ?? panic("BridgeAccessor Capabaility is not yet set")
return cap.borrow() ?? panic("Problem retrieving BridgeAccessor reference")
}

/// Sets the BridgeAccessor Capability in the BridgeRouter
access(EVM.Bridge) fun setBridgeAccessor(_ accessorCap: Capability<auth(EVM.Bridge) &{EVM.BridgeAccessor}>) {
self.bridgeAccessorCap = accessorCap
}
}

init(publishToEVMAccount: Address) {
Expand Down
41 changes: 29 additions & 12 deletions cadence/contracts/standards/EVM.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -609,33 +609,50 @@ contract EVM {
): @{FungibleToken.Vault}
}

/// Interface which captures a Capability to the bridge Accessor, saving it within the BridgeRouter resource
access(all)
resource interface BridgeRouter {
/// BridgeRouter captures a BridgeAccessor Capability and routes bridge calls from COAs to the BridgeAccessor
access(all) resource BridgeRouter {
/// Capability to the BridgeAccessor resource, initialized to nil
access(self) var bridgeAccessorCap: Capability<auth(Bridge) &{BridgeAccessor}>?

init() {
self.bridgeAccessorCap = nil
}

/// Returns a reference to the BridgeAccessor designated for internal bridge requests
access(Bridge) view fun borrowBridgeAccessor(): auth(Bridge) &{BridgeAccessor}
/// Returns a Bridge entitled reference to the underlying BridgeAccessor resource
access(Bridge) view fun borrowBridgeAccessor(): auth(Bridge) &{BridgeAccessor} {
let cap = self.bridgeAccessorCap ?? panic("BridgeAccessor Capabaility is not yet set")
return cap.borrow() ?? panic("Problem retrieving BridgeAccessor reference")
}

/// Sets the BridgeAccessor Capability in the BridgeRouter
access(Bridge) fun setBridgeAccessor(_ accessor: Capability<auth(Bridge) &{BridgeAccessor}>) {
/// Sets the BridgeAccessor Capability
access(Bridge) fun setBridgeAccessor(_ accessorCap: Capability<auth(Bridge) &{BridgeAccessor}>) {
pre {
accessor.check(): "Invalid BridgeAccessor Capability provided"
accessorCap.check(): "Invalid BridgeAccessor Capability provided"
emit BridgeAccessorUpdated(
routerType: self.getType(),
routerUUID: self.uuid,
routerAddress: self.owner?.address ?? panic("Router must have an owner to be identified"),
accessorType: accessor.borrow()!.getType(),
accessorUUID: accessor.borrow()!.uuid,
accessorAddress: accessor.address
accessorType: accessorCap.borrow()!.getType(),
accessorUUID: accessorCap.borrow()!.uuid,
accessorAddress: accessorCap.address
)
}
self.bridgeAccessorCap = accessorCap
}
}

/// Initializes the BridgeRouter resource. Can only be executed if there is not already a BridgeRouter resource.
access(all) fun initBridgeRouter() {
pre {
self.account.storage.type(at: /storage/evmBridgeRouter) == nil: "BridgeRouter has already been initialized"
}
self.account.storage.save(<-create BridgeRouter(), to: /storage/evmBridgeRouter)
}

/// Returns a reference to the BridgeAccessor designated for internal bridge requests
access(self)
view fun borrowBridgeAccessor(): auth(Bridge) &{BridgeAccessor} {
return self.account.storage.borrow<auth(Bridge) &{BridgeRouter}>(from: /storage/evmBridgeRouter)
return self.account.storage.borrow<auth(Bridge) &BridgeRouter>(from: /storage/evmBridgeRouter)
?.borrowBridgeAccessor()
?? panic("Could not borrow reference to the EVM bridge")
}
Expand Down
2 changes: 1 addition & 1 deletion cadence/tests/test_helpers.cdc

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import "FlowEVMBridgeAccessor"
transaction(name: String, provider: Address) {

let accessorCap: Capability<auth(EVM.Bridge) &FlowEVMBridgeAccessor.BridgeAccessor>
let routerRef: auth(EVM.Bridge) &{EVM.BridgeRouter}
let routerRef: auth(EVM.Bridge) &EVM.BridgeRouter

prepare(signer: auth(ClaimInboxCapability, Storage) &Account) {
let routerStoragePath = /storage/evmBridgeRouter
Expand All @@ -31,20 +31,12 @@ transaction(name: String, provider: Address) {
// Ensure the Capability is valid and nothing is stored where the BridgeRouter should be stored
assert(self.accessorCap.check() == true, message: "Invalid BridgeAccessor Capability")

// If a BridgeRouter implementation already exists from previous bridge integration, load and destroy it
if let existingRouter = signer.storage.borrow<auth(EVM.Bridge) &{EVM.BridgeRouter}>(from: routerStoragePath) {
destroy <-signer.storage.load<@AnyResource>(from: routerStoragePath)
if signer.storage.type(at: routerStoragePath) == nil {
EVM.initBridgeRouter()
}

// Ensure there is nothing in storage where the new BridgeRouter should be stored
assert(signer.storage.type(at: routerStoragePath) == nil, message: "Unexpected object found in storage")

// Create and save the BridgeRouter implementation for the current bridge integration
let router <-self.accessorCap.borrow()!.createBridgeRouter()
signer.storage.save(<-router, to: routerStoragePath)

// Borrow the router from storage and set the BridgeAccessor Capability
self.routerRef = signer.storage.borrow<auth(EVM.Bridge) &{EVM.BridgeRouter}>(from: routerStoragePath)
self.routerRef = signer.storage.borrow<auth(EVM.Bridge) &EVM.BridgeRouter>(from: routerStoragePath)
?? panic("BridgeRouter not found in storage")
}

Expand Down
14 changes: 14 additions & 0 deletions cadence/transactions/bridge/admin/evm/destroy_resource.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
/// Destroys the resource at the given storage path.
///
transaction(resourceStoragePath: StoragePath) {

let r: @AnyResource

prepare(signer: auth(LoadValue) &Account) {
self.r <- signer.storage.load<@AnyResource>(from: resourceStoragePath)
}

execute {
destroy self.r
}
}
Loading