-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathNetworkRpcs.R
251 lines (251 loc) · 7.82 KB
/
NetworkRpcs.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#' RPC-JSON API: getpeerinfo
#'
#' Returns data about each connected network node as a json array of objects.
#'
#' @param con object of class \code{CONRPC}.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references \url{https://bitcoin.org/en/developer-reference#getpeerinfo},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name getpeerinfo
#' @aliases getpeerinfo
#' @rdname getpeerinfo
#' @export
getpeerinfo <- function(con){
rpcpost(con, "getpeerinfo")
}
#' RPC-JSON API: getnetworkinfo
#'
#' Returns an object containing various state info regarding P2P networking.
#'
#' @param con object of class \code{CONRPC}.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references \url{https://bitcoin.org/en/developer-reference#getnetworkinfo},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name getnetworkinfo
#' @aliases getnetworkinfo
#' @rdname getnetworkinfo
#' @export
getnetworkinfo <- function(con){
rpcpost(con, "getnetworkinfo")
}
#' RPC-JSON API: ping
#'
#' Requests that a ping be sent to all other nodes, to measure ping time.
#' Results provided in getpeerinfo, pingtime and pingwait fields are
#' decimal seconds. Ping command is handled in queue with all other commands,
#' so it measures processing backlog, not just network ping.
#'
#' @param con object of class \code{CONRPC}.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references \url{https://bitcoin.org/en/developer-reference#ping},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name ping
#' @aliases ping
#' @rdname ping
#' @export
ping <- function(con){
rpcpost(con, "ping")
}
#' RPC-JSON API: getnettotals
#'
#' Returns information about network traffic, including bytes in,
#' bytes out, and current time.
#'
#' @param con object of class \code{CONRPC}.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references \url{https://bitcoin.org/en/developer-reference#getnettotals},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name getnettotals
#' @aliases getnettotals
#' @rdname getnettotals
#' @export
getnettotals <- function(con){
rpcpost(con, "getnettotals")
}
#' RPC-JSON API: getconnectioncount
#'
#' Returns the number of connections to other nodes.
#'
#' @param con object of class \code{CONRPC}.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references
#' \url{https://bitcoin.org/en/developer-reference#getconnectioncount},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name getconnectioncount
#' @aliases getconnectioncount
#' @rdname getconnectioncount
#' @export
getconnectioncount <- function(con){
rpcpost(con, "getconnectioncount")
}
#' RPC-JSON API: setnetworkactive
#'
#' Disable/enable all p2p network activity.
#'
#' @param con object of class \code{CONRPC}.
#' @param state \code{logical} the network state.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references
#' \url{https://bitcoin.org/en/developer-reference#setnetworkactive},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name setnetworkactive
#' @aliases setnetworkactive
#' @rdname setnetworkactive
#' @export
setnetworkactive <- function(con, state = TRUE){
pl <- list(state = state)
rpcpost(con, "setnetworkactive", pl)
}
#' RPC-JSON API: listbanned
#'
#' List all banned IPs/Subnets.
#'
#' @param con object of class \code{CONRPC}.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references
#' \url{https://bitcoin.org/en/developer-reference#listbanned},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name listbanned
#' @aliases listbanned
#' @rdname listbanned
#' @export
listbanned <- function(con){
rpcpost(con, "listbanned")
}
#' RPC-JSON API: addnode
#'
#' Attempts to add or remove a node from the addnode list.
#' Or try a connection to a node once.
#'
#' @param con object of class \code{CONRPC}.
#' @param node \code{character} the node (see \code{getpeerinfo()} for nodes).
#' @param command \code{character} 'add' to add a node to the list,
#' 'remove' to remove a node from the list, 'onetry' to try a connection
#' to the node once.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references
#' \url{https://bitcoin.org/en/developer-reference#addnode},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name addnode
#' @aliases addnode
#' @rdname addnode
#' @export
addnode <- function(con, node, command = c("add", "remove", "onetry")){
node <- as.character(node)
command <- match.arg(command)
pl <- list(node = node, command = command)
rpcpost(con, "addnode", pl)
}
#' RPC-JSON API: clearbanned
#'
#' Clear all banned IPs.
#'
#' @param con object of class \code{CONRPC}.
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references
#' \url{https://bitcoin.org/en/developer-reference#clearbanned},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name clearbanned
#' @aliases clearbanned
#' @rdname clearbanned
#' @export
clearbanned <- function(con){
rpcpost(con, "clearbanned")
}
#' RPC-JSON API: getaddednodeinfo
#'
#' Returns information about the given added node,
#' or all added nodes (note that onetry addnodes
#' are not listed here)
#'
#' @param con object of class \code{CONRPC}.
#' @param node \code{character} the node (see \code{getpeerinfo()}
#' for nodes).
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references
#' \url{https://bitcoin.org/en/developer-reference#getaddednodeinfo},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name getaddednodeinfo
#' @aliases getaddednodeinfo
#' @rdname getaddednodeinfo
#' @export
getaddednodeinfo <- function(con, node = NULL){
if (is.null(node)){
return(rpcpost(con, "getaddednodeinfo"))
} else {
node <- as.character(node)[1]
pl <- list(node = node)
return(rpcpost(con, "getaddednodeinfo", pl))
}
}
#' RPC-JSON API: disconnectnode
#'
#' Immediately disconnects from the specified peer node.
#' Strictly one out of \code{address} and \code{nodeid} can be
#' provided to identify the node.
#'
#' @param con object of class \code{CONRPC}.
#' @param address \code{character} the IP address/port
#' of the node.
#' @param nodeid \code{character} The node ID
#' (see \code{getpeerinfo()} for node IDs).
#'
#' @return A S4-object of class \code{ANSRPC}.
#' @family Network RPCs
#' @author Bernhard Pfaff
#' @references
#' \url{https://bitcoin.org/en/developer-reference#disconnectnode},
#' \url{https://bitcoin.org/en/developer-reference#remote-procedure-calls-rpcs}
#' @name disconnectnode
#' @aliases disconnectnode
#' @rdname disconnectnode
#' @export
disconnectnode <- function(con, address = NULL, nodeid = NULL){
if (is.null(address) & is.null(nodeid)){
stop("Either 'address' or 'nodeid' must be provided.\n")
}
if (!is.null(address) & !is.null(nodeid)){
warning("Arguments 'address' and 'nodeid' provided, using 'address'.\n")
address <- as.character(address)
pl <- list(address = address)
}
if (is.null(address) & !is.null(nodeid)){
nodeid <- as.character(nodeid)
pl <- list(nodeid = nodeid)
}
if (!is.null(address) & is.null(nodeid)){
address <- as.character(address)
pl <- list(address = address)
}
rpcpost(con, "disconnectnode", pl)
}