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

Rebenchmark mantaSbt #1192

Merged
merged 13 commits into from
Aug 28, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
85 changes: 45 additions & 40 deletions pallets/manta-sbt/src/bin/precompute_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ use rand_chacha::ChaCha20Rng;
use scale_codec::Encode;
use std::{
env,
fs::{self, OpenOptions},
fs::{self, File, OpenOptions},
io::Write,
path::PathBuf,
thread,
};
use std::sync::Arc;
/// UTXO Accumulator for Building Circuits
type UtxoAccumulator =
TreeArrayMerkleForest<MerkleTreeConfiguration, Full<MerkleTreeConfiguration>, 256>;
Expand Down Expand Up @@ -79,6 +81,9 @@ macro_rules! write_const_array {
/// Builds sample transactions for testing.
#[inline]
fn main() -> Result<()> {
use std::time::Instant;
let now = Instant::now();

let target_file = env::args()
.nth(1)
.map(PathBuf::from)
Expand All @@ -96,51 +101,51 @@ fn main() -> Result<()> {
let directory = tempfile::tempdir().expect("Unable to generate temporary test directory.");
println!("[INFO] Temporary Directory: {directory:?}");

let mut rng = ChaCha20Rng::from_seed([0; 32]);
let mut rng = Arc::new(ChaCha20Rng::from_seed([0; 32]));
let (proving_context, _, parameters, utxo_accumulator_model) =
load_parameters(directory.path()).expect("Unable to load parameters.");
let mut utxo_accumulator = UtxoAccumulator::new(utxo_accumulator_model);
let asset_id = 1.into();

let to_private = to_private_example(
&proving_context.to_private,
&parameters,
&mut utxo_accumulator,
asset_id,
1,
&mut rng,
);

let step = 20;
let mut threads = vec![];
for i in 0..4 {
let start = i * step;
let end = step + start;
let utxo = utxo_accumulator_model.clone();
let mut utxo_accumulator = Arc::new(UtxoAccumulator::new(utxo));
let mut r = rng.clone();
let context = Arc::new(proving_context.clone());
let params = Arc::new(parameters.clone());
let thread_join_handle = thread::spawn(move || {
let mut mints = Vec::new();
for i in start..end {
let asset_id = (0 + (i % 3)).into();
println!("Iteration count: {:?}", i);

let mut target_file = OpenOptions::new()
.create_new(true)
.write(true)
.open(target_file)?;
let to_private = to_private_example(
&context.to_private,
&params,
&mut *Arc::make_mut(&mut utxo_accumulator),
asset_id,
1,
&mut *Arc::make_mut(&mut r)
);
mints.push(to_private.clone());
println!("to_private size: {:?}", to_private.encode().len());
}
let path = format!("/home/jamie/my-repo/Manta/pallets/manta-sbt/precomputed_mints-{}-to_{}", start, end);
let mut file = File::create(path).unwrap();
file.write_all(&<[TransferPost]>::encode(&mints)).unwrap();
});
threads.push(thread_join_handle);
}

writeln!(
target_file,
indoc! {r"
// Copyright 2020-2023 Manta Network.
// This file is part of Manta.
//
// Manta is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Manta is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Manta. If not, see <http://www.gnu.org/licenses/>.
for t in threads {
t.join();
}

//! Precomputed Coins
//!
//! THIS FILE IS AUTOMATICALLY GENERATED by `src/bin/precompute_coins.rs`. DO NOT EDIT.
"}
)?;
let elapsed = now.elapsed();
println!("Elapsed: {:.2?}", elapsed);

write_const_array!(target_file, TO_PRIVATE, to_private)?;
Ok(directory.close()?)
}
161 changes: 161 additions & 0 deletions tests/mantaSbt_toprivate.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
const fs = require('fs').promises;
const { Keyring } = require('@polkadot/keyring');
const { ApiPromise, WsProvider } = require('@polkadot/api');

const keyring = new Keyring({
type: 'sr25519'
});
const nodeAddress = "ws://127.0.0.1:9988";

async function createPromiseApi(nodeAddress) {
const wsProvider = new WsProvider(nodeAddress);
const api = await ApiPromise.create({
provider: wsProvider
});
await api.isReady;
return api;
}

async function main() {
const api = await createPromiseApi(nodeAddress);
const sender = keyring.addFromMnemonic("bottom drive obey lake curtain smoke basket hold race lonely fit walk//Alice");

const mints_file = '/home/jamie/my-repo/Manta/pallets/manta-sbt/precomputed_mints-0-to_20';
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved

const mints_buffer = await fs.readFile(mints_file);

const mints_offset = 1;
const total_iterations = 20;
const mint_size = 553;

const transactions = [];

// forceToPrivate
// set next id to how many coins
for (let i = 0; i < total_iterations; i++) {
const mints_start = mints_offset + i * mint_size;
const mint = api.tx.mantaSbt.forceToPrivate(
mints_buffer.subarray(mints_start, mints_start + mint_size),
0,
'123',
'dmyLrsKhggNctvRPNWWyAgAhvtr8JrnsRBdUwoKzLkdyc2cza');
transactions.push(mint);

await api.tx.utility.forceBatch(transactions).signAndSend(sender, {
nonce: -1
}, ({
events = [],
status
}) => {
if (status.isFinalized) {
console.log("tx %i success.", status.nonce);
}
if (status.isDropped || status.isUsurped || status.isFinalityTimeout || status.isRetracted) {
console.log(`tx %i ${status.type}.`, status.nonce);
}
});

await new Promise(resolve => setTimeout(resolve, 12000));
transactions.length = 0;
}

// to_private
// reserve_account, and after 4 mint, do it again
for (let i = 0; i < total_iterations; i++) {
const mints_start = mints_offset + i * mint_size;
const mint = api.tx.mantaSbt.toPrivate(
null,
null,
null,
mints_buffer.subarray(mints_start, mints_start + mint_size),
'123',
);
transactions.push(mint);

await api.tx.utility.forceBatch(transactions).signAndSend(sender, {
nonce: -1
}, ({
events = [],
status
}) => {
if (status.isFinalized) {
console.log("tx %i success.", status.nonce);
}
if (status.isDropped || status.isUsurped || status.isFinalityTimeout || status.isRetracted) {
console.log(`tx %i ${status.type}.`, status.nonce);
}
});

await new Promise(resolve => setTimeout(resolve, 12000));
transactions.length = 0;
}

// mint_sbt_eth
// allowlist account
for (let i = 0; i < total_iterations; i++) {
const mints_start = mints_offset + i * mint_size;
const mint = api.tx.mantaSbt.mintSbtEth(
mints_buffer.subarray(mints_start, mints_start + mint_size),
0,
'sig',
0,
0,
0,
'123',
);
transactions.push(mint);

await api.tx.utility.forceBatch(transactions).signAndSend(sender, {
nonce: -1
}, ({
events = [],
status
}) => {
if (status.isFinalized) {
console.log("tx %i success.", status.nonce);
}
if (status.isDropped || status.isUsurped || status.isFinalityTimeout || status.isRetracted) {
console.log(`tx %i ${status.type}.`, status.nonce);
}
});

await new Promise(resolve => setTimeout(resolve, 12000));
transactions.length = 0;
}

// force_mint_sbt_eth
for (let i = 0; i < total_iterations; i++) {
const mints_start = mints_offset + i * mint_size;
const mint = api.tx.mantaSbt.forceMintSbtEth(
mints_buffer.subarray(mints_start, mints_start + mint_size),
0,
'sig',
0,
0,
0,
'123',
);
transactions.push(mint);

await api.tx.utility.forceBatch(transactions).signAndSend(sender, {
nonce: -1
}, ({
events = [],
status
}) => {
if (status.isFinalized) {
console.log("tx %i success.", status.nonce);
}
if (status.isDropped || status.isUsurped || status.isFinalityTimeout || status.isRetracted) {
console.log(`tx %i ${status.type}.`, status.nonce);
}
});

await new Promise(resolve => setTimeout(resolve, 12000));
transactions.length = 0;
}

await api.disconnect();
}

main().catch(console.error);
Loading