Skip to content

Commit

Permalink
Update node_miner.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
vanvianen committed Jul 28, 2023
1 parent 6b7e56c commit 823cd0d
Showing 1 changed file with 41 additions and 3 deletions.
44 changes: 41 additions & 3 deletions src/lib/network/src/node_miner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,22 @@
// limitations under the License.



/**
* The Miner class represents the entity responsible for validating transactions and adding new blocks to the blockchain.
* It uses a wallet to receive mining rewards and a peer-to-peer server to synchronize its blockchain with other nodes.
* It also manages the pool of unconfirmed transactions.
*
* @module Miner
* @see Blockchain
* @see TransactionPool
* @see Transaction
* @see Wallet
* @see P2pServer
* @see Block
*/

// Import required modules
import Blockchain from "../../blockchain/src/blockchain";
import TransactionPool from "../../blockchain/src/transaction-pool";
import Transaction from "../../blockchain/src/transaction"
Expand All @@ -21,14 +37,25 @@ import P2pServer from "./node_server";
import Block from "../../blockchain/src/block";



export default class Miner {

// Blockchain instance
blockchain: Blockchain;
// Transaction Pool instance
tp: TransactionPool;
// Wallet instance
wallet: Wallet;
// Peer-to-peer Server instance
p2pServer: P2pServer;

/**
* Creates a new Miner instance.
*
* @param {Blockchain} blockchain - The blockchain instance.
* @param {TransactionPool} tp - The transaction pool instance.
* @param {Wallet} wallet - The wallet instance.
* @param {P2pServer} p2pServer - The peer-to-peer server instance.
*/
constructor(
blockchain: Blockchain,
tp: TransactionPool,
Expand All @@ -49,16 +76,27 @@ export default class Miner {
* - Synchronizes blockchains between all other peers
* - Clears transaction pool
* - Broadcasts to every miner to clear their transaction pool
*
* @returns {Block} The new block added to the blockchain.
*/
mine(): Block {
// Validate transactions in the pool
const validTransactions: Transaction [] = this.tp.validTransactions();
// Reward miner
validTransactions.push(Transaction.newRewardTransaction(this.wallet, Wallet.getBlockchainWallet()));
// Add new block to the blockchain
let block: Block = this.blockchain.addBlock(validTransactions);

// Synchronize chains across peers
this.p2pServer.syncChains();
// Clear transaction pool
this.tp.clear();
// Clear transaction pools across all miners
this.p2pServer.broadcastClearTxs();

return block;
}
}
}




0 comments on commit 823cd0d

Please sign in to comment.