@leofcoin/chain 1.4.57 → 1.4.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.
- package/exports/browser/chain.js +44 -16
- package/exports/browser/{client-d1c29e20-c5d45b87.js → client-4d80940e-a63ca03c.js} +1 -1
- package/exports/browser/{index-904b9f85-2d6175c2.js → index-4464b87e-d39e42fc.js} +1 -1
- package/exports/browser/{messages-de2c5670-d99ad896.js → messages-27279443-8ccccaf2.js} +1 -1
- package/exports/browser/{node-browser-3d505e1f.js → node-browser-79442920.js} +30 -28
- package/exports/browser/node-browser.js +1 -1
- package/exports/chain.js +44 -16
- package/exports/typings/chain.d.ts +4 -1
- package/package.json +1 -1
package/exports/browser/chain.js
CHANGED
|
@@ -7883,6 +7883,8 @@ class Contract extends Transaction {
|
|
|
7883
7883
|
globalThis.BigNumber = BigNumber;
|
|
7884
7884
|
// check if browser or local
|
|
7885
7885
|
class Chain extends Contract {
|
|
7886
|
+
#lastBlockInQue;
|
|
7887
|
+
#syncErrorCount = 0;
|
|
7886
7888
|
version;
|
|
7887
7889
|
#resolveErrored;
|
|
7888
7890
|
#state;
|
|
@@ -8179,24 +8181,50 @@ class Chain extends Contract {
|
|
|
8179
8181
|
if (this.#chainSyncing)
|
|
8180
8182
|
return 'already syncing';
|
|
8181
8183
|
const latest = await this.#getLatestBlock();
|
|
8182
|
-
await this
|
|
8184
|
+
await this.syncChain(latest);
|
|
8183
8185
|
return 'synced';
|
|
8184
8186
|
}
|
|
8185
|
-
async
|
|
8186
|
-
|
|
8187
|
-
|
|
8188
|
-
|
|
8189
|
-
|
|
8190
|
-
|
|
8191
|
-
|
|
8192
|
-
|
|
8193
|
-
|
|
8194
|
-
|
|
8195
|
-
|
|
8187
|
+
async syncChain(lastBlock) {
|
|
8188
|
+
if (!lastBlock)
|
|
8189
|
+
lastBlock = await this.#getLatestBlock();
|
|
8190
|
+
if (this.#chainSyncing) {
|
|
8191
|
+
if (!this.#lastBlockInQue || lastBlock.index > this.#lastBlockInQue.index)
|
|
8192
|
+
this.#lastBlockInQue = lastBlock;
|
|
8193
|
+
return 'syncing';
|
|
8194
|
+
}
|
|
8195
|
+
this.#chainSyncing = true;
|
|
8196
|
+
const syncPromise = (lastBlock) => new Promise(async (resolve, reject) => {
|
|
8197
|
+
setTimeout(() => {
|
|
8198
|
+
reject('timedOut');
|
|
8199
|
+
}, 10000);
|
|
8200
|
+
try {
|
|
8201
|
+
await this.#syncChain(lastBlock);
|
|
8202
|
+
resolve(true);
|
|
8196
8203
|
}
|
|
8197
|
-
|
|
8198
|
-
|
|
8199
|
-
|
|
8204
|
+
catch (error) {
|
|
8205
|
+
reject(error);
|
|
8206
|
+
}
|
|
8207
|
+
});
|
|
8208
|
+
if (globalThis.peernet.connections.length === 0)
|
|
8209
|
+
return 'connectionless';
|
|
8210
|
+
try {
|
|
8211
|
+
await syncPromise(lastBlock);
|
|
8212
|
+
}
|
|
8213
|
+
catch (error) {
|
|
8214
|
+
console.log(error);
|
|
8215
|
+
this.#syncErrorCount += 1;
|
|
8216
|
+
if (this.#syncErrorCount <= 3)
|
|
8217
|
+
return this.syncChain(this.#lastBlockInQue);
|
|
8218
|
+
return 'errored';
|
|
8219
|
+
}
|
|
8220
|
+
this.#syncErrorCount = 0;
|
|
8221
|
+
if (this.#lastBlockInQue)
|
|
8222
|
+
return this.syncChain(this.#lastBlockInQue);
|
|
8223
|
+
this.#chainSyncing = false;
|
|
8224
|
+
return 'synced';
|
|
8225
|
+
}
|
|
8226
|
+
async #syncChain(lastBlock) {
|
|
8227
|
+
if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.index)
|
|
8200
8228
|
return;
|
|
8201
8229
|
this.#chainSyncing = true;
|
|
8202
8230
|
if (this.#knownBlocks?.length === Number(lastBlock.index) + 1) {
|
|
@@ -8242,7 +8270,7 @@ class Chain extends Contract {
|
|
|
8242
8270
|
console.log(lastBlock);
|
|
8243
8271
|
this.#resolveErrored = false;
|
|
8244
8272
|
if (lastBlock)
|
|
8245
|
-
await this
|
|
8273
|
+
await this.syncChain(lastBlock);
|
|
8246
8274
|
if (await this.hasTransactionToHandle() && !this.#resolveErrored && this.#participating)
|
|
8247
8275
|
this.#runEpoch();
|
|
8248
8276
|
}
|
|
@@ -6309,16 +6309,13 @@ const distanceInKmBetweenEarthCoordinates = (lat1, lon1, lat2, lon2) => {
|
|
|
6309
6309
|
return earthRadiusKm * c;
|
|
6310
6310
|
};
|
|
6311
6311
|
class DhtEarth {
|
|
6312
|
+
providerMap = new Map;
|
|
6312
6313
|
/**
|
|
6313
6314
|
*
|
|
6314
6315
|
*/
|
|
6315
6316
|
constructor() {
|
|
6316
6317
|
this.providerMap = new Map();
|
|
6317
6318
|
}
|
|
6318
|
-
/**
|
|
6319
|
-
* @param {Object} address
|
|
6320
|
-
* @return {Object} {latitude: lat, longitude: lon}
|
|
6321
|
-
*/
|
|
6322
6319
|
async getCoordinates(address) {
|
|
6323
6320
|
if (!fetchedCoordinates[address]) {
|
|
6324
6321
|
const request = `https://whereis.leofcoin.org/?ip=${address}`;
|
|
@@ -6338,10 +6335,6 @@ class DhtEarth {
|
|
|
6338
6335
|
const { latitude, longitude } = await this.getCoordinates(provider.address);
|
|
6339
6336
|
return { provider, distance: distanceInKmBetweenEarthCoordinates(peer.latitude, peer.longitude, latitude, longitude) };
|
|
6340
6337
|
}
|
|
6341
|
-
/**
|
|
6342
|
-
* @param {Array} providers
|
|
6343
|
-
* @return {Object} closestPeer
|
|
6344
|
-
*/
|
|
6345
6338
|
async closestPeer(providers) {
|
|
6346
6339
|
let all = [];
|
|
6347
6340
|
const address = await getAddress();
|
|
@@ -6356,26 +6349,32 @@ class DhtEarth {
|
|
|
6356
6349
|
all = all.sort((previous, current) => previous.distance - current.distance);
|
|
6357
6350
|
return all[0].provider;
|
|
6358
6351
|
}
|
|
6359
|
-
|
|
6360
|
-
|
|
6361
|
-
* @return {Array} providers
|
|
6362
|
-
*/
|
|
6363
|
-
providersFor(hash) {
|
|
6364
|
-
return this.providerMap.get(hash);
|
|
6352
|
+
hasProvider(hash) {
|
|
6353
|
+
return this.providerMap.has(hash);
|
|
6365
6354
|
}
|
|
6366
|
-
|
|
6367
|
-
|
|
6368
|
-
* @param {String} hash
|
|
6369
|
-
* @return {Array} providers
|
|
6370
|
-
*/
|
|
6371
|
-
async addProvider(address, hash) {
|
|
6372
|
-
let providers = [];
|
|
6355
|
+
providersFor(hash) {
|
|
6356
|
+
let providers;
|
|
6373
6357
|
if (this.providerMap.has(hash))
|
|
6374
6358
|
providers = this.providerMap.get(hash);
|
|
6375
|
-
providers = new Set([...providers, address]);
|
|
6376
|
-
this.providerMap.set(hash, providers);
|
|
6377
6359
|
return providers;
|
|
6378
6360
|
}
|
|
6361
|
+
addProvider(address, hash) {
|
|
6362
|
+
let providers = new Set();
|
|
6363
|
+
if (this.providerMap.has(hash)) {
|
|
6364
|
+
providers = this.providerMap.get(hash);
|
|
6365
|
+
}
|
|
6366
|
+
providers.add(address);
|
|
6367
|
+
this.providerMap.set(hash, providers);
|
|
6368
|
+
}
|
|
6369
|
+
removeProvider(address, hash) {
|
|
6370
|
+
let deleted = undefined;
|
|
6371
|
+
if (this.providerMap.has(hash)) {
|
|
6372
|
+
const providers = this.providerMap.get(hash);
|
|
6373
|
+
deleted = providers.delete(address);
|
|
6374
|
+
deleted && this.providerMap.set(hash, providers);
|
|
6375
|
+
}
|
|
6376
|
+
return deleted;
|
|
6377
|
+
}
|
|
6379
6378
|
}
|
|
6380
6379
|
|
|
6381
6380
|
class MessageHandler {
|
|
@@ -20267,7 +20266,7 @@ class Identity {
|
|
|
20267
20266
|
globalThis.peernet.selectedAccount = new TextDecoder().decode(selected);
|
|
20268
20267
|
}
|
|
20269
20268
|
else {
|
|
20270
|
-
const importee = await import(/* webpackChunkName: "generate-account" */ './index-
|
|
20269
|
+
const importee = await import(/* webpackChunkName: "generate-account" */ './index-4464b87e-d39e42fc.js');
|
|
20271
20270
|
const { identity, accounts } = await importee.default(password, this.network);
|
|
20272
20271
|
await globalThis.accountStore.put('public', JSON.stringify({ walletId: identity.walletId }));
|
|
20273
20272
|
await globalThis.walletStore.put('version', String(1));
|
|
@@ -20438,7 +20437,7 @@ class Peernet {
|
|
|
20438
20437
|
this.root = options.root;
|
|
20439
20438
|
const { RequestMessage, ResponseMessage, PeerMessage, PeerMessageResponse, PeernetMessage, DHTMessage, DHTMessageResponse, DataMessage, DataMessageResponse, PsMessage, ChatMessage, PeernetFile
|
|
20440
20439
|
// FolderMessageResponse
|
|
20441
|
-
} = await import(/* webpackChunkName: "messages" */ './messages-
|
|
20440
|
+
} = await import(/* webpackChunkName: "messages" */ './messages-27279443-8ccccaf2.js');
|
|
20442
20441
|
/**
|
|
20443
20442
|
* proto Object containing protos
|
|
20444
20443
|
* @type {Object}
|
|
@@ -20517,7 +20516,7 @@ class Peernet {
|
|
|
20517
20516
|
if (this.#starting || this.#started)
|
|
20518
20517
|
return;
|
|
20519
20518
|
this.#starting = true;
|
|
20520
|
-
const importee = await import('./client-
|
|
20519
|
+
const importee = await import('./client-4d80940e-a63ca03c.js');
|
|
20521
20520
|
/**
|
|
20522
20521
|
* @access public
|
|
20523
20522
|
* @type {PeernetClient}
|
|
@@ -20707,8 +20706,8 @@ class Peernet {
|
|
|
20707
20706
|
if (!closestPeer || !closestPeer.id)
|
|
20708
20707
|
return this.requestData(hash, store?.name || store);
|
|
20709
20708
|
const id = closestPeer.id;
|
|
20710
|
-
|
|
20711
|
-
|
|
20709
|
+
const peer = this.#connections[id];
|
|
20710
|
+
if (peer?.connected) {
|
|
20712
20711
|
let data = await new globalThis.peernet.protos['peernet-data']({ hash, store: store?.name || store });
|
|
20713
20712
|
const node = await this.prepareMessage(data);
|
|
20714
20713
|
if (peer)
|
|
@@ -20731,6 +20730,9 @@ class Peernet {
|
|
|
20731
20730
|
return BufferToUint8Array(proto.decoded.data);
|
|
20732
20731
|
// this.put(hash, proto.decoded.data)
|
|
20733
20732
|
}
|
|
20733
|
+
else {
|
|
20734
|
+
this.dht.removeProvider(id, hash);
|
|
20735
|
+
}
|
|
20734
20736
|
return;
|
|
20735
20737
|
}
|
|
20736
20738
|
get message() {
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { N as default } from './node-browser-
|
|
1
|
+
export { N as default } from './node-browser-79442920.js';
|
|
2
2
|
import './index-640d9f36.js';
|
package/exports/chain.js
CHANGED
|
@@ -469,6 +469,8 @@ class Contract extends Transaction {
|
|
|
469
469
|
globalThis.BigNumber = BigNumber;
|
|
470
470
|
// check if browser or local
|
|
471
471
|
class Chain extends Contract {
|
|
472
|
+
#lastBlockInQue;
|
|
473
|
+
#syncErrorCount = 0;
|
|
472
474
|
version;
|
|
473
475
|
#resolveErrored;
|
|
474
476
|
#state;
|
|
@@ -765,24 +767,50 @@ class Chain extends Contract {
|
|
|
765
767
|
if (this.#chainSyncing)
|
|
766
768
|
return 'already syncing';
|
|
767
769
|
const latest = await this.#getLatestBlock();
|
|
768
|
-
await this
|
|
770
|
+
await this.syncChain(latest);
|
|
769
771
|
return 'synced';
|
|
770
772
|
}
|
|
771
|
-
async
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
773
|
+
async syncChain(lastBlock) {
|
|
774
|
+
if (!lastBlock)
|
|
775
|
+
lastBlock = await this.#getLatestBlock();
|
|
776
|
+
if (this.#chainSyncing) {
|
|
777
|
+
if (!this.#lastBlockInQue || lastBlock.index > this.#lastBlockInQue.index)
|
|
778
|
+
this.#lastBlockInQue = lastBlock;
|
|
779
|
+
return 'syncing';
|
|
780
|
+
}
|
|
781
|
+
this.#chainSyncing = true;
|
|
782
|
+
const syncPromise = (lastBlock) => new Promise(async (resolve, reject) => {
|
|
783
|
+
setTimeout(() => {
|
|
784
|
+
reject('timedOut');
|
|
785
|
+
}, 10000);
|
|
786
|
+
try {
|
|
787
|
+
await this.#syncChain(lastBlock);
|
|
788
|
+
resolve(true);
|
|
789
|
+
}
|
|
790
|
+
catch (error) {
|
|
791
|
+
reject(error);
|
|
782
792
|
}
|
|
783
|
-
}
|
|
784
|
-
|
|
785
|
-
|
|
793
|
+
});
|
|
794
|
+
if (globalThis.peernet.connections.length === 0)
|
|
795
|
+
return 'connectionless';
|
|
796
|
+
try {
|
|
797
|
+
await syncPromise(lastBlock);
|
|
798
|
+
}
|
|
799
|
+
catch (error) {
|
|
800
|
+
console.log(error);
|
|
801
|
+
this.#syncErrorCount += 1;
|
|
802
|
+
if (this.#syncErrorCount <= 3)
|
|
803
|
+
return this.syncChain(this.#lastBlockInQue);
|
|
804
|
+
return 'errored';
|
|
805
|
+
}
|
|
806
|
+
this.#syncErrorCount = 0;
|
|
807
|
+
if (this.#lastBlockInQue)
|
|
808
|
+
return this.syncChain(this.#lastBlockInQue);
|
|
809
|
+
this.#chainSyncing = false;
|
|
810
|
+
return 'synced';
|
|
811
|
+
}
|
|
812
|
+
async #syncChain(lastBlock) {
|
|
813
|
+
if (this.#chainSyncing || !lastBlock || !lastBlock.hash || !lastBlock.index)
|
|
786
814
|
return;
|
|
787
815
|
this.#chainSyncing = true;
|
|
788
816
|
if (this.#knownBlocks?.length === Number(lastBlock.index) + 1) {
|
|
@@ -828,7 +856,7 @@ class Chain extends Contract {
|
|
|
828
856
|
console.log(lastBlock);
|
|
829
857
|
this.#resolveErrored = false;
|
|
830
858
|
if (lastBlock)
|
|
831
|
-
await this
|
|
859
|
+
await this.syncChain(lastBlock);
|
|
832
860
|
if (await this.hasTransactionToHandle() && !this.#resolveErrored && this.#participating)
|
|
833
861
|
this.#runEpoch();
|
|
834
862
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { BlockMessage } from '@leofcoin/messages';
|
|
2
2
|
import Contract from './contract.js';
|
|
3
3
|
import { BigNumberish } from '@ethersproject/bignumber';
|
|
4
|
+
declare type chainSyncState = 'syncing' | 'synced' | 'errored' | 'connectionless';
|
|
4
5
|
export default class Chain extends Contract {
|
|
5
6
|
#private;
|
|
6
7
|
version: String;
|
|
@@ -29,7 +30,8 @@ export default class Chain extends Contract {
|
|
|
29
30
|
hash: string;
|
|
30
31
|
previousHash: string;
|
|
31
32
|
}>;
|
|
32
|
-
triggerSync(): Promise<"
|
|
33
|
+
triggerSync(): Promise<"synced" | "already syncing">;
|
|
34
|
+
syncChain(lastBlock: any): Promise<chainSyncState>;
|
|
33
35
|
getAndPutBlock(hash: string): BlockMessage;
|
|
34
36
|
resolveBlock(hash: any): any;
|
|
35
37
|
resolveBlocks(): any;
|
|
@@ -94,3 +96,4 @@ export default class Chain extends Contract {
|
|
|
94
96
|
address: string;
|
|
95
97
|
};
|
|
96
98
|
}
|
|
99
|
+
export {};
|