Skip to content

Commit b9f5be1

Browse files
authored
buffer as data encoding (#77)
1 parent a0d6fed commit b9f5be1

File tree

14 files changed

+63
-46
lines changed

14 files changed

+63
-46
lines changed

src/cache/index.test.slow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe("LightBlockCache creating cache", () => {
1414
const cacheBlocks = lightBlockCache.cacheBlocks();
1515
await Promise.race([cacheBlocks, delay()]);
1616
const head = await lightBlockCache.getHead();
17-
const block = await lightBlockCache.getBlockByHash(head!.toString("hex"));
17+
const block = await lightBlockCache.getLightBlock(head!);
1818
expect(block).toHaveProperty("protoVersion");
1919
expect(block).toHaveProperty("sequence");
2020
expect(block).toHaveProperty("hash");

src/cache/index.test.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import { LightBlock } from "../models/lightstreamer";
21
import { blockFixture } from "../../test/fixtures";
32
import { lightBlockCache } from ".";
43

54
describe("LightBlockCache", () => {
6-
const fakeHash = "hash1";
7-
85
beforeAll(async () => {
96
await lightBlockCache.open();
107
});
@@ -14,24 +11,25 @@ describe("LightBlockCache", () => {
1411
});
1512

1613
it("storing and retrieving block is successful", async () => {
17-
const encoded = LightBlock.encode(blockFixture).finish();
18-
await lightBlockCache.put(fakeHash, encoded);
19-
await lightBlockCache.put(blockFixture.sequence.toString(), fakeHash);
14+
await lightBlockCache.putLightBlock(blockFixture);
2015

21-
const hashBlock = await lightBlockCache.getBlockByHash(fakeHash);
16+
const hashBlock = await lightBlockCache.getLightBlock(blockFixture.hash);
2217
expect(hashBlock).toEqual(blockFixture);
2318

24-
const sequenceBlock = await lightBlockCache.getBlockBySequence(
19+
const sequenceBlock = await lightBlockCache.getLightBlockBySequence(
2520
blockFixture.sequence,
2621
);
2722
expect(sequenceBlock).toEqual(blockFixture);
2823
});
2924

3025
it("storing and retrieving hash is successful", async () => {
31-
await lightBlockCache.put("head", Buffer.from("deedbeef", "hex"));
26+
await lightBlockCache.putHead(Buffer.from("deadbeef", "hex"), 1000);
27+
28+
const head = await lightBlockCache.getHead();
29+
expect(head?.toString("hex")).toEqual("deadbeef");
3230

33-
const block = await lightBlockCache.getHead();
34-
expect(block!.toString("hex")).toEqual("deedbeef");
31+
const sequence = await lightBlockCache.getHeadSequence();
32+
expect(sequence).toEqual(1000);
3533
});
3634

3735
it("finality sequence is always behind head sequence by specified amount", async () => {

src/cache/index.ts

Lines changed: 39 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -83,31 +83,29 @@ export class LightBlockCache {
8383
} else if (content.type === "disconnected") {
8484
logger.warn(`Removing block ${content.block.sequence}...`);
8585
const block = lightBlock(content);
86-
await this.db.put("head", block.previousBlockHash);
87-
await this.db.put("headSequence", (block.sequence - 1).toString());
88-
await this.db.del(block.sequence);
89-
await this.db.del(block.hash);
86+
await this.putHead(block.previousBlockHash, block.sequence - 1);
87+
await this.del(block.sequence.toString());
88+
await this.del(block.hash.toString("hex"));
9089
}
9190
}
9291
}
9392
}
9493

9594
private async rollbackHead(): Promise<void> {
96-
let head = (await this.getHeadSequence()) - 1;
97-
if (!head) {
95+
let headSequence = (await this.getHeadSequence()) - 1;
96+
if (!headSequence) {
9897
logger.error("Head sequence is not set. Cannot rollback.");
9998
return;
10099
}
101100
let block = null;
102101
while (!block) {
103-
block = await this.getBlockBySequence(head);
102+
block = await this.getLightBlockBySequence(headSequence);
104103
if (!block) {
105-
head -= 1;
104+
headSequence -= 1;
106105
}
107106
}
108-
await this.db.put("headSequence", head.toString());
109-
await this.db.put("head", block.hash);
110-
logger.info(`Rolled back head to block sequence ${head}`);
107+
await this.putHead(block.hash, headSequence);
108+
logger.info(`Rolled back head to block sequence ${headSequence}`);
111109
}
112110

113111
async cacheBlock(block: LightBlock): Promise<void> {
@@ -118,14 +116,12 @@ export class LightBlockCache {
118116
);
119117
}
120118
const hash = block.hash;
121-
await this.db.put(hash, LightBlock.encode(block).finish());
122-
await this.db.put(block.sequence.toString(), hash);
119+
await this.putLightBlock(block);
123120
const finalizedSequence = await this.getFinalizedBlockSequence();
124121
if (block.sequence - this.finalityBlockCount > finalizedSequence) {
125122
this.putFinalizedBlockSequence(block.sequence - this.finalityBlockCount);
126123
}
127-
await this.db.put("head", hash);
128-
await this.db.put("headSequence", block.sequence.toString());
124+
await this.putHead(hash, block.sequence);
129125
}
130126

131127
async getFinalizedBlockSequence(): Promise<number> {
@@ -135,23 +131,32 @@ export class LightBlockCache {
135131
: this.finalityBlockCount + 1;
136132
}
137133

138-
async getHead(): Promise<Buffer | null> {
139-
const head = await this.get("head");
140-
return head ? Buffer.from(head) : null;
134+
async putFinalizedBlockSequence(sequence: number): Promise<void> {
135+
await this.put("finalizedBlockSequence", Buffer.from(sequence.toString()));
141136
}
142137

143-
async putFinalizedBlockSequence(sequence: number): Promise<void> {
144-
await this.db.put("finalizedBlockSequence", sequence.toString());
138+
async putHead(hash: Buffer, sequence: number): Promise<void> {
139+
await this.put("head", hash);
140+
await this.put("headSequence", Buffer.from(sequence.toString()));
145141
}
146142

147-
async getBlockByHash(hash: string): Promise<LightBlock | null> {
148-
const block = await this.get(hash);
149-
return block ? LightBlock.decode(block) : null;
143+
async getLightBlock(hash: Buffer): Promise<LightBlock | null> {
144+
try {
145+
const data = await this.get(hash.toString("hex"));
146+
if (!data) return null;
147+
return LightBlock.decode(data);
148+
} catch (e) {
149+
return null;
150+
}
150151
}
151152

152-
async getBlockBySequence(sequence: number): Promise<LightBlock | null> {
153+
async getLightBlockBySequence(sequence: number): Promise<LightBlock | null> {
153154
const hash = await this.get(sequence.toString());
154-
return hash ? await this.getBlockByHash(hash.toString()) : null;
155+
return hash ? await this.getLightBlock(hash) : null;
156+
}
157+
158+
async getHead(): Promise<Buffer | null> {
159+
return this.get("head");
155160
}
156161

157162
async getHeadSequence(): Promise<number> {
@@ -160,7 +165,7 @@ export class LightBlockCache {
160165
return Number(head.toString());
161166
}
162167

163-
async get(key: string): Promise<Uint8Array | null> {
168+
async get(key: string): Promise<Buffer | null> {
164169
try {
165170
const data = await this.db.get(key);
166171
return data;
@@ -169,10 +174,17 @@ export class LightBlockCache {
169174
}
170175
}
171176

172-
async put(key: string, value: Uint8Array | string): Promise<void> {
177+
private async put(key: string, value: Buffer): Promise<void> {
173178
await this.db.put(key, value);
174179
}
175180

181+
async putLightBlock(block: LightBlock): Promise<void> {
182+
const key = block.hash.toString("hex");
183+
const value = LightBlock.encode(block).finish();
184+
await this.put(block.sequence.toString(), block.hash);
185+
await this.put(key, Buffer.from(value));
186+
}
187+
176188
async del(key: string): Promise<void> {
177189
await this.db.del(key);
178190
}

src/controllers/block.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,9 @@ export class BlockController {
7575

7676
let block = null;
7777
if (hash) {
78-
block = await lightBlockCache.getBlockByHash(String(hash));
78+
block = await lightBlockCache.getLightBlock(Buffer.from(hash, "hex"));
7979
} else if (sequence) {
80-
block = await lightBlockCache.getBlockBySequence(Number(sequence));
80+
block = await lightBlockCache.getLightBlockBySequence(Number(sequence));
8181
}
8282

8383
if (block) {
@@ -127,7 +127,7 @@ export class BlockController {
127127
// Placeholder logic: Fetch blocks from cache or database
128128
const blocks: LightBlock[] = [];
129129
for (let i = start; i <= end; i++) {
130-
const block = await lightBlockCache.getBlockBySequence(i);
130+
const block = await lightBlockCache.getLightBlockBySequence(i);
131131
if (block) {
132132
blocks.push(block);
133133
}

src/upload/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ describe("LightBlockUpload", () => {
2222

2323
it("should gzip blocks/manifest, last block should match", async () => {
2424
const tempFile = path.join(os.tmpdir(), "test");
25-
const firstBlock = (await lightBlockCache.getBlockBySequence(
25+
const firstBlock = (await lightBlockCache.getLightBlockBySequence(
2626
1,
2727
)) as LightBlock;
2828
const triggerUploadTime =

src/upload/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export class LightBlockUpload {
166166

167167
// eslint-disable-next-line no-constant-condition
168168
while (true) {
169-
const block = await this.cache.getBlockBySequence(currentSequence);
169+
const block = await this.cache.getLightBlockBySequence(currentSequence);
170170
const finalizedSequence = await this.cache.getFinalizedBlockSequence();
171171
if (!block || block.sequence > finalizedSequence) {
172172
const currentTimestamp = Date.now();
-7.76 KB
Binary file not shown.
5.93 KB
Binary file not shown.

test/dbfixture/cache/leveldb/000008.log

Whitespace-only changes.

test/dbfixture/cache/leveldb/CURRENT

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
MANIFEST-000002
1+
MANIFEST-000006

0 commit comments

Comments
 (0)