Skip to content

Commit

Permalink
multi: add notifywork websocket rpc.
Browse files Browse the repository at this point in the history
  • Loading branch information
dnldd committed Nov 19, 2018
1 parent 2bde01d commit 06c8a62
Show file tree
Hide file tree
Showing 9 changed files with 348 additions and 17 deletions.
20 changes: 20 additions & 0 deletions dcrjson/chainsvrwscmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ func NewNotifyBlocksCmd() *NotifyBlocksCmd {
return &NotifyBlocksCmd{}
}

// NotifyWorkCmd defines the notifywork JSON-RPC command.
type NotifyWorkCmd struct{}

// NewNotifyWorkCmd returns a new instance which can be used to issue a
// notifywork JSON-RPC command.
func NewNotifyWorkCmd() *NotifyWorkCmd {
return &NotifyWorkCmd{}
}

// NotifyWinningTicketsCmd is a type handling custom marshaling and
// unmarshaling of notifywinningtickets JSON websocket extension
// commands.
Expand Down Expand Up @@ -111,6 +120,15 @@ func NewStopNotifyBlocksCmd() *StopNotifyBlocksCmd {
return &StopNotifyBlocksCmd{}
}

// StopNotifyWorkCmd defines the stopnotifywork JSON-RPC command.
type StopNotifyWorkCmd struct{}

// NewStopNotifyWorkCmd returns a new instance which can be used to issue a
// stopnotifyblocks JSON-RPC command.
func NewStopNotifyWorkCmd() *StopNotifyWorkCmd {
return &StopNotifyWorkCmd{}
}

// NotifyNewTransactionsCmd defines the notifynewtransactions JSON-RPC command.
type NotifyNewTransactionsCmd struct {
Verbose *bool `jsonrpcdefault:"false"`
Expand Down Expand Up @@ -168,6 +186,7 @@ func init() {
MustRegisterCmd("authenticate", (*AuthenticateCmd)(nil), flags)
MustRegisterCmd("loadtxfilter", (*LoadTxFilterCmd)(nil), flags)
MustRegisterCmd("notifyblocks", (*NotifyBlocksCmd)(nil), flags)
MustRegisterCmd("notifywork", (*NotifyWorkCmd)(nil), flags)
MustRegisterCmd("notifynewtransactions", (*NotifyNewTransactionsCmd)(nil), flags)
MustRegisterCmd("notifynewtickets", (*NotifyNewTicketsCmd)(nil), flags)
MustRegisterCmd("notifyspentandmissedtickets",
Expand All @@ -178,6 +197,7 @@ func init() {
(*NotifyWinningTicketsCmd)(nil), flags)
MustRegisterCmd("session", (*SessionCmd)(nil), flags)
MustRegisterCmd("stopnotifyblocks", (*StopNotifyBlocksCmd)(nil), flags)
MustRegisterCmd("stopnotifywork", (*StopNotifyWorkCmd)(nil), flags)
MustRegisterCmd("stopnotifynewtransactions", (*StopNotifyNewTransactionsCmd)(nil), flags)
MustRegisterCmd("rescan", (*RescanCmd)(nil), flags)
}
22 changes: 22 additions & 0 deletions dcrjson/chainsvrwscmds_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,17 @@ func TestChainSvrWsCmds(t *testing.T) {
marshalled: `{"jsonrpc":"1.0","method":"notifyblocks","params":[],"id":1}`,
unmarshalled: &NotifyBlocksCmd{},
},
{
name: "notifywork",
newCmd: func() (interface{}, error) {
return NewCmd("notifywork")
},
staticCmd: func() interface{} {
return NewNotifyWorkCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"notifywork","params":[],"id":1}`,
unmarshalled: &NotifyWorkCmd{},
},
{
name: "stopnotifyblocks",
newCmd: func() (interface{}, error) {
Expand All @@ -105,6 +116,17 @@ func TestChainSvrWsCmds(t *testing.T) {
marshalled: `{"jsonrpc":"1.0","method":"stopnotifyblocks","params":[],"id":1}`,
unmarshalled: &StopNotifyBlocksCmd{},
},
{
name: "stopnotifywork",
newCmd: func() (interface{}, error) {
return NewCmd("stopnotifywork")
},
staticCmd: func() interface{} {
return NewStopNotifyWorkCmd()
},
marshalled: `{"jsonrpc":"1.0","method":"stopnotifywork","params":[],"id":1}`,
unmarshalled: &StopNotifyWorkCmd{},
},
{
name: "notifynewtransactions",
newCmd: func() (interface{}, error) {
Expand Down
20 changes: 20 additions & 0 deletions dcrjson/chainsvrwsntfns.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const (
// the chain server that a block has been disconnected.
BlockDisconnectedNtfnMethod = "blockdisconnected"

// WorkNtfnMethod is the method used for notifications from
// the chain server that a new block template has been generated.
WorkNtfnMethod = "work"

// ReorganizationNtfnMethod is the method used for notifications that the
// block chain is in the process of a reorganization.
ReorganizationNtfnMethod = "reorganization"
Expand Down Expand Up @@ -65,6 +69,21 @@ func NewBlockDisconnectedNtfn(header string) *BlockDisconnectedNtfn {
}
}

// WorkNtfn defines the newwork JSON-RPC notification.
type WorkNtfn struct {
Data string `json:"data"`
Target string `json:"target"`
}

// NewWorkNtfn returns a new instance which can be used to issue a
// newwork JSON-RPC notification.
func NewWorkNtfn(data string, target string) *WorkNtfn {
return &WorkNtfn{
Data: data,
Target: target,
}
}

// ReorganizationNtfn defines the reorganization JSON-RPC notification.
type ReorganizationNtfn struct {
OldHash string `json:"oldhash"`
Expand Down Expand Up @@ -132,6 +151,7 @@ func init() {

MustRegisterCmd(BlockConnectedNtfnMethod, (*BlockConnectedNtfn)(nil), flags)
MustRegisterCmd(BlockDisconnectedNtfnMethod, (*BlockDisconnectedNtfn)(nil), flags)
MustRegisterCmd(WorkNtfnMethod, (*WorkNtfn)(nil), flags)
MustRegisterCmd(ReorganizationNtfnMethod, (*ReorganizationNtfn)(nil), flags)
MustRegisterCmd(TxAcceptedNtfnMethod, (*TxAcceptedNtfn)(nil), flags)
MustRegisterCmd(TxAcceptedVerboseNtfnMethod, (*TxAcceptedVerboseNtfn)(nil), flags)
Expand Down
79 changes: 62 additions & 17 deletions docs/json_rpc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,8 @@ the method name for further details such as parameter and return information.
|---|---|
|Method|getwork|
|Parameters|1. `data`: `(string, optional)` The hex|
|Description|Returns information about a transaction given its hash.|
|Description|Returns either the mining work if data is not specified or if the
the submitted work was accepted.|
|Notes|Since dcrd does not have the wallet integrated to provide payment addresses, dcrd must be configured via the `--miningaddr` option to provide which payment addresses to pay created blocks to for this RPC to function.
|Returns (data not specified)|`(json object)`<br />`data`: (string) hex-encoded block data<br />`hash1`: `(string)` (DEPRECATED) hex-encoded formatted hash buffer <br />`midstate`: `(string)` (DEPRECATED) hex-encoded precomputed hash state after hashing first half of the data <br />`target`: `(string)` the hex-encoded little-endian hash target<br /><br />`{"data": "hex", "hash1": "hex", "midstate": "hex", "target": "hex"}`|
|Returns (data specified)|`true` or `false` (boolean)|
Expand Down Expand Up @@ -706,16 +707,18 @@ user. Click the method name for further details such as parameter and return in
|---|------|-----------|-------------|
|1|[authenticate](#authenticate)|Authenticate the connection against the username and passphrase configured for the RPC server.<br /><br />NOTE: This is only required if an HTTP Authorization header is not being used.|None|
|2|[notifyblocks](#notifyblocks)|Send notifications when a block is connected or disconnected from the best chain.|[blockconnected](#blockconnected) and [blockdisconnected](#blockdisconnected)|
|3|[stopnotifyblocks](#stopnotifyblocks)|Cancel registered notifications for whenever a block is connected or disconnected from the main (best) chain. |None|
|4|[notifyreceived](#notifyreceived)|Send notifications when a txout spends to an address.|[recvtx](#recvtx) and [redeemingtx](#redeemingtx)|
|5|[stopnotifyreceived](#stopnotifyreceived)|Cancel registered notifications for when a txout spends to any of the passed addresses.|None|
|6|[notifyspent](#notifyspent)|Send notification when a txout is spent.|[redeemingtx](#redeemingtx)|
|7|[stopnotifyspent](#stopnotifyspent)|Cancel registered spending notifications for each passed outpoint.|None|
|8|[loadtxfilter](#loadtxfilter)|Load, add to, or reload a websocket client's transaction filter for mempool transactions, new blocks and rescanblocks.|[relevanttxaccepted](#relevanttxaccepted)|
|9|[rescan](#rescan)|Rescan block chain for transactions to addresses and spent transaction outpoints.|[recvtx](#recvtx), [redeemingtx](#redeemingtx), [rescanprogress](#rescanprogress), and [rescanfinished](#rescanfinished) |
|10|[notifynewtransactions](#notifynewtransactions)|Send notifications for all new transactions as they are accepted into the mempool.|[txaccepted](#txaccepted) or [txacceptedverbose](#txacceptedverbose)|
|11|[stopnotifynewtransactions](#stopnotifynewtransactions)|Stop sending either a txaccepted or a txacceptedverbose notification when a new transaction is accepted into the mempool.|None|
|12|[session](#session)|Return details regarding a websocket client's current connection.|None|
|3|[notifywork](#notifywork)|Send notifications when a new block template is generated.|[newwork](#nework)|
|4|[stopnotifyblocks](#stopnotifyblocks)|Cancel registered notifications for whenever a block is connected or disconnected from the main (best) chain. |None|
|5|[stopnotifywork](#stopnotifywork)|Cancel registered notifications for whenever when a new block template is generated for mining. |None|
|6|[notifyreceived](#notifyreceived)|Send notifications when a txout spends to an address.|[recvtx](#recvtx) and [redeemingtx](#redeemingtx)|
|7|[stopnotifyreceived](#stopnotifyreceived)|Cancel registered notifications for when a txout spends to any of the passed addresses.|None|
|8|[notifyspent](#notifyspent)|Send notification when a txout is spent.|[redeemingtx](#redeemingtx)|
|9|[stopnotifyspent](#stopnotifyspent)|Cancel registered spending notifications for each passed outpoint.|None|
|10|[loadtxfilter](#loadtxfilter)|Load, add to, or reload a websocket client's transaction filter for mempool transactions, new blocks and rescanblocks.|[relevanttxaccepted](#relevanttxaccepted)|
|11|[rescan](#rescan)|Rescan block chain for transactions to addresses and spent transaction outpoints.|[recvtx](#recvtx), [redeemingtx](#redeemingtx), [rescanprogress](#rescanprogress), and [rescanfinished](#rescanfinished) |
|12|[notifynewtransactions](#notifynewtransactions)|Send notifications for all new transactions as they are accepted into the mempool.|[txaccepted](#txaccepted) or [txacceptedverbose](#txacceptedverbose)|
|13|[stopnotifynewtransactions](#stopnotifynewtransactions)|Stop sending either a txaccepted or a txacceptedverbose notification when a new transaction is accepted into the mempool.|None|
|14|[session](#session)|Return details regarding a websocket client's current connection.|None|
<a name="WSExtMethodDetails" />

**6.2 Method Details**<br />
Expand Down Expand Up @@ -744,6 +747,20 @@ user. Click the method name for further details such as parameter and return in
[Return to Overview](#WSMethodOverview)<br />

***

<a name="notifywork"/>

| | |
|---|---|
|Method|notifyblocks|
|Notifications|[nework](#newwork)|
|Parameters|None|
|Description|Request notifications for whenever a new block template is generated for mining.|
|Returns|Nothing|
[Return to Overview](#WSMethodOverview)<br />

***

<a name="stopnotifyblocks"/>

| | |
Expand All @@ -757,6 +774,19 @@ user. Click the method name for further details such as parameter and return in

***

<a name="stopnotifywork"/>

| | |
|---|---|
|Method|stopnotifywork|
|Notifications|None|
|Parameters|None|
|Description|Cancel sending notifications for whenever a new block template is generated for mining.|
|Returns|Nothing|
[Return to Overview](#WSMethodOverview)<br />

***

<a name="notifyreceived"/>

| | |
Expand Down Expand Up @@ -892,12 +922,13 @@ The following is an overview of the JSON-RPC notifications used for Websocket co
|---|------|-----------|-------|
|1|[blockconnected](#blockconnected)|Block connected to the main chain.|[notifyblocks](#notifyblocks)|
|2|[blockdisconnected](#blockdisconnected)|Block disconnected from the main chain.|[notifyblocks](#notifyblocks)|
|3|[recvtx](#recvtx)|Processed a transaction output spending to a wallet address.|[notifyreceived](#notifyreceived) and [rescan](#rescan)|
|4|[redeemingtx](#redeemingtx)|Processed a transaction that spends a registered outpoint.|[notifyspent](#notifyspent) and [rescan](#rescan)|
|5|[txaccepted](#txaccepted)|Received a new transaction after requesting simple notifications of all new transactions accepted into the mempool.|[notifynewtransactions](#notifynewtransactions)|
|6|[txacceptedverbose](#txacceptedverbose)|Received a new transaction after requesting verbose notifications of all new transactions accepted into the mempool.|[notifynewtransactions](#notifynewtransactions)|
|7|[rescanprogress](#rescanprogress)|A rescan operation that is underway has made progress.|[rescan](#rescan)|
|8|[rescanfinished](#rescanfinished)|A rescan operation has completed.|[rescan](#rescan)|
|3|[work](#work)|New block template generated.|[work](#work)|
|4|[recvtx](#recvtx)|Processed a transaction output spending to a wallet address.|[notifyreceived](#notifyreceived) and [rescan](#rescan)|
|5|[redeemingtx](#redeemingtx)|Processed a transaction that spends a registered outpoint.|[notifyspent](#notifyspent) and [rescan](#rescan)|
|6|[txaccepted](#txaccepted)|Received a new transaction after requesting simple notifications of all new transactions accepted into the mempool.|[notifynewtransactions](#notifynewtransactions)|
|7|[txacceptedverbose](#txacceptedverbose)|Received a new transaction after requesting verbose notifications of all new transactions accepted into the mempool.|[notifynewtransactions](#notifynewtransactions)|
|8|[rescanprogress](#rescanprogress)|A rescan operation that is underway has made progress.|[rescan](#rescan)|
|9|[rescanfinished](#rescanfinished)|A rescan operation has completed.|[rescan](#rescan)|

<a name="NotificationDetails" />

Expand Down Expand Up @@ -927,6 +958,20 @@ The following is an overview of the JSON-RPC notifications used for Websocket co
|Example|Example blockdisconnected notification for mainnet block 280330 (newlines added for readability):<br /><br />`{"jsonrpc": "1.0","method": "blockdisconnected", "params":["000000000000000004cbdfe387f4df44b914e464ca79838a8ab777b3214dbffd", 280330,1389636265],"id": null}`|
[Return to Overview](#NotificationOverview)<br />


***

<a name="work"/>

| | |
|---|---|
|Method|work|
|Request|[notifywork](#notifywork)|
|Parameters|1. `data`: `(string)` hex-encoded block data.<br />2. `target`: `(string)` the hex-encoded little-endian hash target.|
|Description|Notifies when a new block template has been generated. Notification is sent to all connected clients.|
|Example|Example work notification for testnet (newlines added for readability):<br /><br />`{"jsonrpc":"1.0","method":"work","params":["06000000c9ee7cd5b14b72e41195891a9617f80049eaac06a0dfe063479523060000000088087dbb59923da4faf632a29777805a7632efbd680b86f3c6cbbe3fddf905d45045d61784ad0810749cc045d03a7b9d5e8f6b2ed3ed767c9827f1dad4925771010082ebc7097cf5050007003b1400003b84061cff2d333700000000322400003f10000070b67b5b3bc400803b057900c5575400000000000000000000000000000000000000000000000000060000008000000100000000000005a0","000000000000000000000000000000000000000000000000003b840600000000"],"id":null}`|
[Return to Overview](#NotificationOverview)<br />

***

<a name="recvtx"/>
Expand Down
8 changes: 8 additions & 0 deletions rpcclient/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,14 @@ func (c *Client) reregisterNtfns() error {
}
}

// Reregister notifyblocks if needed.
if stateCopy.notifyWork {
log.Debugf("Reregistering [notifywork]")
if err := c.NotifyWork(); err != nil {
return err
}
}

// Reregister notifywinningtickets if needed.
if stateCopy.notifyWinningTickets {
log.Debugf("Reregistering [notifywinningtickets]")
Expand Down
Loading

0 comments on commit 06c8a62

Please sign in to comment.