Skip to content

Commit

Permalink
adjustment to the faucet server
Browse files Browse the repository at this point in the history
  • Loading branch information
lunfardo314 committed Feb 4, 2025
1 parent cacb5e6 commit c9c9eb0
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 41 deletions.
17 changes: 12 additions & 5 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ const (
PathGetAccountOutputs = PrefixAPIV1 + "/get_account_outputs"
PathGetAccountSimpleSiglockedOutputs = PrefixAPIV1 + "/get_account_simple_siglocked"
PathGetOutputsForAmount = PrefixAPIV1 + "/get_outputs_for_amount"
PathGetNonChainBalance = PrefixAPIV1 + "/get_nonchain_balance"
PathGetChainedOutputs = PrefixAPIV1 + "/get_chain_outputs"
PathGetChainOutput = PrefixAPIV1 + "/get_chain_output"
PathGetOutput = PrefixAPIV1 + "/get_output"
Expand Down Expand Up @@ -64,7 +65,7 @@ type (
// value is hex-encoded raw output data
Outputs map[string]string `json:"outputs,omitempty"`
// latest reliable branch used to extract outputs
LRBID string `json:"lrb_id"`
LRBID string `json:"lrbid"`
}

OutputDataWithID struct {
Expand All @@ -78,13 +79,13 @@ type (
Error
OutputDataWithID
// latest reliable branch used to extract chain ID
LRBID string `json:"lrb_id"`
LRBID string `json:"lrbid"`
}

Chains struct {
Error
Chains map[string]OutputDataWithID `json:"chains"`
LRBID string `json:"lrb_id"`
LRBID string `json:"lrbid"`
}

// OutputData is returned by 'get_output'
Expand All @@ -93,13 +94,13 @@ type (
// hex-encoded output data
OutputData string `json:"output_data,omitempty"`
// latest reliable branch used to extract output
LRBID string `json:"lrb_id"`
LRBID string `json:"lrbid"`
}

ChainedOutputs struct {
Error
Outputs map[string]string `json:"outputs,omitempty"`
LRBID string `json:"lrb_id"`
LRBID string `json:"lrbid"`
}

SyncInfo struct {
Expand Down Expand Up @@ -242,6 +243,12 @@ type (
Error
Branches []BranchData `json:"branches"`
}

Balance struct {
Error
Amount uint64 `json:"amount"`
LRBID string `json:"lrbid"`
}
)

const ErrGetOutputNotFound = "output not found"
Expand Down
23 changes: 23 additions & 0 deletions api/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,29 @@ func (c *APIClient) GetOutputsForAmount(addr ledger.AddressED25519, amount uint6
return ret, &retLRBID, sum, nil
}

// GetNonChainBalance total of outputs locked in the account but without chain constraint
func (c *APIClient) GetNonChainBalance(addr ledger.Accountable) (uint64, *ledger.TransactionID, error) {
path := fmt.Sprintf(api.PathGetNonChainBalance+"?addr=%s", addr.Source())
body, err := c.getBody(path)
if err != nil {
return 0, nil, err
}

var res api.Balance
err = json.Unmarshal(body, &res)
if err != nil {
return 0, nil, err
}
if res.Error.Error != "" {
return 0, nil, fmt.Errorf("from server: %s", res.Error.Error)
}
retLRBID, err := ledger.TransactionIDFromHexString(res.LRBID)
if err != nil {
return 0, nil, fmt.Errorf("while parsing transaction ID: %s", res.Error.Error)
}
return res.Amount, &retLRBID, nil
}

// GetChainedOutputs fetches all outputs of the account. Optionally sorts them on the server
func (c *APIClient) GetChainedOutputs(accountable ledger.Accountable) ([]*ledger.OutputWithChainID, *ledger.TransactionID, error) {
path := fmt.Sprintf(api.PathGetChainedOutputs+"?accountable=%s", accountable.String())
Expand Down
43 changes: 42 additions & 1 deletion api/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (srv *server) registerHandlers() {
srv.addHandler(api.PathGetAccountSimpleSiglockedOutputs, srv.getAccountSimpleSigLockedOutputs)
// GET request format: '/api/v1/get_outputs_for_amount?addr=<a(0x....)>&amount=<amount>'
srv.addHandler(api.PathGetOutputsForAmount, srv.getOutputsForAmount)
// GET request format: '/api/v1/get_nonchain_balance?addr=<a(0x....)>'
srv.addHandler(api.PathGetNonChainBalance, srv.getNonChainBalance)
// GET request format: '/api/v1/get_chained_outputs?accountable=<EasyFL source form of the accountable lock constraint>'
srv.addHandler(api.PathGetChainedOutputs, srv.getChainedOutputs)
// GET request format: '/api/v1/get_chain_output?chainid=<hex-encoded chain ID>'
Expand Down Expand Up @@ -228,6 +230,46 @@ func (srv *server) getAccountSimpleSigLockedOutputs(w http.ResponseWriter, r *ht
})
}

func (srv *server) getNonChainBalance(w http.ResponseWriter, r *http.Request) {
lst, ok := r.URL.Query()["addr"]
if !ok || len(lst) != 1 {
writeErr(w, "wrong parameter 'addr' in request 'get_balance_addr25519'")
return
}
targetAddr, err := ledger.AddressED25519FromSource(lst[0])
if err != nil {
writeErr(w, err.Error())
return
}
var resp api.Balance

err = srv.withLRB(func(rdr multistate.SugaredStateReader) error {
lrbid := rdr.GetStemOutput().ID.TransactionID()
resp.LRBID = lrbid.StringHex()
err1 := rdr.IterateOutputsForAccount(targetAddr, func(_ ledger.OutputID, o *ledger.Output) bool {
if o.Lock().Name() != ledger.AddressED25519Name {
return true
}
if _, idx := o.ChainConstraint(); idx != 0xff {
return true
}
resp.Amount += o.Amount()
return true
})
if err1 != nil {
return err1
}
return nil
})
respBin, err := json.MarshalIndent(resp, "", " ")
if err != nil {
writeErr(w, err.Error())
return
}
_, err = w.Write(respBin)
util.AssertNoError(err)
}

func (srv *server) getOutputsForAmount(w http.ResponseWriter, r *http.Request) {
lst, ok := r.URL.Query()["addr"]
if !ok || len(lst) != 1 {
Expand Down Expand Up @@ -287,7 +329,6 @@ func (srv *server) getOutputsForAmount(w http.ResponseWriter, r *http.Request) {
}
_, err = w.Write(respBin)
util.AssertNoError(err)

}

func (srv *server) getChainOutput(w http.ResponseWriter, r *http.Request) {
Expand Down
78 changes: 43 additions & 35 deletions proxi/node_cmd/faucet_srv.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ type (
}
)

var _fromChain bool

const (
minAmount = 1_000_000
defaultFaucetPort = 9500
Expand All @@ -59,37 +57,6 @@ func initFaucetServerCmd() *cobra.Command {
return cmd
}

func readFaucetServerConfigIn(sub *viper.Viper) (ret faucetServerConfig) {
glb.Assertf(sub != nil, "faucet server configuration has not found")
ret.fromChain = !sub.GetBool("use_wallet")
ret.port = sub.GetUint64("port")
if ret.port == 0 {
ret.port = defaultFaucetPort
}
ret.amount = sub.GetUint64("amount")
glb.Assertf(ret.amount >= minAmount, "amount must be greater than %s", util.Th(minAmount))
if ret.maxRequestsPerHour = sub.GetUint("max_requests_per_hour"); ret.maxRequestsPerHour == 0 {
ret.maxRequestsPerHour = 1
}
if ret.maxRequestsPerDay = sub.GetUint("max_requests_per_day"); ret.maxRequestsPerDay == 0 {
ret.maxRequestsPerDay = 1
}
return
}

func (fct *faucetServer) displayFaucetConfig() {
glb.Infof("faucet server configuration:")
glb.Infof(" amount: %d", fct.cfg.amount)
glb.Infof(" port: %d", fct.cfg.port)
glb.Infof(" wallet address: %s", fct.walletData.Account.String())
glb.Infof(" maximum number of requests per hour: %d, per day: %d", fct.cfg.maxRequestsPerHour, fct.cfg.maxRequestsPerDay)
if fct.cfg.fromChain {
glb.Infof(" funds will be drawn from sequencer %s", fct.walletData.Sequencer.String())
} else {
glb.Infof(" funds will be drawn from wallet address %s", fct.walletData.Account.String())
}
}

func runFaucetServerCmd(_ *cobra.Command, _ []string) {
glb.InitLedgerFromNode()
glb.Infof("\nstarting Proxima faucet server..\n")
Expand All @@ -107,9 +74,9 @@ func runFaucetServerCmd(_ *cobra.Command, _ []string) {
addressRequestList: make(map[string][]time.Time),
}

fct.displayFaucetConfig()

clnt := glb.GetClient()
fct.displayFaucetConfig(clnt)

if cfg.fromChain {
o, _, _, err := clnt.GetChainOutput(*glb.GetOwnSequencerID())
glb.AssertNoError(err)
Expand All @@ -122,6 +89,47 @@ func runFaucetServerCmd(_ *cobra.Command, _ []string) {
fct.run()
}

func readFaucetServerConfigIn(sub *viper.Viper) (ret faucetServerConfig) {
glb.Assertf(sub != nil, "faucet server configuration has not found")
ret.fromChain = !sub.GetBool("use_wallet")
ret.port = sub.GetUint64("port")
if ret.port == 0 {
ret.port = defaultFaucetPort
}
ret.amount = sub.GetUint64("amount")
glb.Assertf(ret.amount >= minAmount, "amount must be greater than %s", util.Th(minAmount))
if ret.maxRequestsPerHour = sub.GetUint("max_requests_per_hour"); ret.maxRequestsPerHour == 0 {
ret.maxRequestsPerHour = 1
}
if ret.maxRequestsPerDay = sub.GetUint("max_requests_per_day"); ret.maxRequestsPerDay == 0 {
ret.maxRequestsPerDay = 1
}
return
}

func (fct *faucetServer) displayFaucetConfig(clnt *client.APIClient) {
walletbalance, lrbid, err := clnt.GetNonChainBalance(fct.walletData.Account)
glb.AssertNoError(err)
glb.PrintLRB(lrbid)

glb.Infof("faucet server configuration:")
glb.Infof(" amount per request: %s", util.Th(fct.cfg.amount))
glb.Infof(" port: %d", fct.cfg.port)
glb.Infof(" wallet address: %s", fct.walletData.Account.String())
glb.Infof(" wallet balance: %s", util.Th(walletbalance))
glb.Infof(" tag-along amount: %d", glb.GetTagAlongFee())
glb.Infof(" tag-along sequencer: %s", glb.GetTagAlongSequencerID().String())
if fct.cfg.fromChain {
chainOut, _, _, err := clnt.GetChainOutput(*fct.walletData.Sequencer)
glb.AssertNoError(err)
glb.Infof(" funds will be drawn from: %s (balance %s)", fct.walletData.Sequencer.String(), util.Th(chainOut.Output.Amount()))

} else {
glb.Infof(" funds will be drawn from: %s (balance %s)", fct.walletData.Account.String(), util.Th(walletbalance))
}
glb.Infof(" maximum number of requests per hour: %d, per day: %d", fct.cfg.maxRequestsPerHour, fct.cfg.maxRequestsPerDay)
}

func (fct *faucetServer) handler(w http.ResponseWriter, r *http.Request) {
targetStr, ok := r.URL.Query()["addr"]
if !ok || len(targetStr) != 1 {
Expand Down

0 comments on commit c9c9eb0

Please sign in to comment.