Skip to content

Commit

Permalink
remove MigrationContractStaging Host path derivation with constant
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Jan 29, 2024
1 parent 1a9479c commit b1024b3
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 28 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,11 @@ transaction(contractName: String, contractCode: String) {
prepare(signer: AuthAccount) {
// Configure Host resource if needed
let hostStoragePath: StoragePath = MigrationContractStaging.deriveHostStoragePath(hostAddress: signer.address)
if signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath) == nil {
signer.save(<-MigrationContractStaging.createHost(), to: hostStoragePath)
if signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath) == nil {
signer.save(<-MigrationContractStaging.createHost(), to: MigrationContractStaging.HostStoragePath)
}
// Assign Host reference
self.host = signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath)!
self.host = signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath)!
}
execute {
Expand Down Expand Up @@ -126,7 +125,7 @@ The basic interface to stage a contract is the same as deploying a contract - na
`stageContract()` again for the same contract will overwrite any existing staged code for that contract.
```cadence
/// 1 - Create a host and save it in your contract-hosting account at the path derived from deriveHostStoragePath().
/// 1 - Create a host and save it in your contract-hosting account at MigrationContractStaging.HostStoragePath
access(all) fun createHost(): @Host
/// 2 - Call stageContract() with the host reference and contract name and contract code you wish to stage.
access(all) fun stageContract(host: &Host, name: String, code: String)
Expand Down Expand Up @@ -181,7 +180,7 @@ access(all) resource Capsule {
}
```
To support ongoing staging progress across the network, the single `StagingStatusUpdated` event is emitted any time a
To support monitoring staging progress across the network, the single `StagingStatusUpdated` event is emitted any time a
contract is staged (`status == true`), staged code is replaced (`status == false`), or a contract is unstaged (`status
== nil`).
Expand Down
22 changes: 6 additions & 16 deletions contracts/MigrationContractStaging.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@
///
access(all) contract MigrationContractStaging {

// Path derivation constants
// Path constants
//
access(self) let delimiter: String
access(self) let hostPathPrefix: String
access(self) let capsulePathPrefix: String
access(all) let HostStoragePath: StoragePath
/// Maps contract addresses to an array of staged contract names
access(self) let stagedContracts: {Address: [String]}

Expand All @@ -37,7 +37,7 @@ access(all) contract MigrationContractStaging {

/* --- Staging Methods --- */

/// 1 - Create a host and save it in your contract-hosting account at the path derived from deriveHostStoragePath()
/// 1 - Create a host and save it in your contract-hosting account at MigrationContractStaging.HostStoragePath
///
/// Creates a Host serving as identification for the contract account. Reference to this resource identifies the
/// calling address so it must be saved in account storage before being used to stage a contract update.
Expand Down Expand Up @@ -147,16 +147,6 @@ access(all) contract MigrationContractStaging {
return stagedCode
}

/// Returns a StoragePath to store the Host of the form:
/// /storage/self.hostPathPrefix_ADDRESS
access(all) fun deriveHostStoragePath(hostAddress: Address): StoragePath {
return StoragePath(
identifier: self.hostPathPrefix
.concat(self.delimiter)
.concat(hostAddress.toString())
) ?? panic("Could not derive Host StoragePath for given address")
}

/// Returns a StoragePath to store the Capsule of the form:
/// /storage/self.capsulePathPrefix_ADDRESS_NAME
access(all) fun deriveCapsuleStoragePath(contractAddress: Address, contractName: String): StoragePath {
Expand Down Expand Up @@ -325,9 +315,9 @@ access(all) contract MigrationContractStaging {

init() {
self.delimiter = "_"
self.hostPathPrefix = "MigrationContractStagingHost".concat(self.delimiter)
.concat(self.delimiter)
.concat(self.account.address.toString())
self.HostStoragePath = StoragePath(
identifier: "MigrationContractStagingHost".concat(self.delimiter).concat(self.account.address.toString())
) ?? panic("Could not derive Host StoragePath")
self.capsulePathPrefix = "MigrationContractStagingCapsule"
.concat(self.delimiter)
.concat(self.account.address.toString())
Expand Down
7 changes: 3 additions & 4 deletions transactions/migration-contract-staging/stage_contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ transaction(contractName: String, contractCode: String) {

prepare(signer: AuthAccount) {
// Configure Host resource if needed
let hostStoragePath: StoragePath = MigrationContractStaging.deriveHostStoragePath(hostAddress: signer.address)
if signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath) == nil {
signer.save(<-MigrationContractStaging.createHost(), to: hostStoragePath)
if signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath) == nil {
signer.save(<-MigrationContractStaging.createHost(), to: MigrationContractStaging.HostStoragePath)
}
// Assign Host reference
self.host = signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath)!
self.host = signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath)!
}

execute {
Expand Down
3 changes: 1 addition & 2 deletions transactions/migration-contract-staging/unstage_contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ transaction(contractName: String) {

prepare(signer: AuthAccount) {
// Assign Host reference
let hostStoragePath: StoragePath = MigrationContractStaging.deriveHostStoragePath(hostAddress: signer.address)
self.host = signer.borrow<&MigrationContractStaging.Host>(from: hostStoragePath)
self.host = signer.borrow<&MigrationContractStaging.Host>(from: MigrationContractStaging.HostStoragePath)
?? panic("Host was not found in storage")
}

Expand Down

0 comments on commit b1024b3

Please sign in to comment.