mirror of https://github.com/status-im/op-geth.git
Initial smart-miner stuff
This commit is contained in:
parent
fbd53f0e34
commit
96fcc1da32
69
ethereum.go
69
ethereum.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bytes"
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/ethereum/eth-go"
|
"github.com/ethereum/eth-go"
|
||||||
"github.com/ethereum/eth-go/ethchain"
|
"github.com/ethereum/eth-go/ethchain"
|
||||||
|
@ -172,6 +173,16 @@ func main() {
|
||||||
RegisterInterupts(ethereum)
|
RegisterInterupts(ethereum)
|
||||||
ethereum.Start()
|
ethereum.Start()
|
||||||
|
|
||||||
|
minerChan := make(chan ethutil.React, 5)
|
||||||
|
ethereum.Reactor().Subscribe("newBlock", minerChan)
|
||||||
|
ethereum.Reactor().Subscribe("newTx", minerChan)
|
||||||
|
|
||||||
|
minerChan2 := make(chan ethutil.React, 5)
|
||||||
|
ethereum.Reactor().Subscribe("newBlock", minerChan2)
|
||||||
|
ethereum.Reactor().Subscribe("newTx", minerChan2)
|
||||||
|
|
||||||
|
ethereum.StateManager().PrepareMiningState()
|
||||||
|
|
||||||
if StartMining {
|
if StartMining {
|
||||||
log.Printf("Miner started\n")
|
log.Printf("Miner started\n")
|
||||||
|
|
||||||
|
@ -181,19 +192,60 @@ func main() {
|
||||||
data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
|
data, _ := ethutil.Config.Db.Get([]byte("KeyRing"))
|
||||||
keyRing := ethutil.NewValueFromBytes(data)
|
keyRing := ethutil.NewValueFromBytes(data)
|
||||||
addr := keyRing.Get(1).Bytes()
|
addr := keyRing.Get(1).Bytes()
|
||||||
|
txs := ethereum.TxPool().Flush()
|
||||||
|
block := ethereum.BlockChain().NewBlock(addr, txs)
|
||||||
|
|
||||||
for {
|
for {
|
||||||
txs := ethereum.TxPool().Flush()
|
select {
|
||||||
// Create a new block which we're going to mine
|
case chanMessage := <-minerChan:
|
||||||
block := ethereum.BlockChain().NewBlock(addr, txs)
|
log.Println("REACTOR: Got new block")
|
||||||
log.Println("Mining on new block. Includes", len(block.Transactions()), "transactions")
|
|
||||||
// Apply all transactions to the block
|
|
||||||
ethereum.StateManager().ApplyTransactions(block, block.Transactions())
|
|
||||||
|
|
||||||
|
if block, ok := chanMessage.Resource.(*ethchain.Block); ok {
|
||||||
|
if bytes.Compare(ethereum.BlockChain().CurrentBlock.Hash(), block.Hash()) == 0 {
|
||||||
|
// TODO: Perhaps continue mining to get some uncle rewards
|
||||||
|
log.Println("New top block found resetting state")
|
||||||
|
// Reapplies the latest block to the mining state, thus resetting
|
||||||
|
ethereum.StateManager().PrepareMiningState()
|
||||||
|
block = ethereum.BlockChain().NewBlock(addr, txs)
|
||||||
|
log.Println("Block set")
|
||||||
|
} else {
|
||||||
|
if bytes.Compare(block.PrevHash, ethereum.BlockChain().CurrentBlock.PrevHash) == 0 {
|
||||||
|
log.Println("HELLO UNCLE")
|
||||||
|
// TODO: Add uncle to block
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if tx, ok := chanMessage.Resource.(*ethchain.Transaction); ok {
|
||||||
|
log.Println("REACTOR: Got new transaction", tx)
|
||||||
|
found := false
|
||||||
|
for _, ctx := range txs {
|
||||||
|
if found = bytes.Compare(ctx.Hash(), tx.Hash()) == 0; found {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
if found == false {
|
||||||
|
log.Println("We did not know about this transaction, adding")
|
||||||
|
txs = append(txs, tx)
|
||||||
|
} else {
|
||||||
|
log.Println("We already had this transaction, ignoring")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
log.Println("Sending block reset")
|
||||||
|
// Start mining over
|
||||||
|
log.Println("Block reset done")
|
||||||
|
default:
|
||||||
|
// Create a new block which we're going to mine
|
||||||
|
log.Println("Mining on block. Includes", len(txs), "transactions")
|
||||||
|
|
||||||
|
// Apply all transactions to the block
|
||||||
|
ethereum.StateManager().ApplyTransactions(block, txs)
|
||||||
ethereum.StateManager().AccumelateRewards(block, block)
|
ethereum.StateManager().AccumelateRewards(block, block)
|
||||||
|
|
||||||
// Search the nonce
|
// Search the nonce
|
||||||
block.Nonce = pow.Search(block)
|
block.Nonce = pow.Search(block, minerChan2)
|
||||||
|
if block.Nonce != nil {
|
||||||
ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
|
ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{block.Value().Val})
|
||||||
err := ethereum.StateManager().ProcessBlock(block)
|
err := ethereum.StateManager().ProcessBlock(block)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -201,6 +253,9 @@ func main() {
|
||||||
} else {
|
} else {
|
||||||
//log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock)
|
//log.Println("\n+++++++ MINED BLK +++++++\n", ethereum.BlockChain().CurrentBlock)
|
||||||
log.Printf("🔨 Mined block %x\n", block.Hash())
|
log.Printf("🔨 Mined block %x\n", block.Hash())
|
||||||
|
block = ethereum.BlockChain().NewBlock(addr, txs)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
|
@ -54,7 +54,7 @@ func AssetPath(p string) string {
|
||||||
// Get Binary Directory
|
// Get Binary Directory
|
||||||
exedir, _ := osext.ExecutableFolder()
|
exedir, _ := osext.ExecutableFolder()
|
||||||
base = filepath.Join(exedir, "../Resources")
|
base = filepath.Join(exedir, "../Resources")
|
||||||
base = "/Users/jeffrey/go/src/github.com/ethereum/go-ethereum"
|
base = "/Users/maranhidskes/projects/go/src/github.com/ethereum/go-ethereum"
|
||||||
case "linux":
|
case "linux":
|
||||||
base = "/usr/share/ethereal"
|
base = "/usr/share/ethereal"
|
||||||
case "window":
|
case "window":
|
||||||
|
|
Loading…
Reference in New Issue