Skip to content

Commit

Permalink
machine, machine-worker: add info to states
Browse files Browse the repository at this point in the history
  • Loading branch information
VandeurenGlenn committed Apr 26, 2024
1 parent 6b0e1a5 commit af8f10f
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 6 deletions.
47 changes: 45 additions & 2 deletions packages/chain/src/machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
]

Expand Down Expand Up @@ -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 })
Expand All @@ -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
}
Expand Down Expand Up @@ -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 })
}
Expand Down
35 changes: 31 additions & 4 deletions packages/workers/src/machine-worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit af8f10f

Please sign in to comment.