This repository has been archived by the owner on Dec 21, 2021. It is now read-only.
/
pool.go
181 lines (160 loc) · 5.5 KB
/
pool.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
package keeper
import (
"encoding/binary"
"fmt"
"strconv"
"github.com/MinterTeam/mhub/chain/x/minter/types"
"github.com/cosmos/cosmos-sdk/store/prefix"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
)
// AddToOutgoingPool
// - checks a counterpart denomintor exists for the given voucher type
// - burns the voucher for transfer amount and fees
// - persists an OutgoingTx
// - adds the TX to the `available` TX pool via a second index
func (k Keeper) AddToOutgoingPool(ctx sdk.Context, sender sdk.AccAddress, counterpartReceiver string, txHash string, amount sdk.Coin) (uint64, error) {
totalInVouchers := sdk.Coins{amount}
// Ensure that the coin is a peggy voucher
if _, err := types.ValidatePeggyCoin(amount, ctx, k.oracleKeeper); err != nil {
return 0, fmt.Errorf("amount not a peggy voucher coin: %s", err)
}
// send coins to module in prep for burn
if err := k.bankKeeper.SendCoinsFromAccountToModule(ctx, sender, types.ModuleName, totalInVouchers); err != nil {
return 0, err
}
// burn vouchers to send them back to ETH
if err := k.bankKeeper.BurnCoins(ctx, types.ModuleName, totalInVouchers); err != nil {
panic(err)
}
// get next tx id from keeper
nextID := k.autoIncrementID(ctx, types.KeyLastTXPoolID)
// construct outgoing tx
outgoing := &types.OutgoingTx{
Sender: sender.String(),
DestAddr: counterpartReceiver,
Amount: amount,
TxHash: txHash,
}
// set the outgoing tx in the pool index
if err := k.setPoolEntry(ctx, nextID, outgoing); err != nil {
return 0, err
}
// add a second index with the fee
k.appendToUnbatchedTXIndex(ctx, nextID)
// todo: add second index for sender so that we can easily query: give pending Tx by sender
// todo: what about a second index for receiver?
poolEvent := sdk.NewEvent(
types.EventTypeBridgeWithdrawalReceived,
sdk.NewAttribute(sdk.AttributeKeyModule, types.ModuleName),
sdk.NewAttribute(types.AttributeKeyContract, k.GetBridgeContractAddress(ctx)),
sdk.NewAttribute(types.AttributeKeyBridgeChainID, strconv.Itoa(int(k.GetBridgeChainID(ctx)))),
sdk.NewAttribute(types.AttributeKeyOutgoingTXID, strconv.Itoa(int(nextID))),
sdk.NewAttribute(types.AttributeKeyNonce, fmt.Sprint(nextID)),
sdk.NewAttribute(types.AttributeKeyTxHash, txHash),
)
ctx.EventManager().EmitEvent(poolEvent)
return nextID, nil
}
// appendToUnbatchedTXIndex add at the end when tx with same fee exists
func (k Keeper) appendToUnbatchedTXIndex(ctx sdk.Context, txID uint64) {
store := ctx.KVStore(k.storeKey)
idxKey := types.SecondIndexOutgoingTXFeeKey
var idSet types.IDSet
if store.Has(idxKey) {
bz := store.Get(idxKey)
k.cdc.MustUnmarshalBinaryBare(bz, &idSet)
}
idSet.Ids = append(idSet.Ids, txID)
store.Set(idxKey, k.cdc.MustMarshalBinaryBare(&idSet))
}
// appendToUnbatchedTXIndex add at the top when tx with same fee exists
func (k Keeper) prependToUnbatchedTXIndex(ctx sdk.Context, txID uint64) {
store := ctx.KVStore(k.storeKey)
idxKey := types.SecondIndexOutgoingTXFeeKey
var idSet types.IDSet
if store.Has(idxKey) {
bz := store.Get(idxKey)
k.cdc.MustUnmarshalBinaryBare(bz, &idSet)
}
idSet.Ids = append([]uint64{txID}, idSet.Ids...)
store.Set(idxKey, k.cdc.MustMarshalBinaryBare(&idSet))
}
// removeFromUnbatchedTXIndex removes the tx from the index and makes it implicit no available anymore
func (k Keeper) removeFromUnbatchedTXIndex(ctx sdk.Context, txID uint64) error {
store := ctx.KVStore(k.storeKey)
idxKey := types.SecondIndexOutgoingTXFeeKey
var idSet types.IDSet
bz := store.Get(idxKey)
if bz == nil {
return sdkerrors.Wrap(types.ErrUnknown, "fee")
}
k.cdc.MustUnmarshalBinaryBare(bz, &idSet)
for i := range idSet.Ids {
if idSet.Ids[i] == txID {
idSet.Ids = append(idSet.Ids[0:i], idSet.Ids[i+1:]...)
if len(idSet.Ids) != 0 {
store.Set(idxKey, k.cdc.MustMarshalBinaryBare(&idSet))
} else {
store.Delete(idxKey)
}
return nil
}
}
return sdkerrors.Wrap(types.ErrUnknown, "tx id")
}
func (k Keeper) setPoolEntry(ctx sdk.Context, id uint64, val *types.OutgoingTx) error {
bz, err := k.cdc.MarshalBinaryBare(val)
if err != nil {
return err
}
store := ctx.KVStore(k.storeKey)
store.Set(types.GetOutgoingTxPoolKey(id), bz)
return nil
}
func (k Keeper) getPoolEntry(ctx sdk.Context, id uint64) (*types.OutgoingTx, error) {
store := ctx.KVStore(k.storeKey)
bz := store.Get(types.GetOutgoingTxPoolKey(id))
if bz == nil {
return nil, types.ErrUnknown
}
var r types.OutgoingTx
k.cdc.UnmarshalBinaryBare(bz, &r)
return &r, nil
}
func (k Keeper) removePoolEntry(ctx sdk.Context, id uint64) {
store := ctx.KVStore(k.storeKey)
store.Delete(types.GetOutgoingTxPoolKey(id))
}
// IterateOutgoingPoolByFee itetates over the outgoing pool which is sorted by fee
func (k Keeper) IterateOutgoingPool(ctx sdk.Context, cb func(uint64, *types.OutgoingTx) bool) {
prefixStore := prefix.NewStore(ctx.KVStore(k.storeKey), types.SecondIndexOutgoingTXFeeKey)
iter := prefixStore.ReverseIterator(nil, nil)
defer iter.Close()
for ; iter.Valid(); iter.Next() {
var ids types.IDSet
k.cdc.MustUnmarshalBinaryBare(iter.Value(), &ids)
// cb returns true to stop early
for _, id := range ids.Ids {
tx, err := k.getPoolEntry(ctx, id)
if err != nil {
return
}
if cb(id, tx) {
return
}
}
}
return
}
func (k Keeper) autoIncrementID(ctx sdk.Context, idKey []byte) uint64 {
store := ctx.KVStore(k.storeKey)
bz := store.Get(idKey)
var id uint64 = 1
if bz != nil {
id = binary.BigEndian.Uint64(bz)
}
bz = sdk.Uint64ToBigEndian(id + 1)
store.Set(idKey, bz)
return id
}