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 20, 2024
1 parent 294b623 commit 7c028f9
Show file tree
Hide file tree
Showing 11 changed files with 60 additions and 140 deletions.
98 changes: 0 additions & 98 deletions assets/db/chain/chain.json

This file was deleted.

7 changes: 0 additions & 7 deletions assets/db/nodes/nodes.json

This file was deleted.

6 changes: 0 additions & 6 deletions assets/db/wallet/wallet.json

This file was deleted.

6 changes: 3 additions & 3 deletions assets/keys/miner.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"publicKey": "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAaix8hwAcEhpuYg+Mjcb2TdzAfr2pQqMPT5+zqCwGrRU=\n-----END PUBLIC KEY-----\n",
"privateKey": "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIGHnA5XP1UNWPIJXgSf2R84ZzicGESUG2xPGXKjFyy1Q\n-----END PRIVATE KEY-----\n",
"publicKeyString": "MCowBQYDK2VwAyEAaix8hwAcEhpuYg+Mjcb2TdzAfr2pQqMPT5+zqCwGrRU=\n"
"publicKey": "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAhyP0w4jZGRf3Ffov7TDGlMIYe5SGZ+ofO+VzCCxz+M4=\n-----END PUBLIC KEY-----\n",
"privateKey": "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIFvJp8GFrfqa9US55EkrqhgF+pDdty2BieIHnkeIhvc5\n-----END PRIVATE KEY-----\n",
"publicKeyString": "MCowBQYDK2VwAyEAhyP0w4jZGRf3Ffov7TDGlMIYe5SGZ+ofO+VzCCxz+M4=\n"
}
6 changes: 3 additions & 3 deletions assets/keys/user.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"publicKey": "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAUEsB5UeZFR/OHNDLFjGMN/gcJ6u9gs1Rx6diNimhD80=\n-----END PUBLIC KEY-----\n",
"privateKey": "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEICFfkDwB+8SKdN9ZnYYZ6tTFytxRW6EP+/Mu+ECnsCev\n-----END PRIVATE KEY-----\n",
"publicKeyString": "MCowBQYDK2VwAyEAUEsB5UeZFR/OHNDLFjGMN/gcJ6u9gs1Rx6diNimhD80=\n"
"publicKey": "-----BEGIN PUBLIC KEY-----\nMCowBQYDK2VwAyEAeih1vk1WjEOdJ7FLffnU432Pm1rS8zmW4hl8T0wIco8=\n-----END PUBLIC KEY-----\n",
"privateKey": "-----BEGIN PRIVATE KEY-----\nMC4CAQAwBQYDK2VwBCIEIAHfKeAY8PNy8GYthLziLqEyrRChfTFSxcWhgtzf3DMl\n-----END PRIVATE KEY-----\n",
"publicKeyString": "MCowBQYDK2VwAyEAeih1vk1WjEOdJ7FLffnU432Pm1rS8zmW4hl8T0wIco8=\n"
}
11 changes: 11 additions & 0 deletions library/chain.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ class ChainStore
fs.writeFileSync( this.folderPath + block.index, JSON.stringify( block, null, "\t" ) );
}

get length ()
{
return fs.readdirSync( this.folderPath ).length;
}

checkDB ( proposedBlock )
{
const [ lastBlock, secondLastBlock ] = [ this.get( proposedBlock.index - 1 ), this.get( proposedBlock.index - 2 ) ];
Expand All @@ -36,6 +41,12 @@ class ChainStore
return JSON.parse( fs.readFileSync( this.blockFilePath( blockNumber ) ) );
}

lastBlock ()
{
const files = fs.readdirSync( this.folderPath );
const lastFile = files.sort().pop();
return JSON.parse( fs.readFileSync( this.blockFilePath( lastFile ) ) );
}
lastTwoBlocks ()
{
const files = fs.readdirSync( this.folderPath );
Expand Down
18 changes: 7 additions & 11 deletions library/db-git.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


const { execSync } = require( "child_process" );
const { createFolder } = require( "./utils" );

class GitDatabase
{
Expand All @@ -20,21 +21,16 @@ class GitDatabase
// };
// this.git = simpleGit( options );
// this.git.clean( simpleGit.CleanOptions.FORCE )
this.repoPath = repoPath;
this.initGitRepo( repoPath );
}

initGitRepo ( folderPath )
initGitRepo ( )
{
try
{
const output = execSync( "git init .", { cwd: folderPath }).toString();
console.log( "Git repository initialized ", output );
}
catch ( error )
{
console.error( `Failed to initialize Git repository in ${folderPath}:`, error );
}
}
createFolder( this.repoPath );
const output = execSync( "git init .", { cwd: this.repoPath }).toString();
console.log( "Git repository initialized ", output );
}

async commit ( blockNumber )
{
Expand Down
1 change: 0 additions & 1 deletion library/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ class Blockchain
{
Block.verify( block, this.latestBlock )
this.consensus.validate( block, this.latestBlock );
this.simulateTransactions( block.transactions )
return true
}

Expand Down
25 changes: 25 additions & 0 deletions library/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,31 @@ exports.deleteFile = function ( filePath )
}
}

exports.recreateFolder = function ( folderPath )
{
if ( fs.existsSync( folderPath ) )
{
fs.rmdirSync( folderPath, { recursive: true });
console.log( `Folder ${folderPath} Deleted` );
}
fs.mkdirSync( folderPath );
console.log( `Folder ${folderPath} Created` );
}

exports.createFolder = function ( folderPath )
{
if ( !fs.existsSync( folderPath ) )
{
fs.mkdirSync( folderPath );
console.log( `Folder ${folderPath} Created` );
}
else
{
console.log( `Folder ${folderPath} already exists` );
return false;
}
}

exports.uuid = function ()
{
return uuidv4();
Expand Down
12 changes: 6 additions & 6 deletions library/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class Wallet
const trx = new Transaction( tmpTrx );
if ( trx.isCoinBase( ) )
{
this.wallet.addBalance( trx.to, trx.amount );
this.addBalance( trx.to, trx.amount );
continue
}
this.wallet.minusBalance( trx.from, trx.amount + trx.fee );
this.wallet.incrementTN( trx.from );
this.wallet.addBalance( trx.to, trx.amount );
this.minusBalance( trx.from, trx.amount + trx.fee );
this.incrementTN( trx.from );
this.addBalance( trx.to, trx.amount );
}
this.wallet.blockNumber = this.wallet.blockNumber + 1
this.wallet.updateDB()
this.wallet.blockNumber = ( this.wallet.blockNumber || -1 ) + 1
this.updateDB()
return transactionList;
}

Expand Down
10 changes: 5 additions & 5 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ const Transaction = require( "./library/transactions" )
const Consensus = require( "./library/pow-consensus" );
const consensus = new Consensus()

const { deleteFile, initJsonFile, createKeyPair } = require( "./library/utils" )
deleteFile( "./assets/db/blockchain.json" );
deleteFile( "./assets/db/wallets.json" );
deleteFile( "./assets/db/nodes.json" );
const { deleteFile, recreateFolder, initJsonFile, createKeyPair } = require( "./library/utils" )
recreateFolder( "./assets/db/chain/" );
deleteFile( "./assets/db/wallet/wallet.json" );
deleteFile( "./assets/db/nodes/nodes.json" );
deleteFile( "./assets/keys/miner.json" );
deleteFile( "./assets/keys/user.json" );

const userKeys = initJsonFile( "./assets/keys/user.json", createKeyPair() );
const minerKeys = initJsonFile( "./assets/keys/miner.json", createKeyPair() );
const blockchain = new Blockchain({
dbFolderPath: "./assets/db/",
chainFolderPath: "./assets/db/chain/chain.json",
chainFolderPath: "./assets/db/chain/",
walletFilePath: "./assets/db/wallet/wallet.json",
nodes: {
filePath: "./assets/db/nodes/nodes.json",
Expand Down

0 comments on commit 7c028f9

Please sign in to comment.