Skip to content

Commit

Permalink
Merge pull request #74 from nguyenphuminh/colorful-colors
Browse files Browse the repository at this point in the history
Add colors and fixed logs
  • Loading branch information
nguyenphuminh authored Jul 26, 2023
2 parents 62663bc + e5782d4 commit 112ce18
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "jechain",
"version": "0.25.1",
"version": "0.25.2",
"description": "Node for JeChain - an experimental smart contract blockchain network",
"main": "./index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/core/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ async function jelscript(input, originalState = {}, gas, stateDB, block, txInfo,
break;

case "log": // Log out data
if (enableLogging) console.log(`LOG [${(new Date()).toISOString()}]`, contractInfo.address + ":", c);
if (enableLogging) console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}]`, contractInfo.address + ":", c);

break;

Expand Down
8 changes: 4 additions & 4 deletions src/core/txPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async function addTransaction(transaction, chainInfo, stateDB) {
await Transaction.isValid(transaction, stateDB)) ||
BigInt(transaction.additionalData.contractGas || 0) > BigInt(BLOCK_GAS_LIMIT)
) {
console.log(`LOG [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
return;
}

Expand All @@ -30,7 +30,7 @@ async function addTransaction(transaction, chainInfo, stateDB) {
const txSenderAddress = SHA256(txSenderPubkey);

if (!(await stateDB.keys().all()).includes(txSenderAddress)) {
console.log(`LOG [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
return;
}

Expand All @@ -47,13 +47,13 @@ async function addTransaction(transaction, chainInfo, stateDB) {
}

if (maxNonce + 1 !== transaction.nonce) {
console.log(`LOG [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Failed to add one transaction to pool.`);
return;
}

txPool.push(transaction);

console.log(`LOG [${(new Date()).toISOString()}] Added one transaction to pool.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Added one transaction to pool.`);
}

async function clearDepreciatedTxns(chainInfo, stateDB) {
Expand Down
24 changes: 12 additions & 12 deletions src/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@ async function startServer(options) {
const keyPair = ec.keyFromPrivate(privateKey, "hex");
const publicKey = keyPair.getPublic("hex");

process.on("uncaughtException", err => console.log(`LOG [${(new Date()).toISOString()}]`, err));
process.on("uncaughtException", err => console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Uncaught Exception`, err));

await codeDB.put(EMPTY_HASH, "");

const server = new WS.Server({ port: PORT });

console.log(`LOG [${(new Date()).toISOString()}] Listening on PORT`, PORT.toString());
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] P2P server listening on PORT`, PORT.toString());

server.on("connection", async (socket, req) => {
// Message handler
Expand Down Expand Up @@ -103,7 +103,7 @@ async function startServer(options) {
chainInfo.checkedBlock[newBlock.hash] = true;

if (await verifyBlock(newBlock, chainInfo, stateDB, codeDB, ENABLE_LOGGING)) {
console.log(`LOG [${(new Date()).toISOString()}] New block received.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] New block received.`);

// If mining is enabled, we will set mined to true, informing that another node has mined before us.
if (ENABLE_MINING) {
Expand All @@ -124,7 +124,7 @@ async function startServer(options) {
// Update the new transaction pool (remove all the transactions that are no longer valid).
chainInfo.transactionPool = await clearDepreciatedTxns(chainInfo, stateDB);

console.log(`LOG [${(new Date()).toISOString()}] Block #${newBlock.blockNumber} synced, state transited.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Block #${newBlock.blockNumber} synced, state transited.`);

sendMessage(message, opened); // Broadcast block to other nodes

Expand Down Expand Up @@ -179,7 +179,7 @@ async function startServer(options) {

if (maxNonce + 1 !== transaction.nonce) return;

console.log(`LOG [${(new Date()).toISOString()}] New transaction received, broadcasted and added to pool.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] New transaction received, broadcasted and added to pool.`);

chainInfo.transactionPool.push(transaction);

Expand All @@ -201,7 +201,7 @@ async function startServer(options) {

socket.send(produceMessage(TYPE.SEND_BLOCK, block)); // Send block

console.log(`LOG [${(new Date()).toISOString()}] Sent block at position ${blockNumber} to ${requestAddress}.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Sent block at position ${blockNumber} to ${requestAddress}.`);
}
}

Expand Down Expand Up @@ -239,7 +239,7 @@ async function startServer(options) {

await updateDifficulty(block, chainInfo, blockDB); // Update difficulty.

console.log(`LOG [${(new Date()).toISOString()}] Synced block at position ${block.blockNumber}.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Synced block at position ${block.blockNumber}.`);

// Continue requesting the next block
for (const node of opened) {
Expand Down Expand Up @@ -345,14 +345,14 @@ function connect(MY_ADDRESS, address) {

connectedNodes++;

console.log(`LOG [${(new Date()).toISOString()}] Connected to ${address}.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Connected to ${address}.`);

// Listen for disconnection, will remove them from "opened" and "connected".
socket.on("close", () => {
opened.splice(connected.indexOf(address), 1);
connected.splice(connected.indexOf(address), 1);

console.log(`LOG [${(new Date()).toISOString()}] Disconnected from ${address}.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Disconnected from ${address}.`);
});
}
});
Expand All @@ -365,7 +365,7 @@ function connect(MY_ADDRESS, address) {
async function sendTransaction(transaction) {
sendMessage(produceMessage(TYPE.CREATE_TRANSACTION, transaction), opened);

console.log(`LOG [${(new Date()).toISOString()}] Sent one transaction.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Sent one transaction.`);

await addTransaction(transaction, chainInfo, stateDB);
}
Expand Down Expand Up @@ -537,7 +537,7 @@ async function mine(publicKey, ENABLE_LOGGING) {

sendMessage(produceMessage(TYPE.NEW_BLOCK, Block.serialize(chainInfo.latestBlock)), opened); // Broadcast the new block

console.log(`LOG [${(new Date()).toISOString()}] Block #${chainInfo.latestBlock.blockNumber} mined and synced, state transited.`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Block #${chainInfo.latestBlock.blockNumber} mined and synced, state transited.`);
} else {
mined = false;
}
Expand All @@ -547,7 +547,7 @@ async function mine(publicKey, ENABLE_LOGGING) {

worker = fork(`${__dirname}/../miner/worker.js`);
})
.catch(err => console.log(err));
.catch(err => console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Error at mining child process`, err));
}

// Function to mine continuously
Expand Down
6 changes: 3 additions & 3 deletions src/rpc/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const fastify = require("fastify")();

function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashDB, codeDB) {

process.on("uncaughtException", err => console.log(`LOG [${(new Date()).toISOString()}]`, err));
process.on("uncaughtException", err => console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Uncaught Exception`, err));

fastify.get("/:option", async (req, reply) => {

Expand Down Expand Up @@ -327,11 +327,11 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD

fastify.listen(PORT, (err, address) => {
if (err) {
console.log(`LOG [${(new Date()).toISOString()}] Error at RPC server: Fastify: `, err);
console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Error at RPC server: Fastify: `, err);
process.exit(1);
}

console.log(`LOG [${(new Date()).toISOString()}] RPC server running on PORT ${PORT}`);
console.log(`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] RPC server listening on PORT ${PORT}`);
});
}

Expand Down

0 comments on commit 112ce18

Please sign in to comment.