Skip to content

Commit 23dd769

Browse files
Merge pull request #81 from nguyenphuminh/tx-from-hash
Tx from hash
2 parents 3264052 + ee39f43 commit 23dd769

File tree

5 files changed

+64
-4
lines changed

5 files changed

+64
-4
lines changed

JSON-RPC.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,18 @@ JSON-RPC APIs are APIs provided by running a JeChain RPC server. They can be use
190190
* Status: 400
191191
* Body: `{ success: false, payload: null, error: { message: "Invalid transaction index." } }`
192192

193+
* `/get_transactionByTxHash`:
194+
* Use case: Get transaction through its hash.
195+
* Request body: `{ params: { hash: <tx_hash_hex_string> } }`
196+
* Reply body: `{ success: true, payload: { transaction: <transaction_object> } }`
197+
* Error body:
198+
* Invalid request (not enough params):
199+
* Status: 400
200+
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`
201+
* Failed to get transaction with the given hash:
202+
* Status: 400
203+
* Body: `{ success: false, payload: null, error: { message: "Failed to get transaction with the given hash." } }`
204+
193205
### Other errors
194206

195207
* Invalid option (non-existent API):

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.1",
3+
"version": "0.28.2",
44
"description": "Node for JeChain - an experimental smart contract blockchain network",
55
"main": "./index.js",
66
"scripts": {

src/consensus/consensus.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ async function verifyBlock(newBlock, chainInfo, stateDB, codeDB, enableLogging =
4848
// Check gas limit
4949
Block.hasValidGasLimit(newBlock) &&
5050

51-
// Check transactions and transit state rih
51+
// Check transactions and transit state right after
5252
await Block.verifyTxAndTransit(newBlock, stateDB, codeDB, enableLogging)
5353
)
5454
}

src/node/server.js

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ const chainInfo = {
4343
const stateDB = new Level(__dirname + "/../../log/stateStore", { valueEncoding: "buffer" });
4444
const blockDB = new Level(__dirname + "/../../log/blockStore", { valueEncoding: "buffer" });
4545
const bhashDB = new Level(__dirname + "/../../log/bhashStore", { valueEncoding: "buffer" });
46+
const txhashDB = new Level(__dirname + "/../../log/txhashStore");
4647
const codeDB = new Level(__dirname + "/../../log/codeStore");
4748

4849
async function startServer(options) {
@@ -122,6 +123,14 @@ async function startServer(options) {
122123
await blockDB.put(newBlock.blockNumber.toString(), Buffer.from(_message.data)); // Add block to chain
123124
await bhashDB.put(newBlock.hash, numToBuffer(newBlock.blockNumber)); // Assign block number to the matching block hash
124125

126+
// Apply to all txns of the block: Assign transaction index and block number to transaction hash
127+
for (let txIndex = 0; txIndex < newBlock.transactions.length; txIndex++) {
128+
const tx = Transaction.deserialize(newBlock.transactions[txIndex]);
129+
const txHash = Transaction.getHash(tx);
130+
131+
await txhashDB.put(txHash, newBlock.blockNumber.toString() + " " + txIndex.toString());
132+
}
133+
125134
chainInfo.latestBlock = newBlock; // Update latest block cache
126135

127136
// Update the new transaction pool (remove all the transactions that are no longer valid).
@@ -237,6 +246,14 @@ async function startServer(options) {
237246
await blockDB.put(block.blockNumber.toString(), Buffer.from(_message.data)); // Add block to chain
238247
await bhashDB.put(block.hash, numToBuffer(block.blockNumber)); // Assign block number to the matching block hash
239248

249+
// Assign transaction index and block number to transaction hash
250+
for (let txIndex = 0; txIndex < block.transactions.length; txIndex++) {
251+
const tx = Transaction.deserialize(block.transactions[txIndex]);
252+
const txHash = Transaction.getHash(tx);
253+
254+
await txhashDB.put(txHash, block.blockNumber.toString() + " " + txIndex.toString());
255+
}
256+
240257
if (!chainInfo.latestSyncBlock) {
241258
chainInfo.latestSyncBlock = block; // Update latest synced block.
242259

@@ -349,7 +366,7 @@ async function startServer(options) {
349366
}
350367

351368
if (ENABLE_MINING) loopMine(publicKey, ENABLE_CHAIN_REQUEST, ENABLE_LOGGING);
352-
if (ENABLE_RPC) rpc(RPC_PORT, { publicKey, mining: ENABLE_MINING, chainInfo }, sendTransaction, keyPair, stateDB, blockDB, bhashDB, codeDB);
369+
if (ENABLE_RPC) rpc(RPC_PORT, { publicKey, mining: ENABLE_MINING, chainInfo }, sendTransaction, keyPair, stateDB, blockDB, bhashDB, codeDB, txhashDB);
353370
}
354371

355372
// Function to connect to a node.
@@ -519,6 +536,14 @@ async function mine(publicKey, ENABLE_LOGGING) {
519536
await blockDB.put(result.blockNumber.toString(), Buffer.from(Block.serialize(result))); // Add block to chain
520537
await bhashDB.put(result.hash, numToBuffer(result.blockNumber)); // Assign block number to the matching block hash
521538

539+
// Assign transaction index and block number to transaction hash
540+
for (let txIndex = 0; txIndex < result.transactions.length; txIndex++) {
541+
const tx = Transaction.deserialize(result.transactions[txIndex]);
542+
const txHash = Transaction.getHash(tx);
543+
544+
await txhashDB.put(txHash, result.blockNumber.toString() + " " + txIndex.toString());
545+
}
546+
522547
chainInfo.latestBlock = result; // Update latest block cache
523548

524549
// Reward

src/rpc/rpc.js

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const { deserializeState, serializeState } = require("../utils/utils");
99

1010
const fastify = require("fastify")();
1111

12-
function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashDB, codeDB) {
12+
function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashDB, codeDB, txhashDB) {
1313

1414
process.on("uncaughtException", err => console.log(`\x1b[31mERROR\x1b[0m [${(new Date()).toISOString()}] Uncaught Exception`, err));
1515

@@ -310,6 +310,29 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD
310310

311311
break;
312312

313+
case "get_transactionByTxHash":
314+
if (
315+
typeof req.body.params !== "object" ||
316+
typeof req.body.params.hash !== "string"
317+
) {
318+
throwError("Invalid request.", 400);
319+
} else {
320+
try {
321+
const indexPair = await txhashDB.get(req.body.params.hash);
322+
323+
const [ blockNumber, txIndex ] = indexPair.split(" ").map(item => parseInt(item));
324+
325+
const block = Block.deserialize([...await blockDB.get( blockNumber.toString() )]);
326+
const transaction = block.transactions[txIndex];
327+
328+
respond({ transaction });
329+
} catch (e) {
330+
throwError("Failed to get transaction with the given hash.", 400);
331+
}
332+
}
333+
334+
break;
335+
313336
case "sendTransaction":
314337
if (
315338
typeof req.body.params !== "object" ||

0 commit comments

Comments
 (0)