Skip to content

Commit 316b66c

Browse files
chore: update word history to storage
1 parent 96ec51f commit 316b66c

File tree

6 files changed

+31
-39
lines changed

6 files changed

+31
-39
lines changed

packages/sds/src/message_channel/local_history.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import { Logger } from "@waku/utils";
22
import _ from "lodash";
33

44
import { type ChannelId, ContentMessage, isContentMessage } from "./message.js";
5-
import { ILocalHistory } from "./message_channel.js";
65
import { PersistentStorage } from "./persistent_storage.js";
76

87
export const DEFAULT_MAX_LENGTH = 10_000;
@@ -28,7 +27,7 @@ export type LocalHistoryOptions = {
2827

2928
const log = new Logger("sds:local-history");
3029

31-
export class LocalHistory implements ILocalHistory {
30+
export class LocalHistory {
3231
private items: ContentMessage[] = [];
3332
private readonly storage?: PersistentStorage;
3433
private readonly maxSize: number;

packages/sds/src/message_channel/message_channel.spec.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ describe("MessageChannel", function () {
318318
testRetrievalHint
319319
);
320320

321-
const localHistory = channelA["localHistory"] as ILocalHistory;
321+
const localHistory = channelA["localHistory"] as LocalHistory;
322322
expect(localHistory.length).to.equal(1);
323323

324324
// Find the message in local history
@@ -440,7 +440,7 @@ describe("MessageChannel", function () {
440440
)
441441
);
442442

443-
const localHistory = channelA["localHistory"] as ILocalHistory;
443+
const localHistory = channelA["localHistory"] as LocalHistory;
444444
expect(localHistory.length).to.equal(2);
445445

446446
// When timestamps are equal, should be ordered by messageId lexicographically

packages/sds/src/message_channel/message_channel.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,6 @@ const DEFAULT_POSSIBLE_ACKS_THRESHOLD = 2;
3838

3939
const log = new Logger("sds:message-channel");
4040

41-
export type ILocalHistory = Pick<
42-
Array<ContentMessage>,
43-
"some" | "push" | "slice" | "find" | "length" | "findIndex"
44-
>;
45-
4641
export interface MessageChannelOptions {
4742
causalHistorySize?: number;
4843
/**
@@ -76,7 +71,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
7671
private outgoingBuffer: ContentMessage[];
7772
private possibleAcks: Map<MessageId, number>;
7873
private incomingBuffer: Array<ContentMessage | SyncMessage>;
79-
private readonly localHistory: ILocalHistory;
74+
private readonly localHistory: LocalHistory;
8075
private timeReceived: Map<MessageId, number>;
8176
private readonly causalHistorySize: number;
8277
private readonly possibleAcksThreshold: number;
@@ -106,7 +101,7 @@ export class MessageChannel extends TypedEventEmitter<MessageChannelEvents> {
106101
channelId: ChannelId,
107102
senderId: ParticipantId,
108103
options: MessageChannelOptions = {},
109-
localHistory?: ILocalHistory
104+
localHistory?: LocalHistory
110105
) {
111106
super();
112107
this.channelId = channelId;

packages/sds/src/message_channel/persistent_storage.spec.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { expect } from "chai";
22

33
import { LocalHistory } from "./local_history.js";
44
import { ContentMessage } from "./message.js";
5-
import { HistoryStorage, PersistentStorage } from "./persistent_storage.js";
5+
import { IStorage, PersistentStorage } from "./persistent_storage.js";
66

77
const channelId = "channel-1";
88

@@ -41,15 +41,15 @@ describe("PersistentStorage", () => {
4141
it("handles corrupt data in storage gracefully", () => {
4242
const storage = new MemoryStorage();
4343
// Corrupt data
44-
storage.setItem("waku:sds:history:channel-1", "{ invalid json }");
44+
storage.setItem("waku:sds:messages:channel-1", "{ invalid json }");
4545

4646
const persistentStorage = PersistentStorage.create(channelId, storage);
4747
const history = new LocalHistory({ storage: persistentStorage });
4848

4949
expect(history.length).to.equal(0);
5050

5151
// Corrupt data is not saved
52-
expect(storage.getItem("waku:sds:history:channel-1")).to.equal(null);
52+
expect(storage.getItem("waku:sds:messages:channel-1")).to.equal(null);
5353
});
5454

5555
it("isolates history by channel ID", () => {
@@ -70,22 +70,22 @@ describe("PersistentStorage", () => {
7070
expect(history2.length).to.equal(1);
7171
expect(history2.slice(0)[0].messageId).to.equal("msg-2");
7272

73-
expect(storage.getItem("waku:sds:history:channel-1")).to.not.be.null;
74-
expect(storage.getItem("waku:sds:history:channel-2")).to.not.be.null;
73+
expect(storage.getItem("waku:sds:messages:channel-1")).to.not.be.null;
74+
expect(storage.getItem("waku:sds:messages:channel-2")).to.not.be.null;
7575
});
7676

7777
it("saves messages after each push", () => {
7878
const storage = new MemoryStorage();
7979
const persistentStorage = PersistentStorage.create(channelId, storage);
8080
const history = new LocalHistory({ storage: persistentStorage });
8181

82-
expect(storage.getItem("waku:sds:history:channel-1")).to.be.null;
82+
expect(storage.getItem("waku:sds:messages:channel-1")).to.be.null;
8383

8484
history.push(createMessage("msg-1", 1));
8585

86-
expect(storage.getItem("waku:sds:history:channel-1")).to.not.be.null;
86+
expect(storage.getItem("waku:sds:messages:channel-1")).to.not.be.null;
8787

88-
const saved = JSON.parse(storage.getItem("waku:sds:history:channel-1")!);
88+
const saved = JSON.parse(storage.getItem("waku:sds:messages:channel-1")!);
8989
expect(saved).to.have.lengthOf(1);
9090
expect(saved[0].messageId).to.equal("msg-1");
9191
});
@@ -146,7 +146,7 @@ describe("PersistentStorage", () => {
146146
"msg-2"
147147
]);
148148

149-
localStorage.removeItem(`waku:sds:history:${testChannelId}`);
149+
localStorage.removeItem(`waku:sds:messages:${testChannelId}`);
150150
});
151151

152152
it("auto-uses localStorage when channelId is provided", () => {
@@ -163,7 +163,7 @@ describe("PersistentStorage", () => {
163163
"msg-auto-2"
164164
]);
165165

166-
localStorage.removeItem(`waku:sds:history:${testChannelId}`);
166+
localStorage.removeItem(`waku:sds:messages:${testChannelId}`);
167167
});
168168
});
169169
});
@@ -181,7 +181,7 @@ const createMessage = (id: string, timestamp: number): ContentMessage => {
181181
);
182182
};
183183

184-
class MemoryStorage implements HistoryStorage {
184+
class MemoryStorage implements IStorage {
185185
private readonly store = new Map<string, string>();
186186

187187
public getItem(key: string): string | null {

packages/sds/src/message_channel/persistent_storage.ts

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@ import { ChannelId, ContentMessage, HistoryEntry } from "./message.js";
55

66
const log = new Logger("sds:persistent-storage");
77

8-
const HISTORY_STORAGE_PREFIX = "waku:sds:history:";
8+
const STORAGE_PREFIX = "waku:sds:storage:";
99

10-
export interface HistoryStorage {
10+
export interface IStorage {
1111
getItem(key: string): string | null;
1212
setItem(key: string, value: string): void;
1313
removeItem(key: string): void;
1414
}
1515

16-
type StoredHistoryEntry = {
16+
type StoredCausalEntry = {
1717
messageId: string;
1818
retrievalHint?: string;
1919
};
@@ -23,14 +23,14 @@ type StoredContentMessage = {
2323
channelId: string;
2424
senderId: string;
2525
lamportTimestamp: string;
26-
causalHistory: StoredHistoryEntry[];
26+
causalHistory: StoredCausalEntry[];
2727
bloomFilter?: string;
2828
content: string;
2929
retrievalHint?: string;
3030
};
3131

3232
/**
33-
* Persistent storage for message history.
33+
* Persistent storage for messages.
3434
*/
3535
export class PersistentStorage {
3636
private readonly storageKey: string;
@@ -42,7 +42,7 @@ export class PersistentStorage {
4242
*/
4343
public static create(
4444
channelId: ChannelId,
45-
storage?: HistoryStorage
45+
storage?: IStorage
4646
): PersistentStorage | undefined {
4747
storage =
4848
storage ??
@@ -59,9 +59,9 @@ export class PersistentStorage {
5959

6060
private constructor(
6161
channelId: ChannelId,
62-
private readonly storage: HistoryStorage
62+
private readonly storage: IStorage
6363
) {
64-
this.storageKey = `${HISTORY_STORAGE_PREFIX}${channelId}`;
64+
this.storageKey = `${STORAGE_PREFIX}${channelId}`;
6565
}
6666

6767
public save(messages: ContentMessage[]): void {
@@ -104,7 +104,7 @@ class MessageSerializer {
104104
senderId: message.senderId,
105105
lamportTimestamp: message.lamportTimestamp.toString(),
106106
causalHistory: message.causalHistory.map((entry) =>
107-
MessageSerializer.serializeHistoryEntry(entry)
107+
MessageSerializer.serializeCausalEntry(entry)
108108
),
109109
bloomFilter: MessageSerializer.toHex(message.bloomFilter),
110110
content: bytesToHex(new Uint8Array(message.content)),
@@ -122,7 +122,7 @@ class MessageSerializer {
122122
record.channelId,
123123
record.senderId,
124124
record.causalHistory.map((entry) =>
125-
MessageSerializer.deserializeHistoryEntry(entry)
125+
MessageSerializer.deserializeCausalEntry(entry)
126126
),
127127
BigInt(record.lamportTimestamp),
128128
MessageSerializer.fromHex(record.bloomFilter),
@@ -135,7 +135,7 @@ class MessageSerializer {
135135
}
136136
}
137137

138-
public static serializeHistoryEntry(entry: HistoryEntry): StoredHistoryEntry {
138+
public static serializeCausalEntry(entry: HistoryEntry): StoredCausalEntry {
139139
return {
140140
messageId: entry.messageId,
141141
retrievalHint: entry.retrievalHint
@@ -144,9 +144,7 @@ class MessageSerializer {
144144
};
145145
}
146146

147-
public static deserializeHistoryEntry(
148-
entry: StoredHistoryEntry
149-
): HistoryEntry {
147+
public static deserializeCausalEntry(entry: StoredCausalEntry): HistoryEntry {
150148
return {
151149
messageId: entry.messageId,
152150
retrievalHint: entry.retrievalHint

packages/sds/src/message_channel/repair/repair.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { Logger } from "@waku/utils";
22

3+
import { LocalHistory } from "../local_history.js";
34
import type { HistoryEntry, MessageId } from "../message.js";
45
import { Message } from "../message.js";
5-
import type { ILocalHistory } from "../message_channel.js";
66

77
import { IncomingRepairBuffer, OutgoingRepairBuffer } from "./buffers.js";
88
import {
@@ -183,7 +183,7 @@ export class RepairManager {
183183
*/
184184
public processIncomingRepairRequests(
185185
requests: HistoryEntry[],
186-
localHistory: ILocalHistory,
186+
localHistory: LocalHistory,
187187
currentTime = Date.now()
188188
): void {
189189
for (const request of requests) {
@@ -248,7 +248,7 @@ export class RepairManager {
248248
* Returns messages that should be rebroadcast
249249
*/
250250
public sweepIncomingBuffer(
251-
localHistory: ILocalHistory,
251+
localHistory: LocalHistory,
252252
currentTime = Date.now()
253253
): Message[] {
254254
const ready = this.incomingBuffer.getReady(currentTime);

0 commit comments

Comments
 (0)