Merge branch 'ethersphere-jsonlogs' into poc-9

This commit is contained in:
obscuren 2015-03-03 20:31:12 +01:00
commit cfe3af099c
6 changed files with 64 additions and 29 deletions

View File

@ -14,7 +14,10 @@ import (
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
) )
var chainlogger = logger.NewLogger("CHAIN") var (
chainlogger = logger.NewLogger("CHAIN")
jsonlogger = logger.NewJsonLogger()
)
type ChainEvent struct { type ChainEvent struct {
Block *types.Block Block *types.Block
@ -124,7 +127,7 @@ func (self *ChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlo
self.mu.RLock() self.mu.RLock()
defer self.mu.RUnlock() defer self.mu.RUnlock()
return self.td, self.currentBlock.Hash(), self.Genesis().Hash() return self.td, self.currentBlock.Hash(), self.genesisBlock.Hash()
} }
func (self *ChainManager) SetProcessor(proc types.BlockProcessor) { func (self *ChainManager) SetProcessor(proc types.BlockProcessor) {
@ -397,11 +400,11 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
var canonical, split bool var canonical, split bool
self.mu.Lock() self.mu.Lock()
cblock := self.currentBlock
{ {
// Write block to database. Eventually we'll have to improve on this and throw away blocks that are // Write block to database. Eventually we'll have to improve on this and throw away blocks that are
// not in the canonical chain. // not in the canonical chain.
self.write(block) self.write(block)
cblock := self.currentBlock
// Compare the TD of the last known block in the canonical chain to make sure it's greater. // Compare the TD of the last known block in the canonical chain to make sure it's greater.
// At this point it's possible that a different chain (fork) becomes the new canonical chain. // At this point it's possible that a different chain (fork) becomes the new canonical chain.
if td.Cmp(self.td) > 0 { if td.Cmp(self.td) > 0 {
@ -419,6 +422,12 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error {
self.mu.Unlock() self.mu.Unlock()
if canonical { if canonical {
jsonlogger.LogJson(&logger.EthChainNewHead{
BlockHash: ethutil.Bytes2Hex(block.Hash()),
BlockNumber: block.Number(),
ChainHeadHash: ethutil.Bytes2Hex(cblock.Hash()),
BlockPrevHash: ethutil.Bytes2Hex(block.ParentHash()),
})
self.setTransState(state.New(block.Root(), self.db)) self.setTransState(state.New(block.Root(), self.db))
self.eventMux.Post(ChainEvent{block, td}) self.eventMux.Post(ChainEvent{block, td})
} }

View File

@ -14,7 +14,7 @@ import (
"github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethdb"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/event"
ethlogger "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/miner" "github.com/ethereum/go-ethereum/miner"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
@ -25,8 +25,8 @@ import (
) )
var ( var (
logger = ethlogger.NewLogger("SERV") ethlogger = logger.NewLogger("SERV")
jsonlogger = ethlogger.NewJsonLogger() jsonlogger = logger.NewJsonLogger()
defaultBootNodes = []*discover.Node{ defaultBootNodes = []*discover.Node{
// ETH/DEV cmd/bootnode // ETH/DEV cmd/bootnode
@ -76,7 +76,7 @@ func (cfg *Config) parseBootNodes() []*discover.Node {
} }
n, err := discover.ParseNode(url) n, err := discover.ParseNode(url)
if err != nil { if err != nil {
logger.Errorf("Bootstrap URL %s: %v\n", url, err) ethlogger.Errorf("Bootstrap URL %s: %v\n", url, err)
continue continue
} }
ns = append(ns, n) ns = append(ns, n)
@ -100,7 +100,7 @@ func (cfg *Config) nodeKey() (*ecdsa.PrivateKey, error) {
return nil, fmt.Errorf("could not generate server key: %v", err) return nil, fmt.Errorf("could not generate server key: %v", err)
} }
if err := ioutil.WriteFile(keyfile, crypto.FromECDSA(key), 0600); err != nil { if err := ioutil.WriteFile(keyfile, crypto.FromECDSA(key), 0600); err != nil {
logger.Errorln("could not persist nodekey: ", err) ethlogger.Errorln("could not persist nodekey: ", err)
} }
return key, nil return key, nil
} }
@ -132,14 +132,14 @@ type Ethereum struct {
WsServer rpc.RpcServer WsServer rpc.RpcServer
keyManager *crypto.KeyManager keyManager *crypto.KeyManager
logger ethlogger.LogSystem logger logger.LogSystem
Mining bool Mining bool
} }
func New(config *Config) (*Ethereum, error) { func New(config *Config) (*Ethereum, error) {
// Boostrap database // Boostrap database
logger := ethlogger.New(config.DataDir, config.LogFile, config.LogLevel, config.LogFormat) ethlogger := logger.New(config.DataDir, config.LogFile, config.LogLevel, config.LogFormat)
db, err := ethdb.NewLDBDatabase("blockchain") db, err := ethdb.NewLDBDatabase("blockchain")
if err != nil { if err != nil {
return nil, err return nil, err
@ -176,7 +176,7 @@ func New(config *Config) (*Ethereum, error) {
keyManager: keyManager, keyManager: keyManager,
blacklist: p2p.NewBlacklist(), blacklist: p2p.NewBlacklist(),
eventMux: &event.TypeMux{}, eventMux: &event.TypeMux{},
logger: logger, logger: ethlogger,
} }
eth.chainManager = core.NewChainManager(db, eth.EventMux()) eth.chainManager = core.NewChainManager(db, eth.EventMux())
@ -220,7 +220,7 @@ func New(config *Config) (*Ethereum, error) {
} }
func (s *Ethereum) KeyManager() *crypto.KeyManager { return s.keyManager } func (s *Ethereum) KeyManager() *crypto.KeyManager { return s.keyManager }
func (s *Ethereum) Logger() ethlogger.LogSystem { return s.logger } func (s *Ethereum) Logger() logger.LogSystem { return s.logger }
func (s *Ethereum) Name() string { return s.net.Name } func (s *Ethereum) Name() string { return s.net.Name }
func (s *Ethereum) ChainManager() *core.ChainManager { return s.chainManager } func (s *Ethereum) ChainManager() *core.ChainManager { return s.chainManager }
func (s *Ethereum) BlockProcessor() *core.BlockProcessor { return s.blockProcessor } func (s *Ethereum) BlockProcessor() *core.BlockProcessor { return s.blockProcessor }
@ -238,7 +238,7 @@ func (s *Ethereum) Coinbase() []byte { return nil } // TODO
// Start the ethereum // Start the ethereum
func (s *Ethereum) Start() error { func (s *Ethereum) Start() error {
jsonlogger.LogJson(&ethlogger.LogStarting{ jsonlogger.LogJson(&logger.LogStarting{
ClientString: s.net.Name, ClientString: s.net.Name,
ProtocolVersion: ProtocolVersion, ProtocolVersion: ProtocolVersion,
}) })
@ -264,7 +264,7 @@ func (s *Ethereum) Start() error {
s.blockSub = s.eventMux.Subscribe(core.NewMinedBlockEvent{}) s.blockSub = s.eventMux.Subscribe(core.NewMinedBlockEvent{})
go s.blockBroadcastLoop() go s.blockBroadcastLoop()
logger.Infoln("Server started") ethlogger.Infoln("Server started")
return nil return nil
} }
@ -299,7 +299,7 @@ func (s *Ethereum) Stop() {
s.whisper.Stop() s.whisper.Stop()
} }
logger.Infoln("Server stopped") ethlogger.Infoln("Server stopped")
close(s.shutdownChan) close(s.shutdownChan)
} }

View File

@ -9,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/errs" "github.com/ethereum/go-ethereum/errs"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
"github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
@ -165,6 +166,12 @@ func (self *ethProtocol) handle() error {
if err := msg.Decode(&txs); err != nil { if err := msg.Decode(&txs); err != nil {
return self.protoError(ErrDecode, "msg %v: %v", msg, err) return self.protoError(ErrDecode, "msg %v: %v", msg, err)
} }
for _, tx := range txs {
jsonlogger.LogJson(&logger.EthTxReceived{
TxHash: ethutil.Bytes2Hex(tx.Hash()),
RemoteId: self.peer.ID().String(),
})
}
self.txPool.AddTransactions(txs) self.txPool.AddTransactions(txs)
case GetBlockHashesMsg: case GetBlockHashesMsg:
@ -243,6 +250,15 @@ func (self *ethProtocol) handle() error {
return self.protoError(ErrDecode, "msg %v: %v", msg, err) return self.protoError(ErrDecode, "msg %v: %v", msg, err)
} }
hash := request.Block.Hash() hash := request.Block.Hash()
_, chainHead, _ := self.chainManager.Status()
jsonlogger.LogJson(&logger.EthChainReceivedNewBlock{
BlockHash: ethutil.Bytes2Hex(hash),
BlockNumber: request.Block.Number(), // this surely must be zero
ChainHeadHash: ethutil.Bytes2Hex(chainHead),
BlockPrevHash: ethutil.Bytes2Hex(request.Block.ParentHash()),
RemoteId: self.peer.ID().String(),
})
// to simplify backend interface adding a new block // to simplify backend interface adding a new block
// uses AddPeer followed by AddBlock only if peer is the best peer // uses AddPeer followed by AddBlock only if peer is the best peer
// (or selected as new best peer) // (or selected as new best peer)

View File

@ -13,7 +13,6 @@ import (
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/errs" "github.com/ethereum/go-ethereum/errs"
"github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/ethutil"
ethlogger "github.com/ethereum/go-ethereum/logger"
"github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p"
"github.com/ethereum/go-ethereum/p2p/discover" "github.com/ethereum/go-ethereum/p2p/discover"
) )

View File

@ -1,6 +1,7 @@
package logger package logger
import ( import (
"math/big"
"time" "time"
) )
@ -54,7 +55,7 @@ func (l *P2PDisconnected) EventName() string {
type EthMinerNewBlock struct { type EthMinerNewBlock struct {
BlockHash string `json:"block_hash"` BlockHash string `json:"block_hash"`
BlockNumber int `json:"block_number"` BlockNumber *big.Int `json:"block_number"`
ChainHeadHash string `json:"chain_head_hash"` ChainHeadHash string `json:"chain_head_hash"`
BlockPrevHash string `json:"block_prev_hash"` BlockPrevHash string `json:"block_prev_hash"`
LogEvent LogEvent
@ -66,10 +67,10 @@ func (l *EthMinerNewBlock) EventName() string {
type EthChainReceivedNewBlock struct { type EthChainReceivedNewBlock struct {
BlockHash string `json:"block_hash"` BlockHash string `json:"block_hash"`
BlockNumber int `json:"block_number"` BlockNumber *big.Int `json:"block_number"`
ChainHeadHash string `json:"chain_head_hash"` ChainHeadHash string `json:"chain_head_hash"`
BlockPrevHash string `json:"block_prev_hash"` BlockPrevHash string `json:"block_prev_hash"`
RemoteId int `json:"remote_id"` RemoteId string `json:"remote_id"`
LogEvent LogEvent
} }
@ -79,7 +80,7 @@ func (l *EthChainReceivedNewBlock) EventName() string {
type EthChainNewHead struct { type EthChainNewHead struct {
BlockHash string `json:"block_hash"` BlockHash string `json:"block_hash"`
BlockNumber int `json:"block_number"` BlockNumber *big.Int `json:"block_number"`
ChainHeadHash string `json:"chain_head_hash"` ChainHeadHash string `json:"chain_head_hash"`
BlockPrevHash string `json:"block_prev_hash"` BlockPrevHash string `json:"block_prev_hash"`
LogEvent LogEvent

View File

@ -11,11 +11,14 @@ import (
"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/logger"
"github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/state" "github.com/ethereum/go-ethereum/state"
"gopkg.in/fatih/set.v0" "gopkg.in/fatih/set.v0"
) )
var jsonlogger = logger.NewJsonLogger()
type environment struct { type environment struct {
totalUsedGas *big.Int totalUsedGas *big.Int
state *state.StateDB state *state.StateDB
@ -152,6 +155,13 @@ func (self *worker) wait() {
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
jsonlogger.LogJson(&logger.EthMinerNewBlock{
BlockHash: ethutil.Bytes2Hex(block.Hash()),
BlockNumber: block.Number(),
ChainHeadHash: ethutil.Bytes2Hex(block.ParentHeaderHash),
BlockPrevHash: ethutil.Bytes2Hex(block.ParentHeaderHash),
})
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})
fmt.Println("GOOD BLOCK", self.current.block) fmt.Println("GOOD BLOCK", self.current.block)