Skip to content

Smart Contract Upgradability#123

Closed
ilblackdragon wants to merge 2 commits into
masterfrom
sc-upgrade
Closed

Smart Contract Upgradability#123
ilblackdragon wants to merge 2 commits into
masterfrom
sc-upgrade

Conversation

@ilblackdragon
Copy link
Copy Markdown
Member

@ilblackdragon ilblackdragon commented Oct 9, 2020

Smart contract upgradability

In NEAR smart contracts are upgradable in-place by default.
This means that given a specific account-id - the contract code may change depending on various rules.

Non-upgradable

There are many contracts that should not be upgradable.

There are two ways that contract code can be upgraded in-place: using an access key and contract re-deploying it's own code.

To determine if contract is not upgradable, need to check that contract doesn't have any full access keys and that contract's code doesn't contain any DeployContract calls to itself.

Additionally, contract code can contain ability to add FullAccessKeys to itself, and in this way return back control to the facilitator (for example multisig and lockup contracts have this ability under certain circumstances).

Accessible contract

Accessible contracts are contracts that have access keys with ability to re-deploy new code on them.

Usually this will be used for development or in some highly permissioned settings.

For example, contracts like multisig is really owned by the person or people who control it and hence they can re-deploy the code on it to upgrade or remove multisig.

Owner-pattern

Owner pattern means that contract's upgradability is owned by a different account.

This allows to introduce a more sophisticated management, anything from multisig to DAO. This also allows to easily evolve the management over time. For example, starting from simple account, one can change owner later to multisig and later upgrade to a DAO.

Owner upgradable pattern introduces next set of methods on a contract that follows this standard:

    /// Returns current owner account.
    get_owner() -> AccountId

    /// Sets new owner. If owner is set to `system` this contract is considered to be non-upgradable without hard forks.
    /// Can only be called by owner.
    set_owner(owner: AccountId)

    /// Returns how long between staging code and deploy can be called in nanoseconds.
    /// Usually staging duration is set at deploy. Good defaults are 1 and 7 days.
    get_staging_duration() -> WrappedDuration

    /// Stages new contract on the given account. 
    /// Can only be called by owner.
    /// This means that contract code is ready to be activated after specific staging time passes.
    /// Caller is responsible for attaching extra balance to cover storage.
    stage_code(code: bytes)

    /// Deploys staged code. 
    /// Can only be called by owner.
    /// If there is no staged code, will fail.
    deploy_code()

TBD: do we want to have a way to change staging duration?

State migrations

This section is TBD on state migration patters

@render
Copy link
Copy Markdown

render Bot commented Oct 9, 2020

@ilblackdragon
Copy link
Copy Markdown
Member Author

Referencing a overview for upgradability in Ethereum - https://blog.openzeppelin.com/the-state-of-smart-contract-upgrades/

@bowenwang1996
Copy link
Copy Markdown
Collaborator

TBD: do we want to have a way to change staging duration?

Should we also allow activation upon some absolute time?

@ilblackdragon
Copy link
Copy Markdown
Member Author

@bowenwang1996

Should we also allow activation upon some absolute time?

Interesting, we can make stage_code to take timestamp. And just check that timestamp is further than staging duration to satisfy the requirement. This would make timing more predictable so people who deploy don't need to stage it perfectly to have a round deployment time.

Also interesting consideration if deploy_code should be available for anyone to call - then there can be a "cron" service that just calls it when it's time. The problem is that there should be a way to "unstage" then.

I also think it's important to match this with an advance tool I was describing: neard --launch-from-mainnet --fast-time. This would start from a current snapshot of mainnet but with given node as a validator and also run with faster time. This way you can fork from snapshot, stage, then test as if it was deployed in mainnet as a final check with all the other contracts and real user state.

@bowenwang1996
Copy link
Copy Markdown
Collaborator

I also think it's important to match this with an advance tool I was describing: neard --launch-from-mainnet --fast-time. This would start from a current snapshot of mainnet but with given node as a validator and also run with faster time. This way you can fork from snapshot, stage, then test as if it was deployed in mainnet as a final check with all the other contracts and real user state.

Sounds nice, but I don't think it is trivial to build :(

@Kouprin
Copy link
Copy Markdown

Kouprin commented Nov 6, 2020

How does voting for upgrade look like? For non-trustless case I see no concerns.

@ilblackdragon
Copy link
Copy Markdown
Member Author

How does voting for upgrade look like? For non-trustless case I see no concerns.

This standard doesn't define how exactly owner decides to upgrade or not. It can be anything from multisig to DAO to a single person deciding, to using Oracle if price is above :)

@alexauroradev
Copy link
Copy Markdown

What about pausablity of a contract or escape hatches or commit-reveal scheme? Should we allow for this because of security upgrades?

@ilblackdragon ilblackdragon mentioned this pull request Jan 26, 2021
@alexauroradev
Copy link
Copy Markdown

I also think it's important to match this with an advance tool I was describing: neard --launch-from-mainnet --fast-time. This would start from a current snapshot of mainnet but with given node as a validator and also run with faster time. This way you can fork from snapshot, stage, then test as if it was deployed in mainnet as a final check with all the other contracts and real user state.

How does this differ from using different staging time?

@ilblackdragon
Copy link
Copy Markdown
Member Author

How does this differ from using different staging time?

This would be a tool for testing locally contracts while developing. Not related to staging itself.

@mfornet
Copy link
Copy Markdown
Member

mfornet commented Jun 13, 2021

This section is TBD on state migration patters

Is there any discussion somewhere about state migration patterns?

@ilblackdragon
Copy link
Copy Markdown
Member Author

https://github.com/near/near-sdk-rs/blob/31cba6213919ec4866bc0fab7f6963de241cc43a/HELP.md#upgrading-a-contract

@frol frol added the WG-contract-standards Contract Standards Work Group should be accountable label Sep 5, 2022
@frol frol added help wanted Extra attention is needed S-draft/needs-author-revision A NEP in the DRAFT stage that needs an author revision. labels Sep 30, 2022
@ori-near ori-near added the A-NEP A NEAR Enhancement Proposal (NEP). label Oct 13, 2022
@ori-near
Copy link
Copy Markdown
Contributor

Hi @ilblackdragon (or anyone who is interested in championing this NEP forward) – We are cleaning the NEP backlog and noticed that this PR still has some remaining "TBD" sections noted. Therefore, we are labeling this PR as "Needs author revision."

Please ping the @near/nep-moderators once you are ready for us to review it. We will review it again in February, unless we hear from you sooner. We typically close NEPs that are inactive for more than two months, so please let us know if you need more time.

@ori-near ori-near added help wanted Extra attention is needed and removed help wanted Extra attention is needed labels Dec 20, 2022
@frol frol marked this pull request as draft December 20, 2022 19:35
@ilblackdragon
Copy link
Copy Markdown
Member Author

I do think it's an important to standardize as we currently have a zoo of upgradability patterns. But I will not be one to do this. I'm going to close until an author who can put in time can be found.

@ori-near ori-near added S-retracted A NEP that was retracted by the author or had no activity for over two months. and removed S-draft/needs-author-revision A NEP in the DRAFT stage that needs an author revision. labels Dec 22, 2022
@ori-near ori-near removed the help wanted Extra attention is needed label Jan 7, 2023
@bucanero bucanero deleted the sc-upgrade branch September 19, 2025 13:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-NEP A NEAR Enhancement Proposal (NEP). S-retracted A NEP that was retracted by the author or had no activity for over two months. WG-contract-standards Contract Standards Work Group should be accountable

Projects

Status: RETRACTED

Development

Successfully merging this pull request may close these issues.

7 participants