rcp.sh: curl wrapper for easier RPC calls

Signed-off-by: Jakub Sokołowski <jakub@status.im>
This commit is contained in:
Jakub Sokołowski 2021-08-24 09:43:07 +02:00 committed by Jakub
parent e4540a0853
commit cd2b53643d
2 changed files with 54 additions and 5 deletions

View File

@ -46,8 +46,13 @@ There's two simple ways to verify your Mailserver is up and running.
By making an HTTP request to the metrics port(`9090` by default) you can check if you Mailserver is receiving envelopes: By making an HTTP request to the metrics port(`9090` by default) you can check if you Mailserver is receiving envelopes:
```sh ```sh
$ curl -s localhost:9090/metrics | grep '^whisper_envelopes_received_total' > curl -sS localhost:9090/metrics | grep '^waku_envelopes_received_total'
whisper_envelopes_received_total 123 waku_envelopes_received_total 123
```
Or numbers and types of peers connected:
```sh
> curl -sS localhost:9090/metrics | grep '^p2p_peers_count'
p2p_peers_count{platform="linux-amd64",type="Statusd",version="v0.79.0"} 3
``` ```
## JSON RPC Calls ## JSON RPC Calls
@ -55,10 +60,16 @@ whisper_envelopes_received_total 123
The JSON RPC port (`8545` by default) allows you to manage your node. The JSON RPC port (`8545` by default) allows you to manage your node.
You can list connected peers by doing: You can list connected peers by doing:
```sh ```sh
$ export DATA='{"jsonrpc":"2.0","method":"admin_peers", "params": [], "id":1}' > export RPC_HOST=localhost RPC_PORT=8545
$ curl -s -H 'content-type: application/json' -d $DATA localhost:8545 \ > _assets/scripts/rpc.sh admin_peers | jq -r '.result[].network.remoteAddress'
| jq -r '.result[].network.remoteAddress'
34.68.132.118:30305 34.68.132.118:30305
134.209.136.123:30305 134.209.136.123:30305
178.128.141.249:443 178.128.141.249:443
``` ```
Where [`rpc.sh`](./_assets/scripts/rpc.sh) is simply a thin wrapper around `curl`.
You can use it to easily add peers too:
```sh
> _assets/scripts/rpc.sh admin_addPeer enode://7aa648d6e855950b2e3d3bf220c496e0cae4adfddef3e1e6062e6b177aec93bc6cdcf1282cb40d1656932ebfdd565729da440368d7c4da7dbd4d004b1ac02bf8@178.128.142.26:443
{"jsonrpc": "2.0", "id": 1, "result": true}
```

38
_assets/scripts/rpc.sh Executable file
View File

@ -0,0 +1,38 @@
#!/usr/bin/env bash
set -euo pipefail
RPC_HOST="${RPC_HOST:-localhost}"
RPC_PORT="${RPC_PORT:-8545}"
RPC_URL="${RPC_URL:-http://${RPC_HOST}:${RPC_PORT}/}"
METHOD="$1"
shift
PARAMS=("$@")
if [[ -z "${METHOD}" ]]; then
echo "No method specified!" >&2
exit 1
fi
# Parameter expansion trick to avoid var unbound error.
if [[ -z "${PARAMS-}" ]]; then
PARAMS_STR=''
else
PARAMS_STR=$(printf '%s\",\"' "${PARAMS[@]}")
PARAMS_STR="\"${PARAMS_STR%%\",\"}\""
fi
PAYLOAD="{
\"id\": 1,
\"jsonrpc\": \"2.0\",
\"method\": \"${METHOD}\",
\"params\": [${PARAMS_STR}]
}"
OUT=$(
curl --fail --show-error --silent \
-H "Content-type:application/json" \
-X POST --data "${PAYLOAD}" \
"${RPC_URL}"
)
echo "${OUT}" | jq .