erigon-pulse/turbo/jsonrpc
Park Changwan ef84972e7c
Add addPeer RPC (#7804)
This PR mirrors https://github.com/testinprod-io/op-erigon/pull/54.

Actual implementation for `admin_addPeer` method.
RPC Spec: Refer to
https://geth.ethereum.org/docs/interacting-with-geth/rpc/ns-admin.

> The addPeer administrative method requests adding a new remote node to
the list of tracked static nodes. The node will try to maintain
connectivity to these nodes at all times, reconnecting every once in a
while if the remote connection goes down.

Requires https://github.com/ledgerwatch/erigon-lib/pull/1033/

After https://github.com/ledgerwatch/erigon-lib/pull/1033 is merged,
will update erigon-lib version, removing replace at go.mod.

Note that even if RPC response returns `true`, it does not guarantee
that RLPx protocol is established between peers. It just adds node
entrypoint to its static peer list, and periodically tries and tests
connections.

## Testing

This RPC needs integration testing, so I made some scenario.

Use below command for testing:

Spin up two dev nodes which p2p enabled:

Start Node 1: RPC running at port 8545:
```sh
./build/bin/erigon --datadir=dev --chain=dev --port=30303 --http.port=8545 --authrpc.port=8551 --torrent.port=42069  --no-downloader --nodiscover --private.api.addr=127.0.0.1:9090 --http --ws --http.api=admin --p2p.allowed-ports=30306,30307,30308  --authrpc.jwtsecret=/tmp/jwt1 --p2p.protocol=67,68  --log.console.verbosity=5
```

Start Node 2: RPC running at port 8546:
```sh
./build/bin/erigon --datadir=dev2 --chain=dev --port=30304 --http.port=8546 --authrpc.port=8552 --torrent.port=42068 --no-downloader --nodiscover --private.api.addr=127.0.0.1:9091 --http --ws --http.api=admin --p2p.allowed-ports=30309,30310,30311  --authrpc.jwtsecret=/tmp/jwt2  --p2p.protocol=67,68  --log.console.verbosity=5
```

Get nodeInfo of node 1 using `admin_nodeInfo` RPC:
```sh
curl --location 'localhost:8545/' \
--header 'Content-Type: application/json' \
--data '{
	"jsonrpc":"2.0",
	"method":"admin_nodeInfo",
	"params":[],
	"id":1
}' 
```
Example response:
```
{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "id": "b75e0c4d2113b6f144ea8fd356a8f90e612a2a5f48a13c78d7e0e176e5724eb2",
        "name": "erigon/v2.47.0-dev-5d86cdb5/darwin-arm64/go1.19.6",
        "enode": "enode://05ab575d947f2d73065ea0f795dc2d96ed0ad603f3e730ab90dc881122d552c9f59ffcb148fe50546bec8b319daeb3c22ec02e7d12a7c4f2ac4cd26456a04a7c@127.0.0.1:30303?discport=0",
   ...
```

Get nodeInfo of node 2 using `admin_nodeInfo` RPC:
```sh
curl --location 'localhost:8546/' \
--header 'Content-Type: application/json' \
--data '{
	"jsonrpc":"2.0",
	"method":"admin_nodeInfo",
	"params":[],
	"id":2
}' 
```
Example response:
```
{
    "jsonrpc": "2.0",
    "id": 2,
    "result": {
        "id": "32d721e4d75219b021d7f83235f1f1eb8b705d6f85e634bccde564b8f7f94d78",
        "name": "erigon/v2.47.0-dev-5d86cdb5/darwin-arm64/go1.19.6",
        "enode": "enode://1abb8579647779e13b7f68d18f9c776cbd29281841c7f950e9cf9afa996e31120a6f481cea8e90e0f42a0eb1aa00aeafee81c4bae6c31aa16810b795c6d6e069@127.0.0.1:30304?discport=0",
   ...
```

Call `admin_addPeer` RPC to node 2:
```sh
curl --location 'localhost:8546/' \
--header 'Content-Type: application/json' \
--data '{
	"jsonrpc":"2.0",
	"method":"admin_addPeer",
	"params":["enode://05ab575d947f2d73065ea0f795dc2d96ed0ad603f3e730ab90dc881122d552c9f59ffcb148fe50546bec8b319daeb3c22ec02e7d12a7c4f2ac4cd26456a04a7c@127.0.0.1:30303"],
	"id":2
}' 
```
Example response:
```
{
    "jsonrpc": "2.0",
    "id": 2,
    "result": true
}
```


Check peer info of node 1 using `admin_peers` RPC:
```sh
curl --location 'localhost:8545/' \
--header 'Content-Type: application/json' \
--data '{
	"jsonrpc":"2.0",
	"method":"admin_peers",
	"params":[],
	"id":1
}' 
```
Example response:
```
{
    "jsonrpc": "2.0",
    "id": 1,
    "result": [
        {
            "enode": "enode://1abb8579647779e13b7f68d18f9c776cbd29281841c7f950e9cf9afa996e31120a6f481cea8e90e0f42a0eb1aa00aeafee81c4bae6c31aa16810b795c6d6e069@127.0.0.1:55426",
            "id": "32d721e4d75219b021d7f83235f1f1eb8b705d6f85e634bccde564b8f7f94d78",
            "name": "erigon/v2.47.0-dev-5d86cdb5/darwin-arm64/go1.19.6",
            "caps": [
                "eth/66",
                "eth/67"
            ],
            "network": {
                "localAddress": "127.0.0.1:30303",
                "remoteAddress": "127.0.0.1:55426",
                "inbound": true,
                "trusted": false,
                "static": false
            },
            "protocols": null
        }
    ]
}
```

---------

Co-authored-by: alex.sharov <AskAlexSharov@gmail.com>
2023-09-06 15:31:02 +07:00
..
contracts moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
admin_api.go Add addPeer RPC (#7804) 2023-09-06 15:31:02 +07:00
bor_api.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
bor_helper.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
bor_snapshot.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
call_traces_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
corner_cases_support_test.go Separated PendingBlock behaviour to be chain agnostic (#7859) 2023-07-10 19:22:03 +02:00
daemon.go Max cap search results for ots API by default + cli flag to override it (#7924) 2023-07-28 08:29:17 +07:00
db_api_deprecated.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
debug_api_test.go Separated PendingBlock behaviour to be chain agnostic (#7859) 2023-07-10 19:22:03 +02:00
debug_api.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
erigon_api.go Removed unused stages (Cumulative index + Translation) (#7884) 2023-07-13 16:55:48 +02:00
erigon_block.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
erigon_cache_check.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
erigon_nodeInfo.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
erigon_receipts_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
erigon_receipts.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
erigon_system.go Correctly compute fork id when timestamp fork is activated in genesis (#8046) 2023-08-21 15:35:13 +02:00
error_messages.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
eth_accounts.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
eth_api_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
eth_api.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
eth_block_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
eth_block.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
eth_call_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
eth_call.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
eth_callMany_test.go Separated PendingBlock behaviour to be chain agnostic (#7859) 2023-07-10 19:22:03 +02:00
eth_callMany.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
eth_deprecated.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
eth_filters_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
eth_filters.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
eth_mining_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
eth_mining.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
eth_receipts.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
eth_subscribe_test.go add flag --force.partial.commit: to workaround problem "start from backup takes long time and can't save partial progress" (#8090) 2023-08-30 08:49:16 +07:00
eth_system_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
eth_system.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
eth_txs.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
eth_uncles.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
gen_traces_test.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
get_chain_config_test.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
graphql_api.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
net_api.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_api.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
otterscan_block_details.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_contract_creator_test.go Max cap search results for ots API by default + cli flag to override it (#7924) 2023-07-28 08:29:17 +07:00
otterscan_contract_creator.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_default_tracer.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_generic_tracer.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
otterscan_has_code.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_search_backward_multi_test.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_search_backward_test.go Max cap search results for ots API by default + cli flag to override it (#7924) 2023-07-28 08:29:17 +07:00
otterscan_search_backward.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_search_forward_multi_test.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_search_forward_test.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_search_forward.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_search_multi.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_search_test.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_search_trace.go EIP-4844: Rename "data gas" to "blob gas" (#7937) 2023-07-28 12:12:05 +02:00
otterscan_trace_contract_creator.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_trace_operations.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_trace_touch.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_trace_transaction.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_transaction_by_sender_and_nonce_test.go Max cap search results for ots API by default + cli flag to override it (#7924) 2023-07-28 08:29:17 +07:00
otterscan_transaction_by_sender_and_nonce.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_transaction_error.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
otterscan_types.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
parity_api_test.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
parity_api.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
send_transaction_test.go add flag --force.partial.commit: to workaround problem "start from backup takes long time and can't save partial progress" (#8090) 2023-08-30 08:49:16 +07:00
send_transaction.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
storage_range.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
trace_adhoc_test.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
trace_adhoc.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
trace_api.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
trace_filtering.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
trace_types.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
tracing.go Restore genesis reads code - remote rpc case. Remove ctx parameter from baseApi func (tx already has internal ctx) (#8122) 2023-09-04 12:42:08 +07:00
txpool_api_test.go Replaced old version of Engine API with newer version (#7972) 2023-08-05 23:33:10 +02:00
txpool_api.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
validator_set.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00
web3_api.go moved cmd/rpcdaemon/commands -> turbo/jsonrpc (#7858) 2023-07-08 19:01:26 +02:00