Skip to content

Commit

Permalink
Merge branch 'main' into perf/poseidon-hash-bottleneck
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinMinkov committed Jan 24, 2024
2 parents 2ed13d9 + ccbc379 commit 1486b30
Show file tree
Hide file tree
Showing 24 changed files with 368 additions and 113 deletions.
4 changes: 2 additions & 2 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[submodule "src/snarkyjs-bindings"]
[submodule "src/o1js-bindings"]
path = src/bindings
url = https://github.com/o1-labs/snarkyjs-bindings.git
url = https://github.com/o1-labs/o1js-bindings.git
[submodule "src/mina"]
path = src/mina
url = https://github.com/MinaProtocol/mina.git
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,16 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
_Security_ in case of vulnerabilities.
-->

## [Unreleased](https://github.com/o1-labs/o1js/compare/08ba27329...HEAD)
## [Unreleased](https://github.com/o1-labs/o1js/compare/be748e42e...HEAD)

## [0.15.3](https://github.com/o1-labs/o1js/compare/1ad7333e9e...be748e42e)

### Breaking changes

- `Mina.accountCreationFee()` is deprecated in favor of `Mina.getNetworkConstants().accountCreationFee`. https://github.com/o1-labs/o1js/pull/1367
- `Mina.getNetworkConstants()` returns:
- [default](https://github.com/o1-labs/o1js/pull/1367/files#diff-ef2c3547d64a8eaa8253cd82b3623288f3271e14f1dc893a0a3ddc1ff4b9688fR7) network constants if used outside of the transaction scope.
- [actual](https://github.com/o1-labs/o1js/pull/1367/files#diff-437f2c15df7c90ad8154c5de1677ec0838d51859bcc0a0cefd8a0424b5736f31R1051) network constants if used within the transaction scope.

### Added

Expand Down
85 changes: 51 additions & 34 deletions README-dev.md

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "o1js",
"description": "TypeScript framework for zk-SNARKs and zkApps",
"version": "0.15.2",
"version": "0.15.3",
"license": "Apache-2.0",
"homepage": "https://github.com/o1-labs/o1js/",
"keywords": [
Expand Down Expand Up @@ -44,8 +44,8 @@
"scripts": {
"dev": "npx tsc -p tsconfig.test.json && node src/build/copy-to-dist.js",
"build": "node src/build/copy-artifacts.js && rimraf ./dist/node && npm run dev && node src/build/buildNode.js",
"build:bindings": "./src/bindings/scripts/build-snarkyjs-node.sh",
"build:update-bindings": "./src/bindings/scripts/update-snarkyjs-bindings.sh",
"build:bindings": "./src/bindings/scripts/build-o1js-node.sh",
"build:update-bindings": "./src/bindings/scripts/update-o1js-bindings.sh",
"build:wasm": "./src/bindings/scripts/update-wasm-and-types.sh",
"build:web": "rimraf ./dist/web && node src/build/buildWeb.js",
"build:examples": "npm run build && rimraf ./dist/examples && npx tsc -p tsconfig.examples.json",
Expand Down
9 changes: 9 additions & 0 deletions run-ci-live-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ echo ""
HELLO_WORLD_PROC=$!
./run src/examples/zkapps/dex/run_live.ts --bundle | add_prefix "DEX" &
DEX_PROC=$!
./run src/examples/fetch_live.ts --bundle | add_prefix "FETCH" &
FETCH_PROC=$!

# Wait for each process and capture their exit statuses
FAILURE=0
Expand All @@ -34,6 +36,13 @@ if [ $? -ne 0 ]; then
echo ""
FAILURE=1
fi
wait $FETCH_PROC
if [ $? -ne 0 ]; then
echo ""
echo "FETCH test failed."
echo ""
FAILURE=1
fi

# Exit with failure if any process failed
if [ $FAILURE -ne 0 ]; then
Expand Down
2 changes: 1 addition & 1 deletion src/build/buildWeb.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ async function buildWeb({ production }) {
await Promise.all([tscPromise, copyPromise]);

if (minify) {
let o1jsWebPath = './dist/web/web_bindings/snarky_js_web.bc.js';
let o1jsWebPath = './dist/web/web_bindings/o1js_web.bc.js';
let o1jsWeb = await readFile(o1jsWebPath, 'utf8');
let { code } = await esbuild.transform(o1jsWeb, {
target,
Expand Down
92 changes: 92 additions & 0 deletions src/examples/fetch_live.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { expect } from 'expect';
import { Lightnet, Mina, PrivateKey, UInt64, fetchAccount } from 'o1js';

const useCustomLocalNetwork = process.env.USE_CUSTOM_LOCAL_NETWORK === 'true';
Mina.setActiveInstance(configureMinaNetwork());
const transactionFee = 100_000_000;
let defaultNetworkConstants: Mina.NetworkConstants = {
genesisTimestamp: UInt64.from(0),
slotTime: UInt64.from(3 * 60 * 1000),
accountCreationFee: UInt64.from(1_000_000_000),
};
const { sender } = await configureFeePayer();

await checkDefaultNetworkConstantsFetching();
await checkActualNetworkConstantsFetching();

await tearDown();

async function checkDefaultNetworkConstantsFetching() {
console.log(
'\nCase #1: check that default network constants can be fetched outside of the transaction scope.'
);
const networkConstants = Mina.getNetworkConstants();
expect(defaultNetworkConstants).toEqual(networkConstants);
logNetworkConstants(networkConstants);
}

async function checkActualNetworkConstantsFetching() {
console.log(
'\nCase #2: check that actual network constants can be fetched within the transaction scope.'
);
let networkConstants: Mina.NetworkConstants | undefined;
await Mina.transaction({ sender, fee: transactionFee }, () => {
networkConstants = Mina.getNetworkConstants();
});
expect(networkConstants?.slotTime).not.toBeUndefined();
expect(networkConstants?.genesisTimestamp).not.toBeUndefined();
expect(defaultNetworkConstants).not.toEqual(networkConstants);
logNetworkConstants(networkConstants);
}

function configureMinaNetwork() {
const minaGraphQlEndpoint = useCustomLocalNetwork
? 'http://localhost:8080/graphql'
: 'https://berkeley.minascan.io/graphql';
return Mina.Network({
mina: minaGraphQlEndpoint,
archive: useCustomLocalNetwork
? 'http://localhost:8282'
: 'https://api.minascan.io/archive/berkeley/v1/graphql',
lightnetAccountManager: 'http://localhost:8181',
});
}

async function configureFeePayer() {
const senderKey = useCustomLocalNetwork
? (await Lightnet.acquireKeyPair()).privateKey
: PrivateKey.random();
const sender = senderKey.toPublicKey();
if (!useCustomLocalNetwork) {
console.log(`\nFunding the fee payer account.`);
await Mina.faucet(sender);
}
console.log(`\nFetching the fee payer account information.`);
const accountDetails = (await fetchAccount({ publicKey: sender })).account;
console.log(
`Using the fee payer account ${sender.toBase58()} with nonce: ${
accountDetails?.nonce
} and balance: ${accountDetails?.balance}.`
);
return { sender, senderKey };
}

async function tearDown() {
const keyPairReleaseMessage = await Lightnet.releaseKeyPair({
publicKey: sender.toBase58(),
});
if (keyPairReleaseMessage) console.info('\n' + keyPairReleaseMessage);
}

function logNetworkConstants(
networkConstants: Mina.NetworkConstants | undefined
) {
console.log(`Account creation fee: ${networkConstants?.accountCreationFee}`);
console.log(`Slot time: ${networkConstants?.slotTime}`);
console.log(`Genesis timestamp: ${networkConstants?.genesisTimestamp}`);
console.log(
`Genesis date: ${new Date(
Number(networkConstants?.genesisTimestamp.toString() ?? '0')
)}`
);
}
4 changes: 2 additions & 2 deletions src/examples/plain-html/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
<html>
<head>
<meta charset="utf-8" />
<title>hello-snarkyjs</title>
<title>hello-o1js</title>
<script type="importmap">
{ "imports": { "snarkyjs": "./index.js" } }
{ "imports": { "o1js": "./index.js" } }
</script>
<script type="module" src="./simple_zkapp.js">
</script>
Expand Down
9 changes: 6 additions & 3 deletions src/examples/zkapps/dex/arbitrary_token_interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { TokenContract, addresses, keys, tokenIds } from './dex.js';
let doProofs = true;
let Local = Mina.LocalBlockchain({ proofsEnabled: doProofs });
Mina.setActiveInstance(Local);
let accountFee = Mina.accountCreationFee();

let [{ privateKey: userKey, publicKey: userAddress }] = Local.testAccounts;
let tx;
Expand All @@ -26,7 +25,9 @@ console.log('deploy & init token contracts...');
tx = await Mina.transaction(userAddress, () => {
// pay fees for creating 2 token contract accounts, and fund them so each can create 1 account themselves
let feePayerUpdate = AccountUpdate.createSigned(userAddress);
feePayerUpdate.balance.subInPlace(accountFee.mul(1));
feePayerUpdate.balance.subInPlace(
Mina.getNetworkConstants().accountCreationFee.mul(1)
);
tokenX.deploy();
});
await tx.prove();
Expand All @@ -36,7 +37,9 @@ await tx.send();
console.log('arbitrary token minting...');
tx = await Mina.transaction(userAddress, () => {
// pay fees for creating user's token X account
AccountUpdate.createSigned(userAddress).balance.subInPlace(accountFee.mul(1));
AccountUpdate.createSigned(userAddress).balance.subInPlace(
Mina.getNetworkConstants().accountCreationFee.mul(1)
);
// 😈😈😈 mint any number of tokens to our account 😈😈😈
let tokenContract = new TokenContract(addresses.tokenX);
tokenContract.token.mint({
Expand Down
4 changes: 2 additions & 2 deletions src/examples/zkapps/dex/dex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ class TokenContract extends SmartContract {
// assert that the receiving account is new, so this can be only done once
receiver.account.isNew.requireEquals(Bool(true));
// pay fees for opened account
this.balance.subInPlace(Mina.accountCreationFee());
this.balance.subInPlace(Mina.getNetworkConstants().accountCreationFee);
}

/**
Expand All @@ -410,7 +410,7 @@ class TokenContract extends SmartContract {
// assert that the receiving account is new, so this can be only done once
receiver.account.isNew.requireEquals(Bool(true));
// pay fees for opened account
this.balance.subInPlace(Mina.accountCreationFee());
this.balance.subInPlace(Mina.getNetworkConstants().accountCreationFee);
}

// this is a very standardized deploy method. instead, we could also take the account update from a callback
Expand Down
2 changes: 1 addition & 1 deletion src/examples/zkapps/dex/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class TrivialCoin extends SmartContract implements Erc20 {
// assert that the receiving account is new, so this can be only done once
receiver.account.isNew.requireEquals(Bool(true));
// pay fees for opened account
this.balance.subInPlace(Mina.accountCreationFee());
this.balance.subInPlace(Mina.getNetworkConstants().accountCreationFee);

// since this is the only method of this zkApp that resets the entire state, provedState: true implies
// that this function was run. Since it can be run only once, this implies it was run exactly once
Expand Down
4 changes: 2 additions & 2 deletions src/examples/zkapps/dex/happy-path-with-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ let Local = Mina.LocalBlockchain({
enforceTransactionLimits: true,
});
Mina.setActiveInstance(Local);
let accountFee = Mina.accountCreationFee();
let [{ privateKey: feePayerKey, publicKey: feePayerAddress }] =
Local.testAccounts;
let tx, balances, oldBalances;
Expand All @@ -44,6 +43,7 @@ let dexTokenHolderY = new DexTokenHolder(addresses.dex, tokenIds.Y);

tic('deploy & init token contracts');
tx = await Mina.transaction(feePayerAddress, () => {
const accountFee = Mina.getNetworkConstants().accountCreationFee;
// pay fees for creating 2 token contract accounts, and fund them so each can create 1 account themselves
let feePayerUpdate = AccountUpdate.createSigned(feePayerAddress);
feePayerUpdate.balance.subInPlace(accountFee.mul(2));
Expand All @@ -61,7 +61,7 @@ tic('deploy dex contracts');
tx = await Mina.transaction(feePayerAddress, () => {
// pay fees for creating 3 dex accounts
AccountUpdate.createSigned(feePayerAddress).balance.subInPlace(
accountFee.mul(3)
Mina.getNetworkConstants().accountCreationFee.mul(3)
);
dex.deploy();
dexTokenHolderX.deploy();
Expand Down
4 changes: 2 additions & 2 deletions src/examples/zkapps/dex/happy-path-with-proofs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ let Local = Mina.LocalBlockchain({
enforceTransactionLimits: false,
});
Mina.setActiveInstance(Local);
let accountFee = Mina.accountCreationFee();
let [{ privateKey: feePayerKey, publicKey: feePayerAddress }] =
Local.testAccounts;
let tx, balances, oldBalances;
Expand Down Expand Up @@ -46,6 +45,7 @@ let dexTokenHolderY = new DexTokenHolder(addresses.dex, tokenIds.Y);

tic('deploy & init token contracts');
tx = await Mina.transaction(feePayerAddress, () => {
const accountFee = Mina.getNetworkConstants().accountCreationFee;
// pay fees for creating 2 token contract accounts, and fund them so each can create 1 account themselves
let feePayerUpdate = AccountUpdate.createSigned(feePayerAddress);
feePayerUpdate.balance.subInPlace(accountFee.mul(2));
Expand All @@ -63,7 +63,7 @@ tic('deploy dex contracts');
tx = await Mina.transaction(feePayerAddress, () => {
// pay fees for creating 3 dex accounts
AccountUpdate.createSigned(feePayerAddress).balance.subInPlace(
accountFee.mul(3)
Mina.getNetworkConstants().accountCreationFee.mul(3)
);
dex.deploy();
dexTokenHolderX.deploy();
Expand Down
16 changes: 11 additions & 5 deletions src/examples/zkapps/dex/run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ let Local = Mina.LocalBlockchain({
enforceTransactionLimits: false,
});
Mina.setActiveInstance(Local);
let accountFee = Mina.accountCreationFee();
let [{ privateKey: feePayerKey, publicKey: feePayerAddress }] =
Local.testAccounts;
let tx, balances, oldBalances;
Expand Down Expand Up @@ -77,6 +76,7 @@ async function main({ withVesting }: { withVesting: boolean }) {

console.log('deploy & init token contracts...');
tx = await Mina.transaction(feePayerAddress, () => {
const accountFee = Mina.getNetworkConstants().accountCreationFee;
// pay fees for creating 2 token contract accounts, and fund them so each can create 2 accounts themselves
let feePayerUpdate = AccountUpdate.fundNewAccount(feePayerAddress, 2);
feePayerUpdate.send({ to: addresses.tokenX, amount: accountFee.mul(2) });
Expand Down Expand Up @@ -110,7 +110,10 @@ async function main({ withVesting }: { withVesting: boolean }) {

console.log('transfer tokens to user');
tx = await Mina.transaction(
{ sender: feePayerAddress, fee: accountFee.mul(1) },
{
sender: feePayerAddress,
fee: Mina.getNetworkConstants().accountCreationFee.mul(1),
},
() => {
let feePayer = AccountUpdate.fundNewAccount(feePayerAddress, 4);
feePayer.send({ to: addresses.user, amount: 20e9 }); // give users MINA to pay fees
Expand All @@ -133,7 +136,10 @@ async function main({ withVesting }: { withVesting: boolean }) {
// supply the initial liquidity where the token ratio can be arbitrary
console.log('supply liquidity -- base');
tx = await Mina.transaction(
{ sender: feePayerAddress, fee: accountFee },
{
sender: feePayerAddress,
fee: Mina.getNetworkConstants().accountCreationFee,
},
() => {
AccountUpdate.fundNewAccount(feePayerAddress);
dex.supplyLiquidityBase(UInt64.from(10_000), UInt64.from(10_000));
Expand Down Expand Up @@ -437,7 +443,7 @@ async function main({ withVesting }: { withVesting: boolean }) {
);
tx = await Mina.transaction(addresses.user2, () => {
AccountUpdate.createSigned(addresses.user2).balance.subInPlace(
accountFee.mul(2)
Mina.getNetworkConstants().accountCreationFee.mul(2)
);
dex.redeemLiquidity(UInt64.from(USER_DL));
dex.redeemLiquidity(UInt64.from(USER_DL));
Expand All @@ -451,7 +457,7 @@ async function main({ withVesting }: { withVesting: boolean }) {
console.log('user2 redeem liquidity');
tx = await Mina.transaction(addresses.user2, () => {
AccountUpdate.createSigned(addresses.user2).balance.subInPlace(
accountFee.mul(2)
Mina.getNetworkConstants().accountCreationFee.mul(2)
);
dex.redeemLiquidity(UInt64.from(USER_DL));
});
Expand Down
6 changes: 4 additions & 2 deletions src/examples/zkapps/dex/run_live.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ const network = Mina.Network({
lightnetAccountManager: 'http://localhost:8181',
});
Mina.setActiveInstance(network);
let accountFee = Mina.accountCreationFee();

let tx, pendingTx: Mina.TransactionId, balances, oldBalances;

Expand Down Expand Up @@ -74,6 +73,7 @@ let userSpec = { sender: addresses.user, fee: 0.1e9 };
if (successfulTransactions <= 0) {
tic('deploy & init token contracts');
tx = await Mina.transaction(senderSpec, () => {
const accountFee = Mina.getNetworkConstants().accountCreationFee;
// pay fees for creating 2 token contract accounts, and fund them so each can create 1 account themselves
let feePayerUpdate = AccountUpdate.createSigned(sender);
feePayerUpdate.balance.subInPlace(accountFee.mul(2));
Expand All @@ -97,7 +97,9 @@ if (successfulTransactions <= 1) {
tic('deploy dex contracts');
tx = await Mina.transaction(senderSpec, () => {
// pay fees for creating 3 dex accounts
AccountUpdate.createSigned(sender).balance.subInPlace(accountFee.mul(3));
AccountUpdate.createSigned(sender).balance.subInPlace(
Mina.getNetworkConstants().accountCreationFee.mul(3)
);
dex.deploy();
dexTokenHolderX.deploy();
tokenX.approveUpdate(dexTokenHolderX.self);
Expand Down
Loading

0 comments on commit 1486b30

Please sign in to comment.