Skip to content

Commit 3264052

Browse files
Merge pull request #80 from nguyenphuminh/genesis-fix
Genesis fix
2 parents c9c9e46 + 54a55f5 commit 3264052

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

config.json

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
{
2-
"PORT": 5000,
3-
"MY_ADDRESS": "ws://localhost:5000",
2+
"PORT": 10507,
3+
"MY_ADDRESS": "ws://localhost:10507",
44

5-
"RPC_PORT": 3000,
5+
"RPC_PORT": 20297,
66

77
"PEERS": [],
88
"MAX_PEERS": 10,
99

1010
"PRIVATE_KEY": "",
1111

12-
"ENABLE_MINING": false,
13-
"ENABLE_LOGGING": false,
12+
"ENABLE_MINING": true,
13+
"ENABLE_LOGGING": true,
1414
"ENABLE_RPC": true,
1515
"ENABLE_CHAIN_REQUEST": false
1616
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "jechain",
3-
"version": "0.28.0",
3+
"version": "0.28.1",
44
"description": "Node for JeChain - an experimental smart contract blockchain network",
55
"main": "./index.js",
66
"scripts": {

src/node/server.js

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ async function startServer(options) {
5555
const ENABLE_LOGGING = options.ENABLE_LOGGING ? true : false; // Enable logging?
5656
const ENABLE_RPC = options.ENABLE_RPC ? true : false; // Enable RPC server?
5757
let ENABLE_CHAIN_REQUEST = options.ENABLE_CHAIN_REQUEST ? true : false; // Enable chain sync request?
58+
const GENESIS_HASH = options.GENESIS_HASH || ""; // Genesis block's hash
5859

5960
const privateKey = options.PRIVATE_KEY || ec.genKeyPair().getPrivate("hex");
6061
const keyPair = ec.keyFromPrivate(privateKey, "hex");
@@ -222,10 +223,16 @@ async function startServer(options) {
222223

223224
if (ENABLE_CHAIN_REQUEST && block.blockNumber === currentSyncBlock) {
224225
const verificationHandler = async function(block) {
226+
// If latest synced block is null, we immediately add the block into the chain without verification.
227+
// This happens due to the fact that the genesis block can discard every possible set rule ¯\_(ツ)_/¯
228+
229+
// But wait, isn't that unsafe? Well, this is because we don't have an official JeChain "network" yet.
230+
// But if there is, one can generate the first genesis block and we can add its hash into config,
231+
// we then check if the genesis block matches with the hash which is safe.
232+
225233
if (
226-
chainInfo.latestSyncBlock === null // If latest synced block is null, we immediately add the block into the chain without verification.
227-
|| // This happens due to the fact that the genesis block can discard every possible set rule ¯\_(ツ)_/¯
228-
await verifyBlock(block, chainInfo, stateDB, codeDB, ENABLE_LOGGING)
234+
(chainInfo.latestSyncBlock === null && (!GENESIS_HASH || GENESIS_HASH === block.hash)) || // For genesis
235+
await verifyBlock(block, chainInfo, stateDB, codeDB, ENABLE_LOGGING) // For all others
229236
) {
230237
await blockDB.put(block.blockNumber.toString(), Buffer.from(_message.data)); // Add block to chain
231238
await bhashDB.put(block.hash, numToBuffer(block.blockNumber)); // Assign block number to the matching block hash
@@ -291,6 +298,16 @@ async function startServer(options) {
291298

292299
await blockDB.put(chainInfo.latestBlock.blockNumber.toString(), Buffer.from(Block.serialize(chainInfo.latestBlock)));
293300
await bhashDB.put(chainInfo.latestBlock.hash, numToBuffer(chainInfo.latestBlock.blockNumber)); // Assign block number to the matching block hash
301+
302+
console.log(
303+
`\x1b[32mLOG\x1b[0m [${(new Date()).toISOString()}] Created Genesis Block with:\n` +
304+
` Block number: ${chainInfo.latestBlock.blockNumber.toString()}\n` +
305+
` Timestamp: ${chainInfo.latestBlock.timestamp.toString()}\n` +
306+
` Difficulty: ${chainInfo.latestBlock.difficulty.toString()}\n` +
307+
` Coinbase: ${chainInfo.latestBlock.coinbase.toString()}\n` +
308+
` Hash: ${chainInfo.latestBlock.hash.toString()}\n` +
309+
` TxRoot: ${chainInfo.latestBlock.txRoot.toString()}`
310+
);
294311

295312
await changeState(chainInfo.latestBlock, stateDB, codeDB);
296313
} else {

0 commit comments

Comments
 (0)