-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from nguyenphuminh/patch-and-rewrite
Patch and rewrite
- Loading branch information
Showing
5 changed files
with
211 additions
and
250 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
Oops, something went wrong.