From 00b9ade296a3bc81c2ab3f2ebb2896ae27a699ae Mon Sep 17 00:00:00 2001 From: Troy Kessler <43882936+troykessler@users.noreply.github.com> Date: Tue, 5 Mar 2024 16:05:25 +0100 Subject: [PATCH] fix: debug bundles (#121) * fix: archive debug bundle on invalid vote and added unit tests --- common/protocol/package.json | 1 - .../src/methods/helpers/archiveDebugBundle.ts | 144 +++++++++--------- yarn.lock | 5 - 3 files changed, 73 insertions(+), 77 deletions(-) diff --git a/common/protocol/package.json b/common/protocol/package.json index 8643a3dd..a9d77583 100644 --- a/common/protocol/package.json +++ b/common/protocol/package.json @@ -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", diff --git a/common/protocol/src/methods/helpers/archiveDebugBundle.ts b/common/protocol/src/methods/helpers/archiveDebugBundle.ts index 6fb133f4..6228dfbc 100644 --- a/common/protocol/src/methods/helpers/archiveDebugBundle.ts +++ b/common/protocol/src/methods/helpers/archiveDebugBundle.ts @@ -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 @@ -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)); diff --git a/yarn.lock b/yarn.lock index 3a9da869..76993553 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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"