From e8147cf7c6f508910698e6743ad347c78010ffe3 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 14 May 2014 12:41:30 +0200 Subject: Refactored mining into utils and exposed it to ethereal. Partly fixes #43 --- utils/cmd.go | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 utils/cmd.go (limited to 'utils') 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() + + }() +} -- cgit v1.2.3 From 2012e0c67abd8e3012a3eb1fae2e282e2a442d01 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 14 May 2014 13:26:15 +0200 Subject: Rewritten a check to only start mining once we are caught up with all peers --- utils/cmd.go | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) (limited to 'utils') diff --git a/utils/cmd.go b/utils/cmd.go index a99fd9eed..44924ce91 100644 --- a/utils/cmd.go +++ b/utils/cmd.go @@ -8,24 +8,27 @@ import ( _ "github.com/ethereum/eth-go/ethrpc" "github.com/ethereum/eth-go/ethutil" "log" + "time" ) 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() { + // Give it some time to connect with peers + time.Sleep(3 * time.Second) + + for ethereum.IsUpToDate() == false { + time.Sleep(5 * time.Second) + } + log.Println("Miner started") + 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() - }() } -- cgit v1.2.3 From 9fce273ce97a8db091a0bf9d0b503a2ea7261f81 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 14 May 2014 13:32:49 +0200 Subject: Refactored RPC client to utils so it can be reused --- utils/cmd.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'utils') diff --git a/utils/cmd.go b/utils/cmd.go index 44924ce91..5a100ca4f 100644 --- a/utils/cmd.go +++ b/utils/cmd.go @@ -5,12 +5,23 @@ import ( "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/ethpub" + "github.com/ethereum/eth-go/ethrpc" "github.com/ethereum/eth-go/ethutil" "log" "time" ) +func DoRpc(ethereum *eth.Ethereum, RpcPort int) { + var err error + ethereum.RpcServer, err = ethrpc.NewJsonRpcServer(ethpub.NewPEthereum(ethereum), RpcPort) + if err != nil { + log.Println("Could not start RPC interface:", err) + } else { + go ethereum.RpcServer.Start() + } +} + func DoMining(ethereum *eth.Ethereum) { // Set Mining status ethereum.Mining = true -- cgit v1.2.3 From f18ec51cb3959cc662bfc7b84314cd1d3b1541b5 Mon Sep 17 00:00:00 2001 From: obscuren Date: Wed, 14 May 2014 13:55:08 +0200 Subject: Switched to new keyring methods --- utils/cmd.go | 11 +++-------- utils/keys.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 8 deletions(-) (limited to 'utils') diff --git a/utils/cmd.go b/utils/cmd.go index a99fd9eed..654014169 100644 --- a/utils/cmd.go +++ b/utils/cmd.go @@ -1,9 +1,7 @@ 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" @@ -18,13 +16,10 @@ func DoMining(ethereum *eth.Ethereum) { // 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() + keyPair := ethutil.GetKeyRing().Get(0) + addr := keyPair.Address() - pair, _ := ethchain.NewKeyPairFromSec(ethutil.FromHex(hex.EncodeToString(addr))) - - miner := ethminer.NewDefaultMiner(pair.Address(), ethereum) + miner := ethminer.NewDefaultMiner(addr, ethereum) miner.Start() }() diff --git a/utils/keys.go b/utils/keys.go index 2f39c10b2..1c0f6a9d6 100644 --- a/utils/keys.go +++ b/utils/keys.go @@ -6,6 +6,60 @@ import ( "github.com/obscuren/secp256k1-go" ) +func CreateKeyPair(force bool) { + if force { + ethutil.GetKeyRing().Reset() + fmt.Println("resetting") + } + + if ethutil.GetKeyRing().Get(0) == nil { + _, prv := secp256k1.GenerateKeyPair() + + keyPair, err := ethutil.GetKeyRing().NewKeyPair(prv) + if err != nil { + panic(err) + } + + mne := ethutil.MnemonicEncode(ethutil.Hex(keyPair.PrivateKey)) + + fmt.Printf(` +Generating new address and keypair. +Please keep your keys somewhere save. + +++++++++++++++++ KeyRing +++++++++++++++++++ +addr: %x +prvk: %x +pubk: %x +++++++++++++++++++++++++++++++++++++++++++++ +save these words so you can restore your account later: %s +`, keyPair.Address(), keyPair.PrivateKey, keyPair.PublicKey, mne) + } +} + +func ImportPrivateKey(sec string) { + ethutil.GetKeyRing().Reset() + + keyPair, err := ethutil.GetKeyRing().NewKeyPair(ethutil.FromHex(sec)) + if err != nil { + panic(err) + } + + mne := ethutil.MnemonicEncode(ethutil.Hex(keyPair.PrivateKey)) + + fmt.Printf(` +Generating new address and keypair. +Please keep your keys somewhere save. + +++++++++++++++++ KeyRing +++++++++++++++++++ +addr: %x +prvk: %x +pubk: %x +++++++++++++++++++++++++++++++++++++++++++++ +save these words so you can restore your account later: %s +`, keyPair.Address(), keyPair.PrivateKey, keyPair.PublicKey, mne) +} + +/* func CreateKeyPair(force bool) { data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) if len(data) == 0 || force { @@ -28,7 +82,9 @@ save these words so you can restore your account later: %s } } +*/ +/* func ImportPrivateKey(prvKey string) { key := ethutil.FromHex(prvKey) msg := []byte("tmp") @@ -49,3 +105,4 @@ pubk: %x `, pair.Address(), key, pub) } +*/ -- cgit v1.2.3 From 2c7b625daaf062192db2fa604344f79c1bdbb232 Mon Sep 17 00:00:00 2001 From: Maran Date: Wed, 14 May 2014 13:55:55 +0200 Subject: Make sure we have a coinbase address to mine with --- utils/cmd.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) (limited to 'utils') diff --git a/utils/cmd.go b/utils/cmd.go index 5a100ca4f..66cfbeff9 100644 --- a/utils/cmd.go +++ b/utils/cmd.go @@ -26,6 +26,16 @@ func DoMining(ethereum *eth.Ethereum) { // Set Mining status ethereum.Mining = true + data, _ := ethutil.Config.Db.Get([]byte("KeyRing")) + if len(data) == 0 { + log.Println("No address found, can't start mining") + return + } + + keyRing := ethutil.NewValueFromBytes(data) + addr := keyRing.Get(0).Bytes() + pair, _ := ethchain.NewKeyPairFromSec(ethutil.FromHex(hex.EncodeToString(addr))) + go func() { // Give it some time to connect with peers time.Sleep(3 * time.Second) @@ -35,10 +45,6 @@ func DoMining(ethereum *eth.Ethereum) { } log.Println("Miner started") - 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() }() -- cgit v1.2.3