-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathproposal_handler.go
257 lines (208 loc) · 8.09 KB
/
proposal_handler.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
251
252
253
254
255
256
257
package keeper
import (
"encoding/json"
"fmt"
"time"
distributiontypes "github.com/cosmos/cosmos-sdk/x/distribution/types"
transfertypes "github.com/cosmos/ibc-go/v6/modules/apps/transfer/types"
clienttypes "github.com/cosmos/ibc-go/v6/modules/core/02-client/types"
errorsmod "cosmossdk.io/errors"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ethereum/go-ethereum/common"
"github.com/peggyjv/sommelier/v7/x/axelarcork/types"
pubsubtypes "github.com/peggyjv/sommelier/v7/x/pubsub/types"
)
func NewAxelarSubscriptionID(chainID uint64, address common.Address) string {
return fmt.Sprintf("%d:%s", chainID, address.String())
}
// HandleAddManagedCellarsProposal is a handler for executing a passed community cellar addition proposal
func HandleAddManagedCellarsProposal(ctx sdk.Context, k Keeper, p types.AddAxelarManagedCellarIDsProposal) error {
config, ok := k.GetChainConfigurationByID(ctx, p.ChainId)
if !ok {
return fmt.Errorf("chain by id %d not found", p.ChainId)
}
_, publisherFound := k.pubsubKeeper.GetPublisher(ctx, p.PublisherDomain)
if !publisherFound {
return fmt.Errorf("not an approved publisher: %s", p.PublisherDomain)
}
if err := p.CellarIds.ValidateBasic(); err != nil {
return err
}
cellarAddresses := k.GetCellarIDs(ctx, config.Id)
for _, proposedCellarID := range p.CellarIds.Ids {
proposedCellarAddress := common.HexToAddress(proposedCellarID)
found := false
for _, id := range cellarAddresses {
if id == proposedCellarAddress {
found = true
}
}
if !found {
cellarAddresses = append(cellarAddresses, proposedCellarAddress)
subscriptionID := NewAxelarSubscriptionID(p.ChainId, proposedCellarAddress)
defaultSubscription := pubsubtypes.DefaultSubscription{
SubscriptionId: subscriptionID,
PublisherDomain: p.PublisherDomain,
}
k.pubsubKeeper.SetDefaultSubscription(ctx, defaultSubscription)
}
}
idStrings := make([]string, len(cellarAddresses))
for i, cid := range cellarAddresses {
idStrings[i] = cid.String()
}
k.SetCellarIDs(ctx, config.Id, types.CellarIDSet{ChainId: config.Id, Ids: idStrings})
return nil
}
// HandleRemoveManagedCellarsProposal is a handler for executing a passed community cellar removal proposal
func HandleRemoveManagedCellarsProposal(ctx sdk.Context, k Keeper, p types.RemoveAxelarManagedCellarIDsProposal) error {
config, ok := k.GetChainConfigurationByID(ctx, p.ChainId)
if !ok {
return fmt.Errorf("chain by id %d not found", p.ChainId)
}
if err := p.CellarIds.ValidateBasic(); err != nil {
return err
}
var outputCellarIDs types.CellarIDSet
for _, existingID := range k.GetCellarIDs(ctx, config.Id) {
found := false
for _, inputID := range p.CellarIds.Ids {
if existingID == common.HexToAddress(inputID) {
found = true
}
}
if !found {
outputCellarIDs.Ids = append(outputCellarIDs.Ids, existingID.String())
}
}
outputCellarIDs.ChainId = config.Id
// unlike for adding an ID, we don't need to re-sort because we're removing elements from an already sorted list
k.SetCellarIDs(ctx, config.Id, outputCellarIDs)
for _, cellarToDelete := range p.CellarIds.Ids {
subscriptionID := NewAxelarSubscriptionID(p.ChainId, common.HexToAddress(cellarToDelete))
k.pubsubKeeper.DeleteDefaultSubscription(ctx, subscriptionID)
}
return nil
}
// HandleScheduledCorkProposal is a handler for executing a passed scheduled cork proposal
func HandleScheduledCorkProposal(ctx sdk.Context, k Keeper, p types.AxelarScheduledCorkProposal) error {
config, ok := k.GetChainConfigurationByID(ctx, p.ChainId)
if !ok {
return fmt.Errorf("chain by id %d not found", p.ChainId)
}
if !k.HasCellarID(ctx, config.Id, common.HexToAddress(p.TargetContractAddress)) {
return errorsmod.Wrapf(types.ErrUnmanagedCellarAddress, "id: %s", p.TargetContractAddress)
}
return nil
}
func HandleCommunityPoolSpendProposal(ctx sdk.Context, k Keeper, p types.AxelarCommunityPoolSpendProposal) error {
params := k.GetParamSet(ctx)
config, ok := k.GetChainConfigurationByID(ctx, p.ChainId)
if !ok {
return fmt.Errorf("chain by id %d not found", p.ChainId)
}
feeFound, feeCoin := config.BridgeFees.Find(p.Amount.Denom)
if !feeFound {
return fmt.Errorf("no matching bridge fee for denom %s", p.Amount.Denom)
}
coinWithBridgeFee := p.Amount.Add(feeCoin)
feePool := k.distributionKeeper.GetFeePool(ctx)
// NOTE the community pool isn't a module account, however its coins
// are held in the distribution module account. Thus, the community pool
// must be reduced separately from the Axelar IBC calls
newPool, negative := feePool.CommunityPool.SafeSub(sdk.NewDecCoinsFromCoins(coinWithBridgeFee))
if negative {
return distributiontypes.ErrBadDistribution
}
feePool.CommunityPool = newPool
// since distribution is not an authorized sender, put them in the axelarcork module account
if err := k.bankKeeper.SendCoinsFromModuleToModule(ctx, distributiontypes.ModuleName, types.ModuleName, sdk.NewCoins(coinWithBridgeFee)); err != nil {
panic(err)
}
sender := k.GetSenderAccount(ctx).GetAddress().String()
axelarMemo := types.AxelarBody{
DestinationChain: config.Name,
DestinationAddress: p.Recipient,
Payload: nil,
Type: types.PureTokenTransfer,
}
memoBz, err := json.Marshal(axelarMemo)
if err != nil {
return err
}
memo := string(memoBz)
transferMsg := transfertypes.NewMsgTransfer(
params.IbcPort,
params.IbcChannel,
coinWithBridgeFee,
sender,
params.GmpAccount,
clienttypes.ZeroHeight(),
uint64(ctx.BlockTime().Add(time.Duration(params.TimeoutDuration)).UnixNano()),
memo,
)
resp, err := k.transferKeeper.Transfer(sdk.WrapSDKContext(ctx), transferMsg)
if err != nil {
return err
}
k.distributionKeeper.SetFeePool(ctx, feePool)
k.Logger(ctx).Info("transfer from the community pool issued to the axelar bridge",
"ibc sequence", resp,
"amount", coinWithBridgeFee.Amount.String(),
"recipient", p.Recipient,
"chain", config.Name,
"sender", sender,
"timeout duration", params.TimeoutDuration,
)
return nil
}
// HandleAddChainConfigurationProposal is a handler for executing a passed chain configuration addition proposal
func HandleAddChainConfigurationProposal(ctx sdk.Context, k Keeper, p types.AddChainConfigurationProposal) error {
err := p.ChainConfiguration.ValidateBasic()
if err != nil {
return err
}
k.SetChainConfiguration(ctx, p.ChainConfiguration.Id, *p.ChainConfiguration)
return nil
}
// HandleRemoveChainConfigurationProposal is a handler for executing a passed chain configuration removal proposal
func HandleRemoveChainConfigurationProposal(ctx sdk.Context, k Keeper, p types.RemoveChainConfigurationProposal) error {
_, ok := k.GetChainConfigurationByID(ctx, p.ChainId)
if !ok {
return fmt.Errorf("chain by id %d not found", p.ChainId)
}
k.DeleteChainConfigurationByID(ctx, p.ChainId)
return nil
}
// HandleUpgradeAxelarProxyContractProposal is a handler for executing a passed axelar proxy contract upgrade proposal
func HandleUpgradeAxelarProxyContractProposal(ctx sdk.Context, k Keeper, p types.UpgradeAxelarProxyContractProposal) error {
_, ok := k.GetChainConfigurationByID(ctx, p.ChainId)
if !ok {
return fmt.Errorf("chain by id %d not found", p.ChainId)
}
cellars := []string{}
for _, c := range k.GetCellarIDs(ctx, p.ChainId) {
cellars = append(cellars, c.String())
}
payload, err := types.EncodeUpgradeArgs(p.NewProxyAddress, cellars)
if err != nil {
return err
}
upgradeData := types.AxelarUpgradeData{
ChainId: p.ChainId,
Payload: payload,
ExecutableHeightThreshold: ctx.BlockHeight() + int64(types.DefaultExecutableHeightThreshold),
}
k.SetAxelarProxyUpgradeData(ctx, p.ChainId, upgradeData)
return nil
}
// HandleCancelAxelarProxyContractUpgradeProposal is a handler for deleting axelar proxy contract upgrade data before the
// executable height threshold is reached.
func HandleCancelAxelarProxyContractUpgradeProposal(ctx sdk.Context, k Keeper, p types.CancelAxelarProxyContractUpgradeProposal) error {
_, ok := k.GetChainConfigurationByID(ctx, p.ChainId)
if !ok {
return fmt.Errorf("chain id %d not found", p.ChainId)
}
k.DeleteAxelarProxyUpgradeData(ctx, p.ChainId)
return nil
}