@optimystic/db-p2p 0.7.0 → 0.9.1
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/cluster/client.d.ts.map +1 -1
- package/dist/src/cluster/client.js +7 -1
- package/dist/src/cluster/client.js.map +1 -1
- package/dist/src/cluster/cluster-repo.d.ts +2 -0
- package/dist/src/cluster/cluster-repo.d.ts.map +1 -1
- package/dist/src/cluster/cluster-repo.js +14 -14
- package/dist/src/cluster/cluster-repo.js.map +1 -1
- package/dist/src/dispute/dispute-service.d.ts.map +1 -1
- package/dist/src/dispute/dispute-service.js +4 -1
- package/dist/src/dispute/dispute-service.js.map +1 -1
- package/dist/src/libp2p-key-network.d.ts.map +1 -1
- package/dist/src/libp2p-key-network.js +5 -3
- package/dist/src/libp2p-key-network.js.map +1 -1
- package/dist/src/repo/cluster-coordinator.d.ts +2 -0
- package/dist/src/repo/cluster-coordinator.d.ts.map +1 -1
- package/dist/src/repo/cluster-coordinator.js +39 -17
- package/dist/src/repo/cluster-coordinator.js.map +1 -1
- package/dist/src/repo/coordinator-repo.js +2 -2
- package/dist/src/repo/coordinator-repo.js.map +1 -1
- package/dist/src/sync/service.d.ts +7 -7
- package/dist/src/sync/service.d.ts.map +1 -1
- package/dist/src/sync/service.js +42 -73
- package/dist/src/sync/service.js.map +1 -1
- package/package.json +5 -4
- package/src/cluster/client.ts +6 -1
- package/src/cluster/cluster-repo.ts +17 -13
- package/src/dispute/dispute-service.ts +5 -2
- package/src/libp2p-key-network.ts +5 -3
- package/src/repo/cluster-coordinator.ts +44 -18
- package/src/repo/coordinator-repo.ts +2 -2
- package/src/sync/service.ts +62 -96
|
@@ -207,11 +207,11 @@ export class CoordinatorRepo implements IRepo {
|
|
|
207
207
|
new Promise<undefined>(resolve => setTimeout(() => resolve(undefined), timeoutMs))
|
|
208
208
|
]);
|
|
209
209
|
|
|
210
|
-
// Query peers in parallel for their latest revision (with
|
|
210
|
+
// Query peers in parallel for their latest revision (with 1s timeout per peer)
|
|
211
211
|
const latestResults = await Promise.allSettled(
|
|
212
212
|
peerIds.map(peerIdStr => {
|
|
213
213
|
const peerId = peerIdFromString(peerIdStr);
|
|
214
|
-
return withTimeout(this.clusterLatestCallback!(peerId, blockId, context),
|
|
214
|
+
return withTimeout(this.clusterLatestCallback!(peerId, blockId, context), 1000);
|
|
215
215
|
})
|
|
216
216
|
);
|
|
217
217
|
|
package/src/sync/service.ts
CHANGED
|
@@ -5,6 +5,7 @@ import { pipe } from 'it-pipe';
|
|
|
5
5
|
import { fromString as u8FromString } from 'uint8arrays/from-string';
|
|
6
6
|
import { toString as u8ToString } from 'uint8arrays/to-string';
|
|
7
7
|
import * as lp from 'it-length-prefixed';
|
|
8
|
+
import type { Uint8ArrayList } from 'uint8arraylist';
|
|
8
9
|
|
|
9
10
|
export interface SyncServiceInit {
|
|
10
11
|
protocolPrefix?: string;
|
|
@@ -48,9 +49,7 @@ export class SyncService implements Startable {
|
|
|
48
49
|
async start(): Promise<void> {
|
|
49
50
|
if (this.running) return;
|
|
50
51
|
|
|
51
|
-
await this.registrar.handle(this.protocol,
|
|
52
|
-
await this.handleSyncRequest(data.stream);
|
|
53
|
-
});
|
|
52
|
+
await this.registrar.handle(this.protocol, this.handleSyncRequest.bind(this));
|
|
54
53
|
|
|
55
54
|
this.running = true;
|
|
56
55
|
this.log('Sync service started on protocol %s', this.protocol);
|
|
@@ -65,104 +64,71 @@ export class SyncService implements Startable {
|
|
|
65
64
|
|
|
66
65
|
/**
|
|
67
66
|
* Handle an incoming sync request stream.
|
|
67
|
+
* Uses a streaming pipeline (like the repo service) to process the
|
|
68
|
+
* first request and yield a response without waiting for the client
|
|
69
|
+
* to close its write side — avoids a read/write deadlock.
|
|
68
70
|
*/
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
request.rev ?? 'latest'
|
|
78
|
-
);
|
|
79
|
-
|
|
80
|
-
// Build archive from local storage
|
|
81
|
-
const archive = await this.buildArchive(
|
|
82
|
-
request.blockId,
|
|
83
|
-
request.rev,
|
|
84
|
-
request.includePending,
|
|
85
|
-
request.maxRevisions
|
|
86
|
-
);
|
|
87
|
-
|
|
88
|
-
// Send response
|
|
89
|
-
const response: SyncResponse = archive
|
|
90
|
-
? {
|
|
91
|
-
success: true,
|
|
92
|
-
archive,
|
|
93
|
-
responderId: stream.id
|
|
94
|
-
}
|
|
95
|
-
: {
|
|
96
|
-
success: false,
|
|
97
|
-
error: 'Block not found in local storage'
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
await this.sendResponse(stream, response);
|
|
101
|
-
|
|
102
|
-
this.log(
|
|
103
|
-
'[Ring Zulu] %s sync request for block %s',
|
|
104
|
-
response.success ? 'Fulfilled' : 'Failed',
|
|
105
|
-
request.blockId
|
|
106
|
-
);
|
|
107
|
-
} catch (error) {
|
|
108
|
-
this.log.error('Error handling sync request:', error);
|
|
109
|
-
|
|
110
|
-
// Try to send error response
|
|
71
|
+
/**
|
|
72
|
+
* Handle an incoming sync request stream.
|
|
73
|
+
* Uses a streaming pipeline (like the repo service) to process the
|
|
74
|
+
* request and yield a response immediately — avoids a read/write deadlock.
|
|
75
|
+
*/
|
|
76
|
+
private handleSyncRequest(stream: any): void {
|
|
77
|
+
const self = this;
|
|
78
|
+
void (async () => {
|
|
111
79
|
try {
|
|
112
|
-
const
|
|
113
|
-
|
|
114
|
-
|
|
80
|
+
const processStream = async function* (source: AsyncIterable<Uint8ArrayList> | Iterable<Uint8ArrayList>) {
|
|
81
|
+
for await (const msg of source) {
|
|
82
|
+
const json = u8ToString(msg.subarray(), 'utf8');
|
|
83
|
+
const request = JSON.parse(json) as SyncRequest;
|
|
84
|
+
|
|
85
|
+
self.log(
|
|
86
|
+
'[Ring Zulu] Received sync request for block %s revision %s',
|
|
87
|
+
request.blockId,
|
|
88
|
+
request.rev ?? 'latest'
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
const archive = await self.buildArchive(
|
|
92
|
+
request.blockId,
|
|
93
|
+
request.rev,
|
|
94
|
+
request.includePending,
|
|
95
|
+
request.maxRevisions
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const response: SyncResponse = archive
|
|
99
|
+
? { success: true, archive, responderId: stream.id ?? stream.stream?.id }
|
|
100
|
+
: { success: false, error: 'Block not found in local storage' };
|
|
101
|
+
|
|
102
|
+
self.log(
|
|
103
|
+
'[Ring Zulu] %s sync request for block %s',
|
|
104
|
+
response.success ? 'Fulfilled' : 'Failed',
|
|
105
|
+
request.blockId
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
yield new TextEncoder().encode(JSON.stringify(response));
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
115
111
|
};
|
|
116
|
-
await this.sendResponse(stream, errorResponse);
|
|
117
|
-
} catch (sendError) {
|
|
118
|
-
this.log.error('Failed to send error response:', sendError);
|
|
119
|
-
}
|
|
120
|
-
} finally {
|
|
121
|
-
try {
|
|
122
|
-
await stream.close();
|
|
123
|
-
} catch (closeError) {
|
|
124
|
-
// Ignore close errors
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
}
|
|
128
112
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
for await (const msg of source) {
|
|
141
|
-
messages.push(msg.subarray());
|
|
113
|
+
// Use the same streaming pipeline pattern as the repo service.
|
|
114
|
+
// The registrar handler receives the stream (or data object) directly.
|
|
115
|
+
const actualStream = stream.stream ?? stream;
|
|
116
|
+
const responses = pipe(
|
|
117
|
+
actualStream,
|
|
118
|
+
(source: any) => lp.decode(source),
|
|
119
|
+
processStream,
|
|
120
|
+
(source: any) => lp.encode(source)
|
|
121
|
+
);
|
|
122
|
+
for await (const chunk of responses) {
|
|
123
|
+
actualStream.send(chunk);
|
|
142
124
|
}
|
|
125
|
+
await actualStream.close();
|
|
126
|
+
} catch (error) {
|
|
127
|
+
self.log.error('Error handling sync request:', error);
|
|
128
|
+
const actualStream = stream.stream ?? stream;
|
|
129
|
+
try { actualStream.abort(error instanceof Error ? error : new Error(String(error))); } catch { /* ignore */ }
|
|
143
130
|
}
|
|
144
|
-
);
|
|
145
|
-
|
|
146
|
-
if (messages.length === 0) {
|
|
147
|
-
throw new Error('No request received');
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
const json = u8ToString(messages[0]!, 'utf8');
|
|
151
|
-
return JSON.parse(json) as SyncRequest;
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Send a sync response to the stream.
|
|
156
|
-
*/
|
|
157
|
-
private async sendResponse(stream: Stream, response: SyncResponse): Promise<void> {
|
|
158
|
-
const json = JSON.stringify(response);
|
|
159
|
-
const bytes = u8FromString(json, 'utf8');
|
|
160
|
-
|
|
161
|
-
// Use stream.send() instead of piping to stream.sink in libp2p v3
|
|
162
|
-
const encoded = pipe([bytes], lp.encode);
|
|
163
|
-
for await (const chunk of encoded) {
|
|
164
|
-
stream.send(chunk);
|
|
165
|
-
}
|
|
131
|
+
})();
|
|
166
132
|
}
|
|
167
133
|
|
|
168
134
|
/**
|
|
@@ -189,7 +155,7 @@ export class SyncService implements Startable {
|
|
|
189
155
|
const result = await this.repo.get({
|
|
190
156
|
blockIds: [blockId],
|
|
191
157
|
context
|
|
192
|
-
});
|
|
158
|
+
}, { skipClusterFetch: true } as any);
|
|
193
159
|
|
|
194
160
|
const blockResult = result[blockId];
|
|
195
161
|
if (!blockResult || !blockResult.state.latest) {
|