Skip to content

Commit

Permalink
push
Browse files Browse the repository at this point in the history
  • Loading branch information
BroProgramerWeb3 committed Dec 23, 2023
1 parent 28cc162 commit 0a0f09c
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 51 deletions.
102 changes: 51 additions & 51 deletions blockchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,63 +29,63 @@ class Blockchain {
return;
}

if (validateTransactions && !this.validTransactionData({ chain })) {
console.error('The incoming chain has invalid data');
return;
}
// if (validateTransactions && !this.validTransactionData({ chain })) {
// console.error('The incoming chain has invalid data');
// return;
// }

if (onSuccess) onSuccess();
console.log('replacing chain with', chain);
this.chain = chain;
}

validTransactionData({ chain }) {
for (let i=1; i<chain.length; i++) {
const block = chain[i];
const transactionSet = new Set();
let rewardTransactionCount = 0;

for (let transaction of block.data) {
if (transaction.input.address === REWARD_INPUT.address) {
rewardTransactionCount += 1;

if (rewardTransactionCount > 1) {
console.error('Miner rewards exceeds limit');
return false;
}

if (Object.values(transaction.outputMap)[0] !== MINING_REWARD) {
console.error('Miner reward amount is invalid');
return false;
}
} else {
if (!Transaction.validTransaction(transaction)) {
console.error('Invalid transaction');
return false;
}

const trueBalance = Wallet.calculateBalance({
chain: this.chain,
address: transaction.input.address
});

if (transaction.input.amount !== trueBalance) {
console.error('Invalid input amount');
return false;
}

if (transactionSet.has(transaction)) {
console.error('An identical transaction appears more than once in the block');
return false;
} else {
transactionSet.add(transaction);
}
}
}
}

return true;
}
// validTransactionData({ chain }) {
// for (let i=1; i<chain.length; i++) {
// const block = chain[i];
// const transactionSet = new Set();
// let rewardTransactionCount = 0;

// for (let transaction of block.data) {
// if (transaction.input.address === REWARD_INPUT.address) {
// rewardTransactionCount += 1;

// if (rewardTransactionCount > 1) {
// console.error('Miner rewards exceeds limit');
// return false;
// }

// if (Object.values(transaction.outputMap)[0] !== MINING_REWARD) {
// console.error('Miner reward amount is invalid');
// return false;
// }
// } else {
// if (!Transaction.validTransaction(transaction)) {
// console.error('Invalid transaction');
// return false;
// }

// const trueBalance = Wallet.calculateBalance({
// chain: this.chain,
// address: transaction.input.address
// });

// if (transaction.input.amount !== trueBalance) {
// console.error('Invalid input amount');
// return false;
// }

// if (transactionSet.has(transaction)) {
// console.error('An identical transaction appears more than once in the block');
// return false;
// } else {
// transactionSet.add(transaction);
// }
// }
// }
// }

// return true;
// }

static isValidChain(chain) {
if(JSON.stringify(chain[0]) !== JSON.stringify(Block.genesis())) return false;
Expand Down
18 changes: 18 additions & 0 deletions wallet/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ class Wallet {

return new Transaction({ senderWallet: this, recipient, amount });
}

static calculateBalance({ chain, address }) {
let outputsTotal = 0;

for (let i=1; i<chain.length; i++) {
const block = chain[i];

for (let transaction of block.data) {
const addressOutput = transaction.outputMap[address];

if (addressOutput) {
outputsTotal = outputsTotal + addressOutput;
}
}
}

return STARTING_BALANCE + outputsTotal;
}
}

module.exports = Wallet;
70 changes: 70 additions & 0 deletions wallet/index.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const Wallet = require('./index');
const Transaction = require('./transaction');
const { verifySignature } = require('../util');
const Blockchain = require('../blockchain');
const {STARTING_BALANCE} = require('../config');

describe('Wallet', () => {
let wallet;
Expand Down Expand Up @@ -70,5 +72,73 @@ describe('Wallet', () => {
expect(transaction.outputMap[recipient]).toEqual(amount);
});
});

describe('and a chain is passed', () => {
it('calls `Wallet.calculateBalance`', () => {
const calculateBalanceMock = jest.fn();

Wallet.calculateBalance = calculateBalanceMock;

wallet.createTransaction({
recipient: 'foo',
amount: 10,
chain: new Blockchain().chain
});

expect(calculateBalanceMock).toHaveBeenCalled();
});
});
});



describe('calculateBalance()', () => {
let blockchain;

beforeEach(() => {
blockchain = new Blockchain();
})

describe('and there are no outputs for the wallet', () => {
it('returns the `STARTING_BALANCE`', () => {
expect(
Wallet.calculateBalance({
chain: blockchain.chain,
address: wallet.publicKey
})
).toEqual(STARTING_BALANCE);
});
});

describe('amd there are outputs for the wallet', () =>{
let transactionOne, transactionTwo;

beforeEach(() => {
transactionOne = new Wallet().createTransaction({
recipient: wallet.publickey,
amount:50
});

transactionTwo = new Wallet().createTransaction({
recipient: wallet.publickey,
amount:60
});

blockchain.addBlock({ data: [transactionOne, transactionTwo] });
});

it('adds the sum of all outputs to the wallet balance', () => {
expect(
Wallet.calculateBalance({
chain: blockchain.chain,
address: wallet.publickey
})
).toEqual(
STARTING_BALANCE +
transactionOne.outputMap[wallet.publickey]+
transactionTwo.outputMap[wallet.publickey]
);
});
});
});
});

0 comments on commit 0a0f09c

Please sign in to comment.