Skip to content

Commit

Permalink
better block validation
Browse files Browse the repository at this point in the history
better git db
better function genesis name
better type name
  • Loading branch information
mlibre committed May 24, 2024
1 parent 539b3fb commit a8ed812
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ interface TransactionData
id?: string;
}

interface nodesBlocks
interface NodesBlocks
{
block: BlockData,
node: string
Expand Down
15 changes: 11 additions & 4 deletions src/library/block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" );
Expand All @@ -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
Expand Down
9 changes: 3 additions & 6 deletions src/library/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 )
{
Expand All @@ -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 });
}
}
4 changes: 2 additions & 2 deletions src/library/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/library/pow-consensus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export default class POW
});
}

chooseChain ( nodesBlocks: nodesBlocks[] ): nodesBlocks | undefined
chooseChain ( nodesBlocks: NodesBlocks[] ): NodesBlocks | undefined
{
return _.maxBy( nodesBlocks, ( nodeBlock ) =>
{
Expand Down

0 comments on commit a8ed812

Please sign in to comment.