From e8147cf7c6f508910698e6743ad347c78010ffe3 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 14 May 2014 12:41:30 +0200 Subject: [PATCH] Refactored mining into utils and exposed it to ethereal. Partly fixes #43 --- ethereal/ethereum.go | 4 ++++ ethereum/ethereum.go | 25 +------------------------ utils/cmd.go | 31 +++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 24 deletions(-) create mode 100644 utils/cmd.go diff --git a/ethereal/ethereum.go b/ethereal/ethereum.go index 15a454bdf..32c16f64f 100644 --- a/ethereal/ethereum.go +++ b/ethereal/ethereum.go @@ -99,6 +99,10 @@ func main() { os.Exit(0) } + if StartMining { + utils.DoMining(ethereum) + } + if StartRpc { ethereum.RpcServer, err = ethrpc.NewJsonRpcServer(ethpub.NewPEthereum(ethereum), RpcPort) if err != nil { diff --git a/ethereum/ethereum.go b/ethereum/ethereum.go index 055cc0bc4..207e61c88 100644 --- a/ethereum/ethereum.go +++ b/ethereum/ethereum.go @@ -1,11 +1,9 @@ package main import ( - "encoding/hex" "fmt" "github.com/ethereum/eth-go" "github.com/ethereum/eth-go/ethchain" - "github.com/ethereum/eth-go/ethminer" "github.com/ethereum/eth-go/ethpub" "github.com/ethereum/eth-go/ethrpc" "github.com/ethereum/eth-go/ethutil" @@ -127,28 +125,7 @@ func main() { ethereum.Mining = StartMining if StartMining { - logger.Infoln("Miner started") - - // Fake block mining. It broadcasts a new block every 5 seconds - go func() { - - if StartMining { - logger.Infoln("Miner started") - - go func() { - data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) - keyRing := ethutil.NewValueFromBytes(data) - addr := keyRing.Get(0).Bytes() - - pair, _ := ethchain.NewKeyPairFromSec(ethutil.FromHex(hex.EncodeToString(addr))) - - miner := ethminer.NewDefaultMiner(pair.Address(), ethereum) - miner.Start() - - }() - } - }() - + utils.DoMining(ethereum) } if StartConsole { diff --git a/utils/cmd.go b/utils/cmd.go new file mode 100644 index 000000000..a99fd9eed --- /dev/null +++ b/utils/cmd.go @@ -0,0 +1,31 @@ +package utils + +import ( + "encoding/hex" + "github.com/ethereum/eth-go" + "github.com/ethereum/eth-go/ethchain" + "github.com/ethereum/eth-go/ethminer" + _ "github.com/ethereum/eth-go/ethrpc" + "github.com/ethereum/eth-go/ethutil" + "log" +) + +func DoMining(ethereum *eth.Ethereum) { + // Set Mining status + ethereum.Mining = true + + log.Println("Miner started") + + // Fake block mining. It broadcasts a new block every 5 seconds + go func() { + data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) + keyRing := ethutil.NewValueFromBytes(data) + addr := keyRing.Get(0).Bytes() + + pair, _ := ethchain.NewKeyPairFromSec(ethutil.FromHex(hex.EncodeToString(addr))) + + miner := ethminer.NewDefaultMiner(pair.Address(), ethereum) + miner.Start() + + }() +}