Skip to content

Commit

Permalink
add network test
Browse files Browse the repository at this point in the history
  • Loading branch information
phn210 committed Apr 5, 2024
1 parent 212b5a6 commit 776e7ea
Show file tree
Hide file tree
Showing 5 changed files with 257 additions and 77 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,6 @@ coverage

# Never commit keys to Git!
keys

# O1JS
caches
4 changes: 0 additions & 4 deletions config.json

This file was deleted.

3 changes: 3 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {

export {
MAX_RETRY,
TX_FEE,
FileSystem,
Profiler,
Logger,
Expand All @@ -28,6 +29,8 @@ export {

const MAX_RETRY = 3;

const TX_FEE = 0.101 * 1e9;

type FileSystem = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
appendFile(...args: any): Promise<void>;
Expand Down
181 changes: 181 additions & 0 deletions src/utils/network.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import {
Cache,
Field,
Mina,
PrivateKey,
Provable,
PublicKey,
Reducer,
SmartContract,
State,
method,
state,
} from 'o1js';
import fs from 'fs/promises';
import {
compile,
deployZkApps,
fetchActions,
fetchEvents,
fetchZkAppState,
prove,
proveAndSendTx,
randomAccounts,
sendTx,
} from './network.js';
import { FeePayer, TX_FEE, ZkApp } from './constants.js';
import { getProfiler } from './benchmark.js';

describe('Network', () => {
class TestContract extends SmartContract {
@state(Field) num = State<Field>();

reducer = Reducer({ actionType: Field });
events = { ['test']: Field };

@method
async test(value1: Field, value2: Field) {
value1.assertEquals(value2);
this.num.set(value1);
this.reducer.dispatch(value1);
this.emitEvent('test', value2);
}
}

const doProofs = true;
const cache = Cache.FileSystem('caches');
const logger = {
info: true,
memoryUsage: true,
};
const profiler = getProfiler('Test', fs);
let feePayer: FeePayer;
let testZkApp: ZkApp;
// const network = Mina.Network({
// mina: 'http://localhost:8080/graphql',
// archive: 'http://localhost:8282',
// lightnetAccountManager: 'http://localhost:8181',
// });
// Mina.setActiveInstance(network);
const Local = Mina.LocalBlockchain({ proofsEnabled: doProofs });
Mina.setActiveInstance(Local);

beforeAll(async () => {
// let accquiredKeys = await Lightnet.listAcquiredKeyPairs({});
// if (accquiredKeys?.length == 0) {
// let privateKey = (await Lightnet.acquireKeyPair()).privateKey;
// let publicKey = privateKey.toPublicKey();
// feePayer = { sender: { privateKey, publicKey } };
// } else {
// feePayer = {
// sender: {
// privateKey: accquiredKeys![0].privateKey,
// publicKey: accquiredKeys![0].publicKey,
// },
// };
feePayer = { sender: Local.testAccounts[0] };
// }
if (true) {
testZkApp = {
key: {
privateKey:
PrivateKey.fromBigInt(
26008585964768579152708118465695424684184602768554064880048813177630307793166n
),
publicKey: PublicKey.fromBase58(
'B62qnh3axyuNcX2VbtRc2EB1731qJRJoFF6H4HVebf381JTwutdypjr'
),
},
name: 'TestContract',
};
} else {
testZkApp = {
key: PrivateKey.randomKeypair(),
name: 'TestContract',
};
}
testZkApp.contract = new TestContract(testZkApp.key.publicKey);
Provable.log('Fee payer:', feePayer.sender);
Provable.log('zkApp:', testZkApp.key);
});

it('should generate random accounts', async () => {
const accountNames = ['test1', 'test2', 'test1'];
const accounts = randomAccounts(accountNames);
expect(Object.entries(accounts).length).toEqual(
new Set(accountNames).size
);
});

it('should compile contract', async () => {
if (doProofs) await compile(TestContract, cache, logger, profiler);
});

it('should deploy zkApp', async () => {
await deployZkApps(
[
{
zkApp: testZkApp,
initArgs: [],
},
],
feePayer
);
});

it('should prove', async () => {
await prove(
TestContract.name,
'test',
(testZkApp.contract as TestContract).test(Field(1), Field(1)),
logger,
profiler
);
});

it('should send tx', async () => {
let tx = await Mina.transaction(
{
sender: feePayer.sender.publicKey,
fee: feePayer.fee || TX_FEE,
memo: feePayer.memo,
nonce: feePayer.nonce,
},
async () =>
(testZkApp.contract as TestContract).test(Field(1), Field(1))
);
await tx.prove();
await sendTx(tx.sign([feePayer.sender.privateKey]), true);
});

it('should prove and send tx', async () => {
await proveAndSendTx(
TestContract.name,
'test',
async () =>
(testZkApp.contract as TestContract).test(Field(1), Field(1)),
feePayer,
logger,
profiler,
true
);
});

it('should fetch actions', async () => {
let actions = await fetchActions(
testZkApp.key.publicKey,
Reducer.initialActionState
);
expect(actions.length).toBeGreaterThan(0);
});

it('should fetch events', async () => {
let events = await fetchEvents(testZkApp.key.publicKey);
expect(events.length).toBeGreaterThan(0);
});

it('should fetch state', async () => {
let state = await fetchZkAppState(testZkApp.key.publicKey);
expect(state.length).toEqual(8);
});
});
Loading

0 comments on commit 776e7ea

Please sign in to comment.