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

represent whether signed and/or proven within Transaction type + rework prove method #1567

Merged
merged 27 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
688304b
enable chaining prove, typedocs
harrysolovay Apr 12, 2024
4b359d2
get rid of needless console log in example
harrysolovay Apr 12, 2024
abdf47d
improve TransactionPromise typing
harrysolovay Apr 12, 2024
ed4d448
revert example change
harrysolovay Apr 12, 2024
e8a23da
typing mania
harrysolovay Apr 12, 2024
d718c28
remove default type param and clean up example
harrysolovay Apr 12, 2024
a10a4c4
add signed type to Transaction and TransactionPromise
harrysolovay Apr 12, 2024
f365521
fix signature of sendTransaction in mina-instance
harrysolovay Apr 12, 2024
38e0874
add todo comment
harrysolovay Apr 12, 2024
f990ce6
make proof plural
harrysolovay Apr 12, 2024
1ad5726
add to changelog
harrysolovay Apr 12, 2024
9a4354c
clean up changelog sentence
harrysolovay Apr 12, 2024
f3a0276
more changelog cleanup
harrysolovay Apr 12, 2024
02c6f47
Transaction shadowing in the same file
harrysolovay Apr 13, 2024
9b4bfe8
remove changelog heading
harrysolovay Apr 13, 2024
e9f47ab
fix merge conflicts
harrysolovay Apr 16, 2024
dc59a79
fix integration test error
harrysolovay Apr 16, 2024
3bf2b8e
sync w upstream and fix merge conflicts
harrysolovay Apr 17, 2024
ea5e13b
make proofs into a method
harrysolovay Apr 17, 2024
442c340
clean up reducer-composite eg
harrysolovay Apr 17, 2024
68c7fab
Merge branch 'main' into prove-in-chain
MartinMinkov Apr 17, 2024
ed7b7c0
Merge branch 'prove-in-chain' of github.com:harrysolovay/o1js into pr…
MartinMinkov Apr 17, 2024
c60ac20
feat(local-blockchain): fix support for actions
MartinMinkov Apr 17, 2024
02f9777
feat(examples): fixup examples causing issues in integration tests
MartinMinkov Apr 17, 2024
c60ecf0
chore(mina): update mina submodule to latest commit for up-to-date fe…
MartinMinkov Apr 17, 2024
155a12b
fix(fake-proof.ts): change the way contractProof is extracted from tx…
MartinMinkov Apr 17, 2024
8e76633
fix(on-chain-state-mgmt-zkapp-ui.js): change proof extraction from tr…
MartinMinkov Apr 17, 2024
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
11 changes: 7 additions & 4 deletions src/examples/chaining-tx-methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ await Mina.transaction(sender, async () => {
console.log('initial state: ' + zkapp.x.get());

console.log('increment');
const incrementTx = Mina.transaction(sender, async () => {

await Mina.transaction(sender, async () => {
await zkapp.increment();
}).sign([senderKey]);
await incrementTx.then((v) => v.prove());
await incrementTx.send().wait();
})
.sign([senderKey])
.prove()
.send()
.wait();

console.log('final state: ' + zkapp.x.get());
64 changes: 49 additions & 15 deletions src/lib/mina/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type Transaction = {
* Submits the {@link Transaction} to the network. This method asynchronously sends the transaction
* for processing. If successful, it returns a {@link PendingTransaction} instance, which can be used to monitor the transaction's progress.
* If the transaction submission fails, this method throws an error that should be caught and handled appropriately.
* @returns A promise that resolves to a {@link PendingTransaction} instance representing the submitted transaction if the submission is successful.
* @returns A {@link PendingTransactionPromise}, which resolves to a {@link PendingTransaction} instance representing the submitted transaction if the submission is successful.
* @throws An error if the transaction cannot be sent or processed by the network, containing details about the failure.
* @example
* ```ts
Expand All @@ -104,7 +104,7 @@ type Transaction = {
* }
* ```
*/
send(): Promise<PendingTransaction>;
send(): PendingTransactionPromise;
/**
* Sends the {@link Transaction} to the network. Unlike the standard {@link Transaction.send}, this function does not throw an error if internal errors are detected. Instead, it returns a {@link PendingTransaction} if the transaction is successfully sent for processing or a {@link RejectedTransaction} if it encounters errors during processing or is outright rejected by the Mina daemon.
* @returns {Promise<PendingTransaction | RejectedTransaction>} A promise that resolves to a {@link PendingTransaction} if the transaction is accepted for processing, or a {@link RejectedTransaction} if the transaction fails or is rejected.
Expand Down Expand Up @@ -280,13 +280,32 @@ type RejectedTransaction = Pick<
errors: string[];
};

/**
* A `Promise<Transaction>` with some additional methods for making chained method calls
* into the pending value upon its resolution.
*/
type TransactionPromise = Promise<Transaction> & {
/** Equivalent to calling the resolved `Transaction`'s `sign` method. */
sign(...args: Parameters<Transaction['sign']>): TransactionPromise;
/** Equivalent to calling the resolved `Transaction`'s `send` method. */
send(): PendingTransactionPromise;
/**
* Calls `prove` upon resolution of the `Transaction`. Returns a
* new `TransactionPromise` with the field `proofPromise` containing
* a promise which resolves to the proof array.
*/
prove(): TransactionPromise;
/**
* If the chain of method calls that produced the current `TransactionPromise`
* contains a `prove` call, then this field contains a promise resolving to the
* proof array which was output from the underlying `prove` call.
*/
proofPromise?: Promise<(Proof<ZkappPublicInput, undefined> | undefined)[]>;
harrysolovay marked this conversation as resolved.
Show resolved Hide resolved
};

function toTransactionPromise(
getPromise: () => Promise<Transaction>
getPromise: () => Promise<Transaction>,
proofPromise?: Promise<(Proof<ZkappPublicInput, undefined> | undefined)[]>
): TransactionPromise {
const pending = getPromise().then();
return Object.assign(pending, {
Expand All @@ -296,10 +315,23 @@ function toTransactionPromise(
send() {
return toPendingTransactionPromise(() => pending.then((v) => v.send()));
},
}) as TransactionPromise;
prove() {
const proofPromise_ = proofPromise ?? pending.then((v) => v.prove());
return toTransactionPromise(async () => {
await proofPromise_;
return await pending;
}, proofPromise_);
},
proofPromise,
});
}

/**
* A `Promise<PendingTransaction>` with an additional `wait` method, which calls
* into the inner `TransactionStatus`'s `wait` method upon its resolution.
*/
type PendingTransactionPromise = Promise<PendingTransaction> & {
/** Equivalent to calling the resolved `PendingTransaction`'s `wait` method. */
wait: PendingTransaction['wait'];
};

Expand All @@ -311,7 +343,7 @@ function toPendingTransactionPromise(
wait(...args: Parameters<PendingTransaction['wait']>) {
return pending.then((v) => v.wait(...args));
},
}) as PendingTransactionPromise;
});
}

async function createTransaction(
Expand Down Expand Up @@ -439,16 +471,18 @@ function newTransaction(transaction: ZkappCommand, proofsEnabled?: boolean) {
toGraphqlQuery() {
return sendZkappQuery(self.toJSON());
},
async send() {
const pendingTransaction = await sendTransaction(self);
if (pendingTransaction.errors.length > 0) {
throw Error(
`Transaction failed with errors:\n- ${pendingTransaction.errors.join(
'\n- '
)}`
);
}
return pendingTransaction;
send() {
return toPendingTransactionPromise(async () => {
const pendingTransaction = await sendTransaction(self);
if (pendingTransaction.errors.length > 0) {
throw Error(
`Transaction failed with errors:\n- ${pendingTransaction.errors.join(
'\n- '
)}`
);
}
return pendingTransaction;
});
},
async safeSend() {
const pendingTransaction = await sendTransaction(self);
Expand Down
Loading