Commit 4914a78c authored by obscuren's avatar obscuren

ethwire => wire

parent af34749a
...@@ -11,8 +11,8 @@ import ( ...@@ -11,8 +11,8 @@ import (
"github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/chain"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/ethwire"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/wire"
) )
var poollogger = logger.NewLogger("BPOOL") var poollogger = logger.NewLogger("BPOOL")
...@@ -103,7 +103,7 @@ func (self *BlockPool) FetchHashes(peer *Peer) bool { ...@@ -103,7 +103,7 @@ func (self *BlockPool) FetchHashes(peer *Peer) bool {
const amount = 256 const amount = 256
peerlogger.Debugf("Fetching hashes (%d) %x...\n", amount, peer.lastReceivedHash[0:4]) peerlogger.Debugf("Fetching hashes (%d) %x...\n", amount, peer.lastReceivedHash[0:4])
peer.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlockHashesTy, []interface{}{peer.lastReceivedHash, uint32(amount)})) peer.QueueMessage(wire.NewMessage(wire.MsgGetBlockHashesTy, []interface{}{peer.lastReceivedHash, uint32(amount)}))
} }
return true return true
...@@ -150,7 +150,7 @@ func (self *BlockPool) addBlock(b *chain.Block, peer *Peer, newBlock bool) { ...@@ -150,7 +150,7 @@ func (self *BlockPool) addBlock(b *chain.Block, peer *Peer, newBlock bool) {
fmt.Println("3.", !self.fetchingHashes) fmt.Println("3.", !self.fetchingHashes)
if !self.eth.ChainManager().HasBlock(b.PrevHash) && self.pool[string(b.PrevHash)] == nil && !self.fetchingHashes { if !self.eth.ChainManager().HasBlock(b.PrevHash) && self.pool[string(b.PrevHash)] == nil && !self.fetchingHashes {
poollogger.Infof("Unknown chain, requesting (%x...)\n", b.PrevHash[0:4]) poollogger.Infof("Unknown chain, requesting (%x...)\n", b.PrevHash[0:4])
peer.QueueMessage(ethwire.NewMessage(ethwire.MsgGetBlockHashesTy, []interface{}{b.Hash(), uint32(256)})) peer.QueueMessage(wire.NewMessage(wire.MsgGetBlockHashesTy, []interface{}{b.Hash(), uint32(256)}))
} }
} }
} else if self.pool[hash] != nil { } else if self.pool[hash] != nil {
......
...@@ -11,10 +11,10 @@ import ( ...@@ -11,10 +11,10 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/ethwire"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/wire"
) )
var statelogger = logger.NewLogger("BLOCK") var statelogger = logger.NewLogger("BLOCK")
...@@ -35,13 +35,13 @@ type EthManager interface { ...@@ -35,13 +35,13 @@ type EthManager interface {
StateManager() *StateManager StateManager() *StateManager
ChainManager() *ChainManager ChainManager() *ChainManager
TxPool() *TxPool TxPool() *TxPool
Broadcast(msgType ethwire.MsgType, data []interface{}) Broadcast(msgType wire.MsgType, data []interface{})
PeerCount() int PeerCount() int
IsMining() bool IsMining() bool
IsListening() bool IsListening() bool
Peers() *list.List Peers() *list.List
KeyManager() *crypto.KeyManager KeyManager() *crypto.KeyManager
ClientIdentity() ethwire.ClientIdentity ClientIdentity() wire.ClientIdentity
Db() ethutil.Database Db() ethutil.Database
EventMux() *event.TypeMux EventMux() *event.TypeMux
} }
......
...@@ -7,9 +7,9 @@ import ( ...@@ -7,9 +7,9 @@ import (
"math/big" "math/big"
"sync" "sync"
"github.com/ethereum/go-ethereum/ethwire"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/wire"
) )
var txplogger = logger.NewLogger("TXP") var txplogger = logger.NewLogger("TXP")
...@@ -93,7 +93,7 @@ func (pool *TxPool) addTransaction(tx *Transaction) { ...@@ -93,7 +93,7 @@ func (pool *TxPool) addTransaction(tx *Transaction) {
pool.pool.PushBack(tx) pool.pool.PushBack(tx)
// Broadcast the transaction to the rest of the peers // Broadcast the transaction to the rest of the peers
pool.Ethereum.Broadcast(ethwire.MsgTxTy, []interface{}{tx.RlpData()}) pool.Ethereum.Broadcast(wire.MsgTxTy, []interface{}{tx.RlpData()})
} }
func (pool *TxPool) ValidateTransaction(tx *Transaction) error { func (pool *TxPool) ValidateTransaction(tx *Transaction) error {
......
...@@ -35,8 +35,8 @@ import ( ...@@ -35,8 +35,8 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethminer" "github.com/ethereum/go-ethereum/ethminer"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/ethwire"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/wire"
"github.com/ethereum/go-ethereum/xeth" "github.com/ethereum/go-ethereum/xeth"
"gopkg.in/qml.v1" "gopkg.in/qml.v1"
) )
...@@ -87,7 +87,7 @@ type Gui struct { ...@@ -87,7 +87,7 @@ type Gui struct {
pipe *xeth.JSXEth pipe *xeth.JSXEth
Session string Session string
clientIdentity *ethwire.SimpleClientIdentity clientIdentity *wire.SimpleClientIdentity
config *ethutil.ConfigManager config *ethutil.ConfigManager
plugins map[string]plugin plugins map[string]plugin
...@@ -97,7 +97,7 @@ type Gui struct { ...@@ -97,7 +97,7 @@ type Gui struct {
} }
// Create GUI, but doesn't start it // Create GUI, but doesn't start it
func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, clientIdentity *ethwire.SimpleClientIdentity, session string, logLevel int) *Gui { func NewWindow(ethereum *eth.Ethereum, config *ethutil.ConfigManager, clientIdentity *wire.SimpleClientIdentity, session string, logLevel int) *Gui {
db, err := ethdb.NewLDBDatabase("tx_database") db, err := ethdb.NewLDBDatabase("tx_database")
if err != nil { if err != nil {
panic(err) panic(err)
......
...@@ -18,9 +18,9 @@ import ( ...@@ -18,9 +18,9 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethminer" "github.com/ethereum/go-ethereum/ethminer"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/ethwire"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/wire"
"github.com/ethereum/go-ethereum/xeth" "github.com/ethereum/go-ethereum/xeth"
) )
...@@ -144,12 +144,12 @@ func NewDatabase() ethutil.Database { ...@@ -144,12 +144,12 @@ func NewDatabase() ethutil.Database {
return db return db
} }
func NewClientIdentity(clientIdentifier, version, customIdentifier string) *ethwire.SimpleClientIdentity { func NewClientIdentity(clientIdentifier, version, customIdentifier string) *wire.SimpleClientIdentity {
clilogger.Infoln("identity created") clilogger.Infoln("identity created")
return ethwire.NewSimpleClientIdentity(clientIdentifier, version, customIdentifier) return wire.NewSimpleClientIdentity(clientIdentifier, version, customIdentifier)
} }
func NewEthereum(db ethutil.Database, clientIdentity ethwire.ClientIdentity, keyManager *crypto.KeyManager, usePnp bool, OutboundPort string, MaxPeer int) *eth.Ethereum { func NewEthereum(db ethutil.Database, clientIdentity wire.ClientIdentity, keyManager *crypto.KeyManager, usePnp bool, OutboundPort string, MaxPeer int) *eth.Ethereum {
ethereum, err := eth.New(db, clientIdentity, keyManager, eth.CapDefault, usePnp) ethereum, err := eth.New(db, clientIdentity, keyManager, eth.CapDefault, usePnp)
if err != nil { if err != nil {
clilogger.Fatalln("eth start err:", err) clilogger.Fatalln("eth start err:", err)
......
...@@ -17,11 +17,11 @@ import ( ...@@ -17,11 +17,11 @@ import (
"github.com/ethereum/go-ethereum/chain" "github.com/ethereum/go-ethereum/chain"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/ethwire"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/rpc" "github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"github.com/ethereum/go-ethereum/wire"
) )
const ( const (
...@@ -88,7 +88,7 @@ type Ethereum struct { ...@@ -88,7 +88,7 @@ type Ethereum struct {
keyManager *crypto.KeyManager keyManager *crypto.KeyManager
clientIdentity ethwire.ClientIdentity clientIdentity wire.ClientIdentity
isUpToDate bool isUpToDate bool
...@@ -97,7 +97,7 @@ type Ethereum struct { ...@@ -97,7 +97,7 @@ type Ethereum struct {
filters map[int]*chain.Filter filters map[int]*chain.Filter
} }
func New(db ethutil.Database, clientIdentity ethwire.ClientIdentity, keyManager *crypto.KeyManager, caps Caps, usePnp bool) (*Ethereum, error) { func New(db ethutil.Database, clientIdentity wire.ClientIdentity, keyManager *crypto.KeyManager, caps Caps, usePnp bool) (*Ethereum, error) {
var err error var err error
var nat NAT var nat NAT
...@@ -142,7 +142,7 @@ func (s *Ethereum) KeyManager() *crypto.KeyManager { ...@@ -142,7 +142,7 @@ func (s *Ethereum) KeyManager() *crypto.KeyManager {
return s.keyManager return s.keyManager
} }
func (s *Ethereum) ClientIdentity() ethwire.ClientIdentity { func (s *Ethereum) ClientIdentity() wire.ClientIdentity {
return s.clientIdentity return s.clientIdentity
} }
...@@ -338,12 +338,12 @@ func (s *Ethereum) InOutPeers() []*Peer { ...@@ -338,12 +338,12 @@ func (s *Ethereum) InOutPeers() []*Peer {
return inboundPeers[:length] return inboundPeers[:length]
} }
func (s *Ethereum) Broadcast(msgType ethwire.MsgType, data []interface{}) { func (s *Ethereum) Broadcast(msgType wire.MsgType, data []interface{}) {
msg := ethwire.NewMessage(msgType, data) msg := wire.NewMessage(msgType, data)
s.BroadcastMsg(msg) s.BroadcastMsg(msg)
} }
func (s *Ethereum) BroadcastMsg(msg *ethwire.Msg) { func (s *Ethereum) BroadcastMsg(msg *wire.Msg) {
eachPeer(s.peers, func(p *Peer, e *list.Element) { eachPeer(s.peers, func(p *Peer, e *list.Element) {
p.QueueMessage(msg) p.QueueMessage(msg)
}) })
......
package ethminer
import (
"bytes"
"sort"
"github.com/ethereum/go-ethereum/chain"
"github.com/ethereum/go-ethereum/ethwire"
"github.com/ethereum/go-ethereum/event"
"github.com/ethereum/go-ethereum/logger"
)
var minerlogger = logger.NewLogger("MINER")
type Miner struct {
pow chain.PoW
ethereum chain.EthManager
coinbase []byte
txs chain.Transactions
uncles []*chain.Block
block *chain.Block
events event.Subscription
powQuitChan chan struct{}
powDone chan struct{}
turbo bool
}
const (
Started = iota
Stopped
)
type Event struct {
Type int // Started || Stopped
Miner *Miner
}
func (self *Miner) GetPow() chain.PoW {
return self.pow
}
func NewDefaultMiner(coinbase []byte, ethereum chain.EthManager) *Miner {
miner := Miner{
pow: &chain.EasyPow{},
ethereum: ethereum,
coinbase: coinbase,
}
return &miner
}
func (self *Miner) ToggleTurbo() {
self.turbo = !self.turbo
self.pow.Turbo(self.turbo)
}
func (miner *Miner) Start() {
// Insert initial TXs in our little miner 'pool'
miner.txs = miner.ethereum.TxPool().Flush()
miner.block = miner.ethereum.ChainManager().NewBlock(miner.coinbase)
mux := miner.ethereum.EventMux()
miner.events = mux.Subscribe(chain.NewBlockEvent{}, chain.TxPreEvent{})
// Prepare inital block
//miner.ethereum.StateManager().Prepare(miner.block.State(), miner.block.State())
go miner.listener()
minerlogger.Infoln("Started")
mux.Post(Event{Started, miner})
}
func (miner *Miner) Stop() {
minerlogger.Infoln("Stopping...")
miner.events.Unsubscribe()
miner.ethereum.EventMux().Post(Event{Stopped, miner})
}
func (miner *Miner) listener() {
miner.startMining()
for {
select {
case event := <-miner.events.Chan():
switch event := event.(type) {
case chain.NewBlockEvent:
miner.stopMining()
block := event.Block
//minerlogger.Infoln("Got new block via Reactor")
if bytes.Compare(miner.ethereum.ChainManager().CurrentBlock.Hash(), block.Hash()) == 0 {
// TODO: Perhaps continue mining to get some uncle rewards
//minerlogger.Infoln("New top block found resetting state")
// Filter out which Transactions we have that were not in this block
var newtxs []*chain.Transaction
for _, tx := range miner.txs {
found := false
for _, othertx := range block.Transactions() {
if bytes.Compare(tx.Hash(), othertx.Hash()) == 0 {
found = true
}
}
if found == false {
newtxs = append(newtxs, tx)
}
}
miner.txs = newtxs
} else {
if bytes.Compare(block.PrevHash, miner.ethereum.ChainManager().CurrentBlock.PrevHash) == 0 {
minerlogger.Infoln("Adding uncle block")
miner.uncles = append(miner.uncles, block)
}
}
miner.startMining()
case chain.TxPreEvent:
miner.stopMining()
found := false
for _, ctx := range miner.txs {
if found = bytes.Compare(ctx.Hash(), event.Tx.Hash()) == 0; found {
break
}
miner.startMining()
}
if found == false {
// Undo all previous commits
miner.block.Undo()
// Apply new transactions
miner.txs = append(miner.txs, event.Tx)
}
}
case <-miner.powDone:
miner.startMining()
}
}
}
func (miner *Miner) startMining() {
if miner.powDone == nil {
miner.powDone = make(chan struct{})
}
miner.powQuitChan = make(chan struct{})
go miner.mineNewBlock()
}
func (miner *Miner) stopMining() {
println("stop mining")
_, isopen := <-miner.powQuitChan
if isopen {
close(miner.powQuitChan)
}
//<-miner.powDone
}
func (self *Miner) mineNewBlock() {
stateManager := self.ethereum.StateManager()
self.block = self.ethereum.ChainManager().NewBlock(self.coinbase)
// Apply uncles
if len(self.uncles) > 0 {
self.block.SetUncles(self.uncles)
}
// Sort the transactions by nonce in case of odd network propagation
sort.Sort(chain.TxByNonce{self.txs})
// Accumulate all valid transactions and apply them to the new state
// Error may be ignored. It's not important during mining
parent := self.ethereum.ChainManager().GetBlock(self.block.PrevHash)
coinbase := self.block.State().GetOrNewStateObject(self.block.Coinbase)
coinbase.SetGasPool(self.block.CalcGasLimit(parent))
receipts, txs, unhandledTxs, erroneous, err := stateManager.ProcessTransactions(coinbase, self.block.State(), self.block, self.block, self.txs)
if err != nil {
minerlogger.Debugln(err)
}
self.ethereum.TxPool().RemoveSet(erroneous)
self.txs = append(txs, unhandledTxs...)
self.block.SetTransactions(txs)
self.block.SetReceipts(receipts)
// Accumulate the rewards included for this block
stateManager.AccumelateRewards(self.block.State(), self.block, parent)
self.block.State().Update()
minerlogger.Infof("Mining on block. Includes %v transactions", len(self.txs))
// Find a valid nonce
nonce := self.pow.Search(self.block, self.powQuitChan)
if nonce != nil {
self.block.Nonce = nonce
err := self.ethereum.StateManager().Process(self.block)
if err != nil {
minerlogger.Infoln(err)
} else {
self.ethereum.Broadcast(ethwire.MsgBlockTy, []interface{}{self.block.Value().Val})
minerlogger.Infof("🔨 Mined block %x\n", self.block.Hash())
minerlogger.Infoln(self.block)
// Gather the new batch of transactions currently in the tx pool
self.txs = self.ethereum.TxPool().CurrentTransactions()
self.ethereum.EventMux().Post(chain.NewBlockEvent{self.block})
}
// Continue mining on the next block
self.startMining()
}
}
This diff is collapsed.
package ethwire package wire
import ( import (
"fmt" "fmt"
......
package ethwire package wire
import ( import (
"fmt" "fmt"
......
package ethwire package wire
import ( import (
"bytes" "bytes"
...@@ -113,7 +113,7 @@ func (self *Connection) readMessages() (err error) { ...@@ -113,7 +113,7 @@ func (self *Connection) readMessages() (err error) {
// The recovering function in case anything goes horribly wrong // The recovering function in case anything goes horribly wrong
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
err = fmt.Errorf("ethwire.ReadMessage error: %v", r) err = fmt.Errorf("wire.ReadMessage error: %v", r)
} }
}() }()
......
// Package ethwire provides low level access to the Ethereum network and allows // Package wire provides low level access to the Ethereum network and allows
// you to broadcast data over the network. // you to broadcast data over the network.
package ethwire package wire
import ( import (
"bytes" "bytes"
...@@ -85,7 +85,7 @@ func ReadMessages(conn net.Conn) (msgs []*Msg, err error) { ...@@ -85,7 +85,7 @@ func ReadMessages(conn net.Conn) (msgs []*Msg, err error) {
// The recovering function in case anything goes horribly wrong // The recovering function in case anything goes horribly wrong
defer func() { defer func() {
if r := recover(); r != nil { if r := recover(); r != nil {
err = fmt.Errorf("ethwire.ReadMessage error: %v", r) err = fmt.Errorf("wire.ReadMessage error: %v", r)
} }
}() }()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment