Skip to content

Commit cf447f2

Browse files
committed
use public accessors explicitly
1 parent 5ca8e1f commit cf447f2

File tree

11 files changed

+84
-88
lines changed

11 files changed

+84
-88
lines changed

.eslintrc.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module.exports = {
1111
},
1212
rules: {
1313
// Place to specify ESLint rules. Can be used to overwrite rules specified from the extended configs
14-
// e.g. "@typescript-eslint/explicit-function-return-type": "off",
14+
'@typescript-eslint/explicit-function-return-type': 'off',
15+
'@typescript-eslint/no-non-null-assertion': 'off',
1516
},
1617
};

src/app/miner.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ import TransactionPool from '../wallet/transaction-pool';
66
import P2pServer from './p2p-server';
77

88
export default class Miner {
9-
blockchain: Blockchain;
10-
transactionPool: TransactionPool;
11-
wallet: Wallet;
12-
p2pServer: P2pServer;
9+
public blockchain: Blockchain;
10+
public transactionPool: TransactionPool;
11+
public wallet: Wallet;
12+
public p2pServer: P2pServer;
1313

14-
constructor(blockchain: Blockchain, transactionPool: TransactionPool, wallet: Wallet, p2pServer: P2pServer) {
14+
public constructor(blockchain: Blockchain, transactionPool: TransactionPool, wallet: Wallet, p2pServer: P2pServer) {
1515
this.blockchain = blockchain;
1616
this.transactionPool = transactionPool;
1717
this.wallet = wallet;
1818
this.p2pServer = p2pServer;
1919
}
2020

21-
mine(): Block {
21+
public mine(): Block {
2222
const validTransactions = this.transactionPool.validTransactions();
2323
validTransactions.push(Transaction.rewardTransaction(this.wallet, Wallet.blockchainWallet()));
2424
const block = this.blockchain.addBlock(validTransactions);

src/app/p2p-server.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ const peers = process.env.PEERS ? process.env.PEERS.split(',') : [];
99
const MESSAGE_TYPES = {
1010
chain: 'CHAIN',
1111
transaction: 'TRANSACTION',
12-
clear_transactions: 'CLEAR_TRANSACTIONS',
12+
clearTransactions: 'CLEAR_TRANSACTIONS',
1313
};
1414

1515
export default class P2pServer {
16-
blockchain: Blockchain;
17-
transactionPool: TransactionPool;
18-
sockets: Websocket[];
16+
public blockchain: Blockchain;
17+
public transactionPool: TransactionPool;
18+
public sockets: Websocket[];
1919

20-
constructor(blockchain: Blockchain, transactionPool: TransactionPool) {
20+
public constructor(blockchain: Blockchain, transactionPool: TransactionPool) {
2121
this.blockchain = blockchain;
2222
this.transactionPool = transactionPool;
2323
this.sockets = [];
2424
}
2525

26-
listen(): void {
26+
public listen(): void {
2727
const server = new Websocket.Server({ port: P2P_PORT });
2828
server.on('connection', socket => this.connectSocket(socket));
2929

@@ -32,15 +32,15 @@ export default class P2pServer {
3232
console.log(`Listening for peer-to-peer connections on: ${P2P_PORT}`);
3333
}
3434

35-
connectToPeers(): void {
35+
public connectToPeers(): void {
3636
peers.forEach(_ => {
3737
const socket = new Websocket(_);
3838

3939
socket.on('open', () => this.connectSocket(socket));
4040
});
4141
}
4242

43-
connectSocket(socket: Websocket): void {
43+
public connectSocket(socket: Websocket): void {
4444
this.sockets.push(socket);
4545
console.log('Socket connected');
4646

@@ -49,7 +49,7 @@ export default class P2pServer {
4949
this.sendChain(socket);
5050
}
5151

52-
messageHandler(socket: Websocket): void {
52+
public messageHandler(socket: Websocket): void {
5353
socket.on('message', message => {
5454
const data = JSON.parse(message as string);
5555
switch (data.type) {
@@ -59,14 +59,14 @@ export default class P2pServer {
5959
case MESSAGE_TYPES.transaction:
6060
this.transactionPool.updateOrAddTransaction(data.transaction);
6161
break;
62-
case MESSAGE_TYPES.clear_transactions:
62+
case MESSAGE_TYPES.clearTransactions:
6363
this.transactionPool.clear();
6464
break;
6565
}
6666
});
6767
}
6868

69-
sendChain(socket: Websocket): void {
69+
public sendChain(socket: Websocket): void {
7070
socket.send(
7171
JSON.stringify({
7272
type: MESSAGE_TYPES.chain,
@@ -75,7 +75,7 @@ export default class P2pServer {
7575
);
7676
}
7777

78-
sendTransaction(socket: Websocket, transaction: Transaction): void {
78+
public sendTransaction(socket: Websocket, transaction: Transaction): void {
7979
socket.send(
8080
JSON.stringify({
8181
type: MESSAGE_TYPES.transaction,
@@ -84,19 +84,19 @@ export default class P2pServer {
8484
);
8585
}
8686

87-
syncChains(): void {
87+
public syncChains(): void {
8888
this.sockets.forEach(socket => this.sendChain(socket));
8989
}
9090

91-
broadcastTransaction(transaction: Transaction): void {
91+
public broadcastTransaction(transaction: Transaction): void {
9292
this.sockets.forEach(socket => this.sendTransaction(socket, transaction));
9393
}
9494

95-
broadcastClearTransactions(): void {
95+
public broadcastClearTransactions(): void {
9696
this.sockets.forEach(socket =>
9797
socket.send(
9898
JSON.stringify({
99-
type: MESSAGE_TYPES.clear_transactions,
99+
type: MESSAGE_TYPES.clearTransactions,
100100
}),
101101
),
102102
);

src/blockchain/block.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@ import { DIFFICULTY, MINE_RATE } from '../config';
33
import Transaction from '../wallet/transaction';
44

55
export default class Block {
6-
timestamp: number;
7-
lastHash: string;
8-
hash: string;
9-
data: Transaction[];
10-
nonce: number;
11-
difficulty: number;
6+
public timestamp: number;
7+
public lastHash: string;
8+
public hash: string;
9+
public data: Transaction[];
10+
public nonce: number;
11+
public difficulty: number;
1212

13-
constructor(
13+
public constructor(
1414
timestamp: number,
1515
lastHash: string,
1616
hash: string,
@@ -26,7 +26,7 @@ export default class Block {
2626
this.difficulty = difficulty || DIFFICULTY;
2727
}
2828

29-
toString(): string {
29+
public toString(): string {
3030
return `Block -
3131
Timestamp : ${this.timestamp}
3232
Last Hash : ${this.lastHash.substring(0, 10)}
@@ -36,13 +36,13 @@ export default class Block {
3636
Data : ${this.data}`;
3737
}
3838

39-
static genesis(): Block {
39+
public static genesis(): Block {
4040
// TODO: Replace '0' by 'Genesis time'
4141
return new this(0, '-----', 'f1r57-h45h', [], 0, DIFFICULTY);
4242
// return new this('Genesis time', '-----', 'f1r57-h45h', [], 0, DIFFICULTY);
4343
}
4444

45-
static mineBlock(lastBlock: Block, data: any): Block {
45+
public static mineBlock(lastBlock: Block, data: any): Block {
4646
let hash;
4747
let timestamp;
4848
const lastHash = lastBlock.hash;
@@ -59,16 +59,16 @@ export default class Block {
5959
return new this(timestamp, lastHash, hash, data, nonce, difficulty);
6060
}
6161

62-
static hash(timestamp: number, lastHash: string, data: any, nonce: number, difficulty: number): string {
62+
public static hash(timestamp: number, lastHash: string, data: any, nonce: number, difficulty: number): string {
6363
return ChainUtil.hash(`${timestamp}${lastHash}${data}${nonce}${difficulty}`).toString();
6464
}
6565

66-
static blockHash(block: Block): string {
66+
public static blockHash(block: Block): string {
6767
const { timestamp, lastHash, data, nonce, difficulty } = block;
6868
return Block.hash(timestamp, lastHash, data, nonce, difficulty);
6969
}
7070

71-
static adjustDifficulty(lastBlock: Block, currentTime: number): number {
71+
public static adjustDifficulty(lastBlock: Block, currentTime: number): number {
7272
let { difficulty } = lastBlock;
7373
difficulty = lastBlock.timestamp + MINE_RATE > currentTime ? difficulty + 1 : difficulty - 1;
7474
return difficulty;

src/blockchain/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
import Block from './block';
22

33
export default class Blockchain {
4-
chain: Block[];
4+
public chain: Block[];
55

6-
constructor() {
6+
public constructor() {
77
this.chain = [Block.genesis()];
88
}
99

10-
addBlock(data: any): Block {
10+
public addBlock(data: any): Block {
1111
const block = Block.mineBlock(this.getLastBlock(), data);
1212
this.chain.push(block);
1313

1414
return block;
1515
}
1616

17-
getLastBlock(): Block {
17+
public getLastBlock(): Block {
1818
return this.chain[this.chain.length - 1];
1919
}
2020

21-
isValidChain(chain: Block[]): boolean {
21+
public isValidChain(chain: Block[]): boolean {
2222
if (JSON.stringify(chain[0]) !== JSON.stringify(Block.genesis())) {
2323
return false;
2424
}
@@ -35,7 +35,7 @@ export default class Blockchain {
3535
return true;
3636
}
3737

38-
replaceChain(newChain: Block[]): void {
38+
public replaceChain(newChain: Block[]): void {
3939
if (newChain.length <= this.chain.length) {
4040
console.log('Received chain is not longer than the current chain.');
4141
return;

src/chain-util.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@ import uuidV1 from 'uuid/v1';
55
const ecInstance = new ec('secp256k1');
66

77
export default class ChainUtil {
8-
static genKeyPair(): ec.KeyPair {
8+
public static genKeyPair(): ec.KeyPair {
99
return ecInstance.genKeyPair();
1010
}
1111

12-
static id(): string {
12+
public static id(): string {
1313
return uuidV1();
1414
}
1515

16-
static hash(data: any): string {
16+
public static hash(data: any): string {
1717
return SHA256(JSON.stringify(data)).toString();
1818
}
1919

20-
static verifySignature(publicKey: string, signature: ec.Signature, dataHash: string): boolean {
20+
public static verifySignature(publicKey: string, signature: ec.Signature, dataHash: string): boolean {
2121
return ecInstance.keyFromPublic(publicKey, 'hex').verify(dataHash, signature);
2222
}
2323
}

src/config.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export const DIFFICULTY: number = 4;
2-
export const MINE_RATE: number = 3000;
3-
export const INITIAL_BALANCE: number = 500;
4-
export const MINING_REWARD: number = 66;
1+
export const DIFFICULTY = 4;
2+
export const MINE_RATE = 3000;
3+
export const INITIAL_BALANCE = 500;
4+
export const MINING_REWARD = 66;

src/errors.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
export class InvalidTransactionException extends Error {
2-
address: string;
2+
public address: string;
33

4-
constructor(address: string) {
4+
public constructor(address: string) {
55
const message = `Invalid transaction from ${address}.`;
66

77
super(message);
@@ -12,9 +12,9 @@ export class InvalidTransactionException extends Error {
1212
}
1313

1414
export class InvalidSignatureException extends Error {
15-
address: string;
15+
public address: string;
1616

17-
constructor(address: string) {
17+
public constructor(address: string) {
1818
const message = `Invalid signature from ${address}.`;
1919

2020
super(message);
@@ -25,10 +25,10 @@ export class InvalidSignatureException extends Error {
2525
}
2626

2727
export class AmountExceedBalanceException extends Error {
28-
amount: number;
29-
balance?: number;
28+
public amount: number;
29+
public balance?: number;
3030

31-
constructor(amount: number, balance?: number) {
31+
public constructor(amount: number, balance?: number) {
3232
const message =
3333
balance === undefined
3434
? `Amount: ${amount} exceeds balance.`

src/wallet/index.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,29 @@ import Transaction from './transaction';
88
import TransactionPool from './transaction-pool';
99

1010
export default class Wallet {
11-
balance: number;
12-
keyPair: ec.KeyPair;
13-
publicKey: string;
14-
address: string;
11+
public balance: number;
12+
public keyPair: ec.KeyPair;
13+
public publicKey: string;
14+
public address: string;
1515

16-
constructor() {
16+
public constructor() {
1717
this.balance = INITIAL_BALANCE;
1818
this.keyPair = ChainUtil.genKeyPair();
1919
this.publicKey = this.keyPair.getPublic().encode('hex');
2020
this.address = '';
2121
}
2222

23-
toString(): string {
23+
public toString(): string {
2424
return `Wallet -
2525
publicKey: ${this.publicKey.toString()}
2626
balance : ${this.balance}`;
2727
}
2828

29-
sign(dataHash: any): ec.Signature {
29+
public sign(dataHash: any): ec.Signature {
3030
return this.keyPair.sign(dataHash);
3131
}
3232

33-
createTransaction(
33+
public createTransaction(
3434
recipient: string,
3535
amount: number,
3636
blockchain: Blockchain,
@@ -54,7 +54,7 @@ export default class Wallet {
5454
return transaction;
5555
}
5656

57-
calculateBalance(blockchain: Blockchain): number {
57+
public calculateBalance(blockchain: Blockchain): number {
5858
let balance = this.balance;
5959
let transactions: Transaction[] = [];
6060
blockchain.chain.forEach(block =>
@@ -76,11 +76,6 @@ export default class Wallet {
7676
startTime = recentInput.input.timestamp;
7777
}
7878

79-
// transactions
80-
// .filter((transaction) => transaction.input.timestamp > startTime)
81-
// .filter((transaction) => output.address === this.publicKey)
82-
// .forEach((transaction)=> {balance += transaction})
83-
8479
transactions.forEach(transaction => {
8580
if (transaction.input.timestamp > startTime) {
8681
transaction.outputs.forEach(output => {
@@ -94,7 +89,7 @@ export default class Wallet {
9489
return balance;
9590
}
9691

97-
static blockchainWallet(): Wallet {
92+
public static blockchainWallet(): Wallet {
9893
const blockchainWallet = new this();
9994
blockchainWallet.address = 'blockchain-wallet';
10095
return blockchainWallet;

0 commit comments

Comments
 (0)