-
Notifications
You must be signed in to change notification settings - Fork 165
/
msgs.go
74 lines (59 loc) · 1.63 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
package types
import (
"encoding/hex"
"fmt"
"math/big"
bbn "github.com/babylonchain/babylon/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
var _ sdk.Msg = (*MsgInsertHeaders)(nil)
func NewMsgInsertHeaders(signer sdk.AccAddress, headersHex string) (*MsgInsertHeaders, error) {
if len(headersHex) == 0 {
return nil, fmt.Errorf("empty headers list")
}
decoded, err := hex.DecodeString(headersHex)
if err != nil {
return nil, err
}
if len(decoded)%bbn.BTCHeaderLen != 0 {
return nil, fmt.Errorf("invalid length of encoded headers: %d", len(decoded))
}
numOfHeaders := len(decoded) / bbn.BTCHeaderLen
headers := make([]bbn.BTCHeaderBytes, numOfHeaders)
for i := 0; i < numOfHeaders; i++ {
headerSlice := decoded[i*bbn.BTCHeaderLen : (i+1)*bbn.BTCHeaderLen]
headerBytes, err := bbn.NewBTCHeaderBytesFromBytes(headerSlice)
if err != nil {
return nil, err
}
headers[i] = headerBytes
}
return &MsgInsertHeaders{Signer: signer.String(), Headers: headers}, nil
}
func (msg *MsgInsertHeaders) ValidateHeaders(powLimit *big.Int) error {
// TOOD: Limit number of headers in message?
for _, header := range msg.Headers {
err := bbn.ValidateBTCHeader(header.ToBlockHeader(), powLimit)
if err != nil {
return err
}
}
return nil
}
func (msg *MsgInsertHeaders) ReporterAddress() sdk.AccAddress {
sender, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
panic(err)
}
return sender
}
func (msg *MsgInsertHeaders) ValidateStateless() error {
_, err := sdk.AccAddressFromBech32(msg.Signer)
if err != nil {
return err
}
if len(msg.Headers) == 0 {
return fmt.Errorf("empty headers list")
}
return nil
}