-
Notifications
You must be signed in to change notification settings - Fork 4
/
limiter.go
executable file
·155 lines (135 loc) · 4.42 KB
/
limiter.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
package main
import (
"time"
rpcclient "github.com/cometbft/cometbft/rpc/client"
httpclient "github.com/cometbft/cometbft/rpc/client/http"
sdkerrors "cosmossdk.io/errors"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
errortypes "github.com/cosmos/cosmos-sdk/types/errors"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
odin "github.com/ODIN-PROTOCOL/odin-core/app"
)
const (
TickerUpdatePeriod = 30 * time.Second
)
// Limiter defines service for limiting faucet withdrawals.
type Limiter struct {
ctx *Context
store LimitStore
client rpcclient.Client
ticker *time.Ticker
keys chan keyring.Record
}
// NewLimiter creates a new limiter.
func NewLimiter(ctx *Context) *Limiter {
httpClient, err := httpclient.New(faucet.config.NodeURI, "/websocket")
if err != nil {
panic(sdkerrors.Wrap(err, "failed to create http client"))
}
kb, err := faucet.keybase.List()
if err != nil {
panic(sdkerrors.Wrap(err, "failed to retrieve keys from keybase"))
}
if len(kb) == 0 {
panic(sdkerrors.Wrap(errortypes.ErrKeyNotFound, "there are no available keys"))
}
keys := make(chan keyring.Record, len(kb))
for _, key := range kb {
keys <- *key
}
return &Limiter{
ctx: ctx,
store: NewLimitStore(),
client: httpClient,
ticker: time.NewTicker(TickerUpdatePeriod),
keys: keys,
}
}
// runCleaner removes deprecated limits per period.
func (l *Limiter) runCleaner() {
for {
select {
case <-l.ticker.C:
l.store.Clean(faucet.config.Period)
}
}
}
// allowed implements Limiter interface.
func (l *Limiter) allowed(rawAddress, denom string) (*WithdrawalLimit, bool) {
limit, ok := l.store.Get(rawAddress)
if !ok {
return nil, true
}
if time.Now().Sub(limit.LastWithdrawals[denom]) > faucet.config.Period {
return limit, true
}
if limit.WithdrawalAmount.AmountOf(denom).LT(l.ctx.maxPerPeriodWithdrawal.AmountOf(denom)) {
return limit, true
}
return limit, false
}
// updateLimitation updates the limitations of account by the given address.
func (l *Limiter) updateLimitation(address, denom string, coins sdk.Coins) {
withdrawalLimit, ok := l.store.Get(address)
if !ok {
withdrawalLimit = &WithdrawalLimit{
LastWithdrawals: make(map[string]time.Time),
WithdrawalAmount: sdk.NewCoins(),
}
}
withdrawalLimit.LastWithdrawals[denom] = time.Now()
withdrawalLimit.WithdrawalAmount = withdrawalLimit.WithdrawalAmount.Add(coins...)
l.store.Set(address, withdrawalLimit)
}
// transferCoinsToClaimer transfers coins from faucet accounts to the claimer.
func (l *Limiter) transferCoinsToClaimer(key keyring.Record, to sdk.AccAddress, amt sdk.Coins) (*sdk.TxResponse, error) {
address, error := key.GetAddress()
if error != nil {
return nil, sdkerrors.Wrap(error, "Error when retrieving address")
}
msg := banktypes.NewMsgSend(address, to, amt)
if err := msg.ValidateBasic(); err != nil {
return nil, sdkerrors.Wrapf(err, "failed to validate a message: %s", msg.String())
}
clientCtx := client.Context{
Client: l.client,
TxConfig: odin.MakeEncodingConfig().TxConfig,
BroadcastMode: flags.BroadcastAsync,
InterfaceRegistry: odin.MakeEncodingConfig().InterfaceRegistry,
}
accountRetriever := authtypes.AccountRetriever{}
acc, err := accountRetriever.GetAccount(clientCtx, address)
if err != nil {
return nil, sdkerrors.Wrapf(err, "failed to the account: %s", acc)
}
txf := tx.Factory{}.
WithAccountNumber(acc.GetAccountNumber()).
WithSequence(acc.GetSequence()).
WithTxConfig(odin.MakeEncodingConfig().TxConfig).
WithGas(GasAmount).WithGasAdjustment(GasAdjustment).
WithChainID(faucet.config.ChainID).
WithMemo("").
WithGasPrices(l.ctx.gasPrices.String()).
WithKeybase(faucet.keybase).
WithAccountRetriever(clientCtx.AccountRetriever)
txb, err := txf.BuildUnsignedTx(msg)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to build unsigned tx")
}
err = tx.Sign(txf, key.Name, txb, true)
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to sign tx")
}
txBytes, err := clientCtx.TxConfig.TxEncoder()(txb.GetTx())
if err != nil {
return nil, sdkerrors.Wrap(err, "failed to encode tx")
}
// broadcast to a Tendermint node
res, err := clientCtx.BroadcastTx(txBytes)
return res, sdkerrors.Wrap(err, "failed to broadcast tx commit")
}