forked from mirror/go-ethereum
parent
f2da6581ba
commit
13e3b2f433
@ -1,37 +0,0 @@ |
|||||||
// Copyright 2014 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package logger |
|
||||||
|
|
||||||
import "os" |
|
||||||
|
|
||||||
func ExampleLogger() { |
|
||||||
logger := NewLogger("TAG") |
|
||||||
logger.Infoln("so awesome") // prints [TAG] so awesome
|
|
||||||
logger.Infof("this %q is raw", "coin") // prints [TAG] this "coin" is raw
|
|
||||||
} |
|
||||||
|
|
||||||
func ExampleLogSystem() { |
|
||||||
filename := "test.log" |
|
||||||
file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) |
|
||||||
fileLog := NewStdLogSystem(file, 0, WarnLevel) |
|
||||||
AddLogSystem(fileLog) |
|
||||||
|
|
||||||
stdoutLog := NewStdLogSystem(os.Stdout, 0, WarnLevel) |
|
||||||
AddLogSystem(stdoutLog) |
|
||||||
|
|
||||||
NewLogger("TAG").Warnln("reactor meltdown") // writes to both logs
|
|
||||||
} |
|
@ -1,65 +0,0 @@ |
|||||||
// Copyright 2015 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package logger |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
"io" |
|
||||||
"log" |
|
||||||
"os" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common" |
|
||||||
) |
|
||||||
|
|
||||||
func openLogFile(datadir string, filename string) *os.File { |
|
||||||
path := common.AbsolutePath(datadir, filename) |
|
||||||
file, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666) |
|
||||||
if err != nil { |
|
||||||
panic(fmt.Sprintf("error opening log file '%s': %v", filename, err)) |
|
||||||
} |
|
||||||
return file |
|
||||||
} |
|
||||||
|
|
||||||
func New(datadir string, logFile string, logLevel int) LogSystem { |
|
||||||
var writer io.Writer |
|
||||||
if logFile == "" { |
|
||||||
writer = os.Stdout |
|
||||||
} else { |
|
||||||
writer = openLogFile(datadir, logFile) |
|
||||||
} |
|
||||||
|
|
||||||
var sys LogSystem |
|
||||||
sys = NewStdLogSystem(writer, log.LstdFlags, LogLevel(logLevel)) |
|
||||||
AddLogSystem(sys) |
|
||||||
|
|
||||||
return sys |
|
||||||
} |
|
||||||
|
|
||||||
func NewJSONsystem(datadir string, logFile string) LogSystem { |
|
||||||
var writer io.Writer |
|
||||||
if logFile == "-" { |
|
||||||
writer = os.Stdout |
|
||||||
} else { |
|
||||||
writer = openLogFile(datadir, logFile) |
|
||||||
} |
|
||||||
|
|
||||||
var sys LogSystem |
|
||||||
sys = NewJsonLogSystem(writer) |
|
||||||
AddLogSystem(sys) |
|
||||||
|
|
||||||
return sys |
|
||||||
} |
|
@ -1,149 +0,0 @@ |
|||||||
// Copyright 2014 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
/* |
|
||||||
Package logger implements a multi-output leveled logger. |
|
||||||
|
|
||||||
Other packages use tagged logger to send log messages to shared |
|
||||||
(process-wide) logging engine. The shared logging engine dispatches to |
|
||||||
multiple log systems. The log level can be set separately per log |
|
||||||
system. |
|
||||||
|
|
||||||
Logging is asynchronous and does not block the caller. Message |
|
||||||
formatting is performed by the caller goroutine to avoid incorrect |
|
||||||
logging of mutable state. |
|
||||||
*/ |
|
||||||
package logger |
|
||||||
|
|
||||||
import ( |
|
||||||
"encoding/json" |
|
||||||
"fmt" |
|
||||||
"os" |
|
||||||
) |
|
||||||
|
|
||||||
type LogLevel uint32 |
|
||||||
|
|
||||||
const ( |
|
||||||
// Standard log levels
|
|
||||||
Silence LogLevel = iota |
|
||||||
ErrorLevel |
|
||||||
WarnLevel |
|
||||||
InfoLevel |
|
||||||
DebugLevel |
|
||||||
DebugDetailLevel |
|
||||||
) |
|
||||||
|
|
||||||
// A Logger prints messages prefixed by a given tag. It provides named
|
|
||||||
// Printf and Println style methods for all loglevels. Each ethereum
|
|
||||||
// component should have its own logger with a unique prefix.
|
|
||||||
type Logger struct { |
|
||||||
tag string |
|
||||||
} |
|
||||||
|
|
||||||
func NewLogger(tag string) *Logger { |
|
||||||
return &Logger{"[" + tag + "] "} |
|
||||||
} |
|
||||||
|
|
||||||
func (logger *Logger) Sendln(level LogLevel, v ...interface{}) { |
|
||||||
logMessageC <- stdMsg{level, logger.tag + fmt.Sprintln(v...)} |
|
||||||
} |
|
||||||
|
|
||||||
func (logger *Logger) Sendf(level LogLevel, format string, v ...interface{}) { |
|
||||||
logMessageC <- stdMsg{level, logger.tag + fmt.Sprintf(format, v...)} |
|
||||||
} |
|
||||||
|
|
||||||
// Errorln writes a message with ErrorLevel.
|
|
||||||
func (logger *Logger) Errorln(v ...interface{}) { |
|
||||||
logger.Sendln(ErrorLevel, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// Warnln writes a message with WarnLevel.
|
|
||||||
func (logger *Logger) Warnln(v ...interface{}) { |
|
||||||
logger.Sendln(WarnLevel, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// Infoln writes a message with InfoLevel.
|
|
||||||
func (logger *Logger) Infoln(v ...interface{}) { |
|
||||||
logger.Sendln(InfoLevel, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// Debugln writes a message with DebugLevel.
|
|
||||||
func (logger *Logger) Debugln(v ...interface{}) { |
|
||||||
logger.Sendln(DebugLevel, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// DebugDetailln writes a message with DebugDetailLevel.
|
|
||||||
func (logger *Logger) DebugDetailln(v ...interface{}) { |
|
||||||
logger.Sendln(DebugDetailLevel, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// Errorf writes a message with ErrorLevel.
|
|
||||||
func (logger *Logger) Errorf(format string, v ...interface{}) { |
|
||||||
logger.Sendf(ErrorLevel, format, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// Warnf writes a message with WarnLevel.
|
|
||||||
func (logger *Logger) Warnf(format string, v ...interface{}) { |
|
||||||
logger.Sendf(WarnLevel, format, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// Infof writes a message with InfoLevel.
|
|
||||||
func (logger *Logger) Infof(format string, v ...interface{}) { |
|
||||||
logger.Sendf(InfoLevel, format, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// Debugf writes a message with DebugLevel.
|
|
||||||
func (logger *Logger) Debugf(format string, v ...interface{}) { |
|
||||||
logger.Sendf(DebugLevel, format, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// DebugDetailf writes a message with DebugDetailLevel.
|
|
||||||
func (logger *Logger) DebugDetailf(format string, v ...interface{}) { |
|
||||||
logger.Sendf(DebugDetailLevel, format, v...) |
|
||||||
} |
|
||||||
|
|
||||||
// Fatalln writes a message with ErrorLevel and exits the program.
|
|
||||||
func (logger *Logger) Fatalln(v ...interface{}) { |
|
||||||
logger.Sendln(ErrorLevel, v...) |
|
||||||
Flush() |
|
||||||
os.Exit(0) |
|
||||||
} |
|
||||||
|
|
||||||
// Fatalf writes a message with ErrorLevel and exits the program.
|
|
||||||
func (logger *Logger) Fatalf(format string, v ...interface{}) { |
|
||||||
logger.Sendf(ErrorLevel, format, v...) |
|
||||||
Flush() |
|
||||||
os.Exit(0) |
|
||||||
} |
|
||||||
|
|
||||||
type JsonLogger struct { |
|
||||||
Coinbase string |
|
||||||
} |
|
||||||
|
|
||||||
func NewJsonLogger() *JsonLogger { |
|
||||||
return &JsonLogger{} |
|
||||||
} |
|
||||||
|
|
||||||
func (logger *JsonLogger) LogJson(v JsonLog) { |
|
||||||
msgname := v.EventName() |
|
||||||
obj := map[string]interface{}{ |
|
||||||
msgname: v, |
|
||||||
} |
|
||||||
|
|
||||||
jsontxt, _ := json.Marshal(obj) |
|
||||||
logMessageC <- (jsonMsg(jsontxt)) |
|
||||||
|
|
||||||
} |
|
@ -1,192 +0,0 @@ |
|||||||
// Copyright 2014 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package logger |
|
||||||
|
|
||||||
import ( |
|
||||||
"io/ioutil" |
|
||||||
"math/rand" |
|
||||||
"os" |
|
||||||
"sync" |
|
||||||
"testing" |
|
||||||
"time" |
|
||||||
) |
|
||||||
|
|
||||||
type TestLogSystem struct { |
|
||||||
mutex sync.Mutex |
|
||||||
output string |
|
||||||
level LogLevel |
|
||||||
} |
|
||||||
|
|
||||||
func (ls *TestLogSystem) LogPrint(msg LogMsg) { |
|
||||||
ls.mutex.Lock() |
|
||||||
if ls.level >= msg.Level() { |
|
||||||
ls.output += msg.String() |
|
||||||
} |
|
||||||
ls.mutex.Unlock() |
|
||||||
} |
|
||||||
|
|
||||||
func (ls *TestLogSystem) SetLogLevel(i LogLevel) { |
|
||||||
ls.mutex.Lock() |
|
||||||
ls.level = i |
|
||||||
ls.mutex.Unlock() |
|
||||||
} |
|
||||||
|
|
||||||
func (ls *TestLogSystem) GetLogLevel() LogLevel { |
|
||||||
ls.mutex.Lock() |
|
||||||
defer ls.mutex.Unlock() |
|
||||||
return ls.level |
|
||||||
} |
|
||||||
|
|
||||||
func (ls *TestLogSystem) CheckOutput(t *testing.T, expected string) { |
|
||||||
ls.mutex.Lock() |
|
||||||
output := ls.output |
|
||||||
ls.mutex.Unlock() |
|
||||||
if output != expected { |
|
||||||
t.Errorf("log output mismatch:\n got: %q\n want: %q\n", output, expected) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
type blockedLogSystem struct { |
|
||||||
LogSystem |
|
||||||
unblock chan struct{} |
|
||||||
} |
|
||||||
|
|
||||||
func (ls blockedLogSystem) LogPrint(msg LogMsg) { |
|
||||||
<-ls.unblock |
|
||||||
ls.LogSystem.LogPrint(msg) |
|
||||||
} |
|
||||||
|
|
||||||
func TestLoggerFlush(t *testing.T) { |
|
||||||
Reset() |
|
||||||
|
|
||||||
logger := NewLogger("TEST") |
|
||||||
ls := blockedLogSystem{&TestLogSystem{level: WarnLevel}, make(chan struct{})} |
|
||||||
AddLogSystem(ls) |
|
||||||
for i := 0; i < 5; i++ { |
|
||||||
// these writes shouldn't hang even though ls is blocked
|
|
||||||
logger.Errorf(".") |
|
||||||
} |
|
||||||
|
|
||||||
beforeFlush := time.Now() |
|
||||||
time.AfterFunc(80*time.Millisecond, func() { close(ls.unblock) }) |
|
||||||
Flush() // this should hang for approx. 80ms
|
|
||||||
if blockd := time.Now().Sub(beforeFlush); blockd < 80*time.Millisecond { |
|
||||||
t.Errorf("Flush didn't block long enough, blocked for %v, should've been >= 80ms", blockd) |
|
||||||
} |
|
||||||
|
|
||||||
ls.LogSystem.(*TestLogSystem).CheckOutput(t, "[TEST] .[TEST] .[TEST] .[TEST] .[TEST] .") |
|
||||||
} |
|
||||||
|
|
||||||
func TestLoggerPrintln(t *testing.T) { |
|
||||||
Reset() |
|
||||||
|
|
||||||
logger := NewLogger("TEST") |
|
||||||
testLogSystem := &TestLogSystem{level: WarnLevel} |
|
||||||
AddLogSystem(testLogSystem) |
|
||||||
logger.Errorln("error") |
|
||||||
logger.Warnln("warn") |
|
||||||
logger.Infoln("info") |
|
||||||
logger.Debugln("debug") |
|
||||||
Flush() |
|
||||||
|
|
||||||
testLogSystem.CheckOutput(t, "[TEST] error\n[TEST] warn\n") |
|
||||||
} |
|
||||||
|
|
||||||
func TestLoggerPrintf(t *testing.T) { |
|
||||||
Reset() |
|
||||||
|
|
||||||
logger := NewLogger("TEST") |
|
||||||
testLogSystem := &TestLogSystem{level: WarnLevel} |
|
||||||
AddLogSystem(testLogSystem) |
|
||||||
logger.Errorf("error to %v\n", []int{1, 2, 3}) |
|
||||||
logger.Warnf("warn %%d %d", 5) |
|
||||||
logger.Infof("info") |
|
||||||
logger.Debugf("debug") |
|
||||||
Flush() |
|
||||||
testLogSystem.CheckOutput(t, "[TEST] error to [1 2 3]\n[TEST] warn %d 5") |
|
||||||
} |
|
||||||
|
|
||||||
func TestMultipleLogSystems(t *testing.T) { |
|
||||||
Reset() |
|
||||||
|
|
||||||
logger := NewLogger("TEST") |
|
||||||
testLogSystem0 := &TestLogSystem{level: ErrorLevel} |
|
||||||
testLogSystem1 := &TestLogSystem{level: WarnLevel} |
|
||||||
AddLogSystem(testLogSystem0) |
|
||||||
AddLogSystem(testLogSystem1) |
|
||||||
logger.Errorln("error") |
|
||||||
logger.Warnln("warn") |
|
||||||
Flush() |
|
||||||
|
|
||||||
testLogSystem0.CheckOutput(t, "[TEST] error\n") |
|
||||||
testLogSystem1.CheckOutput(t, "[TEST] error\n[TEST] warn\n") |
|
||||||
} |
|
||||||
|
|
||||||
func TestFileLogSystem(t *testing.T) { |
|
||||||
Reset() |
|
||||||
|
|
||||||
logger := NewLogger("TEST") |
|
||||||
filename := "test.log" |
|
||||||
file, _ := os.OpenFile(filename, os.O_RDWR|os.O_CREATE, os.ModePerm) |
|
||||||
testLogSystem := NewStdLogSystem(file, 0, WarnLevel) |
|
||||||
AddLogSystem(testLogSystem) |
|
||||||
logger.Errorf("error to %s\n", filename) |
|
||||||
logger.Warnln("warn") |
|
||||||
Flush() |
|
||||||
contents, _ := ioutil.ReadFile(filename) |
|
||||||
output := string(contents) |
|
||||||
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) |
|
||||||
} else { |
|
||||||
os.Remove(filename) |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func TestNoLogSystem(t *testing.T) { |
|
||||||
Reset() |
|
||||||
|
|
||||||
logger := NewLogger("TEST") |
|
||||||
logger.Warnln("warn") |
|
||||||
Flush() |
|
||||||
} |
|
||||||
|
|
||||||
func TestConcurrentAddSystem(t *testing.T) { |
|
||||||
rand.Seed(time.Now().Unix()) |
|
||||||
Reset() |
|
||||||
|
|
||||||
logger := NewLogger("TEST") |
|
||||||
stop := make(chan struct{}) |
|
||||||
writer := func() { |
|
||||||
select { |
|
||||||
case <-stop: |
|
||||||
return |
|
||||||
default: |
|
||||||
logger.Infoln("foo") |
|
||||||
Flush() |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
go writer() |
|
||||||
go writer() |
|
||||||
|
|
||||||
stopTime := time.Now().Add(100 * time.Millisecond) |
|
||||||
for time.Now().Before(stopTime) { |
|
||||||
time.Sleep(time.Duration(rand.Intn(20)) * time.Millisecond) |
|
||||||
AddLogSystem(NewStdLogSystem(ioutil.Discard, 0, InfoLevel)) |
|
||||||
} |
|
||||||
close(stop) |
|
||||||
} |
|
@ -1,76 +0,0 @@ |
|||||||
// Copyright 2015 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package logger |
|
||||||
|
|
||||||
import ( |
|
||||||
"io" |
|
||||||
"log" |
|
||||||
"sync/atomic" |
|
||||||
) |
|
||||||
|
|
||||||
// LogSystem is implemented by log output devices.
|
|
||||||
// All methods can be called concurrently from multiple goroutines.
|
|
||||||
type LogSystem interface { |
|
||||||
LogPrint(LogMsg) |
|
||||||
} |
|
||||||
|
|
||||||
// NewStdLogSystem creates a LogSystem that prints to the given writer.
|
|
||||||
// The flag values are defined package log.
|
|
||||||
func NewStdLogSystem(writer io.Writer, flags int, level LogLevel) *StdLogSystem { |
|
||||||
logger := log.New(writer, "", flags) |
|
||||||
return &StdLogSystem{logger, uint32(level)} |
|
||||||
} |
|
||||||
|
|
||||||
type StdLogSystem struct { |
|
||||||
logger *log.Logger |
|
||||||
level uint32 |
|
||||||
} |
|
||||||
|
|
||||||
func (t *StdLogSystem) LogPrint(msg LogMsg) { |
|
||||||
stdmsg, ok := msg.(stdMsg) |
|
||||||
if ok { |
|
||||||
if t.GetLogLevel() >= stdmsg.Level() { |
|
||||||
t.logger.Print(stdmsg.String()) |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func (t *StdLogSystem) SetLogLevel(i LogLevel) { |
|
||||||
atomic.StoreUint32(&t.level, uint32(i)) |
|
||||||
} |
|
||||||
|
|
||||||
func (t *StdLogSystem) GetLogLevel() LogLevel { |
|
||||||
return LogLevel(atomic.LoadUint32(&t.level)) |
|
||||||
} |
|
||||||
|
|
||||||
// NewJSONLogSystem creates a LogSystem that prints to the given writer without
|
|
||||||
// adding extra information irrespective of loglevel only if message is JSON type
|
|
||||||
func NewJsonLogSystem(writer io.Writer) LogSystem { |
|
||||||
logger := log.New(writer, "", 0) |
|
||||||
return &jsonLogSystem{logger} |
|
||||||
} |
|
||||||
|
|
||||||
type jsonLogSystem struct { |
|
||||||
logger *log.Logger |
|
||||||
} |
|
||||||
|
|
||||||
func (t *jsonLogSystem) LogPrint(msg LogMsg) { |
|
||||||
jsonmsg, ok := msg.(jsonMsg) |
|
||||||
if ok { |
|
||||||
t.logger.Print(jsonmsg.String()) |
|
||||||
} |
|
||||||
} |
|
@ -1,142 +0,0 @@ |
|||||||
// Copyright 2015 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package logger |
|
||||||
|
|
||||||
import ( |
|
||||||
"fmt" |
|
||||||
"sync" |
|
||||||
) |
|
||||||
|
|
||||||
type stdMsg struct { |
|
||||||
level LogLevel |
|
||||||
msg string |
|
||||||
} |
|
||||||
|
|
||||||
type jsonMsg []byte |
|
||||||
|
|
||||||
func (m jsonMsg) Level() LogLevel { |
|
||||||
return 0 |
|
||||||
} |
|
||||||
|
|
||||||
func (m jsonMsg) String() string { |
|
||||||
return string(m) |
|
||||||
} |
|
||||||
|
|
||||||
type LogMsg interface { |
|
||||||
Level() LogLevel |
|
||||||
fmt.Stringer |
|
||||||
} |
|
||||||
|
|
||||||
func (m stdMsg) Level() LogLevel { |
|
||||||
return m.level |
|
||||||
} |
|
||||||
|
|
||||||
func (m stdMsg) String() string { |
|
||||||
return m.msg |
|
||||||
} |
|
||||||
|
|
||||||
var ( |
|
||||||
logMessageC = make(chan LogMsg) |
|
||||||
addSystemC = make(chan LogSystem) |
|
||||||
flushC = make(chan chan struct{}) |
|
||||||
resetC = make(chan chan struct{}) |
|
||||||
) |
|
||||||
|
|
||||||
func init() { |
|
||||||
go dispatchLoop() |
|
||||||
} |
|
||||||
|
|
||||||
// each system can buffer this many messages before
|
|
||||||
// blocking incoming log messages.
|
|
||||||
const sysBufferSize = 500 |
|
||||||
|
|
||||||
func dispatchLoop() { |
|
||||||
var ( |
|
||||||
systems []LogSystem |
|
||||||
systemIn []chan LogMsg |
|
||||||
systemWG sync.WaitGroup |
|
||||||
) |
|
||||||
bootSystem := func(sys LogSystem) { |
|
||||||
in := make(chan LogMsg, sysBufferSize) |
|
||||||
systemIn = append(systemIn, in) |
|
||||||
systemWG.Add(1) |
|
||||||
go sysLoop(sys, in, &systemWG) |
|
||||||
} |
|
||||||
|
|
||||||
for { |
|
||||||
select { |
|
||||||
case msg := <-logMessageC: |
|
||||||
for _, c := range systemIn { |
|
||||||
c <- msg |
|
||||||
} |
|
||||||
|
|
||||||
case sys := <-addSystemC: |
|
||||||
systems = append(systems, sys) |
|
||||||
bootSystem(sys) |
|
||||||
|
|
||||||
case waiter := <-resetC: |
|
||||||
// reset means terminate all systems
|
|
||||||
for _, c := range systemIn { |
|
||||||
close(c) |
|
||||||
} |
|
||||||
systems = nil |
|
||||||
systemIn = nil |
|
||||||
systemWG.Wait() |
|
||||||
close(waiter) |
|
||||||
|
|
||||||
case waiter := <-flushC: |
|
||||||
// flush means reboot all systems
|
|
||||||
for _, c := range systemIn { |
|
||||||
close(c) |
|
||||||
} |
|
||||||
systemIn = nil |
|
||||||
systemWG.Wait() |
|
||||||
for _, sys := range systems { |
|
||||||
bootSystem(sys) |
|
||||||
} |
|
||||||
close(waiter) |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func sysLoop(sys LogSystem, in <-chan LogMsg, wg *sync.WaitGroup) { |
|
||||||
for msg := range in { |
|
||||||
sys.LogPrint(msg) |
|
||||||
} |
|
||||||
wg.Done() |
|
||||||
} |
|
||||||
|
|
||||||
// Reset removes all active log systems.
|
|
||||||
// It blocks until all current messages have been delivered.
|
|
||||||
func Reset() { |
|
||||||
waiter := make(chan struct{}) |
|
||||||
resetC <- waiter |
|
||||||
<-waiter |
|
||||||
} |
|
||||||
|
|
||||||
// Flush waits until all current log messages have been dispatched to
|
|
||||||
// the active log systems.
|
|
||||||
func Flush() { |
|
||||||
waiter := make(chan struct{}) |
|
||||||
flushC <- waiter |
|
||||||
<-waiter |
|
||||||
} |
|
||||||
|
|
||||||
// AddLogSystem starts printing messages to the given LogSystem.
|
|
||||||
func AddLogSystem(sys LogSystem) { |
|
||||||
addSystemC <- sys |
|
||||||
} |
|
@ -1,381 +0,0 @@ |
|||||||
// Copyright 2015 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package logger |
|
||||||
|
|
||||||
import ( |
|
||||||
"math/big" |
|
||||||
"time" |
|
||||||
) |
|
||||||
|
|
||||||
type utctime8601 struct{} |
|
||||||
|
|
||||||
func (utctime8601) MarshalJSON() ([]byte, error) { |
|
||||||
timestr := time.Now().UTC().Format(time.RFC3339Nano) |
|
||||||
// Bounds check
|
|
||||||
if len(timestr) > 26 { |
|
||||||
timestr = timestr[:26] |
|
||||||
} |
|
||||||
return []byte(`"` + timestr + `Z"`), nil |
|
||||||
} |
|
||||||
|
|
||||||
type JsonLog interface { |
|
||||||
EventName() string |
|
||||||
} |
|
||||||
|
|
||||||
type LogEvent struct { |
|
||||||
// Guid string `json:"guid"`
|
|
||||||
Ts utctime8601 `json:"ts"` |
|
||||||
// Level string `json:"level"`
|
|
||||||
} |
|
||||||
|
|
||||||
type LogStarting struct { |
|
||||||
ClientString string `json:"client_impl"` |
|
||||||
ProtocolVersion int `json:"eth_version"` |
|
||||||
LogEvent |
|
||||||
} |
|
||||||
|
|
||||||
func (l *LogStarting) EventName() string { |
|
||||||
return "starting" |
|
||||||
} |
|
||||||
|
|
||||||
type P2PConnected struct { |
|
||||||
RemoteId string `json:"remote_id"` |
|
||||||
RemoteAddress string `json:"remote_addr"` |
|
||||||
RemoteVersionString string `json:"remote_version_string"` |
|
||||||
NumConnections int `json:"num_connections"` |
|
||||||
LogEvent |
|
||||||
} |
|
||||||
|
|
||||||
func (l *P2PConnected) EventName() string { |
|
||||||
return "p2p.connected" |
|
||||||
} |
|
||||||
|
|
||||||
type P2PDisconnected struct { |
|
||||||
NumConnections int `json:"num_connections"` |
|
||||||
RemoteId string `json:"remote_id"` |
|
||||||
LogEvent |
|
||||||
} |
|
||||||
|
|
||||||
func (l *P2PDisconnected) EventName() string { |
|
||||||
return "p2p.disconnected" |
|
||||||
} |
|
||||||
|
|
||||||
type EthMinerNewBlock struct { |
|
||||||
BlockHash string `json:"block_hash"` |
|
||||||
BlockNumber *big.Int `json:"block_number"` |
|
||||||
ChainHeadHash string `json:"chain_head_hash"` |
|
||||||
BlockPrevHash string `json:"block_prev_hash"` |
|
||||||
LogEvent |
|
||||||
} |
|
||||||
|
|
||||||
func (l *EthMinerNewBlock) EventName() string { |
|
||||||
return "eth.miner.new_block" |
|
||||||
} |
|
||||||
|
|
||||||
type EthChainReceivedNewBlock struct { |
|
||||||
BlockHash string `json:"block_hash"` |
|
||||||
BlockNumber *big.Int `json:"block_number"` |
|
||||||
ChainHeadHash string `json:"chain_head_hash"` |
|
||||||
BlockPrevHash string `json:"block_prev_hash"` |
|
||||||
RemoteId string `json:"remote_id"` |
|
||||||
LogEvent |
|
||||||
} |
|
||||||
|
|
||||||
func (l *EthChainReceivedNewBlock) EventName() string { |
|
||||||
return "eth.chain.received.new_block" |
|
||||||
} |
|
||||||
|
|
||||||
type EthChainNewHead struct { |
|
||||||
BlockHash string `json:"block_hash"` |
|
||||||
BlockNumber *big.Int `json:"block_number"` |
|
||||||
ChainHeadHash string `json:"chain_head_hash"` |
|
||||||
BlockPrevHash string `json:"block_prev_hash"` |
|
||||||
LogEvent |
|
||||||
} |
|
||||||
|
|
||||||
func (l *EthChainNewHead) EventName() string { |
|
||||||
return "eth.chain.new_head" |
|
||||||
} |
|
||||||
|
|
||||||
type EthTxReceived struct { |
|
||||||
TxHash string `json:"tx_hash"` |
|
||||||
RemoteId string `json:"remote_id"` |
|
||||||
LogEvent |
|
||||||
} |
|
||||||
|
|
||||||
func (l *EthTxReceived) EventName() string { |
|
||||||
return "eth.tx.received" |
|
||||||
} |
|
||||||
|
|
||||||
//
|
|
||||||
//
|
|
||||||
// The types below are legacy and need to be converted to new format or deleted
|
|
||||||
//
|
|
||||||
//
|
|
||||||
|
|
||||||
// type P2PConnecting struct {
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// RemoteEndpoint string `json:"remote_endpoint"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PConnecting) EventName() string {
|
|
||||||
// return "p2p.connecting"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type P2PHandshaked struct {
|
|
||||||
// RemoteCapabilities []string `json:"remote_capabilities"`
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PHandshaked) EventName() string {
|
|
||||||
// return "p2p.handshaked"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type P2PDisconnecting struct {
|
|
||||||
// Reason string `json:"reason"`
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PDisconnecting) EventName() string {
|
|
||||||
// return "p2p.disconnecting"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type P2PDisconnectingBadHandshake struct {
|
|
||||||
// Reason string `json:"reason"`
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PDisconnectingBadHandshake) EventName() string {
|
|
||||||
// return "p2p.disconnecting.bad_handshake"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type P2PDisconnectingBadProtocol struct {
|
|
||||||
// Reason string `json:"reason"`
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PDisconnectingBadProtocol) EventName() string {
|
|
||||||
// return "p2p.disconnecting.bad_protocol"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type P2PDisconnectingReputation struct {
|
|
||||||
// Reason string `json:"reason"`
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PDisconnectingReputation) EventName() string {
|
|
||||||
// return "p2p.disconnecting.reputation"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type P2PDisconnectingDHT struct {
|
|
||||||
// Reason string `json:"reason"`
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PDisconnectingDHT) EventName() string {
|
|
||||||
// return "p2p.disconnecting.dht"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type P2PEthDisconnectingBadBlock struct {
|
|
||||||
// Reason string `json:"reason"`
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PEthDisconnectingBadBlock) EventName() string {
|
|
||||||
// return "p2p.eth.disconnecting.bad_block"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type P2PEthDisconnectingBadTx struct {
|
|
||||||
// Reason string `json:"reason"`
|
|
||||||
// RemoteId string `json:"remote_id"`
|
|
||||||
// NumConnections int `json:"num_connections"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *P2PEthDisconnectingBadTx) EventName() string {
|
|
||||||
// return "p2p.eth.disconnecting.bad_tx"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthNewBlockBroadcasted struct {
|
|
||||||
// BlockNumber int `json:"block_number"`
|
|
||||||
// HeadHash string `json:"head_hash"`
|
|
||||||
// BlockHash string `json:"block_hash"`
|
|
||||||
// BlockDifficulty int `json:"block_difficulty"`
|
|
||||||
// BlockPrevHash string `json:"block_prev_hash"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthNewBlockBroadcasted) EventName() string {
|
|
||||||
// return "eth.newblock.broadcasted"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthNewBlockIsKnown struct {
|
|
||||||
// BlockNumber int `json:"block_number"`
|
|
||||||
// HeadHash string `json:"head_hash"`
|
|
||||||
// BlockHash string `json:"block_hash"`
|
|
||||||
// BlockDifficulty int `json:"block_difficulty"`
|
|
||||||
// BlockPrevHash string `json:"block_prev_hash"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthNewBlockIsKnown) EventName() string {
|
|
||||||
// return "eth.newblock.is_known"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthNewBlockIsNew struct {
|
|
||||||
// BlockNumber int `json:"block_number"`
|
|
||||||
// HeadHash string `json:"head_hash"`
|
|
||||||
// BlockHash string `json:"block_hash"`
|
|
||||||
// BlockDifficulty int `json:"block_difficulty"`
|
|
||||||
// BlockPrevHash string `json:"block_prev_hash"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthNewBlockIsNew) EventName() string {
|
|
||||||
// return "eth.newblock.is_new"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthNewBlockMissingParent struct {
|
|
||||||
// BlockNumber int `json:"block_number"`
|
|
||||||
// HeadHash string `json:"head_hash"`
|
|
||||||
// BlockHash string `json:"block_hash"`
|
|
||||||
// BlockDifficulty int `json:"block_difficulty"`
|
|
||||||
// BlockPrevHash string `json:"block_prev_hash"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthNewBlockMissingParent) EventName() string {
|
|
||||||
// return "eth.newblock.missing_parent"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthNewBlockIsInvalid struct {
|
|
||||||
// BlockNumber int `json:"block_number"`
|
|
||||||
// HeadHash string `json:"head_hash"`
|
|
||||||
// BlockHash string `json:"block_hash"`
|
|
||||||
// BlockDifficulty int `json:"block_difficulty"`
|
|
||||||
// BlockPrevHash string `json:"block_prev_hash"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthNewBlockIsInvalid) EventName() string {
|
|
||||||
// return "eth.newblock.is_invalid"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthNewBlockChainIsOlder struct {
|
|
||||||
// BlockNumber int `json:"block_number"`
|
|
||||||
// HeadHash string `json:"head_hash"`
|
|
||||||
// BlockHash string `json:"block_hash"`
|
|
||||||
// BlockDifficulty int `json:"block_difficulty"`
|
|
||||||
// BlockPrevHash string `json:"block_prev_hash"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthNewBlockChainIsOlder) EventName() string {
|
|
||||||
// return "eth.newblock.chain.is_older"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthNewBlockChainIsCanonical struct {
|
|
||||||
// BlockNumber int `json:"block_number"`
|
|
||||||
// HeadHash string `json:"head_hash"`
|
|
||||||
// BlockHash string `json:"block_hash"`
|
|
||||||
// BlockDifficulty int `json:"block_difficulty"`
|
|
||||||
// BlockPrevHash string `json:"block_prev_hash"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthNewBlockChainIsCanonical) EventName() string {
|
|
||||||
// return "eth.newblock.chain.is_cannonical"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthNewBlockChainNotCanonical struct {
|
|
||||||
// BlockNumber int `json:"block_number"`
|
|
||||||
// HeadHash string `json:"head_hash"`
|
|
||||||
// BlockHash string `json:"block_hash"`
|
|
||||||
// BlockDifficulty int `json:"block_difficulty"`
|
|
||||||
// BlockPrevHash string `json:"block_prev_hash"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthNewBlockChainNotCanonical) EventName() string {
|
|
||||||
// return "eth.newblock.chain.not_cannonical"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthTxCreated struct {
|
|
||||||
// TxHash string `json:"tx_hash"`
|
|
||||||
// TxSender string `json:"tx_sender"`
|
|
||||||
// TxAddress string `json:"tx_address"`
|
|
||||||
// TxHexRLP string `json:"tx_hexrlp"`
|
|
||||||
// TxNonce int `json:"tx_nonce"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthTxCreated) EventName() string {
|
|
||||||
// return "eth.tx.created"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthTxBroadcasted struct {
|
|
||||||
// TxHash string `json:"tx_hash"`
|
|
||||||
// TxSender string `json:"tx_sender"`
|
|
||||||
// TxAddress string `json:"tx_address"`
|
|
||||||
// TxNonce int `json:"tx_nonce"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthTxBroadcasted) EventName() string {
|
|
||||||
// return "eth.tx.broadcasted"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthTxValidated struct {
|
|
||||||
// TxHash string `json:"tx_hash"`
|
|
||||||
// TxSender string `json:"tx_sender"`
|
|
||||||
// TxAddress string `json:"tx_address"`
|
|
||||||
// TxNonce int `json:"tx_nonce"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthTxValidated) EventName() string {
|
|
||||||
// return "eth.tx.validated"
|
|
||||||
// }
|
|
||||||
|
|
||||||
// type EthTxIsInvalid struct {
|
|
||||||
// TxHash string `json:"tx_hash"`
|
|
||||||
// TxSender string `json:"tx_sender"`
|
|
||||||
// TxAddress string `json:"tx_address"`
|
|
||||||
// Reason string `json:"reason"`
|
|
||||||
// TxNonce int `json:"tx_nonce"`
|
|
||||||
// LogEvent
|
|
||||||
// }
|
|
||||||
|
|
||||||
// func (l *EthTxIsInvalid) EventName() string {
|
|
||||||
// return "eth.tx.is_invalid"
|
|
||||||
// }
|
|
@ -1,176 +0,0 @@ |
|||||||
// Copyright 2014 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package dagger |
|
||||||
|
|
||||||
import ( |
|
||||||
"hash" |
|
||||||
"math/big" |
|
||||||
"math/rand" |
|
||||||
"time" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common" |
|
||||||
"github.com/ethereum/go-ethereum/crypto/sha3" |
|
||||||
"github.com/ethereum/go-ethereum/logger" |
|
||||||
) |
|
||||||
|
|
||||||
var powlogger = logger.NewLogger("POW") |
|
||||||
|
|
||||||
type Dagger struct { |
|
||||||
hash *big.Int |
|
||||||
xn *big.Int |
|
||||||
} |
|
||||||
|
|
||||||
var Found bool |
|
||||||
|
|
||||||
func (dag *Dagger) Find(obj *big.Int, resChan chan int64) { |
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
|
||||||
|
|
||||||
for i := 0; i < 1000; i++ { |
|
||||||
rnd := r.Int63() |
|
||||||
|
|
||||||
res := dag.Eval(big.NewInt(rnd)) |
|
||||||
powlogger.Infof("rnd %v\nres %v\nobj %v\n", rnd, res, obj) |
|
||||||
if res.Cmp(obj) < 0 { |
|
||||||
// Post back result on the channel
|
|
||||||
resChan <- rnd |
|
||||||
// Notify other threads we've found a valid nonce
|
|
||||||
Found = true |
|
||||||
} |
|
||||||
|
|
||||||
// Break out if found
|
|
||||||
if Found { |
|
||||||
break |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
resChan <- 0 |
|
||||||
} |
|
||||||
|
|
||||||
func (dag *Dagger) Search(hash, diff *big.Int) (uint64, []byte) { |
|
||||||
// TODO fix multi threading. Somehow it results in the wrong nonce
|
|
||||||
amountOfRoutines := 1 |
|
||||||
|
|
||||||
dag.hash = hash |
|
||||||
|
|
||||||
obj := common.BigPow(2, 256) |
|
||||||
obj = obj.Div(obj, diff) |
|
||||||
|
|
||||||
Found = false |
|
||||||
resChan := make(chan int64, 3) |
|
||||||
var res int64 |
|
||||||
|
|
||||||
for k := 0; k < amountOfRoutines; k++ { |
|
||||||
go dag.Find(obj, resChan) |
|
||||||
|
|
||||||
// Wait for each go routine to finish
|
|
||||||
} |
|
||||||
for k := 0; k < amountOfRoutines; k++ { |
|
||||||
// Get the result from the channel. 0 = quit
|
|
||||||
if r := <-resChan; r != 0 { |
|
||||||
res = r |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
return uint64(res), nil |
|
||||||
} |
|
||||||
|
|
||||||
func (dag *Dagger) Verify(hash, diff, nonce *big.Int) bool { |
|
||||||
dag.hash = hash |
|
||||||
|
|
||||||
obj := common.BigPow(2, 256) |
|
||||||
obj = obj.Div(obj, diff) |
|
||||||
|
|
||||||
return dag.Eval(nonce).Cmp(obj) < 0 |
|
||||||
} |
|
||||||
|
|
||||||
func DaggerVerify(hash, diff, nonce *big.Int) bool { |
|
||||||
dagger := &Dagger{} |
|
||||||
dagger.hash = hash |
|
||||||
|
|
||||||
obj := common.BigPow(2, 256) |
|
||||||
obj = obj.Div(obj, diff) |
|
||||||
|
|
||||||
return dagger.Eval(nonce).Cmp(obj) < 0 |
|
||||||
} |
|
||||||
|
|
||||||
func (dag *Dagger) Node(L uint64, i uint64) *big.Int { |
|
||||||
if L == i { |
|
||||||
return dag.hash |
|
||||||
} |
|
||||||
|
|
||||||
var m *big.Int |
|
||||||
if L == 9 { |
|
||||||
m = big.NewInt(16) |
|
||||||
} else { |
|
||||||
m = big.NewInt(3) |
|
||||||
} |
|
||||||
|
|
||||||
sha := sha3.NewKeccak256() |
|
||||||
sha.Reset() |
|
||||||
d := sha3.NewKeccak256() |
|
||||||
b := new(big.Int) |
|
||||||
ret := new(big.Int) |
|
||||||
|
|
||||||
for k := 0; k < int(m.Uint64()); k++ { |
|
||||||
d.Reset() |
|
||||||
d.Write(dag.hash.Bytes()) |
|
||||||
d.Write(dag.xn.Bytes()) |
|
||||||
d.Write(big.NewInt(int64(L)).Bytes()) |
|
||||||
d.Write(big.NewInt(int64(i)).Bytes()) |
|
||||||
d.Write(big.NewInt(int64(k)).Bytes()) |
|
||||||
|
|
||||||
b.SetBytes(Sum(d)) |
|
||||||
pk := b.Uint64() & ((1 << ((L - 1) * 3)) - 1) |
|
||||||
sha.Write(dag.Node(L-1, pk).Bytes()) |
|
||||||
} |
|
||||||
|
|
||||||
ret.SetBytes(Sum(sha)) |
|
||||||
|
|
||||||
return ret |
|
||||||
} |
|
||||||
|
|
||||||
func Sum(sha hash.Hash) []byte { |
|
||||||
//in := make([]byte, 32)
|
|
||||||
return sha.Sum(nil) |
|
||||||
} |
|
||||||
|
|
||||||
func (dag *Dagger) Eval(N *big.Int) *big.Int { |
|
||||||
pow := common.BigPow(2, 26) |
|
||||||
dag.xn = pow.Div(N, pow) |
|
||||||
|
|
||||||
sha := sha3.NewKeccak256() |
|
||||||
sha.Reset() |
|
||||||
ret := new(big.Int) |
|
||||||
|
|
||||||
for k := 0; k < 4; k++ { |
|
||||||
d := sha3.NewKeccak256() |
|
||||||
b := new(big.Int) |
|
||||||
|
|
||||||
d.Reset() |
|
||||||
d.Write(dag.hash.Bytes()) |
|
||||||
d.Write(dag.xn.Bytes()) |
|
||||||
d.Write(N.Bytes()) |
|
||||||
d.Write(big.NewInt(int64(k)).Bytes()) |
|
||||||
|
|
||||||
b.SetBytes(Sum(d)) |
|
||||||
pk := (b.Uint64() & 0x1ffffff) |
|
||||||
|
|
||||||
sha.Write(dag.Node(9, pk).Bytes()) |
|
||||||
} |
|
||||||
|
|
||||||
return ret.SetBytes(Sum(sha)) |
|
||||||
} |
|
@ -1,35 +0,0 @@ |
|||||||
// Copyright 2014 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package dagger |
|
||||||
|
|
||||||
import ( |
|
||||||
"math/big" |
|
||||||
"testing" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common" |
|
||||||
) |
|
||||||
|
|
||||||
func BenchmarkDaggerSearch(b *testing.B) { |
|
||||||
hash := big.NewInt(0) |
|
||||||
diff := common.BigPow(2, 36) |
|
||||||
o := big.NewInt(0) // nonce doesn't matter. We're only testing against speed, not validity
|
|
||||||
|
|
||||||
// Reset timer so the big generation isn't included in the benchmark
|
|
||||||
b.ResetTimer() |
|
||||||
// Validate
|
|
||||||
DaggerVerify(hash, diff, o) |
|
||||||
} |
|
@ -1,113 +0,0 @@ |
|||||||
// Copyright 2014 The go-ethereum Authors
|
|
||||||
// This file is part of the go-ethereum library.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
||||||
// it under the terms of the GNU Lesser General Public License as published by
|
|
||||||
// the Free Software Foundation, either version 3 of the License, or
|
|
||||||
// (at your option) any later version.
|
|
||||||
//
|
|
||||||
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
||||||
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
// GNU Lesser General Public License for more details.
|
|
||||||
//
|
|
||||||
// You should have received a copy of the GNU Lesser General Public License
|
|
||||||
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
package ezp |
|
||||||
|
|
||||||
import ( |
|
||||||
"encoding/binary" |
|
||||||
"math/big" |
|
||||||
"math/rand" |
|
||||||
"time" |
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/common" |
|
||||||
"github.com/ethereum/go-ethereum/crypto/sha3" |
|
||||||
"github.com/ethereum/go-ethereum/logger" |
|
||||||
"github.com/ethereum/go-ethereum/pow" |
|
||||||
) |
|
||||||
|
|
||||||
var powlogger = logger.NewLogger("POW") |
|
||||||
|
|
||||||
type EasyPow struct { |
|
||||||
hash *big.Int |
|
||||||
HashRate int64 |
|
||||||
turbo bool |
|
||||||
} |
|
||||||
|
|
||||||
func New() *EasyPow { |
|
||||||
return &EasyPow{turbo: false} |
|
||||||
} |
|
||||||
|
|
||||||
func (pow *EasyPow) GetHashrate() int64 { |
|
||||||
return pow.HashRate |
|
||||||
} |
|
||||||
|
|
||||||
func (pow *EasyPow) Turbo(on bool) { |
|
||||||
pow.turbo = on |
|
||||||
} |
|
||||||
|
|
||||||
func (pow *EasyPow) Search(block pow.Block, stop <-chan struct{}, index int) (uint64, []byte) { |
|
||||||
r := rand.New(rand.NewSource(time.Now().UnixNano())) |
|
||||||
hash := block.HashNoNonce() |
|
||||||
diff := block.Difficulty() |
|
||||||
//i := int64(0)
|
|
||||||
// TODO fix offset
|
|
||||||
i := rand.Int63() |
|
||||||
starti := i |
|
||||||
start := time.Now().UnixNano() |
|
||||||
|
|
||||||
defer func() { pow.HashRate = 0 }() |
|
||||||
|
|
||||||
// Make sure stop is empty
|
|
||||||
empty: |
|
||||||
for { |
|
||||||
select { |
|
||||||
case <-stop: |
|
||||||
default: |
|
||||||
break empty |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
for { |
|
||||||
select { |
|
||||||
case <-stop: |
|
||||||
return 0, nil |
|
||||||
default: |
|
||||||
i++ |
|
||||||
|
|
||||||
elapsed := time.Now().UnixNano() - start |
|
||||||
hashes := ((float64(1e9) / float64(elapsed)) * float64(i-starti)) / 1000 |
|
||||||
pow.HashRate = int64(hashes) |
|
||||||
|
|
||||||
sha := uint64(r.Int63()) |
|
||||||
if verify(hash, diff, sha) { |
|
||||||
return sha, nil |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
if !pow.turbo { |
|
||||||
time.Sleep(20 * time.Microsecond) |
|
||||||
} |
|
||||||
} |
|
||||||
} |
|
||||||
|
|
||||||
func (pow *EasyPow) Verify(block pow.Block) bool { |
|
||||||
return Verify(block) |
|
||||||
} |
|
||||||
|
|
||||||
func verify(hash common.Hash, diff *big.Int, nonce uint64) bool { |
|
||||||
sha := sha3.NewKeccak256() |
|
||||||
n := make([]byte, 8) |
|
||||||
binary.PutUvarint(n, nonce) |
|
||||||
sha.Write(n) |
|
||||||
sha.Write(hash[:]) |
|
||||||
verification := new(big.Int).Div(common.BigPow(2, 256), diff) |
|
||||||
res := common.BigD(sha.Sum(nil)) |
|
||||||
return res.Cmp(verification) <= 0 |
|
||||||
} |
|
||||||
|
|
||||||
func Verify(block pow.Block) bool { |
|
||||||
return verify(block.HashNoNonce(), block.Difficulty(), block.Nonce()) |
|
||||||
} |
|
Loading…
Reference in new issue