Skip to content

Commit

Permalink
Merge pull request #38 from nguyenphuminh/patch-and-rewrite
Browse files Browse the repository at this point in the history
Patch and rewrite
  • Loading branch information
nguyenphuminh authored Jul 27, 2022
2 parents f9ab8a4 + 8d646e9 commit 50596de
Show file tree
Hide file tree
Showing 5 changed files with 211 additions and 250 deletions.
3 changes: 1 addition & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
"PRIVATE_KEY": "",
"ENABLE_MINING": false,
"ENABLE_LOGGING": false,
"ENABLE_RPC": false,
"SYNC_FROM": "",
"ENABLE_RPC": true,
"ENABLE_CHAIN_REQUEST": false
}
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.15.0",
"version": "0.16.0",
"description": "Node for JeChain - an experimental smart contract blockchain network",
"main": "./index.js",
"scripts": {
Expand Down
46 changes: 46 additions & 0 deletions src/consensus/consensus.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
const Block = require("../core/block");
const { log16 } = require("../utils/utils");
const generateMerkleRoot = require("../core/merkle");
const { BLOCK_REWARD, BLOCK_TIME } = require("../config.json");

async function verifyBlock(newBlock, chainInfo, stateDB) {
// Check if the block is valid or not, if yes, we will push it to the chain, update the difficulty, chain state and the transaction pool.

// A block is valid under these factors:
// - The hash of this block is equal to the hash re-generated according to the block's info.
// - The block is mined (the hash starts with (4+difficulty) amount of zeros).
// - Transactions in the block are valid.
// - Block's timestamp is not greater than the current timestamp and is not lower than the previous block's timestamp.
// - Block's parentHash is equal to latest block's hash
// - The new difficulty can only be greater than 1 or lower than 1 compared to the old difficulty.

return (
SHA256(
newBlock.blockNumber.toString() +
newBlock.timestamp.toString() +
newBlock.txRoot +
newBlock.difficulty.toString() +
chainInfo.latestBlock.hash +
newBlock.nonce.toString()
) === newBlock.hash &&
newBlock.hash.startsWith("00000" + Array(Math.floor(log16(chainInfo.difficulty)) + 1).join("0")) &&
await Block.hasValidTransactions(newBlock, stateDB) &&
newBlock.timestamp > chainInfo.latestBlock.timestamp &&
newBlock.timestamp < Date.now() &&
chainInfo.latestBlock.hash === newBlock.parentHash &&
newBlock.blockNumber - 1 === chainInfo.latestBlock.blockNumber &&
newBlock.difficulty === chainInfo.difficulty &&
generateMerkleRoot(newBlock.transactions) === newBlock.txRoot
)
}

async function updateDifficulty(newBlock, chainInfo, blockDB) {
if (newBlock.blockNumber % 100 === 0) {
const oldBlock = await blockDB.get((newBlock.blockNumber - 99).toString());

chainInfo.difficulty = Math.ceil(chainInfo.difficulty * 100 * BLOCK_TIME / (newBlock.timestamp - oldBlock.timestamp));
}
}

module.exports = { verifyBlock, updateDifficulty };
9 changes: 9 additions & 0 deletions src/node/message-types.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
const TYPE = {
NEW_BLOCK: 0,
CREATE_TRANSACTION: 1,
REQUEST_BLOCK: 2,
SEND_BLOCK: 3,
HANDSHAKE: 4
}

module.exports = TYPE;
Loading

0 comments on commit 50596de

Please sign in to comment.