From 60a2704b049a2bc8de417c8f50155ec69b071a9e Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Sun, 1 Mar 2015 16:09:59 +0100 Subject: [PATCH 1/7] Implement eth.miner.new_block event --- logger/types.go | 9 +++++---- miner/worker.go | 10 +++++++++- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/logger/types.go b/logger/types.go index 7ab4a2b8c..86408620e 100644 --- a/logger/types.go +++ b/logger/types.go @@ -1,6 +1,7 @@ package logger import ( + "math/big" "time" ) @@ -53,10 +54,10 @@ func (l *P2PDisconnected) EventName() string { } type EthMinerNewBlock struct { - BlockHash string `json:"block_hash"` - BlockNumber int `json:"block_number"` - ChainHeadHash string `json:"chain_head_hash"` - BlockPrevHash string `json:"block_prev_hash"` + BlockHash string `json:"block_hash"` + BlockNumber *big.Int `json:"block_number"` + ChainHeadHash string `json:"chain_head_hash"` + BlockPrevHash string `json:"block_prev_hash"` LogEvent } diff --git a/miner/worker.go b/miner/worker.go index 4f0909302..6f43a9c39 100644 --- a/miner/worker.go +++ b/miner/worker.go @@ -10,11 +10,14 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethutil" "github.com/ethereum/go-ethereum/event" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/pow" "github.com/ethereum/go-ethereum/state" "gopkg.in/fatih/set.v0" ) +var jsonlogger = logger.NewJsonLogger() + type environment struct { totalUsedGas *big.Int state *state.StateDB @@ -141,7 +144,12 @@ func (self *worker) wait() { block := self.current.block if block.Number().Uint64() == work.Number && block.Nonce() == nil { self.current.block.Header().Nonce = work.Nonce - + 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 { self.mux.Post(core.NewMinedBlockEvent{self.current.block}) } else { From 0976c3024f271e89d1d4de32dfb518c02e691643 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Mon, 2 Mar 2015 08:15:28 -0600 Subject: [PATCH 2/7] Don't import logger as ethlogger --- eth/backend.go | 24 ++++++++++++------------ eth/block_pool.go | 4 ++-- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/eth/backend.go b/eth/backend.go index f67f9c78b..8d5da01fd 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -12,7 +12,7 @@ import ( "github.com/ethereum/go-ethereum/ethdb" "github.com/ethereum/go-ethereum/ethutil" "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/p2p" "github.com/ethereum/go-ethereum/p2p/discover" @@ -23,8 +23,8 @@ import ( ) var ( - logger = ethlogger.NewLogger("SERV") - jsonlogger = ethlogger.NewJsonLogger() + ethlogger = logger.NewLogger("SERV") + jsonlogger = logger.NewJsonLogger() defaultBootNodes = []*discover.Node{ // ETH/DEV cmd/bootnode @@ -74,7 +74,7 @@ func (cfg *Config) parseBootNodes() []*discover.Node { } n, err := discover.ParseNode(url) if err != nil { - logger.Errorf("Bootstrap URL %s: %v\n", url, err) + ethlogger.Errorf("Bootstrap URL %s: %v\n", url, err) continue } ns = append(ns, n) @@ -98,7 +98,7 @@ func (cfg *Config) nodeKey() (*ecdsa.PrivateKey, error) { return nil, fmt.Errorf("could not generate server key: %v", err) } 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 } @@ -130,14 +130,14 @@ type Ethereum struct { WsServer rpc.RpcServer keyManager *crypto.KeyManager - logger ethlogger.LogSystem + logger logger.LogSystem Mining bool } func New(config *Config) (*Ethereum, error) { // 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") if err != nil { return nil, err @@ -174,7 +174,7 @@ func New(config *Config) (*Ethereum, error) { keyManager: keyManager, blacklist: p2p.NewBlacklist(), eventMux: &event.TypeMux{}, - logger: logger, + logger: ethlogger, } eth.chainManager = core.NewChainManager(db, eth.EventMux()) @@ -216,7 +216,7 @@ func New(config *Config) (*Ethereum, error) { } 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) ChainManager() *core.ChainManager { return s.chainManager } func (s *Ethereum) BlockProcessor() *core.BlockProcessor { return s.blockProcessor } @@ -234,7 +234,7 @@ func (s *Ethereum) Coinbase() []byte { return nil } // TODO // Start the ethereum func (s *Ethereum) Start() error { - jsonlogger.LogJson(ðlogger.LogStarting{ + jsonlogger.LogJson(&logger.LogStarting{ ClientString: s.net.Name, ProtocolVersion: ProtocolVersion, }) @@ -260,7 +260,7 @@ func (s *Ethereum) Start() error { s.blockSub = s.eventMux.Subscribe(core.NewMinedBlockEvent{}) go s.blockBroadcastLoop() - logger.Infoln("Server started") + ethlogger.Infoln("Server started") return nil } @@ -295,7 +295,7 @@ func (s *Ethereum) Stop() { s.whisper.Stop() } - logger.Infoln("Server stopped") + ethlogger.Infoln("Server stopped") close(s.shutdownChan) } diff --git a/eth/block_pool.go b/eth/block_pool.go index 13016c694..124a9e8c0 100644 --- a/eth/block_pool.go +++ b/eth/block_pool.go @@ -12,11 +12,11 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethutil" - ethlogger "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/pow" ) -var poolLogger = ethlogger.NewLogger("Blockpool") +var poolLogger = logger.NewLogger("Blockpool") const ( blockHashesBatchSize = 256 From e31ec57f8875147766b2bf8e6f129b9a0c1b5e69 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Mon, 2 Mar 2015 08:17:09 -0600 Subject: [PATCH 3/7] Add event eth.tx.received --- eth/protocol.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/eth/protocol.go b/eth/protocol.go index 8221c1b29..a5cc8ee1a 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -8,6 +8,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/ethutil" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/rlp" ) @@ -137,6 +138,12 @@ func (self *ethProtocol) handle() error { if err := msg.Decode(&txs); err != nil { 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) case GetBlockHashesMsg: From a75af474f71606ed4572db216d9440b7c14a8a37 Mon Sep 17 00:00:00 2001 From: Taylor Gerring Date: Mon, 2 Mar 2015 08:27:26 -0600 Subject: [PATCH 4/7] Fix logger import in tests --- eth/block_pool_test.go | 6 +++--- eth/protocol_test.go | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/eth/block_pool_test.go b/eth/block_pool_test.go index 331dbe504..3d1b28315 100644 --- a/eth/block_pool_test.go +++ b/eth/block_pool_test.go @@ -12,19 +12,19 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" - ethlogger "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/pow" ) const waitTimeout = 60 // seconds -var logsys = ethlogger.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlogger.LogLevel(ethlogger.DebugDetailLevel)) +var logsys = logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.LogLevel(logger.DebugDetailLevel)) var ini = false func logInit() { if !ini { - ethlogger.AddLogSystem(logsys) + logger.AddLogSystem(logsys) ini = true } } diff --git a/eth/protocol_test.go b/eth/protocol_test.go index a91806a1c..87d8974d5 100644 --- a/eth/protocol_test.go +++ b/eth/protocol_test.go @@ -12,12 +12,12 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethutil" - ethlogger "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger" "github.com/ethereum/go-ethereum/p2p" "github.com/ethereum/go-ethereum/p2p/discover" ) -var sys = ethlogger.NewStdLogSystem(os.Stdout, log.LstdFlags, ethlogger.LogLevel(ethlogger.DebugDetailLevel)) +var sys = logger.NewStdLogSystem(os.Stdout, log.LstdFlags, logger.LogLevel(logger.DebugDetailLevel)) type testMsgReadWriter struct { in chan p2p.Msg From 76af536d0d16e3f0742a46d448b3e0dcb46458f6 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 3 Mar 2015 15:37:20 +0700 Subject: [PATCH 5/7] block number is *big.Int, remoteID is string --- logger/types.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/logger/types.go b/logger/types.go index 86408620e..d98f0874a 100644 --- a/logger/types.go +++ b/logger/types.go @@ -66,11 +66,11 @@ func (l *EthMinerNewBlock) EventName() string { } type EthChainReceivedNewBlock struct { - BlockHash string `json:"block_hash"` - BlockNumber int `json:"block_number"` - ChainHeadHash string `json:"chain_head_hash"` - BlockPrevHash string `json:"block_prev_hash"` - RemoteId int `json:"remote_id"` + 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 } @@ -79,10 +79,10 @@ func (l *EthChainReceivedNewBlock) EventName() string { } type EthChainNewHead struct { - BlockHash string `json:"block_hash"` - BlockNumber int `json:"block_number"` - ChainHeadHash string `json:"chain_head_hash"` - BlockPrevHash string `json:"block_prev_hash"` + BlockHash string `json:"block_hash"` + BlockNumber *big.Int `json:"block_number"` + ChainHeadHash string `json:"chain_head_hash"` + BlockPrevHash string `json:"block_prev_hash"` LogEvent } From fc47f0f27b4b672c84e8de230a5a3c5e5519f3aa Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 3 Mar 2015 15:43:05 +0700 Subject: [PATCH 6/7] add eth.chain.received.new_block log to eth protocol --- eth/protocol.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/eth/protocol.go b/eth/protocol.go index a5cc8ee1a..5a7af0e33 100644 --- a/eth/protocol.go +++ b/eth/protocol.go @@ -152,7 +152,6 @@ func (self *ethProtocol) handle() error { return self.protoError(ErrDecode, "->msg %v: %v", msg, err) } - //request.Amount = uint64(math.Min(float64(maxHashes), float64(request.Amount))) if request.Amount > maxHashes { request.Amount = maxHashes } @@ -224,6 +223,15 @@ func (self *ethProtocol) handle() error { return self.protoError(ErrDecode, "msg %v: %v", msg, err) } 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 // uses AddPeer followed by AddHashes, AddBlock only if peer is the best peer // (or selected as new best peer) From 6c2856df2335dec946a5ebc14a4438b261e0c881 Mon Sep 17 00:00:00 2001 From: zelig Date: Tue, 3 Mar 2015 15:44:41 +0700 Subject: [PATCH 7/7] add eth.chain.new_head log to core/chain_manager --- core/chain_manager.go | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/core/chain_manager.go b/core/chain_manager.go index 959bfd398..71d36d57d 100644 --- a/core/chain_manager.go +++ b/core/chain_manager.go @@ -14,7 +14,10 @@ import ( "github.com/ethereum/go-ethereum/state" ) -var chainlogger = logger.NewLogger("CHAIN") +var ( + chainlogger = logger.NewLogger("CHAIN") + jsonlogger = logger.NewJsonLogger() +) type ChainEvent struct { Block *types.Block @@ -122,7 +125,7 @@ func (self *ChainManager) Status() (td *big.Int, currentBlock []byte, genesisBlo self.mu.RLock() 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) { @@ -395,11 +398,11 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { var canonical, split bool self.mu.Lock() + cblock := self.currentBlock { // Write block to database. Eventually we'll have to improve on this and throw away blocks that are // not in the canonical chain. self.write(block) - cblock := self.currentBlock // 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. if td.Cmp(self.td) > 0 { @@ -417,6 +420,12 @@ func (self *ChainManager) InsertChain(chain types.Blocks) error { self.mu.Unlock() 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.eventMux.Post(ChainEvent{block, td}) }