Add timestamps to block(dis)connected notifications.

This commit is contained in:
Josh Rickmar 2015-06-18 11:49:45 -04:00
parent 2ceb6418e7
commit 09ce6f94d3
4 changed files with 20 additions and 14 deletions

View file

@ -50,14 +50,16 @@ const (
type BlockConnectedNtfn struct { type BlockConnectedNtfn struct {
Hash string Hash string
Height int32 Height int32
Time int64
} }
// NewBlockConnectedNtfn returns a new instance which can be used to issue a // NewBlockConnectedNtfn returns a new instance which can be used to issue a
// blockconnected JSON-RPC notification. // blockconnected JSON-RPC notification.
func NewBlockConnectedNtfn(hash string, height int32) *BlockConnectedNtfn { func NewBlockConnectedNtfn(hash string, height int32, time int64) *BlockConnectedNtfn {
return &BlockConnectedNtfn{ return &BlockConnectedNtfn{
Hash: hash, Hash: hash,
Height: height, Height: height,
Time: time,
} }
} }
@ -65,14 +67,16 @@ func NewBlockConnectedNtfn(hash string, height int32) *BlockConnectedNtfn {
type BlockDisconnectedNtfn struct { type BlockDisconnectedNtfn struct {
Hash string Hash string
Height int32 Height int32
Time int64
} }
// NewBlockDisconnectedNtfn returns a new instance which can be used to issue a // NewBlockDisconnectedNtfn returns a new instance which can be used to issue a
// blockdisconnected JSON-RPC notification. // blockdisconnected JSON-RPC notification.
func NewBlockDisconnectedNtfn(hash string, height int32) *BlockDisconnectedNtfn { func NewBlockDisconnectedNtfn(hash string, height int32, time int64) *BlockDisconnectedNtfn {
return &BlockDisconnectedNtfn{ return &BlockDisconnectedNtfn{
Hash: hash, Hash: hash,
Height: height, Height: height,
Time: time,
} }
} }

View file

@ -31,29 +31,31 @@ func TestChainSvrWsNtfns(t *testing.T) {
{ {
name: "blockconnected", name: "blockconnected",
newNtfn: func() (interface{}, error) { newNtfn: func() (interface{}, error) {
return btcjson.NewCmd("blockconnected", "123", 100000) return btcjson.NewCmd("blockconnected", "123", 100000, 123456789)
}, },
staticNtfn: func() interface{} { staticNtfn: func() interface{} {
return btcjson.NewBlockConnectedNtfn("123", 100000) return btcjson.NewBlockConnectedNtfn("123", 100000, 123456789)
}, },
marshalled: `{"jsonrpc":"1.0","method":"blockconnected","params":["123",100000],"id":null}`, marshalled: `{"jsonrpc":"1.0","method":"blockconnected","params":["123",100000,123456789],"id":null}`,
unmarshalled: &btcjson.BlockConnectedNtfn{ unmarshalled: &btcjson.BlockConnectedNtfn{
Hash: "123", Hash: "123",
Height: 100000, Height: 100000,
Time: 123456789,
}, },
}, },
{ {
name: "blockdisconnected", name: "blockdisconnected",
newNtfn: func() (interface{}, error) { newNtfn: func() (interface{}, error) {
return btcjson.NewCmd("blockdisconnected", "123", 100000) return btcjson.NewCmd("blockdisconnected", "123", 100000, 123456789)
}, },
staticNtfn: func() interface{} { staticNtfn: func() interface{} {
return btcjson.NewBlockDisconnectedNtfn("123", 100000) return btcjson.NewBlockDisconnectedNtfn("123", 100000, 123456789)
}, },
marshalled: `{"jsonrpc":"1.0","method":"blockdisconnected","params":["123",100000],"id":null}`, marshalled: `{"jsonrpc":"1.0","method":"blockdisconnected","params":["123",100000,123456789],"id":null}`,
unmarshalled: &btcjson.BlockDisconnectedNtfn{ unmarshalled: &btcjson.BlockDisconnectedNtfn{
Hash: "123", Hash: "123",
Height: 100000, Height: 100000,
Time: 123456789,
}, },
}, },
{ {

View file

@ -818,9 +818,9 @@ The following is an overview of the JSON-RPC notifications used for Websocket co
|---|---| |---|---|
|Method|blockconnected| |Method|blockconnected|
|Request|[notifyblocks](#notifyblocks)| |Request|[notifyblocks](#notifyblocks)|
|Parameters|1. BlockHash (string) hex-encoded bytes of the attached block hash<br />2. BlockHeight (numeric) height of the attached block| |Parameters|1. BlockHash (string) hex-encoded bytes of the attached block hash<br />2. BlockHeight (numeric) height of the attached block<br />3. BlockTime (numeric) unix time of the attached block|
|Description|Notifies when a block has been added to the main chain. Notification is sent to all connected clients.| |Description|Notifies when a block has been added to the main chain. Notification is sent to all connected clients.|
|Example|Example blockconnected notification for mainnet block 280330 (newlines added for readability):<br />`{`<br />&nbsp;`"jsonrpc": "1.0",`<br />&nbsp;`"method": "blockconnected",`<br />&nbsp;`"params":`<br />&nbsp;&nbsp;`[`<br />&nbsp;&nbsp;&nbsp;`"000000000000000004cbdfe387f4df44b914e464ca79838a8ab777b3214dbffd",`<br />&nbsp;&nbsp;&nbsp;`280330`<br />&nbsp;&nbsp;`],`<br />&nbsp;`"id": null`<br />`}`| |Example|Example blockconnected notification for mainnet block 280330 (newlines added for readability):<br />`{`<br />&nbsp;`"jsonrpc": "1.0",`<br />&nbsp;`"method": "blockconnected",`<br />&nbsp;`"params":`<br />&nbsp;&nbsp;`[`<br />&nbsp;&nbsp;&nbsp;`"000000000000000004cbdfe387f4df44b914e464ca79838a8ab777b3214dbffd",`<br />&nbsp;&nbsp;&nbsp;`280330,`<br />&nbsp;&nbsp;&nbsp;`1389636265`<br />&nbsp;&nbsp;`],`<br />&nbsp;`"id": null`<br />`}`|
[Return to Overview](#NotificationOverview)<br /> [Return to Overview](#NotificationOverview)<br />
*** ***
@ -831,9 +831,9 @@ The following is an overview of the JSON-RPC notifications used for Websocket co
|---|---| |---|---|
|Method|blockdisconnected| |Method|blockdisconnected|
|Request|[notifyblocks](#notifyblocks)| |Request|[notifyblocks](#notifyblocks)|
|Parameters|1. BlockHash (string) hex-encoded bytes of the disconnected block hash<br />2. BlockHeight (numeric) height of the disconnected block| |Parameters|1. BlockHash (string) hex-encoded bytes of the disconnected block hash<br />2. BlockHeight (numeric) height of the disconnected block<br />3. BlockTime (numeric) unix time of the disconnected block|
|Description|Notifies when a block has been removed from the main chain. Notification is sent to all connected clients.| |Description|Notifies when a block has been removed from the main chain. Notification is sent to all connected clients.|
|Example|Example blockdisconnected notification for mainnet block 280330 (newlines added for readability):<br />`{`<br />&nbsp;`"jsonrpc": "1.0",`<br />&nbsp;`"method": "blockdisconnected",`<br />&nbsp;`"params":`<br />&nbsp;&nbsp;`[`<br />&nbsp;&nbsp;&nbsp;`"000000000000000004cbdfe387f4df44b914e464ca79838a8ab777b3214dbffd",`<br />&nbsp;&nbsp;&nbsp;`280330`<br />&nbsp;&nbsp;`],`<br />&nbsp;`"id": null`<br />`}`| |Example|Example blockdisconnected notification for mainnet block 280330 (newlines added for readability):<br />`{`<br />&nbsp;`"jsonrpc": "1.0",`<br />&nbsp;`"method": "blockdisconnected",`<br />&nbsp;`"params":`<br />&nbsp;&nbsp;`[`<br />&nbsp;&nbsp;&nbsp;`"000000000000000004cbdfe387f4df44b914e464ca79838a8ab777b3214dbffd",`<br />&nbsp;&nbsp;&nbsp;`280330,`<br />&nbsp;&nbsp;&nbsp;`1389636265`<br />&nbsp;&nbsp;`],`<br />&nbsp;`"id": null`<br />`}`|
[Return to Overview](#NotificationOverview)<br /> [Return to Overview](#NotificationOverview)<br />
*** ***

View file

@ -411,7 +411,7 @@ func (*wsNotificationManager) notifyBlockConnected(clients map[chan struct{}]*ws
// Notify interested websocket clients about the connected block. // Notify interested websocket clients about the connected block.
ntfn := btcjson.NewBlockConnectedNtfn(block.Sha().String(), ntfn := btcjson.NewBlockConnectedNtfn(block.Sha().String(),
int32(block.Height())) int32(block.Height()), block.MsgBlock().Header.Timestamp.Unix())
marshalledJSON, err := btcjson.MarshalCmd(nil, ntfn) marshalledJSON, err := btcjson.MarshalCmd(nil, ntfn)
if err != nil { if err != nil {
rpcsLog.Error("Failed to marshal block connected notification: "+ rpcsLog.Error("Failed to marshal block connected notification: "+
@ -435,7 +435,7 @@ func (*wsNotificationManager) notifyBlockDisconnected(clients map[chan struct{}]
// Notify interested websocket clients about the disconnected block. // Notify interested websocket clients about the disconnected block.
ntfn := btcjson.NewBlockDisconnectedNtfn(block.Sha().String(), ntfn := btcjson.NewBlockDisconnectedNtfn(block.Sha().String(),
int32(block.Height())) int32(block.Height()), block.MsgBlock().Header.Timestamp.Unix())
marshalledJSON, err := btcjson.MarshalCmd(nil, ntfn) marshalledJSON, err := btcjson.MarshalCmd(nil, ntfn)
if err != nil { if err != nil {
rpcsLog.Error("Failed to marshal block disconnected "+ rpcsLog.Error("Failed to marshal block disconnected "+