-
Notifications
You must be signed in to change notification settings - Fork 8
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
upgradability functionality #48
Merged
Merged
Changes from 19 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2352177
basic generic upgrade function
be78ad1
adding Upgrade and UpgradeHook traits
9556b5a
Merge branch 'develop' into upgradability
kenobijon 937577f
Merge remote-tracking branch 'origin/develop' into upgradability
35fcbe9
update to stable near-sdk-rs
fe75f3f
Merge remote-tracking branch 'origin/develop' into upgradability
0f2bbcf
broken upgrade workspaces-tests
8e13ddd
fix: remove migrate argument
encody e4fb18d
Added basic failure tests for upgrade
07ad8a8
added owner check for upgrade tests
fcc138b
not owner failure test
921ffd2
removing JS scripts
fd2703a
Merge branch 'develop' into upgradability
kenobijon aebada0
cleaned up workspaces tests for upgrade
4a2cd37
Merge branch 'develop' into upgradability
kenobijon 451f373
removed unnecessary near_bindgen
57d06ff
fixed macro version issue
2643aa2
fix: some comments
encody 42b1495
chore: conciseness
encody 9214092
fixing naming conventions
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 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
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,54 @@ | ||
//! Upgrade Mod | ||
//! | ||
//! Makes it easier to upgrade your contract by providing a simple interface for upgrading the code and the state of your contract. | ||
|
||
use near_sdk::{env, sys, Gas}; | ||
|
||
use near_sdk::borsh::{BorshDeserialize, BorshSerialize}; | ||
|
||
/// Upgrade Trait | ||
pub trait Upgrade { | ||
/// upgrade_all - Upgrades the code and the state of the contract | ||
fn upgrade_all(); | ||
} | ||
|
||
/// Contracts may implement this trait to inject code into Upgrade functions. | ||
pub trait UpgradeHook { | ||
/// Executed before a upgrade call is conducted | ||
fn on_upgrade(); | ||
} | ||
Comment on lines
+16
to
+19
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this function is ever called. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The idea was to keep it flexible |
||
|
||
/// Naked upgrade function which calls migrate method on the contract | ||
pub fn upgrade<T>() | ||
kenobijon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
where | ||
T: BorshDeserialize + BorshSerialize, | ||
{ | ||
env::setup_panic_hook(); | ||
|
||
const MIGRATE_METHOD_NAME: &[u8; 7] = b"migrate"; | ||
kenobijon marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const UPDATE_GAS_LEFTOVER: Gas = Gas(5_000_000_000_000); | ||
|
||
unsafe { | ||
// Load code into register 0 result from the input argument if factory call or from promise if callback. | ||
sys::input(0); | ||
// Create a promise batch to update current contract with code from register 0. | ||
let promise_id = sys::promise_batch_create( | ||
env::current_account_id().as_bytes().len() as u64, | ||
env::current_account_id().as_bytes().as_ptr() as u64, | ||
); | ||
// Deploy the contract code from register 0. | ||
sys::promise_batch_action_deploy_contract(promise_id, u64::MAX, 0); | ||
// Call promise to migrate the state. | ||
// Batched together to fail upgrade if migration fails. | ||
sys::promise_batch_action_function_call( | ||
promise_id, | ||
MIGRATE_METHOD_NAME.len() as u64, | ||
MIGRATE_METHOD_NAME.as_ptr() as u64, | ||
0, | ||
0, | ||
0, | ||
(env::prepaid_gas() - env::used_gas() - UPDATE_GAS_LEFTOVER).0, | ||
); | ||
sys::promise_return(promise_id); | ||
} | ||
} |
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
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,30 @@ | ||
#![allow(missing_docs)] | ||
|
||
use near_sdk::{ | ||
borsh::{self, BorshDeserialize, BorshSerialize}, | ||
near_bindgen, PanicOnDefault, | ||
}; | ||
|
||
pub fn main() {} // Ignore | ||
|
||
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)] | ||
#[near_bindgen] | ||
pub struct ContractBad { | ||
pub foo: u32, | ||
} | ||
|
||
#[near_bindgen] | ||
impl ContractBad { | ||
#[init] | ||
pub fn new() -> Self { | ||
Self { foo: 0 } | ||
} | ||
|
||
pub fn increment_foo(&mut self) { | ||
self.foo += 1; | ||
} | ||
|
||
pub fn get_foo(&self) -> u32 { | ||
self.foo | ||
} | ||
} |
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,45 @@ | ||
#![allow(missing_docs)] | ||
|
||
use near_contract_tools::{migrate::MigrateExternal, migrate::MigrateHook, Migrate}; | ||
use near_sdk::{ | ||
borsh::{self, BorshDeserialize, BorshSerialize}, | ||
near_bindgen, PanicOnDefault, | ||
}; | ||
|
||
pub fn main() {} | ||
|
||
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault)] | ||
pub struct ContractOld { | ||
pub foo: u32, | ||
} | ||
|
||
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault, Migrate)] | ||
#[migrate(from = "ContractOld")] | ||
#[near_bindgen] | ||
pub struct ContractNew { | ||
pub bar: u64, | ||
} | ||
|
||
impl MigrateHook for ContractNew { | ||
fn on_migrate(old_schema: ContractOld) -> Self { | ||
Self { | ||
bar: old_schema.foo as u64, | ||
} | ||
} | ||
} | ||
|
||
#[near_bindgen] | ||
impl ContractNew { | ||
#[init] | ||
pub fn new() -> Self { | ||
Self { bar: 0 } | ||
} | ||
|
||
pub fn decrement_bar(&mut self) { | ||
self.bar -= 1; | ||
} | ||
|
||
pub fn get_bar(&self) -> u64 { | ||
self.bar | ||
} | ||
} |
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,50 @@ | ||
#![allow(missing_docs)] | ||
|
||
use near_contract_tools::upgrade::{upgrade as other_upgrade, Upgrade, UpgradeHook}; | ||
|
||
use near_contract_tools::{owner::Owner, owner::OwnerExternal, Owner}; | ||
|
||
use near_sdk::{ | ||
borsh::{self, BorshDeserialize, BorshSerialize}, | ||
env, near_bindgen, PanicOnDefault, | ||
}; | ||
pub fn main() {} | ||
|
||
#[derive(BorshSerialize, BorshDeserialize, PanicOnDefault, Owner)] | ||
#[near_bindgen] | ||
pub struct ContractOld { | ||
pub foo: u32, | ||
} | ||
|
||
#[near_bindgen] | ||
impl ContractOld { | ||
#[init] | ||
pub fn new() -> Self { | ||
let mut contract = Self { foo: 0 }; | ||
|
||
Owner::init(&mut contract, &env::predecessor_account_id()); | ||
contract | ||
} | ||
|
||
pub fn increment_foo(&mut self) { | ||
self.foo += 1; | ||
} | ||
|
||
pub fn get_foo(&self) -> u32 { | ||
self.foo | ||
} | ||
} | ||
|
||
impl UpgradeHook for ContractOld { | ||
fn on_upgrade() { | ||
Self::require_owner(); | ||
} | ||
} | ||
|
||
impl Upgrade for ContractOld { | ||
#[no_mangle] | ||
fn upgrade_all() { | ||
Self::require_owner(); | ||
other_upgrade::<ContractOld>(); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this comment accurate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated