-
Notifications
You must be signed in to change notification settings - Fork 139
/
msgs.go
154 lines (120 loc) · 3.81 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
package types
import (
"errors"
fmt "fmt"
"math/big"
errorsmod "cosmossdk.io/errors"
txformat "github.com/babylonchain/babylon/btctxformatter"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
var (
// Ensure that MsgInsertBTCSpvProof implements all functions of the Msg interface
_ sdk.Msg = (*MsgInsertBTCSpvProof)(nil)
_ sdk.Msg = (*MsgUpdateParams)(nil)
)
// Parse and Validate transactions which should contain OP_RETURN data.
// OP_RETURN bytes are not validated in any way. It is up to the caller attach
// semantic meaning and validity to those bytes.
// Returned ParsedProofs are in same order as raw proofs
func ParseTwoProofs(
submitter sdk.AccAddress,
proofs []*BTCSpvProof,
powLimit *big.Int,
expectedTag txformat.BabylonTag) (*RawCheckpointSubmission, error) {
// Expecting as many proofs as many parts our checkpoint is composed of
if len(proofs) != txformat.NumberOfParts {
return nil, fmt.Errorf("expected at exactly valid op return transactions")
}
var parsedProofs []*ParsedProof
for _, proof := range proofs {
parsedProof, e :=
ParseProof(
proof.BtcTransaction,
proof.BtcTransactionIndex,
proof.MerkleNodes,
proof.ConfirmingBtcHeader,
powLimit,
)
if e != nil {
return nil, e
}
parsedProofs = append(parsedProofs, parsedProof)
}
var checkpointData [][]byte
for i, proof := range parsedProofs {
data, err := txformat.GetCheckpointData(
expectedTag,
txformat.CurrentVersion,
uint8(i),
proof.OpReturnData,
)
if err != nil {
return nil, err
}
checkpointData = append(checkpointData, data)
}
// at this point we know we have two correctly formated babylon op return transacitons
// we need to check if parts match
rawCkptData, err := txformat.ConnectParts(txformat.CurrentVersion, checkpointData[0], checkpointData[1])
if err != nil {
return nil, err
}
rawCheckpoint, err := txformat.DecodeRawCheckpoint(txformat.CurrentVersion, rawCkptData)
if err != nil {
return nil, err
}
sub := NewRawCheckpointSubmission(submitter, *parsedProofs[0], *parsedProofs[1], *rawCheckpoint)
return &sub, nil
}
func ParseSubmission(
m *MsgInsertBTCSpvProof,
powLimit *big.Int,
expectedTag txformat.BabylonTag) (*RawCheckpointSubmission, error) {
if m == nil {
return nil, errors.New("msgInsertBTCSpvProof can't nil")
}
address, err := sdk.AccAddressFromBech32(m.Submitter)
if err != nil {
return nil, sdkerrors.ErrInvalidAddress.Wrapf("invalid submitter address: %s", err)
}
sub, err := ParseTwoProofs(address, m.Proofs, powLimit, expectedTag)
if err != nil {
return nil, err
}
return sub, nil
}
func (m *MsgInsertBTCSpvProof) ValidateBasic() error {
// m.Proofs are validated in ante-handler
_, err := sdk.AccAddressFromBech32(m.Submitter)
if err != nil {
return sdkerrors.ErrInvalidAddress.Wrapf("invalid submitter address: %s", err)
}
return nil
}
func (m *MsgInsertBTCSpvProof) GetSigners() []sdk.AccAddress {
// cosmos-sdk modules usually ignore possible error here, we panic for the sake
// of informing something terrible had happend
submitter, err := sdk.AccAddressFromBech32(m.Submitter)
if err != nil {
// Panic, since the GetSigners method is called after ValidateBasic
// which performs the same check.
panic(err)
}
return []sdk.AccAddress{submitter}
}
// GetSigners returns the expected signers for a MsgUpdateParams message.
func (m *MsgUpdateParams) GetSigners() []sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(m.Authority)
return []sdk.AccAddress{addr}
}
// ValidateBasic does a sanity check on the provided data.
func (m *MsgUpdateParams) ValidateBasic() error {
if _, err := sdk.AccAddressFromBech32(m.Authority); err != nil {
return errorsmod.Wrap(err, "invalid authority address")
}
if err := m.Params.Validate(); err != nil {
return err
}
return nil
}