aboutsummaryrefslogtreecommitdiffstats
path: root/miner/miner.go
blob: 79ba685873f6170a3b7de998fd8cd101bd46719d (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
    This file is part of go-ethereum

    go-ethereum is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    go-ethereum is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with go-ethereum.  If not, see <http://www.gnu.org/licenses/>.
*/
/**
 * @authors
 *  Jeffrey Wilcke <i@jev.io>
 * @date 2014
 *
 */

package miner

import (
    "fmt"
    "math/big"
    "sort"

    "github.com/ethereum/c-ethash/go-ethash"
    "github.com/ethereum/go-ethereum/core"
    "github.com/ethereum/go-ethereum/core/types"
    "github.com/ethereum/go-ethereum/crypto"
    "github.com/ethereum/go-ethereum/eth"
    "github.com/ethereum/go-ethereum/ethutil"
    "github.com/ethereum/go-ethereum/event"
    "github.com/ethereum/go-ethereum/logger"
    "github.com/ethereum/go-ethereum/pow"
    "github.com/ethereum/go-ethereum/state"
)

var dx = []byte{0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42, 0x42}
var dy = []byte{0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43, 0x43}

const epochLen = uint64(1000)

func getSeed(chainMan *core.ChainManager, block *types.Block) (x []byte, y []byte) {
    if block.Number().Uint64() == 0 {
        return dx, dy
    } else if block.Number().Uint64()%epochLen == 0 {
        x, y = getSeed(chainMan, chainMan.GetBlock(block.ParentHash()))
        if (block.Number().Uint64()/epochLen)%2 > 0 {
            y = crypto.Sha3(append(y, block.ParentHash()...))
        } else {
            x = crypto.Sha3(append(x, block.ParentHash()...))
        }
        return x, y
    } else {
        return getSeed(chainMan, chainMan.GetBlock(block.ParentHash()))
    }
}

type LocalTx struct {
    To       []byte `json:"to"`
    Data     []byte `json:"data"`
    Gas      string `json:"gas"`
    GasPrice string `json:"gasPrice"`
    Value    string `json:"value"`
}

func (self *LocalTx) Sign(key []byte) *types.Transaction {
    return nil
}

var minerlogger = logger.NewLogger("MINER")

type Miner struct {
    eth    *eth.Ethereum
    events event.Subscription

    uncles    []*types.Header
    localTxs  map[int]*LocalTx
    localTxId int

    pow       pow.PoW
    quitCh    chan struct{}
    powQuitCh chan struct{}

    Coinbase []byte

    mining bool

    MinAcceptedGasPrice *big.Int
    Extra               string
}

func New(coinbase []byte, eth *eth.Ethereum) *Miner {
    return &Miner{
        eth:                 eth,
        powQuitCh:           make(chan struct{}),
        mining:              false,
        localTxs:            make(map[int]*LocalTx),
        MinAcceptedGasPrice: big.NewInt(10000000000000),
        Coinbase:            coinbase,
    }
}

func (self *Miner) GetPow() pow.PoW {
    return self.pow
}

func (self *Miner) AddLocalTx(tx *LocalTx) int {
    minerlogger.Infof("Added local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value)

    self.localTxId++
    self.localTxs[self.localTxId] = tx
    self.eth.EventMux().Post(tx)

    return self.localTxId
}

func (self *Miner) RemoveLocalTx(id int) {
    if tx := self.localTxs[id]; tx != nil {
        minerlogger.Infof("Removed local tx (%x %v / %v)\n", tx.To[0:4], tx.GasPrice, tx.Value)
    }
    self.eth.EventMux().Post(&LocalTx{})

    delete(self.localTxs, id)
}

func (self *Miner) Start() {
    if self.mining {
        return
    }

    minerlogger.Infoln("Starting mining operations")
    self.mining = true
    self.quitCh = make(chan struct{})
    self.powQuitCh = make(chan struct{})

    mux := self.eth.EventMux()
    self.events = mux.Subscribe(core.NewBlockEvent{}, core.TxPreEvent{}, &LocalTx{})

    go self.update()
    go self.mine()
}

func (self *Miner) Stop() {
    if !self.mining {
        return
    }

    self.mining = false

    minerlogger.Infoln("Stopping mining operations")

    self.events.Unsubscribe()

    close(self.quitCh)
    close(self.powQuitCh)
}

func (self *Miner) Mining() bool {
    return self.mining
}

func (self *Miner) update() {
out:
    for {
        select {
        case event := <-self.events.Chan():
            switch event := event.(type) {
            case core.NewBlockEvent:
                block := event.Block
                if self.eth.ChainManager().HasBlock(block.Hash()) {
                    self.reset()
                    self.eth.TxPool().RemoveSet(block.Transactions())
                    go self.mine()
                } else if true {
                    // do uncle stuff
                }
            case core.TxPreEvent, *LocalTx:
                self.reset()
                go self.mine()
            }
        case <-self.quitCh:
            break out
        }
    }
}

func (self *Miner) reset() {
    close(self.powQuitCh)
    self.powQuitCh = make(chan struct{})
}

func (self *Miner) mine() {
    var (
        blockProcessor = self.eth.BlockProcessor()
        chainMan       = self.eth.ChainManager()
        block          = chainMan.NewBlock(self.Coinbase)
        state          = state.New(block.Root(), self.eth.Db())
    )
    block.Header().Extra = self.Extra

    // Apply uncles
    block.SetUncles(self.uncles)

    parent := chainMan.GetBlock(block.ParentHash())
    coinbase := state.GetOrNewStateObject(block.Coinbase())
    coinbase.SetGasPool(core.CalcGasLimit(parent, block))

    transactions := self.finiliseTxs()

    // Accumulate all valid transactions and apply them to the new state
    // Error may be ignored. It's not important during mining
    receipts, txs, _, erroneous, err := blockProcessor.ApplyTransactions(coinbase, state, block, transactions, true)
    if err != nil {
        minerlogger.Debugln(err)
    }
    self.eth.TxPool().RemoveSet(erroneous)

    block.SetTransactions(txs)
    block.SetReceipts(receipts)

    // Accumulate the rewards included for this block
    blockProcessor.AccumelateRewards(state, block, parent)

    state.Update(ethutil.Big0)
    block.SetRoot(state.Root())

    minerlogger.Infof("Mining on block. Includes %v transactions", len(transactions))

    x, _ := getSeed(chainMan, block)
    self.pow, err = ethash.New(x, block)
    if err != nil {
        fmt.Println("miner gave back err", err)
        return
    }

    // Find a valid nonce
    nonce := self.pow.Search(block, self.powQuitCh)
    if nonce != nil {
        block.Header().Nonce = nonce
        err := chainMan.InsertChain(types.Blocks{block})
        if err != nil {
            minerlogger.Infoln(err)
        } else {
            self.eth.EventMux().Post(core.NewMinedBlockEvent{block})

            minerlogger.Infof("🔨  Mined block %x\n", block.Hash())
            minerlogger.Infoln(block)
        }

        go self.mine()
    }
}

func (self *Miner) finiliseTxs() types.Transactions {
    // Sort the transactions by nonce in case of odd network propagation
    actualSize := len(self.localTxs) // See copy below
    txs := make(types.Transactions, actualSize+self.eth.TxPool().Size())

    state := self.eth.ChainManager().TransState()
    // XXX This has to change. Coinbase is, for new, same as key.
    key := self.eth.KeyManager()
    for i, ltx := range self.localTxs {
        tx := types.NewTransactionMessage(ltx.To, ethutil.Big(ltx.Value), ethutil.Big(ltx.Gas), ethutil.Big(ltx.GasPrice), ltx.Data)
        tx.SetNonce(state.GetNonce(self.Coinbase))
        state.SetNonce(self.Coinbase, tx.Nonce()+1)

        tx.Sign(key.PrivateKey())

        txs[i] = tx
    }

    // Faster than append
    for _, tx := range self.eth.TxPool().GetTransactions() {
        if tx.GasPrice().Cmp(self.MinAcceptedGasPrice) >= 0 {
            txs[actualSize] = tx
            actualSize++
        }
    }

    newTransactions := make(types.Transactions, actualSize)
    copy(newTransactions, txs[:actualSize])
    sort.Sort(types.TxByNonce{newTransactions})

    return newTransactions
}