Skip to content

Commit

Permalink
Merge pull request #89 from nguyenphuminh/minor-fix
Browse files Browse the repository at this point in the history
Minor fix
  • Loading branch information
nguyenphuminh authored Jul 10, 2024
2 parents 7f520df + 5ebb8fc commit 81cca05
Show file tree
Hide file tree
Showing 13 changed files with 32 additions and 38 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.30.1",
"version": "0.30.2",
"description": "Node for JeChain - an experimental smart contract blockchain network",
"main": "./index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/consensus/consensus.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
const Block = require("../core/block");
const { log16 } = require("../utils/utils");
Expand Down
2 changes: 1 addition & 1 deletion src/core/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class Block {
// Finalize state and contract storage into DB

for (const address in storage) {
const storageDB = new Level(__dirname + "/../../log/accountStore/" + address);
const storageDB = new Level("./log/accountStore/" + address);
const keys = Object.keys(storage[address]);

states[address].storageRoot = Merkle.buildTxTrie(keys.map(key => key + " " + storage[address][key]), false).root;
Expand Down
4 changes: 0 additions & 4 deletions src/core/genesis.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
"use strict";

const EC = require("elliptic").ec, ec = new EC("secp256k1");
const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");

const Block = require("./block");
const Transaction = require("./transaction");
const { FIRST_ACCOUNT } = require("../config.json");

function generateGenesisBlock() {
Expand Down
10 changes: 7 additions & 3 deletions src/core/runtime.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const { Level } = require('level');

const { bigIntable, isHex, deserializeState, serializeState } = require("../utils/utils");
const { bigIntable, isHex, deserializeState } = require("../utils/utils");
const Transaction = require("./transaction");

const { EMPTY_HASH } = require("../config.json");
Expand All @@ -21,7 +21,7 @@ async function jelscript(input, originalState = {}, gas, stateDB, block, txInfo,


// Get contract state and storage
const storageDB = new Level(__dirname + "/../../log/accountStore/" + contractInfo.address);
const storageDB = new Level("./log/accountStore/" + contractInfo.address);

for (const key of (await storageDB.keys().all())) {
storage[key] = await storageDB.get(key);
Expand Down Expand Up @@ -88,7 +88,11 @@ async function jelscript(input, originalState = {}, gas, stateDB, block, txInfo,
break;

case "mod": // Modulo
setMem(c, "0x" + (a % b).toString(16));
if (b === 0n) {
setMem(c, "0x0");
} else {
setMem(c, "0x" + (a % b).toString(16));
}

break;

Expand Down
3 changes: 1 addition & 2 deletions src/core/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

const { Level } = require('level');
const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
const EC = require("elliptic").ec, ec = new EC("secp256k1");

const Merkle = require("./merkle");
const jelscript = require("./runtime");
Expand Down Expand Up @@ -67,7 +66,7 @@ async function changeState(newBlock, stateDB, codeDB, enableLogging = false) { /

const [ newState, newStorage ] = await jelscript(await codeDB.get(dataFromRecipient.codeHash), {}, BigInt(tx.additionalData.contractGas || 0), stateDB, newBlock, tx, contractInfo, enableLogging);

const storageDB = new Level(__dirname + "/../../log/accountStore/" + tx.recipient);
const storageDB = new Level("./log/accountStore/" + tx.recipient);
const keys = Object.keys(newStorage);

newState[tx.recipient].storageRoot = Merkle.buildTxTrie(keys.map(key => key + " " + newStorage[key]), false).root;
Expand Down
6 changes: 2 additions & 4 deletions src/core/transaction.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
"use strict";

const BN = require("bn.js");
const { isNumber, deserializeState, serializeState } = require("../utils/utils");
const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
const EC = require("elliptic").ec, ec = new EC("secp256k1");

const { EMPTY_HASH, CONTRACT_FLAG } = require("../config.json");
const { serialize } = require("v8");
const { deserializeState } = require("../utils/utils");

class Transaction {
constructor(recipient = "", amount = "0", gas = "1000000000000", additionalData = {}, nonce = 0) {
Expand Down Expand Up @@ -172,7 +171,7 @@ class Transaction {

static async isValid(tx, stateDB) {
let txSenderPubkey;

// If recovering public key fails, then transaction is not valid.
try {
txSenderPubkey = Transaction.getPubKey(tx);
Expand All @@ -189,7 +188,6 @@ class Transaction {
const dataFromSender = deserializeState(await stateDB.get(txSenderAddress));

// If sender is a contract address, then it's not supposed to be used to send money, so return false if it is.

if (dataFromSender.codeHash !== EMPTY_HASH) return false;

return true;
Expand Down
5 changes: 1 addition & 4 deletions src/core/txPool.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
"use strict";

const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
const EC = require("elliptic").ec, ec = new EC("secp256k1");

const Transaction = require("./transaction");
const jelscript = require("./runtime");

const { BLOCK_GAS_LIMIT } = require("../config.json");
const { deserializeState, serializeState } = require("../utils/utils");
const { deserializeState } = require("../utils/utils");

async function addTransaction(transaction, chainInfo, stateDB) {
try {
Expand Down
2 changes: 2 additions & 0 deletions src/node/message.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

function produceMessage(type, data) {
// Produce a JSON message
return JSON.stringify({ type, data });
Expand Down
18 changes: 9 additions & 9 deletions src/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const opened = []; // Addresses and sockets from connected nodes.
const connected = []; // Addresses from connected nodes.
let connectedNodes = 0;

let worker = fork(`${__dirname}/../miner/worker.js`); // Worker thread (for PoW mining).
let worker = fork(`./src/miner/worker.js`); // Worker thread (for PoW mining).
let mined = false; // This will be used to inform the node that another node has already mined before it.


Expand All @@ -40,11 +40,11 @@ const chainInfo = {
difficulty: 1
};

const stateDB = new Level(__dirname + "/../../log/stateStore", { valueEncoding: "buffer" });
const blockDB = new Level(__dirname + "/../../log/blockStore", { valueEncoding: "buffer" });
const bhashDB = new Level(__dirname + "/../../log/bhashStore", { valueEncoding: "buffer" });
const txhashDB = new Level(__dirname + "/../../log/txhashStore");
const codeDB = new Level(__dirname + "/../../log/codeStore");
const stateDB = new Level("./log/stateStore", { valueEncoding: "buffer" });
const blockDB = new Level("./log/blockStore", { valueEncoding: "buffer" });
const bhashDB = new Level("./log/bhashStore", { valueEncoding: "buffer" });
const txhashDB = new Level("./log/txhashStore");
const codeDB = new Level("./log/codeStore");

async function startServer(options) {
const PORT = options.PORT || 3000; // Node's PORT
Expand Down Expand Up @@ -115,7 +115,7 @@ async function startServer(options) {

worker.kill(); // Stop the worker thread

worker = fork(`${__dirname}/../miner/worker.js`); // Renew
worker = fork(`./src/miner/worker.js`); // Renew
}

await updateDifficulty(newBlock, chainInfo, blockDB); // Update difficulty
Expand Down Expand Up @@ -566,7 +566,7 @@ async function mine(publicKey, ENABLE_LOGGING) {

// Transit state
for (const address in storage) {
const storageDB = new Level(__dirname + "/../../log/accountStore/" + address);
const storageDB = new Level("./log/accountStore/" + address);
const keys = Object.keys(storage[address]);

states[address].storageRoot = Merkle.buildTxTrie(keys.map(key => key + " " + storage[address][key]), false).root;
Expand Down Expand Up @@ -597,7 +597,7 @@ async function mine(publicKey, ENABLE_LOGGING) {
// Re-create the worker thread
worker.kill();

worker = fork(`${__dirname}/../miner/worker.js`);
worker = fork(`./src/miner/worker.js`);
})
.catch(err => console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Error at mining child process`, err));
}
Expand Down
8 changes: 3 additions & 5 deletions src/rpc/rpc.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
// Bad RPC server implementation, will be updated soon.

"use strict";

const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
Expand All @@ -20,7 +18,7 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD

reply.send({
success: false,
payload: null,
payload,
error: { message }
});
}
Expand Down Expand Up @@ -224,7 +222,7 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
) {
throwError("Invalid request.", 400);
} else {
const storageDB = new Level(__dirname + "/../../log/accountStore/" + contractInfo.address);
const storageDB = new Level("./log/accountStore/" + contractInfo.address);

respond({ storage: await storageDB.get(req.body.params.key) });

Expand All @@ -240,7 +238,7 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
) {
throwError("Invalid request.", 400);
} else {
const storageDB = new Level(__dirname + "/../../log/accountStore/" + contractInfo.address);
const storageDB = new Level("./log/accountStore/" + contractInfo.address);

respond({ storage: await storageDB.keys().all() });
}
Expand Down
6 changes: 1 addition & 5 deletions src/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ function log16(x) {
return Math.log(x) / Math.log(16);
}

function isNumber(str) {
return str.split("").every(char => "0123456789".includes(char));
}

function isHex(str) {
return (
str.startsWith("0x") &&
Expand Down Expand Up @@ -74,4 +70,4 @@ function deserializeState(stateInBytes) {
return stateHeader;
}

module.exports = { log16, isNumber, isHex, parseJSON, bigIntable, numToBuffer, serializeState, deserializeState };
module.exports = { log16, isHex, parseJSON, bigIntable, numToBuffer, serializeState, deserializeState };
2 changes: 2 additions & 0 deletions utils/keygen.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
"use strict";

const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");

const EC = require("elliptic").ec, ec = new EC("secp256k1");
Expand Down

0 comments on commit 81cca05

Please sign in to comment.