From dd7f197db3c220119261cfae93eb550b9493cae2 Mon Sep 17 00:00:00 2001
From: Alex Sharov <AskAlexSharov@gmail.com>
Date: Tue, 7 Sep 2021 16:12:49 +0700
Subject: [PATCH] Devnet private key (#2639)

---
 cmd/utils/flags.go                          | 30 +++++++++------------
 core/genesis.go                             |  3 +++
 eth/backend.go                              |  1 -
 eth/stagedsync/stage_mining_create_block.go |  2 +-
 turbo/cli/default_flags.go                  |  2 +-
 5 files changed, 18 insertions(+), 20 deletions(-)

diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 881c4db001..0aaecb58f3 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -232,8 +232,8 @@ var (
 		Usage: "Public address for block mining rewards",
 		Value: "0",
 	}
-	MinerSigningKeyFlag = cli.StringFlag{
-		Name:  "miner.sigkey",
+	MinerSigningKeyFileFlag = cli.StringFlag{
+		Name:  "miner.sigfile",
 		Usage: "Private key to sign blocks with",
 		Value: "",
 	}
@@ -774,22 +774,18 @@ func SplitAndTrim(input string) (ret []string) {
 // setEtherbase retrieves the etherbase from the directly specified
 // command line flags.
 func setEtherbase(ctx *cli.Context, cfg *ethconfig.Config) {
-	if ctx.GlobalIsSet(MinerSigningKeyFlag.Name) {
-		sigkey := ctx.GlobalString(MinerSigningKeyFlag.Name)
-		if sigkey != "" {
-			var err error
-			cfg.Miner.SigKey, err = crypto.HexToECDSA(sigkey)
-			if err != nil {
-				Fatalf("Failed to parse ECDSA private key: %v", err)
-			}
-			cfg.Miner.Etherbase = crypto.PubkeyToAddress(cfg.Miner.SigKey.PublicKey)
-		}
-	} else if ctx.GlobalIsSet(MinerEtherbaseFlag.Name) {
-		etherbase := ctx.GlobalString(MinerEtherbaseFlag.Name)
+	var etherbase string
+	if ctx.GlobalIsSet(MinerEtherbaseFlag.Name) {
+		etherbase = ctx.GlobalString(MinerEtherbaseFlag.Name)
 		if etherbase != "" {
 			cfg.Miner.Etherbase = common.HexToAddress(etherbase)
 		}
 	}
+
+	if etherbase == "" && ctx.GlobalString(ChainFlag.Name) == params.DevChainName {
+		cfg.Miner.SigKey = core.DevnetSignPrivateKey
+		cfg.Miner.Etherbase = core.DevnetEtherbase
+	}
 }
 
 func SetP2PConfig(ctx *cli.Context, cfg *p2p.Config, nodeName, dataDir string) {
@@ -994,7 +990,7 @@ func SetupMinerCobra(cmd *cobra.Command, cfg *params.MiningConfig) {
 		panic(err)
 	}
 	if cfg.Enabled && len(cfg.Etherbase.Bytes()) == 0 {
-		panic(fmt.Sprintf("Erigon supports only remote miners. Flag --%s or --%s is required", MinerNotifyFlag.Name, MinerSigningKeyFlag.Name))
+		panic(fmt.Sprintf("Erigon supports only remote miners. Flag --%s or --%s is required", MinerNotifyFlag.Name, MinerSigningKeyFileFlag.Name))
 	}
 	cfg.Notify, err = flags.GetStringArray(MinerNotifyFlag.Name)
 	if err != nil {
@@ -1061,7 +1057,7 @@ func setMiner(ctx *cli.Context, cfg *params.MiningConfig) {
 		cfg.Enabled = true
 	}
 	if cfg.Enabled && len(cfg.Etherbase.Bytes()) == 0 {
-		panic(fmt.Sprintf("Erigon supports only remote miners. Flag --%s or --%s is required", MinerNotifyFlag.Name, MinerSigningKeyFlag.Name))
+		panic(fmt.Sprintf("Erigon supports only remote miners. Flag --%s or --%s is required", MinerNotifyFlag.Name, MinerSigningKeyFileFlag.Name))
 	}
 	if ctx.GlobalIsSet(MinerNotifyFlag.Name) {
 		cfg.Notify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",")
@@ -1152,7 +1148,7 @@ func CheckExclusive(ctx *cli.Context, args ...interface{}) {
 
 // SetEthConfig applies eth-related command line flags to the config.
 func SetEthConfig(ctx *cli.Context, nodeConfig *node.Config, cfg *ethconfig.Config) {
-	CheckExclusive(ctx, MinerSigningKeyFlag, MinerEtherbaseFlag)
+	CheckExclusive(ctx, MinerSigningKeyFileFlag, MinerEtherbaseFlag)
 	setEtherbase(ctx, cfg)
 	setGPO(ctx, &cfg.GPO)
 	setTxPool(ctx, &cfg.TxPool)
diff --git a/core/genesis.go b/core/genesis.go
index 18b1032b05..2d41130148 100644
--- a/core/genesis.go
+++ b/core/genesis.go
@@ -601,6 +601,9 @@ func DefaultSokolGenesisBlock() *Genesis {
 	}
 }
 
+var DevnetSignPrivateKey, _ = crypto.HexToECDSA("289c2857d4598e37fb9647507e47a309d6133539bf21a8b9cb6df88fd5232032")
+var DevnetEtherbase = common.HexToAddress("970e8128ab834e8eac17ab8e3812f010678cf791") // crypto.PubkeyToAddress(DevnetSignPrivateKey.PublicKey)
+
 // DeveloperGenesisBlock returns the 'geth --dev' genesis block.
 func DeveloperGenesisBlock(period uint64, faucet common.Address) *Genesis {
 	// Override the default period to the user requested one
diff --git a/eth/backend.go b/eth/backend.go
index 004dc42ca4..c9d8cb72b4 100644
--- a/eth/backend.go
+++ b/eth/backend.go
@@ -533,7 +533,6 @@ func (s *Ethereum) StartMining(ctx context.Context, kv kv.RwDB, mining *stagedsy
 	if !cfg.Enabled {
 		return nil
 	}
-
 	s.txPool.SetGasPrice(gasPrice)
 
 	// Configure the local mining address
diff --git a/eth/stagedsync/stage_mining_create_block.go b/eth/stagedsync/stage_mining_create_block.go
index 0483807f5f..8832a711e0 100644
--- a/eth/stagedsync/stage_mining_create_block.go
+++ b/eth/stagedsync/stage_mining_create_block.go
@@ -104,6 +104,7 @@ func SpawnMiningCreateBlockStage(s *StageState, tx kv.RwTx, cfg MiningCreateBloc
 	if parent == nil { // todo: how to return error and don't stop Erigon?
 		return fmt.Errorf(fmt.Sprintf("[%s] Empty block", logPrefix), "blocknum", executionAt)
 	}
+	log.Info(fmt.Sprintf("[%s] Start mine", logPrefix), "block", executionAt+1)
 
 	blockNum := executionAt + 1
 	signer := types.MakeSigner(&cfg.chainConfig, blockNum)
@@ -285,7 +286,6 @@ func SpawnMiningCreateBlockStage(s *StageState, tx kv.RwTx, cfg MiningCreateBloc
 
 	current.LocalTxs = types.NewTransactionsByPriceAndNonce(*signer, localTxs)
 	current.RemoteTxs = types.NewTransactionsByPriceAndNonce(*signer, remoteTxs)
-	fmt.Printf("aa: %t, %t,%t\n", current == nil, cfg.miner.MiningBlock == nil, current.Header == nil)
 	return nil
 }
 
diff --git a/turbo/cli/default_flags.go b/turbo/cli/default_flags.go
index 2ec02127da..a585271e9e 100644
--- a/turbo/cli/default_flags.go
+++ b/turbo/cli/default_flags.go
@@ -78,6 +78,6 @@ var DefaultFlags = []cli.Flag{
 	utils.MinerEtherbaseFlag,
 	utils.MinerExtraDataFlag,
 	utils.MinerNoVerfiyFlag,
-	utils.MinerSigningKeyFlag,
+	utils.MinerSigningKeyFileFlag,
 	utils.SentryAddrFlag,
 }
-- 
GitLab