Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
mlibre committed Mar 29, 2024
1 parent 2a91acb commit ace6249
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 10 deletions.
2 changes: 1 addition & 1 deletion assets/bruno/goodchain/block/add.bru
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ post {

body:json {
{
"index": 5,
"index": 4,
"chainName": "GoodChain",
"timestamp": 1707760793668,
"transactions": [
Expand Down
25 changes: 21 additions & 4 deletions library/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,18 @@ class ChainStore
createFolder( this.folderPath );
}

push ( block )
get length ()
{
fs.writeFileSync( `${this.blockFilePath( block.index )}.json`, JSON.stringify( block, null, "\t" ) );
return fs.readdirSync( this.folderPath ).length;
}

get length ()
get all ()
{
return fs.readdirSync( this.folderPath ).length;
return fs.readdirSync( this.folderPath ).map( ( fileName ) =>
{
const filePath = path.join( this.folderPath, fileName );
return JSON.parse( fs.readFileSync( filePath ) );
});
}

get ( blockNumber )
Expand Down Expand Up @@ -58,6 +62,19 @@ class ChainStore
return JSON.parse( fs.readFileSync( this.blockFilePath( lastFile ) ) );
}

push ( block )
{
fs.writeFileSync( `${this.blockFilePath( block.index )}.json`, JSON.stringify( block, null, "\t" ) );
}

replaceBlocks ( blocks )
{
for ( const block of blocks )
{
this.push( block );
}
}

lastTwoBlocks ()
{
const lastBlock = this.latestBlock
Expand Down
15 changes: 11 additions & 4 deletions library/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,17 @@ class Blockchain

replaceChain ( newChain )
{
this.chain = newChain;
updateFile( this.filePath, this.chain )
this.wallet.reCalculateWallet( this.chain )
return newChain
try
{
this.chain.replaceBlocks( newChain );
this.wallet.reCalculateWallet( this.chain )
}
catch ( error )
{
this.db.reset()
this.wallet.reloadDB()
}
return this.chain.all
}

addBlocks ( blocks )
Expand Down
2 changes: 1 addition & 1 deletion restAPI/routes/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { isEqualBlock } = require( "../utils" )

router.get( "/", function ( req, res, next )
{
res.send( blockchain.chain );
res.send( blockchain.chain.all );
});

router.post( "/update", async function ( req, res, next )
Expand Down

0 comments on commit ace6249

Please sign in to comment.