@peerbit/blocks 2.1.10 → 2.2.0
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/dist/src/libp2p.d.ts +3 -0
- package/dist/src/libp2p.d.ts.map +1 -1
- package/dist/src/libp2p.js +5 -3
- package/dist/src/libp2p.js.map +1 -1
- package/dist/src/remote.d.ts +23 -9
- package/dist/src/remote.d.ts.map +1 -1
- package/dist/src/remote.js +61 -17
- package/dist/src/remote.js.map +1 -1
- package/package.json +5 -5
- package/src/libp2p.ts +6 -3
- package/src/remote.ts +79 -22
package/dist/src/libp2p.d.ts
CHANGED
|
@@ -11,6 +11,9 @@ export declare class DirectBlock extends DirectStream implements IBlocks {
|
|
|
11
11
|
canRelayMessage?: boolean;
|
|
12
12
|
localTimeout?: number;
|
|
13
13
|
messageProcessingConcurrency?: number;
|
|
14
|
+
earlyBlocks?: boolean | {
|
|
15
|
+
cacheSize?: number;
|
|
16
|
+
};
|
|
14
17
|
});
|
|
15
18
|
put(bytes: Uint8Array): Promise<string>;
|
|
16
19
|
has(cid: string): Promise<boolean>;
|
package/dist/src/libp2p.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libp2p.d.ts","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,IAAI,OAAO,EAAE,MAAM,2BAA2B,CAAC;AAE/E,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAAE,KAAK,sBAAsB,EAAE,MAAM,iBAAiB,CAAC;AAK9D,MAAM,MAAM,qBAAqB,GAAG,sBAAsB,CAAC;AAE3D,qBAAa,WAAY,SAAQ,YAAa,YAAW,OAAO;IAC/D,OAAO,CAAC,YAAY,CAAe;IACnC,OAAO,CAAC,QAAQ,CAAM;IACtB,OAAO,CAAC,iBAAiB,CAAM;gBAG9B,UAAU,EAAE,qBA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M,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC;IAM7D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAOtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAOrB,IAAI;IAGV,IAAI,MAAM,mDAET;IAED,SAAS,IAAI,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;CAGvC"}
|
package/dist/src/libp2p.js
CHANGED
|
@@ -3,7 +3,7 @@ import { createStore } from "@peerbit/any-store";
|
|
|
3
3
|
import {} from "@peerbit/crypto";
|
|
4
4
|
import { DirectStream } from "@peerbit/stream";
|
|
5
5
|
import {} from "@peerbit/stream";
|
|
6
|
-
import {
|
|
6
|
+
import {} from "@peerbit/stream-interface";
|
|
7
7
|
import { AnyBlockStore } from "./any-blockstore.js";
|
|
8
8
|
import { BlockMessage, RemoteBlocks } from "./remote.js";
|
|
9
9
|
export class DirectBlock extends DirectStream {
|
|
@@ -21,15 +21,17 @@ export class DirectBlock extends DirectStream {
|
|
|
21
21
|
});
|
|
22
22
|
this.remoteBlocks = new RemoteBlocks({
|
|
23
23
|
local: new AnyBlockStore(createStore(options?.directory)),
|
|
24
|
-
publish: (message) => this.publish(serialize(message),
|
|
24
|
+
publish: (message, options) => this.publish(serialize(message), options),
|
|
25
25
|
localTimeout: options?.localTimeout || 1000,
|
|
26
26
|
messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
|
|
27
27
|
waitFor: this.waitFor.bind(this),
|
|
28
|
+
publicKey: this.publicKey,
|
|
29
|
+
earlyBlocks: options?.earlyBlocks,
|
|
28
30
|
});
|
|
29
31
|
this.onDataFn = (data) => {
|
|
30
32
|
data.detail?.data?.length &&
|
|
31
33
|
data.detail?.data.length > 0 &&
|
|
32
|
-
this.remoteBlocks.onMessage(deserialize(data.detail.data, BlockMessage));
|
|
34
|
+
this.remoteBlocks.onMessage(deserialize(data.detail.data, BlockMessage), data.detail.header.signatures?.publicKeys[0]?.hashcode());
|
|
33
35
|
};
|
|
34
36
|
this.onPeerConnectedFn = (evt) => this.remoteBlocks.onReachable(evt.detail);
|
|
35
37
|
}
|
package/dist/src/libp2p.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,OAAO,EAAsB,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAA+B,MAAM,iBAAiB,CAAC;AAC9D,OAAO,
|
|
1
|
+
{"version":3,"file":"libp2p.js","sourceRoot":"","sources":["../../src/libp2p.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AACxD,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AAEjD,OAAO,EAAsB,MAAM,iBAAiB,CAAC;AACrD,OAAO,EAAE,YAAY,EAAE,MAAM,iBAAiB,CAAC;AAC/C,OAAO,EAA+B,MAAM,iBAAiB,CAAC;AAC9D,OAAO,EAAoB,MAAM,2BAA2B,CAAC;AAC7D,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AACpD,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAIzD,MAAM,OAAO,WAAY,SAAQ,YAAY;IACpC,YAAY,CAAe;IAC3B,QAAQ,CAAM;IACd,iBAAiB,CAAM;IAE/B,YACC,UAAiC,EACjC,OAMC;QAED,KAAK,CAAC,UAAU,EAAE,CAAC,kBAAkB,CAAC,EAAE;YACvC,4BAA4B,EAAE,OAAO,EAAE,4BAA4B,IAAI,EAAE;YACzE,eAAe,EAAE,OAAO,EAAE,eAAe,IAAI,IAAI;YACjD,iBAAiB,E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AE,IAAI,CAAC,CAAC;IACvC,CAAC;IACD,IAAI,MAAM;QACT,OAAO,IAAI,CAAC,YAAY,EAAE,MAAM,IAAI,IAAI,CAAC,OAAO,CAAC;IAClD,CAAC;IAED,SAAS;QACR,OAAO,IAAI,CAAC,YAAY,CAAC,SAAS,EAAE,CAAC;IACtC,CAAC;CACD"}
|
package/dist/src/remote.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { PeerId } from "@libp2p/interface";
|
|
2
2
|
import { type GetOptions, type Blocks as IBlocks } from "@peerbit/blocks-interface";
|
|
3
3
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
4
|
+
import type { PublishOptions } from "@peerbit/stream";
|
|
4
5
|
import { AnyBlockStore } from "./any-blockstore.js";
|
|
5
6
|
import type { BlockStore } from "./interface.js";
|
|
6
7
|
export declare const logger: import("pino").Logger<never>;
|
|
@@ -20,14 +21,21 @@ export declare class RemoteBlocks implements IBlocks {
|
|
|
20
21
|
local: AnyBlockStore;
|
|
21
22
|
localTimeout?: number;
|
|
22
23
|
messageProcessingConcurrency?: number;
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
24
|
+
publicKey: PublicSignKey;
|
|
25
|
+
earlyBlocks?: boolean | {
|
|
26
|
+
cacheSize?: number;
|
|
27
|
+
};
|
|
28
|
+
publish: (data: BlockRequest | BlockResponse, options: PublishOptions) => Promise<Uint8Array | void>;
|
|
29
|
+
waitFor: (peer: PeerId | PublicSignKey | string, options?: {
|
|
30
|
+
timeout?: number;
|
|
31
|
+
signal?: AbortSignal;
|
|
32
|
+
neighbour?: boolean;
|
|
33
|
+
}) => Promise<void>;
|
|
27
34
|
};
|
|
28
35
|
localStore: BlockStore;
|
|
29
36
|
private _responseHandler?;
|
|
30
37
|
private _resolvers;
|
|
38
|
+
private _blockCache?;
|
|
31
39
|
private _loadFetchQueue;
|
|
32
40
|
private _readFromPeersPromises;
|
|
33
41
|
_open: boolean;
|
|
@@ -37,10 +45,16 @@ export declare class RemoteBlocks implements IBlocks {
|
|
|
37
45
|
local: AnyBlockStore;
|
|
38
46
|
localTimeout?: number;
|
|
39
47
|
messageProcessingConcurrency?: number;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
48
|
+
publicKey: PublicSignKey;
|
|
49
|
+
earlyBlocks?: boolean | {
|
|
50
|
+
cacheSize?: number;
|
|
51
|
+
};
|
|
52
|
+
publish: (data: BlockRequest | BlockResponse, options: PublishOptions) => Promise<Uint8Array | void>;
|
|
53
|
+
waitFor: (peer: PeerId | PublicSignKey | string, options?: {
|
|
54
|
+
timeout?: number;
|
|
55
|
+
signal?: AbortSignal;
|
|
56
|
+
neighbour?: boolean;
|
|
57
|
+
}) => Promise<void>;
|
|
44
58
|
});
|
|
45
59
|
put(bytes: Uint8Array): Promise<string>;
|
|
46
60
|
has(cid: string): Promise<boolean>;
|
|
@@ -48,7 +62,7 @@ export declare class RemoteBlocks implements IBlocks {
|
|
|
48
62
|
rm(cid: string): Promise<void>;
|
|
49
63
|
iterator(): AsyncGenerator<[string, Uint8Array], void, void>;
|
|
50
64
|
start(): Promise<void>;
|
|
51
|
-
onMessage(data: BlockMessage): any;
|
|
65
|
+
onMessage(data: BlockMessage, from?: string): any;
|
|
52
66
|
onReachable(publicKey: PublicSignKey): void;
|
|
53
67
|
private handleFetchRequest;
|
|
54
68
|
private _readFromPeers;
|
package/dist/src/remote.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,mBAAmB,CAAC;AAEhD,OAAO,EACN,KAAK,UAAU,EACf,KAAK,MAAM,IAAI,OAAO,EAKtB,MAAM,2BAA2B,CAAC;
|
|
1
|
+
{"version":3,"file":"remote.d.ts","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":"AACA,O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MAAM;IAIb,QAAQ,IAAI,cAAc,CAAC,CAAC,MAAM,EAAE,UAAU,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC;IAM7D,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAO5B,SAAS,CAAC,IAAI,EAAE,YAAY,EAAE,IAAI,CAAC,EAAE,MAAM;IAG3C,WAAW,CAAC,SAAS,EAAE,aAAa;YAMtB,kBAAkB;YAsBlB,cAAc;IAuGtB,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC;IAe3B,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAI9C,IAAI;IAIV,IAAI,MAAM,yCAMT;IAED,SAAS,IAAI,OAAO,GAAG,OAAO,CAAC,OAAO,CAAC;CAGvC"}
|
package/dist/src/remote.js
CHANGED
|
@@ -10,8 +10,10 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
10
10
|
import { field, variant } from "@dao-xyz/borsh";
|
|
11
11
|
import { TypedEventEmitter } from "@libp2p/interface";
|
|
12
12
|
import { checkDecodeBlock, cidifyString, codecCodes, stringifyCid, } from "@peerbit/blocks-interface";
|
|
13
|
+
import { Cache } from "@peerbit/cache";
|
|
13
14
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
14
15
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
16
|
+
import { AnyWhere, SilentDelivery } from "@peerbit/stream-interface";
|
|
15
17
|
import { AbortError } from "@peerbit/time";
|
|
16
18
|
import { CID } from "multiformats";
|
|
17
19
|
import {} from "multiformats/block";
|
|
@@ -63,6 +65,7 @@ export class RemoteBlocks {
|
|
|
63
65
|
localStore;
|
|
64
66
|
_responseHandler;
|
|
65
67
|
_resolvers;
|
|
68
|
+
_blockCache;
|
|
66
69
|
_loadFetchQueue;
|
|
67
70
|
_readFromPeersPromises;
|
|
68
71
|
_open = false;
|
|
@@ -77,14 +80,31 @@ export class RemoteBlocks {
|
|
|
77
80
|
this.localStore = options?.local;
|
|
78
81
|
this._resolvers = new Map();
|
|
79
82
|
this._readFromPeersPromises = new Map();
|
|
80
|
-
this.
|
|
83
|
+
this._blockCache = options?.earlyBlocks
|
|
84
|
+
? new Cache({
|
|
85
|
+
max: typeof options.earlyBlocks === "boolean"
|
|
86
|
+
? 1e3
|
|
87
|
+
: (options.earlyBlocks.cacheSize ?? 1e3),
|
|
88
|
+
ttl: 1e4,
|
|
89
|
+
})
|
|
90
|
+
: undefined;
|
|
91
|
+
this._responseHandler = async (message, from) => {
|
|
81
92
|
try {
|
|
82
93
|
if (message instanceof BlockRequest && this.localStore) {
|
|
83
|
-
this._loadFetchQueue.add(() => this.handleFetchRequest(message, localTimeout));
|
|
94
|
+
this._loadFetchQueue.add(() => this.handleFetchRequest(message, localTimeout, from));
|
|
84
95
|
}
|
|
85
96
|
else if (message instanceof BlockResponse) {
|
|
86
97
|
// TODO make sure we are not storing too much bytes in ram (like filter large blocks)
|
|
87
|
-
this._resolvers.get(message.cid)
|
|
98
|
+
let resolver = this._resolvers.get(message.cid);
|
|
99
|
+
if (!resolver) {
|
|
100
|
+
if (options.earlyBlocks) {
|
|
101
|
+
// wait for the resolve to exist
|
|
102
|
+
this._blockCache.add(message.cid, message.bytes);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
resolver(message.bytes);
|
|
107
|
+
}
|
|
88
108
|
}
|
|
89
109
|
}
|
|
90
110
|
catch (error) {
|
|
@@ -133,13 +153,17 @@ export class RemoteBlocks {
|
|
|
133
153
|
await this.localStore?.start();
|
|
134
154
|
this._open = true;
|
|
135
155
|
}
|
|
136
|
-
onMessage(data) {
|
|
137
|
-
return this._responseHandler(data);
|
|
156
|
+
onMessage(data, from) {
|
|
157
|
+
return this._responseHandler(data, from);
|
|
138
158
|
}
|
|
139
159
|
onReachable(publicKey) {
|
|
140
160
|
this._events.dispatchEvent(new CustomEvent("peer:reachable", { detail: publicKey }));
|
|
141
161
|
}
|
|
142
|
-
async handleFetchRequest(request, localTimeout) {
|
|
162
|
+
async handleFetchRequest(request, localTimeout, from) {
|
|
163
|
+
if (!from) {
|
|
164
|
+
logger.warn("No from in handleFetchRequest");
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
143
167
|
const cid = stringifyCid(request.cid);
|
|
144
168
|
const bytes = await this.localStore.get(cid, {
|
|
145
169
|
remote: {
|
|
@@ -149,10 +173,30 @@ export class RemoteBlocks {
|
|
|
149
173
|
if (!bytes) {
|
|
150
174
|
return;
|
|
151
175
|
}
|
|
152
|
-
await this.options.publish(new BlockResponse(cid, bytes));
|
|
176
|
+
await this.options.publish(new BlockResponse(cid, bytes), { to: [from] });
|
|
153
177
|
}
|
|
154
178
|
async _readFromPeers(cidString, cidObject, options = {}) {
|
|
155
179
|
const codec = codecCodes[cidObject.code];
|
|
180
|
+
const tryDecode = async (bytes) => {
|
|
181
|
+
const value = await checkDecodeBlock(cidObject, bytes, {
|
|
182
|
+
codec,
|
|
183
|
+
hasher: options?.hasher,
|
|
184
|
+
});
|
|
185
|
+
return value;
|
|
186
|
+
};
|
|
187
|
+
const cachedValue = this.options.earlyBlocks
|
|
188
|
+
? this._blockCache?.get(cidString)
|
|
189
|
+
: undefined;
|
|
190
|
+
if (cachedValue) {
|
|
191
|
+
this._blockCache.del(cidString);
|
|
192
|
+
try {
|
|
193
|
+
const result = await tryDecode(cachedValue);
|
|
194
|
+
return result.bytes;
|
|
195
|
+
}
|
|
196
|
+
catch (error) {
|
|
197
|
+
// ignore
|
|
198
|
+
}
|
|
199
|
+
}
|
|
156
200
|
let promise = this._readFromPeersPromises.get(cidString);
|
|
157
201
|
if (!promise) {
|
|
158
202
|
promise = new Promise((resolve, reject) => {
|
|
@@ -169,10 +213,7 @@ export class RemoteBlocks {
|
|
|
169
213
|
this.closeController.signal.addEventListener("abort", abortHandler);
|
|
170
214
|
options?.signal?.addEventListener("abort", abortHandler);
|
|
171
215
|
this._resolvers.set(cidString, async (bytes) => {
|
|
172
|
-
const value = await
|
|
173
|
-
codec,
|
|
174
|
-
hasher: options?.hasher,
|
|
175
|
-
});
|
|
216
|
+
const value = await tryDecode(bytes);
|
|
176
217
|
clearTimeout(timeoutCallback);
|
|
177
218
|
this._resolvers.delete(cidString); // TODO concurrency might not work as expected here
|
|
178
219
|
this.closeController.signal.removeEventListener("abort", abortHandler);
|
|
@@ -180,19 +221,21 @@ export class RemoteBlocks {
|
|
|
180
221
|
});
|
|
181
222
|
});
|
|
182
223
|
this._readFromPeersPromises.set(cidString, promise);
|
|
183
|
-
const
|
|
224
|
+
const publishOnNewPeers = (e) => {
|
|
225
|
+
const to = e.detail.hashcode();
|
|
184
226
|
if (!options?.from || options.from.includes(to)) {
|
|
185
227
|
return this.options.publish(new BlockRequest(cidString), {
|
|
228
|
+
// We dont sent explicitly to 'to' here because we want the message to propagate beyond the first peer
|
|
186
229
|
to: [to],
|
|
230
|
+
mode: new AnyWhere(),
|
|
187
231
|
});
|
|
188
232
|
}
|
|
189
233
|
};
|
|
190
|
-
const publishOnNewPeers = (e) => {
|
|
191
|
-
return publish(e.detail.hashcode());
|
|
192
|
-
};
|
|
193
234
|
this._events.addEventListener("peer:reachable", publishOnNewPeers);
|
|
194
|
-
this.options.publish(new BlockRequest(cidString), {
|
|
195
|
-
|
|
235
|
+
await this.options.publish(new BlockRequest(cidString), {
|
|
236
|
+
mode: options.from
|
|
237
|
+
? new SilentDelivery({ to: options.from, redundancy: 1 })
|
|
238
|
+
: new AnyWhere(),
|
|
196
239
|
});
|
|
197
240
|
// we want to make sure that if some new peers join, we also try to ask them
|
|
198
241
|
const result = await promise;
|
|
@@ -215,6 +258,7 @@ export class RemoteBlocks {
|
|
|
215
258
|
await this.localStore?.stop();
|
|
216
259
|
this._readFromPeersPromises.clear();
|
|
217
260
|
this._resolvers.clear();
|
|
261
|
+
this._blockCache?.clear();
|
|
218
262
|
this._open = false;
|
|
219
263
|
// we dont cleanup subscription because we dont know if someone else is sbuscribing also
|
|
220
264
|
}
|
package/dist/src/remote.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAGN,gBAAgB,EAChB,YAAY,EACZ,UAAU,EACV,YAAY,GACZ,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"remote.js","sourceRoot":"","sources":["../../src/remote.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,gBAAgB,CAAC;AAEhD,OAAO,EAAE,iBAAiB,EAAE,MAAM,mBAAmB,CAAC;AACtD,OAAO,EAGN,gBAAgB,EAChB,YAAY,EACZ,UAAU,EACV,YAAY,GACZ,MAAM,2BAA2B,CAAC;AACnC,OAAO,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAChD,OAAO,EAAE,MAAM,IAAI,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAErD,OAAO,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAC;AACrE,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,GAAG,EAAE,MAAM,cAAc,CAAC;AACnC,OAAO,EAAc,MAAM,oBAAoB,CAAC;AAChD,OAAO,MAAM,MAAM,SAAS,CAAC;AAC7B,OAAO,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAGpD,MAAM,CAAC,MAAM,MAAM,GAAG,QAAQ,CAAC,EAAE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ED,KAAK,CAAC,IAAI;QACT,yCAAyC;QAEzC,8BAA8B;QAC9B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,IAAI,CAAC,eAAe,CAAC,KAAK,EAAE,CAAC;QAC7B,MAAM,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE,CAAC,CAAC,mBAAmB;QACxD,MAAM,IAAI,CAAC,UAAU,EAAE,IAAI,EAAE,CAAC;QAC9B,IAAI,CAAC,sBAAsB,CAAC,KAAK,EAAE,CAAC;QACpC,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;QACxB,IAAI,CAAC,WAAW,EAAE,KAAK,EAAE,CAAC;QAC1B,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;QACnB,wFAAwF;IACzF,CAAC;IAED,OAAO,CAAC,IAA4B;QACnC,OAAO,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC;IACnC,CAAC;IAED,KAAK,CAAC,IAAI;QACT,OAAO,IAAI,CAAC,UAAU,CAAC,IAAI,EAAE,CAAC;IAC/B,CAAC;IAED,IAAI,MAAM;QACT,IAAI,IAAI,CAAC,KAAK,EAAE,CAAC;YAChB,OAAO,IAAI,CAAC,UAAU,EAAE,MAAM,EAAE,CAAC;QAClC,CAAC;aAAM,CAAC;YACP,OAAO,QAAQ,CAAC;QACjB,CAAC;IACF,CAAC;IAED,SAAS;QACR,OAAO,IAAI,CAAC,UAAU,EAAE,SAAS,EAAE,IAAI,KAAK,CAAC;IAC9C,CAAC;CACD"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@peerbit/blocks",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Block store streaming",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/src/index.d.ts",
|
|
@@ -78,10 +78,10 @@
|
|
|
78
78
|
},
|
|
79
79
|
"dependencies": {
|
|
80
80
|
"@dao-xyz/borsh": "^5.2.3",
|
|
81
|
-
"@peerbit/any-store": "^2.1.
|
|
82
|
-
"@peerbit/stream": "4.
|
|
83
|
-
"@peerbit/blocks-interface": "1.3.
|
|
84
|
-
"@peerbit/crypto": "2.3.
|
|
81
|
+
"@peerbit/any-store": "^2.1.3",
|
|
82
|
+
"@peerbit/stream": "4.2.0",
|
|
83
|
+
"@peerbit/blocks-interface": "1.3.9",
|
|
84
|
+
"@peerbit/crypto": "2.3.3",
|
|
85
85
|
"@ipld/dag-cbor": "^9.2.1",
|
|
86
86
|
"multiformats": "^13.0.1"
|
|
87
87
|
}
|
package/src/libp2p.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { GetOptions, Blocks as IBlocks } from "@peerbit/blocks-interface";
|
|
|
4
4
|
import { type PublicSignKey } from "@peerbit/crypto";
|
|
5
5
|
import { DirectStream } from "@peerbit/stream";
|
|
6
6
|
import { type DirectStreamComponents } from "@peerbit/stream";
|
|
7
|
-
import {
|
|
7
|
+
import { type DataMessage } from "@peerbit/stream-interface";
|
|
8
8
|
import { AnyBlockStore } from "./any-blockstore.js";
|
|
9
9
|
import { BlockMessage, RemoteBlocks } from "./remote.js";
|
|
10
10
|
|
|
@@ -22,6 +22,7 @@ export class DirectBlock extends DirectStream implements IBlocks {
|
|
|
22
22
|
canRelayMessage?: boolean;
|
|
23
23
|
localTimeout?: number;
|
|
24
24
|
messageProcessingConcurrency?: number;
|
|
25
|
+
earlyBlocks?: boolean | { cacheSize?: number };
|
|
25
26
|
},
|
|
26
27
|
) {
|
|
27
28
|
super(components, ["/lazyblock/0.0.0"], {
|
|
@@ -34,11 +35,12 @@ export class DirectBlock extends DirectStream implements IBlocks {
|
|
|
34
35
|
});
|
|
35
36
|
this.remoteBlocks = new RemoteBlocks({
|
|
36
37
|
local: new AnyBlockStore(createStore(options?.directory)),
|
|
37
|
-
publish: (message) =>
|
|
38
|
-
this.publish(serialize(message), { mode: new AnyWhere() }),
|
|
38
|
+
publish: (message, options) => this.publish(serialize(message), options),
|
|
39
39
|
localTimeout: options?.localTimeout || 1000,
|
|
40
40
|
messageProcessingConcurrency: options?.messageProcessingConcurrency || 10,
|
|
41
41
|
waitFor: this.waitFor.bind(this),
|
|
42
|
+
publicKey: this.publicKey,
|
|
43
|
+
earlyBlocks: options?.earlyBlocks,
|
|
42
44
|
});
|
|
43
45
|
|
|
44
46
|
this.onDataFn = (data: CustomEvent<DataMessage>) => {
|
|
@@ -46,6 +48,7 @@ export class DirectBlock extends DirectStream implements IBlocks {
|
|
|
46
48
|
data.detail?.data.length > 0 &&
|
|
47
49
|
this.remoteBlocks.onMessage(
|
|
48
50
|
deserialize(data.detail.data!, BlockMessage),
|
|
51
|
+
data.detail.header.signatures?.publicKeys[0]?.hashcode(),
|
|
49
52
|
);
|
|
50
53
|
};
|
|
51
54
|
this.onPeerConnectedFn = (evt: CustomEvent<PublicSignKey>) =>
|
package/src/remote.ts
CHANGED
|
@@ -9,8 +9,11 @@ import {
|
|
|
9
9
|
codecCodes,
|
|
10
10
|
stringifyCid,
|
|
11
11
|
} from "@peerbit/blocks-interface";
|
|
12
|
+
import { Cache } from "@peerbit/cache";
|
|
12
13
|
import { PublicSignKey } from "@peerbit/crypto";
|
|
13
14
|
import { logger as loggerFn } from "@peerbit/logger";
|
|
15
|
+
import type { PublishOptions } from "@peerbit/stream";
|
|
16
|
+
import { AnyWhere, SilentDelivery } from "@peerbit/stream-interface";
|
|
14
17
|
import { AbortError } from "@peerbit/time";
|
|
15
18
|
import { CID } from "multiformats";
|
|
16
19
|
import { type Block } from "multiformats/block";
|
|
@@ -51,8 +54,10 @@ export class BlockResponse extends BlockMessage {
|
|
|
51
54
|
export class RemoteBlocks implements IBlocks {
|
|
52
55
|
localStore: BlockStore;
|
|
53
56
|
|
|
54
|
-
private _responseHandler?: (data: BlockMessage) => any;
|
|
57
|
+
private _responseHandler?: (data: BlockMessage, from?: string) => any;
|
|
55
58
|
private _resolvers: Map<string, (data: Uint8Array) => void>;
|
|
59
|
+
private _blockCache?: Cache<Uint8Array>;
|
|
60
|
+
|
|
56
61
|
private _loadFetchQueue: PQueue;
|
|
57
62
|
private _readFromPeersPromises: Map<
|
|
58
63
|
string,
|
|
@@ -69,11 +74,20 @@ export class RemoteBlocks implements IBlocks {
|
|
|
69
74
|
local: AnyBlockStore;
|
|
70
75
|
localTimeout?: number;
|
|
71
76
|
messageProcessingConcurrency?: number;
|
|
77
|
+
publicKey: PublicSignKey;
|
|
78
|
+
earlyBlocks?: boolean | { cacheSize?: number };
|
|
72
79
|
publish: (
|
|
73
|
-
|
|
74
|
-
options
|
|
80
|
+
data: BlockRequest | BlockResponse,
|
|
81
|
+
options: PublishOptions,
|
|
75
82
|
) => Promise<Uint8Array | void>;
|
|
76
|
-
waitFor
|
|
83
|
+
waitFor: (
|
|
84
|
+
peer: PeerId | PublicSignKey | string,
|
|
85
|
+
options?: {
|
|
86
|
+
timeout?: number;
|
|
87
|
+
signal?: AbortSignal;
|
|
88
|
+
neighbour?: boolean;
|
|
89
|
+
},
|
|
90
|
+
) => Promise<void>;
|
|
77
91
|
},
|
|
78
92
|
) {
|
|
79
93
|
const localTimeout = options?.localTimeout || 1000;
|
|
@@ -83,17 +97,33 @@ export class RemoteBlocks implements IBlocks {
|
|
|
83
97
|
this.localStore = options?.local;
|
|
84
98
|
this._resolvers = new Map();
|
|
85
99
|
this._readFromPeersPromises = new Map();
|
|
100
|
+
this._blockCache = options?.earlyBlocks
|
|
101
|
+
? new Cache<Uint8Array>({
|
|
102
|
+
max:
|
|
103
|
+
typeof options.earlyBlocks === "boolean"
|
|
104
|
+
? 1e3
|
|
105
|
+
: (options.earlyBlocks.cacheSize ?? 1e3),
|
|
106
|
+
ttl: 1e4,
|
|
107
|
+
})
|
|
108
|
+
: undefined;
|
|
86
109
|
|
|
87
|
-
this._responseHandler = async (message: BlockMessage) => {
|
|
110
|
+
this._responseHandler = async (message: BlockMessage, from?: string) => {
|
|
88
111
|
try {
|
|
89
112
|
if (message instanceof BlockRequest && this.localStore) {
|
|
90
113
|
this._loadFetchQueue.add(() =>
|
|
91
|
-
this.handleFetchRequest(message, localTimeout),
|
|
114
|
+
this.handleFetchRequest(message, localTimeout, from),
|
|
92
115
|
);
|
|
93
116
|
} else if (message instanceof BlockResponse) {
|
|
94
117
|
// TODO make sure we are not storing too much bytes in ram (like filter large blocks)
|
|
95
|
-
|
|
96
|
-
|
|
118
|
+
let resolver = this._resolvers.get(message.cid);
|
|
119
|
+
if (!resolver) {
|
|
120
|
+
if (options.earlyBlocks) {
|
|
121
|
+
// wait for the resolve to exist
|
|
122
|
+
this._blockCache!.add(message.cid, message.bytes);
|
|
123
|
+
}
|
|
124
|
+
} else {
|
|
125
|
+
resolver(message.bytes);
|
|
126
|
+
}
|
|
97
127
|
}
|
|
98
128
|
} catch (error) {
|
|
99
129
|
logger.error("Got error for libp2p block transport: ", error);
|
|
@@ -151,8 +181,8 @@ export class RemoteBlocks implements IBlocks {
|
|
|
151
181
|
this._open = true;
|
|
152
182
|
}
|
|
153
183
|
|
|
154
|
-
onMessage(data: BlockMessage) {
|
|
155
|
-
return this._responseHandler!(data);
|
|
184
|
+
onMessage(data: BlockMessage, from?: string) {
|
|
185
|
+
return this._responseHandler!(data, from);
|
|
156
186
|
}
|
|
157
187
|
onReachable(publicKey: PublicSignKey) {
|
|
158
188
|
this._events.dispatchEvent(
|
|
@@ -163,17 +193,23 @@ export class RemoteBlocks implements IBlocks {
|
|
|
163
193
|
private async handleFetchRequest(
|
|
164
194
|
request: BlockRequest,
|
|
165
195
|
localTimeout: number,
|
|
196
|
+
from?: string,
|
|
166
197
|
) {
|
|
198
|
+
if (!from) {
|
|
199
|
+
logger.warn("No from in handleFetchRequest");
|
|
200
|
+
return;
|
|
201
|
+
}
|
|
167
202
|
const cid = stringifyCid(request.cid);
|
|
168
203
|
const bytes = await this.localStore.get(cid, {
|
|
169
204
|
remote: {
|
|
170
205
|
timeout: localTimeout,
|
|
171
206
|
},
|
|
172
207
|
});
|
|
208
|
+
|
|
173
209
|
if (!bytes) {
|
|
174
210
|
return;
|
|
175
211
|
}
|
|
176
|
-
await this.options.publish(new BlockResponse(cid, bytes));
|
|
212
|
+
await this.options.publish(new BlockResponse(cid, bytes), { to: [from] });
|
|
177
213
|
}
|
|
178
214
|
|
|
179
215
|
private async _readFromPeers(
|
|
@@ -187,6 +223,28 @@ export class RemoteBlocks implements IBlocks {
|
|
|
187
223
|
} = {},
|
|
188
224
|
): Promise<Uint8Array | undefined> {
|
|
189
225
|
const codec = (codecCodes as any)[cidObject.code];
|
|
226
|
+
|
|
227
|
+
const tryDecode = async (bytes: Uint8Array) => {
|
|
228
|
+
const value = await checkDecodeBlock(cidObject, bytes, {
|
|
229
|
+
codec,
|
|
230
|
+
hasher: options?.hasher,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
return value;
|
|
234
|
+
};
|
|
235
|
+
const cachedValue = this.options.earlyBlocks
|
|
236
|
+
? this._blockCache?.get(cidString)
|
|
237
|
+
: undefined;
|
|
238
|
+
if (cachedValue) {
|
|
239
|
+
this._blockCache.del(cidString);
|
|
240
|
+
try {
|
|
241
|
+
const result = await tryDecode(cachedValue);
|
|
242
|
+
return result.bytes;
|
|
243
|
+
} catch (error) {
|
|
244
|
+
// ignore
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
|
|
190
248
|
let promise = this._readFromPeersPromises.get(cidString);
|
|
191
249
|
if (!promise) {
|
|
192
250
|
promise = new Promise<Block<any, any, any, 1> | undefined>(
|
|
@@ -211,10 +269,7 @@ export class RemoteBlocks implements IBlocks {
|
|
|
211
269
|
options?.signal?.addEventListener("abort", abortHandler);
|
|
212
270
|
|
|
213
271
|
this._resolvers.set(cidString, async (bytes: Uint8Array) => {
|
|
214
|
-
const value = await
|
|
215
|
-
codec,
|
|
216
|
-
hasher: options?.hasher,
|
|
217
|
-
});
|
|
272
|
+
const value = await tryDecode(bytes);
|
|
218
273
|
|
|
219
274
|
clearTimeout(timeoutCallback);
|
|
220
275
|
this._resolvers.delete(cidString); // TODO concurrency might not work as expected here
|
|
@@ -229,22 +284,23 @@ export class RemoteBlocks implements IBlocks {
|
|
|
229
284
|
|
|
230
285
|
this._readFromPeersPromises.set(cidString, promise);
|
|
231
286
|
|
|
232
|
-
const
|
|
287
|
+
const publishOnNewPeers = (e: CustomEvent<PublicSignKey>) => {
|
|
288
|
+
const to = e.detail.hashcode();
|
|
233
289
|
if (!options?.from || options.from.includes(to)) {
|
|
234
290
|
return this.options.publish(new BlockRequest(cidString), {
|
|
291
|
+
// We dont sent explicitly to 'to' here because we want the message to propagate beyond the first peer
|
|
235
292
|
to: [to],
|
|
293
|
+
mode: new AnyWhere(),
|
|
236
294
|
});
|
|
237
295
|
}
|
|
238
296
|
};
|
|
239
297
|
|
|
240
|
-
const publishOnNewPeers = (e: CustomEvent<PublicSignKey>) => {
|
|
241
|
-
return publish(e.detail.hashcode());
|
|
242
|
-
};
|
|
243
298
|
this._events.addEventListener("peer:reachable", publishOnNewPeers);
|
|
244
|
-
this.options.publish(new BlockRequest(cidString), {
|
|
245
|
-
|
|
299
|
+
await this.options.publish(new BlockRequest(cidString), {
|
|
300
|
+
mode: options.from
|
|
301
|
+
? new SilentDelivery({ to: options.from, redundancy: 1 })
|
|
302
|
+
: new AnyWhere(),
|
|
246
303
|
});
|
|
247
|
-
|
|
248
304
|
// we want to make sure that if some new peers join, we also try to ask them
|
|
249
305
|
|
|
250
306
|
const result = await promise;
|
|
@@ -269,6 +325,7 @@ export class RemoteBlocks implements IBlocks {
|
|
|
269
325
|
await this.localStore?.stop();
|
|
270
326
|
this._readFromPeersPromises.clear();
|
|
271
327
|
this._resolvers.clear();
|
|
328
|
+
this._blockCache?.clear();
|
|
272
329
|
this._open = false;
|
|
273
330
|
// we dont cleanup subscription because we dont know if someone else is sbuscribing also
|
|
274
331
|
}
|