This repository has been archived by the owner on Jun 20, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
msgs.go
250 lines (204 loc) · 6.85 KB
/
msgs.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
package types
import (
sdk "github.com/cosmos/cosmos-sdk/types"
)
// RouterKey is a name for the router
const RouterKey = ModuleName // this was defined in your key.go file
/***** IngestHeaderChain *****/
// MsgIngestHeaderChain defines a IngestHeaderChain message
type MsgIngestHeaderChain struct {
Signer sdk.AccAddress `json:"signer"`
Headers []BitcoinHeader `json:"headers"`
}
// NewMsgIngestHeaderChain instantiates a MsgIngestHeaderChain
func NewMsgIngestHeaderChain(address sdk.AccAddress, headers []BitcoinHeader) MsgIngestHeaderChain {
return MsgIngestHeaderChain{
address,
headers,
}
}
// GetSigners gets signers
func (msg MsgIngestHeaderChain) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Signer}
}
// Type returns an identifier
func (msg MsgIngestHeaderChain) Type() string { return "ingest_header_chain" }
// ValidateBasic runs stateless validation
func (msg MsgIngestHeaderChain) ValidateBasic() sdk.Error {
for i := range msg.Headers {
valid, err := msg.Headers[i].Validate()
if !valid || err != nil {
return FromBTCSPVError(DefaultCodespace, err)
}
}
return nil
}
// GetSignBytes returns the sighash for the message
func (msg MsgIngestHeaderChain) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// Route returns the route key
func (msg MsgIngestHeaderChain) Route() string { return RouterKey }
/***** IngestDifficultyChange *****/
// MsgIngestDifficultyChange defines a IngestDifficultyChange message
type MsgIngestDifficultyChange struct {
Signer sdk.AccAddress `json:"signer"`
Start Hash256Digest `json:"prevEpochStartLE"`
Headers []BitcoinHeader `json:"headers"`
}
// NewMsgIngestDifficultyChange instantiates a MsgIngestDifficultyChange
func NewMsgIngestDifficultyChange(address sdk.AccAddress, start Hash256Digest, headers []BitcoinHeader) MsgIngestDifficultyChange {
return MsgIngestDifficultyChange{
address,
start,
headers,
}
}
// GetSigners gets signers
func (msg MsgIngestDifficultyChange) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Signer}
}
// Type returns an identifier
func (msg MsgIngestDifficultyChange) Type() string { return "ingest_difficulty_change" }
// ValidateBasic runs stateless validation
func (msg MsgIngestDifficultyChange) ValidateBasic() sdk.Error {
for i := range msg.Headers {
valid, err := msg.Headers[i].Validate()
if !valid || err != nil {
return FromBTCSPVError(DefaultCodespace, err)
}
}
return nil
}
// GetSignBytes returns the sighash for the message
func (msg MsgIngestDifficultyChange) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// Route returns the route key
func (msg MsgIngestDifficultyChange) Route() string { return RouterKey }
/***** MarkNewHeaviest *****/
// MsgMarkNewHeaviest defines a MarkNewHeaviest message
type MsgMarkNewHeaviest struct {
Signer sdk.AccAddress `json:"signer"`
Ancestor Hash256Digest `json:"ancestor"`
CurrentBest RawHeader `json:"currentBest"`
NewBest RawHeader `json:"newBest"`
Limit uint32 `json:"limit"`
}
// NewMsgMarkNewHeaviest instantiates a MsgMarkNewHeaviest
func NewMsgMarkNewHeaviest(address sdk.AccAddress, ancestor Hash256Digest, currentBest RawHeader, newBest RawHeader, limit uint32) MsgMarkNewHeaviest {
if limit == 0 {
limit = DefaultLookupLimit
}
return MsgMarkNewHeaviest{
address,
ancestor,
currentBest,
newBest,
limit,
}
}
// GetSigners gets signers
func (msg MsgMarkNewHeaviest) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Signer}
}
// Type returns an identifier
func (msg MsgMarkNewHeaviest) Type() string { return "mark_new_heaviest" }
// ValidateBasic runs stateless validation
func (msg MsgMarkNewHeaviest) ValidateBasic() sdk.Error {
if len(msg.CurrentBest) != 80 {
return ErrBadHeaderLength(DefaultCodespace, "currentBest", msg.CurrentBest, len(msg.CurrentBest))
}
if len(msg.NewBest) != 80 {
return ErrBadHeaderLength(DefaultCodespace, "newBest", msg.NewBest, len(msg.NewBest))
}
return nil
}
// GetSignBytes returns the sighash for the message
func (msg MsgMarkNewHeaviest) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// Route returns the route key
func (msg MsgMarkNewHeaviest) Route() string { return RouterKey }
/***** NewRequest *****/
// MsgNewRequest defines a NewRequest message
type MsgNewRequest struct {
Signer sdk.AccAddress `json:"signer"`
Spends HexBytes `json:"spends"`
Pays HexBytes `json:"pays"`
PaysValue uint64 `json:"paysValue"`
NumConfs uint8 `json:"numConfs"`
Origin Origin `json:"origin"`
Action HexBytes `json:"action"`
}
// NewMsgNewRequest instantiates a MsgNewRequest
func NewMsgNewRequest(address sdk.AccAddress, spends, pays []byte, paysValue uint64, numConfs uint8, origin Origin, action HexBytes) MsgNewRequest {
return MsgNewRequest{
address,
spends,
pays,
paysValue,
numConfs,
origin,
action,
}
}
// GetSigners gets signers
func (msg MsgNewRequest) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Signer}
}
// Type returns an identifier
func (msg MsgNewRequest) Type() string { return "new_request" }
// ValidateBasic runs stateless validation
func (msg MsgNewRequest) ValidateBasic() sdk.Error {
// TODO: validate output types
if len(msg.Spends) != 36 && len(msg.Spends) != 0 {
return ErrSpendsLength(DefaultCodespace)
}
if len(msg.Pays) > 50 {
return ErrPaysLength(DefaultCodespace)
}
if len(msg.Action) > 500 {
return ErrActionLength(DefaultCodespace)
}
return nil
}
// GetSignBytes returns the sighash for the message
func (msg MsgNewRequest) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// Route returns the route key
func (msg MsgNewRequest) Route() string { return RouterKey }
/***** ProvideProof *****/
// MsgProvideProof defines a NewRequest message
type MsgProvideProof struct {
Signer sdk.AccAddress `json:"signer"`
Filled FilledRequests `json:"filled"`
}
// NewMsgProvideProof instantiates a MsgProvideProof
func NewMsgProvideProof(address sdk.AccAddress, filledRequests FilledRequests) MsgProvideProof {
return MsgProvideProof{
address,
filledRequests,
}
}
// GetSigners gets signers
func (msg MsgProvideProof) GetSigners() []sdk.AccAddress {
return []sdk.AccAddress{msg.Signer}
}
// ValidateBasic runs stateless validation
func (msg MsgProvideProof) ValidateBasic() sdk.Error {
valid, err := msg.Filled.Proof.Validate()
if !valid || err != nil {
return FromBTCSPVError(DefaultCodespace, err)
}
return nil
}
// Type returns an identifier
func (msg MsgProvideProof) Type() string { return "provide_proof" }
// GetSignBytes returns the sighash for the message
func (msg MsgProvideProof) GetSignBytes() []byte {
return sdk.MustSortJSON(ModuleCdc.MustMarshalJSON(msg))
}
// Route returns the route key
func (msg MsgProvideProof) Route() string { return RouterKey }