@leofcoin/chain 1.7.9 → 1.7.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/exports/browser/chain.js +660 -673
- package/exports/browser/workers/machine-worker.js +49 -28
- package/exports/chain.js +4 -0
- package/exports/workers/machine-worker.js +49 -28
- package/package.json +1 -1
|
@@ -150,10 +150,6 @@ const get = ({ contract, method, params }) => {
|
|
|
150
150
|
return result
|
|
151
151
|
};
|
|
152
152
|
|
|
153
|
-
const resolveContract = (hash) => askFor('contract', hash);
|
|
154
|
-
|
|
155
|
-
const resolveTransaction = (hash) => askFor('transaction', hash);
|
|
156
|
-
|
|
157
153
|
const respond = (id, value) => {
|
|
158
154
|
worker.postMessage({
|
|
159
155
|
type: 'response',
|
|
@@ -255,6 +251,13 @@ const _executeTransaction = async (transaction) => {
|
|
|
255
251
|
}
|
|
256
252
|
};
|
|
257
253
|
|
|
254
|
+
const addToWantList = (hash) => {
|
|
255
|
+
worker.postMessage({
|
|
256
|
+
type: 'addToWantList',
|
|
257
|
+
hash
|
|
258
|
+
});
|
|
259
|
+
};
|
|
260
|
+
|
|
258
261
|
_.init = async (message) => {
|
|
259
262
|
let { peerid, fromState, state, info } = message;
|
|
260
263
|
globalThis.peerid = peerid;
|
|
@@ -272,6 +275,10 @@ _.init = async (message) => {
|
|
|
272
275
|
lastBlock = message.lastBlock;
|
|
273
276
|
const setState = async (address, state) => {
|
|
274
277
|
const contractBytes = await resolveContract(address);
|
|
278
|
+
if (contractBytes === address) {
|
|
279
|
+
addToWantList(address);
|
|
280
|
+
return
|
|
281
|
+
}
|
|
275
282
|
const contract = await new ContractMessage(contractBytes);
|
|
276
283
|
|
|
277
284
|
await _.runContract({ hash: address, decoded: contract.decoded, encoded: contract.encoded }, state);
|
|
@@ -341,30 +348,42 @@ _.init = async (message) => {
|
|
|
341
348
|
latestTransactions.splice(-transactionCount, latestTransactions.length);
|
|
342
349
|
}
|
|
343
350
|
if (!block.loaded && !fromState) {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
351
|
+
try {
|
|
352
|
+
const transactions = await Promise.all(
|
|
353
|
+
block.transactions.map(async (transaction) => {
|
|
354
|
+
const message = new TransactionMessage(await resolveTransaction(transaction)).decode();
|
|
355
|
+
if (message === transaction) {
|
|
356
|
+
throw new Error(`nothing found for ${transaction}`)
|
|
357
|
+
}
|
|
358
|
+
})
|
|
359
|
+
);
|
|
360
|
+
const priority = transactions
|
|
361
|
+
.filter((transaction) => transaction.priority)
|
|
362
|
+
?.sort((a, b) => a.nonce - b.nonce);
|
|
363
|
+
if (priority.length > 0)
|
|
364
|
+
for (const transaction of priority) {
|
|
365
|
+
await _executeTransaction(transaction);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
await Promise.all(
|
|
369
|
+
transactions
|
|
370
|
+
.filter((transaction) => !transaction.priority)
|
|
371
|
+
.map(async (transaction) => _executeTransaction(transaction))
|
|
372
|
+
);
|
|
373
|
+
block.loaded = true;
|
|
374
|
+
worker.postMessage({
|
|
375
|
+
type: 'debug',
|
|
376
|
+
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
377
|
+
});
|
|
378
|
+
} catch (error) {
|
|
379
|
+
// just tell chain it's ready so we can get this node sync
|
|
380
|
+
// when a node connects this node will try to resolve the wantList
|
|
381
|
+
// this should result in the node beeing sync
|
|
382
|
+
if (error.message.includes('nothing found for')) worker.postMessage({ type: 'machine-ready', lastBlock });
|
|
383
|
+
else console.error(error);
|
|
384
|
+
}
|
|
360
385
|
}
|
|
361
|
-
block.loaded = true;
|
|
362
|
-
worker.postMessage({
|
|
363
|
-
type: 'debug',
|
|
364
|
-
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
365
|
-
});
|
|
366
386
|
}
|
|
367
|
-
|
|
368
387
|
if (blocks.length > 0) {
|
|
369
388
|
lastBlock = blocks[blocks.length - 1];
|
|
370
389
|
}
|
|
@@ -373,8 +392,6 @@ _.init = async (message) => {
|
|
|
373
392
|
}
|
|
374
393
|
|
|
375
394
|
worker.postMessage({ type: 'machine-ready', lastBlock });
|
|
376
|
-
|
|
377
|
-
// worker.postMessage({blocks});
|
|
378
395
|
};
|
|
379
396
|
|
|
380
397
|
_.addLoadedBlock = (block) => {
|
|
@@ -399,6 +416,10 @@ const askFor = (question, input) =>
|
|
|
399
416
|
});
|
|
400
417
|
});
|
|
401
418
|
|
|
419
|
+
const resolveContract = (hash) => askFor('contract', hash);
|
|
420
|
+
|
|
421
|
+
const resolveTransaction = (hash) => askFor('transaction', hash);
|
|
422
|
+
|
|
402
423
|
const runTask = async (id, taskName, input) => {
|
|
403
424
|
try {
|
|
404
425
|
const result = await _[taskName](input);
|
package/exports/chain.js
CHANGED
|
@@ -346,6 +346,9 @@ class Machine {
|
|
|
346
346
|
pubsub.publish(data.id, data.value || false);
|
|
347
347
|
break;
|
|
348
348
|
}
|
|
349
|
+
case 'addToWantList': {
|
|
350
|
+
this.wantList.push(data.hash);
|
|
351
|
+
}
|
|
349
352
|
case 'ask': {
|
|
350
353
|
if (data.question === 'contract' || data.question === 'transaction') {
|
|
351
354
|
try {
|
|
@@ -354,6 +357,7 @@ class Machine {
|
|
|
354
357
|
}
|
|
355
358
|
catch (error) {
|
|
356
359
|
console.error(error);
|
|
360
|
+
this.worker.postMessage({ id: data.id, input: data.input });
|
|
357
361
|
this.wantList.push(data.input);
|
|
358
362
|
}
|
|
359
363
|
}
|
|
@@ -150,10 +150,6 @@ const get = ({ contract, method, params }) => {
|
|
|
150
150
|
return result
|
|
151
151
|
};
|
|
152
152
|
|
|
153
|
-
const resolveContract = (hash) => askFor('contract', hash);
|
|
154
|
-
|
|
155
|
-
const resolveTransaction = (hash) => askFor('transaction', hash);
|
|
156
|
-
|
|
157
153
|
const respond = (id, value) => {
|
|
158
154
|
worker.postMessage({
|
|
159
155
|
type: 'response',
|
|
@@ -255,6 +251,13 @@ const _executeTransaction = async (transaction) => {
|
|
|
255
251
|
}
|
|
256
252
|
};
|
|
257
253
|
|
|
254
|
+
const addToWantList = (hash) => {
|
|
255
|
+
worker.postMessage({
|
|
256
|
+
type: 'addToWantList',
|
|
257
|
+
hash
|
|
258
|
+
});
|
|
259
|
+
};
|
|
260
|
+
|
|
258
261
|
_.init = async (message) => {
|
|
259
262
|
let { peerid, fromState, state, info } = message;
|
|
260
263
|
globalThis.peerid = peerid;
|
|
@@ -272,6 +275,10 @@ _.init = async (message) => {
|
|
|
272
275
|
lastBlock = message.lastBlock;
|
|
273
276
|
const setState = async (address, state) => {
|
|
274
277
|
const contractBytes = await resolveContract(address);
|
|
278
|
+
if (contractBytes === address) {
|
|
279
|
+
addToWantList(address);
|
|
280
|
+
return
|
|
281
|
+
}
|
|
275
282
|
const contract = await new ContractMessage(contractBytes);
|
|
276
283
|
|
|
277
284
|
await _.runContract({ hash: address, decoded: contract.decoded, encoded: contract.encoded }, state);
|
|
@@ -341,30 +348,42 @@ _.init = async (message) => {
|
|
|
341
348
|
latestTransactions.splice(-transactionCount, latestTransactions.length);
|
|
342
349
|
}
|
|
343
350
|
if (!block.loaded && !fromState) {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
351
|
+
try {
|
|
352
|
+
const transactions = await Promise.all(
|
|
353
|
+
block.transactions.map(async (transaction) => {
|
|
354
|
+
const message = new TransactionMessage(await resolveTransaction(transaction)).decode();
|
|
355
|
+
if (message === transaction) {
|
|
356
|
+
throw new Error(`nothing found for ${transaction}`)
|
|
357
|
+
}
|
|
358
|
+
})
|
|
359
|
+
);
|
|
360
|
+
const priority = transactions
|
|
361
|
+
.filter((transaction) => transaction.priority)
|
|
362
|
+
?.sort((a, b) => a.nonce - b.nonce);
|
|
363
|
+
if (priority.length > 0)
|
|
364
|
+
for (const transaction of priority) {
|
|
365
|
+
await _executeTransaction(transaction);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
await Promise.all(
|
|
369
|
+
transactions
|
|
370
|
+
.filter((transaction) => !transaction.priority)
|
|
371
|
+
.map(async (transaction) => _executeTransaction(transaction))
|
|
372
|
+
);
|
|
373
|
+
block.loaded = true;
|
|
374
|
+
worker.postMessage({
|
|
375
|
+
type: 'debug',
|
|
376
|
+
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
377
|
+
});
|
|
378
|
+
} catch (error) {
|
|
379
|
+
// just tell chain it's ready so we can get this node sync
|
|
380
|
+
// when a node connects this node will try to resolve the wantList
|
|
381
|
+
// this should result in the node beeing sync
|
|
382
|
+
if (error.message.includes('nothing found for')) worker.postMessage({ type: 'machine-ready', lastBlock });
|
|
383
|
+
else console.error(error);
|
|
384
|
+
}
|
|
360
385
|
}
|
|
361
|
-
block.loaded = true;
|
|
362
|
-
worker.postMessage({
|
|
363
|
-
type: 'debug',
|
|
364
|
-
message: `loaded transactions for block: ${block.hash} @${block.index}`
|
|
365
|
-
});
|
|
366
386
|
}
|
|
367
|
-
|
|
368
387
|
if (blocks.length > 0) {
|
|
369
388
|
lastBlock = blocks[blocks.length - 1];
|
|
370
389
|
}
|
|
@@ -373,8 +392,6 @@ _.init = async (message) => {
|
|
|
373
392
|
}
|
|
374
393
|
|
|
375
394
|
worker.postMessage({ type: 'machine-ready', lastBlock });
|
|
376
|
-
|
|
377
|
-
// worker.postMessage({blocks});
|
|
378
395
|
};
|
|
379
396
|
|
|
380
397
|
_.addLoadedBlock = (block) => {
|
|
@@ -399,6 +416,10 @@ const askFor = (question, input) =>
|
|
|
399
416
|
});
|
|
400
417
|
});
|
|
401
418
|
|
|
419
|
+
const resolveContract = (hash) => askFor('contract', hash);
|
|
420
|
+
|
|
421
|
+
const resolveTransaction = (hash) => askFor('transaction', hash);
|
|
422
|
+
|
|
402
423
|
const runTask = async (id, taskName, input) => {
|
|
403
424
|
try {
|
|
404
425
|
const result = await _[taskName](input);
|