2014-05-14 13:41:30 +03:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/hex"
|
|
|
|
"github.com/ethereum/eth-go"
|
|
|
|
"github.com/ethereum/eth-go/ethchain"
|
|
|
|
"github.com/ethereum/eth-go/ethminer"
|
2014-05-14 14:32:49 +03:00
|
|
|
"github.com/ethereum/eth-go/ethpub"
|
|
|
|
"github.com/ethereum/eth-go/ethrpc"
|
2014-05-14 13:41:30 +03:00
|
|
|
"github.com/ethereum/eth-go/ethutil"
|
|
|
|
"log"
|
2014-05-14 14:26:15 +03:00
|
|
|
"time"
|
2014-05-14 13:41:30 +03:00
|
|
|
)
|
|
|
|
|
2014-05-14 14:32:49 +03:00
|
|
|
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()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-14 13:41:30 +03:00
|
|
|
func DoMining(ethereum *eth.Ethereum) {
|
|
|
|
// Set Mining status
|
|
|
|
ethereum.Mining = true
|
|
|
|
|
|
|
|
go func() {
|
2014-05-14 14:26:15 +03:00
|
|
|
// 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")
|
|
|
|
|
2014-05-14 13:41:30 +03:00
|
|
|
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()
|
|
|
|
}()
|
|
|
|
}
|