Skip to content

Commit

Permalink
Merge pull request #77 from nguyenphuminh/minor-rpc-fix
Browse files Browse the repository at this point in the history
Minor rpc fix
  • Loading branch information
nguyenphuminh authored Sep 9, 2023
2 parents 509f5c9 + 9caf05f commit 4c3b60d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 5 deletions.
55 changes: 52 additions & 3 deletions JSON-RPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ JSON-RPC APIs are APIs provided by running a JeChain RPC server. They can be use
* Reply body: `{ success: true, payload: { blockNumber: <block_number> } }`

* `/get_address`:
* Use case: Get the JeChain address from the RPC server.
* Use case: Get the client's JeChain address from the RPC server.
* Reply body: `{ success: true, payload: { address: <address> } }`

* `/get_pubkey`:
* Use case: Get the client's JeChain pubkey from the RPC server.
* Reply body: `{ success: true, payload: { pubkey: <address> } }`

* `/get_work`:
* Use case: Get hash and nonce from the latest block.
Expand Down Expand Up @@ -84,14 +88,32 @@ JSON-RPC APIs are APIs provided by running a JeChain RPC server. They can be use
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/get_code`:
* Use case: Get code from address.
* `/get_nonce`:
* Use case: Get nonce from address.
* Request body: `{ params: { address: <address> } }`
* Reply body: `{ success: true, payload: { nonce: <nonce> } }`
* Error body:
* Invalid request (not enough params):
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/get_code`:
* Use case: Get code from code hash.
* Request body: `{ params: { codeHash: <code_hash_as_hexstring> } }`
* Reply body: `{ success: true, payload: { code: <code> } }`
* Error body:
* Invalid request (not enough params):
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/get_codeHash`:
* Use case: Get code hash from address.
* Request body: `{ params: { address: <address> } }`
* Reply body: `{ success: true, payload: { codeHash: <code_hash_as_hexstring> } }`
* Error body:
* Invalid request (not enough params):
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/get_storage`:
* Use case: Get the storage object from address.
Expand All @@ -110,6 +132,33 @@ JSON-RPC APIs are APIs provided by running a JeChain RPC server. They can be use
* Invalid request (not enough params):
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/signTransaction`:
* Use case: Sign transaction.
* Request body: `{ params: { transaction: <transaction_object> } }`
* Reply body: `{ success: true, payload: { transaction: <transaction_object> } }`
* Error body:
* Invalid request (not enough params):
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/serializeTransaction`:
* Use case: Serialize transaction.
* Request body: `{ params: { transaction: <transaction_object> } }`
* Reply body: `{ success: true, payload: { transaction: <transaction_bytearray> } }`
* Error body:
* Invalid request (not enough params):
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/deserializeTransaction`:
* Use case: Deserialize transaction.
* Request body: `{ params: { transaction: <transaction_bytearray> } }`
* Reply body: `{ success: true, payload: { transaction: <transaction_object> } }`
* Error body:
* Invalid request (not enough params):
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/get_transactionByBlockHashAndIndex`:
* Use case: Get transaction through block hash and transaction index.
Expand Down
2 changes: 1 addition & 1 deletion src/node/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ async function startServer(options) {
}

if (ENABLE_MINING) loopMine(publicKey, ENABLE_CHAIN_REQUEST, ENABLE_LOGGING);
if (ENABLE_RPC) rpc(RPC_PORT, { publicKey, mining: ENABLE_MINING }, sendTransaction, keyPair, stateDB, blockDB, bhashDB, codeDB);
if (ENABLE_RPC) rpc(RPC_PORT, { publicKey, mining: ENABLE_MINING, chainInfo }, sendTransaction, keyPair, stateDB, blockDB, bhashDB, codeDB);
}

// Function to connect to a node.
Expand Down
44 changes: 43 additions & 1 deletion src/rpc/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"use strict";

const crypto = require("crypto"), SHA256 = message => crypto.createHash("sha256").update(message).digest("hex");
const Transaction = require("../core/transaction");
const Block = require("../core/block");
const { deserializeState, serializeState } = require("../utils/utils");
Expand Down Expand Up @@ -33,11 +34,16 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD

switch (req.params.option) {
case "get_blockNumber":
respond({ blockNumber: Math.max(...(await blockDB.keys().all()).map(key => parseInt(key))) });
respond({ blockNumber: client.chainInfo.latestBlock.blockNumber });

break;

case "get_address":
respond({ address: SHA256(client.publicKey) });

break;

case "get_pubkey":
respond({ address: client.publicKey });

break;
Expand Down Expand Up @@ -336,6 +342,42 @@ function rpc(PORT, client, transactionHandler, keyPair, stateDB, blockDB, bhashD

break;

case "serializeTransaction":
if (
typeof req.body.params !== "object" ||
typeof req.body.params.transaction !== "object"
) {
throwError("Invalid request.", 400);
} else {
const transaction = req.body.params.transaction;

try {
respond({ transaction: Transaction.serialize(transaction) });
} catch (e) {
throwError("Failed to serialize.", 400);
}
}

break;

case "deserializeTransaction":
if (
typeof req.body.params !== "object" ||
!Array.isArray(req.body.params.transaction)
) {
throwError("Invalid request.", 400);
} else {
const transaction = req.body.params.transaction;

try {
respond({ transaction: Transaction.deserialize(transaction) });
} catch (e) {
throwError("Failed to deserialize.", 400);
}
}

break;

default:
throwError("Invalid option.", 404);
}
Expand Down

0 comments on commit 4c3b60d

Please sign in to comment.