@leofcoin/chain 1.5.57 → 1.5.59

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.
@@ -1,4 +1,4 @@
1
- import { E as EasyWorker, B as BigNumber, f as formatBytes, C as ContractMessage, T as TransactionMessage, a as BlockMessage } from './worker-Suy0gj0S.js';
1
+ import { E as EasyWorker, B as BigNumber, f as formatBytes, C as ContractMessage, T as TransactionMessage } from './worker-Suy0gj0S.js';
2
2
 
3
3
  var contractFactory = "237,198,141,3,53,89,84,113,122,116,99,109,98,57,68,110,99,114,50,116,116,57,77,100,50,104,77,84,106,107,102,72,56,50,68,57,116,118,119,76,106,76,55,78,98,117,67,118,72,111,100,51,114,98,115,70,71,117,173,5,99,108,97,115,115,32,70,97,99,116,111,114,121,123,35,110,97,109,101,61,34,65,114,116,79,110,108,105,110,101,67,111,110,116,114,97,99,116,70,97,99,116,111,114,121,34,59,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,61,48,59,35,99,111,110,116,114,97,99,116,115,61,91,93,59,99,111,110,115,116,114,117,99,116,111,114,40,115,116,97,116,101,41,123,115,116,97,116,101,38,38,40,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,61,115,116,97,116,101,46,99,111,110,116,114,97,99,116,115,44,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,61,115,116,97,116,101,46,116,111,116,97,108,67,111,110,116,114,97,99,116,115,41,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,116,111,116,97,108,67,111,110,116,114,97,99,116,115,58,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,44,99,111,110,116,114,97,99,116,115,58,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,125,125,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,99,111,110,116,114,97,99,116,115,40,41,123,114,101,116,117,114,110,91,46,46,46,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,93,125,103,101,116,32,116,111,116,97,108,67,111,110,116,114,97,99,116,115,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,125,105,115,82,101,103,105,115,116,101,114,101,100,40,97,100,100,114,101,115,115,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,46,105,110,99,108,117,100,101,115,40,97,100,100,114,101,115,115,41,125,97,115,121,110,99,32,114,101,103,105,115,116,101,114,67,111,110,116,114,97,99,116,40,97,100,100,114,101,115,115,41,123,105,102,40,97,119,97,105,116,32,109,115,103,46,115,116,97,116,105,99,67,97,108,108,40,97,100,100,114,101,115,115,44,34,104,97,115,82,111,108,101,34,44,91,109,115,103,46,115,101,110,100,101,114,44,34,79,87,78,69,82,34,93,41,44,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,46,105,110,99,108,117,100,101,115,40,97,100,100,114,101,115,115,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,108,114,101,97,100,121,32,114,101,103,105,115,116,101,114,101,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,67,111,110,116,114,97,99,116,115,43,61,49,44,116,104,105,115,46,35,99,111,110,116,114,97,99,116,115,46,112,117,115,104,40,97,100,100,114,101,115,115,41,125,125,114,101,116,117,114,110,32,70,97,99,116,111,114,121,59,2,91,93";
4
4
  var nativeToken$2 = "237,198,141,3,53,89,84,113,122,116,99,109,98,57,68,110,99,114,50,116,116,57,77,100,50,104,77,84,106,107,102,72,56,50,68,57,116,118,119,76,106,76,55,78,98,117,67,118,72,111,100,51,114,98,115,70,71,117,163,26,99,108,97,115,115,32,82,111,108,101,115,123,35,114,111,108,101,115,61,123,79,87,78,69,82,58,91,93,44,77,73,78,84,58,91,93,44,66,85,82,78,58,91,93,125,59,99,111,110,115,116,114,117,99,116,111,114,40,114,111,108,101,115,41,123,105,102,40,114,111,108,101,115,41,123,105,102,40,33,40,114,111,108,101,115,32,105,110,115,116,97,110,99,101,111,102,32,79,98,106,101,99,116,41,41,116,104,114,111,119,32,110,101,119,32,84,121,112,101,69,114,114,111,114,40,34,101,120,112,101,99,116,101,100,32,114,111,108,101,115,32,116,111,32,98,101,32,97,110,32,111,98,106,101,99,116,34,41,59,116,104,105,115,46,35,114,111,108,101,115,61,123,46,46,46,114,111,108,101,115,44,46,46,46,116,104,105,115,46,35,114,111,108,101,115,125,125,101,108,115,101,32,116,104,105,115,46,35,103,114,97,110,116,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,79,87,78,69,82,34,41,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,114,111,108,101,115,58,116,104,105,115,46,114,111,108,101,115,125,125,103,101,116,32,114,111,108,101,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,114,111,108,101,115,125,125,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,114,101,116,117,114,110,33,33,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,38,38,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,105,110,99,108,117,100,101,115,40,97,100,100,114,101,115,115,41,125,35,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,36,123,114,111,108,101,125,32,114,111,108,101,32,97,108,114,101,97,100,121,32,103,114,97,110,116,101,100,32,102,111,114,32,36,123,97,100,100,114,101,115,115,125,96,41,59,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,112,117,115,104,40,97,100,100,114,101,115,115,41,125,35,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,96,36,123,114,111,108,101,125,32,114,111,108,101,32,97,108,114,101,97,100,121,32,114,101,118,111,107,101,100,32,102,111,114,32,36,123,97,100,100,114,101,115,115,125,96,41,59,105,102,40,34,79,87,78,69,82,34,61,61,61,114,111,108,101,38,38,49,61,61,61,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,108,101,110,103,116,104,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,116,108,101,97,115,116,32,111,110,101,32,111,119,110,101,114,32,105,115,32,110,101,101,100,101,100,33,34,41,59,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,115,112,108,105,99,101,40,116,104,105,115,46,35,114,111,108,101,115,91,114,111,108,101,93,46,105,110,100,101,120,79,102,40,97,100,100,114,101,115,115,41,41,125,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,78,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,103,114,97,110,116,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,125,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,97,100,100,114,101,115,115,44,34,79,87,78,69,82,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,78,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,114,101,118,111,107,101,82,111,108,101,40,97,100,100,114,101,115,115,44,114,111,108,101,41,125,125,99,108,97,115,115,32,84,111,107,101,110,32,101,120,116,101,110,100,115,32,82,111,108,101,115,123,35,110,97,109,101,59,35,115,121,109,98,111,108,59,35,104,111,108,100,101,114,115,61,48,59,35,98,97,108,97,110,99,101,115,61,123,125,59,35,97,112,112,114,111,118,97,108,115,61,123,125,59,35,100,101,99,105,109,97,108,115,61,49,56,59,35,116,111,116,97,108,83,117,112,112,108,121,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,48,41,59,99,111,110,115,116,114,117,99,116,111,114,40,110,97,109,101,44,115,121,109,98,111,108,44,100,101,99,105,109,97,108,115,61,49,56,44,115,116,97,116,101,41,123,105,102,40,33,110,97,109,101,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,97,109,101,32,117,110,100,101,102,105,110,101,100,34,41,59,105,102,40,33,115,121,109,98,111,108,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,115,121,109,98,111,108,32,117,110,100,101,102,105,110,101,100,34,41,59,115,117,112,101,114,40,115,116,97,116,101,63,46,114,111,108,101,115,41,44,116,104,105,115,46,35,110,97,109,101,61,110,97,109,101,44,116,104,105,115,46,35,115,121,109,98,111,108,61,115,121,109,98,111,108,44,116,104,105,115,46,35,100,101,99,105,109,97,108,115,61,100,101,99,105,109,97,108,115,125,103,101,116,32,115,116,97,116,101,40,41,123,114,101,116,117,114,110,123,46,46,46,115,117,112,101,114,46,115,116,97,116,101,44,104,111,108,100,101,114,115,58,116,104,105,115,46,104,111,108,100,101,114,115,44,98,97,108,97,110,99,101,115,58,116,104,105,115,46,98,97,108,97,110,99,101,115,44,97,112,112,114,111,118,97,108,115,58,123,46,46,46,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,125,44,116,111,116,97,108,83,117,112,112,108,121,58,116,104,105,115,46,116,111,116,97,108,83,117,112,112,108,121,125,125,103,101,116,32,116,111,116,97,108,83,117,112,112,108,121,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,125,103,101,116,32,110,97,109,101,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,110,97,109,101,125,103,101,116,32,115,121,109,98,111,108,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,115,121,109,98,111,108,125,103,101,116,32,104,111,108,100,101,114,115,40,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,104,111,108,100,101,114,115,125,103,101,116,32,98,97,108,97,110,99,101,115,40,41,123,114,101,116,117,114,110,123,46,46,46,116,104,105,115,46,35,98,97,108,97,110,99,101,115,125,125,109,105,110,116,40,116,111,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,77,73,78,84,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,61,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,46,97,100,100,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,116,111,44,97,109,111,117,110,116,41,125,98,117,114,110,40,102,114,111,109,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,104,97,115,82,111,108,101,40,109,115,103,46,115,101,110,100,101,114,44,34,66,85,82,78,34,41,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,110,111,116,32,97,108,108,111,119,101,100,34,41,59,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,61,116,104,105,115,46,35,116,111,116,97,108,83,117,112,112,108,121,46,115,117,98,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,102,114,111,109,44,97,109,111,117,110,116,41,125,35,98,101,102,111,114,101,84,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,123,105,102,40,33,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,102,114,111,109,93,124,124,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,102,114,111,109,93,60,97,109,111,117,110,116,41,116,104,114,111,119,32,110,101,119,32,69,114,114,111,114,40,34,97,109,111,117,110,116,32,101,120,99,101,101,100,115,32,98,97,108,97,110,99,101,34,41,125,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,123,34,48,120,48,48,34,61,61,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,116,111,72,101,120,83,116,114,105,110,103,40,41,63,116,104,105,115,46,35,104,111,108,100,101,114,115,45,61,49,58,34,48,120,48,48,34,33,61,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,116,111,72,101,120,83,116,114,105,110,103,40,41,38,38,34,48,120,48,48,34,61,61,61,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,46,116,111,72,101,120,83,116,114,105,110,103,40,41,38,38,40,116,104,105,115,46,35,104,111,108,100,101,114,115,43,61,49,41,125,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,97,100,100,114,101,115,115,44,97,109,111,117,110,116,41,123,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,124,124,40,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,48,41,41,59,99,111,110,115,116,32,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,59,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,97,100,100,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,125,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,97,100,100,114,101,115,115,44,97,109,111,117,110,116,41,123,99,111,110,115,116,32,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,59,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,61,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,46,115,117,98,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,117,112,100,97,116,101,72,111,108,100,101,114,115,40,97,100,100,114,101,115,115,44,112,114,101,118,105,111,117,115,66,97,108,97,110,99,101,41,125,98,97,108,97,110,99,101,79,102,40,97,100,100,114,101,115,115,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,98,97,108,97,110,99,101,115,91,97,100,100,114,101,115,115,93,125,115,101,116,65,112,112,114,111,118,97,108,40,111,112,101,114,97,116,111,114,44,97,109,111,117,110,116,41,123,99,111,110,115,116,32,111,119,110,101,114,61,109,115,103,46,115,101,110,100,101,114,59,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,124,124,40,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,61,123,125,41,44,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,91,111,112,101,114,97,116,111,114,93,61,97,109,111,117,110,116,125,97,112,112,114,111,118,101,100,40,111,119,110,101,114,44,111,112,101,114,97,116,111,114,44,97,109,111,117,110,116,41,123,114,101,116,117,114,110,32,116,104,105,115,46,35,97,112,112,114,111,118,97,108,115,91,111,119,110,101,114,93,91,111,112,101,114,97,116,111,114,93,61,61,61,97,109,111,117,110,116,125,116,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,123,97,109,111,117,110,116,61,66,105,103,78,117,109,98,101,114,46,102,114,111,109,40,97,109,111,117,110,116,41,44,116,104,105,115,46,35,98,101,102,111,114,101,84,114,97,110,115,102,101,114,40,102,114,111,109,44,116,111,44,97,109,111,117,110,116,41,44,116,104,105,115,46,35,100,101,99,114,101,97,115,101,66,97,108,97,110,99,101,40,102,114,111,109,44,97,109,111,117,110,116,41,44,116,104,105,115,46,35,105,110,99,114,101,97,115,101,66,97,108,97,110,99,101,40,116,111,44,97,109,111,117,110,116,41,125,125,99,108,97,115,115,32,65,114,116,79,110,108,105,110,101,32,101,120,116,101,110,100,115,32,84,111,107,101,110,123,99,111,110,115,116,114,117,99,116,111,114,40,115,116,97,116,101,41,123,115,117,112,101,114,40,34,65,114,116,79,110,108,105,110,101,34,44,34,65,82,84,34,44,49,56,44,115,116,97,116,101,41,125,125,114,101,116,117,114,110,32,65,114,116,79,110,108,105,110,101,59,2,91,93";
@@ -29,20 +29,27 @@ const contractFactoryMessage = bytecodes.contractFactory;
29
29
  const nativeTokenMessage = bytecodes.nativeToken;
30
30
  const nameServiceMessage = bytecodes.nameService;
31
31
  const validatorsMessage = bytecodes.validators;
32
+
33
+ const latestTransactions = [];
34
+
32
35
  let nativeCalls = 0;
33
36
  let nativeBurns = 0;
34
37
  let nativeMints = 0;
35
38
  let nativeTransfers = 0;
36
- let totalTransactions = 0;
39
+
40
+ let blocks = [];
41
+ let contracts = {};
42
+ const _ = {};
37
43
 
38
44
  globalThis.BigNumber = BigNumber;
39
- globalThis.contracts = {};
45
+
46
+ let lastBlock = { index: 0, hash: '0x0', previousHash: '0x0' };
40
47
 
41
48
  const has = (address) => {
42
49
  return contracts[address] ? true : false
43
50
  };
44
51
 
45
- const get = (contract, method, params) => {
52
+ const get = ({ contract, method, params }) => {
46
53
  let result;
47
54
  if (params?.length > 0) {
48
55
  result = contracts[contract][method](...params);
@@ -52,7 +59,7 @@ const get = (contract, method, params) => {
52
59
  return result
53
60
  };
54
61
 
55
- const runContract = async ({ decoded, hash, encoded }) => {
62
+ _.runContract = async ({ decoded, hash, encoded }) => {
56
63
  const params = decoded.constructorParameters;
57
64
  try {
58
65
  const func = new Function(new TextDecoder().decode(decoded.contract));
@@ -74,7 +81,7 @@ const runContract = async ({ decoded, hash, encoded }) => {
74
81
  }
75
82
  };
76
83
 
77
- const execute = async (contract, method, params) => {
84
+ _.execute = async ({ contract, method, params }) => {
78
85
  try {
79
86
  let result;
80
87
  // don't execute the method on a proxy
@@ -99,26 +106,25 @@ const execute = async (contract, method, params) => {
99
106
  const createMessage = (sender = globalThis.peerid) => {
100
107
  return {
101
108
  sender,
102
- call: execute,
109
+ call: _.execute,
103
110
  staticCall: get
104
111
  }
105
112
  };
106
113
 
107
- const _init = async ({ contracts, blocks, peerid }) => {
114
+ _.init = async (message) => {
115
+ let { contracts, peerid } = message;
108
116
  globalThis.peerid = peerid;
109
117
  contracts = [contractFactoryMessage, nativeTokenMessage, nameServiceMessage, validatorsMessage];
110
118
 
111
119
  contracts = await Promise.all(
112
120
  contracts.map(async (contract) => {
113
121
  contract = await new ContractMessage(new Uint8Array(contract.split(',')));
114
- await runContract({ decoded: contract.decoded, encoded: contract.encoded, hash: await contract.hash() });
122
+ await _.runContract({ decoded: contract.decoded, encoded: contract.encoded, hash: await contract.hash() });
115
123
  return contract
116
124
  })
117
125
  );
118
126
 
119
- let lastBlock = { hash: '0x0' };
120
-
121
- if (blocks?.length > 0) {
127
+ if (message.blocks?.length > 0) {
122
128
  let pre;
123
129
 
124
130
  try {
@@ -134,68 +140,64 @@ const _init = async ({ contracts, blocks, peerid }) => {
134
140
  serialization: 'advanced',
135
141
  type: 'module'
136
142
  });
137
- blocks = await _worker.once(blocks);
143
+ blocks = await _worker.once(message.blocks);
138
144
  _worker = null;
139
145
  // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
140
146
  // for (let i = 0; i < blocks.length; i++) {
141
147
 
142
148
  // }
143
149
  for (const block of blocks) {
144
- try {
145
- await Promise.all(
146
- block.transactions.map(async (transaction) => {
147
- if (!block.loaded) {
150
+ // we only revalidate the latest 24 blocks
151
+ // every 24 blocks a snapshot is taken and stored in state
152
+ // this means contracts will be restored from this state
153
+ // this also means devs NEED to make sure the state can be restored
154
+ // on contract deploy an error will be thrown if state wasn't recoverable
155
+ if (block.index > 24) {
156
+ const transactionCount = blocks[block.index - 1].transactions.length;
157
+ latestTransactions.splice(-(transactionCount - 1), latestTransactions.length);
158
+ }
159
+ await Promise.all(
160
+ block.transactions.map(async (transaction) => {
161
+ if (!block.loaded) {
162
+ const hash = await new TransactionMessage(transaction).hash();
163
+ if (latestTransactions.includes(hash)) {
164
+ throw new Error(`double transaction found: ${hash} in block ${block.index}`)
165
+ } else {
166
+ latestTransactions.push(hash);
148
167
  const { from, to, method, params, nonce } = transaction;
149
168
  globalThis.msg = createMessage(from);
150
169
 
151
- try {
152
- await execute(to, method, params);
153
- if (to === nativeToken) {
154
- nativeCalls += 1;
155
- if (method === 'burn') nativeBurns += 1;
156
- if (method === 'mint') nativeMints += 1;
157
- if (method === 'transfer') nativeTransfers += 1;
158
- }
159
- totalTransactions += 1;
160
-
161
- worker.postMessage({
162
- type: 'transactionLoaded',
163
- result: {
164
- hash: await new TransactionMessage(transaction).hash(),
165
- from,
166
- nonce: String(nonce)
167
- }
168
- });
169
- } catch (error) {
170
- throw error
170
+ await _.execute({ contract: to, method, params });
171
+ if (to === nativeToken) {
172
+ nativeCalls += 1;
173
+ if (method === 'burn') nativeBurns += 1;
174
+ if (method === 'mint') nativeMints += 1;
175
+ if (method === 'transfer') nativeTransfers += 1;
171
176
  }
177
+
178
+ worker.postMessage({
179
+ type: 'transactionLoaded',
180
+ result: {
181
+ hash,
182
+ from,
183
+ nonce: String(nonce)
184
+ }
185
+ });
172
186
  }
173
- })
174
- );
175
- block.loaded = true;
176
- worker.postMessage({
177
- type: 'debug',
178
- message: `loaded transactions for block: ${block.blockInfo.hash} @${block.blockInfo.index} ${formatBytes(
179
- block.blockInfo.size
180
- )}`
181
- });
182
- } catch (error) {
183
- worker.postMessage({
184
- type: 'error',
185
- message: error.message
186
- });
187
- }
187
+ }
188
+ })
189
+ );
190
+ block.loaded = true;
191
+ worker.postMessage({
192
+ type: 'debug',
193
+ message: `loaded transactions for block: ${block.blockInfo.hash} @${block.blockInfo.index} ${formatBytes(
194
+ block.blockInfo.size
195
+ )}`
196
+ });
188
197
  }
189
198
 
190
199
  if (blocks.length > 0) {
191
200
  lastBlock = blocks[blocks.length - 1];
192
- // todo blocks already loaded as blockmessage so use it!
193
- lastBlock = await new BlockMessage(lastBlock);
194
-
195
- lastBlock = {
196
- ...lastBlock.decoded,
197
- hash: await lastBlock.hash()
198
- };
199
201
  }
200
202
  globalThis.blocks = blocks;
201
203
  }
@@ -205,113 +207,91 @@ const _init = async ({ contracts, blocks, peerid }) => {
205
207
  // worker.postMessage({blocks});
206
208
  };
207
209
 
208
- const tasks = async (e) => {
209
- const id = e.id;
210
- if (e.type === 'init') {
211
- try {
212
- await _init(e.input);
213
- } catch (e) {
214
- worker.postMessage({
215
- type: 'initError',
216
- message: e.message,
217
- id
218
- });
219
- }
220
- } else if (e.type === 'nativeCalls') {
221
- worker.postMessage({
222
- type: 'response',
223
- value: nativeCalls,
224
- id
225
- });
226
- } else if (e.type === 'nativeBurns') {
227
- worker.postMessage({
228
- type: 'response',
229
- value: nativeBurns,
230
- id
231
- });
232
- } else if (e.type === 'nativeMints') {
233
- worker.postMessage({
234
- type: 'response',
235
- value: nativeMints,
236
- id
237
- });
238
- } else if (e.type === 'nativeTransfers') {
239
- worker.postMessage({
240
- type: 'response',
241
- value: nativeTransfers,
242
- id
243
- });
244
- } else if (e.type === 'totalTransfers') {
210
+ _.addLoadedBlock = (block) => {
211
+ blocks[block.index - 1] = block;
212
+ return true
213
+ };
214
+
215
+ _.loadBlock = (block) => {
216
+ // todo validate here and deprecate addLoadedBlock
217
+ };
218
+
219
+ const respond = (id, value) => {
220
+ worker.postMessage({
221
+ type: 'response',
222
+ value,
223
+ id
224
+ });
225
+ };
226
+
227
+ const runTask = async (id, taskName, input) => {
228
+ try {
229
+ const result = await _[taskName](input);
230
+ respond(id, result);
231
+ } catch (e) {
245
232
  worker.postMessage({
246
- type: 'response',
247
- value: totalTransfers,
233
+ type: `${taskName}Error`,
234
+ message: e.message,
248
235
  id
249
236
  });
250
237
  }
251
- if (e.type === 'has') {
252
- try {
253
- const value = await has(e.input.address);
254
- worker.postMessage({
255
- type: 'response',
256
- id,
257
- value
258
- });
259
- } catch (error) {
260
- worker.postMessage({
261
- type: 'hasError',
262
- message: error.message,
263
- id
264
- });
265
- }
266
- }
267
- if (e.type === 'run') {
268
- try {
269
- const value = await runContract(e.input);
270
- worker.postMessage({
271
- type: 'response',
272
- id,
273
- value
274
- });
275
- } catch (e) {
276
- worker.postMessage({
277
- type: 'runError',
278
- message: e.message,
279
- id
280
- });
281
- }
282
- }
283
- if (e.type === 'get') {
284
- try {
285
- const value = await get(e.input.contract, e.input.method, e.input.params);
286
- worker.postMessage({
287
- type: 'response',
288
- id,
289
- value
290
- });
291
- } catch (e) {
292
- worker.postMessage({
293
- type: 'fetchError',
294
- message: e.message,
295
- id
296
- });
297
- }
298
- }
299
- if (e.type === 'execute') {
300
- try {
301
- const value = await execute(e.input.contract, e.input.method, e.input.params);
302
- worker.postMessage({
303
- type: 'response',
304
- id,
305
- value
306
- });
307
- } catch (e) {
308
- worker.postMessage({
309
- type: 'executionError',
310
- message: e.message,
311
- id
312
- });
313
- }
314
- }
315
238
  };
316
239
 
317
- worker.onmessage((data) => tasks(data));
240
+ worker.onmessage(({ id, type, input }) => {
241
+ switch (type) {
242
+ case 'init':
243
+ runTask(id, 'init', input);
244
+ break
245
+ case 'run':
246
+ runTask(id, 'runContract', input);
247
+ break
248
+ case 'execute':
249
+ runTask(id, 'execute', input);
250
+ break
251
+ case 'addLoadedBlock':
252
+ runTask(id, 'addLoadedBlock', input);
253
+ break
254
+ case 'nativeCalls':
255
+ respond(id, nativeCalls);
256
+ break
257
+ case 'contracts':
258
+ respond(id, contracts);
259
+ break
260
+ case 'nativeMints':
261
+ respond(id, nativeMints);
262
+ break
263
+ case 'nativeBurns':
264
+ respond(id, nativeBurns);
265
+ break
266
+ case 'nativeTransfers':
267
+ respond(id, nativeTransfers);
268
+ break
269
+ case 'totalTransfers':
270
+ respond(id, totalTransfers);
271
+ break
272
+ case 'totalBlocks':
273
+ respond(id, blocks.length);
274
+ break
275
+ case 'blocks':
276
+ respond(id, input ? blocks.slice(input.from, input.to) : blocks);
277
+ break
278
+ case 'block':
279
+ respond(id, blocks[input - 1]);
280
+ break
281
+ case 'lastBlock':
282
+ respond(id, blocks[blocks.length - 1]);
283
+ break
284
+ case 'latestTransactions':
285
+ respond(id, latestTransactions);
286
+ break
287
+ case 'has':
288
+ respond(id, has(input.address));
289
+ break
290
+ case 'get':
291
+ respond(id, get(input));
292
+ break
293
+ default:
294
+ console.log(`machine-worker: unsupported taskType: ${type}`);
295
+ break
296
+ }
297
+ });