Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactored handling of errors in decompression #87

Merged
merged 2 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions common/protocol/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import {
getBalancesForMetrics,
runCache,
runNode,
saveBundleDecompress,
saveBundleDownload,
saveLoadValidationBundle,
setupCacheProvider,
Expand Down Expand Up @@ -138,7 +137,6 @@ export class Validator {

// validate
protected saveBundleDownload = saveBundleDownload;
protected saveBundleDecompress = saveBundleDecompress;
protected saveLoadValidationBundle = saveLoadValidationBundle;
protected validateBundleProposal = validateBundleProposal;

Expand Down
1 change: 0 additions & 1 deletion common/protocol/src/methods/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export * from "./queries/getBalancesForMetrics";
export * from "./queries/syncPoolState";

// validate
export * from "./validate/saveBundleDecompress";
export * from "./validate/saveBundleDownload";
export * from "./validate/saveLoadValidationBundle";
export * from "./validate/validateBundleProposal";
Expand Down
42 changes: 0 additions & 42 deletions common/protocol/src/methods/validate/saveBundleDecompress.ts

This file was deleted.

44 changes: 37 additions & 7 deletions common/protocol/src/methods/validate/validateBundleProposal.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { VoteType } from "@kyvejs/types/client/kyve/bundles/v1beta1/tx";

import { Validator } from "../..";
import { sha256, standardizeError, VOTE } from "../../utils";
import { DataItem, Validator } from "../..";
import { bytesToBundle, sha256, standardizeError, VOTE } from "../../utils";

/**
* validateBundleProposal validates a proposed bundle proposal
Expand Down Expand Up @@ -137,12 +137,42 @@ export async function validateBundleProposal(

// if storage provider result is empty skip runtime validation
if (storageProviderResult.byteLength) {
try {
// decompress the bundle with the specified compression type
// and convert the bytes into a JSON format
const proposedBundle = await this.saveBundleDecompress(
storageProviderResult
// get current compression defined on pool
this.logger.debug(`this.compressionFactory()`);
const compression = this.compressionFactory();

// decompress the bundle with the specified compression type
// and convert the bytes into a JSON format
this.logger.debug(
`this.compression.decompress($STORAGE_PROVIDER_RESULT)`
);
const decompressed = await compression
.decompress(storageProviderResult)
.catch((err) => {
this.logger.error(
`Unexpected error decompressing bundle. Voting abstain ...`
);
this.logger.error(standardizeError(err));

return null;
});

// vote abstain if decompressed is null
if (decompressed === null) {
const success = await this.voteBundleProposal(
this.pool.bundle_proposal!.storage_id,
VOTE.ABSTAIN
);
return success;
}

this.logger.info(
`Successfully decompressed bundle with Compression:${compression.name}`
);

try {
// parse raw decompressed bundle back to json format
const proposedBundle: DataItem[] = bytesToBundle(decompressed);

// perform custom runtime bundle validation
this.logger.debug(
Expand Down
123 changes: 0 additions & 123 deletions common/protocol/test/compression.test.ts

This file was deleted.

77 changes: 77 additions & 0 deletions common/protocol/test/compression/gzip.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { randomBytes } from "crypto";
import { gzipSync } from "zlib";
import { ICompression } from "../../src/index";
import { Gzip } from "../../src/reactors/compression/Gzip";

/*

TEST CASES - Gzip

* assert name
* assert mime-type
* assert compression
* assert decompression
* assert decompression with invalid input

*/

describe("gzip", () => {
type NewType = ICompression;

let compression: NewType;

beforeEach(() => {
compression = new Gzip();
});

test("assert name", () => {
// ACT
const name = compression.name;

// ASSERT
expect(name).toEqual("Gzip");
});

test("assert mime-type", () => {
// ACT
const mimeType = compression.mimeType;

// ASSERT
expect(mimeType).toEqual("application/gzip");
});

test("assert compression", async () => {
// ARRANGE
const data = randomBytes(32);

// ACT
const compressed = await compression.compress(data);

// ASSERT
const compressedCheck = gzipSync(data);
expect(compressed).toEqual(compressedCheck);
});

test("assert decompression", async () => {
// ARRANGE
const data = randomBytes(32);
const compressed = await compression.compress(data);

// ACT
const decompressed = await compression.decompress(compressed);

// ASSERT
expect(data).toEqual(decompressed);
});

test("assert decompression with invalid input", async () => {
// ARRANGE
const invalidCompressed = randomBytes(32);

const decompress = async () =>
await compression.decompress(invalidCompressed);

// ACT & ASSERT
expect(decompress()).rejects.toThrowError();
});
});
65 changes: 65 additions & 0 deletions common/protocol/test/compression/no_compression.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { randomBytes } from "crypto";
import { ICompression } from "../../src/index";
import { NoCompression } from "../../src/reactors/compression/NoCompression";

/*

TEST CASES - NoCompression

* assert name
* assert mime-type
* assert compression
* assert decompression

*/

describe("noCompression", () => {
type NewType = ICompression;

let compression: NewType;

beforeEach(() => {
compression = new NoCompression();
});

test("assert name", () => {
// ACT
const name = compression.name;

// ASSERT
expect(name).toEqual("NoCompression");
});

test("assert mime-type", () => {
// ACT
const mimeType = compression.mimeType;

// ASSERT
expect(mimeType).toEqual("application/json");
});

test("assert compression", async () => {
// ARRANGE
const data = randomBytes(32);

// ACT
const compressed = await compression.compress(data);

// ASSERT
// since NoCompression does not compress we can make
// an equal check here
expect(data).toEqual(compressed);
});

test("assert decompression", async () => {
// ARRANGE
const data = randomBytes(32);
const compressed = await compression.compress(data);

// ACT
const decompressed = await compression.decompress(compressed);

// ASSERT
expect(data).toEqual(decompressed);
});
});
Loading