go-ethereum/utils/cmd.go

75 lines
1.4 KiB
Go
Raw Normal View History

package utils
import (
"github.com/ethereum/eth-go"
"github.com/ethereum/eth-go/ethminer"
"github.com/ethereum/eth-go/ethpub"
"github.com/ethereum/eth-go/ethrpc"
"github.com/ethereum/eth-go/ethutil"
"time"
)
func DoRpc(ethereum *eth.Ethereum, RpcPort int) {
var err error
ethereum.RpcServer, err = ethrpc.NewJsonRpcServer(ethpub.NewPEthereum(ethereum), RpcPort)
if err != nil {
2014-05-19 05:14:47 -05:00
ethutil.Config.Log.Infoln("Could not start RPC interface:", err)
} else {
go ethereum.RpcServer.Start()
}
}
2014-05-21 17:25:48 -05:00
var miner ethminer.Miner
func DoMining(ethereum *eth.Ethereum) {
// Set Mining status
ethereum.Mining = true
2014-05-14 07:04:43 -05:00
if ethutil.GetKeyRing().Len() == 0 {
2014-05-19 05:14:47 -05:00
ethutil.Config.Log.Infoln("No address found, can't start mining")
return
}
2014-05-14 07:04:43 -05:00
keyPair := ethutil.GetKeyRing().Get(0)
addr := keyPair.Address()
go func() {
2014-05-21 17:25:48 -05:00
miner = ethminer.NewDefaultMiner(addr, ethereum)
// Give it some time to connect with peers
time.Sleep(3 * time.Second)
2014-05-21 04:45:19 -05:00
for ethereum.IsUpToDate() == false {
time.Sleep(5 * time.Second)
}
2014-05-19 05:14:47 -05:00
ethutil.Config.Log.Infoln("Miner started")
2014-05-14 06:55:08 -05:00
miner := ethminer.NewDefaultMiner(addr, ethereum)
miner.Start()
}()
}
2014-05-21 17:25:48 -05:00
func StopMining(ethereum *eth.Ethereum) bool {
if ethereum.Mining {
miner.Stop()
ethutil.Config.Log.Infoln("Miner stopped")
ethereum.Mining = false
return true
}
return false
}
func StartMining(ethereum *eth.Ethereum) bool {
if !ethereum.Mining {
DoMining(ethereum)
return true
}
return false
}