Skip to content

Commit

Permalink
Merge pull request #115 from o1-labs/feature/method-decorator-and-com…
Browse files Browse the repository at this point in the history
…pile

Transaction Construction, Pt 1. `@method` and `compile()`
  • Loading branch information
mitschabaude committed Apr 29, 2022
2 parents 9fd8557 + 6448325 commit 296734d
Show file tree
Hide file tree
Showing 10 changed files with 232 additions and 96 deletions.
2 changes: 1 addition & 1 deletion src/examples/ex00_preimage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ await isReady;
console.log('generating keypair...');
const kp = Main.generateKeypair();

const preimage = Field.random();
const preimage = Field.one;
const hash = Poseidon.hash([preimage]);

console.log('prove...');
Expand Down
15 changes: 13 additions & 2 deletions src/examples/simple_snapp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class SimpleSnapp extends SmartContract {
this.x.set(x);
}

@method async update(y: Field) {
let x = await this.x.get();
@method update(y: Field) {
let x = this.x.get();
this.x.set(x.add(y));
}
}
Expand All @@ -37,6 +37,12 @@ const account2 = Local.testAccounts[1].privateKey;
const snappPrivKey = PrivateKey.random();
const snappPubKey = snappPrivKey.toPublicKey();

console.log('compile');
let {
provers: [updateProver],
} = SimpleSnapp.compile(snappPubKey);

console.log('deploy');
await Mina.transaction(account1, async () => {
let snapp = new SimpleSnapp(snappPubKey);

Expand All @@ -49,6 +55,11 @@ await Mina.transaction(account1, async () => {
.send()
.wait();

console.log('prove');
let proof = updateProver(Field(1));
console.log({ proof });

console.log('update');
let snappState = (await Mina.getAccount(snappPubKey)).snapp.appState[0];
console.log('initial state: ' + snappState);

Expand Down
23 changes: 23 additions & 0 deletions src/lib/circuit_value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,26 @@ export function circuitMain(
Array.from(publicIndexSet).map((i) => paramTypes[i])
);
}

export function toFieldsMagic(thing: any): Field[] {
if (typeof thing == 'object') {
if (thing.toFields) {
// console.log('toFields');
return thing.toFields();
}

if (Array.isArray(thing)) {
// console.log('array', thing.length);
return thing.map((x) => toFieldsMagic(x)).flat();
}

let fields = [] as Field[];
let keys = Object.keys(thing).sort();
for (let key of keys) {
// console.log({ key });
fields.push(...toFieldsMagic(thing[key]));
}
return fields;
}
return [];
}
29 changes: 15 additions & 14 deletions src/lib/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import {
} from '../snarky';
import { UInt32, UInt64 } from './int';
import { PrivateKey, PublicKey } from './signature';
import {
Body,
EpochDataPredicate,
ProtocolStatePredicate,
} from './party';
import { Body, EpochDataPredicate, ProtocolStatePredicate } from './party';

interface TransactionId {
wait(): Promise<void>;
Expand All @@ -41,19 +37,24 @@ export let nextTransactionId: { value: number } = { value: 0 };

type PartyPredicate = UInt32 | FullAccountPredicate | undefined;

export let currentTransaction:
export type CurrentTransaction =
| undefined
| {
sender: PrivateKey;
parties: Array<{ body: Body; predicate: PartyPredicate }>;
nextPartyIndex: number;
protocolState: ProtocolStatePredicate;
}
| undefined = undefined;
};

export let currentTransaction: CurrentTransaction = undefined;
export function setCurrentTransaction(transaction: CurrentTransaction) {
currentTransaction = transaction;
}

interface Mina {
transaction(sender: PrivateKey, f: () => void | Promise<void>): Transaction;
currentSlot(): UInt32;
getAccount(publicKey: PublicKey): Promise<Account>;
getAccount(publicKey: PublicKey): Account;
}

interface MockMina extends Mina {
Expand Down Expand Up @@ -92,7 +93,7 @@ export const LocalBlockchain: () => MockMina = () => {
testAccounts.push({ privateKey: k, publicKey: pk });
}

const getAccount = (pk: PublicKey): Promise<Account> => {
const getAccount = (pk: PublicKey) => {
const r = ledger.getAccount(pk);
if (r == null) {
throw new Error(
Expand All @@ -104,7 +105,7 @@ export const LocalBlockchain: () => MockMina = () => {
nonce: new UInt32(r.nonce.value),
snapp: r.snapp,
};
return new Promise((r) => r(a));
return a;
}
};

Expand Down Expand Up @@ -294,13 +295,13 @@ export function currentSlot(): UInt32 {
/**
* @return The account data associated to the given public key.
*/
export function getAccount(pubkey: PublicKey): Promise<Account> {
export function getAccount(pubkey: PublicKey) {
return activeInstance.getAccount(pubkey);
}

/**
* @return The balance associated to the given public key.
*/
export function getBalance(pubkey: PublicKey): Promise<UInt64> {
return activeInstance.getAccount(pubkey).then((a) => a.balance);
export function getBalance(pubkey: PublicKey) {
return activeInstance.getAccount(pubkey).balance;
}
34 changes: 17 additions & 17 deletions src/lib/party.ts
Original file line number Diff line number Diff line change
Expand Up @@ -758,27 +758,27 @@ export class Party<P> {
return party;
}

static createSigned(signer: PrivateKey): Promise<Party<UInt32>> {
static createSigned(signer: PrivateKey) {
// TODO: This should be a witness block that uses the setVariable
// API to set the value of a variable after it's allocated

const pk = signer.toPublicKey();
const body: Body = Body.keepAll(pk);
return Mina.getAccount(pk).then((a) => {
if (Mina.currentTransaction === undefined) {
throw new Error(
'Party.createSigned: Cannot run outside of a transaction'
);
}

if (a == null) {
throw new Error('Party.createSigned: Account not found');
}

const party = new Party(body, a.nonce);
Mina.currentTransaction.nextPartyIndex++;
Mina.currentTransaction.parties.push(party);
return party;
});
let a = Mina.getAccount(pk);

if (Mina.currentTransaction === undefined) {
throw new Error(
'Party.createSigned: Cannot run outside of a transaction'
);
}

if (a == null) {
throw new Error('Party.createSigned: Account not found');
}

const party = new Party(body, a.nonce);
Mina.currentTransaction.nextPartyIndex++;
Mina.currentTransaction.parties.push(party);
return party;
}
}
Loading

0 comments on commit 296734d

Please sign in to comment.