From 83844f143d8cdfd69a5abda89ef1a536688180a2 Mon Sep 17 00:00:00 2001 From: p4u Date: Fri, 10 Nov 2023 10:04:58 +0100 Subject: [PATCH] e2etest: autodetect faucet endpoint Signed-off-by: p4u --- apiclient/account.go | 13 +++++++++---- apiclient/helpers.go | 18 ++++++++++++++---- cmd/cli/main.go | 6 +++++- cmd/end2endtest/account.go | 2 +- cmd/end2endtest/helpers.go | 30 +++++++++++++++++++++--------- cmd/end2endtest/main.go | 4 ++-- 6 files changed, 52 insertions(+), 21 deletions(-) diff --git a/apiclient/account.go b/apiclient/account.go index 22fa17f18..00840dcad 100644 --- a/apiclient/account.go +++ b/apiclient/account.go @@ -15,10 +15,15 @@ import ( "google.golang.org/protobuf/proto" ) -const ( - // DefaultDevelopmentFaucetURL is the default URL for the development faucet which can be used freely. - DefaultDevelopmentFaucetURL = "https://api-dev.faucet.vocdoni.net/v2/open/claim/" -) +// DefaultFaucetURLs is a map of default faucet URLs for each network. +var DefaultFaucetURLs = map[string]string{ + "dev": "https://api-dev.faucet.vocdoni.net/v2/open/claim/", + "develop": "https://api-dev.faucet.vocdoni.net/v2/open/claim/", + "stg": "https://api-stg.faucet.vocdoni.net/v2/open/claim", + "stage": "https://api-stg.faucet.vocdoni.net/v2/open/claim", + "lts": "https://api-faucet.vocdoni.io/v2/open/claim/", + "prod": "https://api-faucet.vocdoni.io/v2/open/claim/", +} var ( // ErrAccountNotConfigured is returned when the client has not been configured with an account. diff --git a/apiclient/helpers.go b/apiclient/helpers.go index d411158c9..9dd2993cd 100644 --- a/apiclient/helpers.go +++ b/apiclient/helpers.go @@ -255,13 +255,23 @@ func (c *HTTPclient) EncryptionKeys(electionID types.HexBytes) ([]api.Key, error return keysEnc, nil } -// GetFaucetPackageFromDevService returns a faucet package. -// Needs just the destination wallet address, the URL and bearer token are hardcoded -func GetFaucetPackageFromDevService(account string) (*models.FaucetPackage, error) { - url, err := util.BuildURL(DefaultDevelopmentFaucetURL, account) +// GetFaucetPackageFromDefaultService returns a faucet package. +// Needs the destination wallet address and the chainID. +func GetFaucetPackageFromDefaultService(account string, chainID string) (*models.FaucetPackage, error) { + faucetChainID := chainID + chainIDsplit := strings.Split(chainID, "/") + if len(chainIDsplit) > 1 { + faucetChainID = strings.ToLower(chainIDsplit[1]) + } + baseURL, ok := DefaultFaucetURLs[faucetChainID] + if !ok { + return nil, fmt.Errorf("no default faucet URL for network %s", chainID) + } + url, err := util.BuildURL(baseURL, account) if err != nil { return nil, err } + log.Infow("requesting faucet package", "url", url) return GetFaucetPackageFromRemoteService( url, "", diff --git a/cmd/cli/main.go b/cmd/cli/main.go index 5dfa15f2f..e0593825d 100644 --- a/cmd/cli/main.go +++ b/cmd/cli/main.go @@ -305,8 +305,12 @@ func bootStrapAccount(cli *VocdoniCLI) error { return err } } else { + info, err := cli.api.ChainInfo() + if err != nil { + return err + } infoPrint.Printf("trying to fetch faucet package from default remote service...\n") - faucetPkg, err = apiclient.GetFaucetPackageFromDevService(cli.api.MyAddress().Hex()) + faucetPkg, err = apiclient.GetFaucetPackageFromDefaultService(cli.api.MyAddress().Hex(), info.ID) if err != nil { return err } diff --git a/cmd/end2endtest/account.go b/cmd/end2endtest/account.go index 434a9654a..d29a7cd85 100644 --- a/cmd/end2endtest/account.go +++ b/cmd/end2endtest/account.go @@ -57,7 +57,7 @@ func (t *E2ETokenTxs) Setup(api *apiclient.HTTPclient, config *config) error { // get faucet package for alice var err error - t.aliceFP, err = faucetPackage(t.config.faucet, t.config.faucetAuthToken, t.alice.Address().Hex()) + t.aliceFP, err = faucetPackage(t.config.faucet, t.alice.Address().Hex()) if err != nil { return err } diff --git a/cmd/end2endtest/helpers.go b/cmd/end2endtest/helpers.go index 9eb3d90cd..ea863a160 100644 --- a/cmd/end2endtest/helpers.go +++ b/cmd/end2endtest/helpers.go @@ -87,10 +87,18 @@ func (t *e2eElection) createAccount(privateKey string) (*vapi.Account, *apiclien } address := accountApi.MyAddress().Hex() - - faucetPkg, err := faucetPackage(t.config.faucet, t.config.faucetAuthToken, address) - if err != nil { - return nil, nil, err + faucetPkg := &models.FaucetPackage{} + var err error + if t.config.faucet == "" { + faucetPkg, err = apiclient.GetFaucetPackageFromDefaultService(address, t.api.ChainID()) + if err != nil { + return nil, nil, fmt.Errorf("could not get faucet package from default service: %w", err) + } + } else { + faucetPkg, err = faucetPackage(t.config.faucet, address) + if err != nil { + return nil, nil, err + } } accountMetadata := &vapi.AccountMetadata{ @@ -568,18 +576,22 @@ func (t *e2eElection) sendVotes(votes []*apiclient.VoteData) map[int]error { return errs } -func faucetPackage(faucet, faucetAuthToken, myAddress string) (*models.FaucetPackage, error) { - switch faucet { +func faucetPackage(faucetURL, myAddress string) (*models.FaucetPackage, error) { + switch faucetURL { case "": return nil, fmt.Errorf("need to pass a valid URL (--faucet)") case "dev": - return apiclient.GetFaucetPackageFromDevService(myAddress) + return apiclient.GetFaucetPackageFromDefaultService(myAddress, "dev") + case "stg", "stage": + return apiclient.GetFaucetPackageFromDefaultService(myAddress, "stg") + case "prod", "lts": + return apiclient.GetFaucetPackageFromDefaultService(myAddress, "lts") default: - url, err := util.BuildURL(faucet, myAddress) + url, err := util.BuildURL(faucetURL, myAddress) if err != nil { return nil, err } - return apiclient.GetFaucetPackageFromRemoteService(url, faucetAuthToken) + return apiclient.GetFaucetPackageFromRemoteService(url, "") } } diff --git a/cmd/end2endtest/main.go b/cmd/end2endtest/main.go index e56dc11fe..05519e8d5 100644 --- a/cmd/end2endtest/main.go +++ b/cmd/end2endtest/main.go @@ -84,8 +84,8 @@ func main() { "account private key (optional)") flag.IntVar(&c.nvotes, "votes", 10, "number of votes to cast") flag.IntVar(&c.parallelCount, "parallel", 4, "number of parallel requests") - flag.StringVar(&c.faucet, "faucet", "dev", - "faucet URL for fetching tokens (special keyword 'dev' translates into hardcoded URL for dev faucet)") + flag.StringVar(&c.faucet, "faucet", "", + "faucet URL for fetching tokens (if empty, default faucet URL will be used)") flag.StringVar(&c.faucetAuthToken, "faucetAuthToken", "", "(optional) token passed as Bearer when fetching faucetURL") flag.DurationVar(&c.timeout, "timeout", apiclient.WaitTimeout, "timeout duration of each step")