doc.go raw

   1  // Package btcjson provides primitives for working with the bitcoin JSON-RPC
   2  /*
   3  API.
   4  
   5  Overview
   6  
   7  When communicating via the JSON-RPC protocol, all of the commands need to be marshalled to and from the the wire in the
   8  appropriate format. This package provides data structures and primitives to ease this process. In addition, it also
   9  provides some additional features such as custom command registration, command categorization, and reflection-based help
  10  generation.
  11  
  12  JSON-RPC Protocol Overview
  13  
  14  This information is not necessary in order to use this package, but it does provide some intuition into what the
  15  marshalling and unmarshalling that is discussed below is doing under the hood. As defined by the JSON-RPC spec, there
  16  are effectively two forms of messages on the wire:
  17  
  18    - Request Objects
  19  
  20    {"jsonrpc":"1.0","id":"SOMEID","method":"SOMEMETHOD","netparams":[SOMEPARAMS]}
  21  
  22    NOTE: Notifications are the same format except the id field is null.
  23  
  24    - Response Objects
  25  
  26      {"result":SOMETHING,"error":null,"id":"SOMEID"}
  27  
  28      {"result":null,"error":{"code":SOMEINT,"message":SOMESTRING},"id":"SOMEID"}
  29  
  30  For requests, the netparams field can vary in what it contains depending on the method (a.k.a. command) being sent. Each
  31  parameter can be as simple as an int or a complex structure containing many nested fields. The id field is used to
  32  identify a request and will be included in the associated response. When working with asynchronous transports, such as
  33  websockets, spontaneous notifications are also possible. As indicated, they are the same as a request object, except
  34  they have the id field set to null. Therefore, servers will ignore requests with the id field set to null, while clients
  35  can choose to consume or ignore them.
  36  
  37  Unfortunately, the original Bitcoin JSON-RPC API (and hence anything compatible with it) doesn't always follow the spec
  38  and will sometimes return an error string in the result field with a null error for certain commands. However, for the
  39  most part, the error field will be set as described on failure.
  40  
  41  Marshalling and Unmarshalling
  42  
  43  Based upon the discussion above, it should be easy to see how the types of this package map into the required parts of
  44  the protocol
  45  
  46    - Request Objects (type Request)
  47  
  48      - Commands (type <Foo>Cmd)
  49  
  50      - Notifications (type <Foo>Ntfn)
  51  
  52    - Response Objects (type Response)
  53  
  54      - Result (type <Foo>Result)
  55  
  56  To simplify the marshalling of the requests and responses, the MarshalCmd and MarshalResponse functions are provided.
  57  They return the raw bytes ready to be sent across the wire.
  58  
  59  Unmarshalling a received Request object is a two step process:
  60  
  61    1) Unmarshal the raw bytes into a Request struct instance via json.Unmarshal
  62  
  63    2) Use UnmarshalCmd on the Result field of the unmarshalled Request to create a concrete command or notification
  64    instance with all struct fields set accordingly
  65  
  66  This approach is used since it provides the caller with access to the additional fields in the request that are not part
  67  of the command such as the ID. Unmarshalling a received Response object is also a two step process:
  68  
  69    1) Unmarhsal the raw bytes into a Response struct instance via json.Unmarshal
  70  
  71    2) Depending on the ID, unmarshal the Result field of the unmarshalled Response to create a concrete type instance
  72  
  73  As above, this approach is used since it provides the caller with access to the fields in the response such as the ID
  74  and BTCJSONError.
  75  
  76  Command Creation
  77  
  78  This package provides two approaches for creating a new command. This first, and preferred, method is to use one of the
  79  New<Foo>Cmd functions. This allows static compile-time checking to help ensure the parameters stay in sync with the
  80  struct definitions.
  81  
  82  The second approach is the NewCmd function which takes a method (command) name and variable arguments. The function
  83  includes full checking to ensure the parameters are accurate according to provided method, however these checks are,
  84  obviously, run-time which means any mistakes won't be found until the code is actually executed. However, it is quite
  85  useful for user-supplied commands that are intentionally dynamic.
  86  
  87  Custom Command Registration
  88  
  89  The command handling of this package is built around the concept of registered commands. This is true for the wide
  90  variety of commands already provided by the package, but it also means caller can easily provide custom commands with
  91  all of the same functionality as the built-in commands. Use the RegisterCmd function for this purpose.
  92  
  93  A list of all registered methods can be obtained with the RegisteredCmdMethods function.
  94  
  95  Command Inspection
  96  
  97  All registered commands are registered with flags that identify information such as whether the command applies to a
  98  chain server, wallet server, or is a notification along with the method name to use. These flags can be obtained with
  99  the MethodUsageFlags flags, and the method can be obtained with the CmdMethod function.
 100  
 101  Help Generation
 102  
 103  To facilitate providing consistent help to users of the RPC server, this package exposes the GenerateHelp and function
 104  which uses reflection on registered commands or notifications, as well as the provided expected result types, to
 105  generate the final help text.
 106  
 107  In addition, the MethodUsageText function is provided to generate consistent one-line usage for registered commands and
 108  notifications using reflection.
 109  
 110  Errors
 111  
 112  There are 2 distinct type of errors supported by this package:
 113  
 114  - General errors related to marshalling or unmarshalling or improper use of the package (type BTCJSONError)
 115  
 116  - RPC errors which are intended to be returned across the wire as a part of the JSON-RPC response (type RPCError)
 117  
 118  The first category of errors (type BTCJSONError) typically indicates a programmer error and can be avoided by properly
 119  using the API. Errors of this type will be returned from the various functions available in this package. They identify
 120  issues such as unsupported field types, attempts to register malformed commands, and attempting to create a new command
 121  with an improper number of parameters.
 122  
 123  The specific reason for the error can be detected by type asserting it to a *btcjson.BTCJSONError and accessing the
 124  ErrorCode field.
 125  
 126  The second category of errors (type RPCError), on the other hand, are useful for returning errors to RPC clients.
 127  Consequently, they are used in the previously described Response type.
 128  */
 129  package btcjson
 130