Skip to content

Commit

Permalink
Small comments improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
conradoqg committed Apr 30, 2017
1 parent bea8875 commit d087aef
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 25 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -207,7 +207,7 @@ A transaction contains a list of inputs and outputs representing a transfer of c

##### Operator

The operator handles wallet and addresses as well the transaction creation. Most of its operation are CRUD related.
The operator handles wallet and addresses as well the transaction creation. Most of its operation are CRUD related. Each node has its list of wallets and addresses, meaning that they aren't synchronized between nodes.

###### Wallet structure

Expand Down
2 changes: 1 addition & 1 deletion lib/blockchain/block.js
Expand Up @@ -30,7 +30,7 @@ class Block {
}

getDifficulty() {
// 14 is the maximum precision lenght supported by javascript
// 14 is the maximum precision length supported by javascript
return parseInt(this.hash.substring(0, 14), 16);
}

Expand Down
16 changes: 8 additions & 8 deletions lib/blockchain/index.js
Expand Up @@ -25,7 +25,7 @@ class Blockchain {
this.blocksDb = new Db('data/' + dbName + '/' + BLOCKCHAIN_FILE, new Blocks());
this.transactionsDb = new Db('data/' + dbName + '/' + TRANSACTIONS_FILE, new Transactions());

// INFO: In this implementation the database is a file so every time data is saved it rewrites the file, probably it should be a more robust database for performance reasons
// INFO: In this implementation the database is a file and every time data is saved it rewrites the file, probably it should be a more robust database for performance reasons
this.blocks = this.blocksDb.read(Blocks);
this.transactions = this.transactionsDb.read(Transactions);

Expand All @@ -42,7 +42,7 @@ class Blockchain {
this.blocksDb.write(this.blocks);
}

// Remove transactions that are in the chainblock
// Remove transactions that are in the blockchain
console.info('Removing transactions that are in the blockchain');
R.forEach(this.removeBlockTransactionsFromTransactions.bind(this), this.blocks);
}
Expand All @@ -52,11 +52,11 @@ class Blockchain {
}

getBlockByIndex(index) {
return R.find((block) => { return block.index == index; }, this.blocks);
return R.find(R.propEq('index', index), this.blocks);
}

getBlockByHash(hash) {
return R.find((block) => { return block.hash == hash; }, this.blocks);
return R.find(R.propEq('hash', hash), this.blocks);
}

getLastBlock() {
Expand All @@ -81,15 +81,15 @@ class Blockchain {
}

getTransactionById(id) {
return R.find((transaction) => { return transaction.id == id; }, this.transactions);
return R.find(R.propEq('id', id), this.transactions);
}

getTransactionFromBlocks(transactionId) {
return R.find(R.compose(R.find(R.propEq('id', transactionId)), R.prop('transactions')), this.blocks);
}

replaceChain(newBlockchain) {
// Doesn't make sense to replace this blockchain by a smaller one
// It doesn't make sense to replace this blockchain by a smaller one
if (newBlockchain.length <= this.blocks.length) {
console.error('Blockchain shorter than the current blockchain');
throw new BlockchainAssertionError('Blockchain shorter than the current blockchain');
Expand Down Expand Up @@ -117,7 +117,7 @@ class Blockchain {
throw new BlockchainAssertionError('Genesis block aren\'t the same');
}

// Compare every block to the previous one (it skip the first one, because it was verified before)
// Compare every block to the previous one (it skips the first one, because it was verified before)
try {
for (let i = 1; i < blockchainToValidate.length; i++) {
this.checkBlock(blockchainToValidate[i], blockchainToValidate[i - 1]);
Expand Down Expand Up @@ -196,7 +196,7 @@ class Blockchain {
throw new BlockAssertionError(`Invalid block balance: inputs sum '${sumOfInputsAmount}', outputs sum '${sumOfOutputsAmount}'`, { sumOfInputsAmount, sumOfOutputsAmount });
}

// Check if there is only 1 type of fee transaction and 1 type of reward transaction;
// Check if there is only 1 fee transaction and 1 reward transaction;
let transactionsByType = R.countBy(R.prop('type'), newBlock.transactions);
if (transactionsByType.fee && transactionsByType.fee > 1) {
console.error(`Invalid fee transaction count: expected '1' got '${transactionsByType.fee}'`);
Expand Down
9 changes: 5 additions & 4 deletions lib/miner/index.js
Expand Up @@ -42,7 +42,8 @@ class Miner {
// Get the first two avaliable transactions, if there aren't 2, it's empty
let transactions = R.defaultTo([], R.take(2, blockchainTransactions));

// Add fee transaction (1 satoshi per transaction), usually it is a fee over transaction size (not amount)
// Add fee transaction (1 satoshi per transaction)
// INFO: usually it's a fee over transaction size (not amount)
if (transactions.length > 0) {
let feeTransaction = Transaction.fromJson({
id: CryptoUtil.randomId(64),
Expand All @@ -53,7 +54,7 @@ class Miner {
outputs: [
{
amount: FEE_PER_TRANSACTION * transactions.length, // satoshis format
address: address, // Usually here is a locking script (to check who and when this transaction output can be used), in this case it's simple the destination address
address: address, // INFO: Usually here is a locking script (to check who and when this transaction output can be used), in this case it's a simple destination address
}
]
}
Expand All @@ -73,7 +74,7 @@ class Miner {
outputs: [
{
amount: MINING_REWARD, // satoshis format
address: address, // Usually here is a locking script (to check who and when this transaction output can be used), in this case it's simple the destination address
address: address, // INFO: Usually here is a locking script (to check who and when this transaction output can be used), in this case it's simple the destination address
}
]
}
Expand All @@ -96,7 +97,7 @@ class Miner {
let start = process.hrtime();
let block = Block.fromJson(jsonBlock);

// INFO: Every cryptocurrency as a different way to prove work, this is a simple hash sequence
// INFO: Every cryptocurrency has a different way to prove work, this is a simple hash sequence

// Loop incrementing the nonce to find the hash at desired difficulty
do {
Expand Down
8 changes: 4 additions & 4 deletions lib/node/index.js
Expand Up @@ -24,8 +24,8 @@ class Node {
this.broadcast(this.sendTransaction, newTransaction);
});

this.blockchain.emitter.on('blockchainReplaced', (block) => {
this.broadcast(this.sendLatestBlock, R.last(block));
this.blockchain.emitter.on('blockchainReplaced', (blocks) => {
this.broadcast(this.sendLatestBlock, R.last(blocks));
});
}

Expand Down Expand Up @@ -53,7 +53,7 @@ class Node {
}

initConnection(peer) {
// Initially it gets the latest block and all pending transactions
// It initially gets the latest block and all pending transactions
this.getLatestBlock(peer);
this.getTransactions(peer);
}
Expand Down Expand Up @@ -150,7 +150,7 @@ class Node {
}

getConfirmations(transactionId) {
// Get in all peers if the transaction has been confirmed
// Get from all peers if the transaction has been confirmed
let foundLocally = this.blockchain.getTransactionFromBlocks(transactionId) != null ? true : false;
return Promise.all(R.map((peer) => {
return this.getConfirmation(peer, transactionId);
Expand Down
2 changes: 1 addition & 1 deletion lib/operator/index.js
Expand Up @@ -12,7 +12,7 @@ class Operator {
constructor(dbName, blockchain) {
this.db = new Db('data/' + dbName + '/' + OPERATOR_FILE, new Wallets());

// INFO: In this implementation the database is a file so every time data is saved it rewrites the file, probably it should be a more robust database for performance reasons
// INFO: In this implementation the database is a file and every time data is saved it rewrites the file, probably it should be a more robust database for performance reasons
this.wallets = this.db.read(Wallets);
this.blockchain = blockchain;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/operator/wallet.js
Expand Up @@ -11,7 +11,7 @@ class Wallet {
}

generateAddress() {
// If secret is null it means is a brand new wallet
// If secret is null means it is a brand new wallet
if (this.secret == null) {
this.generateSecret();
}
Expand Down
9 changes: 4 additions & 5 deletions test/integrationTest.js
Expand Up @@ -7,14 +7,13 @@ const Miner = require('../lib/miner');
const Node = require('../lib/node');
const fs = require('fs-extra');

const name = 'integrationTest';
require('../lib/util/consoleWrapper.js')(name, 0);

describe('HTTP server', () => {
it('should create wallet, address, mine, create transaction and mine again', () => {
const name = 'integrationTest';

fs.removeSync('data/' + name + '/');

require('../lib/util/consoleWrapper.js')(name, 0);

fs.removeSync('data/' + name + '/');

let blockchain = new Blockchain(name);
let operator = new Operator(name, blockchain);
Expand Down

0 comments on commit d087aef

Please sign in to comment.