@leofcoin/chain 1.5.25 → 1.5.26

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, a as BlockMessage } from './worker-0f66935f.js';
1
+ import { E as EasyWorker, B as BigNumber, f as formatBytes, C as ContractMessage, a as BlockMessage } from './worker-5b02efbe.js';
2
2
 
3
3
  var contractFactory = "237,198,141,3,53,89,84,113,119,88,110,55,76,101,103,89,119,65,109,100,117,75,88,119,116,83,52,118,52,100,97,114,113,66,84,81,82,76,90,106,50,57,117,106,112,114,98,104,52,119,121,76,106,112,56,50,87,106,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 = "237,198,141,3,53,89,84,113,119,88,110,55,76,101,103,89,119,65,109,100,117,75,88,119,116,83,52,118,52,100,97,114,113,66,84,81,82,76,90,106,50,57,117,106,112,114,98,104,52,119,121,76,106,112,56,50,87,106,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";
@@ -11,235 +11,224 @@ var bytecodes = {
11
11
  validators: validators
12
12
  };
13
13
 
14
- const worker = new EasyWorker();
15
-
16
- const contractFactoryMessage = bytecodes.contractFactory;
17
- const nativeTokenMessage = bytecodes.nativeToken;
18
- const nameServiceMessage = bytecodes.nameService;
19
- const validatorsMessage = bytecodes.validators;
20
-
21
- globalThis.BigNumber = BigNumber;
22
- globalThis.contracts = {};
23
-
24
- const has = address => {
25
- return contracts[address] ? true : false
26
- };
27
-
28
- const get = (contract, method, params) => {
29
- let result;
30
- if (params?.length > 0) {
31
- result = contracts[contract][method](...params);
32
- } else {
33
- result = contracts[contract][method];
34
- }
35
- return result
36
- };
37
-
38
- const runContract = async ({decoded, hash, encoded}) => {
39
- const params = decoded.constructorParameters;
40
- try {
41
-
42
- const func = new Function(new TextDecoder().decode(decoded.contract));
43
- const Contract = func();
44
-
45
- globalThis.msg = createMessage(decoded.creator);
46
- contracts[hash] = await new Contract(...params);
47
- worker.postMessage({
48
- type: 'debug',
49
- messages: [
50
- `loaded contract: ${hash}`,
51
- `size: ${formatBytes(encoded.length)}`
52
- ]
53
- });
54
- } catch (e) {
55
- console.log(e);
56
- worker.postMessage({
57
- type: 'contractError',
58
- hash: await contractMessage.hash()
59
- });
60
- }
61
- };
62
-
63
- const execute = async (contract, method, params) => {
64
- try {
65
- let result;
66
- // don't execute the method on a proxy
67
- if (contracts[contract].fallback) {
68
- result = await contracts[contract].fallback(method, params);
69
- } else {
70
- result = await contracts[contract][method](...params);
71
- }
72
- // state.put(result)
73
- return result
74
- } catch (e) {
75
- throw new Error(
76
- `error: ${e.message}
77
- contract: ${contract}
78
- method: ${method}
79
- params: ${JSON.stringify(params, null, '\t')}
80
- `
81
- )
82
- }
83
- };
84
-
85
-
86
- const createMessage = (sender = globalThis.peerid) => {
87
- return {
88
- sender,
89
- call: execute,
90
- staticCall: get
91
- }
92
- };
93
-
94
- const _init = async ({ contracts, blocks, peerid })=> {
95
-
96
- globalThis.peerid = peerid;
97
- contracts = [
98
- contractFactoryMessage,
99
- nativeTokenMessage,
100
- nameServiceMessage,
101
- validatorsMessage
102
- ];
103
-
104
- contracts = await Promise.all(contracts.map(async contract => {
105
- contract = await new ContractMessage(new Uint8Array(contract.split(',')));
106
- await runContract({decoded: contract.decoded, encoded: contract.encoded, hash: await contract.hash()});
107
- return contract
108
- }));
109
-
110
- let lastBlock = {hash: '0x0'};
111
-
112
- if (blocks?.length > 0) {
113
- let pre;
114
-
115
- try {
116
- const importee = await import('url');
117
- const url = importee.default;
118
- if (url) pre = url.fileURLToPath(new URL('.', import.meta.url));
119
- } catch {
120
- // browser env
121
- pre = './';
122
- }
123
-
124
- const _worker = await new EasyWorker(pre + './block-worker.js', {serialization: 'advanced', type: 'module' });
125
- blocks = await _worker.once([blocks[blocks.length - 1]]);
126
-
127
- // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
128
- // for (let i = 0; i < blocks.length; i++) {
129
-
130
- // }
131
- // for (const block of blocks) {
132
- // await Promise.all(block.decoded.transactions.map(async message => {
133
- // if (!block.loaded) {
134
- // const {from, to, method, params} = message;
135
- // globalThis.msg = createMessage(from);
136
-
137
- // await execute(to, method, params);
138
- // block.loaded = true
139
- // }
140
- // }));
141
- // }
142
-
143
- if (blocks.length > 0) {
144
- lastBlock = blocks[blocks.length - 1];
145
- // todo blocks already loaded as blockmessage so use it!
146
- lastBlock = await new BlockMessage(lastBlock);
147
-
148
- lastBlock = {
149
- ...lastBlock.decoded,
150
- hash: await lastBlock.hash()
151
- };
152
- }
153
- globalThis.blocks = blocks;
154
- }
155
-
156
-
157
-
158
-
159
- worker.postMessage({type: 'machine-ready', lastBlock});
160
-
161
- // worker.postMessage({blocks});
162
- };
163
-
164
- const tasks = async (e) => {
165
- const id = e.id;
166
- if (e.type === 'init') {
167
- try {
168
- await _init(e.input);
169
- } catch (e) {
170
- worker.postMessage({
171
- type: 'initError',
172
- message: e.message,
173
- id
174
- });
175
- }
176
- }
177
- if (e.type === 'has') {
178
- try {
179
- const value = await has(e.input.address);
180
- worker.postMessage({
181
- type: 'response',
182
- id,
183
- value
184
- });
185
- } catch (error) {
186
- worker.postMessage({
187
- type: 'hasError',
188
- message: error.message,
189
- id
190
- });
191
- }
192
- }
193
- if (e.type === 'run') {
194
- try {
195
- const value = await runContract(e.input);
196
- worker.postMessage({
197
- type: 'response',
198
- id,
199
- value
200
- });
201
- } catch (e) {
202
- worker.postMessage({
203
- type: 'runError',
204
- message: e.message,
205
- id
206
- });
207
- }
208
- }
209
- if (e.type === 'get') {
210
- try {
211
- const value = await get(e.input.contract, e.input.method, e.input.params);
212
- worker.postMessage({
213
- type: 'response',
214
- id,
215
- value
216
- });
217
- } catch (e) {
218
- worker.postMessage({
219
- type: 'fetchError',
220
- message: e.message,
221
- id
222
- });
223
- }
224
-
225
- }
226
- if (e.type === 'execute') {
227
- try {
228
- const value = await execute(e.input.contract, e.input.method, e.input.params);
229
- worker.postMessage({
230
- type: 'response',
231
- id,
232
- value
233
- });
234
- } catch(e) {
235
- worker.postMessage({
236
- type: 'executionError',
237
- message: e.message,
238
- id
239
-
240
- });
241
- }
242
- }
243
- };
244
-
245
- worker.onmessage(data => tasks(data));
14
+ const worker = new EasyWorker();
15
+
16
+ const contractFactoryMessage = bytecodes.contractFactory;
17
+ const nativeTokenMessage = bytecodes.nativeToken;
18
+ const nameServiceMessage = bytecodes.nameService;
19
+ const validatorsMessage = bytecodes.validators;
20
+
21
+ globalThis.BigNumber = BigNumber;
22
+ globalThis.contracts = {};
23
+
24
+ const has = (address) => {
25
+ return contracts[address] ? true : false
26
+ };
27
+
28
+ const get = (contract, method, params) => {
29
+ let result;
30
+ if (params?.length > 0) {
31
+ result = contracts[contract][method](...params);
32
+ } else {
33
+ result = contracts[contract][method];
34
+ }
35
+ return result
36
+ };
37
+
38
+ const runContract = async ({ decoded, hash, encoded }) => {
39
+ const params = decoded.constructorParameters;
40
+ try {
41
+ const func = new Function(new TextDecoder().decode(decoded.contract));
42
+ const Contract = func();
43
+
44
+ globalThis.msg = createMessage(decoded.creator);
45
+ contracts[hash] = await new Contract(...params);
46
+ worker.postMessage({
47
+ type: 'debug',
48
+ messages: [`loaded contract: ${hash}`, `size: ${formatBytes(encoded.length)}`]
49
+ });
50
+ } catch (e) {
51
+ console.log(e);
52
+ worker.postMessage({
53
+ type: 'contractError',
54
+ hash: await contractMessage.hash()
55
+ });
56
+ }
57
+ };
58
+
59
+ const execute = async (contract, method, params) => {
60
+ try {
61
+ let result;
62
+ // don't execute the method on a proxy
63
+ if (contracts[contract].fallback) {
64
+ result = await contracts[contract].fallback(method, params);
65
+ } else {
66
+ result = await contracts[contract][method](...params);
67
+ }
68
+ // state.put(result)
69
+ return result
70
+ } catch (e) {
71
+ throw new Error(
72
+ `error: ${e.message}
73
+ contract: ${contract}
74
+ method: ${method}
75
+ params: ${JSON.stringify(params, null, '\t')}
76
+ `
77
+ )
78
+ }
79
+ };
80
+
81
+ const createMessage = (sender = globalThis.peerid) => {
82
+ return {
83
+ sender,
84
+ call: execute,
85
+ staticCall: get
86
+ }
87
+ };
88
+
89
+ const _init = async ({ contracts, blocks, peerid }) => {
90
+ globalThis.peerid = peerid;
91
+ contracts = [contractFactoryMessage, nativeTokenMessage, nameServiceMessage, validatorsMessage];
92
+
93
+ contracts = await Promise.all(
94
+ contracts.map(async (contract) => {
95
+ contract = await new ContractMessage(new Uint8Array(contract.split(',')));
96
+ await runContract({ decoded: contract.decoded, encoded: contract.encoded, hash: await contract.hash() });
97
+ return contract
98
+ })
99
+ );
100
+
101
+ let lastBlock = { hash: '0x0' };
102
+
103
+ if (blocks?.length > 0) {
104
+ let pre;
105
+
106
+ try {
107
+ const importee = await import('url');
108
+ const url = importee.default;
109
+ if (url) pre = url.fileURLToPath(new URL('.', import.meta.url));
110
+ } catch {
111
+ // browser env
112
+ pre = './';
113
+ }
114
+
115
+ const _worker = await new EasyWorker(pre + './block-worker.js', {
116
+ serialization: 'advanced',
117
+ type: 'module'
118
+ });
119
+ blocks = await _worker.once([blocks[blocks.length - 1]]);
120
+
121
+ // blocks = unique(globalThis.blocks ? globalThis : [], blocks)
122
+ // for (let i = 0; i < blocks.length; i++) {
123
+
124
+ // }
125
+ // for (const block of blocks) {
126
+ // await Promise.all(block.decoded.transactions.map(async message => {
127
+ // if (!block.loaded) {
128
+ // const {from, to, method, params} = message;
129
+ // globalThis.msg = createMessage(from);
130
+
131
+ // await execute(to, method, params);
132
+ // block.loaded = true
133
+ // }
134
+ // }));
135
+ // }
136
+
137
+ if (blocks.length > 0) {
138
+ lastBlock = blocks[blocks.length - 1];
139
+ // todo blocks already loaded as blockmessage so use it!
140
+ lastBlock = await new BlockMessage(lastBlock);
141
+
142
+ lastBlock = {
143
+ ...lastBlock.decoded,
144
+ hash: await lastBlock.hash()
145
+ };
146
+ }
147
+ globalThis.blocks = blocks;
148
+ }
149
+
150
+ worker.postMessage({ type: 'machine-ready', lastBlock });
151
+
152
+ // worker.postMessage({blocks});
153
+ };
154
+
155
+ const tasks = async (e) => {
156
+ const id = e.id;
157
+ if (e.type === 'init') {
158
+ try {
159
+ await _init(e.input);
160
+ } catch (e) {
161
+ worker.postMessage({
162
+ type: 'initError',
163
+ message: e.message,
164
+ id
165
+ });
166
+ }
167
+ }
168
+ if (e.type === 'has') {
169
+ try {
170
+ const value = await has(e.input.address);
171
+ worker.postMessage({
172
+ type: 'response',
173
+ id,
174
+ value
175
+ });
176
+ } catch (error) {
177
+ worker.postMessage({
178
+ type: 'hasError',
179
+ message: error.message,
180
+ id
181
+ });
182
+ }
183
+ }
184
+ if (e.type === 'run') {
185
+ try {
186
+ const value = await runContract(e.input);
187
+ worker.postMessage({
188
+ type: 'response',
189
+ id,
190
+ value
191
+ });
192
+ } catch (e) {
193
+ worker.postMessage({
194
+ type: 'runError',
195
+ message: e.message,
196
+ id
197
+ });
198
+ }
199
+ }
200
+ if (e.type === 'get') {
201
+ try {
202
+ const value = await get(e.input.contract, e.input.method, e.input.params);
203
+ worker.postMessage({
204
+ type: 'response',
205
+ id,
206
+ value
207
+ });
208
+ } catch (e) {
209
+ worker.postMessage({
210
+ type: 'fetchError',
211
+ message: e.message,
212
+ id
213
+ });
214
+ }
215
+ }
216
+ if (e.type === 'execute') {
217
+ try {
218
+ const value = await execute(e.input.contract, e.input.method, e.input.params);
219
+ worker.postMessage({
220
+ type: 'response',
221
+ id,
222
+ value
223
+ });
224
+ } catch (e) {
225
+ worker.postMessage({
226
+ type: 'executionError',
227
+ message: e.message,
228
+ id
229
+ });
230
+ }
231
+ }
232
+ };
233
+
234
+ worker.onmessage((data) => tasks(data));
@@ -5570,9 +5570,9 @@ class BlockMessage extends FormatInterface {
5570
5570
  encoded = encoded || this.encoded;
5571
5571
  super.decode(encoded);
5572
5572
  // @ts-ignore
5573
- this.decoded.transactions = index$4(this.decoded.transactions).map(transaction => new TransactionMessage(transaction).decoded);
5573
+ this.decoded.transactions = index$4(this.decoded.transactions).map((transaction) => new TransactionMessage(transaction).decoded);
5574
5574
  // @ts-ignore
5575
- this.decoded.validators = index$4(this.decoded.validators).map(validator => new ValidatorMessage(validator).decoded);
5575
+ this.decoded.validators = index$4(this.decoded.validators).map((validator) => new ValidatorMessage(validator).decoded);
5576
5576
  return this.decoded;
5577
5577
  }
5578
5578
  }