From a8ed812057bb9d1ef11a7f83c10ed112758f7fa5 Mon Sep 17 00:00:00 2001 From: mlibre Date: Fri, 24 May 2024 21:42:07 +0330 Subject: [PATCH] better block validation better git db better function genesis name better type name --- src/global.d.ts | 2 +- src/library/block.ts | 15 +++++++++++---- src/library/database.ts | 9 +++------ src/library/main.ts | 4 ++-- src/library/pow-consensus.ts | 2 +- 5 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/global.d.ts b/src/global.d.ts index bce2a23..1b4e93a 100644 --- a/src/global.d.ts +++ b/src/global.d.ts @@ -42,7 +42,7 @@ interface TransactionData id?: string; } -interface nodesBlocks +interface NodesBlocks { block: BlockData, node: string diff --git a/src/library/block.ts b/src/library/block.ts index bb83d0a..939bd2c 100644 --- a/src/library/block.ts +++ b/src/library/block.ts @@ -9,19 +9,18 @@ export function verifyBlock ( currentBlock: BlockData, previousBlock: BlockData { throw new Error( "Invalid block hash" ); } - if ( blockWithoutHash.chainName !== previousBlock.chainName ) + if ( currentBlock.chainName !== previousBlock.chainName ) { throw new Error( "Invalid chain name" ); } - if ( blockWithoutHash.index !== previousBlock.index + 1 ) + if ( currentBlock.index !== previousBlock.index + 1 ) { throw new Error( "Invalid index" ); } - if ( previousBlock.hash !== currentBlock.previousHash ) + if ( currentBlock.previousHash !== previousBlock.hash ) { throw new Error( "Invalid previous hash" ); } - if ( currentBlock.timestamp < previousBlock.timestamp ) { throw new Error( "Block timestamp must be greater than previous block timestamp" ); @@ -40,6 +39,14 @@ export function verifyGenesisBlock ( block: BlockData ): void { throw new Error( "Invalid block hash" ); } + if ( block.index !== 0 ) + { + throw new Error( "Invalid index" ); + } + if ( block.previousHash !== "" ) + { + throw new Error( "Invalid previous hash" ); + } } export function blockify ( data: BlockData ): BlockData diff --git a/src/library/database.ts b/src/library/database.ts index 99f0701..2cb7896 100644 --- a/src/library/database.ts +++ b/src/library/database.ts @@ -26,12 +26,10 @@ export default class GitDatabase commit ( blockNumber: string | number ) { - const addOutput: string = execSync( "git add --all", { cwd: this.repoPath }).toString(); - console.log( "Git repository added files ", addOutput ); - try { - execSync( `git commit -m "${blockNumber}"`, { cwd: this.repoPath }).toString(); + execSync( "git add --all", { cwd: this.repoPath }); + execSync( `git commit -m "${blockNumber}"`, { cwd: this.repoPath }); } catch ( error: unknown ) { @@ -52,7 +50,6 @@ export default class GitDatabase reset () { - const resetOutput: string = execSync( "git reset --hard", { cwd: this.repoPath }).toString(); - console.log( "Git repository reset", resetOutput ); + execSync( "git reset --hard", { cwd: this.repoPath }); } } \ No newline at end of file diff --git a/src/library/main.ts b/src/library/main.ts index bd88e22..da154d5 100644 --- a/src/library/main.ts +++ b/src/library/main.ts @@ -35,13 +35,13 @@ export default class Blockchain if ( this.chain.length === 0 ) { - this.#minGenesisBlock(); + this.#mineGenesisBlock(); } this.consensus.setValues( this.chain.latestBlock ); } - #minGenesisBlock () + #mineGenesisBlock () { const self = this; try diff --git a/src/library/pow-consensus.ts b/src/library/pow-consensus.ts index d8ffbee..8302729 100644 --- a/src/library/pow-consensus.ts +++ b/src/library/pow-consensus.ts @@ -111,7 +111,7 @@ export default class POW }); } - chooseChain ( nodesBlocks: nodesBlocks[] ): nodesBlocks | undefined + chooseChain ( nodesBlocks: NodesBlocks[] ): NodesBlocks | undefined { return _.maxBy( nodesBlocks, ( nodeBlock ) => {