Skip to content

Commit

Permalink
e2etest: autodetect faucet endpoint
Browse files Browse the repository at this point in the history
Signed-off-by: p4u <[email protected]>
  • Loading branch information
p4u committed Nov 11, 2023
1 parent 8f7962f commit 83844f1
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 21 deletions.
13 changes: 9 additions & 4 deletions apiclient/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 14 additions & 4 deletions apiclient/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
"",
Expand Down
6 changes: 5 additions & 1 deletion cmd/cli/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/end2endtest/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
30 changes: 21 additions & 9 deletions cmd/end2endtest/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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, "")
}
}

Expand Down
4 changes: 2 additions & 2 deletions cmd/end2endtest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 83844f1

Please sign in to comment.