@leofcoin/workers 1.4.8 → 1.4.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@leofcoin/workers",
3
- "version": "1.4.8",
3
+ "version": "1.4.10",
4
4
  "description": "Workers used in @leofcoin/chain",
5
5
  "type": "module",
6
6
  "exports": {
@@ -22,8 +22,6 @@ const run = async (blocks) => {
22
22
  const hash = await block.hash()
23
23
  const index = block.decoded.index
24
24
  console.log(`loaded block: ${hash} @${index} ${formatBytes(size)}`)
25
- // todo we don't want this, need shared state
26
- block.decoded.transactions = block.decoded.transactions.sort((a, b) => a.nonce - b.nonce)
27
25
  resolve({ ...block.decoded, hash, blockInfo: { hash, size, index } })
28
26
  })
29
27
  )
@@ -101,6 +101,35 @@ const createMessage = (sender = globalThis.peerid) => {
101
101
  }
102
102
  }
103
103
 
104
+ const _executeTransaction = async (transaction) => {
105
+ const hash = await new TransactionMessage(transaction).hash()
106
+ if (latestTransactions.includes(hash)) {
107
+ throw new Error(`double transaction found: ${hash} in block ${block.index}`)
108
+ } else {
109
+ latestTransactions.push(hash)
110
+ const { from, to, method, params, nonce } = transaction
111
+ globalThis.msg = createMessage(from)
112
+
113
+ await _.execute({ contract: to, method, params })
114
+ if (to === nativeToken) {
115
+ nativeCalls += 1
116
+ if (method === 'burn') nativeBurns += 1
117
+ if (method === 'mint') nativeMints += 1
118
+ if (method === 'transfer') nativeTransfers += 1
119
+ }
120
+ totalTransactions += 1
121
+
122
+ worker.postMessage({
123
+ type: 'transactionLoaded',
124
+ result: {
125
+ hash,
126
+ from,
127
+ nonce: String(nonce)
128
+ }
129
+ })
130
+ }
131
+ }
132
+
104
133
  _.init = async (message) => {
105
134
  let { contracts, peerid } = message
106
135
  globalThis.peerid = peerid
@@ -146,38 +175,20 @@ _.init = async (message) => {
146
175
  const transactionCount = blocks[block.index - 1].transactions.length
147
176
  latestTransactions.splice(-(transactionCount - 1), latestTransactions.length)
148
177
  }
149
- await Promise.all(
150
- block.transactions.map(async (transaction) => {
151
- if (!block.loaded) {
152
- const hash = await new TransactionMessage(transaction).hash()
153
- if (latestTransactions.includes(hash)) {
154
- throw new Error(`double transaction found: ${hash} in block ${block.index}`)
155
- } else {
156
- latestTransactions.push(hash)
157
- const { from, to, method, params, nonce } = transaction
158
- globalThis.msg = createMessage(from)
159
-
160
- await _.execute({ contract: to, method, params })
161
- if (to === nativeToken) {
162
- nativeCalls += 1
163
- if (method === 'burn') nativeBurns += 1
164
- if (method === 'mint') nativeMints += 1
165
- if (method === 'transfer') nativeTransfers += 1
166
- }
167
- totalTransactions += 1
168
-
169
- worker.postMessage({
170
- type: 'transactionLoaded',
171
- result: {
172
- hash,
173
- from,
174
- nonce: String(nonce)
175
- }
176
- })
177
- }
178
- }
179
- })
180
- )
178
+
179
+ if (!block.loaded) {
180
+ const priority = block.transactions.filter((transaction) => transaction.priority)
181
+ if (priority.length > 0)
182
+ await Promise.all(
183
+ priority.sort((a, b) => a.nonce - b.nonce).map((transaction) => _executeTransaction(transaction))
184
+ )
185
+
186
+ await Promise.all(
187
+ block.transactions
188
+ .filter((transaction) => !transaction.priority)
189
+ .map(async (transaction) => _executeTransaction(transaction))
190
+ )
191
+ }
181
192
  block.loaded = true
182
193
  worker.postMessage({
183
194
  type: 'debug',