diff options
author | Jeffrey Wilcke <jeffrey@ethereum.org> | 2016-05-26 04:40:28 +0800 |
---|---|---|
committer | Jeffrey Wilcke <jeffrey@ethereum.org> | 2016-05-26 04:40:28 +0800 |
commit | a7434fd0085f55235acea5348db0c9247e9aac10 (patch) | |
tree | 28c458737e903a3f3b0e8e9e1c90e12e207218a7 /eth | |
parent | d9bb8179d39d356b38fd512f4c946f9ca1d1b6e2 (diff) | |
parent | ca18202eb9a94de1d4b51c1572fa74edfa2773bf (diff) | |
download | dexon-a7434fd0085f55235acea5348db0c9247e9aac10.tar.gz dexon-a7434fd0085f55235acea5348db0c9247e9aac10.tar.zst dexon-a7434fd0085f55235acea5348db0c9247e9aac10.zip |
Merge pull request #2614 from fjl/bad-block-report
eth: enable bad block reports
Diffstat (limited to 'eth')
-rw-r--r-- | eth/bad_block.go | 74 | ||||
-rw-r--r-- | eth/handler.go | 21 |
2 files changed, 93 insertions, 2 deletions
diff --git a/eth/bad_block.go b/eth/bad_block.go new file mode 100644 index 000000000..3a6c3d85c --- /dev/null +++ b/eth/bad_block.go @@ -0,0 +1,74 @@ +// 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 eth + +import ( + "bytes" + "encoding/json" + "fmt" + "net/http" + "time" + + "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/core/types" + "github.com/ethereum/go-ethereum/logger" + "github.com/ethereum/go-ethereum/logger/glog" + "github.com/ethereum/go-ethereum/rlp" +) + +const ( + // The Ethereum main network genesis block. + defaultGenesisHash = "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3" + badBlocksURL = "https://badblocks.ethdev.com" +) + +var EnableBadBlockReporting = false + +func sendBadBlockReport(block *types.Block, err error) { + if !EnableBadBlockReporting { + return + } + + var ( + blockRLP, _ = rlp.EncodeToBytes(block) + params = map[string]interface{}{ + "block": common.Bytes2Hex(blockRLP), + "blockHash": block.Hash().Hex(), + "errortype": err.Error(), + "client": "go", + } + ) + if !block.ReceivedAt.IsZero() { + params["receivedAt"] = block.ReceivedAt.UTC().String() + } + if p, ok := block.ReceivedFrom.(*peer); ok { + params["receivedFrom"] = map[string]interface{}{ + "enode": fmt.Sprintf("enode://%x@%v", p.ID(), p.RemoteAddr()), + "name": p.Name(), + "protocolVersion": p.version, + } + } + jsonStr, _ := json.Marshal(map[string]interface{}{"method": "eth_badBlock", "id": "1", "jsonrpc": "2.0", "params": []interface{}{params}}) + client := http.Client{Timeout: 8 * time.Second} + resp, err := client.Post(badBlocksURL, "application/json", bytes.NewReader(jsonStr)) + if err != nil { + glog.V(logger.Debug).Infoln(err) + return + } + glog.V(logger.Debug).Infof("Bad Block Report posted (%d)", resp.StatusCode) + resp.Body.Close() +} diff --git a/eth/handler.go b/eth/handler.go index 202acdc78..58869a2ee 100644 --- a/eth/handler.go +++ b/eth/handler.go @@ -83,6 +83,8 @@ type ProtocolManager struct { // wait group is used for graceful shutdowns during downloading // and processing wg sync.WaitGroup + + badBlockReportingEnabled bool } // NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable @@ -150,7 +152,7 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, // Construct the different synchronisation mechanisms manager.downloader = downloader.New(chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeader, blockchain.GetBlock, blockchain.CurrentHeader, blockchain.CurrentBlock, blockchain.CurrentFastBlock, blockchain.FastSyncCommitHead, - blockchain.GetTd, blockchain.InsertHeaderChain, blockchain.InsertChain, blockchain.InsertReceiptChain, blockchain.Rollback, + blockchain.GetTd, blockchain.InsertHeaderChain, manager.insertChain, blockchain.InsertReceiptChain, blockchain.Rollback, manager.removePeer) validator := func(block *types.Block, parent *types.Block) error { @@ -159,11 +161,24 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, heighter := func() uint64 { return blockchain.CurrentBlock().NumberU64() } - manager.fetcher = fetcher.New(blockchain.GetBlock, validator, manager.BroadcastBlock, heighter, blockchain.InsertChain, manager.removePeer) + manager.fetcher = fetcher.New(blockchain.GetBlock, validator, manager.BroadcastBlock, heighter, manager.insertChain, manager.removePeer) + + if blockchain.Genesis().Hash().Hex() == defaultGenesisHash && networkId == 1 { + glog.V(logger.Debug).Infoln("Bad Block Reporting is enabled") + manager.badBlockReportingEnabled = true + } return manager, nil } +func (pm *ProtocolManager) insertChain(blocks types.Blocks) (i int, err error) { + i, err = pm.blockchain.InsertChain(blocks) + if pm.badBlockReportingEnabled && core.IsValidationErr(err) && i < len(blocks) { + go sendBadBlockReport(blocks[i], err) + } + return i, err +} + func (pm *ProtocolManager) removePeer(id string) { // Short circuit if the peer was already removed peer := pm.peers.Peer(id) @@ -378,6 +393,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { // Update the receive timestamp of each block for _, block := range blocks { block.ReceivedAt = msg.ReceivedAt + block.ReceivedFrom = p } // Filter out any explicitly requested blocks, deliver the rest to the downloader if blocks := pm.fetcher.FilterBlocks(blocks); len(blocks) > 0 { @@ -664,6 +680,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { return errResp(ErrDecode, "block validation %v: %v", msg, err) } request.Block.ReceivedAt = msg.ReceivedAt + request.Block.ReceivedFrom = p // Mark the peer as owning the block and schedule it for import p.MarkBlock(request.Block.Hash()) |