web-dc-api 0.1.5 → 0.1.6
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/cjs/index.js +1 -1
- package/dist/dc.min.js +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/index.d.ts +934 -878
- package/package.json +4 -8
- package/dist/cjs/helia-core-B1Xqha7a.js +0 -1
- package/dist/cjs/helia-core-D8Uv1KjQ.js +0 -1
- package/dist/cjs/polkadot-api-7PhQf3ws.js +0 -1
- package/dist/cjs/polkadot-api-CtrJVWuZ.js +0 -1
- package/dist/esm/chunks/helia-core-BxMqyK2Y.js +0 -1
- package/dist/esm/chunks/helia-core-DMXRpcO-.js +0 -1
- package/dist/esm/chunks/polkadot-api-5Y9Bw8VT.js +0 -1
- package/dist/esm/chunks/polkadot-api-D69Ioun_.js +0 -1
- package/lib/common/blowfish/block.ts +0 -259
- package/lib/common/blowfish/cipher.ts +0 -144
- package/lib/common/blowfish/const.ts +0 -195
- package/lib/common/chain.ts +0 -469
- package/lib/common/commonclient.ts +0 -202
- package/lib/common/constants.ts +0 -55
- package/lib/common/dc-key/ed25519.ts +0 -343
- package/lib/common/dc-key/keyManager.ts +0 -424
- package/lib/common/dcapi.ts +0 -98
- package/lib/common/dcutil.ts +0 -627
- package/lib/common/define.ts +0 -70
- package/lib/common/error.ts +0 -67
- package/lib/common/grpc-dc.ts +0 -104
- package/lib/common/module-system.ts +0 -184
- package/lib/common/service-worker.ts +0 -234
- package/lib/common/types/types.ts +0 -344
- package/lib/dc.ts +0 -701
- package/lib/implements/account/client.ts +0 -185
- package/lib/implements/account/manager.ts +0 -683
- package/lib/implements/aiproxy/client.ts +0 -357
- package/lib/implements/aiproxy/manager.ts +0 -670
- package/lib/implements/cache/client.ts +0 -105
- package/lib/implements/cache/manager.ts +0 -127
- package/lib/implements/comment/client.ts +0 -982
- package/lib/implements/comment/manager.ts +0 -1151
- package/lib/implements/dc/client.ts +0 -51
- package/lib/implements/dc/manager.ts +0 -33
- package/lib/implements/file/client.ts +0 -253
- package/lib/implements/file/file-cache-manager.ts +0 -142
- package/lib/implements/file/manager.ts +0 -1240
- package/lib/implements/file/seekableFileStream.ts +0 -344
- package/lib/implements/file/streamwriter.ts +0 -322
- package/lib/implements/keyvalue/client.ts +0 -376
- package/lib/implements/keyvalue/manager.ts +0 -759
- package/lib/implements/message/client.ts +0 -250
- package/lib/implements/message/manager.ts +0 -215
- package/lib/implements/threaddb/cbor/coding.ts +0 -62
- package/lib/implements/threaddb/cbor/event.ts +0 -336
- package/lib/implements/threaddb/cbor/node.ts +0 -542
- package/lib/implements/threaddb/cbor/record.ts +0 -398
- package/lib/implements/threaddb/common/AsyncMutex.ts +0 -24
- package/lib/implements/threaddb/common/addrinfo.ts +0 -135
- package/lib/implements/threaddb/common/dispatcher.ts +0 -81
- package/lib/implements/threaddb/common/idbstore-adapter.ts +0 -260
- package/lib/implements/threaddb/common/json-patcher.ts +0 -204
- package/lib/implements/threaddb/common/key.ts +0 -290
- package/lib/implements/threaddb/common/level-adapter.ts +0 -235
- package/lib/implements/threaddb/common/lineReader.ts +0 -79
- package/lib/implements/threaddb/common/logstore.ts +0 -215
- package/lib/implements/threaddb/common/transformed-datastore.ts +0 -308
- package/lib/implements/threaddb/core/app.ts +0 -206
- package/lib/implements/threaddb/core/core.ts +0 -230
- package/lib/implements/threaddb/core/db.ts +0 -249
- package/lib/implements/threaddb/core/event.ts +0 -54
- package/lib/implements/threaddb/core/head.ts +0 -89
- package/lib/implements/threaddb/core/identity.ts +0 -171
- package/lib/implements/threaddb/core/logstore.ts +0 -137
- package/lib/implements/threaddb/core/options.ts +0 -14
- package/lib/implements/threaddb/core/record.ts +0 -54
- package/lib/implements/threaddb/db/collection.ts +0 -1910
- package/lib/implements/threaddb/db/db.ts +0 -698
- package/lib/implements/threaddb/db/json2Query.ts +0 -192
- package/lib/implements/threaddb/db/query.ts +0 -524
- package/lib/implements/threaddb/dbclient.ts +0 -543
- package/lib/implements/threaddb/dbmanager.ts +0 -1906
- package/lib/implements/threaddb/lsstoreds/addr_book.ts +0 -549
- package/lib/implements/threaddb/lsstoreds/cache.ts +0 -36
- package/lib/implements/threaddb/lsstoreds/cyclic_batch.ts +0 -87
- package/lib/implements/threaddb/lsstoreds/global.ts +0 -151
- package/lib/implements/threaddb/lsstoreds/headbook.ts +0 -373
- package/lib/implements/threaddb/lsstoreds/keybook.ts +0 -297
- package/lib/implements/threaddb/lsstoreds/logstore.ts +0 -29
- package/lib/implements/threaddb/lsstoreds/metadata.ts +0 -223
- package/lib/implements/threaddb/net/define.ts +0 -149
- package/lib/implements/threaddb/net/grpcClient.ts +0 -589
- package/lib/implements/threaddb/net/grpcserver.ts +0 -146
- package/lib/implements/threaddb/net/net.ts +0 -2047
- package/lib/implements/threaddb/pb/lstore.proto +0 -38
- package/lib/implements/threaddb/pb/lstore.ts +0 -393
- package/lib/implements/threaddb/pb/lstore_pb.d.ts +0 -433
- package/lib/implements/threaddb/pb/lstore_pb.js +0 -1085
- package/lib/implements/threaddb/pb/net.proto +0 -194
- package/lib/implements/threaddb/pb/net_pb.d.ts +0 -2349
- package/lib/implements/threaddb/pb/net_pb.js +0 -5525
- package/lib/implements/threaddb/pb/proto-custom-types.ts +0 -212
- package/lib/implements/util/client.ts +0 -72
- package/lib/implements/util/manager.ts +0 -146
- package/lib/implements/wallet/manager.ts +0 -671
- package/lib/index.ts +0 -57
- package/lib/interfaces/DCContext.ts +0 -51
- package/lib/interfaces/aiproxy-interface.ts +0 -145
- package/lib/interfaces/auth-interface.ts +0 -118
- package/lib/interfaces/cache-interface.ts +0 -22
- package/lib/interfaces/client-interface.ts +0 -11
- package/lib/interfaces/comment-interface.ts +0 -167
- package/lib/interfaces/components/news-component.ts +0 -0
- package/lib/interfaces/database-interface.ts +0 -169
- package/lib/interfaces/file-interface.ts +0 -120
- package/lib/interfaces/index.ts +0 -10
- package/lib/interfaces/keyvalue-interface.ts +0 -156
- package/lib/interfaces/message-interface.ts +0 -22
- package/lib/interfaces/util-interface.ts +0 -31
- package/lib/modules/aiproxy-module.ts +0 -246
- package/lib/modules/auth-module.ts +0 -753
- package/lib/modules/cache-module.ts +0 -99
- package/lib/modules/client-module.ts +0 -71
- package/lib/modules/comment-module.ts +0 -429
- package/lib/modules/components/news-components.ts +0 -390
- package/lib/modules/database-module.ts +0 -598
- package/lib/modules/file-module.ts +0 -291
- package/lib/modules/index.ts +0 -13
- package/lib/modules/keyvalue-module.ts +0 -379
- package/lib/modules/message-module.ts +0 -107
- package/lib/modules/util-module.ts +0 -148
- package/lib/polyfills/process-env-browser.ts +0 -1
- package/lib/proto/datasource.ts +0 -93
- package/lib/proto/dcnet.proto +0 -1601
- package/lib/proto/dcnet_proto.d.ts +0 -22857
- package/lib/proto/dcnet_proto.js +0 -55204
- package/lib/proto/dcnet_proto_sparse.js +0 -55166
- package/lib/proto/oidfetch.proto +0 -25
- package/lib/proto/oidfetch_proto.d.ts +0 -585
- package/lib/proto/oidfetch_proto.js +0 -1247
- package/lib/serverless/babel-browser.ts +0 -39
- package/lib/serverless/base_entity.ts +0 -78
- package/lib/serverless/base_repository.ts +0 -414
- package/lib/serverless/browser_schema_extractor.ts +0 -283
- package/lib/serverless/decorator_factory.ts +0 -322
- package/lib/util/BrowserLineReader.ts +0 -73
- package/lib/util/base64.ts +0 -105
- package/lib/util/bcrypt.ts +0 -206
- package/lib/util/curve25519Encryption.ts +0 -418
- package/lib/util/dccrypt.ts +0 -73
- package/lib/util/logger.ts +0 -104
- package/lib/util/utils.ts +0 -289
|
@@ -1,543 +0,0 @@
|
|
|
1
|
-
import type { Client } from "../../common/dcapi";
|
|
2
|
-
import type { Multiaddr as TMultiaddr } from "@multiformats/multiaddr";
|
|
3
|
-
import Long from "long";
|
|
4
|
-
import { extractPublicKeyFromPeerId } from "../../common/dc-key/keyManager";
|
|
5
|
-
import { SymmetricKey} from './common/key';
|
|
6
|
-
import { NewThreadOptions } from './core/options';
|
|
7
|
-
import { ThreadID } from '@textile/threads-id';
|
|
8
|
-
import { DBGrpcClient } from "./net/grpcClient";
|
|
9
|
-
import { PeerId } from "@libp2p/interface";
|
|
10
|
-
import { IRecord, } from "./core/record";
|
|
11
|
-
import { DcUtil,BrowserType } from "../../common/dcutil";
|
|
12
|
-
import {net as net_pb} from "./pb/net_pb";
|
|
13
|
-
import {ILogstore} from "./core/logstore";
|
|
14
|
-
import {IThreadInfo, IThreadLogInfo, ThreadInfo} from "./core/core";
|
|
15
|
-
import { getHeadUndef, Head } from "./core/head";
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
import { Net } from "./core/app";
|
|
19
|
-
import {SymKey} from "./core/core";
|
|
20
|
-
import { ThreadToken } from "./core/identity";
|
|
21
|
-
import { CidConverter, PeerIDConverter, ThreadIDConverter } from "./pb/proto-custom-types";
|
|
22
|
-
import {GetRecord, logFromProto } from "./cbor/record";
|
|
23
|
-
import { PeerRecords } from "./net/define";
|
|
24
|
-
import { Errors } from "./core/db";
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
export class DBClient {
|
|
32
|
-
client: Client;
|
|
33
|
-
dc: DcUtil;
|
|
34
|
-
logstore: ILogstore;
|
|
35
|
-
net:Net;
|
|
36
|
-
|
|
37
|
-
constructor(dcClient: Client,dcUtil: DcUtil,net:Net,logstore: ILogstore) {
|
|
38
|
-
this.net = net;
|
|
39
|
-
this.logstore = logstore;
|
|
40
|
-
this.dc = dcUtil;
|
|
41
|
-
this.client = dcClient;
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
async requestThreadID(
|
|
45
|
-
peerAddr?: TMultiaddr
|
|
46
|
-
): Promise<string> {
|
|
47
|
-
try {
|
|
48
|
-
if (this.client.p2pNode == null) {
|
|
49
|
-
throw new Error("p2pNode is null");
|
|
50
|
-
}
|
|
51
|
-
if (!peerAddr) {
|
|
52
|
-
peerAddr = this.client.peerAddr;
|
|
53
|
-
}
|
|
54
|
-
const grpcClient = new DBGrpcClient(
|
|
55
|
-
this.client.p2pNode,
|
|
56
|
-
peerAddr,
|
|
57
|
-
this.client.token,
|
|
58
|
-
this.net,
|
|
59
|
-
this.client.protocol
|
|
60
|
-
);
|
|
61
|
-
const tid = await grpcClient.requestThreadID();
|
|
62
|
-
return tid;
|
|
63
|
-
} catch (err:any) {
|
|
64
|
-
console.error("getHostID error:", err);
|
|
65
|
-
throw err;
|
|
66
|
-
}
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
async createThread(tid:string,opts: NewThreadOptions): Promise<string> {
|
|
70
|
-
try {
|
|
71
|
-
if (this.client.p2pNode == null || this.client.p2pNode.peerId == null) {
|
|
72
|
-
throw new Error("p2pNode is null or node privateKey is null");
|
|
73
|
-
}
|
|
74
|
-
const sPubkey = await extractPublicKeyFromPeerId(this.client.p2pNode.peerId);
|
|
75
|
-
const grpcClient = new DBGrpcClient(
|
|
76
|
-
this.client.p2pNode,
|
|
77
|
-
this.client.peerAddr,
|
|
78
|
-
this.client.token,
|
|
79
|
-
this.net,
|
|
80
|
-
this.client.protocol
|
|
81
|
-
);
|
|
82
|
-
const threadInfo = await grpcClient.createThread(tid, opts);
|
|
83
|
-
return tid;
|
|
84
|
-
} catch (err:any) {
|
|
85
|
-
console.error("createThread error:", err);
|
|
86
|
-
throw err;
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
async pushRecordToPeer(
|
|
92
|
-
tid: ThreadID,
|
|
93
|
-
lid: PeerId,
|
|
94
|
-
rec: IRecord,
|
|
95
|
-
counter: number
|
|
96
|
-
): Promise<void> {
|
|
97
|
-
try {
|
|
98
|
-
if (this.client.p2pNode == null) {
|
|
99
|
-
throw new Error("p2pNode is null");
|
|
100
|
-
}
|
|
101
|
-
const grpcClient = new DBGrpcClient(
|
|
102
|
-
this.client.p2pNode,
|
|
103
|
-
this.client.peerAddr,
|
|
104
|
-
this.client.token,
|
|
105
|
-
this.net,
|
|
106
|
-
this.client.protocol
|
|
107
|
-
);
|
|
108
|
-
await grpcClient.pushRecordToPeer(
|
|
109
|
-
tid,
|
|
110
|
-
lid,
|
|
111
|
-
rec,
|
|
112
|
-
counter,
|
|
113
|
-
this.logstore,
|
|
114
|
-
);
|
|
115
|
-
//开启threaddb的block记录上报流
|
|
116
|
-
await this.dc.createTransferStream(
|
|
117
|
-
this.client.p2pNode,
|
|
118
|
-
this.client.blockstore,
|
|
119
|
-
this.client.peerAddr,
|
|
120
|
-
BrowserType.Record,
|
|
121
|
-
rec.cid().toString(),
|
|
122
|
-
);
|
|
123
|
-
} catch (err:any) {
|
|
124
|
-
throw err;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
async pushLogToPeer(
|
|
132
|
-
tid: ThreadID,
|
|
133
|
-
lid: PeerId,
|
|
134
|
-
rec: IRecord,
|
|
135
|
-
): Promise<Error | null> {
|
|
136
|
-
try {
|
|
137
|
-
if (this.client.p2pNode == null) {
|
|
138
|
-
throw new Error("p2pNode is null");
|
|
139
|
-
}
|
|
140
|
-
const grpcClient = new DBGrpcClient(
|
|
141
|
-
this.client.p2pNode,
|
|
142
|
-
this.client.peerAddr,
|
|
143
|
-
this.client.token,
|
|
144
|
-
this.net,
|
|
145
|
-
this.client.protocol
|
|
146
|
-
);
|
|
147
|
-
await grpcClient.pushLogToPeer(
|
|
148
|
-
tid,
|
|
149
|
-
lid,
|
|
150
|
-
this.logstore,
|
|
151
|
-
);
|
|
152
|
-
await this.dc.createTransferStream(
|
|
153
|
-
this.client.p2pNode,
|
|
154
|
-
this.client.blockstore,
|
|
155
|
-
this.client.peerAddr,
|
|
156
|
-
BrowserType.Record,
|
|
157
|
-
rec.cid().toString(),
|
|
158
|
-
);
|
|
159
|
-
} catch (err:any) {
|
|
160
|
-
return err;
|
|
161
|
-
}
|
|
162
|
-
return null;
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* 与对等点交换边缘(状态信息)
|
|
167
|
-
* @param threadIds threadID数组
|
|
168
|
-
* @returns 无返回值
|
|
169
|
-
*/
|
|
170
|
-
async exchangeEdges(threadIds: ThreadID[]): Promise<void> {
|
|
171
|
-
try {
|
|
172
|
-
if (this.client.p2pNode == null) {
|
|
173
|
-
throw new Error("p2pNode is null");
|
|
174
|
-
}
|
|
175
|
-
|
|
176
|
-
console.debug(`exchanging edges of ${threadIds.length} threads with peer...`);
|
|
177
|
-
|
|
178
|
-
// 创建请求体
|
|
179
|
-
const body: net_pb.pb.ExchangeEdgesRequest.IBody = {
|
|
180
|
-
threads: []
|
|
181
|
-
};
|
|
182
|
-
body.threads = [];
|
|
183
|
-
|
|
184
|
-
// 填充本地边缘信息
|
|
185
|
-
for (const tid of threadIds) {
|
|
186
|
-
try {
|
|
187
|
-
// 获取本地边缘信息
|
|
188
|
-
const { addrEdge, headsEdge } = await this.localEdges(tid);
|
|
189
|
-
// 添加到请求中
|
|
190
|
-
body.threads.push({
|
|
191
|
-
threadID: tid.toBytes(),
|
|
192
|
-
headsEdge: Long.fromString(headsEdge.toString(), true),
|
|
193
|
-
addressEdge: Long.fromString(addrEdge.toString(), true)
|
|
194
|
-
});
|
|
195
|
-
} catch (err:any) {
|
|
196
|
-
if (err.message !== "No address edge" &&
|
|
197
|
-
err.message !== "No heads edge") {
|
|
198
|
-
console.error(`Getting local edges for ${tid} failed:`, err);
|
|
199
|
-
} else {
|
|
200
|
-
body.threads.push({
|
|
201
|
-
threadID: tid.toBytes(),
|
|
202
|
-
headsEdge: 0, // EmptyEdgeValue
|
|
203
|
-
addressEdge: 0 // EmptyEdgeValue
|
|
204
|
-
});
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
if (body.threads.length === 0) {
|
|
210
|
-
return;
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
// 创建完整请求
|
|
214
|
-
const req = new net_pb.pb.ExchangeEdgesRequest();
|
|
215
|
-
req.body = body;
|
|
216
|
-
// 创建gRPC客户端
|
|
217
|
-
const grpcClient = new DBGrpcClient(
|
|
218
|
-
this.client.p2pNode,
|
|
219
|
-
this.client.peerAddr,
|
|
220
|
-
this.client.token,
|
|
221
|
-
this.net,
|
|
222
|
-
this.client.protocol
|
|
223
|
-
);
|
|
224
|
-
|
|
225
|
-
// 发送请求并设置超时
|
|
226
|
-
const timeout = 30000; // 30s timeout
|
|
227
|
-
const abortController = new AbortController();
|
|
228
|
-
const timeoutId = setTimeout(() => abortController.abort(), timeout);
|
|
229
|
-
|
|
230
|
-
try {
|
|
231
|
-
// 发送交换边缘请求
|
|
232
|
-
const reply = await grpcClient.exchangeEdges(req);
|
|
233
|
-
|
|
234
|
-
// 处理响应
|
|
235
|
-
for (const edge of reply.edges || []) {
|
|
236
|
-
if (!edge.threadID) continue;
|
|
237
|
-
const tid = ThreadID.fromBytes(edge.threadID);
|
|
238
|
-
// 获取本地可能已更新的边缘
|
|
239
|
-
let addrEdgeLocal = 0, headsEdgeLocal = 0;
|
|
240
|
-
try {
|
|
241
|
-
const localEdges = await this.localEdges(tid);
|
|
242
|
-
addrEdgeLocal = Number(localEdges.addrEdge)||0;
|
|
243
|
-
headsEdgeLocal = Number(localEdges.headsEdge)||0;
|
|
244
|
-
} catch (err:any) {
|
|
245
|
-
// 允许本地边缘为空
|
|
246
|
-
if (err.message !== "No address edge" &&
|
|
247
|
-
err.message !== "No heads edge") {
|
|
248
|
-
console.error(`Second retrieval of local edges for ${tid} failed:`, err);
|
|
249
|
-
continue;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
let needPush = false;
|
|
253
|
-
// 检查地址边缘是否有更新
|
|
254
|
-
const responseAddrEdge = Number(edge.addressEdge ) || 0;
|
|
255
|
-
if (responseAddrEdge !== 0 && responseAddrEdge !== addrEdgeLocal) {
|
|
256
|
-
// 调度日志更新
|
|
257
|
-
await this.scheduleUpdateLogs(tid);
|
|
258
|
-
needPush = true;
|
|
259
|
-
console.debug(`Log information update for thread ${tid} scheduled`);
|
|
260
|
-
}
|
|
261
|
-
// 检查头部边缘是否有更新
|
|
262
|
-
const responseHeadEdge = Number(edge.headsEdge) || 0;
|
|
263
|
-
if (responseHeadEdge !== 0 && responseHeadEdge !== headsEdgeLocal) {
|
|
264
|
-
// 调度记录更新
|
|
265
|
-
await this.scheduleUpdateRecords(tid);
|
|
266
|
-
needPush = true;
|
|
267
|
-
console.debug(`Record update for thread ${tid} scheduled`);
|
|
268
|
-
}
|
|
269
|
-
if (!needPush) {//将本地的所有log的head都上报给对等点
|
|
270
|
-
await this.pushLogsHeadToPeer(tid);
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
}
|
|
274
|
-
} catch (err:any) {
|
|
275
|
-
// 处理特殊错误码
|
|
276
|
-
if (err.code === 'UNIMPLEMENTED') {
|
|
277
|
-
console.debug(`Peer doesn't support edge exchange, falling back to direct record pulling`);
|
|
278
|
-
for (const tid of threadIds) {
|
|
279
|
-
await this.scheduleUpdateRecords(tid);
|
|
280
|
-
console.debug(`Record update for thread ${tid} scheduled`);
|
|
281
|
-
}
|
|
282
|
-
return;
|
|
283
|
-
} else if (err.code === 'UNAVAILABLE') {
|
|
284
|
-
console.debug(`Peer unavailable, skip edge exchange`);
|
|
285
|
-
return;
|
|
286
|
-
}
|
|
287
|
-
throw err;
|
|
288
|
-
} finally {
|
|
289
|
-
clearTimeout(timeoutId);
|
|
290
|
-
}
|
|
291
|
-
} catch (err:any) {
|
|
292
|
-
throw new Error(`Exchange edges failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
293
|
-
}
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
private async pushLogsHeadToPeer(tid: ThreadID): Promise<void> {
|
|
297
|
-
//取出所有log的head对应的记录
|
|
298
|
-
const logs = await this.logstore.getManagedLogs(tid);
|
|
299
|
-
//提取出serviceKey
|
|
300
|
-
const serviceKey = await this.logstore.keyBook.serviceKey(tid);
|
|
301
|
-
if (!serviceKey) {
|
|
302
|
-
return ;
|
|
303
|
-
}
|
|
304
|
-
const sk = SymmetricKey.fromSymKey(serviceKey);
|
|
305
|
-
for(const log of logs){
|
|
306
|
-
const head = await this.logstore.headBook.heads(tid, log.id);
|
|
307
|
-
let headToPush:Head;
|
|
308
|
-
if(head.length>0){
|
|
309
|
-
//取最新的head
|
|
310
|
-
headToPush = head.reduce((prev, current) => (prev.counter > current.counter) ? prev : current);
|
|
311
|
-
if (headToPush.id && headToPush.counter > 0) {
|
|
312
|
-
const rec = await GetRecord(this.net.getDagService(),headToPush.id,sk);
|
|
313
|
-
if(rec){
|
|
314
|
-
await this.pushRecordToPeer(
|
|
315
|
-
tid,
|
|
316
|
-
log.id,
|
|
317
|
-
rec,
|
|
318
|
-
headToPush.counter
|
|
319
|
-
);
|
|
320
|
-
}
|
|
321
|
-
}
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
* 获取threaddb 的本地边缘值(地址和头部)
|
|
329
|
-
* 边缘值用于确定是否需要从远程对等点获取更新
|
|
330
|
-
*
|
|
331
|
-
* @param tid threaddb ID
|
|
332
|
-
* @returns 地址边缘值和头部边缘值的对象
|
|
333
|
-
* @throws 错误,包括特定的"No address edge"和"No heads edge"错误
|
|
334
|
-
*/
|
|
335
|
-
private async localEdges(tid: ThreadID): Promise<{ addrEdge: bigint, headsEdge: bigint }> {
|
|
336
|
-
// 使用默认值初始化头部边缘值
|
|
337
|
-
let headsEdge = 0n; // EmptyEdgeValue 对应 0n
|
|
338
|
-
let addrEdge = 0n;
|
|
339
|
-
|
|
340
|
-
try {
|
|
341
|
-
const exceptLogId = await this.logstore.metadata.getString(
|
|
342
|
-
tid,
|
|
343
|
-
"local_log_no_record_flag"
|
|
344
|
-
);
|
|
345
|
-
// 尝试获取地址边缘值
|
|
346
|
-
if (exceptLogId && exceptLogId?.length > 0) {
|
|
347
|
-
addrEdge = await this.logstore.addrBook.addrsEdge(tid,exceptLogId);
|
|
348
|
-
}else{
|
|
349
|
-
addrEdge = await this.logstore.addrBook.addrsEdge(tid);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
} catch (err:any) {
|
|
353
|
-
// // 处理threaddb 未找到错误
|
|
354
|
-
// if (err.message.includes("Thread not found")) {
|
|
355
|
-
// throw new Error("No address edge");
|
|
356
|
-
// } else {
|
|
357
|
-
// throw new Error(`Address edge: ${err.message}`);
|
|
358
|
-
// }
|
|
359
|
-
}
|
|
360
|
-
|
|
361
|
-
try {
|
|
362
|
-
// 尝试获取头部边缘值
|
|
363
|
-
headsEdge = await this.logstore.headBook.headsEdge(tid);
|
|
364
|
-
} catch (err:any) {
|
|
365
|
-
// 处理threaddb 未找到错误
|
|
366
|
-
if (err.message.includes("Thread not found")) {
|
|
367
|
-
throw new Error("No heads edge");
|
|
368
|
-
} else {
|
|
369
|
-
throw new Error(`Heads edge: ${err.message}`);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
// 返回边缘值对象
|
|
374
|
-
return { addrEdge, headsEdge };
|
|
375
|
-
}
|
|
376
|
-
|
|
377
|
-
private edgeToBigInt(value: number | Long | undefined | null): bigint {
|
|
378
|
-
if (value == null) {
|
|
379
|
-
return 0n;
|
|
380
|
-
}
|
|
381
|
-
if (typeof value === "number") {
|
|
382
|
-
return BigInt(value >>> 0);
|
|
383
|
-
}
|
|
384
|
-
if (Long.isLong(value)) {
|
|
385
|
-
return BigInt(value.toString());
|
|
386
|
-
}
|
|
387
|
-
return 0n;
|
|
388
|
-
}
|
|
389
|
-
/**
|
|
390
|
-
* 调度日志更新
|
|
391
|
-
* @param tid threaddb ID
|
|
392
|
-
*/
|
|
393
|
-
async scheduleUpdateLogs(tid: ThreadID): Promise<void> {
|
|
394
|
-
// 创建gRPC客户端
|
|
395
|
-
const lgs = await this.getLogs(tid);
|
|
396
|
-
return await this.net.createExternalLogsIfNotExist(tid, lgs)
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
/**
|
|
400
|
-
* 调度记录更新
|
|
401
|
-
* @param tid threaddb ID
|
|
402
|
-
*/
|
|
403
|
-
private async scheduleUpdateRecords(tid: ThreadID): Promise<void> {
|
|
404
|
-
this.net.updateRecordsFromPeer(tid, null,this);
|
|
405
|
-
}
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
/**
|
|
410
|
-
* 构建获取记录的请求
|
|
411
|
-
*/
|
|
412
|
-
private async buildGetRecordsRequest(
|
|
413
|
-
tid: ThreadID,
|
|
414
|
-
offsets: Record<string,Head>,
|
|
415
|
-
limit: number
|
|
416
|
-
): Promise<{ req: net_pb.pb.IGetRecordsRequest, serviceKey: SymKey }> {
|
|
417
|
-
try {
|
|
418
|
-
// 从存储中获取服务密钥
|
|
419
|
-
const serviceKey = await this.logstore.keyBook.serviceKey(tid);
|
|
420
|
-
|
|
421
|
-
if (!serviceKey) {
|
|
422
|
-
throw new Error("A service-key is required to request records");
|
|
423
|
-
}
|
|
424
|
-
|
|
425
|
-
// 创建日志条目
|
|
426
|
-
const logs = Object.entries(offsets).map(([logId, offset]) => {
|
|
427
|
-
const offsetId = offset?.id || undefined;
|
|
428
|
-
const pbLog: net_pb.pb.GetRecordsRequest.Body.ILogEntry = {
|
|
429
|
-
logID: PeerIDConverter.toBytes(logId),
|
|
430
|
-
limit: limit,
|
|
431
|
-
counter: offset.counter,
|
|
432
|
-
offset: offsetId?CidConverter.toBytes(offsetId):null
|
|
433
|
-
}
|
|
434
|
-
return pbLog;
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
// 构建请求体
|
|
438
|
-
const body: net_pb.pb.GetRecordsRequest.IBody = {
|
|
439
|
-
threadID: ThreadIDConverter.toBytes(tid.toString()),
|
|
440
|
-
serviceKey: serviceKey.raw,
|
|
441
|
-
logs: logs
|
|
442
|
-
};
|
|
443
|
-
|
|
444
|
-
// 创建请求
|
|
445
|
-
const req = new net_pb.pb.GetRecordsRequest();
|
|
446
|
-
req.body = body;
|
|
447
|
-
return { req, serviceKey };
|
|
448
|
-
} catch (err:any) {
|
|
449
|
-
console.error("buildGetRecordsRequest error:", err);
|
|
450
|
-
throw err;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
/**
|
|
456
|
-
* 获取threaddb 中的日志
|
|
457
|
-
* @param tid threaddb ID
|
|
458
|
-
* @param pid 对等点ID
|
|
459
|
-
* @returns 日志信息数组
|
|
460
|
-
*/
|
|
461
|
-
async getLogs(tid: ThreadID): Promise<IThreadLogInfo[]> {
|
|
462
|
-
try {
|
|
463
|
-
// 获取服务密钥
|
|
464
|
-
const serviceKey = await this.logstore.keyBook.serviceKey(tid);
|
|
465
|
-
|
|
466
|
-
if (!serviceKey) {
|
|
467
|
-
throw new Error("A service-key is required to request logs");
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
const grpcClient = new DBGrpcClient(
|
|
471
|
-
this.client.p2pNode,
|
|
472
|
-
this.client.peerAddr,
|
|
473
|
-
this.client.token,
|
|
474
|
-
this.net,
|
|
475
|
-
this.client.protocol
|
|
476
|
-
);
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
// 解码响应
|
|
480
|
-
const reply = await grpcClient.getLogs(tid, serviceKey.raw);
|
|
481
|
-
if (!reply.logs) {
|
|
482
|
-
return [];
|
|
483
|
-
}
|
|
484
|
-
// 将日志从protobuf格式转换为应用格式
|
|
485
|
-
const logs: IThreadLogInfo[] = await Promise.all(
|
|
486
|
-
reply.logs.map(async (l) => await logFromProto(l))
|
|
487
|
-
);
|
|
488
|
-
return logs;
|
|
489
|
-
} catch (err:any) {
|
|
490
|
-
console.warn(`Get logs from ${this.client.peerAddr} failed: ${err instanceof Error ? err.message : String(err)}`);
|
|
491
|
-
throw err;
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
async getRecordsFromPeer(
|
|
497
|
-
req: any,
|
|
498
|
-
serviceKey: SymKey
|
|
499
|
-
): Promise<Record<string, PeerRecords>> {
|
|
500
|
-
|
|
501
|
-
if (!serviceKey) {
|
|
502
|
-
throw new Error("A service-key is required to request logs");
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
const grpcClient = new DBGrpcClient(
|
|
506
|
-
this.client.p2pNode,
|
|
507
|
-
this.client.peerAddr,
|
|
508
|
-
this.client.token,
|
|
509
|
-
this.net,
|
|
510
|
-
this.client.protocol
|
|
511
|
-
);
|
|
512
|
-
|
|
513
|
-
const reply = await grpcClient.getRecordsFromPeer( req,SymmetricKey.fromSymKey( serviceKey));
|
|
514
|
-
if (Object.keys(reply).length === 0) {
|
|
515
|
-
return {};
|
|
516
|
-
}
|
|
517
|
-
return reply;
|
|
518
|
-
}
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
async getThreadFromPeer(
|
|
524
|
-
id: ThreadID,
|
|
525
|
-
peerId: PeerId,
|
|
526
|
-
options: { token?: ThreadToken } = {}
|
|
527
|
-
): Promise<IThreadInfo> {
|
|
528
|
-
const grpcClient = new DBGrpcClient(
|
|
529
|
-
this.client.p2pNode,
|
|
530
|
-
this.client.peerAddr,
|
|
531
|
-
this.client.token,
|
|
532
|
-
this.net,
|
|
533
|
-
this.client.protocol
|
|
534
|
-
)
|
|
535
|
-
const threadInfo = await grpcClient.getThreadFromPeer(id);
|
|
536
|
-
return threadInfo;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
}
|