Skip to content

Commit

Permalink
Merge pull request ethereum#34 from ethereum/revert-33-feature/ethuti…
Browse files Browse the repository at this point in the history
…l-refactor

Revert "ethreact - Feature/ethutil refactor"
  • Loading branch information
obscuren committed Jul 7, 2014
2 parents 9dab7dc + 6fe9b4a commit 239a5d3
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 353 deletions.
5 changes: 2 additions & 3 deletions ethchain/dagger.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package ethchain
import (
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethreact"
"github.com/ethereum/eth-go/ethutil"
"github.com/obscuren/sha3"
"hash"
Expand All @@ -15,15 +14,15 @@ import (
var powlogger = ethlog.NewLogger("POW")

type PoW interface {
Search(block *Block, reactChan chan ethreact.Event) []byte
Search(block *Block, reactChan chan ethutil.React) []byte
Verify(hash []byte, diff *big.Int, nonce []byte) bool
}

type EasyPow struct {
hash *big.Int
}

func (pow *EasyPow) Search(block *Block, reactChan chan ethreact.Event) []byte {
func (pow *EasyPow) Search(block *Block, reactChan chan ethutil.React) []byte {
r := rand.New(rand.NewSource(time.Now().UnixNano()))
hash := block.HashNoNonce()
diff := block.Difficulty
Expand Down
3 changes: 1 addition & 2 deletions ethchain/state_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethreact"
"github.com/ethereum/eth-go/ethtrie"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
Expand Down Expand Up @@ -37,7 +36,7 @@ type EthManager interface {
BlockChain() *BlockChain
TxPool() *TxPool
Broadcast(msgType ethwire.MsgType, data []interface{})
Reactor() *ethreact.ReactorEngine
Reactor() *ethutil.ReactorEngine
PeerCount() int
IsMining() bool
IsListening() bool
Expand Down
11 changes: 3 additions & 8 deletions ethereum.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethcrypto"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethreact"
"github.com/ethereum/eth-go/ethrpc"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
Expand Down Expand Up @@ -72,7 +71,7 @@ type Ethereum struct {

listening bool

reactor *ethreact.ReactorEngine
reactor *ethutil.ReactorEngine

RpcServer *ethrpc.JsonRpcServer

Expand Down Expand Up @@ -107,7 +106,7 @@ func New(db ethutil.Database, clientIdentity ethwire.ClientIdentity, keyManager
keyManager: keyManager,
clientIdentity: clientIdentity,
}
ethereum.reactor = ethreact.New()
ethereum.reactor = ethutil.NewReactorEngine()

ethereum.txPool = ethchain.NewTxPool(ethereum)
ethereum.blockChain = ethchain.NewBlockChain(ethereum)
Expand All @@ -119,7 +118,7 @@ func New(db ethutil.Database, clientIdentity ethwire.ClientIdentity, keyManager
return ethereum, nil
}

func (s *Ethereum) Reactor() *ethreact.ReactorEngine {
func (s *Ethereum) Reactor() *ethutil.ReactorEngine {
return s.reactor
}

Expand Down Expand Up @@ -351,7 +350,6 @@ func (s *Ethereum) ReapDeadPeerHandler() {

// Start the ethereum
func (s *Ethereum) Start(seed bool) {
s.reactor.Start()
// Bind to addr and port
ln, err := net.Listen("tcp", ":"+s.Port)
if err != nil {
Expand Down Expand Up @@ -465,9 +463,6 @@ func (s *Ethereum) Stop() {
s.txPool.Stop()
s.stateManager.Stop()

s.reactor.Flush()
s.reactor.Stop()

ethlogger.Infoln("Server stopped")
close(s.shutdownChan)
}
Expand Down
57 changes: 18 additions & 39 deletions ethlog/loggers.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ func (msg *logMessage) send(logger LogSystem) {
var logMessages chan (*logMessage)
var logSystems []LogSystem
var quit chan bool
var drained chan bool
var shutdown chan bool
var mutex = sync.Mutex{}

type LogLevel uint8

Expand All @@ -60,41 +57,29 @@ func start() {
out:
for {
select {
case <-quit:
break out
case msg := <-logMessages:
for _, logSystem := range logSystems {
if logSystem.GetLogLevel() >= msg.LogLevel {
msg.send(logSystem)
}
}
case drained <- true:
default:
drained <- true // this blocks until a message is sent to the queu
}
}
close(shutdown)
}

func Reset() {
mutex.Lock()
defer mutex.Unlock()
if logSystems != nil {
quit <- true
select {
case <-drained:
case <-quit:
break out
}
<-shutdown
}
logSystems = nil
}

// waits until log messages are drained (dispatched to log writers)
func Flush() {
mutex.Lock()
defer mutex.Unlock()
if logSystems != nil {
<-drained
quit <- true

done:
for {
select {
case <-logMessages:
default:
break done
}
}
}

Expand All @@ -107,34 +92,28 @@ func NewLogger(tag string) *Logger {
}

func AddLogSystem(logSystem LogSystem) {
var mutex = &sync.Mutex{}
mutex.Lock()
defer mutex.Unlock()
if logSystems == nil {
logMessages = make(chan *logMessage)
quit = make(chan bool)
drained = make(chan bool, 1)
shutdown = make(chan bool, 1)
go start()
}
logSystems = append(logSystems, logSystem)
}

func send(msg *logMessage) {
select {
case <-drained:
}
logMessages <- msg
}

func (logger *Logger) sendln(level LogLevel, v ...interface{}) {
if logSystems != nil {
send(newPrintlnLogMessage(level, logger.tag, v...))
if logMessages != nil {
msg := newPrintlnLogMessage(level, logger.tag, v...)
logMessages <- msg
}
}

func (logger *Logger) sendf(level LogLevel, format string, v ...interface{}) {
if logSystems != nil {
send(newPrintfLogMessage(level, logger.tag, format, v...))
if logMessages != nil {
msg := newPrintfLogMessage(level, logger.tag, format, v...)
logMessages <- msg
}
}

Expand Down
23 changes: 12 additions & 11 deletions ethlog/loggers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ func (t *TestLogSystem) GetLogLevel() LogLevel {
return t.level
}

func quote(s string) string {
return fmt.Sprintf("'%s'", s)
}

func TestLoggerPrintln(t *testing.T) {
logger := NewLogger("TEST")
testLogSystem := &TestLogSystem{level: WarnLevel}
Expand All @@ -37,10 +41,10 @@ func TestLoggerPrintln(t *testing.T) {
logger.Infoln("info")
logger.Debugln("debug")
Flush()
Reset()
output := testLogSystem.Output
fmt.Println(quote(output))
if output != "[TEST] error\n[TEST] warn\n" {
t.Error("Expected logger output '[TEST] error\\n[TEST] warn\\n', got ", testLogSystem.Output)
t.Error("Expected logger output '[TEST] error\\n[TEST] warn\\n', got ", quote(testLogSystem.Output))
}
}

Expand All @@ -53,10 +57,10 @@ func TestLoggerPrintf(t *testing.T) {
logger.Infof("info")
logger.Debugf("debug")
Flush()
Reset()
output := testLogSystem.Output
fmt.Println(quote(output))
if output != "[TEST] error to { 2}\n[TEST] warn" {
t.Error("Expected logger output '[TEST] error to { 2}\\n[TEST] warn', got ", testLogSystem.Output)
t.Error("Expected logger output '[TEST] error to { 2}\\n[TEST] warn', got ", quote(testLogSystem.Output))
}
}

Expand All @@ -69,14 +73,13 @@ func TestMultipleLogSystems(t *testing.T) {
logger.Errorln("error")
logger.Warnln("warn")
Flush()
Reset()
output0 := testLogSystem0.Output
output1 := testLogSystem1.Output
if output0 != "[TEST] error\n" {
t.Error("Expected logger 0 output '[TEST] error\\n', got ", testLogSystem0.Output)
t.Error("Expected logger 0 output '[TEST] error\\n', got ", quote(testLogSystem0.Output))
}
if output1 != "[TEST] error\n[TEST] warn\n" {
t.Error("Expected logger 1 output '[TEST] error\\n[TEST] warn\\n', got ", testLogSystem1.Output)
t.Error("Expected logger 1 output '[TEST] error\\n[TEST] warn\\n', got ", quote(testLogSystem1.Output))
}
}

Expand All @@ -89,11 +92,11 @@ func TestFileLogSystem(t *testing.T) {
logger.Errorf("error to %s\n", filename)
logger.Warnln("warn")
Flush()
Reset()
contents, _ := ioutil.ReadFile(filename)
output := string(contents)
fmt.Println(quote(output))
if output != "[TEST] error to test.log\n[TEST] warn\n" {
t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", output)
t.Error("Expected contents of file 'test.log': '[TEST] error to test.log\\n[TEST] warn\\n', got ", quote(output))
} else {
os.Remove(filename)
}
Expand All @@ -102,7 +105,5 @@ func TestFileLogSystem(t *testing.T) {
func TestNoLogSystem(t *testing.T) {
logger := NewLogger("TEST")
logger.Warnln("warn")
fmt.Println("1")
Flush()
Reset()
}
12 changes: 6 additions & 6 deletions ethminer/miner.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"bytes"
"github.com/ethereum/eth-go/ethchain"
"github.com/ethereum/eth-go/ethlog"
"github.com/ethereum/eth-go/ethreact"
"github.com/ethereum/eth-go/ethutil"
"github.com/ethereum/eth-go/ethwire"
"sort"
)
Expand All @@ -15,19 +15,19 @@ type Miner struct {
pow ethchain.PoW
ethereum ethchain.EthManager
coinbase []byte
reactChan chan ethreact.Event
reactChan chan ethutil.React
txs ethchain.Transactions
uncles []*ethchain.Block
block *ethchain.Block
powChan chan []byte
powQuitChan chan ethreact.Event
powQuitChan chan ethutil.React
quitChan chan bool
}

func NewDefaultMiner(coinbase []byte, ethereum ethchain.EthManager) Miner {
reactChan := make(chan ethreact.Event, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in
powChan := make(chan []byte, 1) // This is the channel that receives valid sha hases for a given block
powQuitChan := make(chan ethreact.Event, 1) // This is the channel that can exit the miner thread
reactChan := make(chan ethutil.React, 1) // This is the channel that receives 'updates' when ever a new transaction or block comes in
powChan := make(chan []byte, 1) // This is the channel that receives valid sha hases for a given block
powQuitChan := make(chan ethutil.React, 1) // This is the channel that can exit the miner thread
quitChan := make(chan bool, 1)

ethereum.Reactor().Subscribe("newBlock", reactChan)
Expand Down
40 changes: 0 additions & 40 deletions ethreact/README.md

This file was deleted.

Loading

0 comments on commit 239a5d3

Please sign in to comment.