Skip to content

Commit 1e3125e

Browse files
author
lunfardo314
committed
configurable bottom of the faucet
1 parent 4413ee4 commit 1e3125e

File tree

1 file changed

+74
-27
lines changed

1 file changed

+74
-27
lines changed

Diff for: proxi/node_cmd/faucet_srv.go

+74-27
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ type (
3131
port uint64
3232
maxRequestsPerHour uint
3333
maxRequestsPerDay uint
34+
bottom uint64
3435
}
3536

3637
faucetServer struct {
@@ -39,6 +40,7 @@ type (
3940
mutex sync.Mutex
4041
accountRequestList map[string][]time.Time
4142
addressRequestList map[string][]time.Time
43+
client *client.APIClient
4244
}
4345
)
4446

@@ -50,7 +52,7 @@ const (
5052
func initFaucetServerCmd() *cobra.Command {
5153
cmd := &cobra.Command{
5254
Use: "faucet",
53-
Short: `starts a faucet server on the current wallet`,
55+
Short: `starts a faucet server on the wallet`,
5456
Args: cobra.NoArgs,
5557
Run: runFaucetServerCmd,
5658
}
@@ -65,72 +67,117 @@ func runFaucetServerCmd(_ *cobra.Command, _ []string) {
6567
glb.Assertf(glb.GetTagAlongFee() > 0, "tag-along amount not specified")
6668
glb.Assertf(glb.GetTagAlongSequencerID() != nil, "tag-along sequencer not specified")
6769

68-
cfg := readFaucetServerConfigIn(viper.Sub("faucet"))
69-
7070
fct := &faucetServer{
71-
cfg: cfg,
7271
walletData: walletData,
7372
accountRequestList: make(map[string][]time.Time),
7473
addressRequestList: make(map[string][]time.Time),
74+
client: glb.GetClient(),
7575
}
76+
fct.readFaucetServerConfigIn()
7677

77-
clnt := glb.GetClient()
78-
fct.displayFaucetConfig(clnt)
78+
fct.displayFaucetConfig()
7979

80-
if cfg.fromChain {
81-
o, _, _, err := clnt.GetChainOutput(*glb.GetOwnSequencerID())
80+
if fct.cfg.fromChain {
81+
o, _, _, err := fct.client.GetChainOutput(*glb.GetOwnSequencerID())
8282
glb.AssertNoError(err)
83-
glb.Assertf(o.Output.Amount() > ledger.L().ID.MinimumAmountOnSequencer+cfg.amount,
83+
glb.Assertf(o.Output.Amount() > ledger.L().ID.MinimumAmountOnSequencer+fct.cfg.amount,
8484
"not enough balance on own sequencer %s", fct.walletData.Sequencer.String())
8585
} else {
86-
_, _, _, err := clnt.GetOutputsForAmount(walletData.Account, cfg.amount+glb.GetTagAlongFee())
86+
_, _, _, err := fct.client.GetOutputsForAmount(walletData.Account, fct.cfg.amount+glb.GetTagAlongFee())
8787
glb.AssertNoError(err)
8888
}
8989
fct.run()
9090
}
9191

92-
func readFaucetServerConfigIn(sub *viper.Viper) (ret faucetServerConfig) {
93-
glb.Assertf(sub != nil, "faucet server configuration has not found")
94-
ret.fromChain = !sub.GetBool("use_wallet")
95-
ret.port = sub.GetUint64("port")
96-
if ret.port == 0 {
97-
ret.port = defaultFaucetPort
92+
func (fct *faucetServer) readFaucetServerConfigIn() {
93+
sub := viper.Sub("faucet")
94+
glb.Assertf(sub != nil, "faucet server configuration is missing")
95+
fct.cfg.fromChain = !sub.GetBool("use_wallet_as_source")
96+
fct.cfg.port = sub.GetUint64("port")
97+
if fct.cfg.port == 0 {
98+
fct.cfg.port = defaultFaucetPort
9899
}
99-
ret.amount = sub.GetUint64("amount")
100-
glb.Assertf(ret.amount >= minAmount, "amount must be greater than %s", util.Th(minAmount))
101-
if ret.maxRequestsPerHour = sub.GetUint("max_requests_per_hour"); ret.maxRequestsPerHour == 0 {
102-
ret.maxRequestsPerHour = 1
100+
fct.cfg.amount = sub.GetUint64("amount")
101+
glb.Assertf(fct.cfg.amount >= minAmount, "amount must be greater than %s", util.Th(minAmount))
102+
if fct.cfg.maxRequestsPerHour = sub.GetUint("max_requests_per_hour"); fct.cfg.maxRequestsPerHour == 0 {
103+
fct.cfg.maxRequestsPerHour = 1
103104
}
104-
if ret.maxRequestsPerDay = sub.GetUint("max_requests_per_day"); ret.maxRequestsPerDay == 0 {
105-
ret.maxRequestsPerDay = 1
105+
if fct.cfg.maxRequestsPerDay = sub.GetUint("max_requests_per_day"); fct.cfg.maxRequestsPerDay == 0 {
106+
fct.cfg.maxRequestsPerDay = 1
106107
}
108+
fct.cfg.bottom = sub.GetUint64("bottom")
109+
if fct.cfg.bottom < fct.absoluteBottom() {
110+
fct.cfg.bottom = fct.absoluteBottom()
111+
}
112+
113+
err := fct.checkBottom()
114+
glb.AssertNoError(err)
107115
return
108116
}
109117

110-
func (fct *faucetServer) displayFaucetConfig(clnt *client.APIClient) {
111-
walletbalance, lrbid, err := clnt.GetNonChainBalance(fct.walletData.Account)
118+
func (fct *faucetServer) absoluteBottom() uint64 {
119+
if fct.cfg.fromChain {
120+
return ledger.L().ID.MinimumAmountOnSequencer + fct.cfg.amount
121+
}
122+
return fct.cfg.amount + glb.GetTagAlongFee()
123+
}
124+
125+
func (fct *faucetServer) checkBottom() error {
126+
abs := fct.absoluteBottom()
127+
if fct.cfg.fromChain {
128+
o, _, _, err := fct.client.GetChainOutput(*glb.GetOwnSequencerID())
129+
if err != nil {
130+
return err
131+
}
132+
if o.Output.Amount() < abs {
133+
return fmt.Errorf("not enough balance on own sequencer %s. Must be at least %s, got %s",
134+
fct.walletData.Sequencer.String(), util.Th(abs), util.Th(o.Output.Amount()))
135+
}
136+
} else {
137+
balance, _, err := fct.client.GetNonChainBalance(fct.walletData.Account)
138+
if err != nil {
139+
return err
140+
}
141+
if balance < abs {
142+
return fmt.Errorf("not enough balance on source address %s. Must be at least %s, got %s",
143+
fct.walletData.Account.String(), util.Th(abs), util.Th(balance))
144+
}
145+
}
146+
return nil
147+
}
148+
149+
func (fct *faucetServer) displayFaucetConfig() {
150+
walletBalance, lrbid, err := fct.client.GetNonChainBalance(fct.walletData.Account)
112151
glb.AssertNoError(err)
113152
glb.PrintLRB(lrbid)
114153

115154
glb.Infof("faucet server configuration:")
116155
glb.Infof(" amount per request: %s", util.Th(fct.cfg.amount))
117156
glb.Infof(" port: %d", fct.cfg.port)
118157
glb.Infof(" wallet address: %s", fct.walletData.Account.String())
119-
glb.Infof(" wallet balance: %s", util.Th(walletbalance))
158+
glb.Infof(" wallet balance: %s", util.Th(walletBalance))
120159
glb.Infof(" tag-along amount: %d", glb.GetTagAlongFee())
121160
glb.Infof(" tag-along sequencer: %s", glb.GetTagAlongSequencerID().String())
161+
glb.Infof(" bottom: %s", util.Th(fct.cfg.bottom))
122162
if fct.cfg.fromChain {
123-
chainOut, _, _, err := clnt.GetChainOutput(*fct.walletData.Sequencer)
163+
chainOut, _, _, err := fct.client.GetChainOutput(*fct.walletData.Sequencer)
124164
glb.AssertNoError(err)
125165
glb.Infof(" funds will be drawn from: %s (balance %s)", fct.walletData.Sequencer.String(), util.Th(chainOut.Output.Amount()))
126166

127167
} else {
128-
glb.Infof(" funds will be drawn from: %s (balance %s)", fct.walletData.Account.String(), util.Th(walletbalance))
168+
glb.Infof(" funds will be drawn from: %s (balance %s)", fct.walletData.Account.String(), util.Th(walletBalance))
129169
}
130170
glb.Infof(" maximum number of requests per hour: %d, per day: %d", fct.cfg.maxRequestsPerHour, fct.cfg.maxRequestsPerDay)
131171
}
132172

133173
func (fct *faucetServer) handler(w http.ResponseWriter, r *http.Request) {
174+
err := fct.checkBottom()
175+
if err != nil {
176+
glb.Infof("error from checkBottom: %s", err.Error())
177+
writeResponse(w, err.Error())
178+
return
179+
}
180+
134181
targetStr, ok := r.URL.Query()["addr"]
135182
if !ok || len(targetStr) != 1 {
136183
writeResponse(w, "wrong parameter 'addr' in request 'get_funds'")

0 commit comments

Comments
 (0)