Skip to content

Commit

Permalink
No commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
mlibre committed May 30, 2024
1 parent d084e35 commit 71abcb1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
24 changes: 12 additions & 12 deletions src/library/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default class Blockchain
const self = this;
await self.database.clear();
const coinbaseTrx = self.genCoinbaseTransaction();
self.addTransaction( coinbaseTrx );
await self.addTransaction( coinbaseTrx );
const block: BlockData = {
index: 0,
chainName: self.chainName,
Expand All @@ -71,7 +71,7 @@ export default class Blockchain
const blockIndex = await self.chain.length();
self.transactionPool = await self.wallet.cleanupTransactions( self.transactionPool );
const coinbaseTrx = self.genCoinbaseTransaction();
self.addTransaction( coinbaseTrx );
await self.addTransaction( coinbaseTrx );
const lastBlock = await self.chain.latestBlock();
const block: BlockData = {
index: blockIndex + 1,
Expand Down Expand Up @@ -106,9 +106,9 @@ export default class Blockchain
return blocks;
}

getBlocks ( from: number, to: number )
async getBlocks ( from: number, to: number )
{
return this.chain.getRange( from, to );
return await this.chain.getRange( from, to );
}

async verifyCandidateBlock ( block: BlockData )
Expand All @@ -134,10 +134,10 @@ export default class Blockchain

if ( !trx.isCoinBase() && trx.from !== null )
{
this.wallet.validateAddress( trx.from );
await this.wallet.validateAddress( trx.from );
await this.wallet.isTransactionNumberCorrect( trx.from, trx.transaction_number );
}
this.wallet.validateAddress( trx.to );
await this.wallet.validateAddress( trx.to );

trx.validate();
this.isTransactionDuplicate( trx.data.signature );
Expand All @@ -147,7 +147,7 @@ export default class Blockchain
{
return b.fee - a.fee;
});
return this.chain.length();
return await this.chain.length() + 1;
}

async addTransactions ( transactions: TransactionData[] )
Expand Down Expand Up @@ -202,11 +202,6 @@ export default class Blockchain
}
}

addNode ( url: string )
{
return this.nodes.add( url );
}

async replaceChain ( newChain: BlockData[] )
{
await this.database.clear();
Expand All @@ -220,4 +215,9 @@ export default class Blockchain
}
return this.chain.getAll();
}

addNode ( url: string )
{
return this.nodes.add( url );
}
}
28 changes: 14 additions & 14 deletions src/test/blockchain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe( "Blockchain Test Suite", () =>
expect( blockchain.chain.validateChain() ).toBe( true );
});

test( "Sending a transaction from miner to sender and mining a new block", () =>
test( "Sending a transaction from miner to sender and mining a new block", async () =>
{
const transaction1 = new Transaction({
from: minerKeys.publicKey,
Expand All @@ -46,14 +46,14 @@ describe( "Blockchain Test Suite", () =>
signature: null
});
transaction1.sign( minerKeys.privateKey );
blockchain.addTransaction( transaction1.data );
await blockchain.addTransaction( transaction1.data );

const blockWithTransaction1 = blockchain.mineNewBlock(); // miner: 250, miner receives his own trx fee
expect( blockWithTransaction1.transactions.length ).toBe( 2 ); // including coinbase transaction
expect( blockchain.chain.validateChain() ).toBe( true );
});

test( "Sending a transaction from sender to receiver and mining a new block", () =>
test( "Sending a transaction from sender to receiver and mining a new block", async () =>
{
const transaction2 = new Transaction({
from: senderKeys.publicKey,
Expand All @@ -64,7 +64,7 @@ describe( "Blockchain Test Suite", () =>
signature: null
});
transaction2.sign( senderKeys.privateKey );
blockchain.addTransaction( transaction2.data );
await blockchain.addTransaction( transaction2.data );

const blockWithTransaction2 = blockchain.mineNewBlock(); // miner: 351
expect( blockWithTransaction2.transactions.length ).toBe( 2 ); // including coinbase transaction
Expand All @@ -88,7 +88,7 @@ describe( "Blockchain Test Suite", () =>
expect( minerWalletBalance ).toBe( 351 ); // 100 + 100 + 50 + 1 + 100
});

test( "Handling transaction with insufficient funds", () =>
test( "Handling transaction with insufficient funds", async () =>
{
const transaction3 = new Transaction({
from: senderKeys.publicKey,
Expand All @@ -101,7 +101,7 @@ describe( "Blockchain Test Suite", () =>
transaction3.sign( senderKeys.privateKey );
try
{
blockchain.addTransaction( transaction3.data );
await blockchain.addTransaction( transaction3.data );
}
catch ( e )
{
Expand All @@ -116,7 +116,7 @@ describe( "Blockchain Test Suite", () =>
}
});

test( "Handling duplicate transaction number", () =>
test( "Handling duplicate transaction number", async () =>
{
const transaction4 = new Transaction({
from: senderKeys.publicKey,
Expand All @@ -129,7 +129,7 @@ describe( "Blockchain Test Suite", () =>
transaction4.sign( senderKeys.privateKey );
try
{
blockchain.addTransaction( transaction4.data );
await blockchain.addTransaction( transaction4.data );
}
catch ( e: unknown )
{
Expand All @@ -144,7 +144,7 @@ describe( "Blockchain Test Suite", () =>
}
});

test( "Handling invalid signature", () =>
test( "Handling invalid signature", async () =>
{
const transaction5 = new Transaction({
from: senderKeys.publicKey,
Expand All @@ -156,7 +156,7 @@ describe( "Blockchain Test Suite", () =>
});
try
{
blockchain.addTransaction( transaction5.data );
await blockchain.addTransaction( transaction5.data );
}
catch ( e )
{
Expand All @@ -171,7 +171,7 @@ describe( "Blockchain Test Suite", () =>
}
});

test( "Handling transaction with zero amount", () =>
test( "Handling transaction with zero amount", async () =>
{
const transaction6 = new Transaction({
from: senderKeys.publicKey,
Expand All @@ -184,7 +184,7 @@ describe( "Blockchain Test Suite", () =>
transaction6.sign( senderKeys.privateKey );
try
{
blockchain.addTransaction( transaction6.data );
await blockchain.addTransaction( transaction6.data );
}
catch ( e )
{
Expand All @@ -199,7 +199,7 @@ describe( "Blockchain Test Suite", () =>
}
});

test( "Handling transaction with negative amount", () =>
test( "Handling transaction with negative amount", async () =>
{
const transaction7 = new Transaction({
from: senderKeys.publicKey,
Expand All @@ -212,7 +212,7 @@ describe( "Blockchain Test Suite", () =>
transaction7.sign( senderKeys.privateKey );
try
{
blockchain.addTransaction( transaction7.data );
await blockchain.addTransaction( transaction7.data );
}
catch ( e )
{
Expand Down
12 changes: 6 additions & 6 deletions src/test/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const trx = new Transaction({
});
trx.sign( minerKeys.privateKey );

const blockNumber = blockchain.addTransaction( trx.data );
const blockNumber = await blockchain.addTransaction( trx.data );
blockchain.mineNewBlock();
console.log( "Mined block :", blockNumber, blockchain.chain.latestBlock );

Expand All @@ -46,10 +46,10 @@ const trx2 = new Transaction({
});
trx2.sign( userKeys.privateKey );

blockchain.addTransaction( trx2.data );
blockchain.mineNewBlock();
await blockchain.addTransaction( trx2.data );
await blockchain.mineNewBlock();

console.log( "Latest Block :", blockchain.chain.latestBlock );
console.log( "Wallets : ", blockchain.wallet );
console.log( "chain validation:", blockchain.chain.validateChain() );
console.log( "Latest Block :", await blockchain.chain.latestBlock() );
console.log( "Wallets : ", await blockchain.wallet.allWallets() );
console.log( "chain validation:", await blockchain.chain.validateChain() );

0 comments on commit 71abcb1

Please sign in to comment.