Skip to content

Commit

Permalink
Merge pull request #57 from JSKitty/master
Browse files Browse the repository at this point in the history
Fixed negative transactions
  • Loading branch information
conradoqg authored May 26, 2019
2 parents 9116c57 + bee4d10 commit 1aac1e6
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
17 changes: 16 additions & 1 deletion lib/blockchain/transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,17 @@ class Transaction {
// Check if the sum of input transactions are greater than output transactions, it needs to leave some room for the transaction fee
let sumOfInputsAmount = R.sum(R.map(R.prop('amount'), this.data.inputs));
let sumOfOutputsAmount = R.sum(R.map(R.prop('amount'), this.data.outputs));

let negativeOutputsFound = 0;
let i = 0;
let outputsLen = this.data.outputs.length;

// Check for negative outputs
for (i = 0; i < outputsLen; i++) {
if (this.data.outputs[i].amount < 0) {
negativeOutputsFound++;
}
}

let isInputsAmountGreaterOrEqualThanOutputsAmount = R.gte(sumOfInputsAmount, sumOfOutputsAmount);

Expand All @@ -93,6 +104,10 @@ class Transaction {
console.error(`Not enough fee: expected '${Config.FEE_PER_TRANSACTION}' got '${(sumOfInputsAmount - sumOfOutputsAmount)}'`);
throw new TransactionAssertionError(`Not enough fee: expected '${Config.FEE_PER_TRANSACTION}' got '${(sumOfInputsAmount - sumOfOutputsAmount)}'`, { sumOfInputsAmount, sumOfOutputsAmount, FEE_PER_TRANSACTION: Config.FEE_PER_TRANSACTION });
}
if (negativeOutputsFound > 0) {
console.error(`Transaction is either empty or negative, output(s) caught: '${negativeOutputsFound}'`);
throw new TransactionAssertionError(`Transaction is either empty or negative, output(s) caught: '${negativeOutputsFound}'`);
}
}

return true;
Expand All @@ -106,4 +121,4 @@ class Transaction {
}
}

module.exports = Transaction;
module.exports = Transaction;
21 changes: 18 additions & 3 deletions lib/miner/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ class Miner {
let rejectedTransactions = [];
let selectedTransactions = [];
R.forEach((transaction) => {
let negativeOutputsFound = 0;
let i = 0;
let outputsLen = transaction.data.outputs.length;

// Check for negative outputs (avoiding negative transactions or 'stealing')
for (i = 0; i < outputsLen; i++) {
if (transaction.data.outputs[i].amount < 0) {
negativeOutputsFound++;
}
}
// Check if any of the inputs is found in the selectedTransactions or in the blockchain
let transactionInputFoundAnywhere = R.map((input) => {
let findInputTransactionInTransactionList = R.find(
Expand All @@ -82,9 +92,14 @@ class Miner {
return wasItFoundInSelectedTransactions || wasItFoundInBlocks;
}, transaction.data.inputs);

// If no input was found, add the transaction to the transaction list to be mined
if (R.all(R.equals(false), transactionInputFoundAnywhere)) {
selectedTransactions.push(transaction);
if (transaction.type === 'regular' && negativeOutputsFound === 0) {
selectedTransactions.push(transaction);
} else if (transaction.type === 'reward') {
selectedTransactions.push(transaction);
} else if (negativeOutputsFound > 0) {
rejectedTransactions.push(transaction);
}
} else {
rejectedTransactions.push(transaction);
}
Expand Down Expand Up @@ -164,4 +179,4 @@ class Miner {
}
}

module.exports = Miner;
module.exports = Miner;

0 comments on commit 1aac1e6

Please sign in to comment.