bad_block.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. package eth
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "fmt"
  21. "net/http"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/log"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. )
  28. const (
  29. // The Ethereum main network genesis block.
  30. defaultGenesisHash = "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
  31. badBlocksURL = "https://badblocks.ethdev.com"
  32. )
  33. var EnableBadBlockReporting = false
  34. func sendBadBlockReport(block *types.Block, err error) {
  35. if !EnableBadBlockReporting {
  36. return
  37. }
  38. var (
  39. blockRLP, _ = rlp.EncodeToBytes(block)
  40. params = map[string]interface{}{
  41. "block": common.Bytes2Hex(blockRLP),
  42. "blockHash": block.Hash().Hex(),
  43. "errortype": err.Error(),
  44. "client": "go",
  45. }
  46. )
  47. if !block.ReceivedAt.IsZero() {
  48. params["receivedAt"] = block.ReceivedAt.UTC().String()
  49. }
  50. if p, ok := block.ReceivedFrom.(*peer); ok {
  51. params["receivedFrom"] = map[string]interface{}{
  52. "enode": fmt.Sprintf("enode://%x@%v", p.ID(), p.RemoteAddr()),
  53. "name": p.Name(),
  54. "protocolVersion": p.version,
  55. }
  56. }
  57. jsonStr, _ := json.Marshal(map[string]interface{}{"method": "eth_badBlock", "id": "1", "jsonrpc": "2.0", "params": []interface{}{params}})
  58. client := http.Client{Timeout: 8 * time.Second}
  59. resp, err := client.Post(badBlocksURL, "application/json", bytes.NewReader(jsonStr))
  60. if err != nil {
  61. log.Debug("Failed to report bad block", "err", err)
  62. return
  63. }
  64. log.Debug("Bad block report posted", "status", resp.StatusCode)
  65. resp.Body.Close()
  66. }