Skip to content

Commit

Permalink
Feature/cold storage (#34)
Browse files Browse the repository at this point in the history
* Cold storage support

* Fix client initialization

* .gitignore issues

* Merge branch 'feature/sync-stat' into feature/cold-storage

# Conflicts:
#	src/account.ts
#	src/index.ts
  • Loading branch information
EvgenKor authored Dec 19, 2022
1 parent 69bec49 commit 22a44b5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ dist
.cache
/package-lock.json
*.DS_Store
assets
yarn-error.log
14 changes: 13 additions & 1 deletion src/account.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import AES from 'crypto-js/aes';
import Utf8 from 'crypto-js/enc-utf8';
import { EthereumClient, PolkadotClient, Client as NetworkClient } from 'zeropool-support-js';
import { init, ZkBobClient, HistoryRecord, TransferConfig, FeeAmount, TxType, PoolLimits, InitLibCallback, TreeState, EphemeralAddress } from 'zkbob-client-js';
import { init, ZkBobClient, HistoryRecord, TransferConfig, FeeAmount, TxType,
PoolLimits, InitLibCallback, TreeState, EphemeralAddress, SyncStat } from 'zkbob-client-js';
import bip39 from 'bip39-light';
import HDWalletProvider from '@truffle/hdwallet-provider';
import { deriveSpendingKeyZkBob } from 'zkbob-client-js/lib/utils';
Expand Down Expand Up @@ -95,13 +96,16 @@ export default class Account {

const sk = deriveSpendingKeyZkBob(mnemonic, networkType);
this.client = client;

const bulkConfigUrl = `./assets/zkbob-${NETWORK}-coldstorage.cfg`
this.zpClient = await ZkBobClient.create({
sk,
worker,
tokens: {
[TOKEN_ADDRESS]: {
poolAddress: CONTRACT_ADDRESS,
relayerUrl: RELAYER_URL,
coldStorageConfigPath: bulkConfigUrl,
}
},
networkName: NETWORK,
Expand Down Expand Up @@ -234,6 +238,14 @@ export default class Account {
return this.zpClient.getPoolState(TOKEN_ADDRESS);
}

public async getStatFullSync(): Promise<SyncStat | undefined> {
return this.zpClient.getStatFullSync();
}

public async getAverageTimePerTx(): Promise<number | undefined> {
return this.zpClient.getAverageTimePerTx();
}

public async getEphemeralAddress(index: number): Promise<EphemeralAddress> {
return this.zpClient.getEphemeralAddress(TOKEN_ADDRESS, index);
}
Expand Down
25 changes: 25 additions & 0 deletions src/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,31 @@ export async function getRoot() {
});
}

export async function getStateSyncStatistic() {
this.pause();
const fullSyncStat = await this.account.getStatFullSync();
const avgTimePerTx = await this.account.getAverageTimePerTx();

if (fullSyncStat !== undefined) {
this.echo(`Full state sync: [[;white;]${fullSyncStat.totalTime / 1000} sec]`);
this.echo(` average speed: [[;white;]${fullSyncStat.timePerTx.toFixed(1)} msec/tx]`);
this.echo(` total number of tx: [[;white;]${fullSyncStat.txCount}]`);
this.echo(` number of tx [CDN]: [[;white;]${fullSyncStat.cdnTxCnt}]`);
this.echo(` decrypted items: [[;white;]${fullSyncStat.decryptedLeafs}]`);

} else {
this.echo(`Full state: [[;white;]N/A]`);
}

if (avgTimePerTx !== undefined) {
this.echo(`Average sync speed: [[;white;]${avgTimePerTx.toFixed(1)} msec/tx]`);
} else {
this.echo(`Average sync speed: [[;white;]N/A]`);
}

this.resume();
}

export async function getEphemeral(index: string) {
this.pause();
let idx;
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ const COMMANDS: { [key: string]: [(...args) => void, string, string] } = {
'shielded-address-info': [c.shieldedAddressInfo, '<shielded address>', 'get all available info for the shielded address'],
'internal-state': [c.getInternalState, '', 'print your account and incoming notes'],
'root': [c.getRoot, '', 'print local and remote Merkle tree root'],
'sync-stat': [c.getStateSyncStatistic, '', 'print available state synchronization statistics from the library'],
'get-ephemeral-address': [c.getEphemeral, '[index]', 'get the concrete ephemeral address or show first unused one'],
'get-ephemeral-used': [c.getEphemeralUsed, '', 'show used ephemeral addresses'],
'get-ephemeral-address-privkey': [c.getEphemeralPrivKey, '<index>', 'get private key for concrete ephemeral address'],
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7500,6 +7500,11 @@ regenerator-runtime@^0.13.11, regenerator-runtime@^0.13.7, regenerator-runtime@^
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==

regenerator-runtime@^0.13.9:
version "0.13.11"
resolved "https://registry.yarnpkg.com/regenerator-runtime/-/regenerator-runtime-0.13.11.tgz#f6dca3e7ceec20590d07ada785636a90cdca17f9"
integrity sha512-kY1AZVr2Ra+t+piVaJ4gxaFaReZVH40AKNo7UCX6W+dEwBo/2oZJzqfuN1qLq1oL45o56cPaTXELwrTh8Fpggg==

regex-not@^1.0.0, regex-not@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/regex-not/-/regex-not-1.0.2.tgz#1f4ece27e00b0b65e0247a6810e6a85d83a5752c"
Expand Down

0 comments on commit 22a44b5

Please sign in to comment.