Skip to content

Commit bbd4c3b

Browse files
Merge pull request #32 from nguyenphuminh/rpc-changes
Update RPC server
2 parents 2dda6d3 + 73435ef commit bbd4c3b

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

JSON-RPC.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ JSON-RPC APIs are APIs provided by running a JeChain RPC server. They can be use
2121
* Use case: Get hash and nonce from the latest block.
2222
* Reply body: `{ success: true, payload: { hash: <hash>, nonce: <nonce> } }`
2323

24+
* `/mining`:
25+
* Use case: Check on whether client is mining or not.
26+
* Reply body: `{ success: true, payload: { mining: true | false } }`
27+
2428
### POST
2529

2630
* `/get_blockByHash`:
@@ -89,6 +93,15 @@ JSON-RPC APIs are APIs provided by running a JeChain RPC server. They can be use
8993
* Status: 400
9094
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`
9195

96+
* `/get_storage`:
97+
* Use case: Get the storage object from address.
98+
* Request body: `{ params: { address: <address> } }`
99+
* Reply body: `{ success: true, payload: { storage: <storage_object> } }`
100+
* Error body:
101+
* Invalid request (not enough params):
102+
* Status: 400
103+
* Body: `{ success: false, payload: null, error: { message: "Invalid request." } }`
104+
92105
* `/sendTransaction`:
93106
* Use case: Send transaction.
94107
* Request body: `{ params: { transaction: <transaction_object> } }`

src/rpc/rpc.js

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ function rpc(PORT, client, transactionHandler, stateDB, blockDB) {
4848

4949
break;
5050

51+
case "mining":
52+
respond({ mining: client.mining });
53+
54+
break;
55+
5156
default:
5257
throwError("Invalid option.", 404);
5358
}
@@ -158,7 +163,7 @@ function rpc(PORT, client, transactionHandler, stateDB, blockDB) {
158163
throwError("Invalid request.", 400);
159164
} else {
160165
const dataFromTarget = await stateDB.get(req.body.params.address); // Fetch target's state object
161-
const targetBalance = dataFromTarget.balance; // Get target's balance
166+
const targetBalance = dataFromTarget.balance; // Get target's balance
162167

163168
respond({ balance: targetBalance });
164169
}
@@ -169,18 +174,34 @@ function rpc(PORT, client, transactionHandler, stateDB, blockDB) {
169174
if (
170175
typeof req.body.params !== "object" ||
171176
typeof req.body.params.address !== "string" ||
172-
!(await stateDB.keys().all()).includes(tx.sender)
177+
!(await stateDB.keys().all()).includes(req.body.params.address)
173178
) {
174179
throwError("Invalid request.", 400);
175180
} else {
176-
const dataFromTarget = await stateDB.get(tx.sender); // Fetch target's state object
177-
const targetBody = dataFromTarget.body; // Get target's code body
181+
const dataFromTarget = await stateDB.get(req.body.params.address); // Fetch target's state object
182+
const targetBody = dataFromTarget.body; // Get target's code body
178183

179184
respond({ code: targetBody });
180185
}
181186

182187
break;
183188

189+
case "get_storage":
190+
if (
191+
typeof req.body.params !== "object" ||
192+
typeof req.body.params.address !== "string" ||
193+
!(await stateDB.keys().all()).includes(req.body.params.address)
194+
) {
195+
throwError("Invalid request.", 400);
196+
} else {
197+
const dataFromTarget = await stateDB.get(req.body.params.address); // Fetch target's state object
198+
const targetStorage = dataFromTarget.body; // Get target's storage object
199+
200+
respond({ storage: targetStorage });
201+
}
202+
203+
break;
204+
184205
case "get_transactionByBlockNumberAndIndex":
185206
if (
186207
typeof req.body.params !== "object" ||

0 commit comments

Comments
 (0)