-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add integration tests for
Upgradable
(#79)
* Add integration tests * Add Deserialize to derived traits * Remove unit tests * Remove dead util code for unit tests All plugins are now tested in integration tests. * Remove outdated paragraph from README
- Loading branch information
Showing
12 changed files
with
501 additions
and
101 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
use near_sdk::serde_json::json; | ||
use near_sdk::CryptoHash; | ||
use near_sdk::Duration; | ||
use workspaces::result::ExecutionFinalResult; | ||
use workspaces::{Account, Contract}; | ||
|
||
/// Wrapper for a contract that derives `Upgradable`. It allows implementing helpers for calling | ||
/// contract methods provided by `Upgradable`. | ||
pub struct UpgradableContract { | ||
contract: Contract, | ||
} | ||
|
||
impl UpgradableContract { | ||
pub fn new(contract: Contract) -> Self { | ||
Self { contract } | ||
} | ||
|
||
pub fn contract(&self) -> &Contract { | ||
&self.contract | ||
} | ||
|
||
pub async fn up_stage_code( | ||
&self, | ||
caller: &Account, | ||
code: Vec<u8>, | ||
) -> workspaces::Result<ExecutionFinalResult> { | ||
caller | ||
.call(self.contract.id(), "up_stage_code") | ||
.args_borsh(code) | ||
.max_gas() | ||
.transact() | ||
.await | ||
} | ||
|
||
pub async fn up_staged_code(&self, caller: &Account) -> anyhow::Result<Option<Vec<u8>>> { | ||
let res = caller | ||
.call(self.contract.id(), "up_staged_code") | ||
.max_gas() | ||
.transact() | ||
.await?; | ||
Ok(res.borsh::<Option<Vec<u8>>>()?) | ||
} | ||
|
||
pub async fn up_staged_code_hash( | ||
&self, | ||
caller: &Account, | ||
) -> anyhow::Result<Option<CryptoHash>> { | ||
let res = caller | ||
.call(self.contract.id(), "up_staged_code_hash") | ||
.max_gas() | ||
.transact() | ||
.await?; | ||
Ok(res.json::<Option<CryptoHash>>()?) | ||
} | ||
|
||
/// The `Promise` returned by trait method `up_deploy_code` is resolved in the `workspaces` | ||
/// transaction. | ||
pub async fn up_deploy_code( | ||
&self, | ||
caller: &Account, | ||
) -> workspaces::Result<ExecutionFinalResult> { | ||
caller | ||
.call(self.contract.id(), "up_deploy_code") | ||
.max_gas() | ||
.transact() | ||
.await | ||
} | ||
|
||
pub async fn up_init_staging_duration( | ||
&self, | ||
caller: &Account, | ||
staging_duration: Duration, | ||
) -> workspaces::Result<ExecutionFinalResult> { | ||
caller | ||
.call(self.contract.id(), "up_init_staging_duration") | ||
.args_json(json!({ "staging_duration": staging_duration })) | ||
.max_gas() | ||
.transact() | ||
.await | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "upgradable" | ||
version = "0.0.0" | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
near-plugins = { path = "../../../../near-plugins" } | ||
near-sdk = "4.1.0" | ||
|
||
[profile.release] | ||
codegen-units = 1 | ||
opt-level = "z" | ||
lto = true | ||
debug = false | ||
panic = "abort" | ||
overflow-checks = true | ||
|
||
[workspace] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
build: | ||
cargo build --target wasm32-unknown-unknown --release | ||
|
||
# Helpful for debugging. Requires `cargo-expand`. | ||
expand: | ||
cargo expand > expanded.rs | ||
|
||
.PHONY: build expand |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[toolchain] | ||
channel = "1.66.1" | ||
components = ["clippy", "rustfmt"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
use near_plugins::{Ownable, Upgradable}; | ||
use near_sdk::borsh::{self, BorshDeserialize, BorshSerialize}; | ||
use near_sdk::{near_bindgen, AccountId, PanicOnDefault}; | ||
|
||
/// Deriving `Upgradable` requires the contract to be `Ownable.` | ||
#[near_bindgen] | ||
#[derive(Ownable, Upgradable, PanicOnDefault, BorshDeserialize, BorshSerialize)] | ||
pub struct Contract; | ||
|
||
#[near_bindgen] | ||
impl Contract { | ||
/// Parameter `owner` allows setting the owner in the constructor if an `AccountId` is provided. | ||
/// If `owner` is `None`, no owner will be set in the constructor. After contract initialization | ||
/// it is possible to set an owner with `Ownable::owner_set`. | ||
/// | ||
/// Parameter `staging_duration` allows initializing the time that is required to pass between | ||
/// staging and deploying code. This delay provides a safety mechanism to protect users against | ||
/// unfavorable or malicious code upgrades. If `staging_duration` is `None`, no staging duration | ||
/// will be set in the constructor. It is possible to set it later using | ||
/// `Upgradable::up_init_staging_duration`. If no staging duration is set, it defaults to zero, | ||
/// allowing immediate deployments of staged code. | ||
/// | ||
/// Since this constructor uses an `*_unchecked` method, it should be combined with code | ||
/// deployment in a batch transaction. | ||
#[init] | ||
pub fn new(owner: Option<AccountId>) -> Self { | ||
let mut contract = Self; | ||
|
||
// Optionally set the owner. | ||
if owner.is_some() { | ||
contract.owner_set(owner); | ||
} | ||
|
||
contract | ||
} | ||
} |
Oops, something went wrong.