From 2b8494667722c551a301556da04567957d540abb Mon Sep 17 00:00:00 2001 From: Saurabh Patil <55076843+Saurabhpatil-dev@users.noreply.github.com> Date: Wed, 22 Nov 2023 11:22:01 +0530 Subject: [PATCH 1/4] :hammer: Updating state.ts to deprecate precondition apis As discussed in issue #1247 , I have replaced old api by deprecating them rather than removing. Question: Do we need to update this `assertStatePrecondition` too ? Please review and let know if anything else is required. --- src/lib/state.ts | 49 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/src/lib/state.ts b/src/lib/state.ts index 5be755d94d..cd5e744c5f 100644 --- a/src/lib/state.ts +++ b/src/lib/state.ts @@ -22,17 +22,17 @@ type State = { * Get the current on-chain state. * * Caution: If you use this method alone inside a smart contract, it does not prove that your contract uses the current on-chain state. - * To successfully prove that your contract uses the current on-chain state, you must add an additional `.assertEquals()` statement or use `.getAndAssertEquals()`: + * To successfully prove that your contract uses the current on-chain state, you must add an additional `.requireEquals()` statement or use `.getAndRequireEquals()`: * * ```ts * let x = this.x.get(); - * this.x.assertEquals(x); + * this.x.requireEquals(x); * ``` * * OR * * ```ts - * let x = this.x.getAndAssertEquals(); + * let x = this.x.getAndRequireEquals(); * ``` */ get(): A; @@ -40,6 +40,10 @@ type State = { * Get the current on-chain state and prove it really has to equal the on-chain state, * by adding a precondition which the verifying Mina node will check before accepting this transaction. */ + getAndRequireEquals(): A; + /** + * @deprecated use `this.state.getAndRequireEquals()` which is equivalent + */ getAndAssertEquals(): A; /** * Set the on-chain state to a new value. @@ -53,10 +57,18 @@ type State = { * Prove that the on-chain state has to equal the given state, * by adding a precondition which the verifying Mina node will check before accepting this transaction. */ + requireEquals(a: A): void; + /** + * @deprecated use `this.state.requireEquals()` which is equivalent + */ assertEquals(a: A): void; /** * **DANGER ZONE**: Override the error message that warns you when you use `.get()` without adding a precondition. */ + requireNothing(): void; + /** + * @deprecated use `this.state.requireNothing()` which is equivalent + */ assertNothing(): void; /** * Get the state from the raw list of field elements on a zkApp account, for example: @@ -203,6 +215,23 @@ function createState(): InternalStateType { }); }, + requireEquals(state: T) { + if (this._contract === undefined) + throw Error( + 'requireEquals can only be called when the State is assigned to a SmartContract @state.' + ); + let layout = getLayoutPosition(this._contract); + let stateAsFields = this._contract.stateType.toFields(state); + let accountUpdate = this._contract.instance.self; + stateAsFields.forEach((x, i) => { + AccountUpdate.assertEquals( + accountUpdate.body.preconditions.account.state[layout.offset + i], + x + ); + }); + this._contract.wasConstrained = true; + }, + assertEquals(state: T) { if (this._contract === undefined) throw Error( @@ -220,6 +249,14 @@ function createState(): InternalStateType { this._contract.wasConstrained = true; }, + requireNothing() { + if (this._contract === undefined) + throw Error( + 'requireNothing can only be called when the State is assigned to a SmartContract @state.' + ); + this._contract.wasConstrained = true; + }, + assertNothing() { if (this._contract === undefined) throw Error( @@ -294,6 +331,12 @@ function createState(): InternalStateType { return state; }, + getAndRequireEquals(){ + let state = this.get(); + this.requireEquals(state); + return state; + }, + getAndAssertEquals() { let state = this.get(); this.assertEquals(state); From acd687712db2599994bbf233df05155eed764efe Mon Sep 17 00:00:00 2001 From: Saurabh Patil <55076843+Saurabhpatil-dev@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:15:08 +0530 Subject: [PATCH 2/4] :recycle: Refactored assert* to call require* --- src/lib/state.ts | 27 ++++----------------------- 1 file changed, 4 insertions(+), 23 deletions(-) diff --git a/src/lib/state.ts b/src/lib/state.ts index cd5e744c5f..dc848a1996 100644 --- a/src/lib/state.ts +++ b/src/lib/state.ts @@ -233,20 +233,7 @@ function createState(): InternalStateType { }, assertEquals(state: T) { - if (this._contract === undefined) - throw Error( - 'assertEquals can only be called when the State is assigned to a SmartContract @state.' - ); - let layout = getLayoutPosition(this._contract); - let stateAsFields = this._contract.stateType.toFields(state); - let accountUpdate = this._contract.instance.self; - stateAsFields.forEach((x, i) => { - AccountUpdate.assertEquals( - accountUpdate.body.preconditions.account.state[layout.offset + i], - x - ); - }); - this._contract.wasConstrained = true; + this.requireEquals(state); }, requireNothing() { @@ -258,11 +245,7 @@ function createState(): InternalStateType { }, assertNothing() { - if (this._contract === undefined) - throw Error( - 'assertNothing can only be called when the State is assigned to a SmartContract @state.' - ); - this._contract.wasConstrained = true; + this.requireNothing(); }, get() { @@ -331,16 +314,14 @@ function createState(): InternalStateType { return state; }, - getAndRequireEquals(){ + getAndRequireEquals() { let state = this.get(); this.requireEquals(state); return state; }, getAndAssertEquals() { - let state = this.get(); - this.assertEquals(state); - return state; + return this.getAndRequireEquals(); }, async fetch() { From 13c67350b3e62e25aa7a5b027971b32dfda34b4e Mon Sep 17 00:00:00 2001 From: Saurabh Patil <55076843+Saurabhpatil-dev@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:30:02 +0530 Subject: [PATCH 3/4] :memo: Updated Change log with the changes in pullrequest #1263 --- CHANGELOG.md | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7a02ed147d..60b4cf0a98 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,13 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ## [Unreleased](https://github.com/o1-labs/o1js/compare/1ad7333e9e...HEAD) -> No unreleased changes yet +### Changed + +- Preconditioned asset* in state have been renamed to require* : + For example: + - `this.x.getAndAssertEquals()` is now `this.x.getAndRequireEquals()` https://github.com/o1-labs/o1js/pull/1263 + - `this.x.assertEquals(x)` is now `this.x.requireEquals(x)` https://github.com/o1-labs/o1js/pull/1263 + - `this.x.assertNothing()` is now `this.x.requireNothing()` https://github.com/o1-labs/o1js/pull/1263 ## [0.14.2](https://github.com/o1-labs/o1js/compare/26363465d...1ad7333e9e) From 6d4acd3120f14becf13a819bb46ff1bd17170f83 Mon Sep 17 00:00:00 2001 From: Gregor Mitscha-Baude Date: Wed, 22 Nov 2023 15:57:03 +0100 Subject: [PATCH 4/4] Update CHANGELOG.md --- CHANGELOG.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 60b4cf0a98..6201c24a15 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,8 +21,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Changed -- Preconditioned asset* in state have been renamed to require* : - For example: +- Change precondition APIs to use "require" instead of "assert" as the verb, to distinguish them from provable assertions. - `this.x.getAndAssertEquals()` is now `this.x.getAndRequireEquals()` https://github.com/o1-labs/o1js/pull/1263 - `this.x.assertEquals(x)` is now `this.x.requireEquals(x)` https://github.com/o1-labs/o1js/pull/1263 - `this.x.assertNothing()` is now `this.x.requireNothing()` https://github.com/o1-labs/o1js/pull/1263