reorg code, integration publish/subscribe working, still some TODOs

This commit is contained in:
Josip Milovac 2023-02-01 13:01:51 +11:00
parent 043a95d9ef
commit 1af6d56e2d
35 changed files with 3052 additions and 1401 deletions

View file

@ -1,91 +0,0 @@
const Wallet = require('./index');
const TransactionPool = require('./transaction-pool');
const Blockchain = require('../blockchain');
const { INITIAL_BALANCE } = require('../config');
describe('Wallet', () => {
let wallet, tp, bc;
beforeEach(() => {
wallet = new Wallet();
tp = new TransactionPool();
bc = new Blockchain();
});
describe('creating a transaction', () => {
let transaction, sendAmount, recipient;
beforeEach(() => {
sendAmount = 50;
recipient = 'r4nd0m-4ddr355';
transaction = wallet.createTransaction(recipient, sendAmount, bc);
tp.updateOrAddTransaction(transaction);
});
describe('and doing the same transaction', () => {
beforeEach(() => {
transaction = wallet.createTransaction(recipient, sendAmount, bc);
tp.updateOrAddTransaction(transaction);
});
it('doubles the `sendAmount` subtracted from the wallet balance', () => {
expect(transaction.outputs.find(output => output.address === wallet.publicKey).amount)
.toEqual(wallet.balance - sendAmount * 2);
});
it('clones the `sendAmount` output for the recipient', () => {
expect(transaction.outputs.filter(output => output.address === recipient)
.map(output => output.amount)).toEqual([sendAmount, sendAmount]);
});
});
});
describe('calculating a balance', () => {
let addBalance, repeatAdd, senderWallet;
beforeEach(() => {
senderWallet = new Wallet();
addBalance = 100;
repeatAdd = 3;
for (let i=0; i<repeatAdd; i++) {
const transaction = senderWallet.createTransaction(wallet.publicKey, addBalance, bc);
tp.updateOrAddTransaction(transaction);
}
bc.addBlock(tp.transactions);
});
it('calculates the balance for blockchain transactions matching the recipient', () => {
expect(wallet.calculateBalance(bc)).toEqual(INITIAL_BALANCE + (addBalance * repeatAdd));
});
it('calculates the balance for blockchain transactions matching the sender', () => {
expect(senderWallet.calculateBalance(bc)).toEqual(INITIAL_BALANCE - (addBalance * repeatAdd));
});
describe('and the recipient conducts a transaction', () => {
let subtractBalance, recipientBalance;
beforeEach(() => {
tp.clear();
subtractBalance = 60;
recipientBalance = wallet.calculateBalance(bc);
const transaction = wallet.createTransaction(senderWallet.publicKey, subtractBalance, bc);
tp.updateOrAddTransaction(transaction);
bc.addBlock(tp.transactions);
});
describe('and the sender sends another transaction to the recipient', () => {
beforeEach(() => {
tp.clear();
const transaction = senderWallet.createTransaction(wallet.publicKey, addBalance, bc);
tp.updateOrAddTransaction(transaction);
bc.addBlock(tp.transactions);
});
it('calculate the recipient balance only using transactions since its most recent one', () => {
expect(wallet.calculateBalance(bc)).toEqual(recipientBalance - subtractBalance + addBalance);
});
});
});
});
});