-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathminer.ts
More file actions
39 lines (36 loc) · 1.41 KB
/
Copy pathminer.ts
File metadata and controls
39 lines (36 loc) · 1.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import Blockchain from "../blockchain";
import TransactionPool from "../wallet/transaction-pool";
import Transaction from "../wallet/transaction"
import Wallet from "../wallet";
import P2pServer from "./p2p-server";
import Block from "../blockchain/block";
export default class Miner {
blockchain: Blockchain;
tp: TransactionPool;
wallet: Wallet;
p2pServer: P2pServer;
constructor(blockchain: Blockchain, tp: TransactionPool, wallet: Wallet, p2pServer: P2pServer) {
this.blockchain = blockchain;
this.tp = tp;
this.wallet = wallet;
this.p2pServer = p2pServer;
}
/**
* Mines a new transaction on the blockchain by:
* - Validating transactions on the transaction pool
* - Rewarding the miner
* - Creating new block on the blockchain consisting of the newly validated transactions
* - Synchronizes blockchains between all other peers
* - Clears transaction pool
* - Broadcasts to every miner to clear their transaction pool
*/
mine(): Block {
const validTransactions: Transaction [] = this.tp.validTransactions();
validTransactions.push(Transaction.newRewardTransaction(this.wallet, Wallet.getBlockchainWallet()));
let block: Block = this.blockchain.addBlock(validTransactions);
this.p2pServer.syncChains();
this.tp.clear();
this.p2pServer.broadcastClearTxs();
return block;
}
}