Skip to content

Commit

Permalink
Merge pull request #32 from nguyenphuminh/rpc-changes
Browse files Browse the repository at this point in the history
Update RPC server
  • Loading branch information
nguyenphuminh authored Jul 11, 2022
2 parents 2dda6d3 + 73435ef commit bbd4c3b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
13 changes: 13 additions & 0 deletions JSON-RPC.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ JSON-RPC APIs are APIs provided by running a JeChain RPC server. They can be use
* Use case: Get hash and nonce from the latest block.
* Reply body: `{ success: true, payload: { hash: <hash>, nonce: <nonce> } }`

* `/mining`:
* Use case: Check on whether client is mining or not.
* Reply body: `{ success: true, payload: { mining: true | false } }`

### POST

* `/get_blockByHash`:
Expand Down Expand Up @@ -89,6 +93,15 @@ 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_storage`:
* Use case: Get the storage object from address.
* Request body: `{ params: { address: <address> } }`
* Reply body: `{ success: true, payload: { storage: <storage_object> } }`
* Error body:
* Invalid request (not enough params):
* Status: 400
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`

* `/sendTransaction`:
* Use case: Send transaction.
* Request body: `{ params: { transaction: <transaction_object> } }`
Expand Down
29 changes: 25 additions & 4 deletions src/rpc/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ function rpc(PORT, client, transactionHandler, stateDB, blockDB) {

break;

case "mining":
respond({ mining: client.mining });

break;

default:
throwError("Invalid option.", 404);
}
Expand Down Expand Up @@ -158,7 +163,7 @@ function rpc(PORT, client, transactionHandler, stateDB, blockDB) {
throwError("Invalid request.", 400);
} else {
const dataFromTarget = await stateDB.get(req.body.params.address); // Fetch target's state object
const targetBalance = dataFromTarget.balance; // Get target's balance
const targetBalance = dataFromTarget.balance; // Get target's balance

respond({ balance: targetBalance });
}
Expand All @@ -169,18 +174,34 @@ function rpc(PORT, client, transactionHandler, stateDB, blockDB) {
if (
typeof req.body.params !== "object" ||
typeof req.body.params.address !== "string" ||
!(await stateDB.keys().all()).includes(tx.sender)
!(await stateDB.keys().all()).includes(req.body.params.address)
) {
throwError("Invalid request.", 400);
} else {
const dataFromTarget = await stateDB.get(tx.sender); // Fetch target's state object
const targetBody = dataFromTarget.body; // Get target's code body
const dataFromTarget = await stateDB.get(req.body.params.address); // Fetch target's state object
const targetBody = dataFromTarget.body; // Get target's code body

respond({ code: targetBody });
}

break;

case "get_storage":
if (
typeof req.body.params !== "object" ||
typeof req.body.params.address !== "string" ||
!(await stateDB.keys().all()).includes(req.body.params.address)
) {
throwError("Invalid request.", 400);
} else {
const dataFromTarget = await stateDB.get(req.body.params.address); // Fetch target's state object
const targetStorage = dataFromTarget.body; // Get target's storage object

respond({ storage: targetStorage });
}

break;

case "get_transactionByBlockNumberAndIndex":
if (
typeof req.body.params !== "object" ||
Expand Down

0 comments on commit bbd4c3b

Please sign in to comment.