Compare commits
22 commits
Author | SHA1 | Date | |
---|---|---|---|
|
a0ff51b84a | ||
|
4c39a9842c | ||
|
f513fca6a7 | ||
|
6728bf4b08 | ||
|
979d643594 | ||
|
cbc4d489e8 | ||
|
987a533423 | ||
|
6bc9a2b4dd | ||
|
9bcd3d0591 | ||
|
2adfcd211d | ||
|
81ec217899 | ||
|
5acfa4c81b | ||
|
c5193e74ac | ||
|
8a80f0683a | ||
|
5d7a219e35 | ||
|
2d04d31894 | ||
|
ce37025d5a | ||
|
98e5771989 | ||
|
ff324e0fdb | ||
|
be0d7de8da | ||
|
fcfb2af76f | ||
|
78bed14956 |
41 changed files with 2238 additions and 475 deletions
|
@ -56,7 +56,7 @@ If no config is found, it creates a [default one](sample-lbcd.conf), which inclu
|
|||
|
||||
### RPC server
|
||||
|
||||
RPC credentials (`rpcuser` and `rpcpass`) is required to enable RPC server. It can be specify in the `"${LBCDIR}/lbcd.conf"`, using command line options:
|
||||
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
|
||||
|
@ -138,7 +138,7 @@ 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 lbcd_snapshot_1199527_v0.22.105_2022-07-27.tar.zst | tar xf - -C "${LBCDIR}"
|
||||
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:
|
||||
|
@ -147,7 +147,7 @@ By the time the download is finished, the snapshots should be almost uncompresse
|
|||
``` sh
|
||||
mkdir -p "${LBCDDIR}"
|
||||
|
||||
time curl https://snapshots.lbry.com/blockchain/lbcd_snapshot_1199527_v0.22.105_2022-07-27.tar.zst | zstd -d | tar xf - -C "${LBCDIR}"
|
||||
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
|
||||
|
|
|
@ -1394,8 +1394,8 @@ func (b *BlockChain) BlockAttributesByHash(hash *chainhash.Hash, prevHash *chain
|
|||
attrs *BlockAttributes, best *BestState, err error) {
|
||||
best = b.BestSnapshot()
|
||||
node := b.index.LookupNode(hash)
|
||||
if node == nil || !b.bestChain.Contains(node) {
|
||||
str := fmt.Sprintf("block %s is not in the main chain", hash)
|
||||
if node == nil {
|
||||
str := fmt.Sprintf("block %s not found", hash)
|
||||
return nil, best, errNotInMainChain(str)
|
||||
}
|
||||
|
||||
|
@ -1405,6 +1405,9 @@ func (b *BlockChain) BlockAttributesByHash(hash *chainhash.Hash, prevHash *chain
|
|||
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 {
|
||||
|
|
|
@ -11,6 +11,7 @@ import (
|
|||
|
||||
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
||||
"github.com/lbryio/lbcd/txscript"
|
||||
"github.com/lbryio/lbcd/wire"
|
||||
btcutil "github.com/lbryio/lbcutil"
|
||||
)
|
||||
|
||||
|
@ -227,6 +228,20 @@ 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 "+
|
||||
|
|
|
@ -48,6 +48,15 @@ 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 {
|
||||
|
@ -58,7 +67,7 @@ type TransactionInput struct {
|
|||
// CreateRawTransactionCmd defines the createrawtransaction JSON-RPC command.
|
||||
type CreateRawTransactionCmd struct {
|
||||
Inputs []TransactionInput
|
||||
Amounts map[string]float64 `jsonrpcusage:"{\"address\":amount,...}"` // In BTC
|
||||
Outputs map[string]interface{} `jsonrpcusage:"{\"address\":amount, \"data\":\"hex\", ...}"`
|
||||
LockTime *int64
|
||||
}
|
||||
|
||||
|
@ -67,7 +76,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, amounts map[string]float64,
|
||||
func NewCreateRawTransactionCmd(inputs []TransactionInput, outputs map[string]interface{},
|
||||
lockTime *int64) *CreateRawTransactionCmd {
|
||||
// to make sure we're serializing this to the empty list and not null, we
|
||||
// explicitly initialize the list
|
||||
|
@ -76,7 +85,7 @@ func NewCreateRawTransactionCmd(inputs []TransactionInput, amounts map[string]fl
|
|||
}
|
||||
return &CreateRawTransactionCmd{
|
||||
Inputs: inputs,
|
||||
Amounts: amounts,
|
||||
Outputs: outputs,
|
||||
LockTime: lockTime,
|
||||
}
|
||||
}
|
||||
|
@ -757,6 +766,15 @@ 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{}
|
||||
|
||||
|
@ -903,6 +921,39 @@ 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
|
||||
|
@ -1080,6 +1131,9 @@ 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},
|
||||
}
|
||||
amounts := map[string]float64{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, amounts, nil)
|
||||
txOutputs := map[string]interface{}{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, txOutputs, 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}},
|
||||
Amounts: map[string]float64{"456": .0123},
|
||||
Outputs: map[string]interface{}{"456": .0123},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -67,13 +67,13 @@ func TestChainSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("createrawtransaction", `[]`, `{"456":0.0123}`)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
amounts := map[string]float64{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(nil, amounts, nil)
|
||||
txOutputs := map[string]interface{}{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(nil, txOutputs, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"createrawtransaction","params":[[],{"456":0.0123}],"id":1}`,
|
||||
unmarshalled: &btcjson.CreateRawTransactionCmd{
|
||||
Inputs: []btcjson.TransactionInput{},
|
||||
Amounts: map[string]float64{"456": .0123},
|
||||
Outputs: map[string]interface{}{"456": .0123},
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -86,16 +86,35 @@ func TestChainSvrCmds(t *testing.T) {
|
|||
txInputs := []btcjson.TransactionInput{
|
||||
{Txid: "123", Vout: 1},
|
||||
}
|
||||
amounts := map[string]float64{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, amounts, btcjson.Int64(12312333333))
|
||||
txOutputs := map[string]interface{}{"456": .0123}
|
||||
return btcjson.NewCreateRawTransactionCmd(txInputs, txOutputs, 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}},
|
||||
Amounts: map[string]float64{"456": .0123},
|
||||
Outputs: map[string]interface{}{"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,35 +35,37 @@ 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"`
|
||||
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"`
|
||||
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"`
|
||||
}
|
||||
|
||||
type GetBlockVerboseResultBase struct {
|
||||
|
@ -721,6 +723,15 @@ 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"`
|
||||
|
|
|
@ -176,12 +176,13 @@ func NewGetAccountCmd(address string) *GetAccountCmd {
|
|||
|
||||
// GetAccountAddressCmd defines the getaccountaddress JSON-RPC command.
|
||||
type GetAccountAddressCmd struct {
|
||||
Account string
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
AddressType *string `jsonrpcdefault:"\"legacy\""`
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
|
@ -189,12 +190,13 @@ func NewGetAccountAddressCmd(account string) *GetAccountAddressCmd {
|
|||
|
||||
// GetAddressesByAccountCmd defines the getaddressesbyaccount JSON-RPC command.
|
||||
type GetAddressesByAccountCmd struct {
|
||||
Account string
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
}
|
||||
|
||||
// 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,
|
||||
}
|
||||
|
@ -215,8 +217,9 @@ func NewGetAddressInfoCmd(address string) *GetAddressInfoCmd {
|
|||
|
||||
// GetBalanceCmd defines the getbalance JSON-RPC command.
|
||||
type GetBalanceCmd struct {
|
||||
Account *string
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
}
|
||||
|
||||
// NewGetBalanceCmd returns a new instance which can be used to issue a
|
||||
|
@ -242,8 +245,8 @@ func NewGetBalancesCmd() *GetBalancesCmd {
|
|||
|
||||
// GetNewAddressCmd defines the getnewaddress JSON-RPC command.
|
||||
type GetNewAddressCmd struct {
|
||||
Account *string
|
||||
AddressType *string // must be one of legacy / p2pkh or p2sh-p2wkh / p2sh-segwit, or p2wkh / bech32
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
AddressType *string `jsonrpcdefault:"\"legacy\""`
|
||||
}
|
||||
|
||||
// NewGetNewAddressCmd returns a new instance which can be used to issue a
|
||||
|
@ -259,7 +262,8 @@ func NewGetNewAddressCmd(account *string) *GetNewAddressCmd {
|
|||
|
||||
// GetRawChangeAddressCmd defines the getrawchangeaddress JSON-RPC command.
|
||||
type GetRawChangeAddressCmd struct {
|
||||
Account *string
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
AddressType *string `jsonrpcdefault:"\"legacy\""`
|
||||
}
|
||||
|
||||
// NewGetRawChangeAddressCmd returns a new instance which can be used to issue a
|
||||
|
@ -275,7 +279,7 @@ func NewGetRawChangeAddressCmd(account *string) *GetRawChangeAddressCmd {
|
|||
|
||||
// GetReceivedByAccountCmd defines the getreceivedbyaccount JSON-RPC command.
|
||||
type GetReceivedByAccountCmd struct {
|
||||
Account string
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
}
|
||||
|
||||
|
@ -284,7 +288,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,
|
||||
|
@ -408,6 +412,7 @@ func NewKeyPoolRefillCmd(newSize *uint) *KeyPoolRefillCmd {
|
|||
// ListAccountsCmd defines the listaccounts JSON-RPC command.
|
||||
type ListAccountsCmd struct {
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
}
|
||||
|
||||
// NewListAccountsCmd returns a new instance which can be used to issue a
|
||||
|
@ -501,7 +506,7 @@ func NewListSinceBlockCmd(blockHash *string, targetConfirms *int, includeWatchOn
|
|||
|
||||
// ListTransactionsCmd defines the listtransactions JSON-RPC command.
|
||||
type ListTransactionsCmd struct {
|
||||
Account *string
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
Count *int `jsonrpcdefault:"10"`
|
||||
From *int `jsonrpcdefault:"0"`
|
||||
IncludeWatchOnly *bool `jsonrpcdefault:"false"`
|
||||
|
@ -562,6 +567,7 @@ type SendFromCmd struct {
|
|||
ToAddress string
|
||||
Amount float64 // In BTC
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
Comment *string
|
||||
CommentTo *string
|
||||
}
|
||||
|
@ -571,12 +577,15 @@ 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, comment, commentTo *string) *SendFromCmd {
|
||||
func NewSendFromCmd(fromAccount, toAddress string, amount float64,
|
||||
minConf *int, addrType *string, comment, commentTo *string) *SendFromCmd {
|
||||
|
||||
return &SendFromCmd{
|
||||
FromAccount: fromAccount,
|
||||
ToAddress: toAddress,
|
||||
Amount: amount,
|
||||
MinConf: minConf,
|
||||
AddressType: addrType,
|
||||
Comment: comment,
|
||||
CommentTo: commentTo,
|
||||
}
|
||||
|
@ -587,6 +596,7 @@ type SendManyCmd struct {
|
|||
FromAccount string
|
||||
Amounts map[string]float64 `jsonrpcusage:"{\"address\":amount,...}"` // In BTC
|
||||
MinConf *int `jsonrpcdefault:"1"`
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
Comment *string
|
||||
}
|
||||
|
||||
|
@ -595,11 +605,13 @@ 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, comment *string) *SendManyCmd {
|
||||
func NewSendManyCmd(fromAccount string, amounts map[string]float64,
|
||||
minConf *int, addrType *string, comment *string) *SendManyCmd {
|
||||
return &SendManyCmd{
|
||||
FromAccount: fromAccount,
|
||||
Amounts: amounts,
|
||||
MinConf: minConf,
|
||||
AddressType: addrType,
|
||||
Comment: comment,
|
||||
}
|
||||
}
|
||||
|
@ -608,6 +620,7 @@ func NewSendManyCmd(fromAccount string, amounts map[string]float64, minConf *int
|
|||
type SendToAddressCmd struct {
|
||||
Address string
|
||||
Amount float64
|
||||
AddressType *string `jsonrpcdefault:"\"*\""`
|
||||
Comment *string
|
||||
CommentTo *string
|
||||
}
|
||||
|
@ -617,10 +630,12 @@ 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, comment, commentTo *string) *SendToAddressCmd {
|
||||
func NewSendToAddressCmd(address string, amount float64, addrType *string,
|
||||
comment, commentTo *string) *SendToAddressCmd {
|
||||
return &SendToAddressCmd{
|
||||
Address: address,
|
||||
Amount: amount,
|
||||
AddressType: addrType,
|
||||
Comment: comment,
|
||||
CommentTo: commentTo,
|
||||
}
|
||||
|
@ -960,6 +975,24 @@ 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 {
|
||||
|
@ -1081,6 +1114,7 @@ func init() {
|
|||
MustRegisterCmd("listunspent", (*ListUnspentCmd)(nil), flags)
|
||||
MustRegisterCmd("loadwallet", (*LoadWalletCmd)(nil), flags)
|
||||
MustRegisterCmd("lockunspent", (*LockUnspentCmd)(nil), flags)
|
||||
MustRegisterCmd("rescanblockchain", (*RescanBlockchainCmd)(nil), flags)
|
||||
MustRegisterCmd("sendfrom", (*SendFromCmd)(nil), flags)
|
||||
MustRegisterCmd("sendmany", (*SendManyCmd)(nil), flags)
|
||||
MustRegisterCmd("sendtoaddress", (*SendToAddressCmd)(nil), flags)
|
||||
|
|
|
@ -287,11 +287,12 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("getaccountaddress", "acct")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewGetAccountAddressCmd("acct")
|
||||
return btcjson.NewGetAccountAddressCmd(btcjson.String("acct"))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getaccountaddress","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetAccountAddressCmd{
|
||||
Account: "acct",
|
||||
Account: btcjson.String("acct"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -300,11 +301,12 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("getaddressesbyaccount", "acct")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewGetAddressesByAccountCmd("acct")
|
||||
return btcjson.NewGetAddressesByAccountCmd(btcjson.String("acct"))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getaddressesbyaccount","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetAddressesByAccountCmd{
|
||||
Account: "acct",
|
||||
Account: btcjson.String("acct"),
|
||||
AddressType: btcjson.String("*"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -330,8 +332,9 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getbalance","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.GetBalanceCmd{
|
||||
Account: nil,
|
||||
Account: btcjson.String("default"),
|
||||
MinConf: btcjson.Int(1),
|
||||
AddressType: btcjson.String("*"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -346,6 +349,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
unmarshalled: &btcjson.GetBalanceCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
MinConf: btcjson.Int(1),
|
||||
AddressType: btcjson.String("*"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -360,6 +364,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
unmarshalled: &btcjson.GetBalanceCmd{
|
||||
Account: btcjson.String("acct"),
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("*"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -383,7 +388,8 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getnewaddress","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.GetNewAddressCmd{
|
||||
Account: nil,
|
||||
Account: btcjson.String("default"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -397,6 +403,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"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -409,7 +416,8 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getrawchangeaddress","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.GetRawChangeAddressCmd{
|
||||
Account: nil,
|
||||
Account: btcjson.String("default"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -423,6 +431,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"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -431,11 +440,11 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("getreceivedbyaccount", "acct")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewGetReceivedByAccountCmd("acct", nil)
|
||||
return btcjson.NewGetReceivedByAccountCmd(btcjson.String("acct"), nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getreceivedbyaccount","params":["acct"],"id":1}`,
|
||||
unmarshalled: &btcjson.GetReceivedByAccountCmd{
|
||||
Account: "acct",
|
||||
Account: btcjson.String("acct"),
|
||||
MinConf: btcjson.Int(1),
|
||||
},
|
||||
},
|
||||
|
@ -445,11 +454,11 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("getreceivedbyaccount", "acct", 6)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewGetReceivedByAccountCmd("acct", btcjson.Int(6))
|
||||
return btcjson.NewGetReceivedByAccountCmd(btcjson.String("acct"), btcjson.Int(6))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"getreceivedbyaccount","params":["acct",6],"id":1}`,
|
||||
unmarshalled: &btcjson.GetReceivedByAccountCmd{
|
||||
Account: "acct",
|
||||
Account: btcjson.String("acct"),
|
||||
MinConf: btcjson.Int(6),
|
||||
},
|
||||
},
|
||||
|
@ -602,6 +611,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("*"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -615,6 +625,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("*"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -844,7 +855,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"listtransactions","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.ListTransactionsCmd{
|
||||
Account: nil,
|
||||
Account: btcjson.String("default"),
|
||||
Count: btcjson.Int(10),
|
||||
From: btcjson.Int(0),
|
||||
IncludeWatchOnly: btcjson.Bool(false),
|
||||
|
@ -1002,7 +1013,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, nil, nil, nil)
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, nil, nil, nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5],"id":1}`,
|
||||
unmarshalled: &btcjson.SendFromCmd{
|
||||
|
@ -1010,6 +1021,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
ToAddress: "1Address",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(1),
|
||||
AddressType: btcjson.String("*"),
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
},
|
||||
|
@ -1020,7 +1032,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)
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6), nil, nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6],"id":1}`,
|
||||
unmarshalled: &btcjson.SendFromCmd{
|
||||
|
@ -1028,6 +1040,7 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
ToAddress: "1Address",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(6),
|
||||
AddressType: btcjson.String("*"),
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
},
|
||||
|
@ -1035,37 +1048,59 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
{
|
||||
name: "sendfrom optional2",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "comment")
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "legacy")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6),
|
||||
btcjson.String("comment"), nil)
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6), btcjson.String("legacy"),
|
||||
nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"comment"],"id":1}`,
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"legacy"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendFromCmd{
|
||||
FromAccount: "from",
|
||||
ToAddress: "1Address",
|
||||
Amount: 0.5,
|
||||
MinConf: btcjson.Int(6),
|
||||
Comment: btcjson.String("comment"),
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendfrom optional3",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "comment", "commentto")
|
||||
return btcjson.NewCmd("sendfrom", "from", "1Address", 0.5, 6, "legacy", "comment")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendFromCmd("from", "1Address", 0.5, btcjson.Int(6),
|
||||
btcjson.String("comment"), btcjson.String("commentto"))
|
||||
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,"comment","commentto"],"id":1}`,
|
||||
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"),
|
||||
btcjson.String("comment"), btcjson.String("commentto"))
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendfrom","params":["from","1Address",0.5,6,"legacy","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"),
|
||||
},
|
||||
|
@ -1077,13 +1112,14 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
staticCmd: func() interface{} {
|
||||
amounts := map[string]float64{"1Address": 0.5}
|
||||
return btcjson.NewSendManyCmd("from", amounts, nil, nil)
|
||||
return btcjson.NewSendManyCmd("from", amounts, nil, 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,
|
||||
},
|
||||
},
|
||||
|
@ -1094,30 +1130,50 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
},
|
||||
staticCmd: func() interface{} {
|
||||
amounts := map[string]float64{"1Address": 0.5}
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), nil)
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), nil, 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, "comment")
|
||||
return btcjson.NewCmd("sendmany", "from", `{"1Address":0.5}`, 6, "legacy")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
amounts := map[string]float64{"1Address": 0.5}
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), btcjson.String("comment"))
|
||||
return btcjson.NewSendManyCmd("from", amounts, btcjson.Int(6), btcjson.String("legacy"), nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5},6,"comment"],"id":1}`,
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendmany","params":["from",{"1Address":0.5},6,"legacy"],"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"),
|
||||
},
|
||||
},
|
||||
|
@ -1127,12 +1183,13 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5)
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, nil, nil)
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, nil, 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,
|
||||
},
|
||||
|
@ -1140,16 +1197,34 @@ func TestWalletSvrCmds(t *testing.T) {
|
|||
{
|
||||
name: "sendtoaddress optional1",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5, "comment", "commentto")
|
||||
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5, "legacy")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, btcjson.String("comment"),
|
||||
btcjson.String("commentto"))
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, btcjson.String("legacy"), nil, nil)
|
||||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendtoaddress","params":["1Address",0.5,"comment","commentto"],"id":1}`,
|
||||
marshalled: `{"jsonrpc":"1.0","method":"sendtoaddress","params":["1Address",0.5,"legacy"],"id":1}`,
|
||||
unmarshalled: &btcjson.SendToAddressCmd{
|
||||
Address: "1Address",
|
||||
Amount: 0.5,
|
||||
AddressType: btcjson.String("legacy"),
|
||||
Comment: nil,
|
||||
CommentTo: nil,
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "sendtoaddress optional2",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("sendtoaddress", "1Address", 0.5, "legacy", "comment", "commentto")
|
||||
},
|
||||
staticCmd: func() interface{} {
|
||||
return btcjson.NewSendToAddressCmd("1Address", 0.5, btcjson.String("legacy"), btcjson.String("comment"),
|
||||
btcjson.String("commentto"))
|
||||
},
|
||||
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"),
|
||||
},
|
||||
|
|
|
@ -174,6 +174,7 @@ type GetTransactionResult struct {
|
|||
TimeReceived int64 `json:"timereceived"`
|
||||
Details []GetTransactionDetailsResult `json:"details"`
|
||||
Hex string `json:"hex"`
|
||||
Generated bool `json:"generated"`
|
||||
}
|
||||
|
||||
type ScanningOrFalse struct {
|
||||
|
@ -288,7 +289,6 @@ 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,6 +317,12 @@ 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 {
|
||||
|
|
|
@ -40,7 +40,7 @@ func NewExportWatchingWalletCmd(account *string, download *bool) *ExportWatching
|
|||
|
||||
// GetUnconfirmedBalanceCmd defines the getunconfirmedbalance JSON-RPC command.
|
||||
type GetUnconfirmedBalanceCmd struct {
|
||||
Account *string
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
}
|
||||
|
||||
// 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
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
}
|
||||
|
||||
// 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
|
||||
Account *string `jsonrpcdefault:"\"default\""`
|
||||
}
|
||||
|
||||
// NewListAllTransactionsCmd returns a new instance which can be used to issue a
|
||||
|
|
|
@ -71,7 +71,7 @@ func TestWalletSvrWsCmds(t *testing.T) {
|
|||
{
|
||||
name: "exportwatchingwallet optional2",
|
||||
newCmd: func() (interface{}, error) {
|
||||
return btcjson.NewCmd("exportwatchingwallet", "acct", true)
|
||||
return btcjson.NewCmd("exportwatchingwallet", btcjson.String("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: nil,
|
||||
Account: btcjson.String("default"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -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: nil,
|
||||
Account: btcjson.String("default"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
@ -148,7 +148,7 @@ func TestWalletSvrWsCmds(t *testing.T) {
|
|||
},
|
||||
marshalled: `{"jsonrpc":"1.0","method":"listalltransactions","params":[],"id":1}`,
|
||||
unmarshalled: &btcjson.ListAllTransactionsCmd{
|
||||
Account: nil,
|
||||
Account: btcjson.String("default"),
|
||||
},
|
||||
},
|
||||
{
|
||||
|
|
|
@ -4,9 +4,7 @@ import (
|
|||
"bytes"
|
||||
"fmt"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"sync"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
|
@ -249,17 +247,17 @@ func (ct *ClaimTrie) AppendBlock(temporary bool) error {
|
|||
names = append(names, expirations...)
|
||||
names = removeDuplicates(names)
|
||||
|
||||
nhns := ct.makeNameHashNext(names, false, nil)
|
||||
for nhn := range nhns {
|
||||
for _, name := range names {
|
||||
|
||||
ct.merkleTrie.Update(nhn.Name, nhn.Hash, true)
|
||||
if nhn.Next <= 0 {
|
||||
hash, next := ct.nodeManager.Hash(name)
|
||||
ct.merkleTrie.Update(name, hash, true)
|
||||
if next <= 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
newName := normalization.NormalizeIfNecessary(nhn.Name, nhn.Next)
|
||||
newName := normalization.NormalizeIfNecessary(name, next)
|
||||
updateNames = append(updateNames, newName)
|
||||
updateHeights = append(updateHeights, nhn.Next)
|
||||
updateHeights = append(updateHeights, next)
|
||||
}
|
||||
if !temporary && len(updateNames) > 0 {
|
||||
err = ct.temporalRepo.SetNodesAt(updateNames, updateHeights)
|
||||
|
@ -356,22 +354,29 @@ func (ct *ClaimTrie) ResetHeight(height int32) error {
|
|||
}
|
||||
|
||||
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())
|
||||
nhns = ct.makeNameHashNext(nil, true, interrupt)
|
||||
|
||||
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
|
||||
})
|
||||
|
||||
} else {
|
||||
ct.claimLogger = nil
|
||||
nhns = ct.makeNameHashNext(names, false, interrupt)
|
||||
for _, name := range names {
|
||||
hash, _ := ct.nodeManager.Hash(name)
|
||||
ct.merkleTrie.Update(name, hash, false)
|
||||
}
|
||||
}
|
||||
|
||||
for nhn := range nhns {
|
||||
ct.merkleTrie.Update(nhn.Name, nhn.Hash, false)
|
||||
if ct.claimLogger != nil {
|
||||
ct.claimLogger.LogName(nhn.Name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// MerkleHash returns the Merkle Hash of the claimTrie.
|
||||
|
@ -437,12 +442,6 @@ 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
|
||||
|
@ -452,53 +451,3 @@ 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.GOMAXPROCS(0)))
|
||||
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
|
||||
}
|
||||
|
|
85
claimtrie/node/cache.go
Normal file
85
claimtrie/node/cache.go
Normal file
|
@ -0,0 +1,85 @@
|
|||
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()}
|
||||
}
|
|
@ -21,6 +21,7 @@ type Manager interface {
|
|||
IterateNames(predicate func(name []byte) bool)
|
||||
Hash(name []byte) (*chainhash.Hash, int32)
|
||||
Flush() error
|
||||
ClearCache()
|
||||
}
|
||||
|
||||
type BaseManager struct {
|
||||
|
@ -30,19 +31,28 @@ 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?
|
||||
}
|
||||
|
||||
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")
|
||||
|
@ -52,10 +62,32 @@ func (nm *BaseManager) NodeAt(height int32, name []byte) (*Node, error) {
|
|||
changes = append(changes, nm.tempChanges[string(name)]...)
|
||||
}
|
||||
|
||||
n, err := nm.newNodeFromChanges(changes, height)
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
return n, nil
|
||||
}
|
||||
|
@ -66,17 +98,13 @@ func (nm *BaseManager) node(name []byte) (*Node, error) {
|
|||
return nm.NodeAt(nm.height, name)
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func (nm *BaseManager) updateFromChanges(n *Node, changes []change.Change, height int32) (bool, error) {
|
||||
|
||||
if len(changes) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
n := New()
|
||||
previous := changes[0].Height
|
||||
count := len(changes)
|
||||
if count == 0 {
|
||||
return false, nil
|
||||
}
|
||||
previous := changes[0].Height
|
||||
|
||||
for i, chg := range changes {
|
||||
if chg.Height < previous {
|
||||
|
@ -95,15 +123,37 @@ func (nm *BaseManager) newNodeFromChanges(changes []change.Change, height int32)
|
|||
delay := nm.getDelayForName(n, chg)
|
||||
err := n.ApplyChange(chg, delay)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "in apply change")
|
||||
return false, errors.Wrap(err, "in apply change")
|
||||
}
|
||||
}
|
||||
|
||||
if count <= 0 {
|
||||
return nil, nil
|
||||
// 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]
|
||||
return n.AdjustTo(lastChange.Height, height, lastChange.Name), nil
|
||||
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
|
||||
}
|
||||
|
||||
func (nm *BaseManager) AppendChange(chg change.Change) {
|
||||
|
@ -220,6 +270,7 @@ 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")
|
||||
}
|
||||
|
@ -255,6 +306,8 @@ 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) *Node {
|
||||
func (n *Node) AdjustTo(height, maxHeight int32, name []byte) {
|
||||
changed := n.handleExpiredAndActivated(height) > 0
|
||||
n.updateTakeoverHeight(height, name, changed)
|
||||
if maxHeight > height {
|
||||
|
@ -120,7 +120,6 @@ func (n *Node) AdjustTo(height, maxHeight int32, name []byte) *Node {
|
|||
height = h
|
||||
}
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func (n *Node) updateTakeoverHeight(height int32, name []byte, refindBest bool) {
|
||||
|
@ -340,3 +339,28 @@ 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
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ 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)
|
||||
}
|
||||
|
@ -110,5 +111,7 @@ func (nm *NormalizingManager) addNormalizationForkChangesIfNecessary(height int3
|
|||
|
||||
return true
|
||||
}
|
||||
|
||||
nm.Manager.ClearCache()
|
||||
nm.Manager.IterateNames(predicate)
|
||||
}
|
||||
|
|
|
@ -111,6 +111,8 @@ 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
|
||||
|
|
|
@ -9,6 +9,7 @@ import (
|
|||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/lbryio/lbcd/btcjson"
|
||||
)
|
||||
|
@ -133,6 +134,8 @@ 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)
|
||||
|
@ -141,6 +144,16 @@ 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, "[") {
|
||||
|
@ -150,7 +163,7 @@ func main() {
|
|||
err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(dst.String())
|
||||
fmt.Fprintln(output, dst.String())
|
||||
|
||||
} else if strings.HasPrefix(strResult, `"`) {
|
||||
var str string
|
||||
|
@ -159,9 +172,9 @@ func main() {
|
|||
err)
|
||||
os.Exit(1)
|
||||
}
|
||||
fmt.Println(str)
|
||||
fmt.Fprintln(output, str)
|
||||
|
||||
} else if strResult != "null" {
|
||||
fmt.Println(strResult)
|
||||
fmt.Fprintln(output, strResult)
|
||||
}
|
||||
}
|
||||
|
|
42
contrib/showminer.sh
Executable file
42
contrib/showminer.sh
Executable file
|
@ -0,0 +1,42 @@
|
|||
#! /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}
|
2
go.mod
2
go.mod
|
@ -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-rc3
|
||||
github.com/lbryio/lbcutil v1.0.202
|
||||
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-rc3 h1:J7zYnIj3iN/ndPYKqMKBukLaLM1GhCEaiaMOYIMdUCU=
|
||||
github.com/lbryio/lbcutil v1.0.202-rc3/go.mod h1:LGPtVBBzh4cFXfLFb8ginlFcbA2QwumLNFd0yk/as2o=
|
||||
github.com/lbryio/lbcutil v1.0.202 h1:L0aRMs2bdCUAicD8Xe4NmUEvevDDea3qkIpCSACnftI=
|
||||
github.com/lbryio/lbcutil v1.0.202/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=
|
||||
|
|
|
@ -13,12 +13,17 @@ 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) {
|
||||
|
@ -133,13 +138,278 @@ 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,
|
||||
}
|
||||
|
||||
|
@ -151,7 +421,8 @@ 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.
|
||||
btcdCfg := []string{"--rejectnonstd"}
|
||||
// Enable transaction index to be able to fully test GetBlockStats
|
||||
btcdCfg := []string{"--rejectnonstd", "--txindex"}
|
||||
primaryHarness, err = rpctest.New(
|
||||
&chaincfg.SimNetParams, nil, btcdCfg, "",
|
||||
)
|
||||
|
|
|
@ -16,6 +16,7 @@ import (
|
|||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/lbryio/lbcd/btcjson"
|
||||
"github.com/lbryio/lbcd/chaincfg"
|
||||
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
||||
"github.com/lbryio/lbcd/rpcclient"
|
||||
|
@ -512,6 +513,18 @@ 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
|
||||
|
|
|
@ -249,7 +249,7 @@ func GetDustThreshold(txOut *wire.TxOut) int64 {
|
|||
totalSize += 107
|
||||
}
|
||||
|
||||
return 3 * int64(totalSize)
|
||||
return 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 1/3 of the minimum free transaction relay fee.
|
||||
// coins is more than 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) * (1/3) * 1000
|
||||
// The following is equivalent to (value/totalSize) * 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 584",
|
||||
wire.TxOut{Value: 584, PkScript: pkScript},
|
||||
"38 byte public key script with value 194",
|
||||
wire.TxOut{Value: 194, PkScript: pkScript},
|
||||
1000,
|
||||
true,
|
||||
},
|
||||
{
|
||||
"38 byte public key script with value 585",
|
||||
wire.TxOut{Value: 585, PkScript: pkScript},
|
||||
"38 byte public key script with value 195",
|
||||
wire.TxOut{Value: 195, PkScript: pkScript},
|
||||
1000,
|
||||
false,
|
||||
},
|
||||
|
|
|
@ -1294,16 +1294,9 @@ 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.QueueMessageWithEncoding(gdmsg, nil, e)
|
||||
peer.QueueMessage(gdmsg, nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -6,6 +6,7 @@ package main
|
|||
|
||||
import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/lbryio/lbcd/blockchain"
|
||||
"github.com/lbryio/lbcd/chaincfg/chainhash"
|
||||
|
@ -181,6 +182,57 @@ 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.
|
||||
//
|
||||
|
|
|
@ -1,15 +1,21 @@
|
|||
# lbcd Websockets Example
|
||||
# lbcdbloknotify
|
||||
|
||||
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.
|
||||
This bridge program subscribes to lbcd's notifications over websockets using the rpcclient package.
|
||||
Users can specify supported actions upon receiving this notifications.
|
||||
|
||||
## Running the Example
|
||||
## Building(or Running) the Program
|
||||
|
||||
The first step is to clone the lbcd package:
|
||||
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:
|
||||
|
@ -29,18 +35,31 @@ $ go run . -h
|
|||
Stratum server (default "lbrypool.net:3334")
|
||||
-stratumpass string
|
||||
Stratum server password (default "password")
|
||||
-quiet
|
||||
Do not print periodic logs
|
||||
```
|
||||
|
||||
Start the program:
|
||||
Running the program:
|
||||
|
||||
```bash
|
||||
$ go run . -stratumpass <STRATUM PASSWD> -rpcuser <RPC USERNAME> -rpcpass <RPC PASSWD>
|
||||
# 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>
|
||||
|
||||
2022/01/10 23:16:21 NotifyBlocks: Registration Complete
|
||||
2022/01/10 23:16:21 Block count: 1093112
|
||||
2022/01/10 23:16:21 Current 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.
|
||||
|
|
20
rpcclient/examples/lbcdblocknotify/adapter.go
Normal file
20
rpcclient/examples/lbcdblocknotify/adapter.go
Normal file
|
@ -0,0 +1,20 @@
|
|||
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}
|
||||
}
|
172
rpcclient/examples/lbcdblocknotify/bridge.go
Normal file
172
rpcclient/examples/lbcdblocknotify/bridge.go
Normal file
|
@ -0,0 +1,172 @@
|
|||
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()
|
||||
}
|
53
rpcclient/examples/lbcdblocknotify/lbcdclient.go
Normal file
53
rpcclient/examples/lbcdblocknotify/lbcdclient.go
Normal file
|
@ -0,0 +1,53 @@
|
|||
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,133 +1,63 @@
|
|||
// 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 (
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"log"
|
||||
"net"
|
||||
"os"
|
||||
"os/exec"
|
||||
"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")
|
||||
stratum = flag.String("stratum", "", "Stratum server")
|
||||
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 onFilteredBlockConnected(height int32, header *wire.BlockHeader, txns []*lbcutil.Tx) {
|
||||
|
||||
blockHash := header.BlockHash().String()
|
||||
|
||||
log.Printf("Block connected: %v (%d) %v", blockHash, height, header.Timestamp)
|
||||
|
||||
if cmd := *run; len(cmd) != 0 {
|
||||
cmd = strings.ReplaceAll(cmd, "%s", blockHash)
|
||||
err := execCustomCommand(cmd)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: execCustomCommand: %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
if len(*stratum) > 0 && len(*stratumPass) > 0 {
|
||||
err := stratumUpdateBlock(*stratum, *stratumPass, *coinid, blockHash)
|
||||
if err != nil {
|
||||
log.Printf("ERROR: stratumUpdateBlock: %s", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
func execCustomCommand(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.Command(path, strs[1:]...)
|
||||
c.Stdout = os.Stdout
|
||||
return c.Run()
|
||||
}
|
||||
|
||||
func stratumUpdateBlock(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,%s,%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() {
|
||||
|
||||
flag.Parse()
|
||||
|
||||
ntfnHandlers := rpcclient.NotificationHandlers{
|
||||
OnFilteredBlockConnected: onFilteredBlockConnected,
|
||||
// Setup notification handler
|
||||
b := newBridge(*stratumServer, *stratumPass, *coinid)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// 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)
|
||||
}
|
||||
// Start the eventt handler.
|
||||
go b.start()
|
||||
|
||||
// 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")
|
||||
// Adaptater receives lbcd notifications, and emit events.
|
||||
adpt := adapter{b}
|
||||
|
||||
// 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)
|
||||
client := newLbcdClient(*rpcserver, *rpcuser, *rpcpass, *notls, adpt)
|
||||
|
||||
go func() {
|
||||
err := <-b.errorc
|
||||
log.Fatalf("ERROR: %s", err)
|
||||
client.Shutdown()
|
||||
}()
|
||||
|
||||
// Wait until the client either shuts down gracefully (or the user
|
||||
// terminates the process with Ctrl+C).
|
||||
|
|
56
rpcclient/examples/lbcdblocknotify/stratumclient.go
Normal file
56
rpcclient/examples/lbcdblocknotify/stratumclient.go
Normal file
|
@ -0,0 +1,56 @@
|
|||
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)
|
||||
}
|
|
@ -774,7 +774,8 @@ func (c *Client) handleSendPostMessage(jReq *jsonRequest) {
|
|||
tries := 10
|
||||
for i := 0; tries == 0 || i < tries; i++ {
|
||||
bodyReader := bytes.NewReader(jReq.marshalledJSON)
|
||||
httpReq, err := http.NewRequest("POST", url, bodyReader)
|
||||
var httpReq *http.Request
|
||||
httpReq, err = http.NewRequest("POST", url, bodyReader)
|
||||
if err != nil {
|
||||
jReq.responseChan <- &Response{result: nil, err: err}
|
||||
return
|
||||
|
@ -786,7 +787,8 @@ func (c *Client) handleSendPostMessage(jReq *jsonRequest) {
|
|||
}
|
||||
|
||||
// Configure basic access authorization.
|
||||
user, pass, err := c.config.getAuth()
|
||||
var user, pass string
|
||||
user, pass, err = c.config.getAuth()
|
||||
if err != nil {
|
||||
jReq.responseChan <- &Response{result: nil, err: err}
|
||||
return
|
||||
|
|
|
@ -355,6 +355,75 @@ 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,13 +291,18 @@ func (r FutureCreateRawTransactionResult) Receive() (*wire.MsgTx, error) {
|
|||
//
|
||||
// See CreateRawTransaction for the blocking version and more details.
|
||||
func (c *Client) CreateRawTransactionAsync(inputs []btcjson.TransactionInput,
|
||||
amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) FutureCreateRawTransactionResult {
|
||||
outputs map[btcutil.Address]interface{}, lockTime *int64) FutureCreateRawTransactionResult {
|
||||
|
||||
convertedAmts := make(map[string]float64, len(amounts))
|
||||
for addr, amount := range amounts {
|
||||
convertedAmts[addr.String()] = amount.ToBTC()
|
||||
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
|
||||
}
|
||||
cmd := btcjson.NewCreateRawTransactionCmd(inputs, convertedAmts, lockTime)
|
||||
}
|
||||
cmd := btcjson.NewCreateRawTransactionCmd(inputs, convertedData, lockTime)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -305,9 +310,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,
|
||||
amounts map[btcutil.Address]btcutil.Amount, lockTime *int64) (*wire.MsgTx, error) {
|
||||
outputs map[btcutil.Address]interface{}, lockTime *int64) (*wire.MsgTx, error) {
|
||||
|
||||
return c.CreateRawTransactionAsync(inputs, amounts, lockTime).Receive()
|
||||
return c.CreateRawTransactionAsync(inputs, outputs, lockTime).Receive()
|
||||
}
|
||||
|
||||
// FutureSendRawTransactionResult is a future promise to deliver the result
|
||||
|
|
|
@ -536,9 +536,10 @@ 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) FutureSendToAddressResult {
|
||||
func (c *Client) SendToAddressAsync(address btcutil.Address, amount btcutil.Amount,
|
||||
addrType *string) FutureSendToAddressResult {
|
||||
addr := address.EncodeAddress()
|
||||
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), nil, nil)
|
||||
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), addrType, nil, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -550,8 +551,9 @@ 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) (*chainhash.Hash, error) {
|
||||
return c.SendToAddressAsync(address, amount).Receive()
|
||||
func (c *Client) SendToAddress(address btcutil.Address, amount btcutil.Amount,
|
||||
addrType *string) (*chainhash.Hash, error) {
|
||||
return c.SendToAddressAsync(address, amount, addrType).Receive()
|
||||
}
|
||||
|
||||
// SendToAddressCommentAsync returns an instance of a type that can be used to
|
||||
|
@ -560,12 +562,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, comment,
|
||||
amount btcutil.Amount, addrType *string, comment string,
|
||||
commentTo string) FutureSendToAddressResult {
|
||||
|
||||
addr := address.EncodeAddress()
|
||||
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), &comment,
|
||||
&commentTo)
|
||||
cmd := btcjson.NewSendToAddressCmd(addr, amount.ToBTC(), addrType,
|
||||
&comment, &commentTo)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -581,9 +583,10 @@ 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, comment, commentTo string) (*chainhash.Hash, error) {
|
||||
return c.SendToAddressCommentAsync(address, amount, comment,
|
||||
commentTo).Receive()
|
||||
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()
|
||||
}
|
||||
|
||||
// FutureSendFromResult is a future promise to deliver the result of a
|
||||
|
@ -615,10 +618,11 @@ 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) FutureSendFromResult {
|
||||
func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address,
|
||||
amount btcutil.Amount, addrType *string) FutureSendFromResult {
|
||||
addr := toAddress.EncodeAddress()
|
||||
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(), nil,
|
||||
nil, nil)
|
||||
addrType, nil, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -630,8 +634,8 @@ func (c *Client) SendFromAsync(fromAccount string, toAddress btcutil.Address, am
|
|||
//
|
||||
// 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) (*chainhash.Hash, error) {
|
||||
return c.SendFromAsync(fromAccount, toAddress, amount).Receive()
|
||||
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()
|
||||
}
|
||||
|
||||
// SendFromMinConfAsync returns an instance of a type that can be used to get
|
||||
|
@ -639,10 +643,12 @@ 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) FutureSendFromResult {
|
||||
func (c *Client) SendFromMinConfAsync(fromAccount string,
|
||||
toAddress btcutil.Address, amount btcutil.Amount,
|
||||
minConfirms int, addrType *string) FutureSendFromResult {
|
||||
addr := toAddress.EncodeAddress()
|
||||
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(),
|
||||
&minConfirms, nil, nil)
|
||||
&minConfirms, addrType, nil, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -655,9 +661,10 @@ func (c *Client) SendFromMinConfAsync(fromAccount string, toAddress btcutil.Addr
|
|||
//
|
||||
// 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) (*chainhash.Hash, error) {
|
||||
func (c *Client) SendFromMinConf(fromAccount string, toAddress btcutil.Address,
|
||||
amount btcutil.Amount, minConfirms int, addrType *string) (*chainhash.Hash, error) {
|
||||
return c.SendFromMinConfAsync(fromAccount, toAddress, amount,
|
||||
minConfirms).Receive()
|
||||
minConfirms, addrType).Receive()
|
||||
}
|
||||
|
||||
// SendFromCommentAsync returns an instance of a type that can be used to get
|
||||
|
@ -667,11 +674,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,
|
||||
comment, commentTo string) FutureSendFromResult {
|
||||
addrType *string, comment, commentTo string) FutureSendFromResult {
|
||||
|
||||
addr := toAddress.EncodeAddress()
|
||||
cmd := btcjson.NewSendFromCmd(fromAccount, addr, amount.ToBTC(),
|
||||
&minConfirms, &comment, &commentTo)
|
||||
&minConfirms, addrType, &comment, &commentTo)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -687,11 +694,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,
|
||||
amount btcutil.Amount, minConfirms int, addrType *string,
|
||||
comment, commentTo string) (*chainhash.Hash, error) {
|
||||
|
||||
return c.SendFromCommentAsync(fromAccount, toAddress, amount,
|
||||
minConfirms, comment, commentTo).Receive()
|
||||
minConfirms, addrType, comment, commentTo).Receive()
|
||||
}
|
||||
|
||||
// FutureSendManyResult is a future promise to deliver the result of a
|
||||
|
@ -728,7 +735,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)
|
||||
cmd := btcjson.NewSendManyCmd(fromAccount, convertedAmounts, nil, nil, nil)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -751,14 +758,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) FutureSendManyResult {
|
||||
minConfirms int, addrType *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, nil)
|
||||
&minConfirms, nil, addrType)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -773,9 +780,10 @@ 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) (*chainhash.Hash, error) {
|
||||
minConfirms int, addrType *string) (*chainhash.Hash, error) {
|
||||
|
||||
return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms).Receive()
|
||||
return c.SendManyMinConfAsync(fromAccount, amounts, minConfirms,
|
||||
addrType).Receive()
|
||||
}
|
||||
|
||||
// SendManyCommentAsync returns an instance of a type that can be used to get
|
||||
|
@ -785,14 +793,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,
|
||||
comment string) FutureSendManyResult {
|
||||
addrType *string, 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)
|
||||
&minConfirms, &comment, addrType)
|
||||
return c.SendCmd(cmd)
|
||||
}
|
||||
|
||||
|
@ -808,10 +816,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,
|
||||
comment string) (*chainhash.Hash, error) {
|
||||
addrType *string, comment string) (*chainhash.Hash, error) {
|
||||
|
||||
return c.SendManyCommentAsync(fromAccount, amounts, minConfirms,
|
||||
comment).Receive()
|
||||
addrType, comment).Receive()
|
||||
}
|
||||
|
||||
// *************************
|
||||
|
@ -1135,8 +1143,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),
|
||||
|
@ -1147,7 +1155,7 @@ func (c *Client) GetRawChangeAddressAsync(account string) FutureGetRawChangeAddr
|
|||
// 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()
|
||||
}
|
||||
|
||||
|
@ -1226,7 +1234,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,
|
||||
|
@ -1237,7 +1245,7 @@ func (c *Client) GetAccountAddressAsync(account string) FutureGetAccountAddressR
|
|||
|
||||
// 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()
|
||||
}
|
||||
|
||||
|
@ -1317,7 +1325,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,
|
||||
|
@ -1328,7 +1336,7 @@ func (c *Client) GetAddressesByAccountAsync(account string) FutureGetAddressesBy
|
|||
|
||||
// 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()
|
||||
}
|
||||
|
||||
|
@ -1709,7 +1717,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)
|
||||
}
|
||||
|
@ -1719,7 +1727,7 @@ func (c *Client) GetReceivedByAccountAsync(account string) FutureGetReceivedByAc
|
|||
//
|
||||
// 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()
|
||||
}
|
||||
|
||||
|
@ -1728,8 +1736,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)
|
||||
}
|
||||
|
||||
|
@ -1738,7 +1746,7 @@ func (c *Client) GetReceivedByAccountMinConfAsync(account string, minConfirms in
|
|||
// 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()
|
||||
}
|
||||
|
||||
|
@ -2027,6 +2035,44 @@ 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
|
||||
// ************************
|
||||
|
|
615
rpcserver.go
615
rpcserver.go
|
@ -21,6 +21,7 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
|
@ -134,6 +135,7 @@ 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,
|
||||
|
@ -150,10 +152,11 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
|
|||
"getblockcount": handleGetBlockCount,
|
||||
"getblockhash": handleGetBlockHash,
|
||||
"getblockheader": handleGetBlockHeader,
|
||||
"getchaintips": handleGetChainTips,
|
||||
"getblockstats": handleGetBlockStats,
|
||||
"getblocktemplate": handleGetBlockTemplate,
|
||||
"getcfilter": handleGetCFilter,
|
||||
"getcfilterheader": handleGetCFilterHeader,
|
||||
"getchaintips": handleGetChainTips,
|
||||
"getconnectioncount": handleGetConnectionCount,
|
||||
"getcurrentnet": handleGetCurrentNet,
|
||||
"getdifficulty": handleGetDifficulty,
|
||||
|
@ -161,8 +164,8 @@ var rpcHandlersBeforeInit = map[string]commandHandler{
|
|||
"gethashespersec": handleGetHashesPerSec,
|
||||
"getheaders": handleGetHeaders,
|
||||
"getinfo": handleGetInfo,
|
||||
"getmempoolinfo": handleGetMempoolInfo,
|
||||
"getmempoolentry": handleGetMempoolEntry,
|
||||
"getmempoolinfo": handleGetMempoolInfo,
|
||||
"getmininginfo": handleGetMiningInfo,
|
||||
"getnettotals": handleGetNetTotals,
|
||||
"getnetworkhashps": handleGetNetworkHashPS,
|
||||
|
@ -174,11 +177,13 @@ 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,
|
||||
|
@ -225,6 +230,7 @@ var rpcAskWallet = map[string]struct{}{
|
|||
"listtransactions": {},
|
||||
"listunspent": {},
|
||||
"lockunspent": {},
|
||||
"rescanblockchain": {},
|
||||
"sendfrom": {},
|
||||
"sendmany": {},
|
||||
"sendtoaddress": {},
|
||||
|
@ -323,6 +329,15 @@ 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.
|
||||
|
@ -398,6 +413,21 @@ 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)
|
||||
|
@ -549,59 +579,92 @@ func handleCreateRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan
|
|||
// Add all transaction outputs to the transaction after performing
|
||||
// some validity checks.
|
||||
params := s.cfg.ChainParams
|
||||
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",
|
||||
}
|
||||
}
|
||||
|
||||
// Decode the provided address.
|
||||
addr, err := btcutil.DecodeAddress(encodedAddr, params)
|
||||
if err != nil {
|
||||
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.
|
||||
// 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 {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCType,
|
||||
Message: "invalid amount",
|
||||
}
|
||||
}
|
||||
|
||||
addr, err := btcutil.DecodeAddress(encodedAddr, params)
|
||||
if err != nil {
|
||||
return nil, rpcInvalidAddressOrKeyError(encodedAddr,
|
||||
"invalid address or key")
|
||||
}
|
||||
|
||||
switch addr.(type) {
|
||||
case *btcutil.AddressPubKeyHash:
|
||||
case *btcutil.AddressScriptHash:
|
||||
default:
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidAddressOrKey,
|
||||
Message: "Invalid address or key: " + addr.String(),
|
||||
}
|
||||
return nil, rpcInvalidAddressOrKeyError(addr.String(),
|
||||
"invalid address or key")
|
||||
}
|
||||
if !addr.IsForNet(params) {
|
||||
return nil, &btcjson.RPCError{
|
||||
Code: btcjson.ErrRPCInvalidAddressOrKey,
|
||||
Message: "Invalid address: " + encodedAddr +
|
||||
" is for the wrong network",
|
||||
}
|
||||
return nil, rpcInvalidAddressOrKeyError(addr.String(),
|
||||
"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)
|
||||
}
|
||||
|
||||
txOut := wire.NewTxOut(int64(satoshi), pkScript)
|
||||
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
|
||||
}
|
||||
mtx.AddTxOut(txOut)
|
||||
}
|
||||
|
||||
|
@ -1192,7 +1255,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",
|
||||
Message: "Block not found: " + err.Error(),
|
||||
}
|
||||
}
|
||||
// If verbosity is 0, return the serialized block as a hex encoded string.
|
||||
|
@ -1517,6 +1580,405 @@ 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 {
|
||||
|
@ -1906,7 +2368,6 @@ func (state *gbtWorkState) blockTemplateResult(useCoinbaseValue bool, submitOld
|
|||
|
||||
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.
|
||||
|
@ -1939,6 +2400,9 @@ 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
|
||||
}
|
||||
|
||||
|
@ -3146,6 +3610,24 @@ 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_
|
||||
|
@ -3749,6 +4231,59 @@ func handleSendRawTransaction(s *rpcServer, cmd interface{}, closeChan <-chan st
|
|||
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)
|
||||
|
@ -4806,6 +5341,18 @@ 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,13 +49,16 @@ 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-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-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-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",
|
||||
|
@ -197,6 +200,43 @@ 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",
|
||||
|
@ -636,6 +676,16 @@ 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",
|
||||
|
@ -664,6 +714,13 @@ 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",
|
||||
|
@ -880,6 +937,7 @@ 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)},
|
||||
|
@ -892,11 +950,12 @@ 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)},
|
||||
|
@ -918,13 +977,15 @@ 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)},
|
||||
|
|
66
server.go
66
server.go
|
@ -156,13 +156,18 @@ 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]time.Time
|
||||
banned map[string]bannedPeriod
|
||||
outboundGroups map[string]int
|
||||
}
|
||||
|
||||
|
@ -1656,10 +1661,10 @@ func (s *server) handleAddPeerMsg(state *peerState, sp *serverPeer) bool {
|
|||
sp.Disconnect()
|
||||
return false
|
||||
}
|
||||
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))
|
||||
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))
|
||||
sp.Disconnect()
|
||||
return false
|
||||
}
|
||||
|
@ -1781,7 +1786,12 @@ func (s *server) handleBanPeerMsg(state *peerState, sp *serverPeer) {
|
|||
direction := directionString(sp.Inbound())
|
||||
srvrLog.Infof("Banned peer %s (%s) for %v", host, direction,
|
||||
cfg.BanDuration)
|
||||
state.banned[host] = time.Now().Add(cfg.BanDuration)
|
||||
|
||||
since := time.Now()
|
||||
state.banned[host] = bannedPeriod{
|
||||
since: since,
|
||||
until: since.Add(cfg.BanDuration),
|
||||
}
|
||||
}
|
||||
|
||||
// handleRelayInvMsg deals with relaying inventory to peers that are not already
|
||||
|
@ -1876,6 +1886,25 @@ 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
|
||||
|
@ -1924,6 +1953,29 @@ 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.
|
||||
|
@ -2161,7 +2213,7 @@ func (s *server) peerHandler() {
|
|||
inboundPeers: make(map[int32]*serverPeer),
|
||||
persistentPeers: make(map[int32]*serverPeer),
|
||||
outboundPeers: make(map[int32]*serverPeer),
|
||||
banned: make(map[string]time.Time),
|
||||
banned: make(map[string]bannedPeriod),
|
||||
outboundGroups: make(map[string]int),
|
||||
}
|
||||
|
||||
|
|
|
@ -33,6 +33,9 @@ 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 {
|
||||
|
@ -98,11 +101,15 @@ func ExtractClaimScript(script []byte) (*ClaimScript, error) {
|
|||
|
||||
if !tokenizer.Next() || tokenizer.Opcode() != OP_2DROP ||
|
||||
!tokenizer.Next() || tokenizer.Opcode() != OP_DROP {
|
||||
str := fmt.Sprintf("expect OP_2DROP OP_DROP")
|
||||
return nil, claimScriptError(ErrInvalidClaimNameScript, str)
|
||||
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)
|
||||
return nil, claimScriptError(ErrInvalidClaimNameScript, str)
|
||||
}
|
||||
|
||||
return &cs, nil
|
||||
|
||||
case OP_SUPPORTCLAIM:
|
||||
|
@ -128,8 +135,7 @@ 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 {
|
||||
str := fmt.Sprintf("expect OP_2DROP OP_DROP")
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, str)
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, "expect OP_2DROP OP_DROP")
|
||||
}
|
||||
|
||||
case len(tokenizer.Data()) != 0:
|
||||
|
@ -138,19 +144,21 @@ func ExtractClaimScript(script []byte) (*ClaimScript, error) {
|
|||
cs.Value = tokenizer.Data()
|
||||
if !tokenizer.Next() || tokenizer.Opcode() != OP_2DROP ||
|
||||
!tokenizer.Next() || tokenizer.Opcode() != OP_2DROP {
|
||||
str := fmt.Sprintf("expect OP_2DROP OP_2DROP")
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, str)
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, "expect OP_2DROP OP_2DROP")
|
||||
}
|
||||
default:
|
||||
str := fmt.Sprintf("expect OP_2DROP OP_DROP")
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, str)
|
||||
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)
|
||||
return nil, claimScriptError(ErrInvalidClaimSupportScript, str)
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -177,6 +185,11 @@ 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:
|
||||
|
@ -193,7 +206,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)
|
||||
|
@ -205,10 +218,11 @@ func AllClaimsAreSane(script []byte, enforceSoftFork bool) error {
|
|||
}
|
||||
if enforceSoftFork {
|
||||
if !utf8.Valid(cs.Name) {
|
||||
return fmt.Errorf("claim name is not valid UTF-8")
|
||||
return claimScriptError(ErrInvalidClaimName, "claim name is not valid UTF-8")
|
||||
}
|
||||
if bytes.ContainsAny(cs.Name, illegalChars) {
|
||||
return fmt.Errorf("claim name has illegal chars; it should not contain any of these: %s", illegalChars)
|
||||
str := fmt.Sprintf("claim name has illegal chars; it should not contain any of these: %s", illegalChars)
|
||||
return claimScriptError(ErrInvalidClaimName, str)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue