Skip to content

Commit

Permalink
Consolidate preconditions MinaProtocol/mina#11096
Browse files Browse the repository at this point in the history
SnarkyJS side of MinaProtocol/mina#11096 addressing the naming
consistency part of #179
  • Loading branch information
bkase committed May 25, 2022
1 parent 812b31d commit 34fc08c
Show file tree
Hide file tree
Showing 5 changed files with 1,102 additions and 1,060 deletions.
34 changes: 23 additions & 11 deletions src/lib/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ const ZkappStateLength = 8;
type PartyBody = Types.Party['body'];
type Update = PartyBody['update'];

/**
* Preconditions for the network and accounts
*/
export type Preconditions = PartyBody['preconditions'];

/**
* Timing info inside an account.
*/
Expand Down Expand Up @@ -287,8 +292,7 @@ interface Body extends PartyBody {
caller: Field;
callData: Field; //MerkleList<Array<Field>>;
callDepth: number; // TODO: this is an `int As_prover.t`
protocolStatePrecondition: ProtocolStatePrecondition;
accountPrecondition: AccountPrecondition;
preconditions: Preconditions;
useFullCommitment: Bool;
incrementNonce: Bool;
}
Expand Down Expand Up @@ -336,8 +340,7 @@ const Body = {
caller: getDefaultTokenId(),
callData: Field.zero, // TODO new MerkleList(),
callDepth: 0,
protocolStatePrecondition: ProtocolStatePrecondition.ignoreAll(),
accountPrecondition: AccountPrecondition.ignoreAll(),
preconditions: Preconditions.ignoreAll(),
// the default assumption is that snarkyjs transactions don't include the fee payer
// so useFullCommitment has to be false for signatures to be correct
useFullCommitment: Bool(false),
Expand All @@ -362,7 +365,7 @@ const FeePayerBody = {
update: Body.noUpdate(),
events: Events.empty(),
sequenceEvents: Events.empty(),
protocolStatePrecondition: ProtocolStatePrecondition.ignoreAll(),
networkPrecondition: NetworkPrecondition.ignoreAll(),
};
},
};
Expand All @@ -387,9 +390,9 @@ type OrIgnore<T> = { isSome: Bool; value: T };
*/
type ClosedInterval<T> = { lower: T; upper: T };

type ProtocolStatePrecondition = PartyBody['protocolStatePrecondition'];
let ProtocolStatePrecondition = {
ignoreAll(): ProtocolStatePrecondition {
type NetworkPrecondition = Preconditions['network'];
let NetworkPrecondition = {
ignoreAll(): NetworkPrecondition {
let stakingEpochData = {
ledger: { hash: ignore(Field.zero), totalCurrency: uint64() },
seed: ignore(Field.zero),
Expand Down Expand Up @@ -432,7 +435,7 @@ const uint32 = () => ({ lower: UInt32.fromNumber(0), upper: UInt32.MAXINT() });
*/
const uint64 = () => ({ lower: UInt64.fromNumber(0), upper: UInt64.MAXINT() });

export type AccountPrecondition = PartyBody['accountPrecondition'];
export type AccountPrecondition = Preconditions['account'];
export const AccountPrecondition = {
ignoreAll(): AccountPrecondition {
let appState: Array<OrIgnore<Field>> = [];
Expand All @@ -456,6 +459,15 @@ export const AccountPrecondition = {
},
};

export const Preconditions = {
ignoreAll(): Preconditions {
return {
account: AccountPrecondition.ignoreAll(),
network: NetworkPrecondition.ignoreAll()
}
}
};

type Control = Types.Party['authorization'];

type LazySignature = { kind: 'lazy-signature'; privateKey?: PrivateKey };
Expand Down Expand Up @@ -604,7 +616,7 @@ class Party {

setNoncePrecondition(fallbackToZero = false) {
let nonce = Party.getNonce(this, fallbackToZero);
let accountPrecondition = this.body.accountPrecondition;
let accountPrecondition = this.body.preconditions.account;
Party.assertEquals(accountPrecondition.nonce, nonce);
return nonce;
}
Expand Down Expand Up @@ -700,7 +712,7 @@ class Party {
nonceIncrement.add(new UInt32(shouldIncreaseNonce.toField()));
}
nonce = nonce.add(nonceIncrement);
Party.assertEquals(body.accountPrecondition.nonce, nonce);
Party.assertEquals(body.preconditions.account.nonce, nonce);
body.incrementNonce = Bool(true);

let party = new Party(body);
Expand Down
4 changes: 2 additions & 2 deletions src/lib/zkapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ function createState<A>() {
let e: ExecutionState = this._this.executionState();

stateAsFields.forEach((x, i) => {
e.party.body.accountPrecondition.state[layout.offset + i].isSome =
e.party.body.preconditions.account.state[layout.offset + i].isSome =
Bool(true);
e.party.body.accountPrecondition.state[layout.offset + i].value = x;
e.party.body.preconditions.account.state[layout.offset + i].value = x;
});
},

Expand Down
Loading

0 comments on commit 34fc08c

Please sign in to comment.