Skip to content

Commit

Permalink
Working benchmark script
Browse files Browse the repository at this point in the history
Signed-off-by: Shreevatsa N <[email protected]>
  • Loading branch information
vatsa287 committed Mar 11, 2024
1 parent 690ca14 commit d40117c
Showing 1 changed file with 47 additions and 106 deletions.
153 changes: 47 additions & 106 deletions demo/src/asset-working-benchmark.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,8 @@ import * as Cord from "@cord.network/sdk";
import { addNetworkMember } from "./utils/createAuthorities.js";
import { createAccount } from "./utils/createAccount.js";
import { createDid } from "./utils/generateDid";

// import {
// buildFromAssetProperties,
// failproofSubmit,
// buildFromAssetIssueProperties,
// } from "./utils/assets.js";

// import "dotenv/config";

// import { AssetTypeOf, IAssetProperties } from "./utils/asset-types.js";
import { BN } from '@polkadot/util'
import { uriToIdentifier } from '@cord.network/identifier'

import moment from "moment";
import { Address } from '../../packages/utils/lib/esm/Crypto';
Expand All @@ -32,17 +24,20 @@ async function main() {
return -1;
}
console.log("Env Variables: ", networkAddress, anchorUri);

// Temporarily suppress console.log
let originalConsoleLog = console.log;
console.log = () => {};
Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK });

await Cord.connect(networkAddress);
const api = Cord.ConfigService.get("api");

// Restore console.log
console.log = originalConsoleLog;
const txCount = 100;
const perBlock = 100;
const txCount = 20000;
const perBlock = 300;

// Step 1: Setup Identities
console.log(`\n❄️ Identities`);
const networkAuthorityIdentity = Cord.Utils.Crypto.makeKeypairFromUri(
Expand All @@ -62,15 +57,6 @@ async function main() {
`🏛 Issuer (${issuerDid?.assertionMethod![0].type}): ${issuerDid.uri}`
)

// const { mnemonic: holderMnemonic, document: holderDid } = await createDid(
// networkAuthorityIdentity
// )
// const holderKeys = Cord.Utils.Keys.generateKeypairs(holderMnemonic)
// console.log(
// `🏛 Holder (${holderDid?.assertionMethod![0].type}): ${holderDid.uri}`
// )


// Step 2: Create a new Chain Space
console.log(`\n❄️ Chain Space Creation `)
const spaceProperties = await Cord.ChainSpace.buildFromProperties(
Expand Down Expand Up @@ -105,9 +91,9 @@ async function main() {
)
console.log(`✅ Chain Space Approved`)


// Step 3: Create assets on-chain
console.log = () => {};

/* Check which options one needs: note below only agree */
Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_READY });
//Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_IN_BLOCK});
Expand Down Expand Up @@ -145,7 +131,6 @@ async function main() {
}),
)


console.log("✅ Asset created!");

console.log(`\n❄️ Transaction Benchmarking `);
Expand All @@ -159,33 +144,23 @@ async function main() {
for (let k = 0; k < perBlock; k++) {
const { account: holderIdentity } = createAccount();

// const { mnemonic: holderMnemonic, document: holderDid } = await createDid(
// networkAuthorityIdentity
// )
// const holderKeys = Cord.Utils.Keys.generateKeypairs(holderMnemonic)
// console.log(
// `🏛 Holder (${holderDid?.assertionMethod![0].type}): ${holderDid.uri}`
// )

const assetIssuance = await Cord.Asset.buildFromIssueProperties(
assetEntry.uri,
`did:cord:${holderIdentity.address}`,
1,
issuerDid.uri,
space.uri,
);

const issueExtrinsic = await Cord.Asset.prepareExtrinsic(
assetIssuance,
issuerIdentity,
space.authorization,
async ({ data }) => ({
signature: issuerKeys.authentication.sign(data),
keyType: issuerKeys.authentication.type,
}),

const authorizationId: Cord.AuthorizationId = uriToIdentifier(space.authorization)

const tx = await api.tx.asset.issue(
assetIssuance.entry,
assetIssuance.digest,
authorizationId
)

tx_batch1.push(issueExtrinsic);
tx_batch1.push(tx);

process.stdout.write(
" 🔖 Preparing " +
Expand All @@ -196,81 +171,48 @@ async function main() {
);
}
tx_batch[j] = tx_batch1;
console.log("j ", j);
}
} catch (e: any) {
console.log(e.errorCode, "-", e.message);
}
console.log("\n");

// console.log = () => {};
// Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_FINALIZED });
// console.log = originalConsoleLog;
// let batchAncStartTime = moment();
// let promises = [];
// for (let j = 0; j < tx_batch.length; j++) {
// try {
// const tx = await api.tx.utility.batchAll(tx_batch[j]);
// await tx.signAsync(issuerIdentity, {nonce: -1})

// // const authorizedBatch = await Cord.Did.authorizeBatch({
// // batchFunction: api.tx.utility.batchAll,
// // did: issuerDid.uri,
// // extrinsics: tx_batch[j],
// // sign: async ({ data }) => ({
// // signature: issuerKeys.authentication.sign(data),
// // keyType: issuerKeys.authentication.type,
// // }),
// // submitter: issuerIdentity.address
// // })

// const send = new Promise((resolve) => tx.send((result) => {
// if (result.status.isReady)
// //if (result.isInBlock)
// //if (result.isFinalized)
// return resolve(true);
// }));
// promises.push(send);

// //await Cord.Chain.signAndSubmitTx(tx, issuerIdentity);
// } catch (e: any) {
// console.log(e.errorCode, "-", e.message);
// }
// }
// await Promise.all(promises);

/* attempt without loop */
let extSignCallback: Cord.SignExtrinsicCallback = async ({ data }) => ({
signature: issuerKeys.authentication.sign(data),
keyType: issuerKeys.authentication.type,
})

const authorizedBatch = await Cord.Did.authorizeBatch({
batchFunction: api.tx.utility.batchAll,
did: issuerDid.uri,
extrinsics: tx_batch,
sign: extSignCallback,
submitter: issuerIdentity,
})

let batchAncStartTime = moment()
//Cord.ConfigService.set({ submitTxResolveOn: Cord.Chain.IS_FINALIZED });
console.log = originalConsoleLog;
let batchAncStartTime = moment();

let promises = [];
for (let j = 0; j < tx_batch.length; j++) {
try {
// await Cord.Chain.signAndSubmitTx(authorizedBatch, networkAddress, {
// resolveOn: Cord.Chain.IS_READY,
// rejectOn: Cord.Chain.IS_ERROR,
// })
let extSignCallback: Cord.SignExtrinsicCallback = async ({ data }) => ({
signature: issuerKeys.authentication.sign(data),
keyType: issuerKeys.authentication.type,
})

/* Use nonce when authorizing tx in batch(loop) */
const authorizedBatch = await Cord.Did.authorizeBatch({
batchFunction: api.tx.utility.batchAll,
did: issuerDid.uri,
nonce: new BN(j+3),
extrinsics: tx_batch[j],
sign: extSignCallback,
submitter: issuerIdentity.address
})

await Cord.Chain.signAndSubmitTx(authorizedBatch, issuerIdentity)
await authorizedBatch.signAsync(issuerIdentity, {nonce: 2+j});
const send:any = new Promise((resolve) => authorizedBatch.send((result) => {
if (result.status.isReady)
//if (result.isInBlock)
//if (result.isFinalized)
return resolve(true);
}));
promises.push(send);

} catch (e: any) {
console.log(e.errorCode, '-', e.message)
console.log(e.errorCode, "-", e.message);
}

// let batchAncEndTime = moment()
// var batchAncDuration = moment.duration(
// batchAncEndTime.diff(batchAncStartTime)
// )
}
await Promise.all(promises);

var batchAncDuration = moment
.duration(moment().diff(batchAncStartTime))
Expand All @@ -289,8 +231,7 @@ async function main() {

await sleep(1000);
// await sleep(10000);
// await sleep(10000);
// await sleep(10000);

await api.disconnect();
}
main()
Expand Down

0 comments on commit d40117c

Please sign in to comment.