bad_block.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of go-ethereum.
  3. //
  4. // go-ethereum 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. // go-ethereum 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 go-ethereum. If not, see <http://www.gnu.org/licenses/>.
  16. package core
  17. import (
  18. "bytes"
  19. "encoding/json"
  20. "io/ioutil"
  21. "net/http"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/core/types"
  24. "github.com/ethereum/go-ethereum/logger"
  25. "github.com/ethereum/go-ethereum/logger/glog"
  26. "github.com/ethereum/go-ethereum/rlp"
  27. )
  28. // DisabledBadBlockReporting can be set to prevent blocks being reported.
  29. var DisableBadBlockReporting = true
  30. // ReportBlock reports the block to the block reporting tool found at
  31. // badblocks.ethdev.com
  32. func ReportBlock(block *types.Block, err error) {
  33. if DisableBadBlockReporting {
  34. return
  35. }
  36. const url = "https://badblocks.ethdev.com"
  37. blockRlp, _ := rlp.EncodeToBytes(block)
  38. data := map[string]interface{}{
  39. "block": common.Bytes2Hex(blockRlp),
  40. "errortype": err.Error(),
  41. "hints": map[string]interface{}{
  42. "receipts": "NYI",
  43. "vmtrace": "NYI",
  44. },
  45. }
  46. jsonStr, _ := json.Marshal(map[string]interface{}{"method": "eth_badBlock", "params": []interface{}{data}, "id": "1", "jsonrpc": "2.0"})
  47. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  48. req.Header.Set("Content-Type", "application/json")
  49. client := &http.Client{}
  50. resp, err := client.Do(req)
  51. if err != nil {
  52. glog.V(logger.Error).Infoln("POST err:", err)
  53. return
  54. }
  55. defer resp.Body.Close()
  56. if glog.V(logger.Debug) {
  57. glog.Infoln("response Status:", resp.Status)
  58. glog.Infoln("response Headers:", resp.Header)
  59. body, _ := ioutil.ReadAll(resp.Body)
  60. glog.Infoln("response Body:", string(body))
  61. }
  62. }