Skip to content
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

🔨 Updating state.ts to deprecate existing precondition apis and updating them with new #1263

Merged
merged 4 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
mitschabaude marked this conversation as resolved.
Show resolved Hide resolved

## [0.14.2](https://github.com/o1-labs/o1js/compare/26363465d...1ad7333e9e)

Expand Down
42 changes: 33 additions & 9 deletions src/lib/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,28 @@ type State<A> = {
* 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;
/**
* 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.
Expand All @@ -53,10 +57,18 @@ type State<A> = {
* 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:
Expand Down Expand Up @@ -203,10 +215,10 @@ function createState<T>(): InternalStateType<T> {
});
},

assertEquals(state: T) {
requireEquals(state: T) {
if (this._contract === undefined)
throw Error(
'assertEquals can only be called when the State is assigned to a SmartContract @state.'
'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);
Expand All @@ -220,14 +232,22 @@ function createState<T>(): InternalStateType<T> {
this._contract.wasConstrained = true;
},

assertNothing() {
assertEquals(state: T) {
this.requireEquals(state);
},

requireNothing() {
if (this._contract === undefined)
throw Error(
'assertNothing can only be called when the State is assigned to a SmartContract @state.'
'requireNothing can only be called when the State is assigned to a SmartContract @state.'
);
this._contract.wasConstrained = true;
},

assertNothing() {
this.requireNothing();
},

get() {
if (this._contract === undefined)
throw Error(
Expand Down Expand Up @@ -294,12 +314,16 @@ function createState<T>(): InternalStateType<T> {
return state;
},

getAndAssertEquals() {
getAndRequireEquals() {
let state = this.get();
this.assertEquals(state);
this.requireEquals(state);
return state;
},

getAndAssertEquals() {
return this.getAndRequireEquals();
},

async fetch() {
if (this._contract === undefined)
throw Error(
Expand Down
Loading