Skip to content

Commit

Permalink
fix: debug bundles (#121)
Browse files Browse the repository at this point in the history
* fix: archive debug bundle on invalid vote and added unit tests
  • Loading branch information
troykessler authored Mar 5, 2024
1 parent 84cba4e commit 00b9ade
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 77 deletions.
1 change: 0 additions & 1 deletion common/protocol/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
"bignumber.js": "^9.1.2",
"clone": "^2.1.2",
"commander": "^9.4.1",
"diff": "^5.1.0",
"fs-extra": "^10.0.1",
"jsonfile": "^6.1.0",
"jszip": "^3.10.1",
Expand Down
144 changes: 73 additions & 71 deletions common/protocol/src/methods/helpers/archiveDebugBundle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import path from "path";
import { DataItem, standardizeError, Validator } from "../..";
import JSZip from "jszip";
import { VoteType } from "@kyvejs/types/client/kyve/bundles/v1beta1/tx";
import Diff from "diff";

/**
* archiveDebugBundle is used for storing a bundle for debug
Expand All @@ -25,76 +24,79 @@ export function archiveDebugBundle(
metadata: object
): void {
try {
// this.logger.info("Archiving debug bundle");
// // if "debug" folder under target path does not exist create it
// if (!existsSync(path.join(this.home, `debug`))) {
// mkdirSync(path.join(this.home, `debug`), { recursive: true });
// }
// const zip = new JSZip();
// // save metadata which includes vote reasons and args
// const metadata_str = JSON.stringify(metadata || {}, null, 2);
// zip.file("metadata.json", metadata_str);
// // save current pool state including the raw bundle proposal
// const pool_str = JSON.stringify(this.pool || {}, null, 2);
// zip.file("pool.json", pool_str);
// // save the proposed bundle from the uploader
// const proposed_bundle_str = JSON.stringify(proposedBundle || [], null, 2);
// zip.file("proposed_bundle.json", proposed_bundle_str);
// // save the locally created bundle from this node
// const validation_bundle_str = JSON.stringify(
// validationBundle || [],
// null,
// 2
// );
// zip.file("validation_bundle.json", validation_bundle_str);
// // save the diff of the proposed and local bundle
// const diff_str = Diff.createTwoFilesPatch(
// "proposed_bundle.json",
// "validation_bundle.json",
// proposed_bundle_str,
// validation_bundle_str
// );
// zip.file("diff.txt", diff_str);
// // save the logfile of the current session
// const debug_str = readFileSync(path.join(this.home, "logs", this.logFile));
// zip.file("debug.log", debug_str);
// // get human readable vote
// let voteType = "";
// switch (vote) {
// case VoteType.VOTE_TYPE_VALID:
// voteType = "valid";
// break;
// case VoteType.VOTE_TYPE_INVALID:
// voteType = "invalid";
// break;
// case VoteType.VOTE_TYPE_ABSTAIN:
// voteType = "abstain";
// break;
// case VoteType.VOTE_TYPE_UNSPECIFIED:
// voteType = "unspecified";
// break;
// default:
// voteType = "unrecognized";
// }
// const storageId = this.pool?.bundle_proposal?.storage_id ?? "";
// const zipPath = path.join(
// this.home,
// `debug`,
// `${voteType}_${this.pool.id}_${Math.floor(
// Date.now() / 1000
// )}_${storageId.slice(0, 6)}.zip`
// );
// // save zip file
// zip
// .generateNodeStream({ type: "nodebuffer", streamFiles: true })
// .pipe(createWriteStream(zipPath))
// .on("finish", () => {
// this.logger.info("Successfully saved debug information");
// })
// .on("error", (err) => {
// this.logger.error("Failed to save debug information");
// this.logger.error(standardizeError(err));
// });
this.logger.info("Archiving debug bundle");

// if "debug" folder under target path does not exist create it
if (!existsSync(path.join(this.home, `debug`))) {
mkdirSync(path.join(this.home, `debug`), { recursive: true });
}

const zip = new JSZip();

// save metadata which includes vote reasons and args
const metadata_str = JSON.stringify(metadata || {}, null, 2);
zip.file("metadata.json", metadata_str);

// save current pool state including the raw bundle proposal
const pool_str = JSON.stringify(this.pool || {}, null, 2);
zip.file("pool.json", pool_str);

// save the proposed bundle from the uploader
const proposed_bundle_str = JSON.stringify(proposedBundle || [], null, 2);
zip.file("proposed_bundle.json", proposed_bundle_str);

// save the locally created bundle from this node
const validation_bundle_str = JSON.stringify(
validationBundle || [],
null,
2
);
zip.file("validation_bundle.json", validation_bundle_str);

// save the logfile of the current session
const debug_str = readFileSync(path.join(this.home, "logs", this.logFile));
zip.file("debug.log", debug_str);

// get human readable vote
let voteType = "";

switch (vote) {
case VoteType.VOTE_TYPE_VALID:
voteType = "valid";
break;
case VoteType.VOTE_TYPE_INVALID:
voteType = "invalid";
break;
case VoteType.VOTE_TYPE_ABSTAIN:
voteType = "abstain";
break;
case VoteType.VOTE_TYPE_UNSPECIFIED:
voteType = "unspecified";
break;
default:
voteType = "unrecognized";
}

const storageId = this.pool?.bundle_proposal?.storage_id ?? "";
const zipPath = path.join(
this.home,
`debug`,
`${voteType}_${this.pool.id}_${Math.floor(
Date.now() / 1000
)}_${storageId.slice(0, 6)}.zip`
);

// save zip file
zip
.generateNodeStream({ type: "nodebuffer", streamFiles: true })
.pipe(createWriteStream(zipPath))
.on("finish", () => {
this.logger.info("Successfully saved debug information");
})
.on("error", (err) => {
this.logger.error("Failed to save debug information");
this.logger.error(standardizeError(err));
});
} catch (err) {
this.logger.error("Failed to save debug information");
this.logger.error(standardizeError(err));
Expand Down
5 changes: 0 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4901,11 +4901,6 @@ diff@^4.0.1:
resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d"
integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==

diff@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/diff/-/diff-5.1.0.tgz#bc52d298c5ea8df9194800224445ed43ffc87e40"
integrity sha512-D+mk+qE8VC/PAUrlAU34N+VfXev0ghe5ywmpqrawphmVZc1bEfn56uo9qpyGp1p4xpzOHkSW4ztBd6L7Xx4ACw==

dir-glob@^3.0.1:
version "3.0.1"
resolved "https://registry.yarnpkg.com/dir-glob/-/dir-glob-3.0.1.tgz#56dbf73d992a4a93ba1584f4534063fd2e41717f"
Expand Down

0 comments on commit 00b9ade

Please sign in to comment.