Skip to content

Commit

Permalink
customize the frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
BroProgramerWeb3 committed Dec 30, 2023
1 parent 85bb70e commit 6980f8e
Show file tree
Hide file tree
Showing 20 changed files with 2,544 additions and 195 deletions.
117 changes: 95 additions & 22 deletions app/pubsub.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,84 @@
const redis = require('redis');
// const redis = require('redis');

// const CHANNELS = {
// TEST: 'TEST',
// BLOCKCHAIN: 'BLOCKCHAIN',
// TRANSACTION: 'TRANSACTION'
// };

// class PubSub {
// constructor({ blockchain, transactionPool }) {
// this.blockchain = blockchain;
// this.transactionPool = transactionPool;

// this.publisher = redis.createClient();
// this.subscriber = redis.createClient();
// this.init()
// }

// async init() {
// await this.publisher.connect()
// await this.subscriber.connect()

// this.subscribeToChannels();
// }

// handleMessage( message, channel) {
// console.log(`Message received. Channel: ${channel}. Message: ${message}`);

// const parsedMessage = JSON.parse(message);

// switch(channel) {
// case CHANNELS.BLOCKCHAIN:
// this.blockchain.replaceChain(parsedMessage, true, () => {
// this.transactionPool.clearBlockchainTransactions({
// chain: parsedMessage
// });
// });
// break;
// case CHANNELS.TRANSACTION:
// this.transactionPool.setTransaction(parsedMessage);
// break;
// default:
// return;
// }
// }

// subscribeToChannels() {
// Object.values(CHANNELS).forEach(channel => {
// this.subscriber.subscribe(channel, (message, channel) => {
// this.handleMessage(message, channel)});
// });
// }

// // publish({ message, channel}) {
// // this.subscriber.unsubscribe(channel, () => {
// // this.publisher.publish(channel, message, () => {
// // this.subscriber.subscribe(channel);
// // });
// // });
// // }
// publish({ channel, message}) {
// this.publisher.publish(channel, message);
// }
// broadcastChain() {
// this.publish({
// channel: CHANNELS.BLOCKCHAIN,
// message: JSON.stringify(this.blockchain.chain)
// });
// }

// broadcastTransaction(transaction) {
// this.publish({
// channel: CHANNELS.TRANSACTION,
// message: JSON.stringify(transaction)
// })
// }
// }

// module.exports = PubSub;

const redis = require('redis');

const CHANNELS = {
TEST: 'TEST',
Expand All @@ -13,17 +93,16 @@ class PubSub {

this.publisher = redis.createClient();
this.subscriber = redis.createClient();
this.init()
}

async init() {
await this.publisher.connect()
await this.subscriber.connect()
this.subscribeToChannels();

this.subscribeToChannels();
this.subscriber.on(
'message',
(channel, message) => this.handleMessage(channel, message)
);
}

handleMessage(message, channel) {
handleMessage(channel, message) {
console.log(`Message received. Channel: ${channel}. Message: ${message}`);

const parsedMessage = JSON.parse(message);
Expand All @@ -46,22 +125,18 @@ class PubSub {

subscribeToChannels() {
Object.values(CHANNELS).forEach(channel => {
this.subscriber.subscribe(channel, (message, channel) => {
this.handleMessage(message, channel)});
this.subscriber.subscribe(channel);
});
}

// publish({ message, channel}) {
// this.subscriber.unsubscribe(channel, () => {
// this.publisher.publish(channel, message, () => {
// this.subscriber.subscribe(channel);
// });
// });
// }
publish({ channel, message}) {
this.publisher.publish(channel, message);
this.subscriber.unsubscribe(channel, () => {
this.publisher.publish(channel, message, () => {
this.subscriber.subscribe(channel);
});
});
}

broadcastChain() {
this.publish({
channel: CHANNELS.BLOCKCHAIN,
Expand All @@ -77,6 +152,4 @@ class PubSub {
}
}

module.exports = PubSub;


module.exports = PubSub;
11 changes: 5 additions & 6 deletions blockchain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,17 @@ class Blockchain {
if (transaction.input.address === REWARD_INPUT.address) {
rewardTransactionCount += 1;

if (rewardTransactionCount > 1) {
console.error('Miner rewards exceed limit');
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)) {
} else {
if (!Transaction.validTransaction(transaction)) {
console.error('Invalid transaction');
return false;
}
Expand All @@ -70,7 +69,7 @@ class Blockchain {
address: transaction.input.address
});

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

0 comments on commit 6980f8e

Please sign in to comment.