@@ -55,6 +55,7 @@ async function startServer(options) {
55
55
const ENABLE_LOGGING = options . ENABLE_LOGGING ? true : false ; // Enable logging?
56
56
const ENABLE_RPC = options . ENABLE_RPC ? true : false ; // Enable RPC server?
57
57
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
58
59
59
60
const privateKey = options . PRIVATE_KEY || ec . genKeyPair ( ) . getPrivate ( "hex" ) ;
60
61
const keyPair = ec . keyFromPrivate ( privateKey , "hex" ) ;
@@ -222,10 +223,16 @@ async function startServer(options) {
222
223
223
224
if ( ENABLE_CHAIN_REQUEST && block . blockNumber === currentSyncBlock ) {
224
225
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
+
225
233
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
229
236
) {
230
237
await blockDB . put ( block . blockNumber . toString ( ) , Buffer . from ( _message . data ) ) ; // Add block to chain
231
238
await bhashDB . put ( block . hash , numToBuffer ( block . blockNumber ) ) ; // Assign block number to the matching block hash
@@ -291,6 +298,16 @@ async function startServer(options) {
291
298
292
299
await blockDB . put ( chainInfo . latestBlock . blockNumber . toString ( ) , Buffer . from ( Block . serialize ( chainInfo . latestBlock ) ) ) ;
293
300
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
+ ) ;
294
311
295
312
await changeState ( chainInfo . latestBlock , stateDB , codeDB ) ;
296
313
} else {
0 commit comments