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

Unlimited account update support #1919

Merged
merged 7 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
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;
enforceTransactionLimits?: 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;
enforceTransactionLimits?: 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.enforceTransactionLimits !== undefined &&
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be cleaner to name the option bypassTransactionLimits. That way undefined and false both resolve to the default behavior. Then I think this could be

enforceTransactionLimits = options.bypassTransactionLimits ? false : true;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to reuse the same name than LocalBlockchain :
async function LocalBlockchain({ proofsEnabled = true, enforceTransactionLimits = true, } = {}) {

enforceTransactionLimits = true,

typeof options.enforceTransactionLimits === 'boolean') {
enforceTransactionLimits = options.enforceTransactionLimits;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (options.enforceTransactionLimits !== undefined &&
typeof options.enforceTransactionLimits === 'boolean') {
enforceTransactionLimits = options.enforceTransactionLimits;
}
const byPassTransactionLimits = options.enforceTransactionLimits === false;
enforceTransactionLimits = !byPassTransactionLimits;

I still think this would be more understandable, while respecting the argument agreement with the other function

} 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
200 changes: 200 additions & 0 deletions src/lib/mina/transaction.test.ts
youtpout marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import {
State,
state,
UInt64,
Bool,
SmartContract,
Mina,
AccountUpdate,
method,
PublicKey,
Permissions,
VerificationKey,
Field,
Int64,
TokenId,
TokenContract as TokenContractBase,
AccountUpdateForest,
PrivateKey,
} from 'o1js';



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",
enforceTransactionLimits: true
});

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

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

beforeAll(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 enforceTransactionLimits
await expect(txn.sign([bobKey]).safeSend()).rejects.toThrow();
});
});

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

beforeAll(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 enforceTransactionLimits = true
await expect(txn.sign([bobKey]).safeSend()).rejects.toThrow();
});
});

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

beforeAll(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 enforceTransactionLimits = false
await txn.sign([bobKey]).safeSend();
});
});
Loading