github.com/murrekatt/go-ethereum@v1.5.8-0.20170123175102-fc52f2c007fb/eth/bad_block.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package eth
    18  
    19  import (
    20  	"bytes"
    21  	"encoding/json"
    22  	"fmt"
    23  	"net/http"
    24  	"time"
    25  
    26  	"github.com/ethereum/go-ethereum/common"
    27  	"github.com/ethereum/go-ethereum/core/types"
    28  	"github.com/ethereum/go-ethereum/logger"
    29  	"github.com/ethereum/go-ethereum/logger/glog"
    30  	"github.com/ethereum/go-ethereum/rlp"
    31  )
    32  
    33  const (
    34  	// The Ethereum main network genesis block.
    35  	defaultGenesisHash = "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
    36  	badBlocksURL       = "https://badblocks.ethdev.com"
    37  )
    38  
    39  var EnableBadBlockReporting = false
    40  
    41  func sendBadBlockReport(block *types.Block, err error) {
    42  	if !EnableBadBlockReporting {
    43  		return
    44  	}
    45  
    46  	var (
    47  		blockRLP, _ = rlp.EncodeToBytes(block)
    48  		params      = map[string]interface{}{
    49  			"block":     common.Bytes2Hex(blockRLP),
    50  			"blockHash": block.Hash().Hex(),
    51  			"errortype": err.Error(),
    52  			"client":    "go",
    53  		}
    54  	)
    55  	if !block.ReceivedAt.IsZero() {
    56  		params["receivedAt"] = block.ReceivedAt.UTC().String()
    57  	}
    58  	if p, ok := block.ReceivedFrom.(*peer); ok {
    59  		params["receivedFrom"] = map[string]interface{}{
    60  			"enode":           fmt.Sprintf("enode://%x@%v", p.ID(), p.RemoteAddr()),
    61  			"name":            p.Name(),
    62  			"protocolVersion": p.version,
    63  		}
    64  	}
    65  	jsonStr, _ := json.Marshal(map[string]interface{}{"method": "eth_badBlock", "id": "1", "jsonrpc": "2.0", "params": []interface{}{params}})
    66  	client := http.Client{Timeout: 8 * time.Second}
    67  	resp, err := client.Post(badBlocksURL, "application/json", bytes.NewReader(jsonStr))
    68  	if err != nil {
    69  		glog.V(logger.Debug).Infoln(err)
    70  		return
    71  	}
    72  	glog.V(logger.Debug).Infof("Bad Block Report posted (%d)", resp.StatusCode)
    73  	resp.Body.Close()
    74  }