From 72e37942f3f777129b6cfa2d28b1ba7bbdf6437a Mon Sep 17 00:00:00 2001
From: Mudit Gupta <guptamudit@ymail.com>
Date: Wed, 14 Apr 2021 03:21:46 +0530
Subject: [PATCH] cmd/faucet: support testnet flags in the faucet (#22545)

Co-authored-by: Felix Lange <fjl@twurst.com>
Co-authored-by: Martin Holst Swende <martin@swende.se>
---
 cmd/faucet/faucet.go | 30 +++++++++++++++++++++++-------
 1 file changed, 23 insertions(+), 7 deletions(-)

diff --git a/cmd/faucet/faucet.go b/cmd/faucet/faucet.go
index e839f1c88..bb5375384 100644
--- a/cmd/faucet/faucet.go
+++ b/cmd/faucet/faucet.go
@@ -85,6 +85,9 @@ var (
 
 	twitterTokenFlag   = flag.String("twitter.token", "", "Bearer token to authenticate with the v2 Twitter API")
 	twitterTokenV1Flag = flag.String("twitter.token.v1", "", "Bearer token to authenticate with the v1.1 Twitter API")
+
+	goerliFlag  = flag.Bool("goerli", false, "Initializes the faucet with Görli network config")
+	rinkebyFlag = flag.Bool("rinkeby", false, "Initializes the faucet with Rinkeby network config")
 )
 
 var (
@@ -144,13 +147,9 @@ func main() {
 		log.Crit("Failed to render the faucet template", "err", err)
 	}
 	// Load and parse the genesis block requested by the user
-	blob, err := ioutil.ReadFile(*genesisFlag)
+	genesis, err := getGenesis(genesisFlag, *goerliFlag, *rinkebyFlag)
 	if err != nil {
-		log.Crit("Failed to read genesis block contents", "genesis", *genesisFlag, "err", err)
-	}
-	genesis := new(core.Genesis)
-	if err = json.Unmarshal(blob, genesis); err != nil {
-		log.Crit("Failed to parse genesis block json", "err", err)
+		log.Crit("Failed to parse genesis config", "err", err)
 	}
 	// Convert the bootnodes to internal enode representations
 	var enodes []*enode.Node
@@ -162,7 +161,8 @@ func main() {
 		}
 	}
 	// Load up the account key and decrypt its password
-	if blob, err = ioutil.ReadFile(*accPassFlag); err != nil {
+	blob, err := ioutil.ReadFile(*accPassFlag)
+	if err != nil {
 		log.Crit("Failed to read account password contents", "file", *accPassFlag, "err", err)
 	}
 	pass := strings.TrimSuffix(string(blob), "\n")
@@ -884,3 +884,19 @@ func authNoAuth(url string) (string, string, common.Address, error) {
 	}
 	return address.Hex() + "@noauth", "", address, nil
 }
+
+// getGenesis returns a genesis based on input args
+func getGenesis(genesisFlag *string, goerliFlag bool, rinkebyFlag bool) (*core.Genesis, error) {
+	switch {
+	case genesisFlag != nil:
+		var genesis core.Genesis
+		err := common.LoadJSON(*genesisFlag, &genesis)
+		return &genesis, err
+	case goerliFlag:
+		return core.DefaultGoerliGenesisBlock(), nil
+	case rinkebyFlag:
+		return core.DefaultRinkebyGenesisBlock(), nil
+	default:
+		return nil, fmt.Errorf("no genesis flag provided")
+	}
+}
-- 
GitLab