-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
module/admin: add rpc bindings for the
admin
-namespace (#226)
* module/admin: added rpc bindings for the admin-namespace * doc update * cleanup --------- Co-authored-by: lmittmann <lmittmann@users.noreply.github.com>
- Loading branch information
Showing
13 changed files
with
294 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ export default { | |
eth: 'eth', | ||
debug: 'debug', | ||
txpool: 'txpool', | ||
admin: 'admin', | ||
net: 'net', | ||
web3: 'web3', | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# `admin`-Namespace | ||
|
||
List of supported RPC methods for `w3.Client` in the `admin`-namespace. | ||
|
||
## `admin_addPeer` | ||
`AddPeer` adds a new peer to the node's peer set and returns a bool indicating success. | ||
```go {3} | ||
var success bool | ||
client.Call( | ||
admin.AddPeer(url).Returns(&success), | ||
) | ||
``` | ||
|
||
## `admin_removePeer` | ||
`RemovePeer` removes a peer from the node's peer set and returns a bool indicating success. | ||
```go {3} | ||
var success bool | ||
client.Call( | ||
admin.RemovePeer(url).Returns(&success), | ||
) | ||
``` | ||
|
||
## `admin_addTrustedPeer` | ||
`AddTrustedPeer` adds a new trusted peer to the node's trusted peer set and returns a bool indicating success. | ||
```go {3} | ||
var success bool | ||
client.Call( | ||
admin.AddTrustedPeer(url).Returns(&success), | ||
) | ||
``` | ||
|
||
## `admin_removeTrustedPeer` | ||
`RemoveTrustedPeer` removes a trusted peer from the node's trusted peer set and returns a bool indicating success. | ||
```go {3} | ||
var success bool | ||
client.Call( | ||
admin.RemoveTrustedPeer(url).Returns(&success), | ||
) | ||
``` | ||
|
||
## `admin_nodeInfo` | ||
`NodeInfo` returns information about the running node. | ||
```go {3} | ||
var nodeInfo *admin.NodeInfoResponse | ||
client.Call( | ||
admin.NodeInfo().Returns(&nodeInfo), | ||
) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Package admin implements RPC API bindings for methods in the "admin" namespace. | ||
*/ | ||
package admin | ||
|
||
import ( | ||
"math/big" | ||
"net" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/ethereum/go-ethereum/p2p/enode" | ||
"github.com/lmittmann/w3/internal/module" | ||
"github.com/lmittmann/w3/w3types" | ||
) | ||
|
||
// AddPeer adds the given peer to the node's peer set and returns a bool | ||
// indicating success. | ||
func AddPeer(url *enode.Node) w3types.RPCCallerFactory[bool] { | ||
return module.NewFactory[bool]( | ||
"admin_addPeer", | ||
[]any{url}, | ||
) | ||
} | ||
|
||
// RemovePeer disconnects from the given peer if the connection exists and | ||
// returns a bool indicating success. | ||
func RemovePeer(url *enode.Node) w3types.RPCCallerFactory[bool] { | ||
return module.NewFactory[bool]( | ||
"admin_removePeer", | ||
[]any{url}, | ||
) | ||
} | ||
|
||
// AddTrustedPeer adds the given peer to the trusted peers list and returns a | ||
// bool indicating success. | ||
func AddTrustedPeer(url *enode.Node) w3types.RPCCallerFactory[bool] { | ||
return module.NewFactory[bool]( | ||
"admin_addTrustedPeer", | ||
[]any{url}, | ||
) | ||
} | ||
|
||
// RemoveTrustedPeer removes a remote node from the trusted peers list and | ||
// returns a bool indicating success. | ||
func RemoveTrustedPeer(url *enode.Node) w3types.RPCCallerFactory[bool] { | ||
return module.NewFactory[bool]( | ||
"admin_removeTrustedPeer", | ||
[]any{url}, | ||
) | ||
} | ||
|
||
// NodeInfo returns information about the running node. | ||
func NodeInfo() w3types.RPCCallerFactory[*NodeInfoResponse] { | ||
return module.NewFactory[*NodeInfoResponse]( | ||
"admin_nodeInfo", | ||
[]any{}, | ||
) | ||
} | ||
|
||
type NodeInfoResponse struct { | ||
Enode *enode.Node `json:"enode"` | ||
ID string `json:"id"` | ||
IP net.IP `json:"ip"` | ||
ListenAddr string `json:"listenAddr"` | ||
Name string `json:"name"` | ||
Ports *PortsInfo `json:"ports"` | ||
Protocols map[string]*ProtocolInfo `json:"protocols"` | ||
} | ||
|
||
type PortsInfo struct { | ||
Discovery int `json:"discovery"` | ||
Listener int `json:"listener"` | ||
} | ||
|
||
type ProtocolInfo struct { | ||
Difficulty *big.Int `json:"difficulty"` | ||
Genesis common.Hash `json:"genesis"` | ||
Head common.Hash `json:"head"` | ||
Network int `json:"network"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package admin_test | ||
|
||
import ( | ||
"math/big" | ||
"net" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/p2p/enode" | ||
"github.com/ethereum/go-ethereum/p2p/enr" | ||
"github.com/google/go-cmp/cmp/cmpopts" | ||
"github.com/lmittmann/w3" | ||
"github.com/lmittmann/w3/module/admin" | ||
"github.com/lmittmann/w3/rpctest" | ||
) | ||
|
||
func TestAddPeer(t *testing.T) { | ||
rpctest.RunTestCases(t, []rpctest.TestCase[bool]{ | ||
{ | ||
Golden: "add_peer", | ||
Call: admin.AddPeer(enode.MustParse("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303")), | ||
WantRet: true, | ||
}, | ||
}) | ||
} | ||
|
||
func TestRemovePeer(t *testing.T) { | ||
rpctest.RunTestCases(t, []rpctest.TestCase[bool]{ | ||
{ | ||
Golden: "remove_peer", | ||
Call: admin.RemovePeer(enode.MustParse("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303")), | ||
WantRet: true, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAddTrustedPeer(t *testing.T) { | ||
rpctest.RunTestCases(t, []rpctest.TestCase[bool]{ | ||
{ | ||
Golden: "add_trusted_peer", | ||
Call: admin.AddTrustedPeer(enode.MustParse("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303")), | ||
WantRet: true, | ||
}, | ||
}) | ||
} | ||
|
||
func TestRemoveTrustedPeer(t *testing.T) { | ||
rpctest.RunTestCases(t, []rpctest.TestCase[bool]{ | ||
{ | ||
Golden: "remove_trusted_peer", | ||
Call: admin.RemoveTrustedPeer(enode.MustParse("enode://a979fb575495b8d6db44f750317d0f4622bf4c2aa3365d6af7c284339968eef29b69ad0dce72a4d8db5ebb4968de0e3bec910127f134779fbcb0cb6d3331163c@52.16.188.185:30303")), | ||
WantRet: true, | ||
}, | ||
}) | ||
} | ||
|
||
func TestNodeInfo(t *testing.T) { | ||
rpctest.RunTestCases(t, []rpctest.TestCase[*admin.NodeInfoResponse]{ | ||
{ | ||
Golden: "node_info", | ||
Call: admin.NodeInfo(), | ||
WantRet: &admin.NodeInfoResponse{ | ||
Enode: enode.MustParse("enode://44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d@[::]:30303"), | ||
ID: "44826a5d6a55f88a18298bca4773fca5749cdc3a5c9f308aa7d810e9b31123f3e7c5fba0b1d70aac5308426f47df2a128a6747040a3815cc7dd7167d03be320d", | ||
IP: net.ParseIP("::"), | ||
ListenAddr: "[::]:30303", | ||
Name: "reth/v0.0.1/x86_64-unknown-linux-gnu", | ||
Ports: &admin.PortsInfo{ | ||
Discovery: 30303, | ||
Listener: 30303, | ||
}, | ||
Protocols: map[string]*admin.ProtocolInfo{ | ||
"eth": { | ||
Difficulty: w3.I("17334254859343145000"), | ||
Genesis: w3.H("0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"), | ||
Head: w3.H("0xb83f73fbe6220c111136aefd27b160bf4a34085c65ba89f24246b3162257c36a"), | ||
Network: 1, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, cmpopts.IgnoreUnexported(big.Int{}, enode.Node{}, enr.Record{})) | ||
} |
Oops, something went wrong.