@leofcoin/chain 1.4.38 → 1.4.40
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 +42 -15
- package/exports/chain.js +42 -15
- package/package.json +1 -1
package/exports/browser/chain.js
CHANGED
|
@@ -8078,6 +8078,7 @@ class Chain extends Contract {
|
|
|
8078
8078
|
// let node =
|
|
8079
8079
|
// globalThis.peernet.protos['peernet-response']({response: node.encoded})
|
|
8080
8080
|
// })
|
|
8081
|
+
await globalThis.peernet.addRequestHandler('transactionPool', this.#transactionPoolHandler.bind(this));
|
|
8081
8082
|
await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
8082
8083
|
await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
8083
8084
|
globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
@@ -8157,21 +8158,41 @@ class Chain extends Contract {
|
|
|
8157
8158
|
}
|
|
8158
8159
|
this.#chainSyncing = false;
|
|
8159
8160
|
}
|
|
8160
|
-
async #
|
|
8161
|
-
let node = await new globalThis.peernet.protos['peernet-request']({ request
|
|
8162
|
-
|
|
8161
|
+
async #prepareRequest(request) {
|
|
8162
|
+
let node = await new globalThis.peernet.protos['peernet-request']({ request });
|
|
8163
|
+
return globalThis.peernet.prepareMessage(node);
|
|
8164
|
+
}
|
|
8165
|
+
async #makeRequest(peer, request) {
|
|
8166
|
+
const node = await this.#prepareRequest(request);
|
|
8163
8167
|
let response = await peer.request(node.encoded);
|
|
8164
|
-
response = await new globalThis.
|
|
8165
|
-
|
|
8166
|
-
|
|
8167
|
-
|
|
8168
|
-
|
|
8169
|
-
|
|
8170
|
-
|
|
8171
|
-
|
|
8172
|
-
|
|
8168
|
+
response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
|
|
8169
|
+
return response.decoded.response;
|
|
8170
|
+
}
|
|
8171
|
+
async #peerConnected(peer) {
|
|
8172
|
+
const lastBlock = await this.#makeRequest(peer, 'lastBlock');
|
|
8173
|
+
this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
|
|
8174
|
+
let pool = await this.#makeRequest(peer, 'transactionPool');
|
|
8175
|
+
pool = await Promise.all(pool.map(async (hash) => {
|
|
8176
|
+
const has = await globalThis.peernet.has(hash);
|
|
8177
|
+
return { has, hash };
|
|
8178
|
+
}));
|
|
8179
|
+
pool = pool.filter(item => !item.has);
|
|
8180
|
+
await Promise.all(pool.map(async ({ hash }) => {
|
|
8181
|
+
const result = await globalThis.peernet.get(hash);
|
|
8182
|
+
const node = await new TransactionMessage(result);
|
|
8183
|
+
await globalThis.transactionPoolStore.put(hash, node.encoded);
|
|
8184
|
+
}));
|
|
8185
|
+
if (pool.length > 0)
|
|
8186
|
+
this.#runEpoch();
|
|
8187
|
+
console.log(pool);
|
|
8188
|
+
if (lastBlock)
|
|
8189
|
+
this.#syncChain(lastBlock);
|
|
8173
8190
|
}
|
|
8174
8191
|
#epochTimeout;
|
|
8192
|
+
async #transactionPoolHandler() {
|
|
8193
|
+
const pool = await globalThis.transactionPoolStore.keys();
|
|
8194
|
+
return new globalThis.peernet.protos['peernet-response']({ response: pool });
|
|
8195
|
+
}
|
|
8175
8196
|
async #lastBlockHandler() {
|
|
8176
8197
|
return new globalThis.peernet.protos['peernet-response']({ response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index } });
|
|
8177
8198
|
}
|
|
@@ -8270,9 +8291,9 @@ class Chain extends Contract {
|
|
|
8270
8291
|
return result || 'no state change';
|
|
8271
8292
|
}
|
|
8272
8293
|
catch (error) {
|
|
8273
|
-
console.log(error);
|
|
8294
|
+
console.log({ error });
|
|
8274
8295
|
globalThis.pubsub.publish(`transaction.completed.${hash}`, { status: 'fail', hash, error: error });
|
|
8275
|
-
throw error;
|
|
8296
|
+
throw { error, hash, from, to, params, nonce };
|
|
8276
8297
|
}
|
|
8277
8298
|
}
|
|
8278
8299
|
async #addBlock(block) {
|
|
@@ -8351,6 +8372,7 @@ class Chain extends Contract {
|
|
|
8351
8372
|
let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
|
|
8352
8373
|
if (Object.keys(transactions)?.length === 0)
|
|
8353
8374
|
return;
|
|
8375
|
+
const keys = await globalThis.transactionPoolStore.keys();
|
|
8354
8376
|
let block = {
|
|
8355
8377
|
transactions: [],
|
|
8356
8378
|
validators: [],
|
|
@@ -8366,15 +8388,20 @@ class Chain extends Contract {
|
|
|
8366
8388
|
for (let transaction of transactions) {
|
|
8367
8389
|
const hash = await transaction.hash();
|
|
8368
8390
|
try {
|
|
8369
|
-
await this.#executeTransaction({ ...transaction.decoded, hash });
|
|
8391
|
+
const result = await this.#executeTransaction({ ...transaction.decoded, hash });
|
|
8392
|
+
console.log({ result });
|
|
8370
8393
|
block.transactions.push({ hash, ...transaction.decoded });
|
|
8371
8394
|
block.fees = block.fees.add(await calculateFee(transaction.decoded));
|
|
8372
8395
|
await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
|
|
8373
8396
|
}
|
|
8374
8397
|
catch (e) {
|
|
8398
|
+
console.log(keys.includes(hash));
|
|
8399
|
+
console.log({ e });
|
|
8400
|
+
console.log(hash);
|
|
8375
8401
|
await globalThis.transactionPoolStore.delete(hash);
|
|
8376
8402
|
}
|
|
8377
8403
|
}
|
|
8404
|
+
console.log(block.transactions);
|
|
8378
8405
|
// don't add empty block
|
|
8379
8406
|
if (block.transactions.length === 0)
|
|
8380
8407
|
return;
|
package/exports/chain.js
CHANGED
|
@@ -673,6 +673,7 @@ class Chain extends Contract {
|
|
|
673
673
|
// let node =
|
|
674
674
|
// globalThis.peernet.protos['peernet-response']({response: node.encoded})
|
|
675
675
|
// })
|
|
676
|
+
await globalThis.peernet.addRequestHandler('transactionPool', this.#transactionPoolHandler.bind(this));
|
|
676
677
|
await globalThis.peernet.addRequestHandler('lastBlock', this.#lastBlockHandler.bind(this));
|
|
677
678
|
await globalThis.peernet.addRequestHandler('knownBlocks', this.#knownBlocksHandler.bind(this));
|
|
678
679
|
globalThis.peernet.subscribe('add-block', this.#addBlock.bind(this));
|
|
@@ -752,21 +753,41 @@ class Chain extends Contract {
|
|
|
752
753
|
}
|
|
753
754
|
this.#chainSyncing = false;
|
|
754
755
|
}
|
|
755
|
-
async #
|
|
756
|
-
let node = await new globalThis.peernet.protos['peernet-request']({ request
|
|
757
|
-
|
|
756
|
+
async #prepareRequest(request) {
|
|
757
|
+
let node = await new globalThis.peernet.protos['peernet-request']({ request });
|
|
758
|
+
return globalThis.peernet.prepareMessage(node);
|
|
759
|
+
}
|
|
760
|
+
async #makeRequest(peer, request) {
|
|
761
|
+
const node = await this.#prepareRequest(request);
|
|
758
762
|
let response = await peer.request(node.encoded);
|
|
759
|
-
response = await new globalThis.
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
763
|
+
response = await new globalThis.peernet.protos['peernet-response'](new Uint8Array(Object.values(response)));
|
|
764
|
+
return response.decoded.response;
|
|
765
|
+
}
|
|
766
|
+
async #peerConnected(peer) {
|
|
767
|
+
const lastBlock = await this.#makeRequest(peer, 'lastBlock');
|
|
768
|
+
this.#knownBlocks = await this.#makeRequest(peer, 'knownBlocks');
|
|
769
|
+
let pool = await this.#makeRequest(peer, 'transactionPool');
|
|
770
|
+
pool = await Promise.all(pool.map(async (hash) => {
|
|
771
|
+
const has = await globalThis.peernet.has(hash);
|
|
772
|
+
return { has, hash };
|
|
773
|
+
}));
|
|
774
|
+
pool = pool.filter(item => !item.has);
|
|
775
|
+
await Promise.all(pool.map(async ({ hash }) => {
|
|
776
|
+
const result = await globalThis.peernet.get(hash);
|
|
777
|
+
const node = await new TransactionMessage(result);
|
|
778
|
+
await globalThis.transactionPoolStore.put(hash, node.encoded);
|
|
779
|
+
}));
|
|
780
|
+
if (pool.length > 0)
|
|
781
|
+
this.#runEpoch();
|
|
782
|
+
console.log(pool);
|
|
783
|
+
if (lastBlock)
|
|
784
|
+
this.#syncChain(lastBlock);
|
|
768
785
|
}
|
|
769
786
|
#epochTimeout;
|
|
787
|
+
async #transactionPoolHandler() {
|
|
788
|
+
const pool = await globalThis.transactionPoolStore.keys();
|
|
789
|
+
return new globalThis.peernet.protos['peernet-response']({ response: pool });
|
|
790
|
+
}
|
|
770
791
|
async #lastBlockHandler() {
|
|
771
792
|
return new globalThis.peernet.protos['peernet-response']({ response: { hash: this.#lastBlock?.hash, index: this.#lastBlock?.index } });
|
|
772
793
|
}
|
|
@@ -865,9 +886,9 @@ class Chain extends Contract {
|
|
|
865
886
|
return result || 'no state change';
|
|
866
887
|
}
|
|
867
888
|
catch (error) {
|
|
868
|
-
console.log(error);
|
|
889
|
+
console.log({ error });
|
|
869
890
|
globalThis.pubsub.publish(`transaction.completed.${hash}`, { status: 'fail', hash, error: error });
|
|
870
|
-
throw error;
|
|
891
|
+
throw { error, hash, from, to, params, nonce };
|
|
871
892
|
}
|
|
872
893
|
}
|
|
873
894
|
async #addBlock(block) {
|
|
@@ -946,6 +967,7 @@ class Chain extends Contract {
|
|
|
946
967
|
let transactions = await globalThis.transactionPoolStore.values(this.transactionLimit);
|
|
947
968
|
if (Object.keys(transactions)?.length === 0)
|
|
948
969
|
return;
|
|
970
|
+
const keys = await globalThis.transactionPoolStore.keys();
|
|
949
971
|
let block = {
|
|
950
972
|
transactions: [],
|
|
951
973
|
validators: [],
|
|
@@ -961,15 +983,20 @@ class Chain extends Contract {
|
|
|
961
983
|
for (let transaction of transactions) {
|
|
962
984
|
const hash = await transaction.hash();
|
|
963
985
|
try {
|
|
964
|
-
await this.#executeTransaction({ ...transaction.decoded, hash });
|
|
986
|
+
const result = await this.#executeTransaction({ ...transaction.decoded, hash });
|
|
987
|
+
console.log({ result });
|
|
965
988
|
block.transactions.push({ hash, ...transaction.decoded });
|
|
966
989
|
block.fees = block.fees.add(await calculateFee(transaction.decoded));
|
|
967
990
|
await globalThis.accountsStore.put(transaction.decoded.from, new TextEncoder().encode(String(transaction.decoded.nonce)));
|
|
968
991
|
}
|
|
969
992
|
catch (e) {
|
|
993
|
+
console.log(keys.includes(hash));
|
|
994
|
+
console.log({ e });
|
|
995
|
+
console.log(hash);
|
|
970
996
|
await globalThis.transactionPoolStore.delete(hash);
|
|
971
997
|
}
|
|
972
998
|
}
|
|
999
|
+
console.log(block.transactions);
|
|
973
1000
|
// don't add empty block
|
|
974
1001
|
if (block.transactions.length === 0)
|
|
975
1002
|
return;
|