Commit a7434fd0 authored by Jeffrey Wilcke's avatar Jeffrey Wilcke

Merge pull request #2614 from fjl/bad-block-report

eth: enable bad block reports
parents d9bb8179 ca18202e
...@@ -244,6 +244,12 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso ...@@ -244,6 +244,12 @@ JavaScript API. See https://github.com/ethereum/go-ethereum/wiki/Javascipt-Conso
// Start system runtime metrics collection // Start system runtime metrics collection
go metrics.CollectProcessMetrics(3 * time.Second) go metrics.CollectProcessMetrics(3 * time.Second)
// This should be the only place where reporting is enabled
// because it is not intended to run while testing.
// In addition to this check, bad block reports are sent only
// for chains with the main network genesis block and network id 1.
eth.EnableBadBlockReporting = true
utils.SetupNetwork(ctx) utils.SetupNetwork(ctx)
// Deprecation warning. // Deprecation warning.
......
...@@ -1117,15 +1117,12 @@ func (self *BlockChain) update() { ...@@ -1117,15 +1117,12 @@ func (self *BlockChain) update() {
} }
} }
// reportBlock reports the given block and error using the canonical block // reportBlock logs a bad block error.
// reporting tool. Reporting the block to the service is handled in a separate
// goroutine.
func reportBlock(block *types.Block, err error) { func reportBlock(block *types.Block, err error) {
if glog.V(logger.Error) { if glog.V(logger.Error) {
glog.Errorf("Bad block #%v (%s)\n", block.Number(), block.Hash().Hex()) glog.Errorf("Bad block #%v (%s)\n", block.Number(), block.Hash().Hex())
glog.Errorf(" %v", err) glog.Errorf(" %v", err)
} }
go ReportBlock(block, err)
} }
// InsertHeaderChain attempts to insert the given header chain in to the local // InsertHeaderChain attempts to insert the given header chain in to the local
......
...@@ -141,8 +141,10 @@ type Block struct { ...@@ -141,8 +141,10 @@ type Block struct {
// of the chain up to and including the block. // of the chain up to and including the block.
td *big.Int td *big.Int
// ReceivedAt is used by package eth to track block propagation time. // These fields are used by package eth to track
ReceivedAt time.Time // inter-peer block relay.
ReceivedAt time.Time
ReceivedFrom interface{}
} }
// DeprecatedTd is an old relic for extracting the TD of a block. It is in the // DeprecatedTd is an old relic for extracting the TD of a block. It is in the
......
...@@ -14,13 +14,14 @@ ...@@ -14,13 +14,14 @@
// You should have received a copy of the GNU Lesser General Public License // 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/>. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package core package eth
import ( import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"io/ioutil" "fmt"
"net/http" "net/http"
"time"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
...@@ -29,44 +30,45 @@ import ( ...@@ -29,44 +30,45 @@ import (
"github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rlp"
) )
// DisabledBadBlockReporting can be set to prevent blocks being reported. const (
var DisableBadBlockReporting = true // The Ethereum main network genesis block.
defaultGenesisHash = "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
badBlocksURL = "https://badblocks.ethdev.com"
)
var EnableBadBlockReporting = false
// ReportBlock reports the block to the block reporting tool found at func sendBadBlockReport(block *types.Block, err error) {
// badblocks.ethdev.com if !EnableBadBlockReporting {
func ReportBlock(block *types.Block, err error) {
if DisableBadBlockReporting {
return return
} }
const url = "https://badblocks.ethdev.com" var (
blockRLP, _ = rlp.EncodeToBytes(block)
blockRlp, _ := rlp.EncodeToBytes(block) params = map[string]interface{}{
data := map[string]interface{}{ "block": common.Bytes2Hex(blockRLP),
"block": common.Bytes2Hex(blockRlp), "blockHash": block.Hash().Hex(),
"errortype": err.Error(), "errortype": err.Error(),
"hints": map[string]interface{}{ "client": "go",
"receipts": "NYI", }
"vmtrace": "NYI", )
}, if !block.ReceivedAt.IsZero() {
params["receivedAt"] = block.ReceivedAt.UTC().String()
} }
jsonStr, _ := json.Marshal(map[string]interface{}{"method": "eth_badBlock", "params": []interface{}{data}, "id": "1", "jsonrpc": "2.0"}) if p, ok := block.ReceivedFrom.(*peer); ok {
params["receivedFrom"] = map[string]interface{}{
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) "enode": fmt.Sprintf("enode://%x@%v", p.ID(), p.RemoteAddr()),
req.Header.Set("Content-Type", "application/json") "name": p.Name(),
"protocolVersion": p.version,
client := &http.Client{} }
resp, err := client.Do(req) }
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 { if err != nil {
glog.V(logger.Error).Infoln("POST err:", err) glog.V(logger.Debug).Infoln(err)
return return
} }
defer resp.Body.Close() glog.V(logger.Debug).Infof("Bad Block Report posted (%d)", resp.StatusCode)
resp.Body.Close()
if glog.V(logger.Debug) {
glog.Infoln("response Status:", resp.Status)
glog.Infoln("response Headers:", resp.Header)
body, _ := ioutil.ReadAll(resp.Body)
glog.Infoln("response Body:", string(body))
}
} }
...@@ -83,6 +83,8 @@ type ProtocolManager struct { ...@@ -83,6 +83,8 @@ type ProtocolManager struct {
// wait group is used for graceful shutdowns during downloading // wait group is used for graceful shutdowns during downloading
// and processing // and processing
wg sync.WaitGroup wg sync.WaitGroup
badBlockReportingEnabled bool
} }
// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable // 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, ...@@ -150,7 +152,7 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int,
// Construct the different synchronisation mechanisms // Construct the different synchronisation mechanisms
manager.downloader = downloader.New(chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeader, manager.downloader = downloader.New(chaindb, manager.eventMux, blockchain.HasHeader, blockchain.HasBlockAndState, blockchain.GetHeader,
blockchain.GetBlock, blockchain.CurrentHeader, blockchain.CurrentBlock, blockchain.CurrentFastBlock, blockchain.FastSyncCommitHead, 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) manager.removePeer)
validator := func(block *types.Block, parent *types.Block) error { validator := func(block *types.Block, parent *types.Block) error {
...@@ -159,11 +161,24 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int, ...@@ -159,11 +161,24 @@ func NewProtocolManager(config *core.ChainConfig, fastSync bool, networkId int,
heighter := func() uint64 { heighter := func() uint64 {
return blockchain.CurrentBlock().NumberU64() 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 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) { func (pm *ProtocolManager) removePeer(id string) {
// Short circuit if the peer was already removed // Short circuit if the peer was already removed
peer := pm.peers.Peer(id) peer := pm.peers.Peer(id)
...@@ -378,6 +393,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { ...@@ -378,6 +393,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
// Update the receive timestamp of each block // Update the receive timestamp of each block
for _, block := range blocks { for _, block := range blocks {
block.ReceivedAt = msg.ReceivedAt block.ReceivedAt = msg.ReceivedAt
block.ReceivedFrom = p
} }
// Filter out any explicitly requested blocks, deliver the rest to the downloader // Filter out any explicitly requested blocks, deliver the rest to the downloader
if blocks := pm.fetcher.FilterBlocks(blocks); len(blocks) > 0 { if blocks := pm.fetcher.FilterBlocks(blocks); len(blocks) > 0 {
...@@ -664,6 +680,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error { ...@@ -664,6 +680,7 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
return errResp(ErrDecode, "block validation %v: %v", msg, err) return errResp(ErrDecode, "block validation %v: %v", msg, err)
} }
request.Block.ReceivedAt = msg.ReceivedAt request.Block.ReceivedAt = msg.ReceivedAt
request.Block.ReceivedFrom = p
// Mark the peer as owning the block and schedule it for import // Mark the peer as owning the block and schedule it for import
p.MarkBlock(request.Block.Hash()) p.MarkBlock(request.Block.Hash())
......
...@@ -25,8 +25,6 @@ import ( ...@@ -25,8 +25,6 @@ import (
"net/http" "net/http"
"os" "os"
"path/filepath" "path/filepath"
"github.com/ethereum/go-ethereum/core"
) )
var ( var (
...@@ -59,11 +57,6 @@ var ( ...@@ -59,11 +57,6 @@ var (
VmSkipTests = []string{} VmSkipTests = []string{}
) )
// Disable reporting bad blocks for the tests
func init() {
core.DisableBadBlockReporting = true
}
func readJson(reader io.Reader, value interface{}) error { func readJson(reader io.Reader, value interface{}) error {
data, err := ioutil.ReadAll(reader) data, err := ioutil.ReadAll(reader)
if err != nil { if err != nil {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment