From af8f10f118d81b295cd12cdece373c4cd1f5c179 Mon Sep 17 00:00:00 2001 From: VandeurenGlenn Date: Fri, 26 Apr 2024 16:10:22 +0200 Subject: [PATCH] machine, machine-worker: add info to states --- packages/chain/src/machine.ts | 47 ++++++++++++++++++++++++-- packages/workers/src/machine-worker.js | 35 ++++++++++++++++--- 2 files changed, 76 insertions(+), 6 deletions(-) diff --git a/packages/chain/src/machine.ts b/packages/chain/src/machine.ts index d5d7d085..e5cdc978 100644 --- a/packages/chain/src/machine.ts +++ b/packages/chain/src/machine.ts @@ -16,7 +16,15 @@ export default class Machine { index: 0, hash: '' }, - accounts: {} + accounts: {}, + info: { + nativeCalls: 0, + nativeMints: 0, + nativeBurns: 0, + nativeTransfers: 0, + totalTransactions: 0, + totalBlocks: 0 + } } constructor(blocks) { @@ -116,7 +124,21 @@ export default class Machine { const tasks = [ stateStore.put('lastBlock', JSON.stringify(await this.lastBlock)), stateStore.put('states', JSON.stringify(state)), - stateStore.put('accounts', JSON.stringify(accounts)) + stateStore.put('accounts', JSON.stringify(accounts)), + stateStore.put( + 'info', + JSON.stringify({ + nativeCalls: this.nativeCalls, + nativeMints: this.nativeMints, + nativeBurns: this.nativeBurns, + nativeTransfers: this.nativeTransfers, + totalTransactions: this.totalTransactions, + totalBurnAmount: this.totalBurnAmount, + totaMintAmount: this.totaMintAmount, + totalTransferAmount: this.totalTransferAmount, + totalBlocks: await blockStore.length + }) + ) // accountsStore.clear() ] @@ -158,8 +180,18 @@ export default class Machine { this.states.states = JSON.parse(new TextDecoder().decode(await stateStore.get('states'))) try { this.states.accounts = JSON.parse(new TextDecoder().decode(await stateStore.get('accounts'))) + this.states.info = JSON.parse(new TextDecoder().decode(await stateStore.get('info'))) } catch { this.states.accounts = {} + // todo try fetching info from fully synced peer + this.states.info = { + nativeCalls: 0, + nativeMints: 0, + nativeBurns: 0, + nativeTransfers: 0, + totalTransactions: 0, + totalBlocks: 0 + } } console.log({ balances: this.states.states[addresses.nativeToken].balances }) @@ -171,6 +203,7 @@ export default class Machine { fromState: this.states.lastBlock.index > 0, lastBlock: this.states.lastBlock, state: this.states.states, + info: this.states.info, // @ts-ignore peerid: peernet.peerId } @@ -342,6 +375,16 @@ export default class Machine { return this.#askWorker('totalBlocks') } + get totalBurnAmount() { + return this.#askWorker('totalBurnAmount') + } + get totaMintAmount() { + return this.#askWorker('totaMintAmount') + } + get totalTransferAmount() { + return this.#askWorker('totalTransferAmount') + } + getBlocks(from?, to?): Promise<[]> { return this.#askWorker('blocks', { from, to }) } diff --git a/packages/workers/src/machine-worker.js b/packages/workers/src/machine-worker.js index 0632575c..579d4b73 100644 --- a/packages/workers/src/machine-worker.js +++ b/packages/workers/src/machine-worker.js @@ -135,9 +135,18 @@ const _executeTransaction = async (transaction) => { await _.execute({ contract: to, method, params }) if (to === nativeToken) { nativeCalls += 1 - if (method === 'burn') nativeBurns += 1 - if (method === 'mint') nativeMints += 1 - if (method === 'transfer') nativeTransfers += 1 + if (method === 'burn') { + nativeBurns += 1 + totalBurnAmount += params[0] + } + if (method === 'mint') { + nativeMints += 1 + totalMintAmount += params[0] + } + if (method === 'transfer') { + nativeTransfers += 1 + totalTransferAmount += params[0] + } } totalTransactions += 1 @@ -153,10 +162,19 @@ const _executeTransaction = async (transaction) => { } _.init = async (message) => { - let { peerid, fromState, state } = message + let { peerid, fromState, state, info } = message globalThis.peerid = peerid console.log({ fromState }) if (fromState) { + nativeCalls = info.nativeCalls + nativeBurns = info.nativeBurns + nativeMints = info.nativeMints + nativeTransfers = info.nativeTransfers + totalTransactions = info.totalTransactions + totalBurnAmount = info.totalBurnAmount + totalMintAmount = info.totalMintAmount + totalTransferAmount = info.totalTransferAmount + lastBlock = message.lastBlock const setState = async (address, state) => { const contractBytes = await resolveContract(address) @@ -330,6 +348,15 @@ worker.onmessage(({ id, type, input }) => { case 'nativeTransfers': respond(id, nativeTransfers) break + case 'totalBurnAmount': + respond(id, totalBurnAmount) + break + case 'totalMintAmount': + respond(id, totalMintAmount) + break + case 'totalTransferAmount': + respond(id, totalTransferAmount) + break case 'totalTransfers': respond(id, totalTransfers) break