/
chainsvrwsntfns.go
232 lines (197 loc) · 7.9 KB
/
chainsvrwsntfns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Copyright (c) 2014 The btcsuite developers
// Copyright (c) 2015-2019 The Decred developers
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.
// NOTE: This file is intended to house the RPC websocket notifications that are
// supported by a chain server.
package types
import "github.com/decred/dcrd/dcrjson/v3"
const (
// BlockConnectedNtfnMethod is the method used for notifications from
// the chain server that a block has been connected.
BlockConnectedNtfnMethod Method = "blockconnected"
// BlockDisconnectedNtfnMethod is the method used for notifications from
// the chain server that a block has been disconnected.
BlockDisconnectedNtfnMethod Method = "blockdisconnected"
// NewTicketsNtfnMethod is the method of the daemon newtickets notification.
NewTicketsNtfnMethod Method = "newtickets"
// ReorganizationNtfnMethod is the method used for notifications that the
// block chain is in the process of a reorganization.
ReorganizationNtfnMethod Method = "reorganization"
// TxAcceptedNtfnMethod is the method used for notifications from the
// chain server that a transaction has been accepted into the mempool.
TxAcceptedNtfnMethod Method = "txaccepted"
// TxAcceptedVerboseNtfnMethod is the method used for notifications from
// the chain server that a transaction has been accepted into the
// mempool. This differs from TxAcceptedNtfnMethod in that it provides
// more details in the notification.
TxAcceptedVerboseNtfnMethod Method = "txacceptedverbose"
// RelevantTxAcceptedNtfnMethod is the method used for notifications
// from the chain server that inform a client that a relevant
// transaction was accepted by the mempool.
RelevantTxAcceptedNtfnMethod Method = "relevanttxaccepted"
// SpentAndMissedTicketsNtfnMethod is the method of the daemon
// spentandmissedtickets notification.
SpentAndMissedTicketsNtfnMethod Method = "spentandmissedtickets"
// StakeDifficultyNtfnMethod is the method of the daemon stakedifficulty
// notification.
StakeDifficultyNtfnMethod Method = "stakedifficulty"
// WinningTicketsNtfnMethod is the method of the daemon winningtickets
// notification.
WinningTicketsNtfnMethod Method = "winningtickets"
)
// BlockConnectedNtfn defines the blockconnected JSON-RPC notification.
type BlockConnectedNtfn struct {
Header string `json:"header"`
SubscribedTxs []string `json:"subscribedtxs"`
}
// NewBlockConnectedNtfn returns a new instance which can be used to issue a
// blockconnected JSON-RPC notification.
func NewBlockConnectedNtfn(header string, subscribedTxs []string) *BlockConnectedNtfn {
return &BlockConnectedNtfn{
Header: header,
SubscribedTxs: subscribedTxs,
}
}
// BlockDisconnectedNtfn defines the blockdisconnected JSON-RPC notification.
type BlockDisconnectedNtfn struct {
Header string `json:"header"`
}
// NewBlockDisconnectedNtfn returns a new instance which can be used to issue a
// blockdisconnected JSON-RPC notification.
func NewBlockDisconnectedNtfn(header string) *BlockDisconnectedNtfn {
return &BlockDisconnectedNtfn{
Header: header,
}
}
// NewTicketsNtfn is a type handling custom marshaling and
// unmarshaling of newtickets JSON websocket notifications.
type NewTicketsNtfn struct {
Hash string
Height int32
StakeDiff int64
Tickets []string
}
// NewNewTicketsNtfn creates a new NewTicketsNtfn.
func NewNewTicketsNtfn(hash string, height int32, stakeDiff int64, tickets []string) *NewTicketsNtfn {
return &NewTicketsNtfn{
Hash: hash,
Height: height,
StakeDiff: stakeDiff,
Tickets: tickets,
}
}
// ReorganizationNtfn defines the reorganization JSON-RPC notification.
type ReorganizationNtfn struct {
OldHash string `json:"oldhash"`
OldHeight int32 `json:"oldheight"`
NewHash string `json:"newhash"`
NewHeight int32 `json:"newheight"`
}
// NewReorganizationNtfn returns a new instance which can be used to issue a
// blockdisconnected JSON-RPC notification.
func NewReorganizationNtfn(oldHash string, oldHeight int32, newHash string,
newHeight int32) *ReorganizationNtfn {
return &ReorganizationNtfn{
OldHash: oldHash,
OldHeight: oldHeight,
NewHash: newHash,
NewHeight: newHeight,
}
}
// SpentAndMissedTicketsNtfn is a type handling custom marshaling and
// unmarshaling of spentandmissedtickets JSON websocket notifications.
type SpentAndMissedTicketsNtfn struct {
Hash string
Height int32
StakeDiff int64
Tickets map[string]string
}
// NewSpentAndMissedTicketsNtfn creates a new SpentAndMissedTicketsNtfn.
func NewSpentAndMissedTicketsNtfn(hash string, height int32, stakeDiff int64, tickets map[string]string) *SpentAndMissedTicketsNtfn {
return &SpentAndMissedTicketsNtfn{
Hash: hash,
Height: height,
StakeDiff: stakeDiff,
Tickets: tickets,
}
}
// StakeDifficultyNtfn is a type handling custom marshaling and
// unmarshaling of stakedifficulty JSON websocket notifications.
type StakeDifficultyNtfn struct {
BlockHash string
BlockHeight int32
StakeDiff int64
}
// NewStakeDifficultyNtfn creates a new StakeDifficultyNtfn.
func NewStakeDifficultyNtfn(hash string, height int32, stakeDiff int64) *StakeDifficultyNtfn {
return &StakeDifficultyNtfn{
BlockHash: hash,
BlockHeight: height,
StakeDiff: stakeDiff,
}
}
// TxAcceptedNtfn defines the txaccepted JSON-RPC notification.
type TxAcceptedNtfn struct {
TxID string `json:"txid"`
Amount float64 `json:"amount"`
}
// NewTxAcceptedNtfn returns a new instance which can be used to issue a
// txaccepted JSON-RPC notification.
func NewTxAcceptedNtfn(txHash string, amount float64) *TxAcceptedNtfn {
return &TxAcceptedNtfn{
TxID: txHash,
Amount: amount,
}
}
// TxAcceptedVerboseNtfn defines the txacceptedverbose JSON-RPC notification.
type TxAcceptedVerboseNtfn struct {
RawTx TxRawResult `json:"rawtx"`
}
// NewTxAcceptedVerboseNtfn returns a new instance which can be used to issue a
// txacceptedverbose JSON-RPC notification.
func NewTxAcceptedVerboseNtfn(rawTx TxRawResult) *TxAcceptedVerboseNtfn {
return &TxAcceptedVerboseNtfn{
RawTx: rawTx,
}
}
// RelevantTxAcceptedNtfn defines the parameters to the relevanttxaccepted
// JSON-RPC notification.
type RelevantTxAcceptedNtfn struct {
Transaction string `json:"transaction"`
}
// NewRelevantTxAcceptedNtfn returns a new instance which can be used to issue a
// relevantxaccepted JSON-RPC notification.
func NewRelevantTxAcceptedNtfn(txHex string) *RelevantTxAcceptedNtfn {
return &RelevantTxAcceptedNtfn{Transaction: txHex}
}
// WinningTicketsNtfn is a type handling custom marshaling and
// unmarshaling of blockconnected JSON websocket notifications.
type WinningTicketsNtfn struct {
BlockHash string
BlockHeight int32
Tickets map[string]string
}
// NewWinningTicketsNtfn creates a new WinningTicketsNtfn.
func NewWinningTicketsNtfn(hash string, height int32, tickets map[string]string) *WinningTicketsNtfn {
return &WinningTicketsNtfn{
BlockHash: hash,
BlockHeight: height,
Tickets: tickets,
}
}
func init() {
// The commands in this file are only usable by websockets and are
// notifications.
flags := dcrjson.UFWebsocketOnly | dcrjson.UFNotification
dcrjson.MustRegister(BlockConnectedNtfnMethod, (*BlockConnectedNtfn)(nil), flags)
dcrjson.MustRegister(BlockDisconnectedNtfnMethod, (*BlockDisconnectedNtfn)(nil), flags)
dcrjson.MustRegister(NewTicketsNtfnMethod, (*NewTicketsNtfn)(nil), flags)
dcrjson.MustRegister(ReorganizationNtfnMethod, (*ReorganizationNtfn)(nil), flags)
dcrjson.MustRegister(TxAcceptedNtfnMethod, (*TxAcceptedNtfn)(nil), flags)
dcrjson.MustRegister(TxAcceptedVerboseNtfnMethod, (*TxAcceptedVerboseNtfn)(nil), flags)
dcrjson.MustRegister(RelevantTxAcceptedNtfnMethod, (*RelevantTxAcceptedNtfn)(nil), flags)
dcrjson.MustRegister(SpentAndMissedTicketsNtfnMethod, (*SpentAndMissedTicketsNtfn)(nil), flags)
dcrjson.MustRegister(StakeDifficultyNtfnMethod, (*StakeDifficultyNtfn)(nil), flags)
dcrjson.MustRegister(WinningTicketsNtfnMethod, (*WinningTicketsNtfn)(nil), flags)
}