Compare commits
No commits in common. "master" and "v0.22.100-rc.0" have entirely different histories.
master
...
v0.22.100-
106 changed files with 1487 additions and 3845 deletions
2
.github/workflows/basic-check.yml
vendored
2
.github/workflows/basic-check.yml
vendored
|
@ -9,7 +9,7 @@ jobs:
|
|||
runs-on: ubuntu-latest
|
||||
strategy:
|
||||
matrix:
|
||||
go: [1.19]
|
||||
go: [1.18.2]
|
||||
steps:
|
||||
- name: Set up Go
|
||||
uses: actions/setup-go@v2
|
||||
|
|
2
.github/workflows/full-sync-part-1.yml
vendored
2
.github/workflows/full-sync-part-1.yml
vendored
|
@ -14,7 +14,7 @@ jobs:
|
|||
runs-on: self-hosted
|
||||
strategy:
|
||||
matrix:
|
||||
go: [1.19]
|
||||
go: [1.18.2]
|
||||
steps:
|
||||
- run: |
|
||||
echo "Note ${{ github.event.inputs.note }}!"
|
||||
|
|
2
.github/workflows/full-sync-part-2.yml
vendored
2
.github/workflows/full-sync-part-2.yml
vendored
|
@ -14,7 +14,7 @@ jobs:
|
|||
runs-on: self-hosted
|
||||
strategy:
|
||||
matrix:
|
||||
go: [1.19]
|
||||
go: [1.18.2]
|
||||
steps:
|
||||
- run: |
|
||||
echo "Note ${{ github.event.inputs.note }}!"
|
||||
|
|
2
.github/workflows/golangci-lint.yml
vendored
2
.github/workflows/golangci-lint.yml
vendored
|
@ -4,7 +4,7 @@ env:
|
|||
# go needs absolute directories, using the $HOME variable doesn't work here.
|
||||
GOCACHE: /home/runner/work/go/pkg/build
|
||||
GOPATH: /home/runner/work/go
|
||||
GO_VERSION: '^1.19'
|
||||
GO_VERSION: '^1.18.2'
|
||||
|
||||
on:
|
||||
push:
|
||||
|
|
2
.github/workflows/release.yml
vendored
2
.github/workflows/release.yml
vendored
|
@ -28,7 +28,7 @@ jobs:
|
|||
name: Set up Go
|
||||
uses: actions/setup-go@v2
|
||||
with:
|
||||
go-version: 1.19
|
||||
go-version: 1.18.2
|
||||
|
||||
# Login against a Docker registry except on PR
|
||||
# https://github.com/docker/login-action
|
||||
|
|
|
@ -17,7 +17,6 @@ builds:
|
|||
- -trimpath
|
||||
ldflags:
|
||||
- -s -w
|
||||
- -buildid=
|
||||
- -X github.com/lbryio/lbcd/version.appTag={{ .Tag }}
|
||||
targets:
|
||||
- linux_amd64
|
||||
|
@ -34,7 +33,6 @@ builds:
|
|||
- -trimpath
|
||||
ldflags:
|
||||
- -s -w
|
||||
- -buildid=
|
||||
- -X github.com/lbryio/lbcd/version.appTag={{ .Tag }}
|
||||
env:
|
||||
- CGO_ENABLED=0
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
|
||||
ARG ARCH=amd64
|
||||
|
||||
FROM golang:1.19 AS build-container
|
||||
FROM golang:1.18.2 AS build-container
|
||||
|
||||
ARG ARCH
|
||||
|
||||
|
|
398
README.md
398
README.md
|
@ -5,310 +5,17 @@
|
|||
[![ISC License](https://img.shields.io/badge/license-ISC-blue.svg)](http://copyfree.org)
|
||||
<!--[![GoDoc](https://img.shields.io/badge/godoc-reference-blue.svg)](https://pkg.go.dev/github.com/lbryio/lbcd)-->
|
||||
|
||||
**lbcd** is a full node implementation of LBRY's blockchain written in Go (golang).
|
||||
`lbcd` is a full node implementation of LBRY's blockchain written in Go (golang).
|
||||
|
||||
Software stack developed by LBRY teams has been all migrated to **lbcd**.
|
||||
This project is currently under active development and is in a Beta state while
|
||||
we ensure it matches LBRYcrd's functionality. The intention is that it properly
|
||||
downloads, validates, and serves the block chain using the exact rules
|
||||
(including consensus bugs) for block acceptance as LBRYcrd.
|
||||
We have taken great care to avoid lbcd causing a fork to the blockchain.
|
||||
|
||||
We're working with exchanges and pool oerators to migrate from **lbrycrd** to **lbcd**.
|
||||
|
||||
If you're integrating with **lbcd+lbcwallet**, please check the Wiki for current [supported RPCs](wiki/RPC-availability).
|
||||
|
||||
Note: **lbcd** does *NOT* include wallet functionality. That functionality is provided by the
|
||||
Note: `lbcd` does *NOT* include wallet functionality. That functionality is provided by the
|
||||
[lbcwallet](https://github.com/lbryio/lbcwallet) and the [LBRY SDK](https://github.com/lbryio/lbry-sdk).
|
||||
|
||||
## Requirements
|
||||
|
||||
All common operating systems are supported. lbcd requires at least 8GB of RAM
|
||||
and at least 100GB of disk storage. Both RAM and disk requirements increase slowly over time.
|
||||
Using a fast NVMe disk is recommended.
|
||||
|
||||
## Installation
|
||||
|
||||
Acquire binary files from [releases](https://github.com/lbryio/lbcd/releases)
|
||||
|
||||
For compilation, [Go](http://golang.org) 1.19 or newer is required.
|
||||
Install Go according to its [installation instructions](http://golang.org/doc/install).
|
||||
|
||||
``` sh
|
||||
# lbcd (full node)
|
||||
$ go install github.com/lbryio/lbcd@latest
|
||||
|
||||
# lbcctl (rpc client utility)
|
||||
$ go install github.com/lbryio/lbcd/cmd/lbcctl@latest
|
||||
```
|
||||
|
||||
## Usage
|
||||
|
||||
Default application folder `${LBCDDIR}`:
|
||||
|
||||
- Linux: `~/.lbcd/`
|
||||
- MacOS: `/Users/<username>/Library/Application Support/Lbcd/`
|
||||
|
||||
### Start the **lbcd**
|
||||
|
||||
``` sh
|
||||
./lbcd
|
||||
```
|
||||
|
||||
**lbcd** loads config file at `"${LBCDDIR}/lbcd.conf"`.
|
||||
|
||||
If no config is found, it creates a [default one](sample-lbcd.conf), which includes all available options with default settings except randomly generated *RPC credentials* (see below).
|
||||
|
||||
### RPC server
|
||||
|
||||
RPC credentials (`rpcuser` and `rpcpass`) is required to enable RPC server. It can be specify in the `"${LBCDDIR}/lbcd.conf"`, using command line options:
|
||||
|
||||
``` sh
|
||||
./lbcd --rpcuser=rpcuser --rpcpass=rpcpass
|
||||
|
||||
2022-07-28 12:28:19.627 [INF] RPCS: RPC server listening on 0.0.0.0:9245
|
||||
2022-07-28 12:28:19.627 [INF] RPCS: RPC server listening on [::]:9245
|
||||
```
|
||||
|
||||
### Working with TLS (Default)
|
||||
|
||||
By default, **lbcd** runs RPC server with TLS enabled, and generates the `rpc.cert` and `rpc.key` under `${LBCDDIR}`, if not exist already.
|
||||
|
||||
To interact with the RPC server, a client has to either specify the `rpc.cert`, or disable the certification verification for TLS.
|
||||
|
||||
Interact with **lbcd** RPC using `lbcctl`
|
||||
|
||||
``` sh
|
||||
$ ./lbcctl --rpccert "${LBCDDIR}/rpc.cert" getblockcount
|
||||
|
||||
# or disable the certificate verification
|
||||
$ ./lbcctl --skipverify getblockcount
|
||||
|
||||
1200062
|
||||
```
|
||||
|
||||
Interact with **lbcd** RPC using `curl`
|
||||
|
||||
``` sh
|
||||
$ curl --user rpcuser:rpcpass \
|
||||
--cacert "${LBCDDIR}/rpc.cert" \
|
||||
--data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockcount", "params": []}' \
|
||||
-H 'content-type: text/plain;' \
|
||||
https://127.0.0.1:9245/
|
||||
|
||||
# or disable the certificate verification
|
||||
$ curl --user rpcuser:rpcpass \
|
||||
--insecure \
|
||||
--data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockcount", "params": []}' \
|
||||
-H 'content-type: text/plain;' \
|
||||
https://127.0.0.1:9245/
|
||||
```
|
||||
|
||||
``` json
|
||||
{"jsonrpc":"1.0","result":1200062,"error":null,"id":"curltest"}
|
||||
```
|
||||
|
||||
### Working without TLS
|
||||
|
||||
TLS can be disabled using the `--notls` option:
|
||||
|
||||
``` sh
|
||||
$ ./lbcd --notls
|
||||
```
|
||||
|
||||
``` sh
|
||||
$ ./lbcctl --notls getblockcount
|
||||
|
||||
1200062
|
||||
```
|
||||
|
||||
``` sh
|
||||
$ curl --user rpcuser:rpcpass \
|
||||
--data-binary '{"jsonrpc": "1.0", "id": "curltest", "method": "getblockcount", "params": []}' \
|
||||
-H 'content-type: text/plain;' \
|
||||
http://127.0.0.1:9245/
|
||||
```
|
||||
|
||||
``` json
|
||||
{"jsonrpc":"1.0","result":1200062,"error":null,"id":"curltest"}
|
||||
```
|
||||
|
||||
## Using Snapshots (optional)
|
||||
|
||||
[Snapshots](https://snapshots.lbry.com/blockchain/) are created bi-weekly to help new users catch up current block height.
|
||||
|
||||
The snapshots are archived and compressed in [zstd](https://facebook.github.io/zstd/) format for it's compression ratio and speed.
|
||||
|
||||
Download the snapshot, and uncompress it:
|
||||
|
||||
``` sh
|
||||
time curl -O https://snapshots.lbry.com/blockchain/lbcd_snapshot_1199527_v0.22.105_2022-07-27.tar.zst
|
||||
zstd -d --stdout lbcd_snapshot_1199527_v0.22.105_2022-07-27.tar.zst | tar xf - -C "${LBCDDIR}"
|
||||
```
|
||||
|
||||
If preferred, a user can download and uncompress the snapshot on the fly:
|
||||
By the time the download is finished, the snapshots should be almost uncompressed already.
|
||||
|
||||
``` sh
|
||||
mkdir -p "${LBCDDIR}"
|
||||
|
||||
time curl https://snapshots.lbry.com/blockchain/lbcd_snapshot_1199527_v0.22.105_2022-07-27.tar.zst | zstd -d --stdout | tar xf - -C "${LBCDDIR}"
|
||||
|
||||
# % Total % Received % Xferd Average Speed Time Time Time Current
|
||||
# Dload Upload Total Spent Left Speed
|
||||
# 100 64.9G 100 64.9G 0 0 37.0M 0 0:29:49 0:29:49 --:--:-- 33.0M
|
||||
#
|
||||
# real 29m49.962s
|
||||
# user 6m53.710s
|
||||
# sys 8m56.545s
|
||||
```
|
||||
|
||||
## Working with RPCs
|
||||
|
||||
Using `lbcctl -l` to list available RPCs:
|
||||
|
||||
``` sh
|
||||
$ lbcctl -l
|
||||
|
||||
Chain Server Commands:
|
||||
addnode "addr" "add|remove|onetry"
|
||||
createrawtransaction [{"txid":"value","vout":n},...] {"address":amount,...} (locktime)
|
||||
debuglevel "levelspec"
|
||||
decoderawtransaction "hextx"
|
||||
decodescript "hexscript"
|
||||
deriveaddresses "descriptor" ({"value":value})
|
||||
fundrawtransaction "hextx" {"changeaddress":changeaddress,"changeposition":changeposition,"changetype":changetype,"includewatching":includewatching,"lockunspents":lockunspents,"feerate":feerate,"subtractfeefromoutputs":[subtractfeefromoutput,...],"replaceable":replaceable,"conftarget":conftarget,"estimatemode":estimatemode} (iswitness)
|
||||
generate numblocks
|
||||
|
||||
[skipped]
|
||||
|
||||
Wallet Server Commands (--wallet):
|
||||
addmultisigaddress nrequired ["key",...] ("account")
|
||||
addwitnessaddress "address"
|
||||
backupwallet "destination"
|
||||
createmultisig nrequired ["key",...]
|
||||
createnewaccount "account"
|
||||
createwallet "walletname" (disableprivatekeys=false blank=false passphrase="" avoidreuse=false)
|
||||
dumpprivkey "address"
|
||||
dumpwallet "filename"
|
||||
encryptwallet "passphrase"
|
||||
estimatefee numblocks
|
||||
estimatepriority numblocks
|
||||
estimatesmartfee conftarget (estimatemode="CONSERVATIVE")
|
||||
getaccount "address"
|
||||
getaccountaddress "account"
|
||||
getaddressesbyaccount "account"
|
||||
|
||||
[skipped]
|
||||
```
|
||||
|
||||
Using `lbcctl help rpcname` to show the RPC spec:
|
||||
|
||||
``` sh
|
||||
$ lbcctl help getblock
|
||||
|
||||
getblock "hash" (verbosity=1)
|
||||
|
||||
Returns information about a block given its hash.
|
||||
|
||||
Arguments:
|
||||
1. hash (string, required) The hash of the block
|
||||
2. verbosity (numeric, optional, default=1) Specifies whether the block data should be returned as a hex-encoded string (0), as parsed data with a slice of TXIDs (1), or as parsed data with parsed transaction data (2)
|
||||
|
||||
Result (verbosity=0):
|
||||
"value" (string) Hex-encoded bytes of the serialized block
|
||||
|
||||
Result (verbosity=1):
|
||||
{
|
||||
"getblockverboseresultbase": { (object)
|
||||
"hash": "value", (string) The hash of the block (same as provided)
|
||||
"confirmations": n, (numeric) The number of confirmations
|
||||
"strippedsize": n, (numeric) The size of the block without witness data
|
||||
"size": n, (numeric) The size of the block
|
||||
"weight": n, (numeric) The weight of the block
|
||||
"height": n, (numeric) The height of the block in the block chain
|
||||
"version": n, (numeric) The block version
|
||||
"versionHex": "value", (string) The block version in hexadecimal
|
||||
"merkleroot": "value", (string) Root hash of the merkle tree
|
||||
"time": n, (numeric) The block time in seconds since 1 Jan 1970 GMT
|
||||
"mediantime": n, (numeric) The median block time in seconds since 1 Jan 1970 GMT
|
||||
"nonce": n, (numeric) The block nonce
|
||||
"bits": "value", (string) The bits which represent the block difficulty
|
||||
"difficulty": n.nnn, (numeric) The proof-of-work difficulty as a multiple of the minimum difficulty
|
||||
"chainwork": "value", (string) Expected number of hashes required to produce the chain up to this block (in hex)
|
||||
"previousblockhash": "value", (string) The hash of the previous block
|
||||
"nextblockhash": "value", (string) The hash of the next block (only if there is one)
|
||||
"nameclaimroot": "value", (string) Root hash of the claim trie
|
||||
"nTx": n, (numeric) The number of transactions (aka, count of TX)
|
||||
},
|
||||
"tx": ["value",...], (array of string) The transaction hashes (only when verbosity=1)
|
||||
}
|
||||
```
|
||||
|
||||
## **lbcd** & **lbcwallet**
|
||||
|
||||
*Wallet* related functianlities and RPCs are provided by a separate programe - [**lbcwallet**](https://github.com/lbryio/lbcwallet).
|
||||
|
||||
Once setup, lbcwallet can serve wallet related RPCs as well as proxy lbcd RPCs to an assocated lbcd now.
|
||||
It's sufficient for user to connect just the **lbcwallet** instead of both.
|
||||
|
||||
``` mermaid
|
||||
sequenceDiagram
|
||||
actor C as lbcctl
|
||||
participant W as lbcwallet (port: 9244)
|
||||
participant D as lbcd (port: 9245)
|
||||
|
||||
rect rgb(200,200,200)
|
||||
Note over C,D: lbcctl getblockcount
|
||||
C ->>+ D: getblockcount
|
||||
D -->>- C: response
|
||||
end
|
||||
|
||||
rect rgb(200,200,200)
|
||||
Note over C,W: lbcctl --wallet balance
|
||||
C ->>+ W: getbalance
|
||||
W -->>- C: response
|
||||
end
|
||||
|
||||
rect rgb(200,200,200)
|
||||
Note over C,D: lbcctl --wallet getblockcount (lbcd RPC service proxied by lbcwallet)
|
||||
C ->>+ W: getblockcount
|
||||
W ->>+ D: getblockcount
|
||||
D -->>- W: response
|
||||
W -->>- C: response
|
||||
end
|
||||
```
|
||||
|
||||
While **lbcd** can run standalone as a full node, **lbcwallet** requires an associated **lbcd** instance for scanning and sync'ing block data.
|
||||
|
||||
``` mermaid
|
||||
sequenceDiagram
|
||||
participant W as lbcwallet (RPC port: 9244)
|
||||
participant D as lbcd (RPC port: 9245, P2P port: 9246)
|
||||
participant D2 as other lbcd node(s) (P2P port: 9246)
|
||||
|
||||
rect rgb(200,200,200)
|
||||
Note over W,D: Asynchronous websocket notifications
|
||||
W ->> D: subscribe to notifications
|
||||
D -->> W: notification
|
||||
D -->> W: notification
|
||||
end
|
||||
|
||||
rect rgb(200,200,200)
|
||||
Note over W,D: lbcd RPCs
|
||||
W ->>+ D: getblockheader
|
||||
D ->>- W: response
|
||||
end
|
||||
|
||||
rect rgb(200,200,200)
|
||||
Note over D,D2: P2P messages over port 9246
|
||||
D -->> D2: P2P message
|
||||
D2 -->> D: P2P message
|
||||
end
|
||||
|
||||
```
|
||||
|
||||
## Data integrity
|
||||
|
||||
**lbcd** is not immune to data loss. It expects a clean shutdown via SIGINT or
|
||||
SIGTERM. SIGKILL, immediate VM kills, and sudden power loss can cause data
|
||||
corruption, thus requiring chain resynchronization for recovery.
|
||||
|
||||
## Security
|
||||
|
||||
We take security seriously. Please contact [security](mailto:security@lbry.com) regarding any security issues.
|
||||
|
@ -317,6 +24,97 @@ Our PGP key is [here](https://lbry.com/faq/pgp-key) if you need it.
|
|||
We maintain a mailing list for notifications of upgrades, security issues,
|
||||
and soft/hard forks. To join, visit [fork list](https://lbry.com/forklist)
|
||||
|
||||
## Requirements
|
||||
|
||||
All common operating systems are supported. lbcd requires at least 8GB of RAM
|
||||
and at least 100GB of disk storage. Both RAM and disk requirements increase slowly over time.
|
||||
Using a fast NVMe disk is recommended.
|
||||
|
||||
`lbcd` is not immune to data loss. It expects a clean shutdown via SIGINT or
|
||||
SIGTERM. SIGKILL, immediate VM kills, and sudden power loss can cause data
|
||||
corruption, thus requiring chain resynchronization for recovery.
|
||||
|
||||
For compilation, [Go](http://golang.org) 1.16 or newer is required.
|
||||
|
||||
## Installation
|
||||
|
||||
Acquire binary files from [releases](https://github.com/lbryio/lbcd/releases)
|
||||
|
||||
### To build from Source on Linux/BSD/MacOSX/POSIX
|
||||
|
||||
Install Go according to its [installation instructions](http://golang.org/doc/install).
|
||||
|
||||
``` sh
|
||||
git clone https://github.com/lbryio/lbcd
|
||||
cd lbcd
|
||||
|
||||
# Build lbcd
|
||||
go build .
|
||||
|
||||
# Build lbcctl
|
||||
go build ./cmd/lbcctl
|
||||
```
|
||||
|
||||
Both [GoLand](https://www.jetbrains.com/go/)
|
||||
and [VS Code](https://code.visualstudio.com/docs/languages/go) IDEs are supported.
|
||||
|
||||
## Usage
|
||||
|
||||
By default, data and logs are stored in `<LBCDDIR>`:
|
||||
|
||||
- Linux: `~/.lbcd/`
|
||||
- MacOS: `/Users/<username>/Library/Application Support/Lbcd/`
|
||||
|
||||
To enable RPC access a username and password is required. Example:
|
||||
|
||||
``` sh
|
||||
./lbcd --rpcuser=rpcuser --rpcpass=rpcpass
|
||||
```
|
||||
|
||||
Interact with lbcd via RPC using `lbcctl`
|
||||
|
||||
``` sh
|
||||
./lbcctl --rpcuser=rpcuser --rpcpass=rpcpass getblockcount
|
||||
./lbcctl --rpcuser=rpcuser --rpcpass=rpcpass getblocktemplate
|
||||
```
|
||||
|
||||
By default, the RPCs are served over TLS. `lbcd` generates (if not exists) `rpc.cert` and
|
||||
`rpc.key` under `<LBCDDIR>` where `lbcctl` would search and use them.
|
||||
|
||||
The RPCs can also be served without TLS *(on localhost only)* using (`--notls`)
|
||||
|
||||
``` sh
|
||||
./lbcd --rpcuser=rpcuser --rpcpass=rpcpass --notls
|
||||
./lbcctl --rpcuser=rpcuser --rpcpass=rpcpass --notls getblockcount
|
||||
```
|
||||
|
||||
## Working with Different Networks
|
||||
|
||||
By default, `lbcd` and `lbcctl` use the following ports for different networks respectively:
|
||||
|
||||
| Network | RPC Port | Network Port |
|
||||
| ------- | -------- | ------------ |
|
||||
| mainnet | 9245 | 9246 |
|
||||
| testnet | 19245 | 19246 |
|
||||
| regtest | 29245 | 29246 |
|
||||
|
||||
Running `lbcd` and `lbcctl` with `--testnet` or `--regtest` would use different chain params as well as default RPC and Network ports.
|
||||
|
||||
``` sh
|
||||
./lbcd --rpcuser=rpcuser --rpcpass=rpcpass --regtest
|
||||
./lbcctl --rpcuser=rpcuser --rpcpass=rpcpass --regtest getblockcount
|
||||
```
|
||||
|
||||
The default Network and RPC ports of `lbcd` can be overriden using `--listen` and `--rpclisten`
|
||||
`lbcctl` can also connect to RPC server specified by `--rpcserver`
|
||||
|
||||
``` sh
|
||||
./lbcd --rpcuser=rpcuser --rpcpass=rpcpass --regtest --listen=127.0.0.1:29248 --rpclisten=127.0.0.1:29247
|
||||
./lbcctl --rpcuser=rpcuser --rpcpass=rpcpass --regtest --rpcserver=127.0.0.1:29247 getblockcount
|
||||
```
|
||||
|
||||
Note: Wallet related RPCs are provided by [lbcwallet](https://github.com/lbryio/lbcwallet).
|
||||
|
||||
## Contributing
|
||||
|
||||
Contributions to this project are welcome, encouraged, and compensated.
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
/*
|
||||
Package addrmgr implements concurrency safe Bitcoin address manager.
|
||||
|
||||
# Address Manager Overview
|
||||
Address Manager Overview
|
||||
|
||||
In order maintain the peer-to-peer Bitcoin network, there needs to be a source
|
||||
of addresses to connect to as nodes come and go. The Bitcoin protocol provides
|
||||
|
|
|
@ -8,7 +8,6 @@ package blockchain
|
|||
import (
|
||||
"container/list"
|
||||
"fmt"
|
||||
"math/big"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
|
@ -37,9 +36,8 @@ const (
|
|||
// from the block being located.
|
||||
//
|
||||
// For example, assume a block chain with a side chain as depicted below:
|
||||
//
|
||||
// genesis -> 1 -> 2 -> ... -> 15 -> 16 -> 17 -> 18
|
||||
// \-> 16a -> 17a
|
||||
// genesis -> 1 -> 2 -> ... -> 15 -> 16 -> 17 -> 18
|
||||
// \-> 16a -> 17a
|
||||
//
|
||||
// The block locator for block 17a would be the hashes of blocks:
|
||||
// [17a 16a 15 14 13 12 11 10 9 8 7 6 4 genesis]
|
||||
|
@ -489,7 +487,7 @@ func (b *BlockChain) calcSequenceLock(node *blockNode, tx *btcutil.Tx, utxoView
|
|||
// LockTimeToSequence converts the passed relative locktime to a sequence
|
||||
// number in accordance to BIP-68.
|
||||
// See: https://github.com/bitcoin/bips/blob/master/bip-0068.mediawiki
|
||||
// - (Compatibility)
|
||||
// * (Compatibility)
|
||||
func LockTimeToSequence(isSeconds bool, locktime uint32) uint32 {
|
||||
// If we're expressing the relative lock time in blocks, then the
|
||||
// corresponding sequence number is simply the desired input age.
|
||||
|
@ -1108,8 +1106,8 @@ func (b *BlockChain) reorganizeChain(detachNodes, attachNodes *list.List) error
|
|||
// a reorganization to become the main chain).
|
||||
//
|
||||
// The flags modify the behavior of this function as follows:
|
||||
// - BFFastAdd: Avoids several expensive transaction validation operations.
|
||||
// This is useful when using checkpoints.
|
||||
// - BFFastAdd: Avoids several expensive transaction validation operations.
|
||||
// This is useful when using checkpoints.
|
||||
//
|
||||
// This function MUST be called with the chain state lock held (for writes).
|
||||
func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block, flags BehaviorFlags) (bool, error) {
|
||||
|
@ -1250,8 +1248,8 @@ func (b *BlockChain) connectBestChain(node *blockNode, block *btcutil.Block, fla
|
|||
// isCurrent returns whether or not the chain believes it is current. Several
|
||||
// factors are used to guess, but the key factors that allow the chain to
|
||||
// believe it is current are:
|
||||
// - Latest block height is after the latest checkpoint (if enabled)
|
||||
// - Latest block has a timestamp newer than ~6 hours ago (as LBRY block time is one fourth of bitcoin)
|
||||
// - Latest block height is after the latest checkpoint (if enabled)
|
||||
// - Latest block has a timestamp newer than ~6 hours ago (as LBRY block time is one fourth of bitcoin)
|
||||
//
|
||||
// This function MUST be called with the chain state lock held (for reads).
|
||||
func (b *BlockChain) isCurrent() bool {
|
||||
|
@ -1274,8 +1272,8 @@ func (b *BlockChain) isCurrent() bool {
|
|||
// IsCurrent returns whether or not the chain believes it is current. Several
|
||||
// factors are used to guess, but the key factors that allow the chain to
|
||||
// believe it is current are:
|
||||
// - Latest block height is after the latest checkpoint (if enabled)
|
||||
// - Latest block has a timestamp newer than 24 hours ago
|
||||
// - Latest block height is after the latest checkpoint (if enabled)
|
||||
// - Latest block has a timestamp newer than 24 hours ago
|
||||
//
|
||||
// This function is safe for concurrent access.
|
||||
func (b *BlockChain) IsCurrent() bool {
|
||||
|
@ -1375,57 +1373,6 @@ func (b *BlockChain) BlockHashByHeight(blockHeight int32) (*chainhash.Hash, erro
|
|||
return &node.hash, nil
|
||||
}
|
||||
|
||||
// BlockAttributes desribes a Block in relation to others on the main chain.
|
||||
type BlockAttributes struct {
|
||||
Height int32
|
||||
Confirmations int32
|
||||
MedianTime time.Time
|
||||
ChainWork *big.Int
|
||||
PrevHash *chainhash.Hash
|
||||
NextHash *chainhash.Hash
|
||||
}
|
||||
|
||||
// BlockAttributesByHash returns BlockAttributes for the block with the given hash
|
||||
// relative to other blocks in the main chain. A BestState snapshot describing
|
||||
// the main chain is also returned for convenience.
|
||||
//
|
||||
// This function is safe for concurrent access.
|
||||
func (b *BlockChain) BlockAttributesByHash(hash *chainhash.Hash, prevHash *chainhash.Hash) (
|
||||
attrs *BlockAttributes, best *BestState, err error) {
|
||||
best = b.BestSnapshot()
|
||||
node := b.index.LookupNode(hash)
|
||||
if node == nil {
|
||||
str := fmt.Sprintf("block %s not found", hash)
|
||||
return nil, best, errNotInMainChain(str)
|
||||
}
|
||||
|
||||
attrs = &BlockAttributes{
|
||||
Height: node.height,
|
||||
Confirmations: 1 + best.Height - node.height,
|
||||
MedianTime: node.CalcPastMedianTime(),
|
||||
ChainWork: node.workSum,
|
||||
}
|
||||
if !b.bestChain.Contains(node) {
|
||||
attrs.Confirmations = -1
|
||||
}
|
||||
|
||||
// Populate prev block hash if there is one.
|
||||
if node.height > 0 {
|
||||
attrs.PrevHash = prevHash
|
||||
}
|
||||
|
||||
// Populate next block hash if there is one.
|
||||
if node.height < best.Height {
|
||||
nextHash, err := b.BlockHashByHeight(node.height + 1)
|
||||
if err != nil {
|
||||
return nil, best, err
|
||||
}
|
||||
attrs.NextHash = nextHash
|
||||
}
|
||||
|
||||
return attrs, best, nil
|
||||
}
|
||||
|
||||
// HeightRange returns a range of block hashes for the given start and end
|
||||
// heights. It is inclusive of the start height and exclusive of the end
|
||||
// height. The end height will be limited to the current main chain height.
|
||||
|
@ -1561,11 +1508,11 @@ func (b *BlockChain) IntervalBlockHashes(endHash *chainhash.Hash, interval int,
|
|||
//
|
||||
// In addition, there are two special cases:
|
||||
//
|
||||
// - When no locators are provided, the stop hash is treated as a request for
|
||||
// that block, so it will either return the node associated with the stop hash
|
||||
// if it is known, or nil if it is unknown
|
||||
// - When locators are provided, but none of them are known, nodes starting
|
||||
// after the genesis block will be returned
|
||||
// - When no locators are provided, the stop hash is treated as a request for
|
||||
// that block, so it will either return the node associated with the stop hash
|
||||
// if it is known, or nil if it is unknown
|
||||
// - When locators are provided, but none of them are known, nodes starting
|
||||
// after the genesis block will be returned
|
||||
//
|
||||
// This is primarily a helper function for the locateBlocks and locateHeaders
|
||||
// functions.
|
||||
|
@ -1649,11 +1596,11 @@ func (b *BlockChain) locateBlocks(locator BlockLocator, hashStop *chainhash.Hash
|
|||
//
|
||||
// In addition, there are two special cases:
|
||||
//
|
||||
// - When no locators are provided, the stop hash is treated as a request for
|
||||
// that block, so it will either return the stop hash itself if it is known,
|
||||
// or nil if it is unknown
|
||||
// - When locators are provided, but none of them are known, hashes starting
|
||||
// after the genesis block will be returned
|
||||
// - When no locators are provided, the stop hash is treated as a request for
|
||||
// that block, so it will either return the stop hash itself if it is known,
|
||||
// or nil if it is unknown
|
||||
// - When locators are provided, but none of them are known, hashes starting
|
||||
// after the genesis block will be returned
|
||||
//
|
||||
// This function is safe for concurrent access.
|
||||
func (b *BlockChain) LocateBlocks(locator BlockLocator, hashStop *chainhash.Hash, maxHashes uint32) []chainhash.Hash {
|
||||
|
@ -1694,11 +1641,11 @@ func (b *BlockChain) locateHeaders(locator BlockLocator, hashStop *chainhash.Has
|
|||
//
|
||||
// In addition, there are two special cases:
|
||||
//
|
||||
// - When no locators are provided, the stop hash is treated as a request for
|
||||
// that header, so it will either return the header for the stop hash itself
|
||||
// if it is known, or nil if it is unknown
|
||||
// - When locators are provided, but none of them are known, headers starting
|
||||
// after the genesis block will be returned
|
||||
// - When no locators are provided, the stop hash is treated as a request for
|
||||
// that header, so it will either return the header for the stop hash itself
|
||||
// if it is known, or nil if it is unknown
|
||||
// - When locators are provided, but none of them are known, headers starting
|
||||
// after the genesis block will be returned
|
||||
//
|
||||
// This function is safe for concurrent access.
|
||||
func (b *BlockChain) LocateHeaders(locator BlockLocator, hashStop *chainhash.Hash) []wire.BlockHeader {
|
||||
|
|
|
@ -36,13 +36,11 @@ func fastLog2Floor(n uint32) uint8 {
|
|||
// for comparing chains.
|
||||
//
|
||||
// For example, assume a block chain with a side chain as depicted below:
|
||||
//
|
||||
// genesis -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
|
||||
// \-> 4a -> 5a -> 6a
|
||||
// genesis -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
|
||||
// \-> 4a -> 5a -> 6a
|
||||
//
|
||||
// The chain view for the branch ending in 6a consists of:
|
||||
//
|
||||
// genesis -> 1 -> 2 -> 3 -> 4a -> 5a -> 6a
|
||||
// genesis -> 1 -> 2 -> 3 -> 4a -> 5a -> 6a
|
||||
type chainView struct {
|
||||
mtx sync.Mutex
|
||||
nodes []*blockNode
|
||||
|
@ -260,14 +258,12 @@ func (c *chainView) next(node *blockNode) *blockNode {
|
|||
// view.
|
||||
//
|
||||
// For example, assume a block chain with a side chain as depicted below:
|
||||
//
|
||||
// genesis -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
|
||||
// \-> 4a -> 5a -> 6a
|
||||
// genesis -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
|
||||
// \-> 4a -> 5a -> 6a
|
||||
//
|
||||
// Further, assume the view is for the longer chain depicted above. That is to
|
||||
// say it consists of:
|
||||
//
|
||||
// genesis -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
|
||||
// genesis -> 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8
|
||||
//
|
||||
// Invoking this function with block node 5 would return block node 6 while
|
||||
// invoking it with block node 5a would return nil since that node is not part
|
||||
|
@ -325,14 +321,12 @@ func (c *chainView) findFork(node *blockNode) *blockNode {
|
|||
// the chain view. It will return nil if there is no common block.
|
||||
//
|
||||
// For example, assume a block chain with a side chain as depicted below:
|
||||
//
|
||||
// genesis -> 1 -> 2 -> ... -> 5 -> 6 -> 7 -> 8
|
||||
// \-> 6a -> 7a
|
||||
// genesis -> 1 -> 2 -> ... -> 5 -> 6 -> 7 -> 8
|
||||
// \-> 6a -> 7a
|
||||
//
|
||||
// Further, assume the view is for the longer chain depicted above. That is to
|
||||
// say it consists of:
|
||||
//
|
||||
// genesis -> 1 -> 2 -> ... -> 5 -> 6 -> 7 -> 8.
|
||||
// genesis -> 1 -> 2 -> ... -> 5 -> 6 -> 7 -> 8.
|
||||
//
|
||||
// Invoking this function with block node 7a would return block node 5 while
|
||||
// invoking it with block node 7 would return itself since it is already part of
|
||||
|
|
|
@ -185,14 +185,14 @@ func isNonstandardTransaction(tx *btcutil.Tx) bool {
|
|||
// checkpoint candidate.
|
||||
//
|
||||
// The factors used to determine a good checkpoint are:
|
||||
// - The block must be in the main chain
|
||||
// - The block must be at least 'CheckpointConfirmations' blocks prior to the
|
||||
// current end of the main chain
|
||||
// - The timestamps for the blocks before and after the checkpoint must have
|
||||
// timestamps which are also before and after the checkpoint, respectively
|
||||
// (due to the median time allowance this is not always the case)
|
||||
// - The block must not contain any strange transaction such as those with
|
||||
// nonstandard scripts
|
||||
// - The block must be in the main chain
|
||||
// - The block must be at least 'CheckpointConfirmations' blocks prior to the
|
||||
// current end of the main chain
|
||||
// - The timestamps for the blocks before and after the checkpoint must have
|
||||
// timestamps which are also before and after the checkpoint, respectively
|
||||
// (due to the median time allowance this is not always the case)
|
||||
// - The block must not contain any strange transaction such as those with
|
||||
// nonstandard scripts
|
||||
//
|
||||
// The intent is that candidates are reviewed by a developer to make the final
|
||||
// decision and then manually added to the list of checkpoints for a network.
|
||||
|
|
|
@ -42,21 +42,18 @@ func HashToBig(hash *chainhash.Hash) *big.Int {
|
|||
// Like IEEE754 floating point, there are three basic components: the sign,
|
||||
// the exponent, and the mantissa. They are broken out as follows:
|
||||
//
|
||||
// - the most significant 8 bits represent the unsigned base 256 exponent
|
||||
// * the most significant 8 bits represent the unsigned base 256 exponent
|
||||
// * bit 23 (the 24th bit) represents the sign bit
|
||||
// * the least significant 23 bits represent the mantissa
|
||||
//
|
||||
// - bit 23 (the 24th bit) represents the sign bit
|
||||
//
|
||||
// - the least significant 23 bits represent the mantissa
|
||||
//
|
||||
// -------------------------------------------------
|
||||
// | Exponent | Sign | Mantissa |
|
||||
// -------------------------------------------------
|
||||
// | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] |
|
||||
// -------------------------------------------------
|
||||
// -------------------------------------------------
|
||||
// | Exponent | Sign | Mantissa |
|
||||
// -------------------------------------------------
|
||||
// | 8 bits [31-24] | 1 bit [23] | 23 bits [22-00] |
|
||||
// -------------------------------------------------
|
||||
//
|
||||
// The formula to calculate N is:
|
||||
//
|
||||
// N = (-1^sign) * mantissa * 256^(exponent-3)
|
||||
// N = (-1^sign) * mantissa * 256^(exponent-3)
|
||||
//
|
||||
// This compact form is only used in bitcoin to encode unsigned 256-bit numbers
|
||||
// which represent difficulty targets, thus there really is not a need for a
|
||||
|
|
|
@ -26,42 +26,42 @@ caller a high level of flexibility in how they want to react to certain events
|
|||
such as orphan blocks which need their parents requested and newly connected
|
||||
main chain blocks which might result in wallet updates.
|
||||
|
||||
# Bitcoin Chain Processing Overview
|
||||
Bitcoin Chain Processing Overview
|
||||
|
||||
Before a block is allowed into the block chain, it must go through an intensive
|
||||
series of validation rules. The following list serves as a general outline of
|
||||
those rules to provide some intuition into what is going on under the hood, but
|
||||
is by no means exhaustive:
|
||||
|
||||
- Reject duplicate blocks
|
||||
- Perform a series of sanity checks on the block and its transactions such as
|
||||
verifying proof of work, timestamps, number and character of transactions,
|
||||
transaction amounts, script complexity, and merkle root calculations
|
||||
- Compare the block against predetermined checkpoints for expected timestamps
|
||||
and difficulty based on elapsed time since the checkpoint
|
||||
- Save the most recent orphan blocks for a limited time in case their parent
|
||||
blocks become available
|
||||
- Stop processing if the block is an orphan as the rest of the processing
|
||||
depends on the block's position within the block chain
|
||||
- Perform a series of more thorough checks that depend on the block's position
|
||||
within the block chain such as verifying block difficulties adhere to
|
||||
difficulty retarget rules, timestamps are after the median of the last
|
||||
several blocks, all transactions are finalized, checkpoint blocks match, and
|
||||
block versions are in line with the previous blocks
|
||||
- Determine how the block fits into the chain and perform different actions
|
||||
accordingly in order to ensure any side chains which have higher difficulty
|
||||
than the main chain become the new main chain
|
||||
- When a block is being connected to the main chain (either through
|
||||
reorganization of a side chain to the main chain or just extending the
|
||||
main chain), perform further checks on the block's transactions such as
|
||||
verifying transaction duplicates, script complexity for the combination of
|
||||
connected scripts, coinbase maturity, double spends, and connected
|
||||
transaction values
|
||||
- Run the transaction scripts to verify the spender is allowed to spend the
|
||||
coins
|
||||
- Insert the block into the block database
|
||||
- Reject duplicate blocks
|
||||
- Perform a series of sanity checks on the block and its transactions such as
|
||||
verifying proof of work, timestamps, number and character of transactions,
|
||||
transaction amounts, script complexity, and merkle root calculations
|
||||
- Compare the block against predetermined checkpoints for expected timestamps
|
||||
and difficulty based on elapsed time since the checkpoint
|
||||
- Save the most recent orphan blocks for a limited time in case their parent
|
||||
blocks become available
|
||||
- Stop processing if the block is an orphan as the rest of the processing
|
||||
depends on the block's position within the block chain
|
||||
- Perform a series of more thorough checks that depend on the block's position
|
||||
within the block chain such as verifying block difficulties adhere to
|
||||
difficulty retarget rules, timestamps are after the median of the last
|
||||
several blocks, all transactions are finalized, checkpoint blocks match, and
|
||||
block versions are in line with the previous blocks
|
||||
- Determine how the block fits into the chain and perform different actions
|
||||
accordingly in order to ensure any side chains which have higher difficulty
|
||||
than the main chain become the new main chain
|
||||
- When a block is being connected to the main chain (either through
|
||||
reorganization of a side chain to the main chain or just extending the
|
||||
main chain), perform further checks on the block's transactions such as
|
||||
verifying transaction duplicates, script complexity for the combination of
|
||||
connected scripts, coinbase maturity, double spends, and connected
|
||||
transaction values
|
||||
- Run the transaction scripts to verify the spender is allowed to spend the
|
||||
coins
|
||||
- Insert the block into the block database
|
||||
|
||||
# Errors
|
||||
Errors
|
||||
|
||||
Errors returned by this package are either the raw errors provided by underlying
|
||||
calls or of type blockchain.RuleError. This allows the caller to differentiate
|
||||
|
@ -70,12 +70,12 @@ violations through type assertions. In addition, callers can programmatically
|
|||
determine the specific rule violation by examining the ErrorCode field of the
|
||||
type asserted blockchain.RuleError.
|
||||
|
||||
# Bitcoin Improvement Proposals
|
||||
Bitcoin Improvement Proposals
|
||||
|
||||
This package includes spec changes outlined by the following BIPs:
|
||||
|
||||
BIP0016 (https://en.bitcoin.it/wiki/BIP_0016)
|
||||
BIP0030 (https://en.bitcoin.it/wiki/BIP_0030)
|
||||
BIP0034 (https://en.bitcoin.it/wiki/BIP_0034)
|
||||
BIP0016 (https://en.bitcoin.it/wiki/BIP_0016)
|
||||
BIP0030 (https://en.bitcoin.it/wiki/BIP_0030)
|
||||
BIP0034 (https://en.bitcoin.it/wiki/BIP_0034)
|
||||
*/
|
||||
package blockchain
|
||||
|
|
|
@ -464,9 +464,9 @@ func createSpendTxForTx(tx *wire.MsgTx, fee btcutil.Amount) *wire.MsgTx {
|
|||
// - A coinbase that pays the required subsidy to an OP_TRUE script
|
||||
// - When a spendable output is provided:
|
||||
// - A transaction that spends from the provided output the following outputs:
|
||||
// - One that pays the inputs amount minus 1 atom to an OP_TRUE script
|
||||
// - One that contains an OP_RETURN output with a random uint64 in order to
|
||||
// ensure the transaction has a unique hash
|
||||
// - One that pays the inputs amount minus 1 atom to an OP_TRUE script
|
||||
// - One that contains an OP_RETURN output with a random uint64 in order to
|
||||
// ensure the transaction has a unique hash
|
||||
//
|
||||
// Additionally, if one or more munge functions are specified, they will be
|
||||
// invoked with the block prior to solving it. This provides callers with the
|
||||
|
|
|
@ -27,9 +27,8 @@ type blockProgressLogger struct {
|
|||
|
||||
// newBlockProgressLogger returns a new block progress logger.
|
||||
// The progress message is templated as follows:
|
||||
//
|
||||
// {progressAction} {numProcessed} {blocks|block} in the last {timePeriod}
|
||||
// ({numTxs}, height {lastBlockHeight}, {lastBlockTimeStamp})
|
||||
// {progressAction} {numProcessed} {blocks|block} in the last {timePeriod}
|
||||
// ({numTxs}, height {lastBlockHeight}, {lastBlockTimeStamp})
|
||||
func newBlockProgressLogger(progressMessage string, logger btclog.Logger) *blockProgressLogger {
|
||||
return &blockProgressLogger{
|
||||
lastBlockLogTime: time.Now(),
|
||||
|
|
|
@ -11,7 +11,6 @@ import (
|
|||
|
||||
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
||||
"github.com/lbryio/lbcd/txscript"
|
||||
"github.com/lbryio/lbcd/wire"
|
||||
btcutil "github.com/lbryio/lbcutil"
|
||||
)
|
||||
|
||||
|
@ -87,7 +86,7 @@ func HashMerkleBranches(left *chainhash.Hash, right *chainhash.Hash) *chainhash.
|
|||
//
|
||||
// The above stored as a linear array is as follows:
|
||||
//
|
||||
// [h1 h2 h3 h4 h12 h34 root]
|
||||
// [h1 h2 h3 h4 h12 h34 root]
|
||||
//
|
||||
// As the above shows, the merkle root is always the last element in the array.
|
||||
//
|
||||
|
@ -228,20 +227,6 @@ func ValidateWitnessCommitment(blk *btcutil.Block) error {
|
|||
// coinbase transaction MUST have exactly one witness element within
|
||||
// its witness data and that element must be exactly
|
||||
// CoinbaseWitnessDataLen bytes.
|
||||
//
|
||||
// Some popular pool software, for example yiimp, uses pre-BIP0141
|
||||
// coinbase struture. In this case, we don't just accept it, but also
|
||||
// turn it into post-BIP0141 format.
|
||||
if len(coinbaseTx.MsgTx().TxIn[0].Witness) == 0 {
|
||||
log.Infof("pre-BIP0141 coinbase transaction detected. Height: %d", blk.Height())
|
||||
|
||||
var witnessNonce [CoinbaseWitnessDataLen]byte
|
||||
coinbaseTx.MsgTx().TxIn[0].Witness = wire.TxWitness{witnessNonce[:]}
|
||||
blk.MsgBlock().Transactions[0].TxIn[0].Witness = wire.TxWitness{witnessNonce[:]}
|
||||
|
||||
// Clear cached serialized block.
|
||||
blk.SetBytes(nil)
|
||||
}
|
||||
coinbaseWitness := coinbaseTx.MsgTx().TxIn[0].Witness
|
||||
if len(coinbaseWitness) != 1 {
|
||||
str := fmt.Sprintf("the coinbase transaction has %d items in "+
|
||||
|
|
|
@ -50,9 +50,9 @@ func (n NotificationType) String() string {
|
|||
// Notification defines notification that is sent to the caller via the callback
|
||||
// function provided during the call to New and consists of a notification type
|
||||
// as well as associated data that depends on the type as follows:
|
||||
// - NTBlockAccepted: *btcutil.Block
|
||||
// - NTBlockConnected: *btcutil.Block
|
||||
// - NTBlockDisconnected: *btcutil.Block
|
||||
// - NTBlockAccepted: *btcutil.Block
|
||||
// - NTBlockConnected: *btcutil.Block
|
||||
// - NTBlockDisconnected: *btcutil.Block
|
||||
type Notification struct {
|
||||
Type NotificationType
|
||||
Data interface{}
|
||||
|
|
|
@ -232,25 +232,24 @@ func determineMainChainBlocks(blocksMap map[chainhash.Hash]*blockChainContext, t
|
|||
//
|
||||
// The legacy format is as follows:
|
||||
//
|
||||
// <version><height><header code><unspentness bitmap>[<compressed txouts>,...]
|
||||
// <version><height><header code><unspentness bitmap>[<compressed txouts>,...]
|
||||
//
|
||||
// Field Type Size
|
||||
// version VLQ variable
|
||||
// block height VLQ variable
|
||||
// header code VLQ variable
|
||||
// unspentness bitmap []byte variable
|
||||
// compressed txouts
|
||||
// compressed amount VLQ variable
|
||||
// compressed script []byte variable
|
||||
// Field Type Size
|
||||
// version VLQ variable
|
||||
// block height VLQ variable
|
||||
// header code VLQ variable
|
||||
// unspentness bitmap []byte variable
|
||||
// compressed txouts
|
||||
// compressed amount VLQ variable
|
||||
// compressed script []byte variable
|
||||
//
|
||||
// The serialized header code format is:
|
||||
//
|
||||
// bit 0 - containing transaction is a coinbase
|
||||
// bit 1 - output zero is unspent
|
||||
// bit 2 - output one is unspent
|
||||
// bits 3-x - number of bytes in unspentness bitmap. When both bits 1 and 2
|
||||
// are unset, it encodes N-1 since there must be at least one unspent
|
||||
// output.
|
||||
// bit 0 - containing transaction is a coinbase
|
||||
// bit 1 - output zero is unspent
|
||||
// bit 2 - output one is unspent
|
||||
// bits 3-x - number of bytes in unspentness bitmap. When both bits 1 and 2
|
||||
// are unset, it encodes N-1 since there must be at least one unspent
|
||||
// output.
|
||||
//
|
||||
// The rationale for the header code scheme is as follows:
|
||||
// - Transactions which only pay to a single output and a change output are
|
||||
|
@ -270,65 +269,65 @@ func determineMainChainBlocks(blocksMap map[chainhash.Hash]*blockChainContext, t
|
|||
// From tx in main blockchain:
|
||||
// Blk 1, 0e3e2357e806b6cdb1f70b54c3a3a17b6714ee1f0e68bebb44a74b1efd512098
|
||||
//
|
||||
// 010103320496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52
|
||||
// <><><><------------------------------------------------------------------>
|
||||
// | | \--------\ |
|
||||
// | height | compressed txout 0
|
||||
// version header code
|
||||
// 010103320496b538e853519c726a2c91e61ec11600ae1390813a627c66fb8be7947be63c52
|
||||
// <><><><------------------------------------------------------------------>
|
||||
// | | \--------\ |
|
||||
// | height | compressed txout 0
|
||||
// version header code
|
||||
//
|
||||
// - version: 1
|
||||
// - height: 1
|
||||
// - header code: 0x03 (coinbase, output zero unspent, 0 bytes of unspentness)
|
||||
// - unspentness: Nothing since it is zero bytes
|
||||
// - compressed txout 0:
|
||||
// - 0x32: VLQ-encoded compressed amount for 5000000000 (50 BTC)
|
||||
// - 0x04: special script type pay-to-pubkey
|
||||
// - 0x96...52: x-coordinate of the pubkey
|
||||
// - version: 1
|
||||
// - height: 1
|
||||
// - header code: 0x03 (coinbase, output zero unspent, 0 bytes of unspentness)
|
||||
// - unspentness: Nothing since it is zero bytes
|
||||
// - compressed txout 0:
|
||||
// - 0x32: VLQ-encoded compressed amount for 5000000000 (50 BTC)
|
||||
// - 0x04: special script type pay-to-pubkey
|
||||
// - 0x96...52: x-coordinate of the pubkey
|
||||
//
|
||||
// Example 2:
|
||||
// From tx in main blockchain:
|
||||
// Blk 113931, 4a16969aa4764dd7507fc1de7f0baa4850a246de90c45e59a3207f9a26b5036f
|
||||
//
|
||||
// 0185f90b0a011200e2ccd6ec7c6e2e581349c77e067385fa8236bf8a800900b8025be1b3efc63b0ad48e7f9f10e87544528d58
|
||||
// <><----><><><------------------------------------------><-------------------------------------------->
|
||||
// | | | \-------------------\ | |
|
||||
// version | \--------\ unspentness | compressed txout 2
|
||||
// height header code compressed txout 0
|
||||
// 0185f90b0a011200e2ccd6ec7c6e2e581349c77e067385fa8236bf8a800900b8025be1b3efc63b0ad48e7f9f10e87544528d58
|
||||
// <><----><><><------------------------------------------><-------------------------------------------->
|
||||
// | | | \-------------------\ | |
|
||||
// version | \--------\ unspentness | compressed txout 2
|
||||
// height header code compressed txout 0
|
||||
//
|
||||
// - version: 1
|
||||
// - height: 113931
|
||||
// - header code: 0x0a (output zero unspent, 1 byte in unspentness bitmap)
|
||||
// - unspentness: [0x01] (bit 0 is set, so output 0+2 = 2 is unspent)
|
||||
// NOTE: It's +2 since the first two outputs are encoded in the header code
|
||||
// - compressed txout 0:
|
||||
// - 0x12: VLQ-encoded compressed amount for 20000000 (0.2 BTC)
|
||||
// - 0x00: special script type pay-to-pubkey-hash
|
||||
// - 0xe2...8a: pubkey hash
|
||||
// - compressed txout 2:
|
||||
// - 0x8009: VLQ-encoded compressed amount for 15000000 (0.15 BTC)
|
||||
// - 0x00: special script type pay-to-pubkey-hash
|
||||
// - 0xb8...58: pubkey hash
|
||||
// - version: 1
|
||||
// - height: 113931
|
||||
// - header code: 0x0a (output zero unspent, 1 byte in unspentness bitmap)
|
||||
// - unspentness: [0x01] (bit 0 is set, so output 0+2 = 2 is unspent)
|
||||
// NOTE: It's +2 since the first two outputs are encoded in the header code
|
||||
// - compressed txout 0:
|
||||
// - 0x12: VLQ-encoded compressed amount for 20000000 (0.2 BTC)
|
||||
// - 0x00: special script type pay-to-pubkey-hash
|
||||
// - 0xe2...8a: pubkey hash
|
||||
// - compressed txout 2:
|
||||
// - 0x8009: VLQ-encoded compressed amount for 15000000 (0.15 BTC)
|
||||
// - 0x00: special script type pay-to-pubkey-hash
|
||||
// - 0xb8...58: pubkey hash
|
||||
//
|
||||
// Example 3:
|
||||
// From tx in main blockchain:
|
||||
// Blk 338156, 1b02d1c8cfef60a189017b9a420c682cf4a0028175f2f563209e4ff61c8c3620
|
||||
//
|
||||
// 0193d06c100000108ba5b9e763011dd46a006572d820e448e12d2bbb38640bc718e6
|
||||
// <><----><><----><-------------------------------------------------->
|
||||
// | | | \-----------------\ |
|
||||
// version | \--------\ unspentness |
|
||||
// height header code compressed txout 22
|
||||
// 0193d06c100000108ba5b9e763011dd46a006572d820e448e12d2bbb38640bc718e6
|
||||
// <><----><><----><-------------------------------------------------->
|
||||
// | | | \-----------------\ |
|
||||
// version | \--------\ unspentness |
|
||||
// height header code compressed txout 22
|
||||
//
|
||||
// - version: 1
|
||||
// - height: 338156
|
||||
// - header code: 0x10 (2+1 = 3 bytes in unspentness bitmap)
|
||||
// NOTE: It's +1 since neither bit 1 nor 2 are set, so N-1 is encoded.
|
||||
// - unspentness: [0x00 0x00 0x10] (bit 20 is set, so output 20+2 = 22 is unspent)
|
||||
// NOTE: It's +2 since the first two outputs are encoded in the header code
|
||||
// - compressed txout 22:
|
||||
// - 0x8ba5b9e763: VLQ-encoded compressed amount for 366875659 (3.66875659 BTC)
|
||||
// - 0x01: special script type pay-to-script-hash
|
||||
// - 0x1d...e6: script hash
|
||||
// - version: 1
|
||||
// - height: 338156
|
||||
// - header code: 0x10 (2+1 = 3 bytes in unspentness bitmap)
|
||||
// NOTE: It's +1 since neither bit 1 nor 2 are set, so N-1 is encoded.
|
||||
// - unspentness: [0x00 0x00 0x10] (bit 20 is set, so output 20+2 = 22 is unspent)
|
||||
// NOTE: It's +2 since the first two outputs are encoded in the header code
|
||||
// - compressed txout 22:
|
||||
// - 0x8ba5b9e763: VLQ-encoded compressed amount for 366875659 (3.66875659 BTC)
|
||||
// - 0x01: special script type pay-to-script-hash
|
||||
// - 0x1d...e6: script hash
|
||||
func deserializeUtxoEntryV0(serialized []byte) (map[uint32]*UtxoEntry, error) {
|
||||
// Deserialize the version.
|
||||
//
|
||||
|
|
|
@ -334,8 +334,8 @@ func CheckTransactionSanity(tx *btcutil.Tx, enforceSoftFork bool) error {
|
|||
// target difficulty as claimed.
|
||||
//
|
||||
// The flags modify the behavior of this function as follows:
|
||||
// - BFNoPoWCheck: The check to ensure the block hash is less than the target
|
||||
// difficulty is not performed.
|
||||
// - BFNoPoWCheck: The check to ensure the block hash is less than the target
|
||||
// difficulty is not performed.
|
||||
func checkProofOfWork(header *wire.BlockHeader, powLimit *big.Int, flags BehaviorFlags) error {
|
||||
// The target difficulty must be larger than zero.
|
||||
target := CompactToBig(header.Bits)
|
||||
|
@ -669,8 +669,8 @@ func checkSerializedHeight(coinbaseTx *btcutil.Tx, wantHeight int32) error {
|
|||
// which depend on its position within the block chain.
|
||||
//
|
||||
// The flags modify the behavior of this function as follows:
|
||||
// - BFFastAdd: All checks except those involving comparing the header against
|
||||
// the checkpoints are not performed.
|
||||
// - BFFastAdd: All checks except those involving comparing the header against
|
||||
// the checkpoints are not performed.
|
||||
//
|
||||
// This function MUST be called with the chain state lock held (for writes).
|
||||
func (b *BlockChain) checkBlockHeaderContext(header *wire.BlockHeader, prevNode *blockNode, flags BehaviorFlags) error {
|
||||
|
@ -748,8 +748,8 @@ func (b *BlockChain) checkBlockHeaderContext(header *wire.BlockHeader, prevNode
|
|||
// on its position within the block chain.
|
||||
//
|
||||
// The flags modify the behavior of this function as follows:
|
||||
// - BFFastAdd: The transaction are not checked to see if they are finalized
|
||||
// and the somewhat expensive BIP0034 validation is not performed.
|
||||
// - BFFastAdd: The transaction are not checked to see if they are finalized
|
||||
// and the somewhat expensive BIP0034 validation is not performed.
|
||||
//
|
||||
// The flags are also passed to checkBlockHeaderContext. See its documentation
|
||||
// for how the flags modify its behavior.
|
||||
|
|
|
@ -527,7 +527,7 @@ type baseMultTest struct {
|
|||
x, y string
|
||||
}
|
||||
|
||||
// TODO: add more test vectors
|
||||
//TODO: add more test vectors
|
||||
var s256BaseMultTests = []baseMultTest{
|
||||
{
|
||||
"AA5E28D6A97A2479A65527F7290311A3624D4CC0FA1578598EE3C2613BF99522",
|
||||
|
@ -556,7 +556,7 @@ var s256BaseMultTests = []baseMultTest{
|
|||
},
|
||||
}
|
||||
|
||||
// TODO: test different curves as well?
|
||||
//TODO: test different curves as well?
|
||||
func TestBaseMult(t *testing.T) {
|
||||
s256 := S256()
|
||||
for i, e := range s256BaseMultTests {
|
||||
|
|
|
@ -125,30 +125,27 @@ var (
|
|||
// the arithmetic needed for elliptic curve operations.
|
||||
//
|
||||
// The following depicts the internal representation:
|
||||
//
|
||||
// -----------------------------------------------------------------
|
||||
// | n[9] | n[8] | ... | n[0] |
|
||||
// | 32 bits available | 32 bits available | ... | 32 bits available |
|
||||
// | 22 bits for value | 26 bits for value | ... | 26 bits for value |
|
||||
// | 10 bits overflow | 6 bits overflow | ... | 6 bits overflow |
|
||||
// | Mult: 2^(26*9) | Mult: 2^(26*8) | ... | Mult: 2^(26*0) |
|
||||
// -----------------------------------------------------------------
|
||||
// -----------------------------------------------------------------
|
||||
// | n[9] | n[8] | ... | n[0] |
|
||||
// | 32 bits available | 32 bits available | ... | 32 bits available |
|
||||
// | 22 bits for value | 26 bits for value | ... | 26 bits for value |
|
||||
// | 10 bits overflow | 6 bits overflow | ... | 6 bits overflow |
|
||||
// | Mult: 2^(26*9) | Mult: 2^(26*8) | ... | Mult: 2^(26*0) |
|
||||
// -----------------------------------------------------------------
|
||||
//
|
||||
// For example, consider the number 2^49 + 1. It would be represented as:
|
||||
//
|
||||
// n[0] = 1
|
||||
// n[1] = 2^23
|
||||
// n[2..9] = 0
|
||||
// n[0] = 1
|
||||
// n[1] = 2^23
|
||||
// n[2..9] = 0
|
||||
//
|
||||
// The full 256-bit value is then calculated by looping i from 9..0 and
|
||||
// doing sum(n[i] * 2^(26i)) like so:
|
||||
//
|
||||
// n[9] * 2^(26*9) = 0 * 2^234 = 0
|
||||
// n[8] * 2^(26*8) = 0 * 2^208 = 0
|
||||
// ...
|
||||
// n[1] * 2^(26*1) = 2^23 * 2^26 = 2^49
|
||||
// n[0] * 2^(26*0) = 1 * 2^0 = 1
|
||||
// Sum: 0 + 0 + ... + 2^49 + 1 = 2^49 + 1
|
||||
// n[9] * 2^(26*9) = 0 * 2^234 = 0
|
||||
// n[8] * 2^(26*8) = 0 * 2^208 = 0
|
||||
// ...
|
||||
// n[1] * 2^(26*1) = 2^23 * 2^26 = 2^49
|
||||
// n[0] * 2^(26*0) = 1 * 2^0 = 1
|
||||
// Sum: 0 + 0 + ... + 2^49 + 1 = 2^49 + 1
|
||||
type fieldVal struct {
|
||||
n [10]uint32
|
||||
}
|
||||
|
|
|
@ -48,15 +48,6 @@ func NewAddNodeCmd(addr string, subCmd AddNodeSubCmd) *AddNodeCmd {
|
|||
}
|
||||
}
|
||||
|
||||
// ClearBannedCmd defines the clearbanned JSON-RPC command.
|
||||
type ClearBannedCmd struct{}
|
||||
|
||||
// NewClearBannedCmd returns a new instance which can be used to issue an clearbanned
|
||||
// JSON-RPC command.
|
||||
func NewClearBannedCmd() *ClearBannedCmd {
|
||||
return &ClearBannedCmd{}
|
||||
}
|
||||
|
||||
// TransactionInput represents the inputs to a transaction. Specifically a
|
||||
// transaction hash and output number pair.
|
||||
type TransactionInput struct {
|
||||
|
@ -67,7 +58,7 @@ type TransactionInput struct {
|
|||
// CreateRawTransactionCmd defines the createrawtransaction JSON-RPC command.
|
||||
type CreateRawTransactionCmd struct {
|
||||
Inputs []TransactionInput
|
||||
Outputs map[string]interface{} `jsonrpcusage:"{\"address\":amount, \"data\":\"hex\", ...}"`
|
||||
Amounts map[string]float64 `jsonrpcusage:"{\"address\":amount,...}"` // In BTC
|
||||
LockTime *int64
|
||||
}
|
||||
|
||||
|
@ -76,7 +67,7 @@ type CreateRawTransactionCmd struct {
|
|||
//
|
||||
// Amounts are in BTC. Passing in nil and the empty slice as inputs is equivalent,
|
||||
// both gets interpreted as the empty slice.
|
||||
func NewCreateRawTransactionCmd(inputs []TransactionInput, outputs map[string]interface{},
|
||||
func NewCreateRawTransactionCmd(inputs []TransactionInput, amounts map[string]float64,
|
||||
lockTime *int64) *CreateRawTransactionCmd {
|
||||
// to make sure we're serializing this to the empty list and not null, we
|
||||
// explicitly initialize the list
|
||||
|
@ -85,7 +76,7 @@ func NewCreateRawTransactionCmd(inputs []TransactionInput, outputs map[string]in
|
|||
}
|
||||
return &CreateRawTransactionCmd{
|
||||
Inputs: inputs,
|
||||
Outputs: outputs,
|
||||
Amounts: amounts,
|
||||
LockTime: lockTime,
|
||||
}
|
||||
}
|
||||
|
@ -766,15 +757,6 @@ func NewInvalidateBlockCmd(blockHash string) *InvalidateBlockCmd {
|
|||
}
|
||||
}
|
||||
|
||||
// ListBannedCmd defines the listbanned JSON-RPC command.
|
||||
type ListBannedCmd struct{}
|
||||
|
||||
// NewListBannedCmd returns a new instance which can be used to issue a listbanned
|
||||
// JSON-RPC command.
|
||||
func NewListBannedCmd() *ListBannedCmd {
|
||||
return &ListBannedCmd{}
|
||||
}
|
||||
|
||||
// PingCmd defines the ping JSON-RPC command.
|
||||
type PingCmd struct{}
|
||||
|
||||
|
@ -921,39 +903,6 @@ func NewBitcoindSendRawTransactionCmd(hexTx string, maxFeeRate int32) *SendRawTr
|
|||
}
|
||||
}
|
||||
|
||||
// SetBanSubCmd defines the type used in the setban JSON-RPC command for the
|
||||
// sub command field.
|
||||
type SetBanSubCmd string
|
||||
|
||||
const (
|
||||
// SBAdd indicates the specified host should be added as a persistent
|
||||
// peer.
|
||||
SBAdd SetBanSubCmd = "add"
|
||||
|
||||
// SBRemove indicates the specified peer should be removed.
|
||||
SBRemove SetBanSubCmd = "remove"
|
||||
)
|
||||
|
||||
// SetBanCmd defines the setban JSON-RPC command.
|
||||
type SetBanCmd struct {
|
||||
Addr string
|
||||
SubCmd SetBanSubCmd `jsonrpcusage:"\"add|remove\""`
|
||||
BanTime *int `jsonrpcdefault:"0"`
|
||||
Absolute *bool `jsonrpcdefault:"false"`
|
||||
}
|
||||
|
||||
// NewSetBanCmd returns a new instance which can be used to issue an setban
|
||||
// JSON-RPC command.
|
||||
func NewSetBanCmd(addr string, subCmd SetBanSubCmd, banTime *int,
|
||||
absolute *bool) *SetBanCmd {
|
||||
return &SetBanCmd{
|
||||
Addr: addr,
|
||||
SubCmd: subCmd,
|
||||
BanTime: banTime,
|
||||
Absolute: absolute,
|
||||
}
|
||||
}
|
||||
|
||||
// SetGenerateCmd defines the setgenerate JSON-RPC command.
|
||||
type SetGenerateCmd struct {
|
||||
Generate bool
|
||||
|
@ -1131,9 +1080,6 @@ func init() {
|
|||
MustRegisterCmd("getnetworkhashps", (*GetNetworkHashPSCmd)(nil), flags)
|
||||
MustRegisterCmd("getnodeaddresses", (*GetNodeAddressesCmd)(nil), flags)
|
||||
MustRegisterCmd("getpeerinfo", (*GetPeerInfoCmd)(nil), flags)
|
||||
MustRegisterCmd("listbanned", (*ListBannedCmd)(nil), flags)
|
||||
MustRegisterCmd("setban", (*SetBanCmd)(nil), flags)
|
||||
MustRegisterCmd("clearbanned", (*ClearBannedCmd)(nil), flags)
|
||||
MustRegisterCmd("getrawmempool", (*GetRawMempoolCmd)(nil), flags)
|
||||
MustRegisterCmd("getrawtransaction", (*GetRawTransactionCmd)(nil), flags)
|
||||
MustRegisterCmd("gettxout", (*GetTxOutCmd)(nil), flags)
|
||||
|
|
|
@ -52,13 +52,13 @@ func TestChainSvrCmds(t *testing.T) {
|
|||
txInputs := []btcjson.TransactionInput{
|
||||
{Txid: "123", Vout: 1},
|
||||
}
|
||||
txOutputs := map[string]interface{}{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, txOutputs, nil)
|
||||
amounts := map[string]float64{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, amounts, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[{"txid":"123","vout":1}],{"456":0.0123}],"id":1}`,
|
||||
unmarshalled: &btcjson.CreateRawTransactionCmd{
|
||||
Inputs: []btcjson.TransactionInput{{Txid: "123", Vout: 1}},
|
||||
Outputs: map[string]interface{}{"456": .0123},
|
||||
Amounts: map[string]float64{"456": .0123},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -67,13 +67,13 @@ func TestChainSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("createrawtransaction", `[]`, `{"456":0.0123}`)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
txOutputs := map[string]interface{}{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(nil, txOutputs, nil)
|
||||
amounts := map[string]float64{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(nil, amounts, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[],{"456":0.0123}],"id":1}`,
|
||||
unmarshalled: &btcjson.CreateRawTransactionCmd{
|
||||
Inputs: []btcjson.TransactionInput{},
|
||||
Outputs: map[string]interface{}{"456": .0123},
|
||||
Amounts: map[string]float64{"456": .0123},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -86,35 +86,16 @@ func TestChainSvrCmds(t *testing.T) {
|
|||
txInputs := []btcjson.TransactionInput{
|
||||
{Txid: "123", Vout: 1},
|
||||
}
|
||||
txOutputs := map[string]interface{}{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, txOutputs, btcjson.Int64(12312333333))
|
||||
amounts := map[string]float64{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, amounts, btcjson.Int64(12312333333))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[{"txid":"123","vout":1}],{"456":0.0123},12312333333],"id":1}`,
|
||||
unmarshalled: &btcjson.CreateRawTransactionCmd{
|
||||
Inputs: []btcjson.TransactionInput{{Txid: "123", Vout: 1}},
|
||||
Outputs: map[string]interface{}{"456": .0123},
|
||||
Amounts: map[string]float64{"456": .0123},
|
||||
LockTime: btcjson.Int64(12312333333),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "createrawtransaction with data",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("createrawtransaction", `[{"txid":"123","vout":1}]`,
|
||||
`{"data":"6a134920616d204672616374616c456e6372797074"}`)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
txInputs := []btcjson.TransactionInput{
|
||||
{Txid: "123", Vout: 1},
|
||||
}
|
||||
txOutputs := map[string]interface{}{"data": "6a134920616d204672616374616c456e6372797074"}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, txOutputs, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[{"txid":"123","vout":1}],{"data":"6a134920616d204672616374616c456e6372797074"}],"id":1}`,
|
||||
unmarshalled: &btcjson.CreateRawTransactionCmd{
|
||||
Inputs: []btcjson.TransactionInput{{Txid: "123", Vout: 1}},
|
||||
Outputs: map[string]interface{}{"data": "6a134920616d204672616374616c456e6372797074"},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "fundrawtransaction - empty opts",
|
||||
newCmd: func() (i interface{}, e error) {
|
||||
|
|
|
@ -35,37 +35,35 @@ type GetBlockHeaderVerboseResult struct {
|
|||
}
|
||||
|
||||
// GetBlockStatsResult models the data from the getblockstats command.
|
||||
// Pointers are used instead of values to allow for optional fields.
|
||||
type GetBlockStatsResult struct {
|
||||
AverageFee *int64 `json:"avgfee,omitempty"`
|
||||
AverageFeeRate *int64 `json:"avgfeerate,omitempty"`
|
||||
AverageTxSize *int64 `json:"avgtxsize,omitempty"`
|
||||
FeeratePercentiles *[]int64 `json:"feerate_percentiles,omitempty"`
|
||||
Hash *string `json:"blockhash,omitempty"`
|
||||
Height *int64 `json:"height,omitempty"`
|
||||
Ins *int64 `json:"ins,omitempty"`
|
||||
MaxFee *int64 `json:"maxfee,omitempty"`
|
||||
MaxFeeRate *int64 `json:"maxfeerate,omitempty"`
|
||||
MaxTxSize *int64 `json:"maxtxsize,omitempty"`
|
||||
MedianFee *int64 `json:"medianfee,omitempty"`
|
||||
MedianTime *int64 `json:"mediantime,omitempty"`
|
||||
MedianTxSize *int64 `json:"mediantxsize,omitempty"`
|
||||
MinFee *int64 `json:"minfee,omitempty"`
|
||||
MinFeeRate *int64 `json:"minfeerate,omitempty"`
|
||||
MinTxSize *int64 `json:"mintxsize,omitempty"`
|
||||
Outs *int64 `json:"outs,omitempty"`
|
||||
SegWitTotalSize *int64 `json:"swtotal_size,omitempty"`
|
||||
SegWitTotalWeight *int64 `json:"swtotal_weight,omitempty"`
|
||||
SegWitTxs *int64 `json:"swtxs,omitempty"`
|
||||
Subsidy *int64 `json:"subsidy,omitempty"`
|
||||
Time *int64 `json:"time,omitempty"`
|
||||
TotalOut *int64 `json:"total_out,omitempty"`
|
||||
TotalSize *int64 `json:"total_size,omitempty"`
|
||||
TotalWeight *int64 `json:"total_weight,omitempty"`
|
||||
TotalFee *int64 `json:"totalfee,omitempty"`
|
||||
Txs *int64 `json:"txs,omitempty"`
|
||||
UTXOIncrease *int64 `json:"utxo_increase,omitempty"`
|
||||
UTXOSizeIncrease *int64 `json:"utxo_size_inc,omitempty"`
|
||||
AverageFee int64 `json:"avgfee"`
|
||||
AverageFeeRate int64 `json:"avgfeerate"`
|
||||
AverageTxSize int64 `json:"avgtxsize"`
|
||||
FeeratePercentiles []int64 `json:"feerate_percentiles"`
|
||||
Hash string `json:"blockhash"`
|
||||
Height int64 `json:"height"`
|
||||
Ins int64 `json:"ins"`
|
||||
MaxFee int64 `json:"maxfee"`
|
||||
MaxFeeRate int64 `json:"maxfeerate"`
|
||||
MaxTxSize int64 `json:"maxtxsize"`
|
||||
MedianFee int64 `json:"medianfee"`
|
||||
MedianTime int64 `json:"mediantime"`
|
||||
MedianTxSize int64 `json:"mediantxsize"`
|
||||
MinFee int64 `json:"minfee"`
|
||||
MinFeeRate int64 `json:"minfeerate"`
|
||||
MinTxSize int64 `json:"mintxsize"`
|
||||
Outs int64 `json:"outs"`
|
||||
SegWitTotalSize int64 `json:"swtotal_size"`
|
||||
SegWitTotalWeight int64 `json:"swtotal_weight"`
|
||||
SegWitTxs int64 `json:"swtxs"`
|
||||
Subsidy int64 `json:"subsidy"`
|
||||
Time int64 `json:"time"`
|
||||
TotalOut int64 `json:"total_out"`
|
||||
TotalSize int64 `json:"total_size"`
|
||||
TotalWeight int64 `json:"total_weight"`
|
||||
Txs int64 `json:"txs"`
|
||||
UTXOIncrease int64 `json:"utxo_increase"`
|
||||
UTXOSizeIncrease int64 `json:"utxo_size_inc"`
|
||||
}
|
||||
|
||||
type GetBlockVerboseResultBase struct {
|
||||
|
@ -79,11 +77,9 @@ type GetBlockVerboseResultBase struct {
|
|||
VersionHex string `json:"versionHex"`
|
||||
MerkleRoot string `json:"merkleroot"`
|
||||
Time int64 `json:"time"`
|
||||
MedianTime int64 `json:"mediantime"`
|
||||
Nonce uint32 `json:"nonce"`
|
||||
Bits string `json:"bits"`
|
||||
Difficulty float64 `json:"difficulty"`
|
||||
ChainWork string `json:"chainwork"`
|
||||
PreviousHash string `json:"previousblockhash,omitempty"`
|
||||
NextHash string `json:"nextblockhash,omitempty"`
|
||||
|
||||
|
@ -341,13 +337,8 @@ type GetChainTipsResult struct {
|
|||
// GetMempoolInfoResult models the data returned from the getmempoolinfo
|
||||
// command.
|
||||
type GetMempoolInfoResult struct {
|
||||
Size int64 `json:"size"` // Current tx count
|
||||
Bytes int64 `json:"bytes"` // Sum of all virtual transaction sizes as defined in BIP 141. Differs from actual serialized size because witness data is discounted
|
||||
Usage int64 `json:"usage"` // Total memory usage for the mempool
|
||||
TotalFee float64 `json:"total_fee"` // Total fees for the mempool in LBC, ignoring modified fees through prioritizetransaction
|
||||
MemPoolMinFee float64 `json:"mempoolminfee"` // Minimum fee rate in LBC/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee
|
||||
MinRelayTxFee float64 `json:"minrelaytxfee"` // Current minimum relay fee for transactions
|
||||
UnbroadcastCount int64 `json:"unbroadcastcount"` // Current number of transactions that haven't passed initial broadcast yet
|
||||
Size int64 `json:"size"`
|
||||
Bytes int64 `json:"bytes"`
|
||||
}
|
||||
|
||||
// NetworksResult models the networks data from the getnetworkinfo command.
|
||||
|
@ -723,15 +714,6 @@ type InfoChainResult struct {
|
|||
Errors string `json:"errors"`
|
||||
}
|
||||
|
||||
// ListBannedResult models the data returned from the listbanned command.
|
||||
type ListBannedResult struct {
|
||||
Address string `json:"address"`
|
||||
BanCreated int64 `json:"ban_created"`
|
||||
BannedUntil int64 `json:"banned_until"`
|
||||
BanDuration int64 `json:"ban_duration"`
|
||||
TimeRemaining int64 `json:"time_remaining"`
|
||||
}
|
||||
|
||||
// TxRawResult models the data from the getrawtransaction command.
|
||||
type TxRawResult struct {
|
||||
Hex string `json:"hex"`
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
/*
|
||||
Package btcjson provides primitives for working with the bitcoin JSON-RPC API.
|
||||
|
||||
# Overview
|
||||
Overview
|
||||
|
||||
When communicating via the JSON-RPC protocol, all of the commands need to be
|
||||
marshalled to and from the the wire in the appropriate format. This package
|
||||
|
@ -14,7 +14,7 @@ provides data structures and primitives to ease this process.
|
|||
In addition, it also provides some additional features such as custom command
|
||||
registration, command categorization, and reflection-based help generation.
|
||||
|
||||
# JSON-RPC Protocol Overview
|
||||
JSON-RPC Protocol Overview
|
||||
|
||||
This information is not necessary in order to use this package, but it does
|
||||
provide some intuition into what the marshalling and unmarshalling that is
|
||||
|
@ -47,39 +47,39 @@ with it) doesn't always follow the spec and will sometimes return an error
|
|||
string in the result field with a null error for certain commands. However,
|
||||
for the most part, the error field will be set as described on failure.
|
||||
|
||||
# Marshalling and Unmarshalling
|
||||
Marshalling and Unmarshalling
|
||||
|
||||
Based upon the discussion above, it should be easy to see how the types of this
|
||||
package map into the required parts of the protocol
|
||||
|
||||
- Request Objects (type Request)
|
||||
- Commands (type <Foo>Cmd)
|
||||
- Notifications (type <Foo>Ntfn)
|
||||
- Commands (type <Foo>Cmd)
|
||||
- Notifications (type <Foo>Ntfn)
|
||||
- Response Objects (type Response)
|
||||
- Result (type <Foo>Result)
|
||||
- Result (type <Foo>Result)
|
||||
|
||||
To simplify the marshalling of the requests and responses, the MarshalCmd and
|
||||
MarshalResponse functions are provided. They return the raw bytes ready to be
|
||||
sent across the wire.
|
||||
|
||||
Unmarshalling a received Request object is a two step process:
|
||||
1. Unmarshal the raw bytes into a Request struct instance via json.Unmarshal
|
||||
2. Use UnmarshalCmd on the Result field of the unmarshalled Request to create
|
||||
a concrete command or notification instance with all struct fields set
|
||||
accordingly
|
||||
1) Unmarshal the raw bytes into a Request struct instance via json.Unmarshal
|
||||
2) Use UnmarshalCmd on the Result field of the unmarshalled Request to create
|
||||
a concrete command or notification instance with all struct fields set
|
||||
accordingly
|
||||
|
||||
This approach is used since it provides the caller with access to the additional
|
||||
fields in the request that are not part of the command such as the ID.
|
||||
|
||||
Unmarshalling a received Response object is also a two step process:
|
||||
1. Unmarhsal the raw bytes into a Response struct instance via json.Unmarshal
|
||||
2. Depending on the ID, unmarshal the Result field of the unmarshalled
|
||||
Response to create a concrete type instance
|
||||
1) Unmarhsal the raw bytes into a Response struct instance via json.Unmarshal
|
||||
2) Depending on the ID, unmarshal the Result field of the unmarshalled
|
||||
Response to create a concrete type instance
|
||||
|
||||
As above, this approach is used since it provides the caller with access to the
|
||||
fields in the response such as the ID and Error.
|
||||
|
||||
# Command Creation
|
||||
Command Creation
|
||||
|
||||
This package provides two approaches for creating a new command. This first,
|
||||
and preferred, method is to use one of the New<Foo>Cmd functions. This allows
|
||||
|
@ -93,7 +93,7 @@ obviously, run-time which means any mistakes won't be found until the code is
|
|||
actually executed. However, it is quite useful for user-supplied commands
|
||||
that are intentionally dynamic.
|
||||
|
||||
# Custom Command Registration
|
||||
Custom Command Registration
|
||||
|
||||
The command handling of this package is built around the concept of registered
|
||||
commands. This is true for the wide variety of commands already provided by the
|
||||
|
@ -104,7 +104,7 @@ function for this purpose.
|
|||
A list of all registered methods can be obtained with the RegisteredCmdMethods
|
||||
function.
|
||||
|
||||
# Command Inspection
|
||||
Command Inspection
|
||||
|
||||
All registered commands are registered with flags that identify information such
|
||||
as whether the command applies to a chain server, wallet server, or is a
|
||||
|
@ -112,7 +112,7 @@ notification along with the method name to use. These flags can be obtained
|
|||
with the MethodUsageFlags flags, and the method can be obtained with the
|
||||
CmdMethod function.
|
||||
|
||||
# Help Generation
|
||||
Help Generation
|
||||
|
||||
To facilitate providing consistent help to users of the RPC server, this package
|
||||
exposes the GenerateHelp and function which uses reflection on registered
|
||||
|
@ -122,7 +122,7 @@ generate the final help text.
|
|||
In addition, the MethodUsageText function is provided to generate consistent
|
||||
one-line usage for registered commands and notifications using reflection.
|
||||
|
||||
# Errors
|
||||
Errors
|
||||
|
||||
There are 2 distinct type of errors supported by this package:
|
||||
|
||||
|
|
|
@ -476,12 +476,11 @@ func isValidResultType(kind reflect.Kind) bool {
|
|||
// an error will use the key in place of the description.
|
||||
//
|
||||
// The following outlines the required keys:
|
||||
//
|
||||
// "<method>--synopsis" Synopsis for the command
|
||||
// "<method>-<lowerfieldname>" Description for each command argument
|
||||
// "<typename>-<lowerfieldname>" Description for each object field
|
||||
// "<method>--condition<#>" Description for each result condition
|
||||
// "<method>--result<#>" Description for each primitive result num
|
||||
// "<method>--synopsis" Synopsis for the command
|
||||
// "<method>-<lowerfieldname>" Description for each command argument
|
||||
// "<typename>-<lowerfieldname>" Description for each object field
|
||||
// "<method>--condition<#>" Description for each result condition
|
||||
// "<method>--result<#>" Description for each primitive result num
|
||||
//
|
||||
// Notice that the "special" keys synopsis, condition<#>, and result<#> are
|
||||
// preceded by a double dash to ensure they don't conflict with field names.
|
||||
|
@ -493,17 +492,16 @@ func isValidResultType(kind reflect.Kind) bool {
|
|||
// For example, consider the 'help' command itself. There are two possible
|
||||
// returns depending on the provided parameters. So, the help would be
|
||||
// generated by calling the function as follows:
|
||||
//
|
||||
// GenerateHelp("help", descs, (*string)(nil), (*string)(nil)).
|
||||
// GenerateHelp("help", descs, (*string)(nil), (*string)(nil)).
|
||||
//
|
||||
// The following keys would then be required in the provided descriptions map:
|
||||
//
|
||||
// "help--synopsis": "Returns a list of all commands or help for ...."
|
||||
// "help-command": "The command to retrieve help for",
|
||||
// "help--condition0": "no command provided"
|
||||
// "help--condition1": "command specified"
|
||||
// "help--result0": "List of commands"
|
||||
// "help--result1": "Help for specified command"
|
||||
// "help--synopsis": "Returns a list of all commands or help for ...."
|
||||
// "help-command": "The command to retrieve help for",
|
||||
// "help--condition0": "no command provided"
|
||||
// "help--condition1": "command specified"
|
||||
// "help--result0": "List of commands"
|
||||
// "help--result1": "Help for specified command"
|
||||
func GenerateHelp(method string, descs map[string]string, resultTypes ...interface{}) (string, error) {
|
||||
// Look up details about the provided method and error out if not
|
||||
// registered.
|
||||
|
|
|
@ -176,13 +176,12 @@ func NewGetAccountCmd(address string) *GetAccountCmd {
|
|||
|
||||
// GetAccountAddressCmd defines the getaccountaddress JSON-RPC command.
|
||||
type GetAccountAddressCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
AddressType *string `jsonrpcdefault:"\"legacy\""`
|
||||
Account string
|
||||
}
|
||||
|
||||
// NewGetAccountAddressCmd returns a new instance which can be used to issue a
|
||||
// getaccountaddress JSON-RPC command.
|
||||
func NewGetAccountAddressCmd(account *string) *GetAccountAddressCmd {
|
||||
func NewGetAccountAddressCmd(account string) *GetAccountAddressCmd {
|
||||
return &GetAccountAddressCmd{
|
||||
Account: account,
|
||||
}
|
||||
|
@ -190,13 +189,12 @@ func NewGetAccountAddressCmd(account *string) *GetAccountAddressCmd {
|
|||
|
||||
// GetAddressesByAccountCmd defines the getaddressesbyaccount JSON-RPC command.
|
||||
type GetAddressesByAccountCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
Account string
|
||||
}
|
||||
|
||||
// NewGetAddressesByAccountCmd returns a new instance which can be used to issue
|
||||
// a getaddressesbyaccount JSON-RPC command.
|
||||
func NewGetAddressesByAccountCmd(account *string) *GetAddressesByAccountCmd {
|
||||
func NewGetAddressesByAccountCmd(account string) *GetAddressesByAccountCmd {
|
||||
return &GetAddressesByAccountCmd{
|
||||
Account: account,
|
||||
}
|
||||
|
@ -217,9 +215,8 @@ func NewGetAddressInfoCmd(address string) *GetAddressInfoCmd {
|
|||
|
||||
// GetBalanceCmd defines the getbalance JSON-RPC command.
|
||||
type GetBalanceCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
Account *string
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
}
|
||||
|
||||
// NewGetBalanceCmd returns a new instance which can be used to issue a
|
||||
|
@ -245,8 +242,8 @@ func NewGetBalancesCmd() *GetBalancesCmd {
|
|||
|
||||
// GetNewAddressCmd defines the getnewaddress JSON-RPC command.
|
||||
type GetNewAddressCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
AddressType *string `jsonrpcdefault:"\"legacy\""`
|
||||
Account *string
|
||||
AddressType *string // must be one of legacy / p2pkh or p2sh-p2wkh / p2sh-segwit, or p2wkh / bech32
|
||||
}
|
||||
|
||||
// NewGetNewAddressCmd returns a new instance which can be used to issue a
|
||||
|
@ -262,8 +259,7 @@ func NewGetNewAddressCmd(account *string) *GetNewAddressCmd {
|
|||
|
||||
// GetRawChangeAddressCmd defines the getrawchangeaddress JSON-RPC command.
|
||||
type GetRawChangeAddressCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
AddressType *string `jsonrpcdefault:"\"legacy\""`
|
||||
Account *string
|
||||
}
|
||||
|
||||
// NewGetRawChangeAddressCmd returns a new instance which can be used to issue a
|
||||
|
@ -279,8 +275,8 @@ func NewGetRawChangeAddressCmd(account *string) *GetRawChangeAddressCmd {
|
|||
|
||||
// GetReceivedByAccountCmd defines the getreceivedbyaccount JSON-RPC command.
|
||||
type GetReceivedByAccountCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
Account string
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
}
|
||||
|
||||
// NewGetReceivedByAccountCmd returns a new instance which can be used to issue
|
||||
|
@ -288,7 +284,7 @@ type GetReceivedByAccountCmd struct {
|
|||
//
|
||||
// The parameters which are pointers indicate they are optional. Passing nil
|
||||
// for optional parameters will use the default value.
|
||||
func NewGetReceivedByAccountCmd(account *string, minConf *int) *GetReceivedByAccountCmd {
|
||||
func NewGetReceivedByAccountCmd(account string, minConf *int) *GetReceivedByAccountCmd {
|
||||
return &GetReceivedByAccountCmd{
|
||||
Account: account,
|
||||
MinConf: minConf,
|
||||
|
@ -411,8 +407,7 @@ func NewKeyPoolRefillCmd(newSize *uint) *KeyPoolRefillCmd {
|
|||
|
||||
// ListAccountsCmd defines the listaccounts JSON-RPC command.
|
||||
type ListAccountsCmd struct {
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
}
|
||||
|
||||
// NewListAccountsCmd returns a new instance which can be used to issue a
|
||||
|
@ -506,10 +501,10 @@ func NewListSinceBlockCmd(blockHash *string, targetConfirms *int, includeWatchOn
|
|||
|
||||
// ListTransactionsCmd defines the listtransactions JSON-RPC command.
|
||||
type ListTransactionsCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
Count *int `jsonrpcdefault:"10"`
|
||||
From *int `jsonrpcdefault:"0"`
|
||||
IncludeWatchOnly *bool `jsonrpcdefault:"false"`
|
||||
Account *string
|
||||
Count *int `jsonrpcdefault:"10"`
|
||||
From *int `jsonrpcdefault:"0"`
|
||||
IncludeWatchOnly *bool `jsonrpcdefault:"false"`
|
||||
}
|
||||
|
||||
// NewListTransactionsCmd returns a new instance which can be used to issue a
|
||||
|
@ -561,13 +556,36 @@ func NewLockUnspentCmd(unlock bool, transactions []TransactionInput) *LockUnspen
|
|||
}
|
||||
}
|
||||
|
||||
// MoveCmd defines the move JSON-RPC command.
|
||||
type MoveCmd struct {
|
||||
FromAccount string
|
||||
ToAccount string
|
||||
Amount float64 // In BTC
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
Comment *string
|
||||
}
|
||||
|
||||
// NewMoveCmd returns a new instance which can be used to issue a move JSON-RPC
|
||||
// command.
|
||||
//
|
||||
// The parameters which are pointers indicate they are optional. Passing nil
|
||||
// for optional parameters will use the default value.
|
||||
func NewMoveCmd(fromAccount, toAccount string, amount float64, minConf *int, comment *string) *MoveCmd {
|
||||
return &MoveCmd{
|
||||
FromAccount: fromAccount,
|
||||
ToAccount: toAccount,
|
||||
Amount: amount,
|
||||
MinConf: minConf,
|
||||
Comment: comment,
|
||||
}
|
||||
}
|
||||
|
||||
// SendFromCmd defines the sendfrom JSON-RPC command.
|
||||
type SendFromCmd struct {
|
||||
FromAccount string
|
||||
ToAddress string
|
||||
Amount float64 // In BTC
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
Comment *string
|
||||
CommentTo *string
|
||||
}
|
||||
|
@ -577,15 +595,12 @@ type SendFromCmd struct {
|
|||
//
|
||||
// The parameters which are pointers indicate they are optional. Passing nil
|
||||
// for optional parameters will use the default value.
|
||||
func NewSendFromCmd(fromAccount, toAddress string, amount float64,
|
||||
minConf *int, addrType *string, comment, commentTo *string) *SendFromCmd {
|
||||
|
||||
func NewSendFromCmd(fromAccount, toAddress string, amount float64, minConf *int, comment, commentTo *string) *SendFromCmd {
|
||||
return &SendFromCmd{
|
||||
FromAccount: fromAccount,
|
||||
ToAddress: toAddress,
|
||||
Amount: amount,
|
||||
MinConf: minConf,
|
||||
AddressType: addrType,
|
||||
Comment: comment,
|
||||
CommentTo: commentTo,
|
||||
}
|
||||
|
@ -596,7 +611,6 @@ type SendManyCmd struct {
|
|||
FromAccount string
|
||||
Amounts map[string]float64 `jsonrpcusage:"{\"address\":amount,...}"` // In BTC
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
Comment *string
|
||||
}
|
||||
|
||||
|
@ -605,24 +619,21 @@ type SendManyCmd struct {
|
|||
//
|
||||
// The parameters which are pointers indicate they are optional. Passing nil
|
||||
// for optional parameters will use the default value.
|
||||
func NewSendManyCmd(fromAccount string, amounts map[string]float64,
|
||||
minConf *int, addrType *string, comment *string) *SendManyCmd {
|
||||
func NewSendManyCmd(fromAccount string, amounts map[string]float64, minConf *int, comment *string) *SendManyCmd {
|
||||
return &SendManyCmd{
|
||||
FromAccount: fromAccount,
|
||||
Amounts: amounts,
|
||||
MinConf: minConf,
|
||||
AddressType: addrType,
|
||||
Comment: comment,
|
||||
}
|
||||
}
|
||||
|
||||
// SendToAddressCmd defines the sendtoaddress JSON-RPC command.
|
||||
type SendToAddressCmd struct {
|
||||
Address string
|
||||
Amount float64
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
Comment *string
|
||||
CommentTo *string
|
||||
Address string
|
||||
Amount float64
|
||||
Comment *string
|
||||
CommentTo *string
|
||||
}
|
||||
|
||||
// NewSendToAddressCmd returns a new instance which can be used to issue a
|
||||
|
@ -630,14 +641,27 @@ type SendToAddressCmd struct {
|
|||
//
|
||||
// The parameters which are pointers indicate they are optional. Passing nil
|
||||
// for optional parameters will use the default value.
|
||||
func NewSendToAddressCmd(address string, amount float64, addrType *string,
|
||||
comment, commentTo *string) *SendToAddressCmd {
|
||||
func NewSendToAddressCmd(address string, amount float64, comment, commentTo *string) *SendToAddressCmd {
|
||||
return &SendToAddressCmd{
|
||||
Address: address,
|
||||
Amount: amount,
|
||||
AddressType: addrType,
|
||||
Comment: comment,
|
||||
CommentTo: commentTo,
|
||||
Address: address,
|
||||
Amount: amount,
|
||||
Comment: comment,
|
||||
CommentTo: commentTo,
|
||||
}
|
||||
}
|
||||
|
||||
// SetAccountCmd defines the setaccount JSON-RPC command.
|
||||
type SetAccountCmd struct {
|
||||
Address string
|
||||
Account string
|
||||
}
|
||||
|
||||
// NewSetAccountCmd returns a new instance which can be used to issue a
|
||||
// setaccount JSON-RPC command.
|
||||
func NewSetAccountCmd(address, account string) *SetAccountCmd {
|
||||
return &SetAccountCmd{
|
||||
Address: address,
|
||||
Account: account,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -848,8 +872,7 @@ func (s *ScriptPubKey) UnmarshalJSON(data []byte) error {
|
|||
//
|
||||
// Descriptors are typically ranged when specified in the form of generic HD
|
||||
// chain paths.
|
||||
//
|
||||
// Example of a ranged descriptor: pkh(tpub.../*)
|
||||
// Example of a ranged descriptor: pkh(tpub.../*)
|
||||
//
|
||||
// The value can be an int to specify the end of the range, or the range
|
||||
// itself, as []int{begin, end}.
|
||||
|
@ -975,24 +998,6 @@ func NewImportMultiCmd(requests []ImportMultiRequest, options *ImportMultiOption
|
|||
}
|
||||
}
|
||||
|
||||
// RescanBlockchainCmd defines the RescanBlockchain JSON-RPC command.
|
||||
type RescanBlockchainCmd struct {
|
||||
StartHeight *int32 `jsonrpcdefault:"0"`
|
||||
StopHeight *int32
|
||||
}
|
||||
|
||||
// NewRescanBlockchainCmd returns a new instance which can be used to issue
|
||||
// an RescanBlockchain JSON-RPC command.
|
||||
//
|
||||
// The parameters which are pointers indicate they are optional. Passing nil
|
||||
// for optional parameters will use the default value.
|
||||
func NewRescanBlockchainCmd(startHeight *int32, stopHeight *int32) *RescanBlockchainCmd {
|
||||
return &RescanBlockchainCmd{
|
||||
StartHeight: startHeight,
|
||||
StopHeight: stopHeight,
|
||||
}
|
||||
}
|
||||
|
||||
// PsbtInput represents an input to include in the PSBT created by the
|
||||
// WalletCreateFundedPsbtCmd command.
|
||||
type PsbtInput struct {
|
||||
|
@ -1114,10 +1119,11 @@ func init() {
|
|||
MustRegisterCmd("listunspent", (*ListUnspentCmd)(nil), flags)
|
||||
MustRegisterCmd("loadwallet", (*LoadWalletCmd)(nil), flags)
|
||||
MustRegisterCmd("lockunspent", (*LockUnspentCmd)(nil), flags)
|
||||
MustRegisterCmd("rescanblockchain", (*RescanBlockchainCmd)(nil), flags)
|
||||
MustRegisterCmd("move", (*MoveCmd)(nil), flags)
|
||||
MustRegisterCmd("sendfrom", (*SendFromCmd)(nil), flags)
|
||||
MustRegisterCmd("sendmany", (*SendManyCmd)(nil), flags)
|
||||
MustRegisterCmd("sendtoaddress", (*SendToAddressCmd)(nil), flags)
|
||||
MustRegisterCmd("setaccount", (*SetAccountCmd)(nil), flags)
|
||||
MustRegisterCmd("settxfee", (*SetTxFeeCmd)(nil), flags)
|
||||
MustRegisterCmd("signmessage", (*SignMessageCmd)(nil), flags)
|
||||
MustRegisterCmd("signrawtransaction", (*SignRawTransactionCmd)(nil), flags)
|
||||
|
|
|
@ -287,12 +287,11 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("getaccountaddress", "acct")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewGetAccountAddressCmd(btcjson.String("acct"))
|
||||
return btcjson.NewGetAccountAddressCmd("acct")
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getaccountaddress","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetAccountAddressCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Account: "acct",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -301,12 +300,11 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("getaddressesbyaccount", "acct")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewGetAddressesByAccountCmd(btcjson.String("acct"))
|
||||
return btcjson.NewGetAddressesByAccountCmd("acct")
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getaddressesbyaccount","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetAddressesByAccountCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
AddressType: btcjson.String("*"),
|
||||
Account: "acct",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -332,9 +330,8 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getbalance","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.GetBalanceCmd{
|
||||
Account: btcjson.String("default"),
|
||||
MinConf: btcjson.Int(1),
|
||||
AddressType: btcjson.String("*"),
|
||||
Account: nil,
|
||||
MinConf: btcjson.Int(1),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -347,9 +344,8 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getbalance","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetBalanceCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
MinConf: btcjson.Int(1),
|
||||
AddressType: btcjson.String("*"),
|
||||
Account: btcjson.String("acct"),
|
||||
MinConf: btcjson.Int(1),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -362,9 +358,8 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getbalance","params":["acct",6],"id":1}`,
|
||||
unmarshalled: &btcjson.GetBalanceCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("*"),
|
||||
Account: btcjson.String("acct"),
|
||||
MinConf: btcjson.Int(6),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -388,8 +383,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getnewaddress","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.GetNewAddressCmd{
|
||||
Account: btcjson.String("default"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Account: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -402,8 +396,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getnewaddress","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetNewAddressCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Account: btcjson.String("acct"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -416,8 +409,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getrawchangeaddress","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.GetRawChangeAddressCmd{
|
||||
Account: btcjson.String("default"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Account: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -430,8 +422,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getrawchangeaddress","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetRawChangeAddressCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Account: btcjson.String("acct"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -440,11 +431,11 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("getreceivedbyaccount", "acct")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewGetReceivedByAccountCmd(btcjson.String("acct"), nil)
|
||||
return btcjson.NewGetReceivedByAccountCmd("acct", nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getreceivedbyaccount","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetReceivedByAccountCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
Account: "acct",
|
||||
MinConf: btcjson.Int(1),
|
||||
},
|
||||
},
|
||||
|
@ -454,11 +445,11 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("getreceivedbyaccount", "acct", 6)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewGetReceivedByAccountCmd(btcjson.String("acct"), btcjson.Int(6))
|
||||
return btcjson.NewGetReceivedByAccountCmd("acct", btcjson.Int(6))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getreceivedbyaccount","params":["acct",6],"id":1}`,
|
||||
unmarshalled: &btcjson.GetReceivedByAccountCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
Account: "acct",
|
||||
MinConf: btcjson.Int(6),
|
||||
},
|
||||
},
|
||||
|
@ -610,8 +601,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"listaccounts","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.ListAccountsCmd{
|
||||
MinConf: btcjson.Int(1),
|
||||
AddressType: btcjson.String("*"),
|
||||
MinConf: btcjson.Int(1),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -624,8 +614,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"listaccounts","params":[6],"id":1}`,
|
||||
unmarshalled: &btcjson.ListAccountsCmd{
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("*"),
|
||||
MinConf: btcjson.Int(6),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -855,7 +844,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"listtransactions","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.ListTransactionsCmd{
|
||||
Account: btcjson.String("default"),
|
||||
Account: nil,
|
||||
Count: btcjson.Int(10),
|
||||
From: btcjson.Int(0),
|
||||
IncludeWatchOnly: btcjson.Bool(false),
|
||||
|
@ -1007,13 +996,64 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "move",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("move", "from", "to", 0.5)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewMoveCmd("from", "to", 0.5, nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"move","params":["from","to",0.5],"id":1}`,
|
||||
unmarshalled: &btcjson.MoveCmd{
|
||||
FromAccount: "from",
|
||||
ToAccount: "to",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(1),
|
||||
Comment: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "move optional1",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("move", "from", "to", 0.5, 6)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewMoveCmd("from", "to", 0.5, btcjson.Int(6), nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"move","params":["from","to",0.5,6],"id":1}`,
|
||||
unmarshalled: &btcjson.MoveCmd{
|
||||
FromAccount: "from",
|
||||
ToAccount: "to",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(6),
|
||||
Comment: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "move optional2",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("move", "from", "to", 0.5, 6, "comment")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewMoveCmd("from", "to", 0.5, btcjson.Int(6), btcjson.String("comment"))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"move","params":["from","to",0.5,6,"comment"],"id":1}`,
|
||||
unmarshalled: &btcjson.MoveCmd{
|
||||
FromAccount: "from",
|
||||
ToAccount: "to",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(6),
|
||||
Comment: btcjson.String("comment"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendfrom",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, nil, nil, nil, nil)
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, nil, nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5],"id":1}`,
|
||||
unmarshalled: &btcjson.SendFromCmd{
|
||||
|
@ -1021,7 +1061,6 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
ToAddress: "1Address",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(1),
|
||||
AddressType: btcjson.String("*"),
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
},
|
||||
|
@ -1032,7 +1071,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6), nil, nil, nil)
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6), nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6],"id":1}`,
|
||||
unmarshalled: &btcjson.SendFromCmd{
|
||||
|
@ -1040,7 +1079,6 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
ToAddress: "1Address",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("*"),
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
},
|
||||
|
@ -1048,59 +1086,37 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
{
|
||||
name: "sendfrom optional2",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "legacy")
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "comment")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6), btcjson.String("legacy"),
|
||||
nil, nil)
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6),
|
||||
btcjson.String("comment"), nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"legacy"],"id":1}`,
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"comment"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendFromCmd{
|
||||
FromAccount: "from",
|
||||
ToAddress: "1Address",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: nil,
|
||||
Comment: btcjson.String("comment"),
|
||||
CommentTo: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendfrom optional3",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "legacy", "comment")
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "comment", "commentto")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6), btcjson.String("legacy"),
|
||||
btcjson.String("comment"), nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"legacy","comment"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendFromCmd{
|
||||
FromAccount: "from",
|
||||
ToAddress: "1Address",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: btcjson.String("comment"),
|
||||
CommentTo: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendfrom optional4",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "legacy", "comment", "commentto")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6), btcjson.String("legacy"),
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6),
|
||||
btcjson.String("comment"), btcjson.String("commentto"))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"legacy","comment","commentto"],"id":1}`,
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"comment","commentto"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendFromCmd{
|
||||
FromAccount: "from",
|
||||
ToAddress: "1Address",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: btcjson.String("comment"),
|
||||
CommentTo: btcjson.String("commentto"),
|
||||
},
|
||||
|
@ -1112,14 +1128,13 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
staticCmd: func() interface{} {
|
||||
amounts := map[string]float64{"1Address": 0.5}
|
||||
return btcjson.NewSendManyCmd("from", amounts, nil, nil, nil)
|
||||
return btcjson.NewSendManyCmd("from", amounts, nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5}],"id":1}`,
|
||||
unmarshalled: &btcjson.SendManyCmd{
|
||||
FromAccount: "from",
|
||||
Amounts: map[string]float64{"1Address": 0.5},
|
||||
MinConf: btcjson.Int(1),
|
||||
AddressType: btcjson.String("*"),
|
||||
Comment: nil,
|
||||
},
|
||||
},
|
||||
|
@ -1130,50 +1145,30 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
staticCmd: func() interface{} {
|
||||
amounts := map[string]float64{"1Address": 0.5}
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), nil, nil)
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5},6],"id":1}`,
|
||||
unmarshalled: &btcjson.SendManyCmd{
|
||||
FromAccount: "from",
|
||||
Amounts: map[string]float64{"1Address": 0.5},
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("*"),
|
||||
Comment: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendmany optional2",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendmany", "from", `{"1Address":0.5}`, 6, "legacy")
|
||||
return btcjson.NewCmd("sendmany", "from", `{"1Address":0.5}`, 6, "comment")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
amounts := map[string]float64{"1Address": 0.5}
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), btcjson.String("legacy"), nil)
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), btcjson.String("comment"))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5},6,"legacy"],"id":1}`,
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5},6,"comment"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendManyCmd{
|
||||
FromAccount: "from",
|
||||
Amounts: map[string]float64{"1Address": 0.5},
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendmany optional3",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendmany", "from", `{"1Address":0.5}`, 6, "legacy", "comment")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
amounts := map[string]float64{"1Address": 0.5}
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), btcjson.String("legacy"), btcjson.String("comment"))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5},6,"legacy","comment"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendManyCmd{
|
||||
FromAccount: "from",
|
||||
Amounts: map[string]float64{"1Address": 0.5},
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: btcjson.String("comment"),
|
||||
},
|
||||
},
|
||||
|
@ -1183,50 +1178,45 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, nil, nil, nil)
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendtoaddress","params":["1Address",0.5],"id":1}`,
|
||||
unmarshalled: &btcjson.SendToAddressCmd{
|
||||
Address: "1Address",
|
||||
Amount: 0.5,
|
||||
AddressType: btcjson.String("*"),
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
Address: "1Address",
|
||||
Amount: 0.5,
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendtoaddress optional1",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5, "legacy")
|
||||
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5, "comment", "commentto")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, btcjson.String("legacy"), nil, nil)
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, btcjson.String("comment"),
|
||||
btcjson.String("commentto"))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendtoaddress","params":["1Address",0.5,"legacy"],"id":1}`,
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendtoaddress","params":["1Address",0.5,"comment","commentto"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendToAddressCmd{
|
||||
Address: "1Address",
|
||||
Amount: 0.5,
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
Address: "1Address",
|
||||
Amount: 0.5,
|
||||
Comment: btcjson.String("comment"),
|
||||
CommentTo: btcjson.String("commentto"),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendtoaddress optional2",
|
||||
name: "setaccount",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5, "legacy", "comment", "commentto")
|
||||
return btcjson.NewCmd("setaccount", "1Address", "acct")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, btcjson.String("legacy"), btcjson.String("comment"),
|
||||
btcjson.String("commentto"))
|
||||
return btcjson.NewSetAccountCmd("1Address", "acct")
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendtoaddress","params":["1Address",0.5,"legacy","comment","commentto"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendToAddressCmd{
|
||||
Address: "1Address",
|
||||
Amount: 0.5,
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: btcjson.String("comment"),
|
||||
CommentTo: btcjson.String("commentto"),
|
||||
marshalled: `{"jsonrpc":"1.0","method":"setaccount","params":["1Address","acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.SetAccountCmd{
|
||||
Address: "1Address",
|
||||
Account: "acct",
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
@ -47,32 +47,13 @@ type embeddedAddressInfo struct {
|
|||
// Reference: https://bitcoincore.org/en/doc/0.20.0/rpc/wallet/getaddressinfo
|
||||
//
|
||||
// The GetAddressInfoResult has three segments:
|
||||
// 1. General information about the address.
|
||||
// 2. Metadata (Timestamp, HDKeyPath, HDSeedID) and wallet fields
|
||||
// (IsMine, IsWatchOnly).
|
||||
// 3. Information about the embedded address in case of P2SH or P2WSH.
|
||||
// Same structure as (1).
|
||||
// 1. General information about the address.
|
||||
// 2. Metadata (Timestamp, HDKeyPath, HDSeedID) and wallet fields
|
||||
// (IsMine, IsWatchOnly).
|
||||
// 3. Information about the embedded address in case of P2SH or P2WSH.
|
||||
// Same structure as (1).
|
||||
type GetAddressInfoResult struct {
|
||||
// The following fields are identical to embeddedAddressInfo.
|
||||
// However, the utility to generate RPC help message can't handle
|
||||
// embedded field properly. So, spell out the attributes individually.
|
||||
Address string `json:"address"`
|
||||
ScriptPubKey string `json:"scriptPubKey"`
|
||||
Descriptor *string `json:"desc,omitempty"`
|
||||
IsScript bool `json:"isscript"`
|
||||
IsChange bool `json:"ischange"`
|
||||
IsWitness bool `json:"iswitness"`
|
||||
WitnessVersion int `json:"witness_version,omitempty"`
|
||||
WitnessProgram *string `json:"witness_program,omitempty"`
|
||||
ScriptType *txscript.ScriptClass `json:"script,omitempty"`
|
||||
Hex *string `json:"hex,omitempty"`
|
||||
PubKeys *[]string `json:"pubkeys,omitempty"`
|
||||
SignaturesRequired *int `json:"sigsrequired,omitempty"`
|
||||
PubKey *string `json:"pubkey,omitempty"`
|
||||
IsCompressed *bool `json:"iscompressed,omitempty"`
|
||||
HDMasterFingerprint *string `json:"hdmasterfingerprint,omitempty"`
|
||||
Labels []string `json:"labels"`
|
||||
|
||||
embeddedAddressInfo
|
||||
IsMine bool `json:"ismine"`
|
||||
IsWatchOnly bool `json:"iswatchonly"`
|
||||
Timestamp *int `json:"timestamp,omitempty"`
|
||||
|
@ -174,7 +155,6 @@ type GetTransactionResult struct {
|
|||
TimeReceived int64 `json:"timereceived"`
|
||||
Details []GetTransactionDetailsResult `json:"details"`
|
||||
Hex string `json:"hex"`
|
||||
Generated bool `json:"generated"`
|
||||
}
|
||||
|
||||
type ScanningOrFalse struct {
|
||||
|
@ -289,6 +269,7 @@ type ListReceivedByAccountResult struct {
|
|||
// ListReceivedByAddressResult models the data from the listreceivedbyaddress
|
||||
// command.
|
||||
type ListReceivedByAddressResult struct {
|
||||
Account string `json:"account"`
|
||||
Address string `json:"address"`
|
||||
Amount float64 `json:"amount"`
|
||||
Confirmations uint64 `json:"confirmations"`
|
||||
|
@ -317,12 +298,6 @@ type ListUnspentResult struct {
|
|||
IsStake bool `json:"isstake"`
|
||||
}
|
||||
|
||||
// RescanBlockchainResult models the data returned from the rescanblockchain command.
|
||||
type RescanBlockchainResult struct {
|
||||
StartHeight int32 `json:"start_height"`
|
||||
StoptHeight int32 `json:"stop_height"`
|
||||
}
|
||||
|
||||
// SignRawTransactionError models the data that contains script verification
|
||||
// errors from the signrawtransaction request.
|
||||
type SignRawTransactionError struct {
|
||||
|
|
|
@ -37,8 +37,10 @@ func TestGetAddressInfoResult(t *testing.T) {
|
|||
name: "GetAddressInfoResult - ScriptType",
|
||||
result: `{"script":"nonstandard","address":"1abc"}`,
|
||||
want: GetAddressInfoResult{
|
||||
Address: "1abc",
|
||||
ScriptType: nonStandard,
|
||||
embeddedAddressInfo: embeddedAddressInfo{
|
||||
Address: "1abc",
|
||||
ScriptType: nonStandard,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
@ -40,7 +40,7 @@ func NewExportWatchingWalletCmd(account *string, download *bool) *ExportWatching
|
|||
|
||||
// GetUnconfirmedBalanceCmd defines the getunconfirmedbalance JSON-RPC command.
|
||||
type GetUnconfirmedBalanceCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
Account *string
|
||||
}
|
||||
|
||||
// NewGetUnconfirmedBalanceCmd returns a new instance which can be used to issue
|
||||
|
@ -58,7 +58,7 @@ func NewGetUnconfirmedBalanceCmd(account *string) *GetUnconfirmedBalanceCmd {
|
|||
// command.
|
||||
type ListAddressTransactionsCmd struct {
|
||||
Addresses []string
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
Account *string
|
||||
}
|
||||
|
||||
// NewListAddressTransactionsCmd returns a new instance which can be used to
|
||||
|
@ -75,7 +75,7 @@ func NewListAddressTransactionsCmd(addresses []string, account *string) *ListAdd
|
|||
|
||||
// ListAllTransactionsCmd defines the listalltransactions JSON-RPC command.
|
||||
type ListAllTransactionsCmd struct {
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
Account *string
|
||||
}
|
||||
|
||||
// NewListAllTransactionsCmd returns a new instance which can be used to issue a
|
||||
|
@ -114,8 +114,9 @@ func NewWalletIsLockedCmd() *WalletIsLockedCmd {
|
|||
}
|
||||
|
||||
func init() {
|
||||
// The commands in this file are only usable with a wallet server.
|
||||
flags := UFWalletOnly
|
||||
// The commands in this file are only usable with a wallet server via
|
||||
// websockets.
|
||||
flags := UFWalletOnly | UFWebsocketOnly
|
||||
|
||||
MustRegisterCmd("createencryptedwallet", (*CreateEncryptedWalletCmd)(nil), flags)
|
||||
MustRegisterCmd("exportwatchingwallet", (*ExportWatchingWalletCmd)(nil), flags)
|
||||
|
|
|
@ -71,7 +71,7 @@ func TestWalletSvrWsCmds(t *testing.T) {
|
|||
{
|
||||
name: "exportwatchingwallet optional2",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("exportwatchingwallet", btcjson.String("acct"), true)
|
||||
return btcjson.NewCmd("exportwatchingwallet", "acct", true)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewExportWatchingWalletCmd(btcjson.String("acct"),
|
||||
|
@ -93,7 +93,7 @@ func TestWalletSvrWsCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getunconfirmedbalance","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.GetUnconfirmedBalanceCmd{
|
||||
Account: btcjson.String("default"),
|
||||
Account: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -120,7 +120,7 @@ func TestWalletSvrWsCmds(t *testing.T) {
|
|||
marshalled: `{"jsonrpc":"1.0","method":"listaddresstransactions","params":[["1Address"]],"id":1}`,
|
||||
unmarshalled: &btcjson.ListAddressTransactionsCmd{
|
||||
Addresses: []string{"1Address"},
|
||||
Account: btcjson.String("default"),
|
||||
Account: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -148,7 +148,7 @@ func TestWalletSvrWsCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.ListAllTransactionsCmd{
|
||||
Account: btcjson.String("default"),
|
||||
Account: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
@ -39,11 +39,11 @@ func DoubleHashH(b []byte) Hash {
|
|||
|
||||
// LbryPoWHashH calculates returns the PoW Hash.
|
||||
//
|
||||
// doubled := SHA256(SHA256(b))
|
||||
// expanded := SHA512(doubled)
|
||||
// left := RIPEMD160(expanded[0:32])
|
||||
// right := RIPEMD160(expanded[32:64])
|
||||
// result := SHA256(SHA256(left||right))
|
||||
// doubled := SHA256(SHA256(b))
|
||||
// expanded := SHA512(doubled)
|
||||
// left := RIPEMD160(expanded[0:32])
|
||||
// right := RIPEMD160(expanded[32:64])
|
||||
// result := SHA256(SHA256(left||right))
|
||||
func LbryPoWHashH(b []byte) Hash {
|
||||
doubled := DoubleHashB(b)
|
||||
expanded := sha512.Sum512(doubled)
|
||||
|
|
|
@ -18,40 +18,40 @@
|
|||
// When a network parameter is needed, it may then be looked up through this
|
||||
// variable (either directly, or hidden in a library call).
|
||||
//
|
||||
// package main
|
||||
// package main
|
||||
//
|
||||
// import (
|
||||
// "flag"
|
||||
// "fmt"
|
||||
// "log"
|
||||
// import (
|
||||
// "flag"
|
||||
// "fmt"
|
||||
// "log"
|
||||
//
|
||||
// btcutil "github.com/lbryio/lbcutil"
|
||||
// "github.com/lbryio/lbcd/chaincfg"
|
||||
// )
|
||||
// btcutil "github.com/lbryio/lbcutil"
|
||||
// "github.com/lbryio/lbcd/chaincfg"
|
||||
// )
|
||||
//
|
||||
// var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network")
|
||||
// var testnet = flag.Bool("testnet", false, "operate on the testnet Bitcoin network")
|
||||
//
|
||||
// // By default (without -testnet), use mainnet.
|
||||
// var chainParams = &chaincfg.MainNetParams
|
||||
// // By default (without -testnet), use mainnet.
|
||||
// var chainParams = &chaincfg.MainNetParams
|
||||
//
|
||||
// func main() {
|
||||
// flag.Parse()
|
||||
// func main() {
|
||||
// flag.Parse()
|
||||
//
|
||||
// // Modify active network parameters if operating on testnet.
|
||||
// if *testnet {
|
||||
// chainParams = &chaincfg.TestNet3Params
|
||||
// }
|
||||
// // Modify active network parameters if operating on testnet.
|
||||
// if *testnet {
|
||||
// chainParams = &chaincfg.TestNet3Params
|
||||
// }
|
||||
//
|
||||
// // later...
|
||||
// // later...
|
||||
//
|
||||
// // Create and print new payment address, specific to the active network.
|
||||
// pubKeyHash := make([]byte, 20)
|
||||
// addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, chainParams)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// fmt.Println(addr)
|
||||
// }
|
||||
// // Create and print new payment address, specific to the active network.
|
||||
// pubKeyHash := make([]byte, 20)
|
||||
// addr, err := btcutil.NewAddressPubKeyHash(pubKeyHash, chainParams)
|
||||
// if err != nil {
|
||||
// log.Fatal(err)
|
||||
// }
|
||||
// fmt.Println(addr)
|
||||
// }
|
||||
//
|
||||
// If an application does not use one of the three standard Bitcoin networks,
|
||||
// a new Params struct may be created which defines the parameters for the
|
||||
|
|
|
@ -794,9 +794,8 @@ func IsBech32SegwitPrefix(prefix string) bool {
|
|||
// ErrInvalidHDKeyID error will be returned.
|
||||
//
|
||||
// Reference:
|
||||
//
|
||||
// SLIP-0132 : Registered HD version bytes for BIP-0032
|
||||
// https://github.com/satoshilabs/slips/blob/master/slip-0132.md
|
||||
// SLIP-0132 : Registered HD version bytes for BIP-0032
|
||||
// https://github.com/satoshilabs/slips/blob/master/slip-0132.md
|
||||
func RegisterHDKeyID(hdPublicKeyID []byte, hdPrivateKeyID []byte) error {
|
||||
if len(hdPublicKeyID) != 4 || len(hdPrivateKeyID) != 4 {
|
||||
return ErrInvalidHDKeyID
|
||||
|
|
|
@ -78,10 +78,9 @@ func (c *Change) Marshal(enc *bytes.Buffer) error {
|
|||
binary.BigEndian.PutUint32(temp[:4], uint32(len(c.SpentChildren)))
|
||||
enc.Write(temp[:4])
|
||||
for key := range c.SpentChildren {
|
||||
keySize := uint16(len(key))
|
||||
binary.BigEndian.PutUint16(temp[:2], keySize) // technically limited to 255; not sure we trust it
|
||||
binary.BigEndian.PutUint16(temp[:2], uint16(len(key))) // technically limited to 255; not sure we trust it
|
||||
enc.Write(temp[:2])
|
||||
enc.WriteString(key[:keySize])
|
||||
enc.WriteString(key)
|
||||
}
|
||||
} else {
|
||||
binary.BigEndian.PutUint32(temp[:4], 0)
|
||||
|
|
|
@ -4,7 +4,9 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
|
@ -47,9 +49,6 @@ type ClaimTrie struct {
|
|||
|
||||
// Registrered cleanup functions which are invoked in the Close() in reverse order.
|
||||
cleanups []func() error
|
||||
|
||||
// claimLogger communicates progress of claimtrie rebuild.
|
||||
claimLogger *claimProgressLogger
|
||||
}
|
||||
|
||||
func New(cfg config.Config) (*ClaimTrie, error) {
|
||||
|
@ -247,17 +246,17 @@ func (ct *ClaimTrie) AppendBlock(temporary bool) error {
|
|||
names = append(names, expirations...)
|
||||
names = removeDuplicates(names)
|
||||
|
||||
for _, name := range names {
|
||||
nhns := ct.makeNameHashNext(names, false, nil)
|
||||
for nhn := range nhns {
|
||||
|
||||
hash, next := ct.nodeManager.Hash(name)
|
||||
ct.merkleTrie.Update(name, hash, true)
|
||||
if next <= 0 {
|
||||
ct.merkleTrie.Update(nhn.Name, nhn.Hash, true)
|
||||
if nhn.Next <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
newName := normalization.NormalizeIfNecessary(name, next)
|
||||
newName := normalization.NormalizeIfNecessary(nhn.Name, nhn.Next)
|
||||
updateNames = append(updateNames, newName)
|
||||
updateHeights = append(updateHeights, next)
|
||||
updateHeights = append(updateHeights, nhn.Next)
|
||||
}
|
||||
if !temporary && len(updateNames) > 0 {
|
||||
err = ct.temporalRepo.SetNodesAt(updateNames, updateHeights)
|
||||
|
@ -331,52 +330,31 @@ func (ct *ClaimTrie) ResetHeight(height int32) error {
|
|||
if passedHashFork {
|
||||
names = nil // force them to reconsider all names
|
||||
}
|
||||
|
||||
var fullRebuildRequired bool
|
||||
|
||||
err = ct.merkleTrie.SetRoot(hash)
|
||||
if err == merkletrie.ErrFullRebuildRequired {
|
||||
fullRebuildRequired = true
|
||||
} else if err != nil {
|
||||
return errors.Wrapf(err, "setRoot")
|
||||
}
|
||||
|
||||
if fullRebuildRequired {
|
||||
ct.runFullTrieRebuild(names, nil)
|
||||
}
|
||||
|
||||
if !ct.MerkleHash().IsEqual(hash) {
|
||||
return errors.Errorf("unable to restore the hash at height %d"+
|
||||
" (fullTriedRebuilt: %t)", height, fullRebuildRequired)
|
||||
return errors.Errorf("unable to restore the hash at height %d", height)
|
||||
}
|
||||
|
||||
return errors.WithStack(ct.blockRepo.Delete(height+1, oldHeight))
|
||||
}
|
||||
|
||||
func (ct *ClaimTrie) runFullTrieRebuild(names [][]byte, interrupt <-chan struct{}) {
|
||||
var nhns chan NameHashNext
|
||||
if names == nil {
|
||||
node.Log("Building the entire claim trie in RAM...")
|
||||
ct.claimLogger = newClaimProgressLogger("Processed", node.GetLogger())
|
||||
|
||||
ct.nodeManager.IterateNames(func(name []byte) bool {
|
||||
if interruptRequested(interrupt) {
|
||||
return false
|
||||
}
|
||||
clone := make([]byte, len(name))
|
||||
copy(clone, name)
|
||||
hash, _ := ct.nodeManager.Hash(clone)
|
||||
ct.merkleTrie.Update(clone, hash, false)
|
||||
ct.claimLogger.LogName(name)
|
||||
return true
|
||||
})
|
||||
node.LogOnce("Building the entire claim trie in RAM...")
|
||||
|
||||
nhns = ct.makeNameHashNext(nil, true, interrupt)
|
||||
} else {
|
||||
for _, name := range names {
|
||||
hash, _ := ct.nodeManager.Hash(name)
|
||||
ct.merkleTrie.Update(name, hash, false)
|
||||
}
|
||||
nhns = ct.makeNameHashNext(names, false, interrupt)
|
||||
}
|
||||
|
||||
for nhn := range nhns {
|
||||
ct.merkleTrie.Update(nhn.Name, nhn.Hash, false)
|
||||
}
|
||||
}
|
||||
|
||||
// MerkleHash returns the Merkle Hash of the claimTrie.
|
||||
|
@ -442,6 +420,12 @@ func (ct *ClaimTrie) FlushToDisk() {
|
|||
}
|
||||
}
|
||||
|
||||
type NameHashNext struct {
|
||||
Name []byte
|
||||
Hash *chainhash.Hash
|
||||
Next int32
|
||||
}
|
||||
|
||||
func interruptRequested(interrupted <-chan struct{}) bool {
|
||||
select {
|
||||
case <-interrupted: // should never block on nil
|
||||
|
@ -451,3 +435,53 @@ func interruptRequested(interrupted <-chan struct{}) bool {
|
|||
|
||||
return false
|
||||
}
|
||||
|
||||
func (ct *ClaimTrie) makeNameHashNext(names [][]byte, all bool, interrupt <-chan struct{}) chan NameHashNext {
|
||||
inputs := make(chan []byte, 512)
|
||||
outputs := make(chan NameHashNext, 512)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
hashComputationWorker := func() {
|
||||
for name := range inputs {
|
||||
hash, next := ct.nodeManager.Hash(name)
|
||||
outputs <- NameHashNext{name, hash, next}
|
||||
}
|
||||
wg.Done()
|
||||
}
|
||||
|
||||
threads := int(0.8 * float32(runtime.NumCPU()))
|
||||
if threads < 1 {
|
||||
threads = 1
|
||||
}
|
||||
for threads > 0 {
|
||||
threads--
|
||||
wg.Add(1)
|
||||
go hashComputationWorker()
|
||||
}
|
||||
go func() {
|
||||
if all {
|
||||
ct.nodeManager.IterateNames(func(name []byte) bool {
|
||||
if interruptRequested(interrupt) {
|
||||
return false
|
||||
}
|
||||
clone := make([]byte, len(name))
|
||||
copy(clone, name) // iteration name buffer is reused on future loops
|
||||
inputs <- clone
|
||||
return true
|
||||
})
|
||||
} else {
|
||||
for _, name := range names {
|
||||
if interruptRequested(interrupt) {
|
||||
break
|
||||
}
|
||||
inputs <- name
|
||||
}
|
||||
}
|
||||
close(inputs)
|
||||
}()
|
||||
go func() {
|
||||
wg.Wait()
|
||||
close(outputs)
|
||||
}()
|
||||
return outputs
|
||||
}
|
||||
|
|
|
@ -23,14 +23,12 @@ func NewBlocCommands() *cobra.Command {
|
|||
|
||||
return cmd
|
||||
}
|
||||
func NewBlockBestCommand() *cobra.Command {
|
||||
|
||||
var showHash bool
|
||||
var showHeight bool
|
||||
func NewBlockBestCommand() *cobra.Command {
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "best",
|
||||
Short: "Show the block hash and height of the best block",
|
||||
Short: "Show the height and hash of the best block",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
db, err := loadBlocksDB()
|
||||
|
@ -45,23 +43,12 @@ func NewBlockBestCommand() *cobra.Command {
|
|||
}
|
||||
|
||||
state := chain.BestSnapshot()
|
||||
|
||||
switch {
|
||||
case showHeight && showHash:
|
||||
fmt.Printf("%s:%d\n", state.Hash, state.Height)
|
||||
case !showHeight && showHash:
|
||||
fmt.Printf("%s\n", state.Hash)
|
||||
case showHeight && !showHash:
|
||||
fmt.Printf("%d\n", state.Height)
|
||||
}
|
||||
fmt.Printf("Block %7d: %s\n", state.Height, state.Hash.String())
|
||||
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
cmd.Flags().BoolVar(&showHeight, "showheight", true, "Display block height")
|
||||
cmd.Flags().BoolVar(&showHash, "showhash", true, "Display block hash")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
|
@ -69,12 +56,10 @@ func NewBlockListCommand() *cobra.Command {
|
|||
|
||||
var fromHeight int32
|
||||
var toHeight int32
|
||||
var showHash bool
|
||||
var showHeight bool
|
||||
|
||||
cmd := &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List block hash and height between blocks <from_height> <to_height>",
|
||||
Short: "List merkle hash of blocks between <from_height> <to_height>",
|
||||
Args: cobra.NoArgs,
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
|
@ -98,14 +83,7 @@ func NewBlockListCommand() *cobra.Command {
|
|||
if err != nil {
|
||||
return errors.Wrapf(err, "load hash for %d", ht)
|
||||
}
|
||||
switch {
|
||||
case showHeight && showHash:
|
||||
fmt.Printf("%s:%d\n", hash, ht)
|
||||
case !showHeight && showHash:
|
||||
fmt.Printf("%s\n", hash)
|
||||
case showHeight && !showHash:
|
||||
fmt.Printf("%d\n", ht)
|
||||
}
|
||||
fmt.Printf("Block %7d: %s\n", ht, hash.String())
|
||||
}
|
||||
|
||||
return nil
|
||||
|
@ -114,8 +92,6 @@ func NewBlockListCommand() *cobra.Command {
|
|||
|
||||
cmd.Flags().Int32Var(&fromHeight, "from", 0, "From height (inclusive)")
|
||||
cmd.Flags().Int32Var(&toHeight, "to", 0, "To height (inclusive)")
|
||||
cmd.Flags().BoolVar(&showHeight, "showheight", true, "Display block height")
|
||||
cmd.Flags().BoolVar(&showHash, "showhash", true, "Display block hash")
|
||||
cmd.Flags().SortFlags = false
|
||||
|
||||
return cmd
|
||||
|
|
|
@ -15,7 +15,7 @@ import (
|
|||
func loadBlocksDB() (database.DB, error) {
|
||||
|
||||
dbPath := filepath.Join(dataDir, netName, "blocks_ffldb")
|
||||
log.Debugf("Loading blocks database: %s", dbPath)
|
||||
log.Infof("Loading blocks database: %s", dbPath)
|
||||
db, err := database.Open("ffldb", dbPath, chainPramas().Net)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "open blocks database")
|
||||
|
@ -27,7 +27,7 @@ func loadBlocksDB() (database.DB, error) {
|
|||
func loadChain(db database.DB) (*blockchain.BlockChain, error) {
|
||||
paramsCopy := chaincfg.MainNetParams
|
||||
|
||||
log.Debugf("Loading chain from database")
|
||||
log.Infof("Loading chain from database")
|
||||
|
||||
startTime := time.Now()
|
||||
chain, err := blockchain.New(&blockchain.Config{
|
||||
|
@ -40,7 +40,7 @@ func loadChain(db database.DB) (*blockchain.BlockChain, error) {
|
|||
return nil, errors.Wrapf(err, "create blockchain")
|
||||
}
|
||||
|
||||
log.Debugf("Loaded chain from database (%s)", time.Since(startTime))
|
||||
log.Infof("Loaded chain from database (%s)", time.Since(startTime))
|
||||
|
||||
return chain, err
|
||||
|
||||
|
|
|
@ -3,21 +3,20 @@ package cmd
|
|||
import (
|
||||
"os"
|
||||
|
||||
"github.com/btcsuite/btclog"
|
||||
"github.com/lbryio/lbcd/claimtrie/config"
|
||||
"github.com/lbryio/lbcd/claimtrie/param"
|
||||
"github.com/lbryio/lbcd/limits"
|
||||
"github.com/lbryio/lbcd/wire"
|
||||
|
||||
"github.com/btcsuite/btclog"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
log = btclog.NewBackend(os.Stdout).Logger("CMDL")
|
||||
cfg = config.DefaultConfig
|
||||
netName string
|
||||
dataDir string
|
||||
debugLevel string
|
||||
log btclog.Logger
|
||||
cfg = config.DefaultConfig
|
||||
netName string
|
||||
dataDir string
|
||||
)
|
||||
|
||||
var rootCmd = NewRootCommand()
|
||||
|
@ -29,9 +28,6 @@ func NewRootCommand() *cobra.Command {
|
|||
Short: "ClaimTrie Command Line Interface",
|
||||
SilenceUsage: true,
|
||||
PersistentPreRun: func(cmd *cobra.Command, args []string) {
|
||||
level, _ := btclog.LevelFromString(debugLevel)
|
||||
log.SetLevel(level)
|
||||
|
||||
switch netName {
|
||||
case "mainnet":
|
||||
param.SetNetwork(wire.MainNet)
|
||||
|
@ -41,20 +37,21 @@ func NewRootCommand() *cobra.Command {
|
|||
param.SetNetwork(wire.TestNet)
|
||||
}
|
||||
},
|
||||
PersistentPostRun: func(cmd *cobra.Command, args []string) {
|
||||
os.Stdout.Sync()
|
||||
},
|
||||
}
|
||||
|
||||
cmd.PersistentFlags().StringVar(&netName, "netname", "mainnet", "Net name")
|
||||
cmd.PersistentFlags().StringVarP(&dataDir, "datadir", "b", cfg.DataDir, "Data dir")
|
||||
cmd.PersistentFlags().StringVarP(&debugLevel, "debuglevel", "d", cfg.DebugLevel, "Debug level")
|
||||
|
||||
return cmd
|
||||
}
|
||||
|
||||
func Execute() {
|
||||
|
||||
backendLogger := btclog.NewBackend(os.Stdout)
|
||||
defer os.Stdout.Sync()
|
||||
log = backendLogger.Logger("CMDL")
|
||||
log.SetLevel(btclog.LevelDebug)
|
||||
|
||||
// Up some limits.
|
||||
if err := limits.SetLimits(); err != nil {
|
||||
log.Errorf("failed to set limits: %v\n", err)
|
||||
|
|
|
@ -8,10 +8,11 @@ import (
|
|||
)
|
||||
|
||||
var DefaultConfig = Config{
|
||||
Params: param.MainNet,
|
||||
RamTrie: true, // as it stands the other trie uses more RAM, more time, and 40GB+ of disk space
|
||||
DebugLevel: "info",
|
||||
DataDir: filepath.Join(btcutil.AppDataDir("lbcd", false), "data"),
|
||||
Params: param.MainNet,
|
||||
|
||||
RamTrie: true, // as it stands the other trie uses more RAM, more time, and 40GB+ of disk space
|
||||
|
||||
DataDir: filepath.Join(btcutil.AppDataDir("chain", false), "data"),
|
||||
|
||||
BlockRepoPebble: pebbleConfig{
|
||||
Path: "blocks_pebble_db",
|
||||
|
@ -29,10 +30,11 @@ var DefaultConfig = Config{
|
|||
|
||||
// Config is the container of all configurations.
|
||||
type Config struct {
|
||||
Params param.ClaimTrieParams
|
||||
RamTrie bool
|
||||
DataDir string
|
||||
DebugLevel string
|
||||
Params param.ClaimTrieParams
|
||||
|
||||
RamTrie bool
|
||||
|
||||
DataDir string
|
||||
|
||||
BlockRepoPebble pebbleConfig
|
||||
NodeRepoPebble pebbleConfig
|
||||
|
|
|
@ -1,73 +0,0 @@
|
|||
// Copyright (c) 2015-2017 The btcsuite developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package claimtrie
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/btcsuite/btclog"
|
||||
)
|
||||
|
||||
// claimProgressLogger provides periodic logging for other services in order
|
||||
// to show users progress of certain "actions" involving some or all current
|
||||
// claim names. Ex: rebuilding claimtrie.
|
||||
type claimProgressLogger struct {
|
||||
totalLogName int64
|
||||
recentLogName int64
|
||||
lastLogNameTime time.Time
|
||||
|
||||
subsystemLogger btclog.Logger
|
||||
progressAction string
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
// newClaimProgressLogger returns a new name progress logger.
|
||||
// The progress message is templated as follows:
|
||||
//
|
||||
// {progressAction} {numProcessed} {names|name} in the last {timePeriod} (total {totalProcessed})
|
||||
func newClaimProgressLogger(progressMessage string, logger btclog.Logger) *claimProgressLogger {
|
||||
return &claimProgressLogger{
|
||||
lastLogNameTime: time.Now(),
|
||||
progressAction: progressMessage,
|
||||
subsystemLogger: logger,
|
||||
}
|
||||
}
|
||||
|
||||
// LogName logs a new claim name as an information message to show progress
|
||||
// to the user. In order to prevent spam, it limits logging to one message
|
||||
// every 10 seconds with duration and totals included.
|
||||
func (n *claimProgressLogger) LogName(name []byte) {
|
||||
n.Lock()
|
||||
defer n.Unlock()
|
||||
|
||||
n.totalLogName++
|
||||
n.recentLogName++
|
||||
|
||||
now := time.Now()
|
||||
duration := now.Sub(n.lastLogNameTime)
|
||||
if duration < time.Second*10 {
|
||||
return
|
||||
}
|
||||
|
||||
// Truncate the duration to 10s of milliseconds.
|
||||
durationMillis := int64(duration / time.Millisecond)
|
||||
tDuration := 10 * time.Millisecond * time.Duration(durationMillis/10)
|
||||
|
||||
// Log information about progress.
|
||||
nameStr := "names"
|
||||
if n.recentLogName == 1 {
|
||||
nameStr = "name"
|
||||
}
|
||||
n.subsystemLogger.Infof("%s %d claim %s in the last %s (total %d)",
|
||||
n.progressAction, n.recentLogName, nameStr, tDuration, n.totalLogName)
|
||||
|
||||
n.recentLogName = 0
|
||||
n.lastLogNameTime = now
|
||||
}
|
||||
|
||||
func (n *claimProgressLogger) SetLastLogTime(time time.Time) {
|
||||
n.lastLogNameTime = time
|
||||
}
|
|
@ -17,11 +17,10 @@ func newVertex(hash *chainhash.Hash) *vertex {
|
|||
// TODO: more professional to use msgpack here?
|
||||
|
||||
// nbuf decodes the on-disk format of a node, which has the following form:
|
||||
//
|
||||
// ch(1B) hash(32B)
|
||||
// ...
|
||||
// ch(1B) hash(32B)
|
||||
// vhash(32B)
|
||||
// ch(1B) hash(32B)
|
||||
// ...
|
||||
// ch(1B) hash(32B)
|
||||
// vhash(32B)
|
||||
type nbuf []byte
|
||||
|
||||
func (nb nbuf) entries() int {
|
||||
|
|
|
@ -1,85 +0,0 @@
|
|||
package node
|
||||
|
||||
import (
|
||||
"container/list"
|
||||
|
||||
"github.com/lbryio/lbcd/claimtrie/change"
|
||||
)
|
||||
|
||||
type cacheLeaf struct {
|
||||
node *Node
|
||||
element *list.Element
|
||||
changes []change.Change
|
||||
height int32
|
||||
}
|
||||
|
||||
type Cache struct {
|
||||
nodes map[string]*cacheLeaf
|
||||
order *list.List
|
||||
limit int
|
||||
}
|
||||
|
||||
func (nc *Cache) insert(name []byte, n *Node, height int32) {
|
||||
key := string(name)
|
||||
|
||||
existing := nc.nodes[key]
|
||||
if existing != nil {
|
||||
existing.node = n
|
||||
existing.height = height
|
||||
existing.changes = nil
|
||||
nc.order.MoveToFront(existing.element)
|
||||
return
|
||||
}
|
||||
|
||||
for nc.order.Len() >= nc.limit {
|
||||
// TODO: maybe ensure that we don't remove nodes that have a lot of changes?
|
||||
delete(nc.nodes, nc.order.Back().Value.(string))
|
||||
nc.order.Remove(nc.order.Back())
|
||||
}
|
||||
|
||||
element := nc.order.PushFront(key)
|
||||
nc.nodes[key] = &cacheLeaf{node: n, element: element, height: height}
|
||||
}
|
||||
|
||||
func (nc *Cache) fetch(name []byte, height int32) (*Node, []change.Change, int32) {
|
||||
key := string(name)
|
||||
|
||||
existing := nc.nodes[key]
|
||||
if existing != nil && existing.height <= height {
|
||||
nc.order.MoveToFront(existing.element)
|
||||
return existing.node, existing.changes, existing.height
|
||||
}
|
||||
return nil, nil, -1
|
||||
}
|
||||
|
||||
func (nc *Cache) addChanges(changes []change.Change, height int32) {
|
||||
for _, c := range changes {
|
||||
key := string(c.Name)
|
||||
existing := nc.nodes[key]
|
||||
if existing != nil && existing.height <= height {
|
||||
existing.changes = append(existing.changes, c)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (nc *Cache) drop(names [][]byte) {
|
||||
for _, name := range names {
|
||||
key := string(name)
|
||||
existing := nc.nodes[key]
|
||||
if existing != nil {
|
||||
// we can't roll it backwards because we don't know its previous height value; just toast it
|
||||
delete(nc.nodes, key)
|
||||
nc.order.Remove(existing.element)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (nc *Cache) clear() {
|
||||
nc.nodes = map[string]*cacheLeaf{}
|
||||
nc.order = list.New()
|
||||
// we'll let the GC sort out the remains...
|
||||
}
|
||||
|
||||
func NewCache(limit int) *Cache {
|
||||
return &Cache{limit: limit, nodes: map[string]*cacheLeaf{}, order: list.New()}
|
||||
}
|
|
@ -29,10 +29,6 @@ func UseLogger(logger btclog.Logger) {
|
|||
log = logger
|
||||
}
|
||||
|
||||
func GetLogger() btclog.Logger {
|
||||
return log
|
||||
}
|
||||
|
||||
var loggedStrings = map[string]bool{} // is this gonna get too large?
|
||||
var loggedStringsMutex sync.Mutex
|
||||
|
||||
|
@ -46,10 +42,6 @@ func LogOnce(s string) {
|
|||
log.Info(s)
|
||||
}
|
||||
|
||||
func Log(s string) {
|
||||
log.Info(s)
|
||||
}
|
||||
|
||||
func Warn(s string) {
|
||||
log.Warn(s)
|
||||
}
|
||||
|
|
|
@ -21,7 +21,6 @@ type Manager interface {
|
|||
IterateNames(predicate func(name []byte) bool)
|
||||
Hash(name []byte) (*chainhash.Hash, int32)
|
||||
Flush() error
|
||||
ClearCache()
|
||||
}
|
||||
|
||||
type BaseManager struct {
|
||||
|
@ -31,62 +30,31 @@ type BaseManager struct {
|
|||
changes []change.Change
|
||||
|
||||
tempChanges map[string][]change.Change
|
||||
|
||||
cache *Cache
|
||||
}
|
||||
|
||||
func NewBaseManager(repo Repo) (*BaseManager, error) {
|
||||
|
||||
nm := &BaseManager{
|
||||
repo: repo,
|
||||
cache: NewCache(10000), // TODO: how many should we cache?
|
||||
repo: repo,
|
||||
}
|
||||
|
||||
return nm, nil
|
||||
}
|
||||
|
||||
func (nm *BaseManager) ClearCache() {
|
||||
nm.cache.clear()
|
||||
}
|
||||
|
||||
func (nm *BaseManager) NodeAt(height int32, name []byte) (*Node, error) {
|
||||
|
||||
n, changes, oldHeight := nm.cache.fetch(name, height)
|
||||
if n == nil {
|
||||
changes, err := nm.repo.LoadChanges(name)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "in load changes")
|
||||
}
|
||||
changes, err := nm.repo.LoadChanges(name)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "in load changes")
|
||||
}
|
||||
|
||||
if nm.tempChanges != nil { // making an assumption that we only ever have tempChanges for a single block
|
||||
changes = append(changes, nm.tempChanges[string(name)]...)
|
||||
}
|
||||
if nm.tempChanges != nil { // making an assumption that we only ever have tempChanges for a single block
|
||||
changes = append(changes, nm.tempChanges[string(name)]...)
|
||||
}
|
||||
|
||||
n, err = nm.newNodeFromChanges(changes, height)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "in new node")
|
||||
}
|
||||
// TODO: how can we tell what needs to be cached?
|
||||
if nm.tempChanges == nil && height == nm.height && n != nil && (len(changes) > 4 || len(name) < 12) {
|
||||
nm.cache.insert(name, n, height)
|
||||
}
|
||||
} else {
|
||||
if nm.tempChanges != nil { // making an assumption that we only ever have tempChanges for a single block
|
||||
changes = append(changes, nm.tempChanges[string(name)]...)
|
||||
n = n.Clone()
|
||||
} else if height != nm.height {
|
||||
n = n.Clone()
|
||||
}
|
||||
updated, err := nm.updateFromChanges(n, changes, height)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "in update from changes")
|
||||
}
|
||||
if !updated {
|
||||
n.AdjustTo(oldHeight, height, name)
|
||||
}
|
||||
if nm.tempChanges == nil && height == nm.height {
|
||||
nm.cache.insert(name, n, height)
|
||||
}
|
||||
n, err := nm.newNodeFromChanges(changes, height)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "in new node")
|
||||
}
|
||||
|
||||
return n, nil
|
||||
|
@ -98,13 +66,17 @@ func (nm *BaseManager) node(name []byte) (*Node, error) {
|
|||
return nm.NodeAt(nm.height, name)
|
||||
}
|
||||
|
||||
func (nm *BaseManager) updateFromChanges(n *Node, changes []change.Change, height int32) (bool, error) {
|
||||
// newNodeFromChanges returns a new Node constructed from the changes.
|
||||
// The changes must preserve their order received.
|
||||
func (nm *BaseManager) newNodeFromChanges(changes []change.Change, height int32) (*Node, error) {
|
||||
|
||||
count := len(changes)
|
||||
if count == 0 {
|
||||
return false, nil
|
||||
if len(changes) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
n := New()
|
||||
previous := changes[0].Height
|
||||
count := len(changes)
|
||||
|
||||
for i, chg := range changes {
|
||||
if chg.Height < previous {
|
||||
|
@ -123,37 +95,15 @@ func (nm *BaseManager) updateFromChanges(n *Node, changes []change.Change, heigh
|
|||
delay := nm.getDelayForName(n, chg)
|
||||
err := n.ApplyChange(chg, delay)
|
||||
if err != nil {
|
||||
return false, errors.Wrap(err, "in apply change")
|
||||
return nil, errors.Wrap(err, "in apply change")
|
||||
}
|
||||
}
|
||||
|
||||
if count <= 0 {
|
||||
// we applied no changes, which means we shouldn't exist if we had all the changes
|
||||
// or might mean nothing significant if we are applying a partial changeset
|
||||
return false, nil
|
||||
}
|
||||
lastChange := changes[count-1]
|
||||
n.AdjustTo(lastChange.Height, height, lastChange.Name)
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// newNodeFromChanges returns a new Node constructed from the changes.
|
||||
// The changes must preserve their order received.
|
||||
func (nm *BaseManager) newNodeFromChanges(changes []change.Change, height int32) (*Node, error) {
|
||||
|
||||
if len(changes) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
n := New()
|
||||
updated, err := nm.updateFromChanges(n, changes, height)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "in update from changes")
|
||||
}
|
||||
if updated {
|
||||
return n, nil
|
||||
}
|
||||
return nil, nil
|
||||
lastChange := changes[count-1]
|
||||
return n.AdjustTo(lastChange.Height, height, lastChange.Name), nil
|
||||
}
|
||||
|
||||
func (nm *BaseManager) AppendChange(chg change.Change) {
|
||||
|
@ -270,7 +220,6 @@ func (nm *BaseManager) IncrementHeightTo(height int32, temporary bool) ([][]byte
|
|||
}
|
||||
|
||||
if !temporary {
|
||||
nm.cache.addChanges(nm.changes, height)
|
||||
if err := nm.repo.AppendChanges(nm.changes); err != nil { // destroys names
|
||||
return nil, errors.Wrap(err, "in append changes")
|
||||
}
|
||||
|
@ -306,8 +255,6 @@ func (nm *BaseManager) DecrementHeightTo(affectedNames [][]byte, height int32) (
|
|||
return affectedNames, errors.Wrap(err, "in drop changes")
|
||||
}
|
||||
}
|
||||
|
||||
nm.cache.drop(affectedNames)
|
||||
}
|
||||
nm.height = height
|
||||
|
||||
|
|
|
@ -110,7 +110,7 @@ func (n *Node) ApplyChange(chg change.Change, delay int32) error {
|
|||
}
|
||||
|
||||
// AdjustTo activates claims and computes takeovers until it reaches the specified height.
|
||||
func (n *Node) AdjustTo(height, maxHeight int32, name []byte) {
|
||||
func (n *Node) AdjustTo(height, maxHeight int32, name []byte) *Node {
|
||||
changed := n.handleExpiredAndActivated(height) > 0
|
||||
n.updateTakeoverHeight(height, name, changed)
|
||||
if maxHeight > height {
|
||||
|
@ -120,6 +120,7 @@ func (n *Node) AdjustTo(height, maxHeight int32, name []byte) {
|
|||
height = h
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (n *Node) updateTakeoverHeight(height int32, name []byte, refindBest bool) {
|
||||
|
@ -339,28 +340,3 @@ func (n *Node) SortClaimsByBid() {
|
|||
return OutPointLess(n.Claims[j].OutPoint, n.Claims[i].OutPoint)
|
||||
})
|
||||
}
|
||||
|
||||
func (n *Node) Clone() *Node {
|
||||
clone := New()
|
||||
if n.SupportSums != nil {
|
||||
clone.SupportSums = map[string]int64{}
|
||||
for key, value := range n.SupportSums {
|
||||
clone.SupportSums[key] = value
|
||||
}
|
||||
}
|
||||
clone.Supports = make(ClaimList, len(n.Supports))
|
||||
for i, support := range n.Supports {
|
||||
clone.Supports[i] = &Claim{}
|
||||
*clone.Supports[i] = *support
|
||||
}
|
||||
clone.Claims = make(ClaimList, len(n.Claims))
|
||||
for i, claim := range n.Claims {
|
||||
clone.Claims[i] = &Claim{}
|
||||
*clone.Claims[i] = *claim
|
||||
}
|
||||
clone.TakenOverAt = n.TakenOverAt
|
||||
if n.BestClaim != nil {
|
||||
clone.BestClaim = clone.Claims.find(byID(n.BestClaim.ClaimID))
|
||||
}
|
||||
return clone
|
||||
}
|
||||
|
|
|
@ -2,9 +2,7 @@ package noderepo
|
|||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/cockroachdb/pebble"
|
||||
"github.com/lbryio/lbcd/claimtrie/change"
|
||||
|
@ -15,76 +13,9 @@ type Pebble struct {
|
|||
db *pebble.DB
|
||||
}
|
||||
|
||||
type pooledMerger struct {
|
||||
values [][]byte
|
||||
index []int
|
||||
pool *sync.Pool
|
||||
buffer []byte
|
||||
}
|
||||
|
||||
func (a *pooledMerger) Len() int { return len(a.index) }
|
||||
func (a *pooledMerger) Less(i, j int) bool { return a.index[i] < a.index[j] }
|
||||
func (a *pooledMerger) Swap(i, j int) {
|
||||
a.index[i], a.index[j] = a.index[j], a.index[i]
|
||||
a.values[i], a.values[j] = a.values[j], a.values[i]
|
||||
}
|
||||
|
||||
func (a *pooledMerger) MergeNewer(value []byte) error {
|
||||
vc := a.pool.Get().([]byte)[:0]
|
||||
vc = append(vc, value...)
|
||||
a.values = append(a.values, vc)
|
||||
a.index = append(a.index, len(a.values))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *pooledMerger) MergeOlder(value []byte) error {
|
||||
vc := a.pool.Get().([]byte)[:0]
|
||||
vc = append(vc, value...)
|
||||
a.values = append(a.values, vc)
|
||||
a.index = append(a.index, -len(a.values))
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *pooledMerger) Finish(includesBase bool) ([]byte, io.Closer, error) {
|
||||
sort.Sort(a)
|
||||
|
||||
a.buffer = a.pool.Get().([]byte)[:0]
|
||||
for i := range a.values {
|
||||
a.buffer = append(a.buffer, a.values[i]...)
|
||||
}
|
||||
|
||||
return a.buffer, a, nil
|
||||
}
|
||||
|
||||
func (a *pooledMerger) Close() error {
|
||||
for i := range a.values {
|
||||
a.pool.Put(a.values[i])
|
||||
}
|
||||
a.pool.Put(a.buffer)
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewPebble(path string) (*Pebble, error) {
|
||||
|
||||
mp := &sync.Pool{
|
||||
New: func() interface{} {
|
||||
return make([]byte, 0, 256)
|
||||
},
|
||||
}
|
||||
|
||||
db, err := pebble.Open(path, &pebble.Options{
|
||||
Merger: &pebble.Merger{
|
||||
Merge: func(key, value []byte) (pebble.ValueMerger, error) {
|
||||
p := &pooledMerger{pool: mp}
|
||||
return p, p.MergeNewer(value)
|
||||
},
|
||||
Name: pebble.DefaultMerger.Name, // yes, it's a lie
|
||||
},
|
||||
Cache: pebble.NewCache(64 << 20),
|
||||
BytesPerSync: 8 << 20,
|
||||
MaxOpenFiles: 2000,
|
||||
})
|
||||
|
||||
db, err := pebble.Open(path, &pebble.Options{Cache: pebble.NewCache(64 << 20), BytesPerSync: 8 << 20, MaxOpenFiles: 2000})
|
||||
repo := &Pebble{db: db}
|
||||
|
||||
return repo, errors.Wrapf(err, "unable to open %s", path)
|
||||
|
|
|
@ -34,7 +34,6 @@ func (nm *NormalizingManager) IncrementHeightTo(height int32, temporary bool) ([
|
|||
func (nm *NormalizingManager) DecrementHeightTo(affectedNames [][]byte, height int32) ([][]byte, error) {
|
||||
if nm.normalizedAt > height {
|
||||
nm.normalizedAt = -1
|
||||
nm.ClearCache()
|
||||
}
|
||||
return nm.Manager.DecrementHeightTo(affectedNames, height)
|
||||
}
|
||||
|
@ -111,7 +110,5 @@ func (nm *NormalizingManager) addNormalizationForkChangesIfNecessary(height int3
|
|||
|
||||
return true
|
||||
}
|
||||
|
||||
nm.Manager.ClearCache()
|
||||
nm.Manager.IterateNames(predicate)
|
||||
}
|
||||
|
|
|
@ -111,8 +111,6 @@ type config struct {
|
|||
SigNet bool `long:"signet" description:"Connect to signet (default RPC server: localhost:49245)"`
|
||||
Wallet bool `long:"wallet" description:"Connect to wallet RPC server instead (default: localhost:9244, testnet: localhost:19244, regtest: localhost:29244)"`
|
||||
ShowVersion bool `short:"V" long:"version" description:"Display version information and exit"`
|
||||
Timed bool `short:"t" long:"timed" description:"Display RPC response time"`
|
||||
Quiet bool `short:"q" long:"quiet" description:"Do not output results to stdout"`
|
||||
}
|
||||
|
||||
// normalizeAddress returns addr with the passed default port appended if
|
||||
|
@ -177,10 +175,10 @@ func cleanAndExpandPath(path string) string {
|
|||
// line options.
|
||||
//
|
||||
// The configuration proceeds as follows:
|
||||
// 1. Start with a default config with sane settings
|
||||
// 2. Pre-parse the command line to check for an alternative config file
|
||||
// 3. Load configuration file overwriting defaults with any specified options
|
||||
// 4. Parse CLI options and overwrite/add any specified options
|
||||
// 1) Start with a default config with sane settings
|
||||
// 2) Pre-parse the command line to check for an alternative config file
|
||||
// 3) Load configuration file overwriting defaults with any specified options
|
||||
// 4) Parse CLI options and overwrite/add any specified options
|
||||
//
|
||||
// The above results in functioning properly without any config settings
|
||||
// while still allowing the user to override settings with config files and
|
||||
|
|
|
@ -9,7 +9,6 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lbryio/lbcd/btcjson"
|
||||
)
|
||||
|
@ -134,8 +133,6 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
started := time.Now()
|
||||
|
||||
// Send the JSON-RPC request to the server using the user-specified
|
||||
// connection configuration.
|
||||
result, err := sendPostRequest(marshalledJSON, cfg)
|
||||
|
@ -144,16 +141,6 @@ func main() {
|
|||
os.Exit(1)
|
||||
}
|
||||
|
||||
if cfg.Timed {
|
||||
elapsed := time.Since(started)
|
||||
defer fmt.Fprintf(os.Stderr, "%s\n", elapsed)
|
||||
}
|
||||
|
||||
var output io.Writer = os.Stdout
|
||||
if cfg.Quiet {
|
||||
output = io.Discard
|
||||
}
|
||||
|
||||
// Choose how to display the result based on its type.
|
||||
strResult := string(result)
|
||||
if strings.HasPrefix(strResult, "{") || strings.HasPrefix(strResult, "[") {
|
||||
|
@ -163,7 +150,7 @@ func main() {
|
|||
err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Fprintln(output, dst.String())
|
||||
fmt.Println(dst.String())
|
||||
|
||||
} else if strings.HasPrefix(strResult, `"`) {
|
||||
var str string
|
||||
|
@ -172,9 +159,9 @@ func main() {
|
|||
err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Fprintln(output, str)
|
||||
fmt.Println(str)
|
||||
|
||||
} else if strResult != "null" {
|
||||
fmt.Fprintln(output, strResult)
|
||||
fmt.Println(strResult)
|
||||
}
|
||||
}
|
||||
|
|
25
config.go
25
config.go
|
@ -117,7 +117,6 @@ type config struct {
|
|||
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
|
||||
ConnectPeers []string `long:"connect" description:"Connect only to the specified peers at startup"`
|
||||
CPUProfile string `long:"cpuprofile" description:"Write CPU profile to the specified file"`
|
||||
MemProfile string `long:"memprofile" description:"Write memory profile to the specified file"`
|
||||
DataDir string `short:"b" long:"datadir" description:"Directory to store data"`
|
||||
DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"`
|
||||
DebugLevel string `short:"d" long:"debuglevel" description:"Logging level for all subsystems {trace, debug, info, warn, error, critical} -- You may also specify <subsystem>=<level>,<subsystem2>=<level>,... to set the log level for individual subsystems -- Use show to list available subsystems"`
|
||||
|
@ -409,10 +408,10 @@ func newConfigParser(cfg *config, so *serviceOptions, options flags.Options) *fl
|
|||
// line options.
|
||||
//
|
||||
// The configuration proceeds as follows:
|
||||
// 1. Start with a default config with sane settings
|
||||
// 2. Pre-parse the command line to check for an alternative config file
|
||||
// 3. Load configuration file overwriting defaults with any specified options
|
||||
// 4. Parse CLI options and overwrite/add any specified options
|
||||
// 1) Start with a default config with sane settings
|
||||
// 2) Pre-parse the command line to check for an alternative config file
|
||||
// 3) Load configuration file overwriting defaults with any specified options
|
||||
// 4) Parse CLI options and overwrite/add any specified options
|
||||
//
|
||||
// The above results in lbcd functioning properly without any config settings
|
||||
// while still allowing the user to override settings with config files and
|
||||
|
@ -977,8 +976,13 @@ func loadConfig() (*config, []string, error) {
|
|||
// Only allow TLS to be disabled if the RPC is bound to localhost
|
||||
// addresses.
|
||||
if !cfg.DisableRPC && cfg.DisableTLS {
|
||||
allowedTLSListeners := map[string]struct{}{
|
||||
"localhost": {},
|
||||
"127.0.0.1": {},
|
||||
"::1": {},
|
||||
}
|
||||
for _, addr := range cfg.RPCListeners {
|
||||
_, _, err := net.SplitHostPort(addr)
|
||||
host, _, err := net.SplitHostPort(addr)
|
||||
if err != nil {
|
||||
str := "%s: RPC listen interface '%s' is " +
|
||||
"invalid: %v"
|
||||
|
@ -987,6 +991,15 @@ func loadConfig() (*config, []string, error) {
|
|||
fmt.Fprintln(os.Stderr, usageMessage)
|
||||
return nil, nil, err
|
||||
}
|
||||
if _, ok := allowedTLSListeners[host]; !ok {
|
||||
str := "%s: the --notls option may not be used " +
|
||||
"when binding RPC to non localhost " +
|
||||
"addresses: %s"
|
||||
err := fmt.Errorf(str, funcName, addr)
|
||||
fmt.Fprintln(os.Stderr, err)
|
||||
fmt.Fprintln(os.Stderr, usageMessage)
|
||||
return nil, nil, err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,6 +20,7 @@ var (
|
|||
// parameters which are command-line only. These fields are copied line-by-line
|
||||
// from "config" struct in "config.go", and the field names, types, and tags must
|
||||
// match for the test to work.
|
||||
//
|
||||
type configCmdLineOnly struct {
|
||||
ConfigFile string `short:"C" long:"configfile" description:"Path to configuration file"`
|
||||
DbType string `long:"dbtype" description:"Database backend to use for the Block Chain"`
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
/*
|
||||
Package connmgr implements a generic Bitcoin network connection manager.
|
||||
|
||||
# Connection Manager Overview
|
||||
Connection Manager Overview
|
||||
|
||||
Connection Manager handles all the general connection concerns such as
|
||||
maintaining a set number of outbound connections, sourcing peers, banning,
|
||||
|
|
|
@ -1,42 +0,0 @@
|
|||
#! /bin/bash
|
||||
|
||||
read -r -d '' help << EOM
|
||||
$0 - helper script for displaying miner of a mined block.
|
||||
|
||||
Options:
|
||||
|
||||
-h Display this message.
|
||||
|
||||
--height Specify blockheight.
|
||||
--hash Specify blockhash.
|
||||
EOM
|
||||
|
||||
while getopts ":h-:" optchar; do
|
||||
case "${optchar}" in
|
||||
-)
|
||||
case "${OPTARG}" in
|
||||
hash)
|
||||
blockhash="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
|
||||
;;
|
||||
height)
|
||||
blockheight="${!OPTIND}"; OPTIND=$(( $OPTIND + 1 ))
|
||||
blockhash=$(lbcctl getblockhash ${blockheight})
|
||||
;;
|
||||
*) echo "Unknown long option --${OPTARG}" >&2; exit -2 ;;
|
||||
esac
|
||||
;;
|
||||
h) printf "${help}\n\n"; exit 2;;
|
||||
*) echo "Unknown option -${OPTARG}" >&2; exit -2;;
|
||||
esac
|
||||
done
|
||||
|
||||
|
||||
block=$(lbcctl getblock $blockhash)
|
||||
blockheight=$(lbcctl getblock $blockhash | jq -r .height)
|
||||
|
||||
coinbase_txid=$(echo ${block} | jq -r '.tx[0]')
|
||||
coinbase_raw=$(lbcctl getrawtransaction ${coinbase_txid} 1)
|
||||
coinbase=$(echo ${coinbase_raw} | jq '.vin[0].coinbase')
|
||||
miner=$(echo ${coinbase} | grep -o '2f.*2f' | xxd -r -p | strings)
|
||||
|
||||
echo ${blockheight}: ${blockhash}: ${miner}
|
|
@ -1,63 +0,0 @@
|
|||
#!/bin/sh
|
||||
|
||||
read -r -d '' help << EOM
|
||||
snapshot.sh - helper script for generating snapshot from lbcd's app dir.
|
||||
|
||||
The default output name "lbcd_snapshot_<height>_<lbcd_ver>_<date>.tar.zst"
|
||||
|
||||
To extract the snapshot (data/mainter/):
|
||||
|
||||
zstd -d lbcd_snapshot_<height>_<lbcd_ver>_<date>.tar.zst | tar xf - -C <appdir>
|
||||
|
||||
Default appdir of lbcd on different OSes:
|
||||
|
||||
Darwin) "\${HOME}/Library/Application Support/Lbcd"
|
||||
Linux) "\${HOME}/.lbcd"
|
||||
Windows) "%%LOCALAPPDATA%%/lbcd"
|
||||
|
||||
Options:
|
||||
|
||||
-h Display this message.
|
||||
-d Specify APPDIR to copy the snapshot from.
|
||||
|
||||
-o Specify the output filename of snapshot.
|
||||
-b Specify the best block height of the snapshot. (ignored if -o is specified)
|
||||
-l Specify git tag of the running lbcd. (ignored if -o is specified)
|
||||
-t Specify the date when the snapshot is generated. (ignored if -o is specified)
|
||||
EOM
|
||||
|
||||
while getopts o:d:b:l:t:h flag
|
||||
do
|
||||
case "${flag}" in
|
||||
h) printf "${help}\n\n"; exit 0;;
|
||||
d) appdir=${OPTARG};;
|
||||
|
||||
o) snapshot=${OPTARG};;
|
||||
b) height=${OPTARG};;
|
||||
l) lbcd_ver=${OPTARG};;
|
||||
t) date=${OPTARG};;
|
||||
esac
|
||||
done
|
||||
|
||||
if [ -z "$appdir" ]; then
|
||||
case $(uname) in
|
||||
Darwin) appdir="${HOME}/Library/Application Support/Lbcd" ;;
|
||||
Linux) appdir="${HOME}/.lbcd" ;;
|
||||
Windows) appdir="%LOCALAPPDATA%/lbcd" ;;
|
||||
esac
|
||||
fi
|
||||
|
||||
|
||||
if [ -z ${snapshot} ]; then
|
||||
git_repo=$(git rev-parse --show-toplevel)
|
||||
[ -z "${height}" ] && height=$(go run ${git_repo}/claimtrie/cmd block best --showhash=false)
|
||||
[ -z "${lbcd_ver}" ] && lbcd_ver=$(git describe --tags)
|
||||
[ -z "${date}" ] && date=$(date +"%Y-%m-%d")
|
||||
snapshot="lbcd_snapshot_${height}_${lbcd_ver}_${date}.tar.zst"
|
||||
fi
|
||||
|
||||
|
||||
echo "Generating $snapshot ..."
|
||||
|
||||
tar c -C "${appdir}" data/mainnet | zstd -9 --no-progress -o "${snapshot}"
|
||||
|
|
@ -5,7 +5,7 @@
|
|||
/*
|
||||
Package database provides a block and metadata storage database.
|
||||
|
||||
# Overview
|
||||
Overview
|
||||
|
||||
As of Feb 2016, there are over 400,000 blocks in the Bitcoin block chain and
|
||||
and over 112 million transactions (which turns out to be over 60GB of data).
|
||||
|
@ -18,15 +18,15 @@ storage, and strict checksums in key areas to ensure data integrity.
|
|||
|
||||
A quick overview of the features database provides are as follows:
|
||||
|
||||
- Key/value metadata store
|
||||
- Bitcoin block storage
|
||||
- Efficient retrieval of block headers and regions (transactions, scripts, etc)
|
||||
- Read-only and read-write transactions with both manual and managed modes
|
||||
- Nested buckets
|
||||
- Supports registration of backend databases
|
||||
- Comprehensive test coverage
|
||||
- Key/value metadata store
|
||||
- Bitcoin block storage
|
||||
- Efficient retrieval of block headers and regions (transactions, scripts, etc)
|
||||
- Read-only and read-write transactions with both manual and managed modes
|
||||
- Nested buckets
|
||||
- Supports registration of backend databases
|
||||
- Comprehensive test coverage
|
||||
|
||||
# Database
|
||||
Database
|
||||
|
||||
The main entry point is the DB interface. It exposes functionality for
|
||||
transactional-based access and storage of metadata and block data. It is
|
||||
|
@ -43,14 +43,14 @@ The Begin function provides an unmanaged transaction while the View and Update
|
|||
functions provide a managed transaction. These are described in more detail
|
||||
below.
|
||||
|
||||
# Transactions
|
||||
Transactions
|
||||
|
||||
The Tx interface provides facilities for rolling back or committing changes that
|
||||
took place while the transaction was active. It also provides the root metadata
|
||||
bucket under which all keys, values, and nested buckets are stored. A
|
||||
transaction can either be read-only or read-write and managed or unmanaged.
|
||||
|
||||
# Managed versus Unmanaged Transactions
|
||||
Managed versus Unmanaged Transactions
|
||||
|
||||
A managed transaction is one where the caller provides a function to execute
|
||||
within the context of the transaction and the commit or rollback is handled
|
||||
|
@ -63,7 +63,7 @@ call Commit or Rollback when they are finished with it. Leaving transactions
|
|||
open for long periods of time can have several adverse effects, so it is
|
||||
recommended that managed transactions are used instead.
|
||||
|
||||
# Buckets
|
||||
Buckets
|
||||
|
||||
The Bucket interface provides the ability to manipulate key/value pairs and
|
||||
nested buckets as well as iterate through them.
|
||||
|
@ -73,7 +73,7 @@ CreateBucket, CreateBucketIfNotExists, and DeleteBucket functions work with
|
|||
buckets. The ForEach function allows the caller to provide a function to be
|
||||
called with each key/value pair and nested bucket in the current bucket.
|
||||
|
||||
# Metadata Bucket
|
||||
Metadata Bucket
|
||||
|
||||
As discussed above, all of the functions which are used to manipulate key/value
|
||||
pairs and nested buckets exist on the Bucket interface. The root metadata
|
||||
|
@ -81,7 +81,7 @@ bucket is the upper-most bucket in which data is stored and is created at the
|
|||
same time as the database. Use the Metadata function on the Tx interface
|
||||
to retrieve it.
|
||||
|
||||
# Nested Buckets
|
||||
Nested Buckets
|
||||
|
||||
The CreateBucket and CreateBucketIfNotExists functions on the Bucket interface
|
||||
provide the ability to create an arbitrary number of nested buckets. It is
|
||||
|
|
|
@ -622,8 +622,8 @@ func (s *blockStore) syncBlocks() error {
|
|||
// were partially written.
|
||||
//
|
||||
// There are effectively two scenarios to consider here:
|
||||
// 1. Transient write failures from which recovery is possible
|
||||
// 2. More permanent failures such as hard disk death and/or removal
|
||||
// 1) Transient write failures from which recovery is possible
|
||||
// 2) More permanent failures such as hard disk death and/or removal
|
||||
//
|
||||
// In either case, the write cursor will be repositioned to the old block file
|
||||
// offset regardless of any other errors that occur while attempting to undo
|
||||
|
|
|
@ -10,7 +10,7 @@ This driver is the recommended driver for use with lbcd. It makes use leveldb
|
|||
for the metadata, flat files for block storage, and checksums in key areas to
|
||||
ensure data integrity.
|
||||
|
||||
# Usage
|
||||
Usage
|
||||
|
||||
This package is a driver to the database package and provides the database type
|
||||
of "ffldb". The parameters the Open and Create functions take are the
|
||||
|
|
|
@ -318,14 +318,13 @@ func (iter *Iterator) ForceReseek() {
|
|||
// unexpected keys and/or values.
|
||||
//
|
||||
// For example:
|
||||
//
|
||||
// iter := t.Iterator(nil, nil)
|
||||
// for iter.Next() {
|
||||
// if someCondition {
|
||||
// t.Delete(iter.Key())
|
||||
// iter.ForceReseek()
|
||||
// }
|
||||
// }
|
||||
// iter := t.Iterator(nil, nil)
|
||||
// for iter.Next() {
|
||||
// if someCondition {
|
||||
// t.Delete(iter.Key())
|
||||
// iter.ForceReseek()
|
||||
// }
|
||||
// }
|
||||
func (t *Mutable) Iterator(startKey, limitKey []byte) *Iterator {
|
||||
iter := &Iterator{
|
||||
t: t,
|
||||
|
|
267
doc.go
267
doc.go
|
@ -18,144 +18,143 @@ on Windows. The -C (--configfile) flag, as shown below, can be used to override
|
|||
this location.
|
||||
|
||||
Usage:
|
||||
|
||||
lbcd [OPTIONS]
|
||||
lbcd [OPTIONS]
|
||||
|
||||
Application Options:
|
||||
|
||||
--addcheckpoint= Add a custom checkpoint. Format:
|
||||
'<height>:<hash>'
|
||||
-a, --addpeer= Add a peer to connect with at startup
|
||||
--addrindex Maintain a full address-based transaction index
|
||||
which makes the searchrawtransactions RPC
|
||||
available
|
||||
--banduration= How long to ban misbehaving peers. Valid time
|
||||
units are {s, m, h}. Minimum 1 second (default:
|
||||
24h0m0s)
|
||||
--banthreshold= Maximum allowed ban score before disconnecting
|
||||
and banning misbehaving peers. (default: 100)
|
||||
--blockmaxsize= Maximum block size in bytes to be used when
|
||||
creating a block (default: 750000)
|
||||
--blockminsize= Mininum block size in bytes to be used when
|
||||
creating a block
|
||||
--blockmaxweight= Maximum block weight to be used when creating a
|
||||
block (default: 3000000)
|
||||
--blockminweight= Mininum block weight to be used when creating a
|
||||
block
|
||||
--blockprioritysize= Size in bytes for high-priority/low-fee
|
||||
transactions when creating a block (default:
|
||||
50000)
|
||||
--blocksonly Do not accept transactions from remote peers.
|
||||
-C, --configfile= Path to configuration file
|
||||
--connect= Connect only to the specified peers at startup
|
||||
--cpuprofile= Write CPU profile to the specified file
|
||||
-b, --datadir= Directory to store data
|
||||
--dbtype= Database backend to use for the Block Chain
|
||||
(default: ffldb)
|
||||
-d, --debuglevel= Logging level for all subsystems {trace, debug,
|
||||
info, warn, error, critical} -- You may also
|
||||
specify
|
||||
<subsystem>=<level>,<subsystem2>=<level>,... to
|
||||
set the log level for individual subsystems --
|
||||
Use show to list available subsystems (default:
|
||||
info)
|
||||
--dropaddrindex Deletes the address-based transaction index from
|
||||
the database on start up and then exits.
|
||||
--dropcfindex Deletes the index used for committed filtering
|
||||
(CF) support from the database on start up and
|
||||
then exits.
|
||||
--droptxindex Deletes the hash-based transaction index from the
|
||||
database on start up and then exits.
|
||||
--externalip= Add an ip to the list of local addresses we claim
|
||||
to listen on to peers
|
||||
--generate Generate (mine) bitcoins using the CPU
|
||||
--limitfreerelay= Limit relay of transactions with no transaction
|
||||
fee to the given amount in thousands of bytes per
|
||||
minute (default: 15)
|
||||
--listen= Add an interface/port to listen for connections
|
||||
(default all interfaces port: 9246, testnet:
|
||||
19246, regtest: 29246, signet: 39246)
|
||||
--logdir= Directory to log output
|
||||
--maxorphantx= Max number of orphan transactions to keep in
|
||||
memory (default: 100)
|
||||
--maxpeers= Max number of inbound and outbound peers
|
||||
(default: 125)
|
||||
--memprofile= Write memory profile to the specified file
|
||||
--miningaddr= Add the specified payment address to the list of
|
||||
addresses to use for generated blocks -- At least
|
||||
one address is required if the generate option is
|
||||
set
|
||||
--minrelaytxfee= The minimum transaction fee in BTC/kB to be
|
||||
considered a non-zero fee. (default: 1e-05)
|
||||
--nobanning Disable banning of misbehaving peers
|
||||
--nocfilters Disable committed filtering (CF) support
|
||||
--nocheckpoints Disable built-in checkpoints. Don't do this
|
||||
unless you know what you're doing.
|
||||
--nodnsseed Disable DNS seeding for peers
|
||||
--nolisten Disable listening for incoming connections --
|
||||
NOTE: Listening is automatically disabled if the
|
||||
--connect or --proxy options are used without
|
||||
also specifying listen interfaces via --listen
|
||||
--noonion Disable connecting to tor hidden services
|
||||
--nopeerbloomfilters Disable bloom filtering support
|
||||
--norelaypriority Do not require free or low-fee transactions to
|
||||
have high priority for relaying
|
||||
--norpc Disable built-in RPC server -- NOTE: The RPC
|
||||
server is disabled by default if no
|
||||
rpcuser/rpcpass or rpclimituser/rpclimitpass is
|
||||
specified
|
||||
--notls Disable TLS for the RPC server
|
||||
--onion= Connect to tor hidden services via SOCKS5 proxy
|
||||
(eg. 127.0.0.1:9050)
|
||||
--onionpass= Password for onion proxy server
|
||||
--onionuser= Username for onion proxy server
|
||||
--profile= Enable HTTP profiling on given port -- NOTE port
|
||||
must be between 1024 and 65536
|
||||
--proxy= Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)
|
||||
--proxypass= Password for proxy server
|
||||
--proxyuser= Username for proxy server
|
||||
--regtest Use the regression test network
|
||||
--rejectnonstd Reject non-standard transactions regardless of
|
||||
the default settings for the active network.
|
||||
--relaynonstd Relay non-standard transactions regardless of the
|
||||
default settings for the active network.
|
||||
--rpccert= File containing the certificate file
|
||||
--rpckey= File containing the certificate key
|
||||
--rpclimitpass= Password for limited RPC connections
|
||||
--rpclimituser= Username for limited RPC connections
|
||||
--rpclisten= Add an interface/port to listen for RPC
|
||||
connections (default port: 9245, testnet: 19245, regtest: 29245)
|
||||
--rpcmaxclients= Max number of RPC clients for standard
|
||||
connections (default: 10)
|
||||
--rpcmaxconcurrentreqs= Max number of concurrent RPC requests that may be
|
||||
processed concurrently (default: 20)
|
||||
--rpcmaxwebsockets= Max number of RPC websocket connections (default:
|
||||
25)
|
||||
--rpcquirks Mirror some JSON-RPC quirks of Bitcoin Core --
|
||||
NOTE: Discouraged unless interoperability issues
|
||||
need to be worked around
|
||||
-P, --rpcpass= Password for RPC connections
|
||||
-u, --rpcuser= Username for RPC connections
|
||||
--sigcachemaxsize= The maximum number of entries in the signature
|
||||
verification cache (default: 100000)
|
||||
--simnet Use the simulation test network
|
||||
--testnet Use the test network
|
||||
--torisolation Enable Tor stream isolation by randomizing user
|
||||
credentials for each connection.
|
||||
--trickleinterval= Minimum time between attempts to send new
|
||||
inventory to a connected peer (default: 10s)
|
||||
--txindex Maintain a full hash-based transaction index
|
||||
which makes all transactions available via the
|
||||
getrawtransaction RPC
|
||||
--uacomment= Comment to add to the user agent -- See BIP 14
|
||||
for more information.
|
||||
--upnp Use UPnP to map our listening port outside of NAT
|
||||
-V, --version Display version information and exit
|
||||
--whitelist= Add an IP network or IP that will not be banned.
|
||||
(eg. 192.168.1.0/24 or ::1)
|
||||
--addcheckpoint= Add a custom checkpoint. Format:
|
||||
'<height>:<hash>'
|
||||
-a, --addpeer= Add a peer to connect with at startup
|
||||
--addrindex Maintain a full address-based transaction index
|
||||
which makes the searchrawtransactions RPC
|
||||
available
|
||||
--banduration= How long to ban misbehaving peers. Valid time
|
||||
units are {s, m, h}. Minimum 1 second (default:
|
||||
24h0m0s)
|
||||
--banthreshold= Maximum allowed ban score before disconnecting
|
||||
and banning misbehaving peers. (default: 100)
|
||||
--blockmaxsize= Maximum block size in bytes to be used when
|
||||
creating a block (default: 750000)
|
||||
--blockminsize= Mininum block size in bytes to be used when
|
||||
creating a block
|
||||
--blockmaxweight= Maximum block weight to be used when creating a
|
||||
block (default: 3000000)
|
||||
--blockminweight= Mininum block weight to be used when creating a
|
||||
block
|
||||
--blockprioritysize= Size in bytes for high-priority/low-fee
|
||||
transactions when creating a block (default:
|
||||
50000)
|
||||
--blocksonly Do not accept transactions from remote peers.
|
||||
-C, --configfile= Path to configuration file
|
||||
--connect= Connect only to the specified peers at startup
|
||||
--cpuprofile= Write CPU profile to the specified file
|
||||
-b, --datadir= Directory to store data
|
||||
--dbtype= Database backend to use for the Block Chain
|
||||
(default: ffldb)
|
||||
-d, --debuglevel= Logging level for all subsystems {trace, debug,
|
||||
info, warn, error, critical} -- You may also
|
||||
specify
|
||||
<subsystem>=<level>,<subsystem2>=<level>,... to
|
||||
set the log level for individual subsystems --
|
||||
Use show to list available subsystems (default:
|
||||
info)
|
||||
--dropaddrindex Deletes the address-based transaction index from
|
||||
the database on start up and then exits.
|
||||
--dropcfindex Deletes the index used for committed filtering
|
||||
(CF) support from the database on start up and
|
||||
then exits.
|
||||
--droptxindex Deletes the hash-based transaction index from the
|
||||
database on start up and then exits.
|
||||
--externalip= Add an ip to the list of local addresses we claim
|
||||
to listen on to peers
|
||||
--generate Generate (mine) bitcoins using the CPU
|
||||
--limitfreerelay= Limit relay of transactions with no transaction
|
||||
fee to the given amount in thousands of bytes per
|
||||
minute (default: 15)
|
||||
--listen= Add an interface/port to listen for connections
|
||||
(default all interfaces port: 9246, testnet:
|
||||
19246, regtest: 29246, signet: 39246)
|
||||
--logdir= Directory to log output
|
||||
--maxorphantx= Max number of orphan transactions to keep in
|
||||
memory (default: 100)
|
||||
--maxpeers= Max number of inbound and outbound peers
|
||||
(default: 125)
|
||||
--miningaddr= Add the specified payment address to the list of
|
||||
addresses to use for generated blocks -- At least
|
||||
one address is required if the generate option is
|
||||
set
|
||||
--minrelaytxfee= The minimum transaction fee in BTC/kB to be
|
||||
considered a non-zero fee. (default: 1e-05)
|
||||
--nobanning Disable banning of misbehaving peers
|
||||
--nocfilters Disable committed filtering (CF) support
|
||||
--nocheckpoints Disable built-in checkpoints. Don't do this
|
||||
unless you know what you're doing.
|
||||
--nodnsseed Disable DNS seeding for peers
|
||||
--nolisten Disable listening for incoming connections --
|
||||
NOTE: Listening is automatically disabled if the
|
||||
--connect or --proxy options are used without
|
||||
also specifying listen interfaces via --listen
|
||||
--noonion Disable connecting to tor hidden services
|
||||
--nopeerbloomfilters Disable bloom filtering support
|
||||
--norelaypriority Do not require free or low-fee transactions to
|
||||
have high priority for relaying
|
||||
--norpc Disable built-in RPC server -- NOTE: The RPC
|
||||
server is disabled by default if no
|
||||
rpcuser/rpcpass or rpclimituser/rpclimitpass is
|
||||
specified
|
||||
--notls Disable TLS for the RPC server -- NOTE: This is
|
||||
only allowed if the RPC server is bound to
|
||||
localhost
|
||||
--onion= Connect to tor hidden services via SOCKS5 proxy
|
||||
(eg. 127.0.0.1:9050)
|
||||
--onionpass= Password for onion proxy server
|
||||
--onionuser= Username for onion proxy server
|
||||
--profile= Enable HTTP profiling on given port -- NOTE port
|
||||
must be between 1024 and 65536
|
||||
--proxy= Connect via SOCKS5 proxy (eg. 127.0.0.1:9050)
|
||||
--proxypass= Password for proxy server
|
||||
--proxyuser= Username for proxy server
|
||||
--regtest Use the regression test network
|
||||
--rejectnonstd Reject non-standard transactions regardless of
|
||||
the default settings for the active network.
|
||||
--relaynonstd Relay non-standard transactions regardless of the
|
||||
default settings for the active network.
|
||||
--rpccert= File containing the certificate file
|
||||
--rpckey= File containing the certificate key
|
||||
--rpclimitpass= Password for limited RPC connections
|
||||
--rpclimituser= Username for limited RPC connections
|
||||
--rpclisten= Add an interface/port to listen for RPC
|
||||
connections (default port: 9245, testnet: 19245, regtest: 29245)
|
||||
--rpcmaxclients= Max number of RPC clients for standard
|
||||
connections (default: 10)
|
||||
--rpcmaxconcurrentreqs= Max number of concurrent RPC requests that may be
|
||||
processed concurrently (default: 20)
|
||||
--rpcmaxwebsockets= Max number of RPC websocket connections (default:
|
||||
25)
|
||||
--rpcquirks Mirror some JSON-RPC quirks of Bitcoin Core --
|
||||
NOTE: Discouraged unless interoperability issues
|
||||
need to be worked around
|
||||
-P, --rpcpass= Password for RPC connections
|
||||
-u, --rpcuser= Username for RPC connections
|
||||
--sigcachemaxsize= The maximum number of entries in the signature
|
||||
verification cache (default: 100000)
|
||||
--simnet Use the simulation test network
|
||||
--testnet Use the test network
|
||||
--torisolation Enable Tor stream isolation by randomizing user
|
||||
credentials for each connection.
|
||||
--trickleinterval= Minimum time between attempts to send new
|
||||
inventory to a connected peer (default: 10s)
|
||||
--txindex Maintain a full hash-based transaction index
|
||||
which makes all transactions available via the
|
||||
getrawtransaction RPC
|
||||
--uacomment= Comment to add to the user agent -- See BIP 14
|
||||
for more information.
|
||||
--upnp Use UPnP to map our listening port outside of NAT
|
||||
-V, --version Display version information and exit
|
||||
--whitelist= Add an IP network or IP that will not be banned.
|
||||
(eg. 192.168.1.0/24 or ::1)
|
||||
|
||||
Help Options:
|
||||
-h, --help Show this help message
|
||||
|
||||
-h, --help Show this help message
|
||||
*/
|
||||
package main
|
||||
|
|
42
fees/doc.go
42
fees/doc.go
|
@ -7,11 +7,11 @@ Package fees provides decred-specific methods for tracking and estimating fee
|
|||
rates for new transactions to be mined into the network. Fee rate estimation has
|
||||
two main goals:
|
||||
|
||||
- Ensuring transactions are mined within a target _confirmation range_
|
||||
(expressed in blocks);
|
||||
- Attempting to minimize fees while maintaining be above restriction.
|
||||
- Ensuring transactions are mined within a target _confirmation range_
|
||||
(expressed in blocks);
|
||||
- Attempting to minimize fees while maintaining be above restriction.
|
||||
|
||||
# Preliminaries
|
||||
Preliminaries
|
||||
|
||||
There are two main regimes against which fee estimation needs to be evaluated
|
||||
according to how full blocks being mined are (and consequently how important fee
|
||||
|
@ -39,7 +39,7 @@ The current approach to implement this estimation is based on bitcoin core's
|
|||
algorithm. References [1] and [2] provide a high level description of how it
|
||||
works there. Actual code is linked in references [3] and [4].
|
||||
|
||||
# Outline of the Algorithm
|
||||
Outline of the Algorithm
|
||||
|
||||
The algorithm is currently based in fee estimation as used in v0.14 of bitcoin
|
||||
core (which is also the basis for the v0.15+ method). A more comprehensive
|
||||
|
@ -54,31 +54,31 @@ The basic algorithm is as follows (as executed by a single full node):
|
|||
|
||||
Stats building stage:
|
||||
|
||||
- For each transaction observed entering mempool, record the block at which it
|
||||
was first seen
|
||||
- For each mined transaction which was previously observed to enter the mempool,
|
||||
record how long (in blocks) it took to be mined and its fee rate
|
||||
- Group mined transactions into fee rate _buckets_ and _confirmation ranges_,
|
||||
creating a table of how many transactions were mined at each confirmation
|
||||
range and fee rate bucket and their total committed fee
|
||||
- Whenever a new block is mined, decay older transactions to account for a
|
||||
dynamic fee environment
|
||||
- For each transaction observed entering mempool, record the block at which it
|
||||
was first seen
|
||||
- For each mined transaction which was previously observed to enter the mempool,
|
||||
record how long (in blocks) it took to be mined and its fee rate
|
||||
- Group mined transactions into fee rate _buckets_ and _confirmation ranges_,
|
||||
creating a table of how many transactions were mined at each confirmation
|
||||
range and fee rate bucket and their total committed fee
|
||||
- Whenever a new block is mined, decay older transactions to account for a
|
||||
dynamic fee environment
|
||||
|
||||
Estimation stage:
|
||||
|
||||
- Input a target confirmation range (how many blocks to wait for the tx to be
|
||||
mined)
|
||||
- Starting at the highest fee bucket, look for buckets where the chance of
|
||||
confirmation within the desired confirmation window is > 95%
|
||||
- Average all such buckets to get the estimated fee rate
|
||||
- Input a target confirmation range (how many blocks to wait for the tx to be
|
||||
mined)
|
||||
- Starting at the highest fee bucket, look for buckets where the chance of
|
||||
confirmation within the desired confirmation window is > 95%
|
||||
- Average all such buckets to get the estimated fee rate
|
||||
|
||||
# Simulation
|
||||
Simulation
|
||||
|
||||
Development of the estimator was originally performed and simulated using the
|
||||
code in [5]. Simulation of the current code can be performed by using the
|
||||
dcrfeesim tool available in [6].
|
||||
|
||||
# Acknowledgements
|
||||
Acknowledgements
|
||||
|
||||
Thanks to @davecgh for providing the initial review of the results and the
|
||||
original developers of the bitcoin core code (the brunt of which seems to have
|
||||
|
|
4
go.mod
4
go.mod
|
@ -1,6 +1,6 @@
|
|||
module github.com/lbryio/lbcd
|
||||
|
||||
go 1.19
|
||||
go 1.18
|
||||
|
||||
require (
|
||||
github.com/btcsuite/btclog v0.0.0-20170628155309-84c8d2346e9f
|
||||
|
@ -14,7 +14,7 @@ require (
|
|||
github.com/felixge/fgprof v0.9.2
|
||||
github.com/jessevdk/go-flags v1.5.0
|
||||
github.com/jrick/logrotate v1.0.0
|
||||
github.com/lbryio/lbcutil v1.0.202
|
||||
github.com/lbryio/lbcutil v1.0.202-rc3
|
||||
github.com/pkg/errors v0.9.1
|
||||
github.com/shirou/gopsutil/v3 v3.22.4
|
||||
github.com/spf13/cobra v1.1.3
|
||||
|
|
4
go.sum
4
go.sum
|
@ -284,8 +284,8 @@ github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
|||
github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
|
||||
github.com/labstack/echo/v4 v4.5.0/go.mod h1:czIriw4a0C1dFun+ObrXp7ok03xON0N1awStJ6ArI7Y=
|
||||
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
|
||||
github.com/lbryio/lbcutil v1.0.202 h1:L0aRMs2bdCUAicD8Xe4NmUEvevDDea3qkIpCSACnftI=
|
||||
github.com/lbryio/lbcutil v1.0.202/go.mod h1:LGPtVBBzh4cFXfLFb8ginlFcbA2QwumLNFd0yk/as2o=
|
||||
github.com/lbryio/lbcutil v1.0.202-rc3 h1:J7zYnIj3iN/ndPYKqMKBukLaLM1GhCEaiaMOYIMdUCU=
|
||||
github.com/lbryio/lbcutil v1.0.202-rc3/go.mod h1:LGPtVBBzh4cFXfLFb8ginlFcbA2QwumLNFd0yk/as2o=
|
||||
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0/go.mod h1:zJYVVT2jmtg6P3p1VtQj7WsuWi/y4VnjVBn7F8KPB3I=
|
||||
github.com/lufia/plan9stats v0.0.0-20220517141722-cf486979b281 h1:aczX6NMOtt6L4YT0fQvKkDK6LZEtdOso9sUH89V1+P0=
|
||||
github.com/lufia/plan9stats v0.0.0-20220517141722-cf486979b281/go.mod h1:lc+czkgO/8F7puNki5jk8QyujbfK1LOT7Wl0ON2hxyk=
|
||||
|
|
|
@ -282,20 +282,19 @@ func testBIP0009(t *testing.T, forkKey string, deploymentID uint32) {
|
|||
// - Assert the chain height is 0 and the state is ThresholdDefined
|
||||
// - Generate 1 fewer blocks than needed to reach the first state transition
|
||||
// - Assert chain height is expected and state is still ThresholdDefined
|
||||
//
|
||||
// - Generate 1 more block to reach the first state transition
|
||||
// - Assert chain height is expected and state moved to ThresholdStarted
|
||||
// - Generate enough blocks to reach the next state transition window, but only
|
||||
// signal support in 1 fewer than the required number to achieve
|
||||
// ThresholdLockedIn
|
||||
// - Generate enough blocks to reach the next state transition window, but only
|
||||
// signal support in 1 fewer than the required number to achieve
|
||||
// ThresholdLockedIn
|
||||
// - Assert chain height is expected and state is still ThresholdStarted
|
||||
// - Generate enough blocks to reach the next state transition window with only
|
||||
// the exact number of blocks required to achieve locked in status signalling
|
||||
// support.
|
||||
// - Generate enough blocks to reach the next state transition window with only
|
||||
// the exact number of blocks required to achieve locked in status signalling
|
||||
// support.
|
||||
// - Assert chain height is expected and state moved to ThresholdLockedIn
|
||||
// - Generate 1 fewer blocks than needed to reach the next state transition
|
||||
// - Generate 1 fewer blocks than needed to reach the next state transition
|
||||
// - Assert chain height is expected and state is still ThresholdLockedIn
|
||||
// - Generate 1 more block to reach the next state transition
|
||||
// - Generate 1 more block to reach the next state transition
|
||||
// - Assert chain height is expected and state moved to ThresholdActive
|
||||
func TestBIP0009(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
@ -310,14 +309,11 @@ func TestBIP0009(t *testing.T) {
|
|||
// Overview:
|
||||
// - Generate block 1
|
||||
// - Assert bit is NOT set (ThresholdDefined)
|
||||
//
|
||||
// - Generate enough blocks to reach first state transition
|
||||
// - Assert bit is NOT set for block prior to state transition
|
||||
// - Assert bit is set for block at state transition (ThresholdStarted)
|
||||
//
|
||||
// - Generate enough blocks to reach second state transition
|
||||
// - Assert bit is set for block at state transition (ThresholdLockedIn)
|
||||
//
|
||||
// - Generate enough blocks to reach third state transition
|
||||
// - Assert bit is set for block prior to state transition (ThresholdLockedIn)
|
||||
// - Assert bit is NOT set for block at state transition (ThresholdActive)
|
||||
|
|
|
@ -95,22 +95,17 @@ func makeTestOutput(r *rpctest.Harness, t *testing.T,
|
|||
// them.
|
||||
//
|
||||
// Overview:
|
||||
// - Pre soft-fork:
|
||||
// - Transactions with non-final lock-times from the PoV of MTP should be
|
||||
// rejected from the mempool.
|
||||
// - Transactions within non-final MTP based lock-times should be accepted
|
||||
// in valid blocks.
|
||||
//
|
||||
// - Pre soft-fork:
|
||||
//
|
||||
// - Transactions with non-final lock-times from the PoV of MTP should be
|
||||
// rejected from the mempool.
|
||||
//
|
||||
// - Transactions within non-final MTP based lock-times should be accepted
|
||||
// in valid blocks.
|
||||
//
|
||||
// - Post soft-fork:
|
||||
//
|
||||
// - Transactions with non-final lock-times from the PoV of MTP should be
|
||||
// rejected from the mempool and when found within otherwise valid blocks.
|
||||
//
|
||||
// - Transactions with final lock-times from the PoV of MTP should be
|
||||
// accepted to the mempool and mined in future block.
|
||||
// - Post soft-fork:
|
||||
// - Transactions with non-final lock-times from the PoV of MTP should be
|
||||
// rejected from the mempool and when found within otherwise valid blocks.
|
||||
// - Transactions with final lock-times from the PoV of MTP should be
|
||||
// accepted to the mempool and mined in future block.
|
||||
func TestBIP0113Activation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
@ -396,13 +391,13 @@ func assertTxInBlock(r *rpctest.Harness, t *testing.T, blockHash *chainhash.Hash
|
|||
// 112 and BIP 68 rule-set after the activation of the CSV-package soft-fork.
|
||||
//
|
||||
// Overview:
|
||||
// - Pre soft-fork:
|
||||
// - A transaction spending a CSV output validly should be rejected from the
|
||||
// mempool, but accepted in a valid generated block including the
|
||||
// transaction.
|
||||
// - Post soft-fork:
|
||||
// - See the cases exercised within the table driven tests towards the end
|
||||
// of this test.
|
||||
// - Pre soft-fork:
|
||||
// - A transaction spending a CSV output validly should be rejected from the
|
||||
// mempool, but accepted in a valid generated block including the
|
||||
// transaction.
|
||||
// - Post soft-fork:
|
||||
// - See the cases exercised within the table driven tests towards the end
|
||||
// of this test.
|
||||
func TestBIP0068AndBIP0112Activation(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
|
|
|
@ -13,17 +13,12 @@ import (
|
|||
"fmt"
|
||||
"os"
|
||||
"runtime/debug"
|
||||
"sort"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lbryio/lbcd/chaincfg"
|
||||
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
||||
"github.com/lbryio/lbcd/integration/rpctest"
|
||||
"github.com/lbryio/lbcd/rpcclient"
|
||||
"github.com/lbryio/lbcd/txscript"
|
||||
"github.com/lbryio/lbcd/wire"
|
||||
"github.com/lbryio/lbcutil"
|
||||
)
|
||||
|
||||
func testGetBestBlock(r *rpctest.Harness, t *testing.T) {
|
||||
|
@ -138,278 +133,13 @@ func testBulkClient(r *rpctest.Harness, t *testing.T) {
|
|||
t.Fatalf("expected hash %s to be in generated hash list", blockHash)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testGetBlockStats(r *rpctest.Harness, t *testing.T) {
|
||||
t.Parallel()
|
||||
|
||||
baseFeeRate := int64(10)
|
||||
txValue := int64(50000000)
|
||||
txQuantity := 10
|
||||
txs := make([]*lbcutil.Tx, txQuantity)
|
||||
fees := make([]int64, txQuantity)
|
||||
sizes := make([]int64, txQuantity)
|
||||
feeRates := make([]int64, txQuantity)
|
||||
var outputCount int
|
||||
|
||||
// Generate test sample.
|
||||
for i := 0; i < txQuantity; i++ {
|
||||
address, err := r.NewAddress()
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to generate address: %v", err)
|
||||
}
|
||||
|
||||
pkScript, err := txscript.PayToAddrScript(address)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to generate PKScript: %v", err)
|
||||
}
|
||||
|
||||
// This feerate is not the actual feerate. See comment below.
|
||||
feeRate := baseFeeRate * int64(i)
|
||||
|
||||
tx, err := r.CreateTransaction([]*wire.TxOut{wire.NewTxOut(txValue, pkScript)}, lbcutil.Amount(feeRate), true)
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to generate segwit transaction: %v", err)
|
||||
}
|
||||
|
||||
txs[i] = lbcutil.NewTx(tx)
|
||||
sizes[i] = int64(tx.SerializeSize())
|
||||
|
||||
// memWallet.fundTx makes some assumptions when calculating fees.
|
||||
// For instance, it assumes the signature script has exactly 108 bytes
|
||||
// and it does not account for the size of the change output.
|
||||
// This needs to be taken into account when getting the true feerate.
|
||||
scriptSigOffset := 108 - len(tx.TxIn[0].SignatureScript)
|
||||
changeOutputSize := tx.TxOut[len(tx.TxOut)-1].SerializeSize()
|
||||
fees[i] = (sizes[i] + int64(scriptSigOffset) - int64(changeOutputSize)) * feeRate
|
||||
feeRates[i] = fees[i] / sizes[i]
|
||||
|
||||
outputCount += len(tx.TxOut)
|
||||
}
|
||||
|
||||
stats := func(slice []int64) (int64, int64, int64, int64, int64) {
|
||||
var total, average, min, max, median int64
|
||||
min = slice[0]
|
||||
length := len(slice)
|
||||
for _, item := range slice {
|
||||
if min > item {
|
||||
min = item
|
||||
}
|
||||
if max < item {
|
||||
max = item
|
||||
}
|
||||
total += item
|
||||
}
|
||||
average = total / int64(length)
|
||||
sort.Slice(slice, func(i, j int) bool { return slice[i] < slice[j] })
|
||||
if length == 0 {
|
||||
median = 0
|
||||
} else if length%2 == 0 {
|
||||
median = (slice[length/2-1] + slice[length/2]) / 2
|
||||
} else {
|
||||
median = slice[length/2]
|
||||
}
|
||||
return total, average, min, max, median
|
||||
}
|
||||
|
||||
totalFee, avgFee, minFee, maxFee, medianFee := stats(fees)
|
||||
totalSize, avgSize, minSize, maxSize, medianSize := stats(sizes)
|
||||
_, avgFeeRate, minFeeRate, maxFeeRate, _ := stats(feeRates)
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
txs []*lbcutil.Tx
|
||||
stats []string
|
||||
expectedResults map[string]interface{}
|
||||
}{
|
||||
{
|
||||
name: "empty block",
|
||||
txs: []*lbcutil.Tx{},
|
||||
stats: []string{},
|
||||
expectedResults: map[string]interface{}{
|
||||
"avgfee": int64(0),
|
||||
"avgfeerate": int64(0),
|
||||
"avgtxsize": int64(0),
|
||||
"feerate_percentiles": []int64{0, 0, 0, 0, 0},
|
||||
"ins": int64(0),
|
||||
"maxfee": int64(0),
|
||||
"maxfeerate": int64(0),
|
||||
"maxtxsize": int64(0),
|
||||
"medianfee": int64(0),
|
||||
"mediantxsize": int64(0),
|
||||
"minfee": int64(0),
|
||||
"mintxsize": int64(0),
|
||||
"outs": int64(1),
|
||||
"swtotal_size": int64(0),
|
||||
"swtotal_weight": int64(0),
|
||||
"swtxs": int64(0),
|
||||
"total_out": int64(0),
|
||||
"total_size": int64(0),
|
||||
"total_weight": int64(0),
|
||||
"txs": int64(1),
|
||||
"utxo_increase": int64(1),
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "block with 10 transactions + coinbase",
|
||||
txs: txs,
|
||||
stats: []string{"avgfee", "avgfeerate", "avgtxsize", "feerate_percentiles",
|
||||
"ins", "maxfee", "maxfeerate", "maxtxsize", "medianfee", "mediantxsize",
|
||||
"minfee", "minfeerate", "mintxsize", "outs", "subsidy", "swtxs",
|
||||
"total_size", "total_weight", "totalfee", "txs", "utxo_increase"},
|
||||
expectedResults: map[string]interface{}{
|
||||
"avgfee": avgFee,
|
||||
"avgfeerate": avgFeeRate,
|
||||
"avgtxsize": avgSize,
|
||||
"feerate_percentiles": []int64{feeRates[0], feeRates[2],
|
||||
feeRates[4], feeRates[7], feeRates[8]},
|
||||
"ins": int64(txQuantity),
|
||||
"maxfee": maxFee,
|
||||
"maxfeerate": maxFeeRate,
|
||||
"maxtxsize": maxSize,
|
||||
"medianfee": medianFee,
|
||||
"mediantxsize": medianSize,
|
||||
"minfee": minFee,
|
||||
"minfeerate": minFeeRate,
|
||||
"mintxsize": minSize,
|
||||
"outs": int64(outputCount + 1), // Coinbase output also counts.
|
||||
"subsidy": int64(100000000),
|
||||
"swtotal_weight": nil, // This stat was not selected, so it should be nil.
|
||||
"swtxs": int64(0),
|
||||
"total_size": totalSize,
|
||||
"total_weight": totalSize * 4,
|
||||
"totalfee": totalFee,
|
||||
"txs": int64(txQuantity + 1), // Coinbase transaction also counts.
|
||||
"utxo_increase": int64(outputCount + 1 - txQuantity),
|
||||
"utxo_size_inc": nil,
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
// Submit a new block with the provided transactions.
|
||||
block, err := r.GenerateAndSubmitBlock(test.txs, -1, time.Time{})
|
||||
if err != nil {
|
||||
t.Fatalf("Unable to generate block: %v from test %s", err, test.name)
|
||||
}
|
||||
|
||||
blockStats, err := r.GetBlockStats(block.Hash(), &test.stats)
|
||||
if err != nil {
|
||||
t.Fatalf("Call to `getblockstats` on test %s failed: %v", test.name, err)
|
||||
}
|
||||
|
||||
if blockStats.Height != (*int64)(nil) && *blockStats.Height != int64(block.Height()) {
|
||||
t.Fatalf("Unexpected result in test %s, stat: %v, expected: %v, got: %v", test.name, "height", block.Height(), *blockStats.Height)
|
||||
}
|
||||
|
||||
for stat, value := range test.expectedResults {
|
||||
var result interface{}
|
||||
switch stat {
|
||||
case "avgfee":
|
||||
result = blockStats.AverageFee
|
||||
case "avgfeerate":
|
||||
result = blockStats.AverageFeeRate
|
||||
case "avgtxsize":
|
||||
result = blockStats.AverageTxSize
|
||||
case "feerate_percentiles":
|
||||
result = blockStats.FeeratePercentiles
|
||||
case "blockhash":
|
||||
result = blockStats.Hash
|
||||
case "height":
|
||||
result = blockStats.Height
|
||||
case "ins":
|
||||
result = blockStats.Ins
|
||||
case "maxfee":
|
||||
result = blockStats.MaxFee
|
||||
case "maxfeerate":
|
||||
result = blockStats.MaxFeeRate
|
||||
case "maxtxsize":
|
||||
result = blockStats.MaxTxSize
|
||||
case "medianfee":
|
||||
result = blockStats.MedianFee
|
||||
case "mediantime":
|
||||
result = blockStats.MedianTime
|
||||
case "mediantxsize":
|
||||
result = blockStats.MedianTxSize
|
||||
case "minfee":
|
||||
result = blockStats.MinFee
|
||||
case "minfeerate":
|
||||
result = blockStats.MinFeeRate
|
||||
case "mintxsize":
|
||||
result = blockStats.MinTxSize
|
||||
case "outs":
|
||||
result = blockStats.Outs
|
||||
case "swtotal_size":
|
||||
result = blockStats.SegWitTotalSize
|
||||
case "swtotal_weight":
|
||||
result = blockStats.SegWitTotalWeight
|
||||
case "swtxs":
|
||||
result = blockStats.SegWitTxs
|
||||
case "subsidy":
|
||||
result = blockStats.Subsidy
|
||||
case "time":
|
||||
result = blockStats.Time
|
||||
case "total_out":
|
||||
result = blockStats.TotalOut
|
||||
case "total_size":
|
||||
result = blockStats.TotalSize
|
||||
case "total_weight":
|
||||
result = blockStats.TotalWeight
|
||||
case "totalfee":
|
||||
result = blockStats.TotalFee
|
||||
case "txs":
|
||||
result = blockStats.Txs
|
||||
case "utxo_increase":
|
||||
result = blockStats.UTXOIncrease
|
||||
case "utxo_size_inc":
|
||||
result = blockStats.UTXOSizeIncrease
|
||||
}
|
||||
|
||||
var equality bool
|
||||
|
||||
// Check for nil equality.
|
||||
if value == nil && result == (*int64)(nil) {
|
||||
equality = true
|
||||
break
|
||||
} else if result == nil || value == nil {
|
||||
equality = false
|
||||
}
|
||||
|
||||
var resultValue interface{}
|
||||
switch v := value.(type) {
|
||||
case int64:
|
||||
resultValue = *result.(*int64)
|
||||
equality = v == resultValue
|
||||
case string:
|
||||
resultValue = *result.(*string)
|
||||
equality = v == resultValue
|
||||
case []int64:
|
||||
resultValue = *result.(*[]int64)
|
||||
resultSlice := resultValue.([]int64)
|
||||
equality = true
|
||||
for i, item := range resultSlice {
|
||||
if item != v[i] {
|
||||
equality = false
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if !equality {
|
||||
if result != nil {
|
||||
t.Fatalf("Unexpected result in test %s, stat: %v, expected: %v, got: %v", test.name, stat, value, resultValue)
|
||||
} else {
|
||||
t.Fatalf("Unexpected result in test %s, stat: %v, expected: %v, got: %v", test.name, stat, value, "<nil>")
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var rpcTestCases = []rpctest.HarnessTestCase{
|
||||
testGetBestBlock,
|
||||
testGetBlockCount,
|
||||
testGetBlockHash,
|
||||
testGetBlockStats,
|
||||
testBulkClient,
|
||||
}
|
||||
|
||||
|
@ -421,8 +151,7 @@ func TestMain(m *testing.M) {
|
|||
// In order to properly test scenarios on as if we were on mainnet,
|
||||
// ensure that non-standard transactions aren't accepted into the
|
||||
// mempool or relayed.
|
||||
// Enable transaction index to be able to fully test GetBlockStats
|
||||
btcdCfg := []string{"--rejectnonstd", "--txindex"}
|
||||
btcdCfg := []string{"--rejectnonstd"}
|
||||
primaryHarness, err = rpctest.New(
|
||||
&chaincfg.SimNetParams, nil, btcdCfg, "",
|
||||
)
|
||||
|
|
|
@ -16,7 +16,6 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lbryio/lbcd/btcjson"
|
||||
"github.com/lbryio/lbcd/chaincfg"
|
||||
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
||||
"github.com/lbryio/lbcd/rpcclient"
|
||||
|
@ -513,18 +512,6 @@ func (h *Harness) GenerateAndSubmitBlockWithCustomCoinbaseOutputs(
|
|||
return newBlock, nil
|
||||
}
|
||||
|
||||
// GetBlockStats returns block statistics. First argument specifies height or
|
||||
// hash of the target block. Second argument allows to select certain stats to
|
||||
// return. If second argument is empty, all stats are returned.
|
||||
func (h *Harness) GetBlockStats(hashOrHeight interface{}, stats *[]string) (
|
||||
*btcjson.GetBlockStatsResult, error) {
|
||||
|
||||
h.Lock()
|
||||
defer h.Unlock()
|
||||
|
||||
return h.Client.GetBlockStats(hashOrHeight, stats)
|
||||
}
|
||||
|
||||
// generateListeningAddresses returns two strings representing listening
|
||||
// addresses designated for the current rpc test. If there haven't been any
|
||||
// test instances created, the default ports are used. Otherwise, in order to
|
||||
|
|
23
lbcd.go
23
lbcd.go
|
@ -92,25 +92,6 @@ func btcdMain(serverChan chan<- *server) error {
|
|||
defer pprof.StopCPUProfile()
|
||||
}
|
||||
|
||||
// Write memory profile if requested.
|
||||
if cfg.MemProfile != "" {
|
||||
f, err := os.Create(cfg.MemProfile + ".heap")
|
||||
if err != nil {
|
||||
btcdLog.Errorf("Unable to create mem profile: %v", err)
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
defer pprof.Lookup("heap").WriteTo(f, 0)
|
||||
|
||||
f, err = os.Create(cfg.MemProfile + ".allocs")
|
||||
if err != nil {
|
||||
btcdLog.Errorf("Unable to create mem profile: %v", err)
|
||||
return err
|
||||
}
|
||||
defer f.Close()
|
||||
defer pprof.Lookup("allocs").WriteTo(f, 0)
|
||||
}
|
||||
|
||||
// Perform upgrades to btcd as new versions require it.
|
||||
if err := doUpgrades(); err != nil {
|
||||
btcdLog.Errorf("%v", err)
|
||||
|
@ -300,9 +281,7 @@ func main() {
|
|||
// limits the garbage collector from excessively overallocating during
|
||||
// bursts. This value was arrived at with the help of profiling live
|
||||
// usage.
|
||||
if _, ok := os.LookupEnv("GOGC"); !ok {
|
||||
debug.SetGCPercent(10)
|
||||
}
|
||||
debug.SetGCPercent(10)
|
||||
|
||||
// Up some limits.
|
||||
if err := limits.SetLimits(); err != nil {
|
||||
|
|
|
@ -31,40 +31,40 @@ proceed. Typically, this will involve things such as relaying the transactions
|
|||
to other peers on the network and notifying the mining process that new
|
||||
transactions are available.
|
||||
|
||||
# Feature Overview
|
||||
Feature Overview
|
||||
|
||||
The following is a quick overview of the major features. It is not intended to
|
||||
be an exhaustive list.
|
||||
|
||||
- Maintain a pool of fully validated transactions
|
||||
- Reject non-fully-spent duplicate transactions
|
||||
- Reject coinbase transactions
|
||||
- Reject double spends (both from the chain and other transactions in pool)
|
||||
- Reject invalid transactions according to the network consensus rules
|
||||
- Full script execution and validation with signature cache support
|
||||
- Individual transaction query support
|
||||
- Orphan transaction support (transactions that spend from unknown outputs)
|
||||
- Configurable limits (see transaction acceptance policy)
|
||||
- Automatic addition of orphan transactions that are no longer orphans as new
|
||||
transactions are added to the pool
|
||||
- Individual orphan transaction query support
|
||||
- Configurable transaction acceptance policy
|
||||
- Option to accept or reject standard transactions
|
||||
- Option to accept or reject transactions based on priority calculations
|
||||
- Rate limiting of low-fee and free transactions
|
||||
- Non-zero fee threshold
|
||||
- Max signature operations per transaction
|
||||
- Max orphan transaction size
|
||||
- Max number of orphan transactions allowed
|
||||
- Additional metadata tracking for each transaction
|
||||
- Timestamp when the transaction was added to the pool
|
||||
- Most recent block height when the transaction was added to the pool
|
||||
- The fee the transaction pays
|
||||
- The starting priority for the transaction
|
||||
- Manual control of transaction removal
|
||||
- Recursive removal of all dependent transactions
|
||||
- Maintain a pool of fully validated transactions
|
||||
- Reject non-fully-spent duplicate transactions
|
||||
- Reject coinbase transactions
|
||||
- Reject double spends (both from the chain and other transactions in pool)
|
||||
- Reject invalid transactions according to the network consensus rules
|
||||
- Full script execution and validation with signature cache support
|
||||
- Individual transaction query support
|
||||
- Orphan transaction support (transactions that spend from unknown outputs)
|
||||
- Configurable limits (see transaction acceptance policy)
|
||||
- Automatic addition of orphan transactions that are no longer orphans as new
|
||||
transactions are added to the pool
|
||||
- Individual orphan transaction query support
|
||||
- Configurable transaction acceptance policy
|
||||
- Option to accept or reject standard transactions
|
||||
- Option to accept or reject transactions based on priority calculations
|
||||
- Rate limiting of low-fee and free transactions
|
||||
- Non-zero fee threshold
|
||||
- Max signature operations per transaction
|
||||
- Max orphan transaction size
|
||||
- Max number of orphan transactions allowed
|
||||
- Additional metadata tracking for each transaction
|
||||
- Timestamp when the transaction was added to the pool
|
||||
- Most recent block height when the transaction was added to the pool
|
||||
- The fee the transaction pays
|
||||
- The starting priority for the transaction
|
||||
- Manual control of transaction removal
|
||||
- Recursive removal of all dependent transactions
|
||||
|
||||
# Errors
|
||||
Errors
|
||||
|
||||
Errors returned by this package are either the raw errors provided by underlying
|
||||
calls or of type mempool.RuleError. Since there are two classes of rules
|
||||
|
|
|
@ -8,7 +8,6 @@ import (
|
|||
"container/list"
|
||||
"fmt"
|
||||
"math"
|
||||
"reflect"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
@ -157,15 +156,6 @@ type Policy struct {
|
|||
RejectReplacement bool
|
||||
}
|
||||
|
||||
// aggregateInfo tracks aggregated serialized size, memory usage, and fees
|
||||
// for TxDesc in the mempool.
|
||||
type aggregateInfo struct {
|
||||
totalCount int64
|
||||
totalBytes int64
|
||||
totalMem int64
|
||||
totalFee int64
|
||||
}
|
||||
|
||||
// TxDesc is a descriptor containing a transaction in the mempool along with
|
||||
// additional metadata.
|
||||
type TxDesc struct {
|
||||
|
@ -176,20 +166,6 @@ type TxDesc struct {
|
|||
StartingPriority float64
|
||||
}
|
||||
|
||||
func (txD *TxDesc) incr(info *aggregateInfo) {
|
||||
info.totalCount += 1
|
||||
info.totalBytes += int64(txD.Tx.MsgTx().SerializeSize())
|
||||
info.totalMem += int64(dynamicMemUsage(reflect.ValueOf(txD)))
|
||||
info.totalFee += txD.Fee
|
||||
}
|
||||
|
||||
func (txD *TxDesc) decr(info *aggregateInfo) {
|
||||
info.totalCount -= 1
|
||||
info.totalBytes -= int64(txD.Tx.MsgTx().SerializeSize())
|
||||
info.totalMem -= int64(dynamicMemUsage(reflect.ValueOf(txD)))
|
||||
info.totalFee -= txD.Fee
|
||||
}
|
||||
|
||||
// orphanTx is normal transaction that references an ancestor transaction
|
||||
// that is not yet available. It also contains additional information related
|
||||
// to it such as an expiration time to help prevent caching the orphan forever.
|
||||
|
@ -199,18 +175,6 @@ type orphanTx struct {
|
|||
expiration time.Time
|
||||
}
|
||||
|
||||
func (otx *orphanTx) incr(info *aggregateInfo) {
|
||||
info.totalCount += 1
|
||||
info.totalBytes += int64(otx.tx.MsgTx().SerializeSize())
|
||||
info.totalMem += int64(dynamicMemUsage(reflect.ValueOf(otx)))
|
||||
}
|
||||
|
||||
func (otx *orphanTx) decr(info *aggregateInfo) {
|
||||
info.totalCount -= 1
|
||||
info.totalBytes -= int64(otx.tx.MsgTx().SerializeSize())
|
||||
info.totalMem -= int64(dynamicMemUsage(reflect.ValueOf(otx)))
|
||||
}
|
||||
|
||||
// TxPool is used as a source of transactions that need to be mined into blocks
|
||||
// and relayed to other peers. It is safe for concurrent access from multiple
|
||||
// peers.
|
||||
|
@ -232,12 +196,6 @@ type TxPool struct {
|
|||
// the scan will only run when an orphan is added to the pool as opposed
|
||||
// to on an unconditional timer.
|
||||
nextExpireScan time.Time
|
||||
|
||||
// stats are aggregated over pool, orphans, etc.
|
||||
stats aggregateInfo
|
||||
|
||||
// unbroadcast is a set of transactions yet to be broadcast.
|
||||
unbroadcast map[chainhash.Hash]bool
|
||||
}
|
||||
|
||||
// Ensure the TxPool type implements the mining.TxSource interface.
|
||||
|
@ -282,9 +240,6 @@ func (mp *TxPool) removeOrphan(tx *btcutil.Tx, removeRedeemers bool) {
|
|||
|
||||
// Remove the transaction from the orphan pool.
|
||||
delete(mp.orphans, *txHash)
|
||||
|
||||
// Update stats.
|
||||
otx.decr(&mp.stats)
|
||||
}
|
||||
|
||||
// RemoveOrphan removes the passed orphan transaction from the orphan pool and
|
||||
|
@ -381,12 +336,11 @@ func (mp *TxPool) addOrphan(tx *btcutil.Tx, tag Tag) {
|
|||
// orphan if space is still needed.
|
||||
mp.limitNumOrphans()
|
||||
|
||||
otx := &orphanTx{
|
||||
mp.orphans[*tx.Hash()] = &orphanTx{
|
||||
tx: tx,
|
||||
tag: tag,
|
||||
expiration: time.Now().Add(orphanTTL),
|
||||
}
|
||||
mp.orphans[*tx.Hash()] = otx
|
||||
for _, txIn := range tx.MsgTx().TxIn {
|
||||
if _, exists := mp.orphansByPrev[txIn.PreviousOutPoint]; !exists {
|
||||
mp.orphansByPrev[txIn.PreviousOutPoint] =
|
||||
|
@ -395,9 +349,6 @@ func (mp *TxPool) addOrphan(tx *btcutil.Tx, tag Tag) {
|
|||
mp.orphansByPrev[txIn.PreviousOutPoint][*tx.Hash()] = tx
|
||||
}
|
||||
|
||||
// Update stats.
|
||||
otx.incr(&mp.stats)
|
||||
|
||||
log.Debugf("Stored orphan transaction %v (total: %d)", tx.Hash(),
|
||||
len(mp.orphans))
|
||||
}
|
||||
|
@ -547,9 +498,6 @@ func (mp *TxPool) removeTransaction(tx *btcutil.Tx, removeRedeemers bool) {
|
|||
}
|
||||
delete(mp.pool, *txHash)
|
||||
|
||||
// Update stats.
|
||||
txDesc.decr(&mp.stats)
|
||||
|
||||
// Inform associated fee estimator that the transaction has been removed
|
||||
// from the mempool
|
||||
if mp.cfg.RemoveTxFromFeeEstimation != nil {
|
||||
|
@ -631,9 +579,6 @@ func (mp *TxPool) addTransaction(utxoView *blockchain.UtxoViewpoint, tx *btcutil
|
|||
mp.cfg.AddTxToFeeEstimation(txD.Tx.Hash(), txD.Fee, size)
|
||||
}
|
||||
|
||||
// Update stats.
|
||||
txD.incr(&mp.stats)
|
||||
|
||||
return txD
|
||||
}
|
||||
|
||||
|
@ -1558,38 +1503,6 @@ func (mp *TxPool) MiningDescs() []*mining.TxDesc {
|
|||
return descs
|
||||
}
|
||||
|
||||
func (mp *TxPool) AddUnbroadcastTx(hash *chainhash.Hash) {
|
||||
mp.mtx.Lock()
|
||||
mp.unbroadcast[*hash] = true
|
||||
mp.mtx.Unlock()
|
||||
}
|
||||
|
||||
func (mp *TxPool) RemoveUnbroadcastTx(hash *chainhash.Hash) {
|
||||
mp.mtx.Lock()
|
||||
delete(mp.unbroadcast, *hash)
|
||||
mp.mtx.Unlock()
|
||||
}
|
||||
|
||||
func (mp *TxPool) MempoolInfo() *btcjson.GetMempoolInfoResult {
|
||||
mp.mtx.RLock()
|
||||
policy := mp.cfg.Policy
|
||||
stats := mp.stats
|
||||
unbroadcastCount := int64(len(mp.unbroadcast))
|
||||
mp.mtx.RUnlock()
|
||||
|
||||
ret := &btcjson.GetMempoolInfoResult{
|
||||
Size: stats.totalCount,
|
||||
Usage: stats.totalMem,
|
||||
Bytes: stats.totalBytes,
|
||||
TotalFee: btcutil.Amount(stats.totalFee).ToBTC(),
|
||||
MemPoolMinFee: btcutil.Amount(calcMinRequiredTxRelayFee(1000, policy.MinRelayTxFee)).ToBTC(),
|
||||
MinRelayTxFee: policy.MinRelayTxFee.ToBTC(),
|
||||
UnbroadcastCount: unbroadcastCount,
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
// RawMempoolVerbose returns all the entries in the mempool as a fully
|
||||
// populated btcjson result.
|
||||
//
|
||||
|
@ -1663,6 +1576,5 @@ func New(cfg *Config) *TxPool {
|
|||
orphansByPrev: make(map[wire.OutPoint]map[chainhash.Hash]*btcutil.Tx),
|
||||
nextExpireScan: time.Now().Add(orphanExpireScanInterval),
|
||||
outpoints: make(map[wire.OutPoint]*btcutil.Tx),
|
||||
unbroadcast: make(map[chainhash.Hash]bool),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -21,12 +21,6 @@ import (
|
|||
btcutil "github.com/lbryio/lbcutil"
|
||||
)
|
||||
|
||||
func init() {
|
||||
// Toggle assert & debug messages when running tests.
|
||||
dynamicMemUsageAssert = true
|
||||
dynamicMemUsageDebug = false
|
||||
}
|
||||
|
||||
// fakeChain is used by the pool harness to provide generated test utxos and
|
||||
// a current faked chain height to the pool callbacks. This, in turn, allows
|
||||
// transactions to appear as though they are spending completely valid utxos.
|
||||
|
|
|
@ -1,89 +0,0 @@
|
|||
// Copyright (c) 2013-2016 The btcsuite developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package mempool
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"reflect"
|
||||
)
|
||||
|
||||
var (
|
||||
dynamicMemUsageAssert = false
|
||||
dynamicMemUsageDebug = false
|
||||
dynamicMemUsageMaxDepth = 10
|
||||
)
|
||||
|
||||
func dynamicMemUsage(v reflect.Value) uintptr {
|
||||
return dynamicMemUsageCrawl(v, 0)
|
||||
}
|
||||
|
||||
func dynamicMemUsageCrawl(v reflect.Value, depth int) uintptr {
|
||||
t := v.Type()
|
||||
bytes := t.Size()
|
||||
if dynamicMemUsageDebug {
|
||||
println("[", depth, "]", t.Kind().String(), "(", t.String(), ") ->", t.Size())
|
||||
}
|
||||
|
||||
if depth >= dynamicMemUsageMaxDepth {
|
||||
if dynamicMemUsageAssert {
|
||||
panic("crawl reached maximum depth")
|
||||
}
|
||||
return bytes
|
||||
}
|
||||
|
||||
// For complex types, we need to peek inside slices/arrays/structs and chase pointers.
|
||||
switch t.Kind() {
|
||||
case reflect.Pointer, reflect.Interface:
|
||||
if !v.IsNil() {
|
||||
bytes += dynamicMemUsageCrawl(v.Elem(), depth+1)
|
||||
}
|
||||
case reflect.Array, reflect.Slice:
|
||||
for j := 0; j < v.Len(); j++ {
|
||||
vi := v.Index(j)
|
||||
k := vi.Type().Kind()
|
||||
if dynamicMemUsageDebug {
|
||||
println("[", depth, "] index:", j, "kind:", k.String())
|
||||
}
|
||||
elemBytes := uintptr(0)
|
||||
if t.Kind() == reflect.Array {
|
||||
if (k == reflect.Pointer || k == reflect.Interface) && !vi.IsNil() {
|
||||
elemBytes += dynamicMemUsageCrawl(vi.Elem(), depth+1)
|
||||
}
|
||||
} else { // slice
|
||||
elemBytes += dynamicMemUsageCrawl(vi, depth+1)
|
||||
}
|
||||
if k == reflect.Uint8 {
|
||||
// short circuit for byte slice/array
|
||||
bytes += elemBytes * uintptr(v.Len())
|
||||
if dynamicMemUsageDebug {
|
||||
println("...", v.Len(), "elements")
|
||||
}
|
||||
break
|
||||
}
|
||||
bytes += elemBytes
|
||||
}
|
||||
case reflect.Struct:
|
||||
for _, f := range reflect.VisibleFields(t) {
|
||||
vf := v.FieldByIndex(f.Index)
|
||||
k := vf.Type().Kind()
|
||||
if dynamicMemUsageDebug {
|
||||
println("[", depth, "] field:", f.Name, "kind:", k.String())
|
||||
}
|
||||
if (k == reflect.Pointer || k == reflect.Interface) && !vf.IsNil() {
|
||||
bytes += dynamicMemUsageCrawl(vf.Elem(), depth+1)
|
||||
} else if k == reflect.Array || k == reflect.Slice {
|
||||
bytes -= vf.Type().Size()
|
||||
bytes += dynamicMemUsageCrawl(vf, depth+1)
|
||||
}
|
||||
}
|
||||
case reflect.Uint8:
|
||||
default:
|
||||
if dynamicMemUsageAssert {
|
||||
panic(fmt.Sprintf("unsupported kind: %v", t.Kind()))
|
||||
}
|
||||
}
|
||||
|
||||
return bytes
|
||||
}
|
|
@ -249,7 +249,7 @@ func GetDustThreshold(txOut *wire.TxOut) int64 {
|
|||
totalSize += 107
|
||||
}
|
||||
|
||||
return int64(totalSize)
|
||||
return 3 * int64(totalSize)
|
||||
}
|
||||
|
||||
// IsDust returns whether or not the passed transaction output amount is
|
||||
|
@ -264,7 +264,7 @@ func IsDust(txOut *wire.TxOut, minRelayTxFee btcutil.Amount) bool {
|
|||
}
|
||||
|
||||
// The output is considered dust if the cost to the network to spend the
|
||||
// coins is more than the minimum free transaction relay fee.
|
||||
// coins is more than 1/3 of the minimum free transaction relay fee.
|
||||
// minFreeTxRelayFee is in Satoshi/KB, so multiply by 1000 to
|
||||
// convert to bytes.
|
||||
//
|
||||
|
@ -273,7 +273,7 @@ func IsDust(txOut *wire.TxOut, minRelayTxFee btcutil.Amount) bool {
|
|||
// fee of 1000, this equates to values less than 546 satoshi being
|
||||
// considered dust.
|
||||
//
|
||||
// The following is equivalent to (value/totalSize) * 1000
|
||||
// The following is equivalent to (value/totalSize) * (1/3) * 1000
|
||||
// without needing to do floating point math.
|
||||
if txOut.Value > dustCap {
|
||||
return false
|
||||
|
|
|
@ -233,14 +233,14 @@ func TestDust(t *testing.T) {
|
|||
true,
|
||||
},
|
||||
{
|
||||
"38 byte public key script with value 194",
|
||||
wire.TxOut{Value: 194, PkScript: pkScript},
|
||||
"38 byte public key script with value 584",
|
||||
wire.TxOut{Value: 584, PkScript: pkScript},
|
||||
1000,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"38 byte public key script with value 195",
|
||||
wire.TxOut{Value: 195, PkScript: pkScript},
|
||||
"38 byte public key script with value 585",
|
||||
wire.TxOut{Value: 585, PkScript: pkScript},
|
||||
1000,
|
||||
false,
|
||||
},
|
||||
|
|
|
@ -420,26 +420,26 @@ func NewBlkTmplGenerator(policy *Policy, params *chaincfg.Params,
|
|||
//
|
||||
// Given the above, a block generated by this function is of the following form:
|
||||
//
|
||||
// ----------------------------------- -- --
|
||||
// | Coinbase Transaction | | |
|
||||
// |-----------------------------------| | |
|
||||
// | | | | ----- policy.BlockPrioritySize
|
||||
// | High-priority Transactions | | |
|
||||
// | | | |
|
||||
// |-----------------------------------| | --
|
||||
// | | |
|
||||
// | | |
|
||||
// | | |--- policy.BlockMaxSize
|
||||
// | Transactions prioritized by fee | |
|
||||
// | until <= policy.TxMinFreeFee | |
|
||||
// | | |
|
||||
// | | |
|
||||
// | | |
|
||||
// |-----------------------------------| |
|
||||
// | Low-fee/Non high-priority (free) | |
|
||||
// | transactions (while block size | |
|
||||
// | <= policy.BlockMinSize) | |
|
||||
// ----------------------------------- --
|
||||
// ----------------------------------- -- --
|
||||
// | Coinbase Transaction | | |
|
||||
// |-----------------------------------| | |
|
||||
// | | | | ----- policy.BlockPrioritySize
|
||||
// | High-priority Transactions | | |
|
||||
// | | | |
|
||||
// |-----------------------------------| | --
|
||||
// | | |
|
||||
// | | |
|
||||
// | | |--- policy.BlockMaxSize
|
||||
// | Transactions prioritized by fee | |
|
||||
// | until <= policy.TxMinFreeFee | |
|
||||
// | | |
|
||||
// | | |
|
||||
// | | |
|
||||
// |-----------------------------------| |
|
||||
// | Low-fee/Non high-priority (free) | |
|
||||
// | transactions (while block size | |
|
||||
// | <= policy.BlockMinSize) | |
|
||||
// ----------------------------------- --
|
||||
func (g *BlkTmplGenerator) NewBlockTemplate(payToAddress btcutil.Address) (*BlockTemplate, error) {
|
||||
// Extend the most recently known best block.
|
||||
best := g.chain.BestSnapshot()
|
||||
|
|
|
@ -27,9 +27,8 @@ type blockProgressLogger struct {
|
|||
|
||||
// newBlockProgressLogger returns a new block progress logger.
|
||||
// The progress message is templated as follows:
|
||||
//
|
||||
// {progressAction} {numProcessed} {blocks|block} in the last {timePeriod}
|
||||
// ({numTxs}, height {lastBlockHeight}, {lastBlockTimeStamp})
|
||||
// {progressAction} {numProcessed} {blocks|block} in the last {timePeriod}
|
||||
// ({numTxs}, height {lastBlockHeight}, {lastBlockTimeStamp})
|
||||
func newBlockProgressLogger(progressMessage string, logger btclog.Logger) *blockProgressLogger {
|
||||
return &blockProgressLogger{
|
||||
lastBlockLogTime: time.Now(),
|
||||
|
|
|
@ -903,7 +903,7 @@ func (sm *SyncManager) fetchHeaderBlocks() {
|
|||
numRequested++
|
||||
}
|
||||
sm.startHeader = e.Next()
|
||||
if numRequested >= wire.MaxInvPerMsg/99 {
|
||||
if numRequested >= wire.MaxInvPerMsg {
|
||||
break
|
||||
}
|
||||
}
|
||||
|
@ -1294,9 +1294,16 @@ func (sm *SyncManager) handleInvMsg(imsg *invMsg) {
|
|||
break
|
||||
}
|
||||
}
|
||||
|
||||
e := wire.BaseEncoding
|
||||
// we think that the iv.Type set above is sufficient. If not:
|
||||
// if peer.IsWitnessEnabled() {
|
||||
// e = wire.WitnessEncoding
|
||||
//}
|
||||
|
||||
state.requestQueue = requestQueue
|
||||
if len(gdmsg.InvList) > 0 {
|
||||
peer.QueueMessage(gdmsg, nil)
|
||||
peer.QueueMessageWithEncoding(gdmsg, nil, e)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
84
peer/doc.go
84
peer/doc.go
|
@ -6,7 +6,7 @@
|
|||
Package peer provides a common base for creating and managing Bitcoin network
|
||||
peers.
|
||||
|
||||
# Overview
|
||||
Overview
|
||||
|
||||
This package builds upon the wire package, which provides the fundamental
|
||||
primitives necessary to speak the bitcoin wire protocol, in order to simplify
|
||||
|
@ -16,41 +16,41 @@ Payment Verification (SPV) nodes, proxies, etc.
|
|||
|
||||
A quick overview of the major features peer provides are as follows:
|
||||
|
||||
- Provides a basic concurrent safe bitcoin peer for handling bitcoin
|
||||
communications via the peer-to-peer protocol
|
||||
- Full duplex reading and writing of bitcoin protocol messages
|
||||
- Automatic handling of the initial handshake process including protocol
|
||||
version negotiation
|
||||
- Asynchronous message queuing of outbound messages with optional channel for
|
||||
notification when the message is actually sent
|
||||
- Flexible peer configuration
|
||||
- Caller is responsible for creating outgoing connections and listening for
|
||||
incoming connections so they have flexibility to establish connections as
|
||||
they see fit (proxies, etc)
|
||||
- User agent name and version
|
||||
- Bitcoin network
|
||||
- Service support signalling (full nodes, bloom filters, etc)
|
||||
- Maximum supported protocol version
|
||||
- Ability to register callbacks for handling bitcoin protocol messages
|
||||
- Inventory message batching and send trickling with known inventory detection
|
||||
and avoidance
|
||||
- Automatic periodic keep-alive pinging and pong responses
|
||||
- Random nonce generation and self connection detection
|
||||
- Proper handling of bloom filter related commands when the caller does not
|
||||
specify the related flag to signal support
|
||||
- Disconnects the peer when the protocol version is high enough
|
||||
- Does not invoke the related callbacks for older protocol versions
|
||||
- Snapshottable peer statistics such as the total number of bytes read and
|
||||
written, the remote address, user agent, and negotiated protocol version
|
||||
- Helper functions pushing addresses, getblocks, getheaders, and reject
|
||||
messages
|
||||
- These could all be sent manually via the standard message output function,
|
||||
but the helpers provide additional nice functionality such as duplicate
|
||||
filtering and address randomization
|
||||
- Ability to wait for shutdown/disconnect
|
||||
- Comprehensive test coverage
|
||||
- Provides a basic concurrent safe bitcoin peer for handling bitcoin
|
||||
communications via the peer-to-peer protocol
|
||||
- Full duplex reading and writing of bitcoin protocol messages
|
||||
- Automatic handling of the initial handshake process including protocol
|
||||
version negotiation
|
||||
- Asynchronous message queuing of outbound messages with optional channel for
|
||||
notification when the message is actually sent
|
||||
- Flexible peer configuration
|
||||
- Caller is responsible for creating outgoing connections and listening for
|
||||
incoming connections so they have flexibility to establish connections as
|
||||
they see fit (proxies, etc)
|
||||
- User agent name and version
|
||||
- Bitcoin network
|
||||
- Service support signalling (full nodes, bloom filters, etc)
|
||||
- Maximum supported protocol version
|
||||
- Ability to register callbacks for handling bitcoin protocol messages
|
||||
- Inventory message batching and send trickling with known inventory detection
|
||||
and avoidance
|
||||
- Automatic periodic keep-alive pinging and pong responses
|
||||
- Random nonce generation and self connection detection
|
||||
- Proper handling of bloom filter related commands when the caller does not
|
||||
specify the related flag to signal support
|
||||
- Disconnects the peer when the protocol version is high enough
|
||||
- Does not invoke the related callbacks for older protocol versions
|
||||
- Snapshottable peer statistics such as the total number of bytes read and
|
||||
written, the remote address, user agent, and negotiated protocol version
|
||||
- Helper functions pushing addresses, getblocks, getheaders, and reject
|
||||
messages
|
||||
- These could all be sent manually via the standard message output function,
|
||||
but the helpers provide additional nice functionality such as duplicate
|
||||
filtering and address randomization
|
||||
- Ability to wait for shutdown/disconnect
|
||||
- Comprehensive test coverage
|
||||
|
||||
# Peer Configuration
|
||||
Peer Configuration
|
||||
|
||||
All peer configuration is handled with the Config struct. This allows the
|
||||
caller to specify things such as the user agent name and version, the bitcoin
|
||||
|
@ -58,7 +58,7 @@ network to use, which services it supports, and callbacks to invoke when bitcoin
|
|||
messages are received. See the documentation for each field of the Config
|
||||
struct for more details.
|
||||
|
||||
# Inbound and Outbound Peers
|
||||
Inbound and Outbound Peers
|
||||
|
||||
A peer can either be inbound or outbound. The caller is responsible for
|
||||
establishing the connection to remote peers and listening for incoming peers.
|
||||
|
@ -73,7 +73,7 @@ Disconnect to disconnect from the peer and clean up all resources.
|
|||
WaitForDisconnect can be used to block until peer disconnection and resource
|
||||
cleanup has completed.
|
||||
|
||||
# Callbacks
|
||||
Callbacks
|
||||
|
||||
In order to do anything useful with a peer, it is necessary to react to bitcoin
|
||||
messages. This is accomplished by creating an instance of the MessageListeners
|
||||
|
@ -92,7 +92,7 @@ It is often useful to use closures which encapsulate state when specifying the
|
|||
callback handlers. This provides a clean method for accessing that state when
|
||||
callbacks are invoked.
|
||||
|
||||
# Queuing Messages and Inventory
|
||||
Queuing Messages and Inventory
|
||||
|
||||
The QueueMessage function provides the fundamental means to send messages to the
|
||||
remote peer. As the name implies, this employs a non-blocking queue. A done
|
||||
|
@ -106,7 +106,7 @@ QueueInventory function. It employs batching and trickling along with
|
|||
intelligent known remote peer inventory detection and avoidance through the use
|
||||
of a most-recently used algorithm.
|
||||
|
||||
# Message Sending Helper Functions
|
||||
Message Sending Helper Functions
|
||||
|
||||
In addition to the bare QueueMessage function previously described, the
|
||||
PushAddrMsg, PushGetBlocksMsg, PushGetHeadersMsg, and PushRejectMsg functions
|
||||
|
@ -128,13 +128,13 @@ appropriate reject message based on the provided parameters as well as
|
|||
optionally provides a flag to cause it to block until the message is actually
|
||||
sent.
|
||||
|
||||
# Peer Statistics
|
||||
Peer Statistics
|
||||
|
||||
A snapshot of the current peer statistics can be obtained with the StatsSnapshot
|
||||
function. This includes statistics such as the total number of bytes read and
|
||||
written, the remote address, user agent, and negotiated protocol version.
|
||||
|
||||
# Logging
|
||||
Logging
|
||||
|
||||
This package provides extensive logging capabilities through the UseLogger
|
||||
function which allows a btclog.Logger to be specified. For example, logging at
|
||||
|
@ -142,7 +142,7 @@ the debug level provides summaries of every message sent and received, and
|
|||
logging at the trace level provides full dumps of parsed messages as well as the
|
||||
raw message bytes using a format similar to hexdump -C.
|
||||
|
||||
# Bitcoin Improvement Proposals
|
||||
Bitcoin Improvement Proposals
|
||||
|
||||
This package supports all BIPS supported by the wire package.
|
||||
(https://pkg.go.dev/github.com/lbryio/lbcd/wire#hdr-Bitcoin_Improvement_Proposals)
|
||||
|
|
16
peer/peer.go
16
peer/peer.go
|
@ -2096,10 +2096,10 @@ func (p *Peer) writeLocalVersionMsg() error {
|
|||
// peer. The events should occur in the following order, otherwise an error is
|
||||
// returned:
|
||||
//
|
||||
// 1. Remote peer sends their version.
|
||||
// 2. We send our version.
|
||||
// 3. We send our verack.
|
||||
// 4. Remote peer sends their verack.
|
||||
// 1. Remote peer sends their version.
|
||||
// 2. We send our version.
|
||||
// 3. We send our verack.
|
||||
// 4. Remote peer sends their verack.
|
||||
func (p *Peer) negotiateInboundProtocol() error {
|
||||
if err := p.readRemoteVersionMsg(); err != nil {
|
||||
return err
|
||||
|
@ -2121,10 +2121,10 @@ func (p *Peer) negotiateInboundProtocol() error {
|
|||
// peer. The events should occur in the following order, otherwise an error is
|
||||
// returned:
|
||||
//
|
||||
// 1. We send our version.
|
||||
// 2. Remote peer sends their version.
|
||||
// 3. Remote peer sends their verack.
|
||||
// 4. We send our verack.
|
||||
// 1. We send our version.
|
||||
// 2. Remote peer sends their version.
|
||||
// 3. Remote peer sends their verack.
|
||||
// 4. We send our verack.
|
||||
func (p *Peer) negotiateOutboundProtocol() error {
|
||||
if err := p.writeLocalVersionMsg(); err != nil {
|
||||
return err
|
||||
|
|
|
@ -6,7 +6,6 @@ package main
|
|||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/lbryio/lbcd/blockchain"
|
||||
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
||||
|
@ -182,57 +181,6 @@ func (cm *rpcConnManager) ConnectedPeers() []rpcserverPeer {
|
|||
return peers
|
||||
}
|
||||
|
||||
// BannedPeers returns a map consisting of all banned host with banned period.
|
||||
//
|
||||
// This function is safe for concurrent access and is part of the
|
||||
// rpcserverConnManager interface implementation.
|
||||
func (cm *rpcConnManager) BannedPeers() map[string]bannedPeriod {
|
||||
replyChan := make(chan map[string]bannedPeriod)
|
||||
cm.server.query <- listBannedPeersMsg{reply: replyChan}
|
||||
return <-replyChan
|
||||
}
|
||||
|
||||
// SetBan removes the peer associated with the provided address from the
|
||||
// list of persistent peers.
|
||||
//
|
||||
// This function is safe for concurrent access and is part of the
|
||||
// rpcserverConnManager interface implementation.
|
||||
func (cm *rpcConnManager) SetBan(addr string, since, until time.Time) error {
|
||||
replyChan := make(chan error)
|
||||
cm.server.query <- setBanMsg{
|
||||
addr: addr,
|
||||
since: since,
|
||||
until: until,
|
||||
reply: replyChan,
|
||||
}
|
||||
return <-replyChan
|
||||
}
|
||||
|
||||
// RemoveBan removes a host from banned list.
|
||||
//
|
||||
// This function is safe for concurrent access and is part of the
|
||||
// rpcserverConnManager interface implementation.
|
||||
func (cm *rpcConnManager) RemoveBan(addr string) error {
|
||||
replyChan := make(chan error)
|
||||
cm.server.query <- removeBanMsg{
|
||||
addr: addr,
|
||||
reply: replyChan,
|
||||
}
|
||||
return <-replyChan
|
||||
}
|
||||
|
||||
// ClearBanned removes all banned host with banned period.
|
||||
//
|
||||
// This function is safe for concurrent access and is part of the
|
||||
// rpcserverConnManager interface implementation.
|
||||
func (cm *rpcConnManager) ClearBanned() error {
|
||||
replyChan := make(chan error)
|
||||
cm.server.query <- clearBannedMsg{
|
||||
reply: replyChan,
|
||||
}
|
||||
return <-replyChan
|
||||
}
|
||||
|
||||
// PersistentPeers returns an array consisting of all the added persistent
|
||||
// peers.
|
||||
//
|
||||
|
|
|
@ -5,7 +5,7 @@
|
|||
/*
|
||||
Package rpcclient implements a websocket-enabled Bitcoin JSON-RPC client.
|
||||
|
||||
# Overview
|
||||
Overview
|
||||
|
||||
This client provides a robust and easy to use client for interfacing with a
|
||||
Bitcoin RPC server that uses a btcd/bitcoin core compatible Bitcoin JSON-RPC
|
||||
|
@ -24,7 +24,7 @@ btcd or btcwallet by default. However, configuration options are provided to
|
|||
fall back to HTTP POST and disable TLS to support talking with inferior bitcoin
|
||||
core style RPC servers.
|
||||
|
||||
# Websockets vs HTTP POST
|
||||
Websockets vs HTTP POST
|
||||
|
||||
In HTTP POST-based JSON-RPC, every request creates a new HTTP connection,
|
||||
issues the call, waits for the response, and closes the connection. This adds
|
||||
|
@ -40,7 +40,7 @@ can be invoked without having to go through a connect/disconnect cycle for every
|
|||
call. In addition, the websocket interface provides other nice features such as
|
||||
the ability to register for asynchronous notifications of various events.
|
||||
|
||||
# Synchronous vs Asynchronous API
|
||||
Synchronous vs Asynchronous API
|
||||
|
||||
The client provides both a synchronous (blocking) and asynchronous API.
|
||||
|
||||
|
@ -57,7 +57,7 @@ the Receive method on the returned instance will either return the result
|
|||
immediately if it has already arrived, or block until it has. This is useful
|
||||
since it provides the caller with greater control over concurrency.
|
||||
|
||||
# Notifications
|
||||
Notifications
|
||||
|
||||
The first important part of notifications is to realize that they will only
|
||||
work when connected via websockets. This should intuitively make sense
|
||||
|
@ -67,7 +67,7 @@ All notifications provided by btcd require registration to opt-in. For example,
|
|||
if you want to be notified when funds are received by a set of addresses, you
|
||||
register the addresses via the NotifyReceived (or NotifyReceivedAsync) function.
|
||||
|
||||
# Notification Handlers
|
||||
Notification Handlers
|
||||
|
||||
Notifications are exposed by the client through the use of callback handlers
|
||||
which are setup via a NotificationHandlers instance that is specified by the
|
||||
|
@ -83,7 +83,7 @@ will cause a deadlock as more server responses won't be read until the callback
|
|||
returns, but the callback would be waiting for a response. Thus, any
|
||||
additional RPCs must be issued an a completely decoupled manner.
|
||||
|
||||
# Automatic Reconnection
|
||||
Automatic Reconnection
|
||||
|
||||
By default, when running in websockets mode, this client will automatically
|
||||
keep trying to reconnect to the RPC server should the connection be lost. There
|
||||
|
@ -116,7 +116,7 @@ chain services will be available. Depending on your application, you might only
|
|||
need chain-related RPCs. In contrast, btcwallet provides pass through treatment
|
||||
for chain-related RPCs, so it supports them in addition to wallet-related RPCs.
|
||||
|
||||
# Errors
|
||||
Errors
|
||||
|
||||
There are 3 categories of errors that will be returned throughout this package:
|
||||
|
||||
|
@ -144,35 +144,35 @@ The third category of errors, that is errors returned by the server, can be
|
|||
detected by type asserting the error in a *btcjson.RPCError. For example, to
|
||||
detect if a command is unimplemented by the remote RPC server:
|
||||
|
||||
amount, err := client.GetBalance("")
|
||||
if err != nil {
|
||||
if jerr, ok := err.(*btcjson.RPCError); ok {
|
||||
switch jerr.Code {
|
||||
case btcjson.ErrRPCUnimplemented:
|
||||
// Handle not implemented error
|
||||
amount, err := client.GetBalance("")
|
||||
if err != nil {
|
||||
if jerr, ok := err.(*btcjson.RPCError); ok {
|
||||
switch jerr.Code {
|
||||
case btcjson.ErrRPCUnimplemented:
|
||||
// Handle not implemented error
|
||||
|
||||
// Handle other specific errors you care about
|
||||
}
|
||||
}
|
||||
// Handle other specific errors you care about
|
||||
}
|
||||
}
|
||||
|
||||
// Log or otherwise handle the error knowing it was not one returned
|
||||
// from the remote RPC server.
|
||||
}
|
||||
// Log or otherwise handle the error knowing it was not one returned
|
||||
// from the remote RPC server.
|
||||
}
|
||||
|
||||
# Example Usage
|
||||
Example Usage
|
||||
|
||||
The following full-blown client examples are in the examples directory:
|
||||
|
||||
- bitcoincorehttp
|
||||
Connects to a bitcoin core RPC server using HTTP POST mode with TLS disabled
|
||||
and gets the current block count
|
||||
- btcdwebsockets
|
||||
Connects to a btcd RPC server using TLS-secured websockets, registers for
|
||||
block connected and block disconnected notifications, and gets the current
|
||||
block count
|
||||
- btcwalletwebsockets
|
||||
Connects to a btcwallet RPC server using TLS-secured websockets, registers
|
||||
for notifications about changes to account balances, and gets a list of
|
||||
unspent transaction outputs (utxos) the wallet can sign
|
||||
- bitcoincorehttp
|
||||
Connects to a bitcoin core RPC server using HTTP POST mode with TLS disabled
|
||||
and gets the current block count
|
||||
- btcdwebsockets
|
||||
Connects to a btcd RPC server using TLS-secured websockets, registers for
|
||||
block connected and block disconnected notifications, and gets the current
|
||||
block count
|
||||
- btcwalletwebsockets
|
||||
Connects to a btcwallet RPC server using TLS-secured websockets, registers
|
||||
for notifications about changes to account balances, and gets a list of
|
||||
unspent transaction outputs (utxos) the wallet can sign
|
||||
*/
|
||||
package rpcclient
|
||||
|
|
|
@ -1,21 +1,15 @@
|
|||
# lbcdbloknotify
|
||||
# lbcd Websockets Example
|
||||
|
||||
This bridge program subscribes to lbcd's notifications over websockets using the rpcclient package.
|
||||
Users can specify supported actions upon receiving this notifications.
|
||||
This example shows how to use the rpcclient package to connect to a btcd RPC
|
||||
server using TLS-secured websockets, register for block connected and block
|
||||
disconnected notifications, and get the current block count.
|
||||
|
||||
## Building(or Running) the Program
|
||||
## Running the Example
|
||||
|
||||
Clone the lbcd package:
|
||||
The first step is to clone the lbcd package:
|
||||
|
||||
```bash
|
||||
$ git clone github.com/lbryio/lbcd
|
||||
$ cd lbcd/rpcclient/examples
|
||||
|
||||
# build the program
|
||||
$ go build .
|
||||
|
||||
# or directly run it (build implicitly behind the scene)
|
||||
$ go run .
|
||||
```
|
||||
|
||||
Display available options:
|
||||
|
@ -35,31 +29,18 @@ $ go run . -h
|
|||
Stratum server (default "lbrypool.net:3334")
|
||||
-stratumpass string
|
||||
Stratum server password (default "password")
|
||||
-quiet
|
||||
Do not print periodic logs
|
||||
```
|
||||
|
||||
Running the program:
|
||||
Start the program:
|
||||
|
||||
```bash
|
||||
# Send stratum mining.update_block mesage upon receving block connected notifiations.
|
||||
$ go run . -rpcuser <RPC USERNAME> -rpcpass <RPC PASSWD> --notls -stratum <STRATUM SERVER> -stratumpass <STRATUM PASSWD>
|
||||
$ go run . -stratumpass <STRATUM PASSWD> -rpcuser <RPC USERNAME> -rpcpass <RPC PASSWD>
|
||||
|
||||
2022/01/10 23:16:21 Current block count: 1093112
|
||||
2022/01/10 23:16:21 NotifyBlocks: Registration Complete
|
||||
2022/01/10 23:16:21 Block count: 1093112
|
||||
...
|
||||
|
||||
# Execute a custome command (with blockhash) upon receving block connected notifiations.
|
||||
$ go run . -rpcuser <RPC USERNAME> -rpcpass <RPC PASSWD> --notls -run "echo %s"
|
||||
```
|
||||
|
||||
## Notes
|
||||
|
||||
* Stratum TCP connection is persisted with auto-reconnect. (retry backoff increases from 1s to 60s maximum)
|
||||
|
||||
* Stratum update_block jobs on previous notifications are canceled when a new notification arrives.
|
||||
Usually, the jobs are so short and completed immediately. However, if the Stratum connection is broken, this
|
||||
prevents the bridge from accumulating stale jobs.
|
||||
|
||||
## License
|
||||
|
||||
This example is licensed under the [copyfree](http://copyfree.org) ISC License.
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"github.com/lbryio/lbcd/wire"
|
||||
"github.com/lbryio/lbcutil"
|
||||
)
|
||||
|
||||
type eventBlockConected struct {
|
||||
height int32
|
||||
header *wire.BlockHeader
|
||||
txns []*lbcutil.Tx
|
||||
}
|
||||
|
||||
type adapter struct {
|
||||
*bridge
|
||||
}
|
||||
|
||||
func (a *adapter) onFilteredBlockConnected(height int32, header *wire.BlockHeader, txns []*lbcutil.Tx) {
|
||||
a.eventCh <- &eventBlockConected{height, header, txns}
|
||||
}
|
|
@ -1,172 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
"time"
|
||||
)
|
||||
|
||||
type bridge struct {
|
||||
ctx context.Context
|
||||
|
||||
prevJobContext context.Context
|
||||
prevJobCancel context.CancelFunc
|
||||
|
||||
eventCh chan interface{}
|
||||
errorc chan error
|
||||
wg sync.WaitGroup
|
||||
|
||||
stratum *stratumClient
|
||||
|
||||
customCmd string
|
||||
}
|
||||
|
||||
func newBridge(stratumServer, stratumPass, coinid string) *bridge {
|
||||
|
||||
s := &bridge{
|
||||
ctx: context.Background(),
|
||||
eventCh: make(chan interface{}),
|
||||
errorc: make(chan error),
|
||||
}
|
||||
|
||||
if len(stratumServer) > 0 {
|
||||
s.stratum = newStratumClient(stratumServer, stratumPass, coinid)
|
||||
}
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (b *bridge) start() {
|
||||
|
||||
if b.stratum != nil {
|
||||
backoff := time.Second
|
||||
for {
|
||||
err := b.stratum.dial()
|
||||
if err == nil {
|
||||
break
|
||||
}
|
||||
log.Printf("WARN: stratum.dial() error: %s, retry in %s", err, backoff)
|
||||
time.Sleep(backoff)
|
||||
if backoff < 60*time.Second {
|
||||
backoff += time.Second
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for e := range b.eventCh {
|
||||
switch e := e.(type) {
|
||||
case *eventBlockConected:
|
||||
b.handleFilteredBlockConnected(e)
|
||||
default:
|
||||
b.errorc <- fmt.Errorf("unknown event type: %T", e)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *bridge) handleFilteredBlockConnected(e *eventBlockConected) {
|
||||
|
||||
if !*quiet {
|
||||
log.Printf("Block connected: %s (%d) %v", e.header.BlockHash(), e.height, e.header.Timestamp)
|
||||
}
|
||||
|
||||
hash := e.header.BlockHash().String()
|
||||
height := e.height
|
||||
|
||||
// Cancel jobs on previous block. It's safe if they are already done.
|
||||
if b.prevJobContext != nil {
|
||||
select {
|
||||
case <-b.prevJobContext.Done():
|
||||
log.Printf("prev one canceled")
|
||||
default:
|
||||
b.prevJobCancel()
|
||||
}
|
||||
}
|
||||
|
||||
// Wait until all previous jobs are done or canceled.
|
||||
b.wg.Wait()
|
||||
|
||||
// Create and save cancelable subcontext for new jobs.
|
||||
ctx, cancel := context.WithCancel(b.ctx)
|
||||
b.prevJobContext, b.prevJobCancel = ctx, cancel
|
||||
|
||||
if len(b.customCmd) > 0 {
|
||||
go b.execCustomCommand(ctx, hash, height)
|
||||
}
|
||||
|
||||
// Send stratum update block message
|
||||
if b.stratum != nil {
|
||||
go b.stratumUpdateBlock(ctx, hash, height)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *bridge) stratumUpdateBlock(ctx context.Context, hash string, height int32) {
|
||||
s.wg.Add(1)
|
||||
defer s.wg.Done()
|
||||
|
||||
backoff := time.Second
|
||||
retry := func(err error) {
|
||||
if backoff < 60*time.Second {
|
||||
backoff += time.Second
|
||||
}
|
||||
log.Printf("WARN: stratum.send() on block %d error: %s", height, err)
|
||||
time.Sleep(backoff)
|
||||
s.stratum.dial()
|
||||
}
|
||||
|
||||
msg := stratumUpdateBlockMsg(*stratumPass, *coinid, hash)
|
||||
|
||||
for {
|
||||
switch err := s.stratum.send(ctx, msg); {
|
||||
case err == nil:
|
||||
return
|
||||
case errors.Is(err, context.Canceled):
|
||||
log.Printf("INFO: stratum.send() on block %d: %s.", height, err)
|
||||
return
|
||||
case errors.Is(err, syscall.EPIPE):
|
||||
errClose := s.stratum.conn.Close()
|
||||
if errClose != nil {
|
||||
log.Printf("WARN: stratum.conn.Close() on block %d: %s.", height, errClose)
|
||||
}
|
||||
retry(err)
|
||||
case errors.Is(err, net.ErrClosed):
|
||||
retry(err)
|
||||
default:
|
||||
retry(err)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (s *bridge) execCustomCommand(ctx context.Context, hash string, height int32) {
|
||||
s.wg.Add(1)
|
||||
defer s.wg.Done()
|
||||
|
||||
cmd := strings.ReplaceAll(s.customCmd, "%s", hash)
|
||||
err := doExecCustomCommand(ctx, cmd)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: execCustomCommand on block %s(%d): %s", hash, height, err)
|
||||
}
|
||||
}
|
||||
|
||||
func doExecCustomCommand(ctx context.Context, cmd string) error {
|
||||
strs := strings.Split(cmd, " ")
|
||||
path, err := exec.LookPath(strs[0])
|
||||
if errors.Is(err, exec.ErrDot) {
|
||||
err = nil
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
c := exec.CommandContext(ctx, path, strs[1:]...)
|
||||
c.Stdout = os.Stdout
|
||||
return c.Run()
|
||||
}
|
|
@ -1,53 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"path/filepath"
|
||||
|
||||
"github.com/lbryio/lbcd/rpcclient"
|
||||
)
|
||||
|
||||
func newLbcdClient(server, user, pass string, notls bool, adpt adapter) *rpcclient.Client {
|
||||
|
||||
ntfnHandlers := rpcclient.NotificationHandlers{
|
||||
OnFilteredBlockConnected: adpt.onFilteredBlockConnected,
|
||||
}
|
||||
|
||||
// Config lbcd RPC client with websockets.
|
||||
connCfg := &rpcclient.ConnConfig{
|
||||
Host: server,
|
||||
Endpoint: "ws",
|
||||
User: user,
|
||||
Pass: pass,
|
||||
DisableTLS: true,
|
||||
}
|
||||
|
||||
if !notls {
|
||||
cert, err := ioutil.ReadFile(filepath.Join(lbcdHomeDir, "rpc.cert"))
|
||||
if err != nil {
|
||||
log.Fatalf("can't read lbcd certificate: %s", err)
|
||||
}
|
||||
connCfg.Certificates = cert
|
||||
connCfg.DisableTLS = false
|
||||
}
|
||||
|
||||
client, err := rpcclient.New(connCfg, &ntfnHandlers)
|
||||
if err != nil {
|
||||
log.Fatalf("can't create rpc client: %s", err)
|
||||
}
|
||||
|
||||
// Register for block connect and disconnect notifications.
|
||||
if err = client.NotifyBlocks(); err != nil {
|
||||
log.Fatalf("can't register block notification: %s", err)
|
||||
}
|
||||
|
||||
// Get the current block count.
|
||||
blockCount, err := client.GetBlockCount()
|
||||
if err != nil {
|
||||
log.Fatalf("can't get block count: %s", err)
|
||||
}
|
||||
log.Printf("Current block count: %d", blockCount)
|
||||
|
||||
return client
|
||||
}
|
|
@ -1,63 +1,103 @@
|
|||
// Copyright (c) 2014-2017 The btcsuite developers
|
||||
// Use of this source code is governed by an ISC
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"os/exec"
|
||||
"net"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/lbryio/lbcd/rpcclient"
|
||||
"github.com/lbryio/lbcd/wire"
|
||||
"github.com/lbryio/lbcutil"
|
||||
)
|
||||
|
||||
var (
|
||||
lbcdHomeDir = lbcutil.AppDataDir("lbcd", false)
|
||||
defaultCert = filepath.Join(lbcdHomeDir, "rpc.cert")
|
||||
)
|
||||
var (
|
||||
coinid = flag.String("coinid", "1425", "Coin ID")
|
||||
stratumServer = flag.String("stratum", "", "Stratum server")
|
||||
stratumPass = flag.String("stratumpass", "", "Stratum server password")
|
||||
rpcserver = flag.String("rpcserver", "localhost:9245", "LBCD RPC server")
|
||||
rpcuser = flag.String("rpcuser", "rpcuser", "LBCD RPC username")
|
||||
rpcpass = flag.String("rpcpass", "rpcpass", "LBCD RPC password")
|
||||
rpccert = flag.String("rpccert", defaultCert, "LBCD RPC certificate")
|
||||
notls = flag.Bool("notls", false, "Connect to LBCD with TLS disabled")
|
||||
run = flag.String("run", "", "Run custom shell command")
|
||||
quiet = flag.Bool("quiet", false, "Do not print logs")
|
||||
)
|
||||
func send(stratum, stratumPass, coinid, blockHash string) error {
|
||||
addr, err := net.ResolveTCPAddr("tcp", stratum)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't resolve addr: %w", err)
|
||||
}
|
||||
|
||||
conn, err := net.DialTCP("tcp", nil, addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't dial tcp: %w", err)
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
msg := fmt.Sprintf(`{"id":1,"method":"mining.update_block","params":[%q,%q,%q]}`,
|
||||
stratumPass, coinid, blockHash)
|
||||
|
||||
_, err = conn.Write([]byte(msg))
|
||||
if err != nil {
|
||||
return fmt.Errorf("can't write message: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
||||
var (
|
||||
coinid = flag.String("coinid", "1425", "Coin ID")
|
||||
stratum = flag.String("stratum", "lbrypool.net:3334", "Stratum server")
|
||||
stratumPass = flag.String("stratumpass", "password", "Stratum server password")
|
||||
rpcserver = flag.String("rpcserver", "localhost:9245", "LBCD RPC server")
|
||||
rpcuser = flag.String("rpcuser", "rpcuser", "LBCD RPC username")
|
||||
rpcpass = flag.String("rpcpass", "rpcpass", "LBCD RPC password")
|
||||
notls = flag.Bool("notls", false, "Connect to LBCD with TLS disabled")
|
||||
)
|
||||
|
||||
flag.Parse()
|
||||
|
||||
// Setup notification handler
|
||||
b := newBridge(*stratumServer, *stratumPass, *coinid)
|
||||
ntfnHandlers := rpcclient.NotificationHandlers{
|
||||
OnFilteredBlockConnected: func(height int32, header *wire.BlockHeader, txns []*lbcutil.Tx) {
|
||||
|
||||
if len(*run) > 0 {
|
||||
// Check if ccommand exists.
|
||||
strs := strings.Split(*run, " ")
|
||||
cmd := strs[0]
|
||||
_, err := exec.LookPath(cmd)
|
||||
if err != nil {
|
||||
log.Fatalf("ERROR: %s not found: %s", cmd, err)
|
||||
}
|
||||
b.customCmd = *run
|
||||
blockHash := header.BlockHash().String()
|
||||
|
||||
log.Printf("Block connected: %v (%d) %v", blockHash, height, header.Timestamp)
|
||||
|
||||
if err := send(*stratum, *stratumPass, *coinid, blockHash); err != nil {
|
||||
log.Printf("ERROR: failed to notify stratum: %s", err)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
// Start the eventt handler.
|
||||
go b.start()
|
||||
// Connect to local lbcd RPC server using websockets.
|
||||
lbcdHomeDir := lbcutil.AppDataDir("lbcd", false)
|
||||
certs, err := ioutil.ReadFile(filepath.Join(lbcdHomeDir, "rpc.cert"))
|
||||
if err != nil {
|
||||
log.Fatalf("can't read lbcd certificate: %s", err)
|
||||
}
|
||||
connCfg := &rpcclient.ConnConfig{
|
||||
Host: *rpcserver,
|
||||
Endpoint: "ws",
|
||||
User: *rpcuser,
|
||||
Pass: *rpcpass,
|
||||
Certificates: certs,
|
||||
DisableTLS: *notls,
|
||||
}
|
||||
client, err := rpcclient.New(connCfg, &ntfnHandlers)
|
||||
if err != nil {
|
||||
log.Fatalf("can't create rpc client: %s", err)
|
||||
}
|
||||
|
||||
// Adaptater receives lbcd notifications, and emit events.
|
||||
adpt := adapter{b}
|
||||
// Register for block connect and disconnect notifications.
|
||||
if err = client.NotifyBlocks(); err != nil {
|
||||
log.Fatalf("can't register block notification: %s", err)
|
||||
}
|
||||
log.Printf("NotifyBlocks: Registration Complete")
|
||||
|
||||
client := newLbcdClient(*rpcserver, *rpcuser, *rpcpass, *notls, adpt)
|
||||
|
||||
go func() {
|
||||
err := <-b.errorc
|
||||
log.Fatalf("ERROR: %s", err)
|
||||
client.Shutdown()
|
||||
}()
|
||||
// Get the current block count.
|
||||
blockCount, err := client.GetBlockCount()
|
||||
if err != nil {
|
||||
log.Fatalf("can't get block count: %s", err)
|
||||
}
|
||||
log.Printf("Block count: %d", blockCount)
|
||||
|
||||
// Wait until the client either shuts down gracefully (or the user
|
||||
// terminates the process with Ctrl+C).
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
)
|
||||
|
||||
type stratumClient struct {
|
||||
server string
|
||||
passwd string
|
||||
coinid string
|
||||
conn *net.TCPConn
|
||||
}
|
||||
|
||||
func newStratumClient(server, passwd, coinid string) *stratumClient {
|
||||
|
||||
return &stratumClient{
|
||||
server: server,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *stratumClient) dial() error {
|
||||
|
||||
addr, err := net.ResolveTCPAddr("tcp", c.server)
|
||||
if err != nil {
|
||||
return fmt.Errorf("resolve tcp addr: %w", err)
|
||||
}
|
||||
|
||||
conn, err := net.DialTCP("tcp", nil, addr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("dial tcp: %w", err)
|
||||
}
|
||||
c.conn = conn
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *stratumClient) send(ctx context.Context, msg string) error {
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
_, err := c.conn.Write([]byte(msg))
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func stratumUpdateBlockMsg(stratumPass, coinid, blockHash string) string {
|
||||
|
||||
return fmt.Sprintf(`{"id":1,"method":"mining.update_block","params":[%q,%s,%q]}`,
|
||||
stratumPass, coinid, blockHash)
|
||||
}
|
|
@ -56,8 +56,7 @@ func (c *Client) DebugLevelAsync(levelSpec string) FutureDebugLevelResult {
|
|||
// specification.
|
||||
//
|
||||
// The levelspec can be either a debug level or of the form:
|
||||
//
|
||||
// <subsystem>=<level>,<subsystem2>=<level2>,...
|
||||
// <subsystem>=<level>,<subsystem2>=<level2>,...
|
||||
//
|
||||
// Additionally, the special keyword 'show' can be used to get a list of the
|
||||
// available subsystems.
|
||||
|
|
|
@ -774,8 +774,7 @@ func (c *Client) handleSendPostMessage(jReq *jsonRequest) {
|
|||
tries := 10
|
||||
for i := 0; tries == 0 || i < tries; i++ {
|
||||
bodyReader := bytes.NewReader(jReq.marshalledJSON)
|
||||
var httpReq *http.Request
|
||||
httpReq, err = http.NewRequest("POST", url, bodyReader)
|
||||
httpReq, err := http.NewRequest("POST", url, bodyReader)
|
||||
if err != nil {
|
||||
jReq.responseChan <- &Response{result: nil, err: err}
|
||||
return
|
||||
|
@ -787,8 +786,7 @@ func (c *Client) handleSendPostMessage(jReq *jsonRequest) {
|
|||
}
|
||||
|
||||
// Configure basic access authorization.
|
||||
var user, pass string
|
||||
user, pass, err = c.config.getAuth()
|
||||
user, pass, err := c.config.getAuth()
|
||||
if err != nil {
|
||||
jReq.responseChan <- &Response{result: nil, err: err}
|
||||
return
|
||||
|
|
|
@ -355,75 +355,6 @@ func (c *Client) GetPeerInfo() ([]btcjson.GetPeerInfoResult, error) {
|
|||
return c.GetPeerInfoAsync().Receive()
|
||||
}
|
||||
|
||||
// FutureListBannedResult is a future promise to deliver the result of a
|
||||
// ListBannedAsync RPC invocation (or an applicable error).
|
||||
type FutureListBannedResult chan *Response
|
||||
|
||||
// Receive waits for the Response promised by the future and returns data about
|
||||
// each connected network peer.
|
||||
func (r FutureListBannedResult) Receive() ([]btcjson.ListBannedResult, error) {
|
||||
res, err := ReceiveFuture(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Unmarshal result as an array of ListBanned result objects.
|
||||
var bannedPeers []btcjson.ListBannedResult
|
||||
err = json.Unmarshal(res, &bannedPeers)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return bannedPeers, nil
|
||||
}
|
||||
|
||||
// SetBanCommand enumerates the available commands that the SetBanCommand function
|
||||
// accepts.
|
||||
type SetBanCommand string
|
||||
|
||||
// Constants used to indicate the command for the SetBanCommand function.
|
||||
const (
|
||||
// SBAdd indicates the specified host should be added as a banned
|
||||
// peer.
|
||||
SBAdd SetBanCommand = "add"
|
||||
|
||||
// SBRemove indicates the specified peer should be removed.
|
||||
SBRemove SetBanCommand = "remove"
|
||||
)
|
||||
|
||||
// String returns the SetBanCommand in human-readable form.
|
||||
func (cmd SetBanCommand) String() string {
|
||||
return string(cmd)
|
||||
}
|
||||
|
||||
// FutureSetBanResult is a future promise to deliver the result of an
|
||||
// SetBanAsync RPC invocation (or an applicable error).
|
||||
type FutureSetBanResult chan *Response
|
||||
|
||||
// Receive waits for the Response promised by the future and returns an error if
|
||||
// any occurred when performing the specified command.
|
||||
func (r FutureSetBanResult) Receive() error {
|
||||
_, err := ReceiveFuture(r)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetBanAsync returns an instance of a type that can be used to get the result
|
||||
// of the RPC at some future time by invoking the Receive function on the
|
||||
// returned instance.
|
||||
func (c *Client) SetBanAsync(addr string, command string, banTime *int,
|
||||
absolute *bool) FutureSetBanResult {
|
||||
cmd := btcjson.NewSetBanCmd(addr, btcjson.SetBanSubCmd(command), banTime,
|
||||
absolute)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
// SetBan attempts to perform the passed command on the passed persistent peer.
|
||||
// For example, it can be used to add or a remove a banned peer.
|
||||
func (c *Client) SetBan(addr string, command string, banTime *int,
|
||||
absolute *bool) error {
|
||||
return c.SetBanAsync(addr, command, banTime, absolute).Receive()
|
||||
}
|
||||
|
||||
// FutureGetNetTotalsResult is a future promise to deliver the result of a
|
||||
// GetNetTotalsAsync RPC invocation (or an applicable error).
|
||||
type FutureGetNetTotalsResult chan *Response
|
||||
|
|
|
@ -291,18 +291,13 @@ func (r FutureCreateRawTransactionResult) Receive() (*wire.MsgTx, error) {
|
|||
//
|
||||
// See CreateRawTransaction for the blocking version and more details.
|
||||
func (c *Client) CreateRawTransactionAsync(inputs []btcjson.TransactionInput,
|
||||
outputs map[btcutil.Address]interface{}, lockTime *int64) FutureCreateRawTransactionResult {
|
||||
amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) FutureCreateRawTransactionResult {
|
||||
|
||||
convertedData := make(map[string]interface{}, len(outputs))
|
||||
for key, value := range outputs {
|
||||
switch val := value.(type) {
|
||||
case btcutil.Amount:
|
||||
convertedData[key.String()] = val.ToBTC()
|
||||
case string:
|
||||
convertedData[key.String()] = val
|
||||
}
|
||||
convertedAmts := make(map[string]float64, len(amounts))
|
||||
for addr, amount := range amounts {
|
||||
convertedAmts[addr.String()] = amount.ToBTC()
|
||||
}
|
||||
cmd := btcjson.NewCreateRawTransactionCmd(inputs, convertedData, lockTime)
|
||||
cmd := btcjson.NewCreateRawTransactionCmd(inputs, convertedAmts, lockTime)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -310,9 +305,9 @@ func (c *Client) CreateRawTransactionAsync(inputs []btcjson.TransactionInput,
|
|||
// and sending to the provided addresses. If the inputs are either nil or an
|
||||
// empty slice, it is interpreted as an empty slice.
|
||||
func (c *Client) CreateRawTransaction(inputs []btcjson.TransactionInput,
|
||||
outputs map[btcutil.Address]interface{}, lockTime *int64) (*wire.MsgTx, error) {
|
||||
amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) (*wire.MsgTx, error) {
|
||||
|
||||
return c.CreateRawTransactionAsync(inputs, outputs, lockTime).Receive()
|
||||
return c.CreateRawTransactionAsync(inputs, amounts, lockTime).Receive()
|
||||
}
|
||||
|
||||
// FutureSendRawTransactionResult is a future promise to deliver the result
|
||||
|
|
|
@ -536,10 +536,9 @@ func (r FutureSendToAddressResult) Receive() (*chainhash.Hash, error) {
|
|||
// returned instance.
|
||||
//
|
||||
// See SendToAddress for the blocking version and more details.
|
||||
func (c *Client) SendToAddressAsync(address btcutil.Address, amount btcutil.Amount,
|
||||
addrType *string) FutureSendToAddressResult {
|
||||
func (c *Client) SendToAddressAsync(address btcutil.Address, amount btcutil.Amount) FutureSendToAddressResult {
|
||||
addr := address.EncodeAddress()
|
||||
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), addrType, nil, nil)
|
||||
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), nil, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -551,9 +550,8 @@ func (c *Client) SendToAddressAsync(address btcutil.Address, amount btcutil.Amou
|
|||
//
|
||||
// NOTE: This function requires to the wallet to be unlocked. See the
|
||||
// WalletPassphrase function for more details.
|
||||
func (c *Client) SendToAddress(address btcutil.Address, amount btcutil.Amount,
|
||||
addrType *string) (*chainhash.Hash, error) {
|
||||
return c.SendToAddressAsync(address, amount, addrType).Receive()
|
||||
func (c *Client) SendToAddress(address btcutil.Address, amount btcutil.Amount) (*chainhash.Hash, error) {
|
||||
return c.SendToAddressAsync(address, amount).Receive()
|
||||
}
|
||||
|
||||
// SendToAddressCommentAsync returns an instance of a type that can be used to
|
||||
|
@ -562,12 +560,12 @@ func (c *Client) SendToAddress(address btcutil.Address, amount btcutil.Amount,
|
|||
//
|
||||
// See SendToAddressComment for the blocking version and more details.
|
||||
func (c *Client) SendToAddressCommentAsync(address btcutil.Address,
|
||||
amount btcutil.Amount, addrType *string, comment string,
|
||||
amount btcutil.Amount, comment,
|
||||
commentTo string) FutureSendToAddressResult {
|
||||
|
||||
addr := address.EncodeAddress()
|
||||
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), addrType,
|
||||
&comment, &commentTo)
|
||||
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), &comment,
|
||||
&commentTo)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -583,10 +581,9 @@ func (c *Client) SendToAddressCommentAsync(address btcutil.Address,
|
|||
//
|
||||
// NOTE: This function requires to the wallet to be unlocked. See the
|
||||
// WalletPassphrase function for more details.
|
||||
func (c *Client) SendToAddressComment(address btcutil.Address, amount btcutil.Amount,
|
||||
addrType *string, comment, commentTo string) (*chainhash.Hash, error) {
|
||||
return c.SendToAddressCommentAsync(address, amount, addrType,
|
||||
comment, commentTo).Receive()
|
||||
func (c *Client) SendToAddressComment(address btcutil.Address, amount btcutil.Amount, comment, commentTo string) (*chainhash.Hash, error) {
|
||||
return c.SendToAddressCommentAsync(address, amount, comment,
|
||||
commentTo).Receive()
|
||||
}
|
||||
|
||||
// FutureSendFromResult is a future promise to deliver the result of a
|
||||
|
@ -618,11 +615,10 @@ func (r FutureSendFromResult) Receive() (*chainhash.Hash, error) {
|
|||
// returned instance.
|
||||
//
|
||||
// See SendFrom for the blocking version and more details.
|
||||
func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address,
|
||||
amount btcutil.Amount, addrType *string) FutureSendFromResult {
|
||||
func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount) FutureSendFromResult {
|
||||
addr := toAddress.EncodeAddress()
|
||||
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), nil,
|
||||
addrType, nil, nil)
|
||||
nil, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -634,8 +630,8 @@ func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address,
|
|||
//
|
||||
// NOTE: This function requires to the wallet to be unlocked. See the
|
||||
// WalletPassphrase function for more details.
|
||||
func (c *Client) SendFrom(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount, addrType *string) (*chainhash.Hash, error) {
|
||||
return c.SendFromAsync(fromAccount, toAddress, amount, addrType).Receive()
|
||||
func (c *Client) SendFrom(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount) (*chainhash.Hash, error) {
|
||||
return c.SendFromAsync(fromAccount, toAddress, amount).Receive()
|
||||
}
|
||||
|
||||
// SendFromMinConfAsync returns an instance of a type that can be used to get
|
||||
|
@ -643,12 +639,10 @@ func (c *Client) SendFrom(fromAccount string, toAddress btcutil.Address, amount
|
|||
// the returned instance.
|
||||
//
|
||||
// See SendFromMinConf for the blocking version and more details.
|
||||
func (c *Client) SendFromMinConfAsync(fromAccount string,
|
||||
toAddress btcutil.Address, amount btcutil.Amount,
|
||||
minConfirms int, addrType *string) FutureSendFromResult {
|
||||
func (c *Client) SendFromMinConfAsync(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount, minConfirms int) FutureSendFromResult {
|
||||
addr := toAddress.EncodeAddress()
|
||||
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(),
|
||||
&minConfirms, addrType, nil, nil)
|
||||
&minConfirms, nil, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -661,10 +655,9 @@ func (c *Client) SendFromMinConfAsync(fromAccount string,
|
|||
//
|
||||
// NOTE: This function requires to the wallet to be unlocked. See the
|
||||
// WalletPassphrase function for more details.
|
||||
func (c *Client) SendFromMinConf(fromAccount string, toAddress btcutil.Address,
|
||||
amount btcutil.Amount, minConfirms int, addrType *string) (*chainhash.Hash, error) {
|
||||
func (c *Client) SendFromMinConf(fromAccount string, toAddress btcutil.Address, amount btcutil.Amount, minConfirms int) (*chainhash.Hash, error) {
|
||||
return c.SendFromMinConfAsync(fromAccount, toAddress, amount,
|
||||
minConfirms, addrType).Receive()
|
||||
minConfirms).Receive()
|
||||
}
|
||||
|
||||
// SendFromCommentAsync returns an instance of a type that can be used to get
|
||||
|
@ -674,11 +667,11 @@ func (c *Client) SendFromMinConf(fromAccount string, toAddress btcutil.Address,
|
|||
// See SendFromComment for the blocking version and more details.
|
||||
func (c *Client) SendFromCommentAsync(fromAccount string,
|
||||
toAddress btcutil.Address, amount btcutil.Amount, minConfirms int,
|
||||
addrType *string, comment, commentTo string) FutureSendFromResult {
|
||||
comment, commentTo string) FutureSendFromResult {
|
||||
|
||||
addr := toAddress.EncodeAddress()
|
||||
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(),
|
||||
&minConfirms, addrType, &comment, &commentTo)
|
||||
&minConfirms, &comment, &commentTo)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -694,11 +687,11 @@ func (c *Client) SendFromCommentAsync(fromAccount string,
|
|||
// NOTE: This function requires to the wallet to be unlocked. See the
|
||||
// WalletPassphrase function for more details.
|
||||
func (c *Client) SendFromComment(fromAccount string, toAddress btcutil.Address,
|
||||
amount btcutil.Amount, minConfirms int, addrType *string,
|
||||
amount btcutil.Amount, minConfirms int,
|
||||
comment, commentTo string) (*chainhash.Hash, error) {
|
||||
|
||||
return c.SendFromCommentAsync(fromAccount, toAddress, amount,
|
||||
minConfirms, addrType, comment, commentTo).Receive()
|
||||
minConfirms, comment, commentTo).Receive()
|
||||
}
|
||||
|
||||
// FutureSendManyResult is a future promise to deliver the result of a
|
||||
|
@ -735,7 +728,7 @@ func (c *Client) SendManyAsync(fromAccount string, amounts map[btcutil.Address]b
|
|||
for addr, amount := range amounts {
|
||||
convertedAmounts[addr.EncodeAddress()] = amount.ToBTC()
|
||||
}
|
||||
cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, nil, nil, nil)
|
||||
cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, nil, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -758,14 +751,14 @@ func (c *Client) SendMany(fromAccount string, amounts map[btcutil.Address]btcuti
|
|||
// See SendManyMinConf for the blocking version and more details.
|
||||
func (c *Client) SendManyMinConfAsync(fromAccount string,
|
||||
amounts map[btcutil.Address]btcutil.Amount,
|
||||
minConfirms int, addrType *string) FutureSendManyResult {
|
||||
minConfirms int) FutureSendManyResult {
|
||||
|
||||
convertedAmounts := make(map[string]float64, len(amounts))
|
||||
for addr, amount := range amounts {
|
||||
convertedAmounts[addr.EncodeAddress()] = amount.ToBTC()
|
||||
}
|
||||
cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts,
|
||||
&minConfirms, nil, addrType)
|
||||
&minConfirms, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -780,10 +773,9 @@ func (c *Client) SendManyMinConfAsync(fromAccount string,
|
|||
// WalletPassphrase function for more details.
|
||||
func (c *Client) SendManyMinConf(fromAccount string,
|
||||
amounts map[btcutil.Address]btcutil.Amount,
|
||||
minConfirms int, addrType *string) (*chainhash.Hash, error) {
|
||||
minConfirms int) (*chainhash.Hash, error) {
|
||||
|
||||
return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms,
|
||||
addrType).Receive()
|
||||
return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms).Receive()
|
||||
}
|
||||
|
||||
// SendManyCommentAsync returns an instance of a type that can be used to get
|
||||
|
@ -793,14 +785,14 @@ func (c *Client) SendManyMinConf(fromAccount string,
|
|||
// See SendManyComment for the blocking version and more details.
|
||||
func (c *Client) SendManyCommentAsync(fromAccount string,
|
||||
amounts map[btcutil.Address]btcutil.Amount, minConfirms int,
|
||||
addrType *string, comment string) FutureSendManyResult {
|
||||
comment string) FutureSendManyResult {
|
||||
|
||||
convertedAmounts := make(map[string]float64, len(amounts))
|
||||
for addr, amount := range amounts {
|
||||
convertedAmounts[addr.EncodeAddress()] = amount.ToBTC()
|
||||
}
|
||||
cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts,
|
||||
&minConfirms, &comment, addrType)
|
||||
&minConfirms, &comment)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -816,10 +808,10 @@ func (c *Client) SendManyCommentAsync(fromAccount string,
|
|||
// WalletPassphrase function for more details.
|
||||
func (c *Client) SendManyComment(fromAccount string,
|
||||
amounts map[btcutil.Address]btcutil.Amount, minConfirms int,
|
||||
addrType *string, comment string) (*chainhash.Hash, error) {
|
||||
comment string) (*chainhash.Hash, error) {
|
||||
|
||||
return c.SendManyCommentAsync(fromAccount, amounts, minConfirms,
|
||||
addrType, comment).Receive()
|
||||
comment).Receive()
|
||||
}
|
||||
|
||||
// *************************
|
||||
|
@ -1024,10 +1016,10 @@ func (c *Client) CreateWalletAsync(name string, opts ...CreateWalletOpt) FutureC
|
|||
//
|
||||
// Optional parameters can be specified using functional-options pattern. The
|
||||
// following functions are available:
|
||||
// - WithCreateWalletDisablePrivateKeys
|
||||
// - WithCreateWalletBlank
|
||||
// - WithCreateWalletPassphrase
|
||||
// - WithCreateWalletAvoidReuse
|
||||
// * WithCreateWalletDisablePrivateKeys
|
||||
// * WithCreateWalletBlank
|
||||
// * WithCreateWalletPassphrase
|
||||
// * WithCreateWalletAvoidReuse
|
||||
func (c *Client) CreateWallet(name string, opts ...CreateWalletOpt) (*btcjson.CreateWalletResult, error) {
|
||||
return c.CreateWalletAsync(name, opts...).Receive()
|
||||
}
|
||||
|
@ -1143,8 +1135,8 @@ func (r FutureGetRawChangeAddressResult) Receive() (btcutil.Address, error) {
|
|||
// function on the returned instance.
|
||||
//
|
||||
// See GetRawChangeAddress for the blocking version and more details.
|
||||
func (c *Client) GetRawChangeAddressAsync(account *string) FutureGetRawChangeAddressResult {
|
||||
cmd := btcjson.NewGetRawChangeAddressCmd(account)
|
||||
func (c *Client) GetRawChangeAddressAsync(account string) FutureGetRawChangeAddressResult {
|
||||
cmd := btcjson.NewGetRawChangeAddressCmd(&account)
|
||||
result := FutureGetRawChangeAddressResult{
|
||||
network: c.chainParams,
|
||||
responseChannel: c.SendCmd(cmd),
|
||||
|
@ -1155,7 +1147,7 @@ func (c *Client) GetRawChangeAddressAsync(account *string) FutureGetRawChangeAdd
|
|||
// GetRawChangeAddress returns a new address for receiving change that will be
|
||||
// associated with the provided account. Note that this is only for raw
|
||||
// transactions and NOT for normal use.
|
||||
func (c *Client) GetRawChangeAddress(account *string) (btcutil.Address, error) {
|
||||
func (c *Client) GetRawChangeAddress(account string) (btcutil.Address, error) {
|
||||
return c.GetRawChangeAddressAsync(account).Receive()
|
||||
}
|
||||
|
||||
|
@ -1234,7 +1226,7 @@ func (r FutureGetAccountAddressResult) Receive() (btcutil.Address, error) {
|
|||
// the returned instance.
|
||||
//
|
||||
// See GetAccountAddress for the blocking version and more details.
|
||||
func (c *Client) GetAccountAddressAsync(account *string) FutureGetAccountAddressResult {
|
||||
func (c *Client) GetAccountAddressAsync(account string) FutureGetAccountAddressResult {
|
||||
cmd := btcjson.NewGetAccountAddressCmd(account)
|
||||
result := FutureGetAccountAddressResult{
|
||||
network: c.chainParams,
|
||||
|
@ -1245,7 +1237,7 @@ func (c *Client) GetAccountAddressAsync(account *string) FutureGetAccountAddress
|
|||
|
||||
// GetAccountAddress returns the current Bitcoin address for receiving payments
|
||||
// to the specified account.
|
||||
func (c *Client) GetAccountAddress(account *string) (btcutil.Address, error) {
|
||||
func (c *Client) GetAccountAddress(account string) (btcutil.Address, error) {
|
||||
return c.GetAccountAddressAsync(account).Receive()
|
||||
}
|
||||
|
||||
|
@ -1287,6 +1279,33 @@ func (c *Client) GetAccount(address btcutil.Address) (string, error) {
|
|||
return c.GetAccountAsync(address).Receive()
|
||||
}
|
||||
|
||||
// FutureSetAccountResult is a future promise to deliver the result of a
|
||||
// SetAccountAsync RPC invocation (or an applicable error).
|
||||
type FutureSetAccountResult chan *Response
|
||||
|
||||
// Receive waits for the Response promised by the future and returns the result
|
||||
// of setting the account to be associated with the passed address.
|
||||
func (r FutureSetAccountResult) Receive() error {
|
||||
_, err := ReceiveFuture(r)
|
||||
return err
|
||||
}
|
||||
|
||||
// SetAccountAsync returns an instance of a type that can be used to get the
|
||||
// result of the RPC at some future time by invoking the Receive function on the
|
||||
// returned instance.
|
||||
//
|
||||
// See SetAccount for the blocking version and more details.
|
||||
func (c *Client) SetAccountAsync(address btcutil.Address, account string) FutureSetAccountResult {
|
||||
addr := address.EncodeAddress()
|
||||
cmd := btcjson.NewSetAccountCmd(addr, account)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
// SetAccount sets the account associated with the passed address.
|
||||
func (c *Client) SetAccount(address btcutil.Address, account string) error {
|
||||
return c.SetAccountAsync(address, account).Receive()
|
||||
}
|
||||
|
||||
// FutureGetAddressesByAccountResult is a future promise to deliver the result
|
||||
// of a GetAddressesByAccountAsync RPC invocation (or an applicable error).
|
||||
type FutureGetAddressesByAccountResult struct {
|
||||
|
@ -1325,7 +1344,7 @@ func (r FutureGetAddressesByAccountResult) Receive() ([]btcutil.Address, error)
|
|||
// function on the returned instance.
|
||||
//
|
||||
// See GetAddressesByAccount for the blocking version and more details.
|
||||
func (c *Client) GetAddressesByAccountAsync(account *string) FutureGetAddressesByAccountResult {
|
||||
func (c *Client) GetAddressesByAccountAsync(account string) FutureGetAddressesByAccountResult {
|
||||
cmd := btcjson.NewGetAddressesByAccountCmd(account)
|
||||
result := FutureGetAddressesByAccountResult{
|
||||
network: c.chainParams,
|
||||
|
@ -1336,7 +1355,7 @@ func (c *Client) GetAddressesByAccountAsync(account *string) FutureGetAddressesB
|
|||
|
||||
// GetAddressesByAccount returns the list of addresses associated with the
|
||||
// passed account.
|
||||
func (c *Client) GetAddressesByAccount(account *string) ([]btcutil.Address, error) {
|
||||
func (c *Client) GetAddressesByAccount(account string) ([]btcutil.Address, error) {
|
||||
return c.GetAddressesByAccountAsync(account).Receive()
|
||||
}
|
||||
|
||||
|
@ -1363,6 +1382,74 @@ func (r FutureMoveResult) Receive() (bool, error) {
|
|||
return moveResult, nil
|
||||
}
|
||||
|
||||
// MoveAsync returns an instance of a type that can be used to get the result of
|
||||
// the RPC at some future time by invoking the Receive function on the returned
|
||||
// instance.
|
||||
//
|
||||
// See Move for the blocking version and more details.
|
||||
func (c *Client) MoveAsync(fromAccount, toAccount string, amount btcutil.Amount) FutureMoveResult {
|
||||
cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(), nil,
|
||||
nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
// Move moves specified amount from one account in your wallet to another. Only
|
||||
// funds with the default number of minimum confirmations will be used.
|
||||
//
|
||||
// See MoveMinConf and MoveComment for different options.
|
||||
func (c *Client) Move(fromAccount, toAccount string, amount btcutil.Amount) (bool, error) {
|
||||
return c.MoveAsync(fromAccount, toAccount, amount).Receive()
|
||||
}
|
||||
|
||||
// MoveMinConfAsync returns an instance of a type that can be used to get the
|
||||
// result of the RPC at some future time by invoking the Receive function on the
|
||||
// returned instance.
|
||||
//
|
||||
// See MoveMinConf for the blocking version and more details.
|
||||
func (c *Client) MoveMinConfAsync(fromAccount, toAccount string,
|
||||
amount btcutil.Amount, minConfirms int) FutureMoveResult {
|
||||
|
||||
cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(),
|
||||
&minConfirms, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
// MoveMinConf moves specified amount from one account in your wallet to
|
||||
// another. Only funds with the passed number of minimum confirmations will be
|
||||
// used.
|
||||
//
|
||||
// See Move to use the default number of minimum confirmations and MoveComment
|
||||
// for additional options.
|
||||
func (c *Client) MoveMinConf(fromAccount, toAccount string, amount btcutil.Amount, minConf int) (bool, error) {
|
||||
return c.MoveMinConfAsync(fromAccount, toAccount, amount, minConf).Receive()
|
||||
}
|
||||
|
||||
// MoveCommentAsync returns an instance of a type that can be used to get the
|
||||
// result of the RPC at some future time by invoking the Receive function on the
|
||||
// returned instance.
|
||||
//
|
||||
// See MoveComment for the blocking version and more details.
|
||||
func (c *Client) MoveCommentAsync(fromAccount, toAccount string,
|
||||
amount btcutil.Amount, minConfirms int, comment string) FutureMoveResult {
|
||||
|
||||
cmd := btcjson.NewMoveCmd(fromAccount, toAccount, amount.ToBTC(),
|
||||
&minConfirms, &comment)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
// MoveComment moves specified amount from one account in your wallet to
|
||||
// another and stores the provided comment in the wallet. The comment
|
||||
// parameter is only available in the wallet. Only funds with the passed number
|
||||
// of minimum confirmations will be used.
|
||||
//
|
||||
// See Move and MoveMinConf to use defaults.
|
||||
func (c *Client) MoveComment(fromAccount, toAccount string, amount btcutil.Amount,
|
||||
minConf int, comment string) (bool, error) {
|
||||
|
||||
return c.MoveCommentAsync(fromAccount, toAccount, amount, minConf,
|
||||
comment).Receive()
|
||||
}
|
||||
|
||||
// FutureRenameAccountResult is a future promise to deliver the result of a
|
||||
// RenameAccountAsync RPC invocation (or an applicable error).
|
||||
type FutureRenameAccountResult chan *Response
|
||||
|
@ -1717,7 +1804,7 @@ func (r FutureGetReceivedByAccountResult) Receive() (btcutil.Amount, error) {
|
|||
// function on the returned instance.
|
||||
//
|
||||
// See GetReceivedByAccount for the blocking version and more details.
|
||||
func (c *Client) GetReceivedByAccountAsync(account *string) FutureGetReceivedByAccountResult {
|
||||
func (c *Client) GetReceivedByAccountAsync(account string) FutureGetReceivedByAccountResult {
|
||||
cmd := btcjson.NewGetReceivedByAccountCmd(account, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
@ -1727,7 +1814,7 @@ func (c *Client) GetReceivedByAccountAsync(account *string) FutureGetReceivedByA
|
|||
//
|
||||
// See GetReceivedByAccountMinConf to override the minimum number of
|
||||
// confirmations.
|
||||
func (c *Client) GetReceivedByAccount(account *string) (btcutil.Amount, error) {
|
||||
func (c *Client) GetReceivedByAccount(account string) (btcutil.Amount, error) {
|
||||
return c.GetReceivedByAccountAsync(account).Receive()
|
||||
}
|
||||
|
||||
|
@ -1736,8 +1823,8 @@ func (c *Client) GetReceivedByAccount(account *string) (btcutil.Amount, error) {
|
|||
// function on the returned instance.
|
||||
//
|
||||
// See GetReceivedByAccountMinConf for the blocking version and more details.
|
||||
func (c *Client) GetReceivedByAccountMinConfAsync(account *string, minConfirms *int) FutureGetReceivedByAccountResult {
|
||||
cmd := btcjson.NewGetReceivedByAccountCmd(account, minConfirms)
|
||||
func (c *Client) GetReceivedByAccountMinConfAsync(account string, minConfirms int) FutureGetReceivedByAccountResult {
|
||||
cmd := btcjson.NewGetReceivedByAccountCmd(account, &minConfirms)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -1746,7 +1833,7 @@ func (c *Client) GetReceivedByAccountMinConfAsync(account *string, minConfirms *
|
|||
// confirmations.
|
||||
//
|
||||
// See GetReceivedByAccount to use the default minimum number of confirmations.
|
||||
func (c *Client) GetReceivedByAccountMinConf(account *string, minConfirms *int) (btcutil.Amount, error) {
|
||||
func (c *Client) GetReceivedByAccountMinConf(account string, minConfirms int) (btcutil.Amount, error) {
|
||||
return c.GetReceivedByAccountMinConfAsync(account, minConfirms).Receive()
|
||||
}
|
||||
|
||||
|
@ -2035,44 +2122,6 @@ func (c *Client) ListReceivedByAddressIncludeEmpty(minConfirms int, includeEmpty
|
|||
includeEmpty).Receive()
|
||||
}
|
||||
|
||||
// FutureRescanBlockchainResult is a future promise to deliver the error result of a
|
||||
// RescanBlockchainAsync RPC invocation.
|
||||
type FutureRescanBlockchainResult chan *Response
|
||||
|
||||
// Receive waits for the Response promised by the future and returns the result
|
||||
// of locking or unlocking the unspent output(s).
|
||||
func (r FutureRescanBlockchainResult) Receive() (*btcjson.RescanBlockchainResult, error) {
|
||||
res, err := ReceiveFuture(r)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Unmarshal as an array of listreceivedbyaddress result objects.
|
||||
var received btcjson.RescanBlockchainResult
|
||||
err = json.Unmarshal(res, &received)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &received, nil
|
||||
}
|
||||
|
||||
// RescanBlockchainAsync returns an instance of a type that can be used to get the
|
||||
// result of the RPC at some future time by invoking the Receive function on the
|
||||
// returned instance.
|
||||
//
|
||||
// See RescanBlockchain for the blocking version and more details.
|
||||
func (c *Client) RescanBlockchainAsync(startHeight *int32, stopHeight *int32) FutureRescanBlockchainResult {
|
||||
cmd := btcjson.NewRescanBlockchainCmd(startHeight, stopHeight)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
// RescanBlockchain rescans the local blockchain for wallet related
|
||||
// transactions from the startHeight to the the inclusive stopHeight.
|
||||
func (c *Client) RescanBlockchain(startHeight *int32, stopHeight *int32) (*btcjson.RescanBlockchainResult, error) {
|
||||
return c.RescanBlockchainAsync(startHeight, stopHeight).Receive()
|
||||
}
|
||||
|
||||
// ************************
|
||||
// Wallet Locking Functions
|
||||
// ************************
|
||||
|
|
666
rpcserver.go
666
rpcserver.go
|
@ -21,7 +21,6 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -135,7 +134,6 @@ type commandHandler func(*rpcServer, interface{}, <-chan struct{}) (interface{},
|
|||
var rpcHandlers map[string]commandHandler
|
||||
var rpcHandlersBeforeInit = map[string]commandHandler{
|
||||
"addnode": handleAddNode,
|
||||
"clearbanned": handleClearBanned,
|
||||
"createrawtransaction": handleCreateRawTransaction,
|
||||
"debuglevel": handleDebugLevel,
|
||||
"decoderawtransaction": handleDecodeRawTransaction,
|
||||
|
@ -152,11 +150,10 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
|
|||
"getblockcount": handleGetBlockCount,
|
||||
"getblockhash": handleGetBlockHash,
|
||||
"getblockheader": handleGetBlockHeader,
|
||||
"getblockstats": handleGetBlockStats,
|
||||
"getchaintips": handleGetChainTips,
|
||||
"getblocktemplate": handleGetBlockTemplate,
|
||||
"getcfilter": handleGetCFilter,
|
||||
"getcfilterheader": handleGetCFilterHeader,
|
||||
"getchaintips": handleGetChainTips,
|
||||
"getconnectioncount": handleGetConnectionCount,
|
||||
"getcurrentnet": handleGetCurrentNet,
|
||||
"getdifficulty": handleGetDifficulty,
|
||||
|
@ -164,8 +161,8 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
|
|||
"gethashespersec": handleGetHashesPerSec,
|
||||
"getheaders": handleGetHeaders,
|
||||
"getinfo": handleGetInfo,
|
||||
"getmempoolentry": handleGetMempoolEntry,
|
||||
"getmempoolinfo": handleGetMempoolInfo,
|
||||
"getmempoolentry": handleGetMempoolEntry,
|
||||
"getmininginfo": handleGetMiningInfo,
|
||||
"getnettotals": handleGetNetTotals,
|
||||
"getnetworkhashps": handleGetNetworkHashPS,
|
||||
|
@ -177,13 +174,11 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
|
|||
"gettxout": handleGetTxOut,
|
||||
"help": handleHelp,
|
||||
"invalidateblock": handleInvalidateBlock,
|
||||
"listbanned": handleListBanned,
|
||||
"node": handleNode,
|
||||
"ping": handlePing,
|
||||
"reconsiderblock": handleReconsiderBlock,
|
||||
"searchrawtransactions": handleSearchRawTransactions,
|
||||
"sendrawtransaction": handleSendRawTransaction,
|
||||
"setban": handleSetBan,
|
||||
"setgenerate": handleSetGenerate,
|
||||
"signmessagewithprivkey": handleSignMessageWithPrivKey,
|
||||
"stop": handleStop,
|
||||
|
@ -230,10 +225,11 @@ var rpcAskWallet = map[string]struct{}{
|
|||
"listtransactions": {},
|
||||
"listunspent": {},
|
||||
"lockunspent": {},
|
||||
"rescanblockchain": {},
|
||||
"move": {},
|
||||
"sendfrom": {},
|
||||
"sendmany": {},
|
||||
"sendtoaddress": {},
|
||||
"setaccount": {},
|
||||
"settxfee": {},
|
||||
"signmessage": {},
|
||||
"signrawtransaction": {},
|
||||
|
@ -329,15 +325,6 @@ func rpcDecodeHexError(gotHex string) *btcjson.RPCError {
|
|||
gotHex))
|
||||
}
|
||||
|
||||
// rpcInvalidAddressOrKey is a convenience function for returning a nicely
|
||||
// formatted RPC error which indicates the address or key is invalid.
|
||||
func rpcInvalidAddressOrKeyError(addr string, msg string) *btcjson.RPCError {
|
||||
return &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidAddressOrKey,
|
||||
Message: msg,
|
||||
}
|
||||
}
|
||||
|
||||
// rpcNoTxInfoError is a convenience function for returning a nicely formatted
|
||||
// RPC error which indicates there is no information available for the provided
|
||||
// transaction hash.
|
||||
|
@ -413,21 +400,6 @@ func handleAddNode(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (in
|
|||
return nil, nil
|
||||
}
|
||||
|
||||
// handleClearBanned handles clearbanned commands.
|
||||
func handleClearBanned(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
|
||||
err := s.cfg.ConnMgr.ClearBanned()
|
||||
if err != nil {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidParameter,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
// no data returned unless an error.
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// handleNode handles node commands.
|
||||
func handleNode(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
c := cmd.(*btcjson.NodeCmd)
|
||||
|
@ -579,92 +551,59 @@ func handleCreateRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan
|
|||
// Add all transaction outputs to the transaction after performing
|
||||
// some validity checks.
|
||||
params := s.cfg.ChainParams
|
||||
|
||||
// Ensure amount is in the valid range for monetary amounts.
|
||||
// Decode the provided address.
|
||||
// Ensure the address is one of the supported types and that
|
||||
// the network encoded with the address matches the network the
|
||||
// server is currently on.
|
||||
// Create a new script which pays to the provided address.
|
||||
// Convert the amount to satoshi.
|
||||
handleAmountFn := func(amount float64, encodedAddr string) (*wire.TxOut,
|
||||
error) {
|
||||
|
||||
if amount <= 0 ||
|
||||
amount*btcutil.SatoshiPerBitcoin > btcutil.MaxSatoshi {
|
||||
for encodedAddr, amount := range c.Amounts {
|
||||
// Ensure amount is in the valid range for monetary amounts.
|
||||
if amount <= 0 || amount*btcutil.SatoshiPerBitcoin > btcutil.MaxSatoshi {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCType,
|
||||
Message: "invalid amount",
|
||||
Message: "Invalid amount",
|
||||
}
|
||||
}
|
||||
|
||||
// Decode the provided address.
|
||||
addr, err := btcutil.DecodeAddress(encodedAddr, params)
|
||||
if err != nil {
|
||||
return nil, rpcInvalidAddressOrKeyError(encodedAddr,
|
||||
"invalid address or key")
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidAddressOrKey,
|
||||
Message: "Invalid address or key: " + err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the address is one of the supported types and that
|
||||
// the network encoded with the address matches the network the
|
||||
// server is currently on.
|
||||
switch addr.(type) {
|
||||
case *btcutil.AddressPubKeyHash:
|
||||
case *btcutil.AddressScriptHash:
|
||||
default:
|
||||
return nil, rpcInvalidAddressOrKeyError(addr.String(),
|
||||
"invalid address or key")
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidAddressOrKey,
|
||||
Message: "Invalid address or key: " + addr.String(),
|
||||
}
|
||||
}
|
||||
if !addr.IsForNet(params) {
|
||||
return nil, rpcInvalidAddressOrKeyError(addr.String(),
|
||||
"wrong network")
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidAddressOrKey,
|
||||
Message: "Invalid address: " + encodedAddr +
|
||||
" is for the wrong network",
|
||||
}
|
||||
}
|
||||
|
||||
// Create a new script which pays to the provided address.
|
||||
pkScript, err := txscript.PayToAddrScript(addr)
|
||||
if err != nil {
|
||||
context := "failed to generate pay-to-address script"
|
||||
context := "Failed to generate pay-to-address script"
|
||||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
|
||||
// Convert the amount to satoshi.
|
||||
satoshi, err := btcutil.NewAmount(amount)
|
||||
if err != nil {
|
||||
context := "failed to convert amount"
|
||||
context := "Failed to convert amount"
|
||||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
|
||||
return wire.NewTxOut(int64(satoshi), pkScript), nil
|
||||
}
|
||||
|
||||
handleDataFn := func(key string, value string) (*wire.TxOut, error) {
|
||||
if key != "data" {
|
||||
context := "output key must be an address or \"data\""
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidParameter,
|
||||
Message: context,
|
||||
}
|
||||
}
|
||||
var data []byte
|
||||
data, err := hex.DecodeString(value)
|
||||
if err != nil {
|
||||
return nil, rpcDecodeHexError(value)
|
||||
}
|
||||
return wire.NewTxOut(0, data), nil
|
||||
}
|
||||
|
||||
for key, value := range c.Outputs {
|
||||
var err error
|
||||
var txOut *wire.TxOut
|
||||
switch value := value.(type) {
|
||||
case float64:
|
||||
txOut, err = handleAmountFn(value, key)
|
||||
case string:
|
||||
txOut, err = handleDataFn(key, value)
|
||||
default:
|
||||
context := "output value must be a string or float"
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCType,
|
||||
Message: context,
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
txOut := wire.NewTxOut(int64(satoshi), pkScript)
|
||||
mtx.AddTxOut(txOut)
|
||||
}
|
||||
|
||||
|
@ -1255,7 +1194,7 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
|
|||
if err != nil {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCBlockNotFound,
|
||||
Message: "Block not found: " + err.Error(),
|
||||
Message: "Block not found",
|
||||
}
|
||||
}
|
||||
// If verbosity is 0, return the serialized block as a hex encoded string.
|
||||
|
@ -1272,23 +1211,31 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
|
|||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
|
||||
params := s.cfg.ChainParams
|
||||
blockHeader := &blk.MsgBlock().Header
|
||||
|
||||
// Get further details (height, confirmations, nexthash, mediantime, etc.) from chain.
|
||||
attrs, best, err := s.cfg.Chain.BlockAttributesByHash(hash, &blockHeader.PrevBlock)
|
||||
// Get the block height from chain.
|
||||
blockHeight, err := s.cfg.Chain.BlockHeightByHash(hash)
|
||||
if err != nil {
|
||||
context := "Failed to obtain block details"
|
||||
context := "Failed to obtain block height"
|
||||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
blk.SetHeight(blockHeight)
|
||||
best := s.cfg.Chain.BestSnapshot()
|
||||
|
||||
var prevHashString string
|
||||
if attrs.PrevHash != nil {
|
||||
prevHashString = attrs.PrevHash.String()
|
||||
}
|
||||
// Get next block hash unless there are none.
|
||||
var nextHashString string
|
||||
if attrs.NextHash != nil {
|
||||
nextHashString = attrs.NextHash.String()
|
||||
if blockHeight < best.Height {
|
||||
nextHash, err := s.cfg.Chain.BlockHashByHeight(blockHeight + 1)
|
||||
if err != nil {
|
||||
context := "No next block"
|
||||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
nextHashString = nextHash.String()
|
||||
}
|
||||
|
||||
params := s.cfg.ChainParams
|
||||
blockHeader := &blk.MsgBlock().Header
|
||||
var prevHashString string
|
||||
if blockHeight > 0 {
|
||||
prevHashString = blockHeader.PrevBlock.String()
|
||||
}
|
||||
|
||||
base := btcjson.GetBlockVerboseResultBase{
|
||||
|
@ -1299,15 +1246,13 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
|
|||
PreviousHash: prevHashString,
|
||||
Nonce: blockHeader.Nonce,
|
||||
Time: blockHeader.Timestamp.Unix(),
|
||||
MedianTime: attrs.MedianTime.Unix(),
|
||||
Confirmations: int64(attrs.Confirmations),
|
||||
Height: int64(attrs.Height),
|
||||
Confirmations: int64(1 + best.Height - blockHeight),
|
||||
Height: int64(blockHeight),
|
||||
Size: int32(len(blkBytes)),
|
||||
StrippedSize: int32(blk.MsgBlock().SerializeSizeStripped()),
|
||||
Weight: int32(blockchain.GetBlockWeight(blk)),
|
||||
Bits: strconv.FormatInt(int64(blockHeader.Bits), 16),
|
||||
Difficulty: getDifficultyRatio(blockHeader.Bits, params),
|
||||
ChainWork: attrs.ChainWork.Text(16),
|
||||
NextHash: nextHashString,
|
||||
ClaimTrie: blockHeader.ClaimTrie.String(),
|
||||
}
|
||||
|
@ -1332,7 +1277,7 @@ func handleGetBlock(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (i
|
|||
for i, tx := range txns {
|
||||
rawTxn, err := createTxRawResult(params, tx.MsgTx(),
|
||||
tx.Hash().String(), blockHeader, hash.String(),
|
||||
attrs.Height, best.Height)
|
||||
blockHeight, best.Height)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -1580,405 +1525,6 @@ func handleGetChainTips(s *rpcServer, cmd interface{}, closeChan <-chan struct{}
|
|||
return results, nil
|
||||
}
|
||||
|
||||
// handleGetBlockStats implements the getblockstats command.
|
||||
func handleGetBlockStats(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
c := cmd.(*btcjson.GetBlockStatsCmd)
|
||||
|
||||
// Check whether a block height or hash was provided.
|
||||
blockHeight, ok := c.HashOrHeight.Value.(int)
|
||||
var hash *chainhash.Hash
|
||||
var err error
|
||||
if ok {
|
||||
// Block height was provided.
|
||||
hash, err = s.cfg.Chain.BlockHashByHeight(int32(blockHeight))
|
||||
if err != nil {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCOutOfRange,
|
||||
Message: "Block number out of range",
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Block hash was provided.
|
||||
hashString := c.HashOrHeight.Value.(string)
|
||||
hash, err = chainhash.NewHashFromStr(hashString)
|
||||
if err != nil {
|
||||
return nil, rpcDecodeHexError(hashString)
|
||||
}
|
||||
|
||||
// Get the block height from chain.
|
||||
blockHeightByHash, err := s.cfg.Chain.BlockHeightByHash(hash)
|
||||
if err != nil {
|
||||
context := "Failed to obtain block height"
|
||||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
blockHeight = int(blockHeightByHash)
|
||||
}
|
||||
|
||||
// Load block bytes from the database.
|
||||
var blkBytes []byte
|
||||
err = s.cfg.DB.View(func(dbTx database.Tx) error {
|
||||
var err error
|
||||
blkBytes, err = dbTx.FetchBlock(hash)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCBlockNotFound,
|
||||
Message: "Block not found",
|
||||
}
|
||||
}
|
||||
|
||||
// Deserialize the block.
|
||||
blk, err := btcutil.NewBlockFromBytes(blkBytes)
|
||||
if err != nil {
|
||||
context := "Failed to deserialize block"
|
||||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
|
||||
var selectedStats []string
|
||||
if c.Stats != nil {
|
||||
selectedStats = *c.Stats
|
||||
}
|
||||
|
||||
// Create a set of selected stats to facilitate queries.
|
||||
statsSet := make(map[string]bool)
|
||||
for _, value := range selectedStats {
|
||||
statsSet[value] = true
|
||||
}
|
||||
|
||||
// Return all stats if an empty array was provided.
|
||||
allStats := len(selectedStats) == 0
|
||||
calcFees := statsSet["avgfee"] || statsSet["avgfeerate"] || statsSet["maxfee"] || statsSet["maxfeerate"] ||
|
||||
statsSet["medianfee"] || statsSet["totalfee"] || statsSet["feerate_percentiles"]
|
||||
|
||||
if calcFees && s.cfg.TxIndex == nil {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCNoTxInfo,
|
||||
Message: "The transaction index must be " +
|
||||
"enabled to obtain fee statistics " +
|
||||
"(specify --txindex)",
|
||||
}
|
||||
}
|
||||
|
||||
txs := blk.Transactions()
|
||||
txCount := len(txs)
|
||||
var inputCount, outputCount int
|
||||
var totalOutputValue int64
|
||||
|
||||
// Create a map of transaction statistics.
|
||||
txStats := make([]map[string]interface{}, txCount)
|
||||
for i, tx := range txs {
|
||||
size := tx.MsgTx().SerializeSize()
|
||||
witnessSize := size - tx.MsgTx().SerializeSizeStripped()
|
||||
weight := int64(tx.MsgTx().SerializeSizeStripped()*4 + witnessSize)
|
||||
|
||||
var fee, feeRate int64
|
||||
if (calcFees || allStats) && s.cfg.TxIndex != nil && !blockchain.IsCoinBaseTx(tx.MsgTx()) {
|
||||
fee, err = calculateFee(tx, s.cfg.TxIndex, s.cfg.DB)
|
||||
if err != nil {
|
||||
context := "Failed to calculate fees"
|
||||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
if weight != 0 {
|
||||
feeRate = fee * 4 / weight
|
||||
}
|
||||
}
|
||||
segwit := tx.HasWitness()
|
||||
txStats[i] = map[string]interface{}{"tx": tx, "fee": fee, "size": int64(size),
|
||||
"feeRate": feeRate, "weight": weight, "segwit": segwit}
|
||||
inputCount += len(tx.MsgTx().TxIn)
|
||||
outputCount += len(tx.MsgTx().TxOut)
|
||||
|
||||
// Coinbase is excluded from the total output.
|
||||
if !blockchain.IsCoinBase(tx) {
|
||||
for _, txOut := range tx.MsgTx().TxOut {
|
||||
totalOutputValue += txOut.Value
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var totalFees, minFee, maxFee, minFeeRate, maxFeeRate, segwitCount,
|
||||
segwitWeight, totalWeight, totalSize, minSize, maxSize, segwitSize int64
|
||||
if txCount > 1 {
|
||||
minFee = txStats[1]["fee"].(int64)
|
||||
minFeeRate = txStats[1]["feeRate"].(int64)
|
||||
}
|
||||
for i := 0; i < len(txStats); i++ {
|
||||
var fee, feeRate int64
|
||||
tx := txStats[i]["tx"].(*btcutil.Tx)
|
||||
if !blockchain.IsCoinBaseTx(tx.MsgTx()) {
|
||||
// Fee statistics.
|
||||
fee = txStats[i]["fee"].(int64)
|
||||
feeRate = txStats[i]["feeRate"].(int64)
|
||||
if minFee > fee {
|
||||
minFee = fee
|
||||
}
|
||||
if maxFee < fee {
|
||||
maxFee = fee
|
||||
}
|
||||
if minFeeRate > feeRate {
|
||||
minFeeRate = feeRate
|
||||
}
|
||||
if maxFeeRate < feeRate {
|
||||
maxFeeRate = feeRate
|
||||
}
|
||||
totalFees += txStats[i]["fee"].(int64)
|
||||
|
||||
// Segwit statistics.
|
||||
if txStats[i]["segwit"].(bool) {
|
||||
segwitCount++
|
||||
segwitSize += txStats[i]["size"].(int64)
|
||||
segwitWeight += txStats[i]["weight"].(int64)
|
||||
}
|
||||
|
||||
// Size statistics.
|
||||
size := txStats[i]["size"].(int64)
|
||||
if minSize == 0 {
|
||||
minSize = size
|
||||
}
|
||||
if maxSize < size {
|
||||
maxSize = size
|
||||
} else if minSize > size {
|
||||
minSize = size
|
||||
}
|
||||
totalSize += txStats[i]["size"].(int64)
|
||||
|
||||
totalWeight += txStats[i]["weight"].(int64)
|
||||
}
|
||||
}
|
||||
|
||||
var avgFee, avgFeeRate, avgSize int64
|
||||
if txCount > 1 {
|
||||
avgFee = totalFees / int64(txCount-1)
|
||||
}
|
||||
if totalWeight != 0 {
|
||||
avgFeeRate = totalFees * 4 / totalWeight
|
||||
}
|
||||
if txCount > 1 {
|
||||
avgSize = totalSize / int64(txCount-1)
|
||||
}
|
||||
|
||||
subsidy := blockchain.CalcBlockSubsidy(int32(blockHeight), s.cfg.ChainParams)
|
||||
|
||||
medianStat := func(stat string) int64 {
|
||||
size := len(txStats) - 1
|
||||
if size == 0 {
|
||||
return 0
|
||||
}
|
||||
statArray := make([]int64, size)
|
||||
// Start with the second element to ignore entry associated with coinbase.
|
||||
for i, stats := range txStats[1:] {
|
||||
statArray[i] = stats[stat].(int64)
|
||||
}
|
||||
sort.Slice(statArray, func(i, j int) bool {
|
||||
return statArray[i] < statArray[j]
|
||||
})
|
||||
if size%2 == 0 {
|
||||
return (statArray[size/2-1] + statArray[size/2]) / 2
|
||||
}
|
||||
return statArray[size/2]
|
||||
}
|
||||
|
||||
var medianFee int64
|
||||
if totalFees > 0 {
|
||||
medianFee = medianStat("fee")
|
||||
} else {
|
||||
medianFee = 0
|
||||
}
|
||||
medianSize := medianStat("size")
|
||||
|
||||
// Calculate feerate percentiles.
|
||||
var feeratePercentiles []int64
|
||||
if allStats || calcFees {
|
||||
|
||||
// Sort by feerate.
|
||||
sort.Slice(txStats, func(i, j int) bool {
|
||||
return txStats[i]["feeRate"].(int64) < txStats[j]["feeRate"].(int64)
|
||||
})
|
||||
totalWeight := float64(totalWeight)
|
||||
|
||||
// Find 10th, 25th, 50th, 75th and 90th percentile weight units.
|
||||
weights := []float64{
|
||||
totalWeight / 10, totalWeight / 4, totalWeight / 2,
|
||||
(totalWeight * 3) / 4, (totalWeight * 9) / 10}
|
||||
var cumulativeWeight int64
|
||||
feeratePercentiles = make([]int64, len(weights))
|
||||
nextPercentileIndex := 0
|
||||
for i := 0; i < len(txStats); i++ {
|
||||
cumulativeWeight += txStats[i]["weight"].(int64)
|
||||
for nextPercentileIndex < len(weights) && float64(cumulativeWeight) >= weights[nextPercentileIndex] {
|
||||
feeratePercentiles[nextPercentileIndex] = txStats[i]["feeRate"].(int64)
|
||||
nextPercentileIndex++
|
||||
}
|
||||
}
|
||||
|
||||
// Fill any remaining percentiles with the last value.
|
||||
for i := nextPercentileIndex; i < len(weights); i++ {
|
||||
feeratePercentiles[i] = txStats[len(txStats)-1]["feeRate"].(int64)
|
||||
}
|
||||
}
|
||||
|
||||
var blockHash string
|
||||
if allStats || statsSet["blockhash"] {
|
||||
blockHash = blk.Hash().String()
|
||||
}
|
||||
|
||||
medianTime, err := medianBlockTime(blk.Hash(), s.cfg.Chain)
|
||||
if err != nil {
|
||||
context := "Failed to obtain block median time"
|
||||
return nil, internalRPCError(err.Error(), context)
|
||||
}
|
||||
|
||||
resultMap := map[string]int64{
|
||||
"avgfee": avgFee,
|
||||
"avgfeerate": avgFeeRate,
|
||||
"avgtxsize": avgSize,
|
||||
"height": int64(blockHeight),
|
||||
"ins": int64(inputCount - 1), // Coinbase input is not included.
|
||||
"maxfee": maxFee,
|
||||
"maxfeerate": maxFeeRate,
|
||||
"maxtxsize": maxSize,
|
||||
"medianfee": medianFee,
|
||||
"mediantime": medianTime.Unix(),
|
||||
"mediantxsize": medianSize,
|
||||
"minfee": minFee,
|
||||
"minfeerate": minFeeRate,
|
||||
"mintxsize": minSize,
|
||||
"outs": int64(outputCount),
|
||||
"swtotal_size": segwitSize,
|
||||
"swtotal_weight": segwitWeight,
|
||||
"swtxs": segwitCount,
|
||||
"subsidy": subsidy,
|
||||
"time": blk.MsgBlock().Header.Timestamp.Unix(),
|
||||
"total_out": totalOutputValue,
|
||||
"total_size": totalSize,
|
||||
"total_weight": totalWeight,
|
||||
"totalfee": totalFees,
|
||||
"txs": int64(len(txs)),
|
||||
"utxo_increase": int64(outputCount - (inputCount - 1)),
|
||||
}
|
||||
|
||||
// This function determines whether a statistic goes into the
|
||||
// final result, except for blockhash and feerate_percentiles
|
||||
// which are handled separately.
|
||||
resultFilter := func(stat string) *int64 {
|
||||
if allStats && s.cfg.TxIndex == nil {
|
||||
// There are no fee statistics to send.
|
||||
excludedStats := []string{"avgfee", "avgfeerate", "maxfee", "maxfeerate", "medianfee", "minfee", "minfeerate"}
|
||||
for _, excluded := range excludedStats {
|
||||
if stat == excluded {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
}
|
||||
if allStats || statsSet[stat] {
|
||||
if value, ok := resultMap[stat]; ok {
|
||||
return &value
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
result := &btcjson.GetBlockStatsResult{
|
||||
AverageFee: resultFilter("avgfee"),
|
||||
AverageFeeRate: resultFilter("avgfeerate"),
|
||||
AverageTxSize: resultFilter("avgtxsize"),
|
||||
FeeratePercentiles: &feeratePercentiles,
|
||||
Hash: &blockHash,
|
||||
Height: resultFilter("height"),
|
||||
Ins: resultFilter("ins"),
|
||||
MaxFee: resultFilter("maxfee"),
|
||||
MaxFeeRate: resultFilter("maxfeerate"),
|
||||
MaxTxSize: resultFilter("maxtxsize"),
|
||||
MedianFee: resultFilter("medianfee"),
|
||||
MedianTime: resultFilter("mediantime"),
|
||||
MedianTxSize: resultFilter("mediantxsize"),
|
||||
MinFee: resultFilter("minfee"),
|
||||
MinFeeRate: resultFilter("minfeerate"),
|
||||
MinTxSize: resultFilter("mintxsize"),
|
||||
Outs: resultFilter("outs"),
|
||||
SegWitTotalSize: resultFilter("swtotal_size"),
|
||||
SegWitTotalWeight: resultFilter("swtotal_weight"),
|
||||
SegWitTxs: resultFilter("swtxs"),
|
||||
Subsidy: resultFilter("subsidy"),
|
||||
Time: resultFilter("time"),
|
||||
TotalOut: resultFilter("total_out"),
|
||||
TotalSize: resultFilter("total_size"),
|
||||
TotalWeight: resultFilter("total_weight"),
|
||||
TotalFee: resultFilter("totalfee"),
|
||||
Txs: resultFilter("txs"),
|
||||
UTXOIncrease: resultFilter("utxo_increase"),
|
||||
UTXOSizeIncrease: resultFilter("utxo_size_inc"),
|
||||
}
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// calculateFee returns the fee of a transaction.
|
||||
func calculateFee(tx *btcutil.Tx, txIndex *indexers.TxIndex, db database.DB) (int64, error) {
|
||||
var inValue, outValue int64
|
||||
for _, input := range tx.MsgTx().TxIn {
|
||||
prevTxHash := input.PreviousOutPoint.Hash
|
||||
// Look up the location of the previous transaction in the index.
|
||||
blockRegion, err := txIndex.TxBlockRegion(&prevTxHash)
|
||||
if err != nil {
|
||||
context := "Failed to retrieve transaction location"
|
||||
return 0, internalRPCError(err.Error(), context)
|
||||
}
|
||||
if blockRegion == nil {
|
||||
return 0, rpcNoTxInfoError(&prevTxHash)
|
||||
}
|
||||
|
||||
// Load the raw transaction bytes from the database.
|
||||
var txBytes []byte
|
||||
err = db.View(func(dbTx database.Tx) error {
|
||||
var err error
|
||||
txBytes, err = dbTx.FetchBlockRegion(blockRegion)
|
||||
return err
|
||||
})
|
||||
if err != nil {
|
||||
return 0, rpcNoTxInfoError(&prevTxHash)
|
||||
}
|
||||
|
||||
var msgTx wire.MsgTx
|
||||
err = msgTx.Deserialize(bytes.NewReader(txBytes))
|
||||
if err != nil {
|
||||
context := "Failed to deserialize transaction"
|
||||
return 0, internalRPCError(err.Error(), context)
|
||||
}
|
||||
prevOutValue := msgTx.TxOut[input.PreviousOutPoint.Index].Value
|
||||
inValue += prevOutValue
|
||||
}
|
||||
for _, output := range tx.MsgTx().TxOut {
|
||||
outValue += output.Value
|
||||
}
|
||||
fee := inValue - outValue
|
||||
return fee, nil
|
||||
}
|
||||
|
||||
// medianBlockTime returns the median time of a block and its 10 previous blocks
|
||||
// as per BIP113.
|
||||
func medianBlockTime(blockHash *chainhash.Hash, chain *blockchain.BlockChain) (*time.Time, error) {
|
||||
blockTimes := make([]time.Time, 0)
|
||||
currentHash := blockHash
|
||||
for i := 0; i < 11; i++ {
|
||||
header, err := chain.HeaderByHash(currentHash)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
blockTimes = append(blockTimes, header.Timestamp)
|
||||
genesisPrevBlock, _ := chainhash.NewHashFromStr("0000000000000000000000000000000000000000000000000000000000000000")
|
||||
if header.PrevBlock.IsEqual(genesisPrevBlock) {
|
||||
// This is the genesis block so there's no need to iterate further.
|
||||
break
|
||||
}
|
||||
currentHash = &header.PrevBlock
|
||||
}
|
||||
sort.Slice(blockTimes, func(i, j int) bool {
|
||||
return blockTimes[i].Before(blockTimes[j])
|
||||
})
|
||||
return &blockTimes[len(blockTimes)/2], nil
|
||||
}
|
||||
|
||||
// encodeTemplateID encodes the passed details into an ID that can be used to
|
||||
// uniquely identify a block template.
|
||||
func encodeTemplateID(prevHash *chainhash.Hash, lastGenerated time.Time) string {
|
||||
|
@ -2362,12 +1908,11 @@ func (state *gbtWorkState) blockTemplateResult(useCoinbaseValue bool, submitOld
|
|||
if template.WitnessCommitment != nil {
|
||||
reply.DefaultWitnessCommitment = hex.EncodeToString(template.WitnessCommitment)
|
||||
reply.Rules = append(reply.Rules, "!segwit")
|
||||
} else {
|
||||
reply.Rules = append(reply.Rules, "segwit")
|
||||
}
|
||||
|
||||
if useCoinbaseValue {
|
||||
reply.CoinbaseAux = gbtCoinbaseAux
|
||||
reply.CoinbaseValue = &msgBlock.Transactions[0].TxOut[0].Value
|
||||
} else {
|
||||
// Ensure the template has a valid payment address associated
|
||||
// with it when a full coinbase is requested.
|
||||
|
@ -2400,9 +1945,6 @@ func (state *gbtWorkState) blockTemplateResult(useCoinbaseValue bool, submitOld
|
|||
reply.CoinbaseTxn = &resultTx
|
||||
}
|
||||
|
||||
// Return coinbasevalue anyway as lbrycrd and bitcoind do.
|
||||
reply.CoinbaseValue = &msgBlock.Transactions[0].TxOut[0].Value
|
||||
|
||||
return &reply, nil
|
||||
}
|
||||
|
||||
|
@ -2926,7 +2468,19 @@ func handleGetInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (in
|
|||
|
||||
// handleGetMempoolInfo implements the getmempoolinfo command.
|
||||
func handleGetMempoolInfo(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
return s.cfg.TxMemPool.MempoolInfo(), nil
|
||||
mempoolTxns := s.cfg.TxMemPool.TxDescs()
|
||||
|
||||
var numBytes int64
|
||||
for _, txD := range mempoolTxns {
|
||||
numBytes += int64(txD.Tx.MsgTx().SerializeSize())
|
||||
}
|
||||
|
||||
ret := &btcjson.GetMempoolInfoResult{
|
||||
Size: int64(len(mempoolTxns)),
|
||||
Bytes: numBytes,
|
||||
}
|
||||
|
||||
return ret, nil
|
||||
}
|
||||
|
||||
// handleGetMempoolEntry implements the getmempoolentry command.
|
||||
|
@ -3610,24 +3164,6 @@ func handleHelp(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (inter
|
|||
return help, nil
|
||||
}
|
||||
|
||||
// handleListBanned handles the listbanned command.
|
||||
func handleListBanned(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
banned := s.cfg.ConnMgr.BannedPeers()
|
||||
reply := make([]*btcjson.ListBannedResult, 0, len(banned))
|
||||
for address, period := range banned {
|
||||
since, until := period.since, period.until
|
||||
r := btcjson.ListBannedResult{
|
||||
Address: address,
|
||||
BanCreated: since.Unix(),
|
||||
BannedUntil: until.Unix(),
|
||||
BanDuration: int64(until.Sub(since).Seconds()),
|
||||
TimeRemaining: int64(time.Until(until).Seconds()),
|
||||
}
|
||||
reply = append(reply, &r)
|
||||
}
|
||||
return reply, nil
|
||||
}
|
||||
|
||||
// handlePing implements the ping command.
|
||||
func handlePing(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
// Ask server to ping \o_
|
||||
|
@ -4224,66 +3760,12 @@ func handleSendRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan st
|
|||
// Keep track of all the sendrawtransaction request txns so that they
|
||||
// can be rebroadcast if they don't make their way into a block.
|
||||
txD := acceptedTxs[0]
|
||||
s.cfg.TxMemPool.AddUnbroadcastTx(txD.Tx.Hash())
|
||||
iv := wire.NewInvVect(wire.InvTypeTx, txD.Tx.Hash())
|
||||
s.cfg.ConnMgr.AddRebroadcastInventory(iv, txD)
|
||||
|
||||
return tx.Hash().String(), nil
|
||||
}
|
||||
|
||||
// handleSetBan handles the setban command.
|
||||
func handleSetBan(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
c := cmd.(*btcjson.SetBanCmd)
|
||||
|
||||
addr := net.ParseIP(c.Addr)
|
||||
if addr == nil {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidParameter,
|
||||
Message: "invalid addr for setban",
|
||||
}
|
||||
}
|
||||
|
||||
since := time.Now()
|
||||
until := since.Add(time.Second * time.Duration(*c.BanTime))
|
||||
if *c.BanTime == 0 {
|
||||
until = since.Add(defaultBanDuration)
|
||||
}
|
||||
if *c.Absolute {
|
||||
until = time.Unix(int64(*c.BanTime), 0)
|
||||
}
|
||||
|
||||
var err error
|
||||
switch c.SubCmd {
|
||||
case "add":
|
||||
err = s.cfg.ConnMgr.SetBan(addr.String(), since, until)
|
||||
addr := addr.String()
|
||||
peers := s.cfg.ConnMgr.ConnectedPeers()
|
||||
for _, peer := range peers {
|
||||
p := peer.ToPeer()
|
||||
if p.NA().IP.String() == addr {
|
||||
p.Disconnect()
|
||||
}
|
||||
}
|
||||
case "remove":
|
||||
err = s.cfg.ConnMgr.RemoveBan(addr.String())
|
||||
default:
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidParameter,
|
||||
Message: "invalid subcommand for setban",
|
||||
}
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidParameter,
|
||||
Message: err.Error(),
|
||||
}
|
||||
}
|
||||
|
||||
// no data returned unless an error.
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// handleSetGenerate implements the setgenerate command.
|
||||
func handleSetGenerate(s *rpcServer, cmd interface{}, closeChan <-chan struct{}) (interface{}, error) {
|
||||
c := cmd.(*btcjson.SetGenerateCmd)
|
||||
|
@ -5341,18 +4823,6 @@ type rpcserverConnManager interface {
|
|||
// ConnectedPeers returns an array consisting of all connected peers.
|
||||
ConnectedPeers() []rpcserverPeer
|
||||
|
||||
// BannedPeers returns an array consisting of all Banned peers.
|
||||
BannedPeers() map[string]bannedPeriod
|
||||
|
||||
// SetBan add the addr to the ban list.
|
||||
SetBan(addr string, since time.Time, until time.Time) error
|
||||
|
||||
// RemoveBan remove the subnet from the ban list.
|
||||
RemoveBan(subnet string) error
|
||||
|
||||
// ClearBanned removes all banned IPs.
|
||||
ClearBanned() error
|
||||
|
||||
// PersistentPeers returns an array consisting of all the persistent
|
||||
// peers.
|
||||
PersistentPeers() []rpcserverPeer
|
||||
|
|
|
@ -49,16 +49,13 @@ var helpDescsEnUS = map[string]string{
|
|||
"The transaction inputs are not signed in the created transaction.\n" +
|
||||
"The signrawtransaction RPC command provided by wallet must be used to sign the resulting transaction.",
|
||||
"createrawtransaction-inputs": "The inputs to the transaction",
|
||||
"createrawtransaction-outputs": "JSON object with the destination addresses as keys and amounts as values",
|
||||
"createrawtransaction-outputs--key": "address or \"data\"",
|
||||
"createrawtransaction-outputs--value": "value in BTC as floating point number or hex-encoded data for \"data\"",
|
||||
"createrawtransaction-outputs--desc": "The destination address as the key and the amount in LBC as the value",
|
||||
"createrawtransaction-amounts": "JSON object with the destination addresses as keys and amounts as values",
|
||||
"createrawtransaction-amounts--key": "address",
|
||||
"createrawtransaction-amounts--value": "n.nnn",
|
||||
"createrawtransaction-amounts--desc": "The destination address as the key and the amount in LBC as the value",
|
||||
"createrawtransaction-locktime": "Locktime value; a non-zero value will also locktime-activate the inputs",
|
||||
"createrawtransaction--result0": "Hex-encoded bytes of the serialized transaction",
|
||||
|
||||
// ClearBannedCmd help.
|
||||
"clearbanned--synopsis": "Clear all banned IPs.",
|
||||
|
||||
// ScriptSig help.
|
||||
"scriptsig-asm": "Disassembly of the script",
|
||||
"scriptsig-hex": "Hex-encoded bytes of the script",
|
||||
|
@ -200,43 +197,6 @@ var helpDescsEnUS = map[string]string{
|
|||
"getblockchaininforesult-softforks": "The status of the super-majority soft-forks",
|
||||
"getblockchaininforesult-unifiedsoftforks": "The status of the super-majority soft-forks used by bitcoind on or after v0.19.0",
|
||||
|
||||
// GetBlockStatsCmd help.
|
||||
"getblockstats--synopsis": "Returns statistics about a block given its hash or height. --txindex must be enabled for fee and feerate statistics.",
|
||||
"getblockstats-hashorheight": "The hash or height of the block",
|
||||
"hashorheight-value": "The hash or height of the block",
|
||||
"getblockstats-stats": "Selected statistics",
|
||||
|
||||
// GetBlockStatsResult help.
|
||||
"getblockstatsresult-avgfee": "The average fee in the block",
|
||||
"getblockstatsresult-avgfeerate": "The average feerate in the block (in satoshis per virtual byte)",
|
||||
"getblockstatsresult-avgtxsize": "The average transaction size in the block",
|
||||
"getblockstatsresult-blockhash": "The block hash",
|
||||
"getblockstatsresult-feerate_percentiles": "Feerates at the 10th, 25th, 50th, 75th, and 90th percentile weight unit (in satoshis per virtual byte)",
|
||||
"getblockstatsresult-height": "The block height",
|
||||
"getblockstatsresult-ins": "The number of inputs (excluding coinbase)",
|
||||
"getblockstatsresult-maxfee": "Maxium fee in the block",
|
||||
"getblockstatsresult-maxfeerate": "Maximum feerate in the block (in satoshis per virtual byte)",
|
||||
"getblockstatsresult-maxtxsize": "Maximum transaction size",
|
||||
"getblockstatsresult-medianfee": "Truncated median fee",
|
||||
"getblockstatsresult-mediantime": "The median time from the block and its previous 10 blocks (BIP113)",
|
||||
"getblockstatsresult-mediantxsize": "Truncated median transaction size",
|
||||
"getblockstatsresult-minfee": "Minimum fee in the block",
|
||||
"getblockstatsresult-minfeerate": "Minimum feerate in the block (in satoshis per virtual byte)",
|
||||
"getblockstatsresult-mintxsize": "Minimum transaction size",
|
||||
"getblockstatsresult-outs": "The number of outputs",
|
||||
"getblockstatsresult-subsidy": "The block subsidy",
|
||||
"getblockstatsresult-swtotal_size": "Total size of all segwit transactions in the block (excluding coinbase)",
|
||||
"getblockstatsresult-swtotal_weight": "Total weight of all segwit transactions in the block (excluding coinbase)",
|
||||
"getblockstatsresult-swtxs": "The number of segwit transactions in the block (excluding coinbase)",
|
||||
"getblockstatsresult-time": "The block time",
|
||||
"getblockstatsresult-total_out": "Total amount in all outputs (excluding coinbase)",
|
||||
"getblockstatsresult-total_size": "Total size of all transactions (excluding coinbase)",
|
||||
"getblockstatsresult-total_weight": "Total weight of all transactions (excluding coinbase)",
|
||||
"getblockstatsresult-totalfee": "The total of fees",
|
||||
"getblockstatsresult-txs": "The number of transactions (excluding coinbase)",
|
||||
"getblockstatsresult-utxo_increase": "The increase/decrease in the number of unspent outputs",
|
||||
"getblockstatsresult-utxo_size_inc": "The increase/decrease in size for the utxo index",
|
||||
|
||||
// SoftForkDescription help.
|
||||
"softforkdescription-reject": "The current activation status of the softfork",
|
||||
"softforkdescription-version": "The block version that signals enforcement of this softfork",
|
||||
|
@ -300,11 +260,9 @@ var helpDescsEnUS = map[string]string{
|
|||
"getblockverboseresult-tx": "The transaction hashes (only when verbosity=1)",
|
||||
"getblockverboseresult-nTx": "The number of transactions (aka, count of TX)",
|
||||
"getblockverboseresult-time": "The block time in seconds since 1 Jan 1970 GMT",
|
||||
"getblockverboseresult-mediantime": "The median block time in seconds since 1 Jan 1970 GMT",
|
||||
"getblockverboseresult-nonce": "The block nonce",
|
||||
"getblockverboseresult-bits": "The bits which represent the block difficulty",
|
||||
"getblockverboseresult-difficulty": "The proof-of-work difficulty as a multiple of the minimum difficulty",
|
||||
"getblockverboseresult-chainwork": "Expected number of hashes required to produce the chain up to this block (in hex)",
|
||||
"getblockverboseresult-previousblockhash": "The hash of the previous block",
|
||||
"getblockverboseresult-nextblockhash": "The hash of the next block (only if there is one)",
|
||||
"getblockverboseresult-strippedsize": "The size of the block without witness data",
|
||||
|
@ -495,13 +453,8 @@ var helpDescsEnUS = map[string]string{
|
|||
"getmempoolinfo--synopsis": "Returns memory pool information",
|
||||
|
||||
// GetMempoolInfoResult help.
|
||||
"getmempoolinforesult-bytes": "Size in bytes of the mempool",
|
||||
"getmempoolinforesult-size": "Number of transactions in the mempool",
|
||||
"getmempoolinforesult-usage": "Total memory usage for the mempool",
|
||||
"getmempoolinforesult-total_fee": "Total fees for the mempool in LBC, ignoring modified fees through prioritizetransaction",
|
||||
"getmempoolinforesult-mempoolminfee": "Minimum fee rate in LBC/kvB for tx to be accepted. Is the maximum of minrelaytxfee and minimum mempool fee",
|
||||
"getmempoolinforesult-minrelaytxfee": "Current minimum relay fee for transactions",
|
||||
"getmempoolinforesult-unbroadcastcount": "Current number of transactions that haven't passed initial broadcast yet",
|
||||
"getmempoolinforesult-bytes": "Size in bytes of the mempool",
|
||||
"getmempoolinforesult-size": "Number of transactions in the mempool",
|
||||
|
||||
// GetMiningInfoResult help.
|
||||
"getmininginforesult-blocks": "Height of the latest best block",
|
||||
|
@ -676,16 +629,6 @@ var helpDescsEnUS = map[string]string{
|
|||
"ping--synopsis": "Queues a ping to be sent to each connected peer.\n" +
|
||||
"Ping times are provided by getpeerinfo via the pingtime and pingwait fields.",
|
||||
|
||||
// ListBannedCmd help.
|
||||
"listbanned--synopsis": "List all banned IPs.",
|
||||
|
||||
// ListBannedResult help.
|
||||
"listbannedresult-address": "The IP of the banned node.",
|
||||
"listbannedresult-ban_created": "The UNIX epoch time the ban was created.",
|
||||
"listbannedresult-banned_until": "The UNIX epoch time the ban expires.",
|
||||
"listbannedresult-ban_duration": "The duration of the ban, in seconds.",
|
||||
"listbannedresult-time_remaining": "The time remaining on the ban, in seconds",
|
||||
|
||||
// ReconsiderBlockCmd
|
||||
"reconsiderblock--synopsis": "Reconsider a block for validation.",
|
||||
"reconsiderblock-blockhash": "Hash of the block you want to reconsider",
|
||||
|
@ -714,13 +657,6 @@ var helpDescsEnUS = map[string]string{
|
|||
"sendrawtransaction--result0": "The hash of the transaction",
|
||||
"allowhighfeesormaxfeerate-value": "Either the boolean value for the allowhighfees parameter in bitcoind < v0.19.0 or the numerical value for the maxfeerate field in bitcoind v0.19.0 and later",
|
||||
|
||||
// SetBanCmd help.
|
||||
"setban--synopsis": "Add or remove an IP from the banned list. (Currently, subnet is not supported.)",
|
||||
"setban-addr": "The IP to ban. (Currently, subnet is not supported.)",
|
||||
"setban-subcmd": "'add' to add an IP to the list, 'remove' to remove an IP from the list",
|
||||
"setban-bantime": "Time in seconds the IP is banned (0 or empty means using the default time of 24h which can also be overwritten by the -bantime startup argument)",
|
||||
"setban-absolute": "If set, the bantime must be an absolute timestamp expressed in UNIX epoch time; default to false.",
|
||||
|
||||
// SetGenerateCmd help.
|
||||
"setgenerate--synopsis": "Set the server to generate coins (mine) or not.",
|
||||
"setgenerate-generate": "Use true to enable generation, false to disable it",
|
||||
|
@ -937,7 +873,6 @@ var helpDescsEnUS = map[string]string{
|
|||
// pointer to the type (or nil to indicate no return value).
|
||||
var rpcResultTypes = map[string][]interface{}{
|
||||
"addnode": nil,
|
||||
"clearbanned": nil,
|
||||
"createrawtransaction": {(*string)(nil)},
|
||||
"debuglevel": {(*string)(nil), (*string)(nil)},
|
||||
"decoderawtransaction": {(*btcjson.TxRawDecodeResult)(nil)},
|
||||
|
@ -950,12 +885,11 @@ var rpcResultTypes = map[string][]interface{}{
|
|||
"getbestblock": {(*btcjson.GetBestBlockResult)(nil)},
|
||||
"getbestblockhash": {(*string)(nil)},
|
||||
"getblock": {(*string)(nil), (*btcjson.GetBlockVerboseResult)(nil)},
|
||||
"getblockchaininfo": {(*btcjson.GetBlockChainInfoResult)(nil)},
|
||||
"getblockcount": {(*int64)(nil)},
|
||||
"getblockhash": {(*string)(nil)},
|
||||
"getblockheader": {(*string)(nil), (*btcjson.GetBlockHeaderVerboseResult)(nil)},
|
||||
"getblockstats": {(*btcjson.GetBlockStatsResult)(nil)},
|
||||
"getblocktemplate": {(*btcjson.GetBlockTemplateResult)(nil), (*string)(nil), nil},
|
||||
"getblockchaininfo": {(*btcjson.GetBlockChainInfoResult)(nil)},
|
||||
"getcfilter": {(*string)(nil)},
|
||||
"getcfilterheader": {(*string)(nil)},
|
||||
"getchaintips": {(*[]btcjson.GetChainTipsResult)(nil)},
|
||||
|
@ -977,15 +911,13 @@ var rpcResultTypes = map[string][]interface{}{
|
|||
"getrawmempool": {(*[]string)(nil), (*btcjson.GetRawMempoolVerboseResult)(nil)},
|
||||
"getrawtransaction": {(*string)(nil), (*btcjson.TxRawResult)(nil)},
|
||||
"gettxout": {(*btcjson.GetTxOutResult)(nil)},
|
||||
"node": nil,
|
||||
"help": {(*string)(nil), (*string)(nil)},
|
||||
"invalidateblock": nil,
|
||||
"listbanned": {(*[]btcjson.ListBannedResult)(nil)},
|
||||
"node": nil,
|
||||
"ping": nil,
|
||||
"reconsiderblock": nil,
|
||||
"searchrawtransactions": {(*string)(nil), (*[]btcjson.SearchRawTransactionsResult)(nil)},
|
||||
"sendrawtransaction": {(*string)(nil)},
|
||||
"setban": nil,
|
||||
"setgenerate": nil,
|
||||
"signmessagewithprivkey": {(*string)(nil)},
|
||||
"stop": {(*string)(nil)},
|
||||
|
|
|
@ -376,9 +376,6 @@
|
|||
; Write CPU profile to the specified file.
|
||||
; cpuprofile=
|
||||
|
||||
; Write memory profile to the specified file.
|
||||
; memprofile=
|
||||
|
||||
; The port used to listen for HTTP profile requests. The profile server will
|
||||
; be disabled if this option is not specified. The profile information can be
|
||||
; accessed at http://localhost:<profileport>/debug/pprof once running.
|
||||
|
|
71
server.go
71
server.go
|
@ -156,18 +156,13 @@ type updatePeerHeightsMsg struct {
|
|||
originPeer *peer.Peer
|
||||
}
|
||||
|
||||
type bannedPeriod struct {
|
||||
since time.Time
|
||||
until time.Time
|
||||
}
|
||||
|
||||
// peerState maintains state of inbound, persistent, outbound peers as well
|
||||
// as banned peers and outbound groups.
|
||||
type peerState struct {
|
||||
inboundPeers map[int32]*serverPeer
|
||||
outboundPeers map[int32]*serverPeer
|
||||
persistentPeers map[int32]*serverPeer
|
||||
banned map[string]bannedPeriod
|
||||
banned map[string]time.Time
|
||||
outboundGroups map[string]int
|
||||
}
|
||||
|
||||
|
@ -704,11 +699,6 @@ func (sp *serverPeer) OnGetData(_ *peer.Peer, msg *wire.MsgGetData) {
|
|||
if i == len(msg.InvList)-1 && c != nil {
|
||||
<-c
|
||||
}
|
||||
} else if iv.Type == wire.InvTypeWitnessTx || iv.Type == wire.InvTypeTx {
|
||||
// We interpret fulfilling a GETDATA for a transaction as a
|
||||
// successful initial broadcast and remove it from our
|
||||
// unbroadcast set.
|
||||
sp.server.txMemPool.RemoveUnbroadcastTx(&iv.Hash)
|
||||
}
|
||||
numAdded++
|
||||
waitChan = c
|
||||
|
@ -1661,10 +1651,10 @@ func (s *server) handleAddPeerMsg(state *peerState, sp *serverPeer) bool {
|
|||
sp.Disconnect()
|
||||
return false
|
||||
}
|
||||
if ban, ok := state.banned[host]; ok {
|
||||
if time.Now().Before(ban.until) {
|
||||
srvrLog.Infof("Peer %s is banned for another %v - disconnecting",
|
||||
host, time.Until(ban.until))
|
||||
if banEnd, ok := state.banned[host]; ok {
|
||||
if time.Now().Before(banEnd) {
|
||||
srvrLog.Debugf("Peer %s is banned for another %v - disconnecting",
|
||||
host, time.Until(banEnd))
|
||||
sp.Disconnect()
|
||||
return false
|
||||
}
|
||||
|
@ -1786,12 +1776,7 @@ func (s *server) handleBanPeerMsg(state *peerState, sp *serverPeer) {
|
|||
direction := directionString(sp.Inbound())
|
||||
srvrLog.Infof("Banned peer %s (%s) for %v", host, direction,
|
||||
cfg.BanDuration)
|
||||
|
||||
since := time.Now()
|
||||
state.banned[host] = bannedPeriod{
|
||||
since: since,
|
||||
until: since.Add(cfg.BanDuration),
|
||||
}
|
||||
state.banned[host] = time.Now().Add(cfg.BanDuration)
|
||||
}
|
||||
|
||||
// handleRelayInvMsg deals with relaying inventory to peers that are not already
|
||||
|
@ -1886,25 +1871,6 @@ type getPeersMsg struct {
|
|||
reply chan []*serverPeer
|
||||
}
|
||||
|
||||
type listBannedPeersMsg struct {
|
||||
reply chan map[string]bannedPeriod
|
||||
}
|
||||
|
||||
type setBanMsg struct {
|
||||
addr string
|
||||
since time.Time
|
||||
until time.Time
|
||||
reply chan error
|
||||
}
|
||||
|
||||
type removeBanMsg struct {
|
||||
addr string
|
||||
reply chan error
|
||||
}
|
||||
|
||||
type clearBannedMsg struct {
|
||||
reply chan error
|
||||
}
|
||||
type getOutboundGroup struct {
|
||||
key string
|
||||
reply chan int
|
||||
|
@ -1953,29 +1919,6 @@ func (s *server) handleQuery(state *peerState, querymsg interface{}) {
|
|||
})
|
||||
msg.reply <- peers
|
||||
|
||||
case listBannedPeersMsg:
|
||||
banned := map[string]bannedPeriod{}
|
||||
for host, ban := range state.banned {
|
||||
banned[host] = ban
|
||||
}
|
||||
msg.reply <- banned
|
||||
|
||||
case setBanMsg:
|
||||
ban := bannedPeriod{
|
||||
since: msg.since,
|
||||
until: msg.until,
|
||||
}
|
||||
state.banned[msg.addr] = ban
|
||||
msg.reply <- nil
|
||||
|
||||
case removeBanMsg:
|
||||
delete(state.banned, msg.addr)
|
||||
msg.reply <- nil
|
||||
|
||||
case clearBannedMsg:
|
||||
state.banned = map[string]bannedPeriod{}
|
||||
msg.reply <- nil
|
||||
|
||||
case connectNodeMsg:
|
||||
// TODO: duplicate oneshots?
|
||||
// Limit max number of total peers.
|
||||
|
@ -2213,7 +2156,7 @@ func (s *server) peerHandler() {
|
|||
inboundPeers: make(map[int32]*serverPeer),
|
||||
persistentPeers: make(map[int32]*serverPeer),
|
||||
outboundPeers: make(map[int32]*serverPeer),
|
||||
banned: make(map[string]bannedPeriod),
|
||||
banned: make(map[string]time.Time),
|
||||
outboundGroups: make(map[string]int),
|
||||
}
|
||||
|
||||
|
|
|
@ -33,9 +33,6 @@ const (
|
|||
|
||||
// ErrInvalidClaimUpdateScript is returned a claim update script does not conform to the format.
|
||||
ErrInvalidClaimUpdateScript
|
||||
|
||||
// ErrInvalidClaimName is returned when the claim name is invalid.
|
||||
ErrInvalidClaimName
|
||||
)
|
||||
|
||||
func claimScriptError(c ErrorCode, desc string) Error {
|
||||
|
@ -101,15 +98,11 @@ func ExtractClaimScript(script []byte) (*ClaimScript, error) {
|
|||
|
||||
if !tokenizer.Next() || tokenizer.Opcode() != OP_2DROP ||
|
||||
!tokenizer.Next() || tokenizer.Opcode() != OP_DROP {
|
||||
return nil, claimScriptError(ErrInvalidClaimNameScript, "expect OP_2DROP OP_DROP")
|
||||
}
|
||||
|
||||
cs.Size = int(tokenizer.ByteIndex())
|
||||
if cs.Size > MaxClaimScriptSize {
|
||||
str := fmt.Sprintf("script size %d exceeds limit %d", cs.Size, MaxClaimScriptSize)
|
||||
str := fmt.Sprintf("expect OP_2DROP OP_DROP")
|
||||
return nil, claimScriptError(ErrInvalidClaimNameScript, str)
|
||||
}
|
||||
|
||||
cs.Size = int(tokenizer.ByteIndex())
|
||||
return &cs, nil
|
||||
|
||||
case OP_SUPPORTCLAIM:
|
||||
|
@ -135,7 +128,8 @@ func ExtractClaimScript(script []byte) (*ClaimScript, error) {
|
|||
case tokenizer.Opcode() == OP_2DROP:
|
||||
// Case 1: OP_SUPPORTCLAIM <Name> <ClaimID> OP_2DROP OP_DROP <P2PKH>
|
||||
if !tokenizer.Next() || tokenizer.Opcode() != OP_DROP {
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, "expect OP_2DROP OP_DROP")
|
||||
str := fmt.Sprintf("expect OP_2DROP OP_DROP")
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, str)
|
||||
}
|
||||
|
||||
case len(tokenizer.Data()) != 0:
|
||||
|
@ -144,21 +138,19 @@ func ExtractClaimScript(script []byte) (*ClaimScript, error) {
|
|||
cs.Value = tokenizer.Data()
|
||||
if !tokenizer.Next() || tokenizer.Opcode() != OP_2DROP ||
|
||||
!tokenizer.Next() || tokenizer.Opcode() != OP_2DROP {
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, "expect OP_2DROP OP_2DROP")
|
||||
str := fmt.Sprintf("expect OP_2DROP OP_2DROP")
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, str)
|
||||
}
|
||||
default:
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, "expect OP_2DROP OP_DROP")
|
||||
}
|
||||
|
||||
cs.Size = int(tokenizer.ByteIndex())
|
||||
if cs.Size > MaxClaimScriptSize {
|
||||
str := fmt.Sprintf("script size %d exceeds limit %d", cs.Size, MaxClaimScriptSize)
|
||||
str := fmt.Sprintf("expect OP_2DROP OP_DROP")
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, str)
|
||||
}
|
||||
|
||||
cs.Size = int(tokenizer.ByteIndex())
|
||||
return &cs, nil
|
||||
|
||||
case OP_UPDATECLAIM:
|
||||
|
||||
// OP_UPDATECLAIM <Name> <ClaimID> <Value> OP_2DROP OP_2DROP <P2PKH>
|
||||
if !tokenizer.Next() || len(tokenizer.Data()) > MaxClaimNameSize {
|
||||
str := fmt.Sprintf("name size %d exceeds limit %d", len(tokenizer.data), MaxClaimNameSize)
|
||||
|
@ -185,11 +177,6 @@ func ExtractClaimScript(script []byte) (*ClaimScript, error) {
|
|||
}
|
||||
|
||||
cs.Size = int(tokenizer.ByteIndex())
|
||||
if cs.Size > MaxClaimScriptSize {
|
||||
str := fmt.Sprintf("script size %d exceeds limit %d", cs.Size, MaxClaimScriptSize)
|
||||
return nil, claimScriptError(ErrInvalidClaimUpdateScript, str)
|
||||
}
|
||||
|
||||
return &cs, nil
|
||||
|
||||
default:
|
||||
|
@ -206,7 +193,7 @@ func StripClaimScriptPrefix(script []byte) []byte {
|
|||
return script[cs.Size:]
|
||||
}
|
||||
|
||||
const illegalChars = "=&#:$%?/;\\\b\n\t\r\x00"
|
||||
const illegalChars = "=&#:*$%?/;\\\b\n\t\r\x00"
|
||||
|
||||
func AllClaimsAreSane(script []byte, enforceSoftFork bool) error {
|
||||
cs, err := ExtractClaimScript(script)
|
||||
|
@ -218,11 +205,10 @@ func AllClaimsAreSane(script []byte, enforceSoftFork bool) error {
|
|||
}
|
||||
if enforceSoftFork {
|
||||
if !utf8.Valid(cs.Name) {
|
||||
return claimScriptError(ErrInvalidClaimName, "claim name is not valid UTF-8")
|
||||
return fmt.Errorf("claim name is not valid UTF-8")
|
||||
}
|
||||
if bytes.ContainsAny(cs.Name, illegalChars) {
|
||||
str := fmt.Sprintf("claim name has illegal chars; it should not contain any of these: %s", illegalChars)
|
||||
return claimScriptError(ErrInvalidClaimName, str)
|
||||
return fmt.Errorf("claim name has illegal chars; it should not contain any of these: %s", illegalChars)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ overview to provide information on how to use the package.
|
|||
This package provides data structures and functions to parse and execute
|
||||
bitcoin transaction scripts.
|
||||
|
||||
# Script Overview
|
||||
Script Overview
|
||||
|
||||
Bitcoin transaction scripts are written in a stack-base, FORTH-like language.
|
||||
|
||||
|
@ -30,7 +30,7 @@ is used to prove the the spender is authorized to perform the transaction.
|
|||
One benefit of using a scripting language is added flexibility in specifying
|
||||
what conditions must be met in order to spend bitcoins.
|
||||
|
||||
# Errors
|
||||
Errors
|
||||
|
||||
Errors returned by this package are of type txscript.Error. This allows the
|
||||
caller to programmatically determine the specific error by examining the
|
||||
|
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue