aboutsummaryrefslogtreecommitdiffstats
path: root/utils/cmd.go
blob: 98005d7de0b4dc78f18943175b6face04c726184 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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 {
        ethutil.Config.Log.Infoln("Could not start RPC interface:", err)
    } else {
        go ethereum.RpcServer.Start()
    }
}

var miner ethminer.Miner

func DoMining(ethereum *eth.Ethereum) {
    // Set Mining status
    ethereum.Mining = true

    if ethutil.GetKeyRing().Len() == 0 {
        ethutil.Config.Log.Infoln("No address found, can't start mining")
        return
    }
    keyPair := ethutil.GetKeyRing().Get(0)
    addr := keyPair.Address()

    go func() {
        ethutil.Config.Log.Infoln("Miner started")

        miner = ethminer.NewDefaultMiner(addr, ethereum)

        // Give it some time to connect with peers
        time.Sleep(3 * time.Second)

        miner.Start()
    }()
}

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
}