fixed pow stuff

This commit is contained in:
obscuren 2015-03-03 17:55:23 +01:00
parent 22b132e28f
commit 313fe3861b
10 changed files with 28 additions and 58 deletions

View File

@ -114,7 +114,7 @@ func main() {
} }
if StartMining { if StartMining {
utils.StartMining(ethereum) ethereum.Miner().Start()
} }
if len(ImportChain) > 0 { if len(ImportChain) > 0 {

View File

@ -32,7 +32,6 @@ import (
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
rpchttp "github.com/ethereum/go-ethereum/rpc/http" rpchttp "github.com/ethereum/go-ethereum/rpc/http"
rpcws "github.com/ethereum/go-ethereum/rpc/ws" rpcws "github.com/ethereum/go-ethereum/rpc/ws"
@ -182,32 +181,6 @@ func StartWebSockets(eth *eth.Ethereum, wsPort int) {
} }
} }
var gminer *miner.Miner
func GetMiner() *miner.Miner {
return gminer
}
func StartMining(ethereum *eth.Ethereum) bool {
if !ethereum.Mining {
ethereum.Mining = true
addr := ethereum.KeyManager().Address()
go func() {
clilogger.Infoln("Start mining")
if gminer == nil {
gminer = miner.New(addr, ethereum, 4)
}
gminer.Start()
}()
RegisterInterrupt(func(os.Signal) {
StopMining(ethereum)
})
return true
}
return false
}
func FormatTransactionData(data string) []byte { func FormatTransactionData(data string) []byte {
d := ethutil.StringToByteFunc(data, func(s string) (ret []byte) { d := ethutil.StringToByteFunc(data, func(s string) (ret []byte) {
slice := regexp.MustCompile("\\n|\\s").Split(s, 1000000000) slice := regexp.MustCompile("\\n|\\s").Split(s, 1000000000)
@ -221,18 +194,6 @@ func FormatTransactionData(data string) []byte {
return d return d
} }
func StopMining(ethereum *eth.Ethereum) bool {
if ethereum.Mining && gminer != nil {
gminer.Stop()
clilogger.Infoln("Stopped mining")
ethereum.Mining = false
return true
}
return false
}
// Replay block // Replay block
func BlockDo(ethereum *eth.Ethereum, hash []byte) error { func BlockDo(ethereum *eth.Ethereum, hash []byte) error {
block := ethereum.ChainManager().GetBlock(hash) block := ethereum.ChainManager().GetBlock(hash)

View File

@ -7,7 +7,6 @@ import (
"sync" "sync"
"time" "time"
"github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
@ -46,11 +45,11 @@ type BlockProcessor struct {
eventMux *event.TypeMux eventMux *event.TypeMux
} }
func NewBlockProcessor(db ethutil.Database, txpool *TxPool, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor { func NewBlockProcessor(db ethutil.Database, pow pow.PoW, txpool *TxPool, chainManager *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
sm := &BlockProcessor{ sm := &BlockProcessor{
db: db, db: db,
mem: make(map[string]*big.Int), mem: make(map[string]*big.Int),
Pow: ethash.New(chainManager), Pow: pow,
bc: chainManager, bc: chainManager,
eventMux: eventMux, eventMux: eventMux,
txpool: txpool, txpool: txpool,

View File

@ -2,12 +2,13 @@ package core
import ( import (
"fmt" "fmt"
"math/big"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"math/big"
) )
// So we can generate blocks easily // So we can generate blocks easily
@ -119,8 +120,7 @@ func newChainManager(block *types.Block, eventMux *event.TypeMux, db ethutil.Dat
// block processor with fake pow // block processor with fake pow
func newBlockProcessor(db ethutil.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor { func newBlockProcessor(db ethutil.Database, txpool *TxPool, cman *ChainManager, eventMux *event.TypeMux) *BlockProcessor {
bman := NewBlockProcessor(db, txpool, newChainManager(nil, eventMux, db), eventMux) bman := NewBlockProcessor(db, FakePow{}, txpool, newChainManager(nil, eventMux, db), eventMux)
bman.Pow = FakePow{}
return bman return bman
} }

View File

@ -268,7 +268,10 @@ func (self *Header) String() string {
Time: %v Time: %v
Extra: %v Extra: %v
Nonce: %x Nonce: %x
`, self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra, self.Nonce) MixDigest: %x
SeedHash: %x
`, self.ParentHash, self.UncleHash, self.Coinbase, self.Root, self.TxHash, self.ReceiptHash, self.Bloom, self.Difficulty, self.Number, self.GasLimit, self.GasUsed, self.Time, self.Extra, self.Nonce, self.MixDigest, self.SeedHash)
} }
type Blocks []*Block type Blocks []*Block

View File

@ -7,6 +7,7 @@ import (
"path" "path"
"strings" "strings"
"github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/blockpool" "github.com/ethereum/go-ethereum/blockpool"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
@ -179,11 +180,13 @@ func New(config *Config) (*Ethereum, error) {
} }
eth.chainManager = core.NewChainManager(db, eth.EventMux()) eth.chainManager = core.NewChainManager(db, eth.EventMux())
pow := ethash.New(eth.chainManager)
eth.txPool = core.NewTxPool(eth.EventMux()) eth.txPool = core.NewTxPool(eth.EventMux())
eth.blockProcessor = core.NewBlockProcessor(db, eth.txPool, eth.chainManager, eth.EventMux()) eth.blockProcessor = core.NewBlockProcessor(db, pow, eth.txPool, eth.chainManager, eth.EventMux())
eth.chainManager.SetProcessor(eth.blockProcessor) eth.chainManager.SetProcessor(eth.blockProcessor)
eth.whisper = whisper.New() eth.whisper = whisper.New()
eth.miner = miner.New(keyManager.Address(), eth, config.MinerThreads) eth.miner = miner.New(keyManager.Address(), eth, pow, config.MinerThreads)
hasBlock := eth.chainManager.HasBlock hasBlock := eth.chainManager.HasBlock
insertChain := eth.chainManager.InsertChain insertChain := eth.chainManager.InsertChain

View File

@ -14,7 +14,7 @@ import (
) )
const ( const (
ProtocolVersion = 54 ProtocolVersion = 55
NetworkId = 0 NetworkId = 0
ProtocolLength = uint64(8) ProtocolLength = uint64(8)
ProtocolMaxMsgSize = 10 * 1024 * 1024 ProtocolMaxMsgSize = 10 * 1024 * 1024

View File

@ -7,7 +7,6 @@ import (
"path" "path"
"path/filepath" "path/filepath"
"github.com/ethereum/go-ethereum/cmd/utils"
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/eth" "github.com/ethereum/go-ethereum/eth"
@ -157,13 +156,14 @@ func (self *JSRE) dump(call otto.FunctionCall) otto.Value {
} }
func (self *JSRE) stopMining(call otto.FunctionCall) otto.Value { func (self *JSRE) stopMining(call otto.FunctionCall) otto.Value {
v, _ := self.Vm.ToValue(utils.StopMining(self.ethereum)) self.xeth.Miner().Stop()
return v
return otto.TrueValue()
} }
func (self *JSRE) startMining(call otto.FunctionCall) otto.Value { func (self *JSRE) startMining(call otto.FunctionCall) otto.Value {
v, _ := self.Vm.ToValue(utils.StartMining(self.ethereum)) self.xeth.Miner().Start()
return v return otto.TrueValue()
} }
func (self *JSRE) connect(call otto.FunctionCall) otto.Value { func (self *JSRE) connect(call otto.FunctionCall) otto.Value {

View File

@ -5,7 +5,7 @@ import (
"github.com/ethereum/go-ethereum/core" "github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/pow/ezp" "github.com/ethereum/go-ethereum/pow"
) )
var minerlogger = logger.NewLogger("MINER") var minerlogger = logger.NewLogger("MINER")
@ -18,16 +18,19 @@ type Miner struct {
Coinbase []byte Coinbase []byte
mining bool mining bool
pow pow.PoW
} }
func New(coinbase []byte, eth core.Backend, minerThreads int) *Miner { func New(coinbase []byte, eth core.Backend, pow pow.PoW, minerThreads int) *Miner {
miner := &Miner{ miner := &Miner{
Coinbase: coinbase, Coinbase: coinbase,
worker: newWorker(coinbase, eth), worker: newWorker(coinbase, eth),
pow: pow,
} }
for i := 0; i < minerThreads; i++ { for i := 0; i < minerThreads; i++ {
miner.worker.register(NewCpuMiner(i, ezp.New())) miner.worker.register(NewCpuMiner(i, miner.pow))
} }
return miner return miner

View File

@ -146,6 +146,7 @@ func (self *worker) wait() {
self.current.block.Header().Nonce = work.Nonce self.current.block.Header().Nonce = work.Nonce
self.current.block.Header().MixDigest = work.MixDigest self.current.block.Header().MixDigest = work.MixDigest
self.current.block.Header().SeedHash = work.SeedHash self.current.block.Header().SeedHash = work.SeedHash
fmt.Println(self.current.block)
if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil { if err := self.chain.InsertChain(types.Blocks{self.current.block}); err == nil {
self.mux.Post(core.NewMinedBlockEvent{self.current.block}) self.mux.Post(core.NewMinedBlockEvent{self.current.block})