diff options
Diffstat (limited to 'miner')
-rw-r--r-- | miner/agent.go | 81 | ||||
-rw-r--r-- | miner/miner.go | 60 | ||||
-rw-r--r-- | miner/worker.go | 280 |
3 files changed, 421 insertions, 0 deletions
diff --git a/miner/agent.go b/miner/agent.go new file mode 100644 index 000000000..6865d5a08 --- /dev/null +++ b/miner/agent.go @@ -0,0 +1,81 @@ +package miner + +import ( + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/pow" +) + +type CpuMiner struct { + c chan *types.Block + quit chan struct{} + quitCurrentOp chan struct{} + returnCh chan<- Work + + index int + pow pow.PoW +} + +func NewCpuMiner(index int, pow pow.PoW) *CpuMiner { + miner := &CpuMiner{ + pow: pow, + index: index, + } + + return miner +} + +func (self *CpuMiner) Work() chan<- *types.Block { return self.c } +func (self *CpuMiner) Pow() pow.PoW { return self.pow } +func (self *CpuMiner) SetWorkCh(ch chan<- Work) { self.returnCh = ch } + +func (self *CpuMiner) Stop() { + close(self.quit) + close(self.quitCurrentOp) +} + +func (self *CpuMiner) Start() { + self.quit = make(chan struct{}) + self.quitCurrentOp = make(chan struct{}, 1) + self.c = make(chan *types.Block, 1) + + go self.update() +} + +func (self *CpuMiner) update() { + justStarted := true +out: + for { + select { + case block := <-self.c: + if justStarted { + justStarted = true + } else { + self.quitCurrentOp <- struct{}{} + } + + go self.mine(block) + case <-self.quit: + break out + } + } + +done: + // Empty channel + for { + select { + case <-self.c: + default: + close(self.c) + + break done + } + } +} + +func (self *CpuMiner) mine(block *types.Block) { + minerlogger.Infof("(re)started agent[%d]. mining...\n", self.index) + nonce, mixDigest, seedHash := self.pow.Search(block, self.quitCurrentOp) + if nonce != 0 { + self.returnCh <- Work{block.Number().Uint64(), nonce, mixDigest, seedHash} + } +} diff --git a/miner/miner.go b/miner/miner.go new file mode 100644 index 000000000..b95c65859 --- /dev/null +++ b/miner/miner.go @@ -0,0 +1,60 @@ +package miner + +import ( + "math/big" + + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/pow" +) + +var minerlogger = logger.NewLogger("MINER") + +type Miner struct { + worker *worker + + MinAcceptedGasPrice *big.Int + Extra string + + Coinbase []byte + mining bool + + pow pow.PoW +} + +func New(coinbase []byte, eth core.Backend, pow pow.PoW, minerThreads int) *Miner { + miner := &Miner{ + Coinbase: coinbase, + worker: newWorker(coinbase, eth), + pow: pow, + } + + minerThreads = 1 + for i := 0; i < minerThreads; i++ { + miner.worker.register(NewCpuMiner(i, miner.pow)) + } + + return miner +} + +func (self *Miner) Mining() bool { + return self.mining +} + +func (self *Miner) Start() { + self.mining = true + + self.worker.start() + + self.worker.commitNewWork() +} + +func (self *Miner) Stop() { + self.mining = false + + self.worker.stop() +} + +func (self *Miner) HashRate() int64 { + return self.worker.HashRate() +} diff --git a/miner/worker.go b/miner/worker.go new file mode 100644 index 000000000..29992b327 --- /dev/null +++ b/miner/worker.go @@ -0,0 +1,280 @@ +package miner + +import ( + "fmt" + "math/big" + "sort" + "sync" + "time" + + "github.com/ethereum/ethash" + "github.com/ethereum/go-ethereum/core" + "github.com/ethereum/go-ethereum/core/types" + "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" + "gopkg.in/fatih/set.v0" +) + +var jsonlogger = logger.NewJsonLogger() + +type environment struct { + totalUsedGas *big.Int + state *state.StateDB + coinbase *state.StateObject + block *types.Block + ancestors *set.Set + uncles *set.Set +} + +func env(block *types.Block, eth core.Backend) *environment { + state := state.New(block.Root(), eth.Db()) + env := &environment{ + totalUsedGas: new(big.Int), + state: state, + block: block, + ancestors: set.New(), + uncles: set.New(), + coinbase: state.GetOrNewStateObject(block.Coinbase()), + } + for _, ancestor := range eth.ChainManager().GetAncestors(block, 7) { + env.ancestors.Add(string(ancestor.Hash())) + } + + return env +} + +type Work struct { + Number uint64 + Nonce uint64 + MixDigest []byte + SeedHash []byte +} + +type Agent interface { + Work() chan<- *types.Block + SetWorkCh(chan<- Work) + Stop() + Start() + Pow() pow.PoW +} + +type worker struct { + mu sync.Mutex + agents []Agent + recv chan Work + mux *event.TypeMux + quit chan struct{} + pow pow.PoW + + eth core.Backend + chain *core.ChainManager + proc *core.BlockProcessor + coinbase []byte + + current *environment + + mining bool +} + +func newWorker(coinbase []byte, eth core.Backend) *worker { + return &worker{ + eth: eth, + mux: eth.EventMux(), + recv: make(chan Work), + chain: eth.ChainManager(), + proc: eth.BlockProcessor(), + coinbase: coinbase, + } +} + +func (self *worker) start() { + self.mining = true + + self.quit = make(chan struct{}) + + // spin up agents + for _, agent := range self.agents { + agent.Start() + } + + go self.update() + go self.wait() +} + +func (self *worker) stop() { + self.mining = false + + close(self.quit) +} + +func (self *worker) register(agent Agent) { + self.agents = append(self.agents, agent) + agent.SetWorkCh(self.recv) +} + +func (self *worker) update() { + events := self.mux.Subscribe(core.ChainEvent{}, core.NewMinedBlockEvent{}) + + timer := time.NewTicker(2 * time.Second) + +out: + for { + select { + case event := <-events.Chan(): + switch ev := event.(type) { + case core.ChainEvent: + if self.current.block != ev.Block { + self.commitNewWork() + } + case core.NewMinedBlockEvent: + self.commitNewWork() + } + case <-self.quit: + // stop all agents + for _, agent := range self.agents { + agent.Stop() + } + break out + case <-timer.C: + minerlogger.Infoln("Hash rate:", self.HashRate(), "Khash") + } + } + + events.Unsubscribe() +} + +func (self *worker) wait() { + for { + for work := range self.recv { + // Someone Successfully Mined! + block := self.current.block + if block.Number().Uint64() == work.Number && block.Nonce() == 0 { + self.current.block.SetNonce(work.Nonce) + self.current.block.Header().MixDigest = work.MixDigest + self.current.block.Header().SeedHash = work.SeedHash + + jsonlogger.LogJson(&logger.EthMinerNewBlock{ + BlockHash: ethutil.Bytes2Hex(block.Hash()), + BlockNumber: block.Number(), + ChainHeadHash: ethutil.Bytes2Hex(block.ParentHeaderHash), + BlockPrevHash: ethutil.Bytes2Hex(block.ParentHeaderHash), + }) + + if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil { + self.mux.Post(core.NewMinedBlockEvent{self.current.block}) + } else { + self.commitNewWork() + } + } + break + } + } +} + +func (self *worker) push() { + if self.mining { + self.current.block.Header().GasUsed = self.current.totalUsedGas + self.current.block.SetRoot(self.current.state.Root()) + + // push new work to agents + for _, agent := range self.agents { + agent.Work() <- self.current.block + } + } +} + +func (self *worker) commitNewWork() { + self.mu.Lock() + defer self.mu.Unlock() + + block := self.chain.NewBlock(self.coinbase) + seednum := ethash.GetSeedBlockNum(block.NumberU64()) + block.Header().SeedHash = self.chain.GetBlockByNumber(seednum).SeedHash() + + self.current = env(block, self.eth) + parent := self.chain.GetBlock(self.current.block.ParentHash()) + self.current.coinbase.SetGasPool(core.CalcGasLimit(parent, self.current.block)) + + transactions := self.eth.TxPool().GetTransactions() + sort.Sort(types.TxByNonce{transactions}) + + minerlogger.Infof("committing new work with %d txs\n", len(transactions)) + // Keep track of transactions which return errors so they can be removed + var remove types.Transactions +gasLimit: + for _, tx := range transactions { + err := self.commitTransaction(tx) + switch { + case core.IsNonceErr(err): + // Remove invalid transactions + remove = append(remove, tx) + case state.IsGasLimitErr(err): + // Break on gas limit + break gasLimit + } + + if err != nil { + minerlogger.Infoln(err) + } + } + self.eth.TxPool().RemoveSet(remove) + + self.current.coinbase.AddBalance(core.BlockReward) + + self.current.state.Update(ethutil.Big0) + self.push() +} + +var ( + inclusionReward = new(big.Int).Div(core.BlockReward, big.NewInt(32)) + _uncleReward = new(big.Int).Mul(core.BlockReward, big.NewInt(15)) + uncleReward = new(big.Int).Div(_uncleReward, big.NewInt(16)) +) + +func (self *worker) commitUncle(uncle *types.Header) error { + if self.current.uncles.Has(string(uncle.Hash())) { + // Error not unique + return core.UncleError("Uncle not unique") + } + self.current.uncles.Add(string(uncle.Hash())) + + if !self.current.ancestors.Has(string(uncle.ParentHash)) { + return core.UncleError(fmt.Sprintf("Uncle's parent unknown (%x)", uncle.ParentHash[0:4])) + } + + if !self.pow.Verify(types.NewBlockWithHeader(uncle)) { + return core.ValidationError("Uncle's nonce is invalid (= %x)", uncle.Nonce) + } + + uncleAccount := self.current.state.GetAccount(uncle.Coinbase) + uncleAccount.AddBalance(uncleReward) + + self.current.coinbase.AddBalance(uncleReward) + + return nil +} + +func (self *worker) commitTransaction(tx *types.Transaction) error { + //fmt.Printf("proc %x %v\n", tx.Hash()[:3], tx.Nonce()) + receipt, _, err := self.proc.ApplyTransaction(self.current.coinbase, self.current.state, self.current.block, tx, self.current.totalUsedGas, true) + if err != nil && (core.IsNonceErr(err) || state.IsGasLimitErr(err)) { + return err + } + + self.current.block.AddTransaction(tx) + self.current.block.AddReceipt(receipt) + + return nil +} + +func (self *worker) HashRate() int64 { + var tot int64 + for _, agent := range self.agents { + tot += agent.Pow().GetHashrate() + } + + return tot +} |