mirror of
https://github.com/btcsuite/btcd.git
synced 2025-03-15 04:11:37 +01:00
Merge f9d94bc301
into c7191d2913
This commit is contained in:
commit
3ee4e11c22
5 changed files with 135 additions and 0 deletions
|
@ -225,6 +225,15 @@ func NewGetBlockChainInfoCmd() *GetBlockChainInfoCmd {
|
||||||
return &GetBlockChainInfoCmd{}
|
return &GetBlockChainInfoCmd{}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetIndexInfoCmd defines the getindexinfo JSON-RPC command.
|
||||||
|
type GetIndexInfoCmd struct{}
|
||||||
|
|
||||||
|
// NewGetIndexInfoCmd returns a new instance which can be used to issue a
|
||||||
|
// getindexinfo JSON-RPC command.
|
||||||
|
func NewGetIndexInfoCmd() *GetIndexInfoCmd {
|
||||||
|
return &GetIndexInfoCmd{}
|
||||||
|
}
|
||||||
|
|
||||||
// GetBlockCountCmd defines the getblockcount JSON-RPC command.
|
// GetBlockCountCmd defines the getblockcount JSON-RPC command.
|
||||||
type GetBlockCountCmd struct{}
|
type GetBlockCountCmd struct{}
|
||||||
|
|
||||||
|
@ -1131,6 +1140,7 @@ func init() {
|
||||||
MustRegisterCmd("getgenerate", (*GetGenerateCmd)(nil), flags)
|
MustRegisterCmd("getgenerate", (*GetGenerateCmd)(nil), flags)
|
||||||
MustRegisterCmd("gethashespersec", (*GetHashesPerSecCmd)(nil), flags)
|
MustRegisterCmd("gethashespersec", (*GetHashesPerSecCmd)(nil), flags)
|
||||||
MustRegisterCmd("getinfo", (*GetInfoCmd)(nil), flags)
|
MustRegisterCmd("getinfo", (*GetInfoCmd)(nil), flags)
|
||||||
|
MustRegisterCmd("getindexinfo", (*GetIndexInfoCmd)(nil), flags)
|
||||||
MustRegisterCmd("getmempoolentry", (*GetMempoolEntryCmd)(nil), flags)
|
MustRegisterCmd("getmempoolentry", (*GetMempoolEntryCmd)(nil), flags)
|
||||||
MustRegisterCmd("getmempoolinfo", (*GetMempoolInfoCmd)(nil), flags)
|
MustRegisterCmd("getmempoolinfo", (*GetMempoolInfoCmd)(nil), flags)
|
||||||
MustRegisterCmd("getmininginfo", (*GetMiningInfoCmd)(nil), flags)
|
MustRegisterCmd("getmininginfo", (*GetMiningInfoCmd)(nil), flags)
|
||||||
|
|
|
@ -369,6 +369,17 @@ func TestChainSvrCmds(t *testing.T) {
|
||||||
marshalled: `{"jsonrpc":"1.0","method":"getblockchaininfo","params":[],"id":1}`,
|
marshalled: `{"jsonrpc":"1.0","method":"getblockchaininfo","params":[],"id":1}`,
|
||||||
unmarshalled: &btcjson.GetBlockChainInfoCmd{},
|
unmarshalled: &btcjson.GetBlockChainInfoCmd{},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "getindexinfo",
|
||||||
|
newCmd: func() (interface{}, error) {
|
||||||
|
return btcjson.NewCmd("getindexinfo")
|
||||||
|
},
|
||||||
|
staticCmd: func() interface{} {
|
||||||
|
return btcjson.NewGetIndexInfoCmd()
|
||||||
|
},
|
||||||
|
marshalled: `{"jsonrpc":"1.0","method":"getindexinfo","params":[],"id":1}`,
|
||||||
|
unmarshalled: &btcjson.GetIndexInfoCmd{},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "getblockcount",
|
name: "getblockcount",
|
||||||
newCmd: func() (interface{}, error) {
|
newCmd: func() (interface{}, error) {
|
||||||
|
|
|
@ -242,6 +242,17 @@ type GetBlockChainInfoResult struct {
|
||||||
*UnifiedSoftForks
|
*UnifiedSoftForks
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// GetIndexInfoResult models the data returned from the getindexinfo command.
|
||||||
|
type GetIndexInfoResult struct {
|
||||||
|
*TxIndex `json:"txindex,omitempty"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// TxIndex describes the current txindex results from getindexinfo.
|
||||||
|
type TxIndex struct {
|
||||||
|
Synced bool `json:"synced"`
|
||||||
|
BestBlockHeight int32 `json:"best_block_height"`
|
||||||
|
}
|
||||||
|
|
||||||
// GetBlockFilterResult models the data returned from the getblockfilter
|
// GetBlockFilterResult models the data returned from the getblockfilter
|
||||||
// command.
|
// command.
|
||||||
type GetBlockFilterResult struct {
|
type GetBlockFilterResult struct {
|
||||||
|
|
|
@ -507,6 +507,54 @@ func (c *Client) GetBlockChainInfo() (*btcjson.GetBlockChainInfoResult, error) {
|
||||||
return c.GetBlockChainInfoAsync().Receive()
|
return c.GetBlockChainInfoAsync().Receive()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FutureGetIndexInfoResult is a promise to deliver the result of a
|
||||||
|
// GetIndexInfoAsync RPC invocation (or an applicable error).
|
||||||
|
type FutureGetIndexInfoResult struct {
|
||||||
|
client *Client
|
||||||
|
Response chan *Response
|
||||||
|
}
|
||||||
|
|
||||||
|
// unmarshalPartialGetIndexInfoResult unmarshals the response into an
|
||||||
|
// instance of GetIndexInfoResult.
|
||||||
|
func unmarshalPartialGetIndexInfoResult(res []byte) (*btcjson.GetIndexInfoResult, error) {
|
||||||
|
var indexInfo btcjson.GetIndexInfoResult
|
||||||
|
if err := json.Unmarshal(res, &indexInfo); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &indexInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Receive waits for the Response promised by the future and returns txindex info
|
||||||
|
// result provided by the server.
|
||||||
|
func (r FutureGetIndexInfoResult) Receive() (*btcjson.GetIndexInfoResult, error) {
|
||||||
|
res, err := ReceiveFuture(r.Response)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
indexInfo, err := unmarshalPartialGetIndexInfoResult(res)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return indexInfo, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetIndexInfoAsync 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) GetIndexInfoAsync() FutureGetIndexInfoResult {
|
||||||
|
cmd := btcjson.NewGetIndexInfoCmd()
|
||||||
|
return FutureGetIndexInfoResult{
|
||||||
|
client: c,
|
||||||
|
Response: c.SendCmd(cmd),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetIndexInfo returns information related to the txindex state.
|
||||||
|
func (c *Client) GetIndexInfo() (*btcjson.GetIndexInfoResult, error) {
|
||||||
|
return c.GetIndexInfoAsync().Receive()
|
||||||
|
}
|
||||||
|
|
||||||
// FutureGetBlockFilterResult is a future promise to deliver the result of a
|
// FutureGetBlockFilterResult is a future promise to deliver the result of a
|
||||||
// GetBlockFilterAsync RPC invocation (or an applicable error).
|
// GetBlockFilterAsync RPC invocation (or an applicable error).
|
||||||
type FutureGetBlockFilterResult chan *Response
|
type FutureGetBlockFilterResult chan *Response
|
||||||
|
|
|
@ -103,6 +103,61 @@ func TestUnmarshalGetBlockChainInfoResultSoftForks(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestUnmarshalGetIndexInfoResult ensures that the Txindex of GetIndexInfoResult
|
||||||
|
// are properly unmarshaled.
|
||||||
|
func TestUnmarshalGetIndexInfoResult(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
res []byte
|
||||||
|
enabled bool
|
||||||
|
blockHeight int32
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "txindex synced and best block height valid",
|
||||||
|
res: []byte(`{"txindex": {"synced": true, "best_block_height": 3522710}}`),
|
||||||
|
enabled: true,
|
||||||
|
blockHeight: 3522710,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "txindex not enabled",
|
||||||
|
res: []byte(`{}`),
|
||||||
|
enabled: false,
|
||||||
|
blockHeight: 0,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
success := t.Run(test.name, func(t *testing.T) {
|
||||||
|
// We'll start by unmarshalling the JSON into a struct.
|
||||||
|
info, err := unmarshalPartialGetIndexInfoResult(test.res)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if test.enabled {
|
||||||
|
if info.TxIndex == nil {
|
||||||
|
t.Fatalf("unable to unmarshal txindex: %v", err)
|
||||||
|
}
|
||||||
|
if info.TxIndex.Synced == false {
|
||||||
|
t.Fatalf("expected TxIndex.Synced to be true")
|
||||||
|
}
|
||||||
|
if info.TxIndex.BestBlockHeight != test.blockHeight {
|
||||||
|
t.Fatalf("expected TxIndex.BestBlockHeight to be equal")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if info.TxIndex != nil {
|
||||||
|
t.Fatalf("expected TxIndex to be empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
if !success {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestFutureGetBlockCountResultReceiveErrors(t *testing.T) {
|
func TestFutureGetBlockCountResultReceiveErrors(t *testing.T) {
|
||||||
responseChan := FutureGetBlockCountResult(make(chan *Response))
|
responseChan := FutureGetBlockCountResult(make(chan *Response))
|
||||||
response := Response{
|
response := Response{
|
||||||
|
|
Loading…
Add table
Reference in a new issue