From cd2b53643d824b0499a37ce7bf8bd3d468935e08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Soko=C5=82owski?= Date: Tue, 24 Aug 2021 09:43:07 +0200 Subject: [PATCH] rcp.sh: curl wrapper for easier RPC calls MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jakub SokoĊ‚owski --- MAILSERVER.md | 21 ++++++++++++++++----- _assets/scripts/rpc.sh | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 5 deletions(-) create mode 100755 _assets/scripts/rpc.sh diff --git a/MAILSERVER.md b/MAILSERVER.md index c71dc3211..6eb6d2e70 100644 --- a/MAILSERVER.md +++ b/MAILSERVER.md @@ -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: ```sh - $ curl -s localhost:9090/metrics | grep '^whisper_envelopes_received_total' -whisper_envelopes_received_total 123 + > curl -sS localhost:9090/metrics | grep '^waku_envelopes_received_total' +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 @@ -55,10 +60,16 @@ whisper_envelopes_received_total 123 The JSON RPC port (`8545` by default) allows you to manage your node. You can list connected peers by doing: ```sh - $ export DATA='{"jsonrpc":"2.0","method":"admin_peers", "params": [], "id":1}' - $ curl -s -H 'content-type: application/json' -d $DATA localhost:8545 \ - | jq -r '.result[].network.remoteAddress' + > export RPC_HOST=localhost RPC_PORT=8545 + > _assets/scripts/rpc.sh admin_peers | jq -r '.result[].network.remoteAddress' 34.68.132.118:30305 134.209.136.123:30305 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} +``` diff --git a/_assets/scripts/rpc.sh b/_assets/scripts/rpc.sh new file mode 100755 index 000000000..3fc1fe321 --- /dev/null +++ b/_assets/scripts/rpc.sh @@ -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 .