git.gammaspectra.live/P2Pool/consensus/v3@v3.8.0/monero/client/rpc/daemon/jsonrpc.go (about) 1 package daemon 2 3 import ( 4 "context" 5 "fmt" 6 "git.gammaspectra.live/P2Pool/consensus/v3/types" 7 "git.gammaspectra.live/P2Pool/consensus/v3/utils" 8 ) 9 10 const ( 11 methodGenerateBlocks = "generateblocks" 12 methodGetAlternateChains = "get_alternate_chains" 13 methodGetBans = "get_bans" 14 methodGetBlock = "get_block" 15 methodGetBlockCount = "get_block_count" 16 methodGetBlockHeadersRange = "get_block_headers_range" 17 methodGetBlockHeaderByHash = "get_block_header_by_hash" 18 methodGetBlockHeaderByHeight = "get_block_header_by_height" 19 methodGetBlockTemplate = "get_block_template" 20 methodGetMinerData = "get_miner_data" 21 methodGetCoinbaseTxSum = "get_coinbase_tx_sum" 22 methodGetConnections = "get_connections" 23 methodGetFeeEstimate = "get_fee_estimate" 24 methodGetInfo = "get_info" 25 methodGetLastBlockHeader = "get_last_block_header" 26 methodGetVersion = "get_version" 27 methodHardForkInfo = "hard_fork_info" 28 methodOnGetBlockHash = "on_get_block_hash" 29 methodRPCAccessTracking = "rpc_access_tracking" 30 methodRelayTx = "relay_tx" 31 methodSetBans = "set_bans" 32 methodSyncInfo = "sync_info" 33 methodSubmitBlock = "submit_block" 34 ) 35 36 // GetAlternateChains displays alternative chains seen by the node. 37 // 38 // (restricted). 39 func (c *Client) GetAlternateChains( 40 ctx context.Context, 41 ) (*GetAlternateChainsResult, error) { 42 resp := &GetAlternateChainsResult{} 43 44 err := c.JSONRPC(ctx, methodGetAlternateChains, nil, resp) 45 if err != nil { 46 return nil, fmt.Errorf("jsonrpc: %w", err) 47 } 48 49 return resp, nil 50 } 51 52 // RPCAccessTracking retrieves statistics that the monero daemon keeps track of 53 // about the use of each RPC method and endpoint. 54 // 55 // (restricted). 56 func (c *Client) RPCAccessTracking( 57 ctx context.Context, 58 ) (*RPCAccessTrackingResult, error) { 59 resp := &RPCAccessTrackingResult{} 60 61 err := c.JSONRPC(ctx, methodRPCAccessTracking, nil, resp) 62 if err != nil { 63 return nil, fmt.Errorf("jsonrpc: %w", err) 64 } 65 66 return resp, nil 67 } 68 69 // HardForkInfo looks up informaiton about the last hard fork. 70 func (c *Client) HardForkInfo( 71 ctx context.Context, 72 ) (*HardForkInfoResult, error) { 73 resp := &HardForkInfoResult{} 74 75 err := c.JSONRPC(ctx, methodHardForkInfo, nil, resp) 76 if err != nil { 77 return nil, fmt.Errorf("jsonrpc: %w", err) 78 } 79 80 return resp, nil 81 } 82 83 // GetBans retrieves the list of banned IPs. 84 // 85 // (restricted). 86 func (c *Client) GetBans(ctx context.Context) (*GetBansResult, error) { 87 resp := &GetBansResult{} 88 89 err := c.JSONRPC(ctx, methodGetBans, nil, resp) 90 if err != nil { 91 return nil, fmt.Errorf("jsonrpc: %w", err) 92 } 93 94 return resp, nil 95 } 96 97 type SetBansBan struct { 98 Host string `json:"host"` 99 Ban bool `json:"ban"` 100 Seconds int64 `json:"seconds"` 101 } 102 103 type SetBansRequestParameters struct { 104 Bans []SetBansBan `json:"bans"` 105 } 106 107 // SetBans bans a particular host. 108 // 109 // (restricted). 110 func (c *Client) SetBans( 111 ctx context.Context, params SetBansRequestParameters, 112 ) (*SetBansResult, error) { 113 resp := &SetBansResult{} 114 115 err := c.JSONRPC(ctx, methodSetBans, params, resp) 116 if err != nil { 117 return nil, fmt.Errorf("jsonrpc: %w", err) 118 } 119 120 return resp, nil 121 } 122 123 // GetVersion retrieves the version of monerod that the node uses. 124 // 125 // (restricted). 126 func (c *Client) GetVersion(ctx context.Context) (*GetVersionResult, error) { 127 resp := &GetVersionResult{} 128 129 err := c.JSONRPC(ctx, methodGetVersion, nil, resp) 130 if err != nil { 131 return nil, fmt.Errorf("jsonrpc: %w", err) 132 } 133 134 return resp, nil 135 } 136 137 // GenerateBlocksRequestParameters is the set of parameters to be passed to the 138 // GenerateBlocks RPC method. 139 type GenerateBlocksRequestParameters struct { 140 // AmountOfBlocks is the number of blocks to be generated. 141 // 142 AmountOfBlocks uint64 `json:"amount_of_blocks,omitempty"` 143 144 // WalletAddress is the address of the wallet that will get the rewards 145 // of the coinbase transaction for such the blocks generates. 146 // 147 WalletAddress string `json:"wallet_address,omitempty"` 148 149 // PreviousBlock TODO 150 // 151 PreviousBlock string `json:"prev_block,omitempty"` 152 153 // StartingNonce TODO 154 // 155 StartingNonce uint32 `json:"starting_nonce,omitempty"` 156 } 157 158 // GenerateBlocks combines functionality from `GetBlockTemplate` and 159 // `SubmitBlock` RPC calls to allow rapid block creation. 160 // 161 // Difficulty is set permanently to 1 for regtest. 162 // 163 // (restricted). 164 func (c *Client) GenerateBlocks( 165 ctx context.Context, params GenerateBlocksRequestParameters, 166 ) (*GenerateBlocksResult, error) { 167 resp := &GenerateBlocksResult{} 168 169 err := c.JSONRPC(ctx, methodGenerateBlocks, params, resp) 170 if err != nil { 171 return nil, fmt.Errorf("jsonrpc: %w", err) 172 } 173 174 return resp, nil 175 } 176 177 func (c *Client) GetBlockCount( 178 ctx context.Context, 179 ) (*GetBlockCountResult, error) { 180 resp := &GetBlockCountResult{} 181 182 err := c.JSONRPC(ctx, methodGetBlockCount, nil, resp) 183 if err != nil { 184 return nil, fmt.Errorf("jsonrpc: %w", err) 185 } 186 187 return resp, nil 188 } 189 190 func (c *Client) OnGetBlockHash( 191 ctx context.Context, height uint64, 192 ) (types.Hash, error) { 193 resp := "" 194 params := []uint64{height} 195 196 err := c.JSONRPC(ctx, methodOnGetBlockHash, params, &resp) 197 if err != nil { 198 return types.ZeroHash, fmt.Errorf("jsonrpc: %w", err) 199 } 200 201 return types.HashFromString(resp) 202 } 203 204 func (c *Client) RelayTx( 205 ctx context.Context, txns []types.Hash, 206 ) (*RelayTxResult, error) { 207 resp := &RelayTxResult{} 208 params := map[string]interface{}{ 209 "txids": txns, 210 } 211 212 err := c.JSONRPC(ctx, methodRelayTx, params, resp) 213 if err != nil { 214 return nil, fmt.Errorf("jsonrpc: %w", err) 215 } 216 217 return resp, nil 218 } 219 220 // GetBlockTemplate gets a block template on which mining a new block. 221 func (c *Client) GetBlockTemplate( 222 ctx context.Context, walletAddress string, reserveSize uint, 223 ) (*GetBlockTemplateResult, error) { 224 resp := &GetBlockTemplateResult{} 225 params := map[string]interface{}{ 226 "wallet_address": walletAddress, 227 "reserve_size": reserveSize, 228 } 229 230 err := c.JSONRPC(ctx, methodGetBlockTemplate, params, resp) 231 if err != nil { 232 return nil, fmt.Errorf("jsonrpc: %w", err) 233 } 234 235 return resp, nil 236 } 237 238 func (c *Client) GetMinerData(ctx context.Context) (*GetMinerDataResult, error) { 239 resp := &GetMinerDataResult{} 240 241 err := c.JSONRPC(ctx, methodGetMinerData, nil, resp) 242 if err != nil { 243 return nil, fmt.Errorf("jsonrpc: %w", err) 244 } 245 246 return resp, nil 247 } 248 249 func (c *Client) SubmitBlock(ctx context.Context, blobs ...types.Bytes) (*SubmitBlockResult, error) { 250 resp := &SubmitBlockResult{} 251 252 err := c.JSONRPC(ctx, methodSubmitBlock, blobs, resp) 253 if err != nil { 254 return nil, fmt.Errorf("jsonrpc: %w", err) 255 } 256 257 return resp, nil 258 } 259 260 func (c *Client) GetConnections( 261 ctx context.Context, 262 ) (*GetConnectionsResult, error) { 263 resp := &GetConnectionsResult{} 264 265 err := c.JSONRPC(ctx, methodGetConnections, nil, resp) 266 if err != nil { 267 return nil, fmt.Errorf("jsonrpc: %w", err) 268 } 269 270 return resp, nil 271 } 272 273 // GetInfo retrieves general information about the state of the node and the 274 // network. 275 func (c *Client) GetInfo(ctx context.Context) (*GetInfoResult, error) { 276 resp := &GetInfoResult{} 277 278 if err := c.JSONRPC(ctx, methodGetInfo, nil, resp); err != nil { 279 return nil, fmt.Errorf("jsonrpc: %w", err) 280 } 281 282 return resp, nil 283 } 284 285 func (c *Client) GetLastBlockHeader( 286 ctx context.Context, 287 ) (*GetLastBlockHeaderResult, error) { 288 resp := &GetLastBlockHeaderResult{} 289 290 err := c.JSONRPC(ctx, methodGetLastBlockHeader, nil, resp) 291 if err != nil { 292 return nil, fmt.Errorf("jsonrpc: %w", err) 293 } 294 295 return resp, nil 296 } 297 298 func (c *Client) GetCoinbaseTxSum( 299 ctx context.Context, height, count uint64, 300 ) (*GetCoinbaseTxSumResult, error) { 301 resp := &GetCoinbaseTxSumResult{} 302 params := map[string]uint64{ 303 "height": height, 304 "count": count, 305 } 306 307 err := c.JSONRPC(ctx, methodGetCoinbaseTxSum, params, resp) 308 if err != nil { 309 return nil, fmt.Errorf("jsonrpc: %w", err) 310 } 311 312 return resp, nil 313 } 314 315 // InnerJSON parses the content of the JSON embedded in `GetBlockResult`. 316 func (j *GetBlockResult) InnerJSON() (*GetBlockResultJSON, error) { 317 res := &GetBlockResultJSON{} 318 319 err := utils.UnmarshalJSON([]byte(j.JSON), res) 320 if err != nil { 321 return nil, fmt.Errorf("unmarshal: %w", err) 322 } 323 324 return res, nil 325 } 326 327 func (c *Client) GetBlockHeadersRange( 328 ctx context.Context, start, end uint64, 329 ) (*GetBlockHeadersRangeResult, error) { 330 resp := &GetBlockHeadersRangeResult{} 331 params := map[string]interface{}{ 332 "start_height": start, 333 "end_height": end, 334 } 335 336 err := c.JSONRPC(ctx, methodGetBlockHeadersRange, params, resp) 337 if err != nil { 338 return nil, fmt.Errorf("jsonrpc: %w", err) 339 } 340 341 return resp, nil 342 } 343 344 // GetBlockHeaderByHeight retrieves block header information for either one or 345 // multiple blocks. 346 func (c *Client) GetBlockHeaderByHeight( 347 ctx context.Context, height uint64, 348 ) (*GetBlockHeaderByHeightResult, error) { 349 resp := &GetBlockHeaderByHeightResult{} 350 params := map[string]interface{}{ 351 "height": height, 352 } 353 354 err := c.JSONRPC(ctx, methodGetBlockHeaderByHeight, params, resp) 355 if err != nil { 356 return nil, fmt.Errorf("jsonrpc: %w", err) 357 } 358 359 return resp, nil 360 } 361 362 // GetBlockHeaderByHash retrieves block header information for either one or 363 // multiple blocks. 364 func (c *Client) GetBlockHeaderByHash( 365 ctx context.Context, hashes []types.Hash, 366 ) (*GetBlockHeaderByHashResult, error) { 367 resp := &GetBlockHeaderByHashResult{} 368 params := map[string]interface{}{ 369 "hashes": hashes, 370 } 371 372 err := c.JSONRPC(ctx, methodGetBlockHeaderByHash, params, resp) 373 if err != nil { 374 return nil, fmt.Errorf("jsonrpc: %w", err) 375 } 376 377 return resp, nil 378 } 379 380 // GetBlockRequestParameters represents the set of possible parameters that can 381 // be used for submitting a call to the `get_block` jsonrpc method. 382 type GetBlockRequestParameters struct { 383 Height uint64 `json:"height,omitempty"` 384 Hash types.Hash `json:"hash,omitempty"` 385 } 386 387 // GetBlock fetches full block information from a block at a particular hash OR 388 // height. 389 func (c *Client) GetBlock( 390 ctx context.Context, params GetBlockRequestParameters, 391 ) (*GetBlockResult, error) { 392 resp := &GetBlockResult{} 393 394 err := c.JSONRPC(ctx, methodGetBlock, params, resp) 395 if err != nil { 396 return nil, fmt.Errorf("jsonrpc: %w", err) 397 } 398 399 return resp, nil 400 } 401 402 func (c *Client) GetFeeEstimate( 403 ctx context.Context, graceBlocks uint64, 404 ) (*GetFeeEstimateResult, error) { 405 resp := &GetFeeEstimateResult{} 406 params := map[string]uint64{ 407 "grace_blocks": graceBlocks, 408 } 409 410 err := c.JSONRPC(ctx, methodGetFeeEstimate, params, resp) 411 if err != nil { 412 return nil, fmt.Errorf("jsonrpc: %w", err) 413 } 414 415 return resp, nil 416 } 417 418 func (c *Client) SyncInfo(ctx context.Context) (*SyncInfoResult, error) { 419 resp := &SyncInfoResult{} 420 421 err := c.JSONRPC(ctx, methodSyncInfo, nil, resp) 422 if err != nil { 423 return nil, fmt.Errorf("jsonrpc: %w", err) 424 } 425 426 return resp, nil 427 }