-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathzeroGStore.ts
More file actions
197 lines (178 loc) · 6.55 KB
/
zeroGStore.ts
File metadata and controls
197 lines (178 loc) · 6.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import { Indexer, MemData } from "@0gfoundation/0g-storage-ts-sdk";
import { ethers } from "ethers";
import type { Decision, Policy } from "../core/types.js";
import type { AnchorRecord, Store } from "./store.js";
export type { AnchorRecord };
interface UploadResultSingle {
rootHash: string;
txHash: string;
txSeq: number;
}
interface UploadResultMulti {
rootHashes: string[];
txHashes: string[];
txSeqs: number[];
}
type UploadResult = UploadResultSingle | UploadResultMulti;
interface IndexerLike {
upload(
file: MemData,
blockchainRpc: string,
signer: ethers.Signer,
): Promise<[UploadResult, Error | null]>;
}
export interface ZeroGStoreOptions {
rpcUrl: string;
indexerRpc: string;
privateKey?: string;
signer?: ethers.Signer;
indexer?: IndexerLike;
logger?: Pick<Console, "log" | "warn">;
}
interface TaggedPolicy {
policy: Policy;
clientId: string | null;
}
interface TaggedDecision {
decision: Decision;
clientId: string | null;
}
export class ZeroGStore implements Store {
private readonly indexer: IndexerLike;
private readonly signer: ethers.Signer;
private readonly rpcUrl: string;
private readonly logger: Pick<Console, "log" | "warn">;
private policies = new Map<string, TaggedPolicy>();
private decisions: TaggedDecision[] = [];
private anchors = new Map<string, AnchorRecord>();
/**
* Tracks every in-flight 0G upload by row id so tests + the API admin
* route can `await` for the anchor to land. In production nothing awaits
* this — `putPolicy`/`appendDecision` return immediately after the local
* write so the request hot path stays under 50ms instead of 5-30s.
*/
private pending = new Map<string, Promise<void>>();
constructor(opts: ZeroGStoreOptions) {
this.rpcUrl = opts.rpcUrl;
this.logger = opts.logger ?? console;
this.indexer = opts.indexer ?? (new Indexer(opts.indexerRpc) as unknown as IndexerLike);
if (opts.signer) {
this.signer = opts.signer;
} else {
if (!opts.privateKey) {
throw new Error("ZeroGStore requires either signer or privateKey");
}
const provider = new ethers.JsonRpcProvider(opts.rpcUrl);
this.signer = new ethers.Wallet(opts.privateKey, provider);
}
}
async putPolicy(policy: Policy, clientId?: string): Promise<void> {
this.policies.set(policy.id, { policy, clientId: clientId ?? null });
this.scheduleAnchor(policy.id, `policy:${policy.id}`, JSON.stringify(policy));
}
async getPolicy(id: string, clientId?: string): Promise<Policy | null> {
const row = this.policies.get(id);
if (!row) return null;
if (clientId !== undefined && row.clientId !== clientId) return null;
return row.policy;
}
async listPolicies(filter: {
owner?: Policy["owner"];
clientId?: string;
} = {}): Promise<Policy[]> {
const all = [...this.policies.values()];
return all
.filter((row) => {
if (filter.clientId !== undefined && row.clientId !== filter.clientId) return false;
if (filter.owner && row.policy.owner.toLowerCase() !== filter.owner.toLowerCase()) {
return false;
}
return true;
})
.map((row) => row.policy);
}
async appendDecision(decision: Decision, clientId?: string): Promise<void> {
this.decisions.push({ decision, clientId: clientId ?? null });
this.scheduleAnchor(decision.id, `decision:${decision.id}`, JSON.stringify(decision));
}
async listDecisions(filter: {
owner?: Policy["owner"];
from?: number;
to?: number;
clientId?: string;
}): Promise<Decision[]> {
return this.decisions
.filter((row) => {
if (filter.clientId !== undefined && row.clientId !== filter.clientId) return false;
const d = row.decision;
if (filter.from !== undefined && d.timestamp < filter.from) return false;
if (filter.to !== undefined && d.timestamp > filter.to) return false;
if (filter.owner && d.intent.from.toLowerCase() !== filter.owner.toLowerCase()) {
return false;
}
return true;
})
.map((row) => row.decision);
}
getAnchor(id: string): AnchorRecord | undefined {
return this.anchors.get(id);
}
/**
* Awaits the background anchor upload for a single row. Used by tests so
* they can assert anchor presence after the upload settles, without
* coupling production code to a synchronous wait. Resolves immediately
* if no upload was scheduled for that id (e.g. wrong id, or anchor
* already finished).
*/
async waitForAnchor(id: string): Promise<void> {
const p = this.pending.get(id);
if (p) await p;
}
private scheduleAnchor(rowId: string, label: string, json: string): void {
// Kick off the upload but DO NOT await — production callers return as
// soon as the in-memory write is done. The promise is parked in
// `pending` so tests + admin tooling can settle it.
const p = this.tryAnchor(label, json)
.then((anchor) => {
if (anchor) this.anchors.set(rowId, anchor);
})
.finally(() => {
// Only delete if the entry still points at this promise — a
// subsequent overwrite of the same id (rare for policies, never
// for decisions) would replace it and we don't want to clobber.
if (this.pending.get(rowId) === p) this.pending.delete(rowId);
});
this.pending.set(rowId, p);
}
private async tryAnchor(label: string, json: string): Promise<AnchorRecord | null> {
const bytes = new TextEncoder().encode(json);
const file = new MemData(bytes);
try {
const [result, err] = await this.indexer.upload(file, this.rpcUrl, this.signer);
if (err) {
this.logger.warn(`[zeroG] anchor ${label} failed: ${err.message}`);
return null;
}
if ("rootHash" in result) {
if (!result.rootHash) {
this.logger.warn(`[zeroG] anchor ${label} returned empty rootHash; skipping`);
return null;
}
this.logger.log(`[zeroG] anchored ${label} root=${result.rootHash}`);
return { rootHash: result.rootHash, txHash: result.txHash };
}
const rootHash = result.rootHashes[0];
const txHash = result.txHashes[0];
if (!rootHash) {
this.logger.warn(`[zeroG] anchor ${label} returned empty multi-result; skipping`);
return null;
}
this.logger.log(`[zeroG] anchored ${label} (multi) root=${rootHash}`);
return { rootHash, txHash: txHash ?? "" };
} catch (e) {
const msg = e instanceof Error ? e.message : String(e);
this.logger.warn(`[zeroG] anchor ${label} threw: ${msg}`);
return null;
}
}
}