Skip to content

Commit

Permalink
Merge branch 'main' into feature/expose-proof-base64
Browse files Browse the repository at this point in the history
  • Loading branch information
mitschabaude committed Nov 28, 2024
2 parents 577d5e7 + cb0b270 commit 66df4f2
Show file tree
Hide file tree
Showing 4 changed files with 226 additions and 6 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,18 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Added

- Add `enforceTransactionLimits` parameter on Network https://github.com/o1-labs/o1js/issues/1910
- Expose low-level conversion methods `Proof.{_proofToBase64,_proofFromBase64}` https://github.com/o1-labs/o1js/pull/1928

### Fixed

- Compiling stuck in the browser for recursive zkprograms https://github.com/o1-labs/o1js/pull/1906
- Error message in `rangeCheck16` gadget https://github.com/o1-labs/o1js/pull/1920

### Added

- Method for optional types to assert none https://github.com/o1-labs/o1js/pull/1922

## [2.1.0](https://github.com/o1-labs/o1js/compare/b04520d...e1bac02) - 2024-11-13

### Added
Expand Down
202 changes: 202 additions & 0 deletions src/lib/mina/mina.network.unit-test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import {
State,
state,
UInt64,
Bool,
SmartContract,
Mina,
AccountUpdate,
method,
PublicKey,
Permissions,
VerificationKey,
Field,
Int64,
TokenId,
TokenContract as TokenContractBase,
AccountUpdateForest,
PrivateKey,
} from 'o1js';
import { test, describe, it, before } from 'node:test';
import { expect } from 'expect';



const defaultNetwork = Mina.Network({
networkId: "testnet",
mina: "https://example.com/graphql",
archive: "https://example.com//graphql"
});

const enforcedNetwork = Mina.Network({
networkId: "testnet",
mina: "https://example.com/graphql",
archive: "https://example.com//graphql",
bypassTransactionLimits: false
});

const unlimitedNetwork = Mina.Network({
networkId: "testnet",
mina: "https://unlimited.com/graphql",
archive: "https://unlimited.com//graphql",
bypassTransactionLimits: true
});

describe('Test default network', () => {
let bobAccount: PublicKey,
bobKey: PrivateKey;

before(async () => {

Mina.setActiveInstance(defaultNetwork);
bobKey = PrivateKey.random();
bobAccount = bobKey.toPublicKey();
});


it('Simple account update', async () => {

let txn = await Mina.transaction(async () => {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(1));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
});
await txn.prove();
await txn.sign([bobKey]).safeSend();

});

it('Multiple account update', async () => {

let txn = await Mina.transaction(async () => {
for (let index = 0; index < 2; index++) {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(index));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
}
});
await txn.prove();
await txn.sign([bobKey]).safeSend();

});

it('More than limit account update', async () => {

let txn = await Mina.transaction(async () => {
for (let index = 0; index < 12; index++) {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(index));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
}
});
await txn.prove();
// failure with default bypassTransactionLimits value
await expect(txn.sign([bobKey]).safeSend()).rejects.toThrow();
});
});

describe('Test enforced network', () => {
let bobAccount: PublicKey,
bobKey: PrivateKey;

before(async () => {

Mina.setActiveInstance(enforcedNetwork);
bobKey = PrivateKey.random();
bobAccount = bobKey.toPublicKey();
});


it('Simple account update', async () => {

let txn = await Mina.transaction(async () => {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(1));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
});
await txn.prove();
await txn.sign([bobKey]).safeSend();

});

it('Multiple account update', async () => {

let txn = await Mina.transaction(async () => {
for (let index = 0; index < 2; index++) {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(index));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
}
});
await txn.prove();
await txn.sign([bobKey]).safeSend();

});

it('More than limit account update', async () => {

let txn = await Mina.transaction(async () => {
for (let index = 0; index < 12; index++) {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(index));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
}
});
await txn.prove();
// failure with bypassTransactionLimits = false
await expect(txn.sign([bobKey]).safeSend()).rejects.toThrow();
});
});

describe('Test unlimited network', () => {
let bobAccount: PublicKey,
bobKey: PrivateKey;

before(async () => {

Mina.setActiveInstance(unlimitedNetwork);
bobKey = PrivateKey.random();
bobAccount = bobKey.toPublicKey();
});


it('Simple account update', async () => {

let txn = await Mina.transaction(async () => {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(1));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
});
await txn.prove();
await txn.sign([bobKey]).safeSend();

});

it('Multiple account update', async () => {

let txn = await Mina.transaction(async () => {
for (let index = 0; index < 2; index++) {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(index));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
}
});
await txn.prove();
await txn.sign([bobKey]).safeSend();

});

it('More than limit account update', async () => {

let txn = await Mina.transaction(async () => {
for (let index = 0; index < 12; index++) {
const accountUpdateBob = AccountUpdate.create(bobAccount, Field.from(index));
accountUpdateBob.account.balance.requireEquals(UInt64.zero);
accountUpdateBob.balance.addInPlace(UInt64.one);
}
});
await txn.prove();
// success with bypassTransactionLimits = true
await txn.sign([bobKey]).safeSend();
});
});
20 changes: 14 additions & 6 deletions src/lib/mina/mina.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,21 +104,24 @@ function Network(options: {
mina: string | string[];
archive?: string | string[];
lightnetAccountManager?: string;
bypassTransactionLimits?: boolean;
}): Mina;
function Network(
options:
| {
networkId?: NetworkId;
mina: string | string[];
archive?: string | string[];
lightnetAccountManager?: string;
}
networkId?: NetworkId;
mina: string | string[];
archive?: string | string[];
lightnetAccountManager?: string;
bypassTransactionLimits?: boolean;
}
| string
): Mina {
let minaNetworkId: NetworkId = 'testnet';
let minaGraphqlEndpoint: string;
let archiveEndpoint: string;
let lightnetAccountManagerEndpoint: string;
let enforceTransactionLimits: boolean = true;

if (options && typeof options === 'string') {
minaGraphqlEndpoint = options;
Expand Down Expand Up @@ -158,6 +161,11 @@ function Network(
lightnetAccountManagerEndpoint = options.lightnetAccountManager;
Fetch.setLightnetAccountManagerEndpoint(lightnetAccountManagerEndpoint);
}

if (options.bypassTransactionLimits !== undefined &&
typeof options.bypassTransactionLimits === 'boolean') {
enforceTransactionLimits = !options.bypassTransactionLimits;
}
} else {
throw new Error(
"Network: malformed input. Please provide a string or an object with 'mina' and 'archive' endpoints."
Expand Down Expand Up @@ -251,7 +259,7 @@ function Network(
},
sendTransaction(txn) {
return toPendingTransactionPromise(async () => {
verifyTransactionLimits(txn.transaction);
if (enforceTransactionLimits) verifyTransactionLimits(txn.transaction);

let [response, error] = await Fetch.sendZkapp(txn.toJSON());
let errors: string[] = [];
Expand Down
5 changes: 5 additions & 0 deletions src/lib/provable/option.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export { Option, OptionOrValue };

type Option<T, V = any> = { isSome: Bool; value: T } & {
assertSome(message?: string): T;
assertNone(message?: string): void;
orElse(defaultValue: T | V): T;
};

Expand Down Expand Up @@ -104,6 +105,10 @@ function Option<A extends ProvableType>(
return this.value;
}

assertNone(message?: string): void {
this.isSome.assertFalse(message);
}

static from(value?: V | T) {
return value === undefined
? new Option_({
Expand Down

0 comments on commit 66df4f2

Please sign in to comment.