-
Notifications
You must be signed in to change notification settings - Fork 0
/
transactions.go
172 lines (154 loc) · 4.58 KB
/
transactions.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
package process
import (
"slices"
errorsmod "cosmossdk.io/errors"
abci "github.com/cometbft/cometbft/abci/types"
"github.com/jinxprotocol/v4-chain/protocol/lib"
sdk "github.com/cosmos/cosmos-sdk/types"
)
const (
minTxsCount = 4
proposedOperationsTxIndex = 0
updateMarketPricesTxLenOffset = -1
addPremiumVotesTxLenOffset = -2
acknowledgeBridgesTxLenOffset = -3
lastOtherTxLenOffset = acknowledgeBridgesTxLenOffset
firstOtherTxIndex = proposedOperationsTxIndex + 1
)
func init() {
txIndicesAndOffsets := []int{
proposedOperationsTxIndex,
acknowledgeBridgesTxLenOffset,
addPremiumVotesTxLenOffset,
updateMarketPricesTxLenOffset,
}
if minTxsCount != len(txIndicesAndOffsets) {
panic("minTxsCount does not match expected count of Txs.")
}
if lib.ContainsDuplicates(txIndicesAndOffsets) {
panic("Duplicate indices/offsets defined for Txs.")
}
if slices.Min[[]int](txIndicesAndOffsets) != lastOtherTxLenOffset {
panic("lastTxLenOffset is not the lowest offset")
}
if slices.Max[[]int](txIndicesAndOffsets)+1 != firstOtherTxIndex {
panic("firstOtherTxIndex is <= the maximum offset")
}
txIndicesForMinTxsCount := []int{
proposedOperationsTxIndex,
acknowledgeBridgesTxLenOffset + minTxsCount,
addPremiumVotesTxLenOffset + minTxsCount,
updateMarketPricesTxLenOffset + minTxsCount,
}
if minTxsCount != len(txIndicesForMinTxsCount) {
panic("minTxsCount does not match expected count of Txs.")
}
if lib.ContainsDuplicates(txIndicesForMinTxsCount) {
panic("Overlapping indices and offsets defined for Txs.")
}
if minTxsCount != firstOtherTxIndex-lastOtherTxLenOffset {
panic("Unexpected gap between firstOtherTxIndex and lastOtherTxLenOffset which is greater than minTxsCount")
}
}
// ProcessProposalTxs is used as an intermediary struct to validate a proposed list of txs
// for `ProcessProposal`.
type ProcessProposalTxs struct {
// Single msg txs.
ProposedOperationsTx *ProposedOperationsTx
AcknowledgeBridgesTx *AcknowledgeBridgesTx
AddPremiumVotesTx *AddPremiumVotesTx
UpdateMarketPricesTx *UpdateMarketPricesTx
// Multi msgs txs.
OtherTxs []*OtherMsgsTx
}
// DecodeProcessProposalTxs returns a new `processProposalTxs`.
func DecodeProcessProposalTxs(
ctx sdk.Context,
decoder sdk.TxDecoder,
req abci.RequestProcessProposal,
bridgeKeeper ProcessBridgeKeeper,
pricesKeeper ProcessPricesKeeper,
) (*ProcessProposalTxs, error) {
// Check len.
numTxs := len(req.Txs)
if numTxs < minTxsCount {
return nil, errorsmod.Wrapf(
ErrUnexpectedNumMsgs,
"Expected the proposal to contain at least %d txs, but got %d",
minTxsCount,
numTxs,
)
}
// Operations.
operationsTx, err := DecodeProposedOperationsTx(decoder, req.Txs[proposedOperationsTxIndex])
if err != nil {
return nil, err
}
// Acknowledge bridges.
acknowledgeBridgesTx, err := DecodeAcknowledgeBridgesTx(
ctx,
bridgeKeeper,
decoder,
req.Txs[numTxs+acknowledgeBridgesTxLenOffset],
)
if err != nil {
return nil, err
}
// Funding samples.
addPremiumVotesTx, err := DecodeAddPremiumVotesTx(decoder, req.Txs[numTxs+addPremiumVotesTxLenOffset])
if err != nil {
return nil, err
}
// Price updates.
updatePricesTx, err := DecodeUpdateMarketPricesTx(
ctx,
pricesKeeper,
decoder,
req.Txs[numTxs+updateMarketPricesTxLenOffset],
)
if err != nil {
return nil, err
}
// Other txs.
allOtherTxs := make([]*OtherMsgsTx, numTxs-minTxsCount)
for i, txBytes := range req.Txs[firstOtherTxIndex : numTxs+lastOtherTxLenOffset] {
otherTx, err := DecodeOtherMsgsTx(decoder, txBytes)
if err != nil {
return nil, err
}
allOtherTxs[i] = otherTx
}
return &ProcessProposalTxs{
ProposedOperationsTx: operationsTx,
AcknowledgeBridgesTx: acknowledgeBridgesTx,
AddPremiumVotesTx: addPremiumVotesTx,
UpdateMarketPricesTx: updatePricesTx,
OtherTxs: allOtherTxs,
}, nil
}
// Validate performs `ValidateBasic` on the underlying msgs that are part of the txs.
// Returns nil if all are valid. Otherwise, returns error.
//
// Exception: for UpdateMarketPricesTx, we perform "in-memory stateful" validation
// to ensure that the new proposed prices are "valid" in comparison to index prices.
func (ppt *ProcessProposalTxs) Validate() error {
// Validate single msg txs.
singleTxs := []SingleMsgTx{
ppt.ProposedOperationsTx,
ppt.AddPremiumVotesTx,
ppt.AcknowledgeBridgesTx,
ppt.UpdateMarketPricesTx,
}
for _, smt := range singleTxs {
if err := smt.Validate(); err != nil {
return err
}
}
// Validate multi msgs txs.
for _, mmt := range ppt.OtherTxs {
if err := mmt.Validate(); err != nil {
return err
}
}
return nil
}