An alternative full node implementation of LBRY's blockchain written in Go (golang)
Find a file
Dave Collins cb7c24141a Reimagine btcjson package with version 2.
This commit implements a reimagining of the way the btcjson package
functions based upon how the project has evolved and lessons learned while
using it since it was first written.  It therefore contains significant
changes to the API.  For now, it has been implemented in a v2 subdirectory
to prevent breaking existing callers, but the ultimate goal is to update
all callers to use the new version and then to replace the old API with
the new one.

This also removes the need for the btcws completely since those commands
have been rolled in.

The following is an overview of the changes and some reasoning behind why
they were made:

- The infrastructure has been completely changed to be reflection based instead
  of requiring thousands and thousands of lines of manual, and therefore error
  prone, marshal/unmarshal code
  - This makes it much easier to add new commands without making marshalling
    mistakes since it is simply a struct definition and a call to register that
    new struct (plus a trivial New<foo>Cmd function and tests, of course)
  - It also makes it much easier to gain a lot of information from simply
    looking at the struct definition which was previously not possible
    such as the order of the parameters, which parameters are required
    versus optional, and what the default values for optional parameters
    are
- Each command now has usage flags associated with them that can be
  queried which are intended to allow classification of the commands such
  as for chain server and wallet server and websocket-only
- The help infrastructure has been completely redone to provide automatic
  generation with caller provided description map and result types. This
  is in contrast to the previous method of providing the help directly
  which meant it would end up in the binary of anything that imported the
  package
- Many of the structs have been renamed to use the terminology from the
  JSON-RPC
  specification:
  - RawCmd/Message is now only a single struct named Request to reflect the fact
    it is a JSON-RPC request
  - Error is now called RPCError to reflect the fact it is specifically an RPC
    error as opposed to many of the other errors that are possible
    - All RPC error codes except the standard JSON-RPC 2.0 errors have been
      converted from full structs to only codes since an audit of the codebase
      has shown that the messages are overridden the vast majority of the time
      with specifics (as they should be) and removing them also avoids the
      temptation to return non-specific, and therefore not as helpful, error
      messages
  - There is now an Error which provides a type assertable error with
    error codes so callers can better ascertain failure reasons
    programatically
- The ID is no longer a part of the command and is instead specified at the time
  the command is marshalled into a JSON-RPC request.  This aligns better with
  the way JSON-RPC functions since it is the caller who manages the ID that is
  sent with any given _request_, not the package
- All <Foo>Cmd structs now treat non-pointers as required fields and pointers as
  optional fields
- All New<Foo>Cmd functions now accept the exact number of parameters, with
  pointers to the appropriate type for optional parameters
  - This is preferrable to the old vararg syntax since it means the code will
    fail to compile if the optional arguments are changed now which helps
    prevent errors creep in over time from missed modifications to optional args
- All of the connection related code has been completely eliminated since this
  package is not intended to used a client, rather it is intended to provide
  the infrastructure needed to marshal/unmarshal Bitcoin-specific JSON-RPC
  requests and replies from static types
  - The btcrpcclient package provides a robust client with connection management
    and higher-level types that in turn uses the primitives provided by this
    package
  - Even if the caller does not wish to use btcrpcclient for some reason, they
    should still be responsible for connection management since they might want
    to use any number of connection features which the package would not
    necessarily support
- Synced a few of the commands that have added new optional fields that
  have since been added to Bitcoin Core
- Includes all of the commands and notifications that were previously in
  btcws
- Now provides 100% test coverage with parallel tests
- The code is completely golint and go vet clean

This has the side effect of addressing nearly everything in, and therefore
closes #26.

Also fixes #18 and closes #19.
2015-02-19 00:41:07 -06:00
v2/btcjson Reimagine btcjson package with version 2. 2015-02-19 00:41:07 -06:00
.gitignore Initial commit. 2013-05-10 15:06:18 -05:00
.travis.yml Update Go versions for Travis. 2015-02-06 10:02:09 -05:00
cmdhelp.go Change searchrawtransaction to be compatible with bitcoind 2015-02-05 15:31:39 -08:00
CONTRIBUTORS Correct misspelled contributor name. 2014-05-05 22:15:37 -05:00
cov_report.sh Initial implementation. 2013-05-13 12:25:41 -04:00
doc.go Return pointers to structs 2014-05-05 17:18:22 -04:00
internal_test.go Minor golint cleanups. 2014-09-05 13:45:01 -04:00
jsonapi.go Change searchrawtransaction to be compatible with bitcoind 2015-02-05 15:31:39 -08:00
jsonapi_test.go Change searchrawtransaction to be compatible with bitcoind 2015-02-05 15:31:39 -08:00
jsoncmd.go Added new cmds importaddress, importpubkey 2015-02-07 11:09:13 +05:30
jsoncmd_test.go Added new cmds importaddress, importpubkey 2015-02-07 11:09:13 +05:30
jsonerr.go Properly set reply.Result to output of unmarshalled GetTxOutResult JSON. 2014-07-15 09:46:25 -04:00
jsonfxns.go Use the net package function for credentials. 2014-09-20 19:05:38 -05:00
jsonfxns_test.go Update btcjson import paths to new location. 2015-01-16 23:49:20 -06:00
jsonresults.go Extend getpeerinfo result with current block height. 2015-02-14 14:17:56 -08:00
jsonresults_test.go Change searchrawtransaction to be compatible with bitcoind 2015-02-05 15:31:39 -08:00
LICENSE Add 2014 to copyright dates. 2014-01-08 23:49:06 -06:00
README.md Reimagine btcjson package with version 2. 2015-02-19 00:41:07 -06:00
test_coverage.txt Add searchrawtransaction 2015-01-07 20:05:56 -08:00

btcjson

[Build Status] (https://travis-ci.org/btcsuite/btcjson)

Package btcjson implements concrete types for marshalling to and from the bitcoin JSON-RPC API. A comprehensive suite of tests is provided to ensure proper functionality. Package btcjson is licensed under the copyfree ISC license.

This package is one of the core packages from btcd, an alternative full-node implementation of bitcoin which is under active development by Conformal. Although it was primarily written for btcd, this package has intentionally been designed so it can be used as a standalone package for any projects needing to marshal to and from bitcoin JSON-RPC requests and responses.

Note that although it's possible to use this package directly to implement an RPC client, it is not recommended since it is only intended as an infrastructure package. Instead, RPC clients should use the btcrpcclient package which provides a full blown RPC client with many features such as automatic connection management, websocket support, automatic notification re-registration on reconnect, and conversion from the raw underlying RPC types (strings, floats, ints, etc) to higher-level types with many nice and useful properties.

JSON RPC

Bitcoin provides an extensive API call list to control bitcoind or bitcoin-qt through JSON-RPC. These can be used to get information from the client or to cause the client to perform some action.

The general form of the commands are:

	{"jsonrpc": "1.0", "id":"test", "method": "getinfo", "params": []}

btcjson provides code to easily create these commands from go (as some of the commands can be fairly complex), to send the commands to a running bitcoin RPC server, and to handle the replies (putting them in useful Go data structures).

Sample Use

	// Create a new command.
	cmd, err := btcjson.NewGetBlockCountCmd()
	if err != nil {
		// Handle error
	}

	// Marshal the command to a JSON-RPC formatted byte slice.
	marshalled, err := btcjson.MarshalCmd(id, cmd)
	if err != nil {
		// Handle error
	}

	// At this point marshalled contains the raw bytes that are ready to send
	// to the RPC server to issue the command.
	fmt.Printf("%s\n", marshalled)

Documentation

Full go doc style documentation for the project can be viewed online without installing this package by using the GoDoc site here.

You can also view the documentation locally once the package is installed with the godoc tool by running godoc -http=":6060" and pointing your browser to http://localhost:6060/pkg/github.com/btcsuite/btcjson

Installation

$ go get github.com/btcsuite/btcjson

GPG Verification Key

All official release tags are signed by Conformal so users can ensure the code has not been tampered with and is coming from Conformal. To verify the signature perform the following:

  • Download the public key from the Conformal website at https://opensource.conformal.com/GIT-GPG-KEY-conformal.txt

  • Import the public key into your GPG keyring:

    gpg --import GIT-GPG-KEY-conformal.txt
    
  • Verify the release tag with the following command where TAG_NAME is a placeholder for the specific tag:

    git tag -v TAG_NAME
    

License

Package btcjson is licensed under the copyfree ISC License.