bad_block.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package core
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/core/types"
  9. "github.com/ethereum/go-ethereum/logger"
  10. "github.com/ethereum/go-ethereum/logger/glog"
  11. "github.com/ethereum/go-ethereum/rlp"
  12. )
  13. // DisabledBadBlockReporting can be set to prevent blocks being reported.
  14. var DisableBadBlockReporting = true
  15. // ReportBlock reports the block to the block reporting tool found at
  16. // badblocks.ethdev.com
  17. func ReportBlock(block *types.Block, err error) {
  18. if DisableBadBlockReporting {
  19. return
  20. }
  21. const url = "https://badblocks.ethdev.com"
  22. blockRlp, _ := rlp.EncodeToBytes(block)
  23. data := map[string]interface{}{
  24. "block": common.Bytes2Hex(blockRlp),
  25. "errortype": err.Error(),
  26. "hints": map[string]interface{}{
  27. "receipts": "NYI",
  28. "vmtrace": "NYI",
  29. },
  30. }
  31. jsonStr, _ := json.Marshal(map[string]interface{}{"method": "eth_badBlock", "params": []interface{}{data}, "id": "1", "jsonrpc": "2.0"})
  32. req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  33. req.Header.Set("Content-Type", "application/json")
  34. client := &http.Client{}
  35. resp, err := client.Do(req)
  36. if err != nil {
  37. glog.V(logger.Error).Infoln("POST err:", err)
  38. return
  39. }
  40. defer resp.Body.Close()
  41. if glog.V(logger.Debug) {
  42. glog.Infoln("response Status:", resp.Status)
  43. glog.Infoln("response Headers:", resp.Header)
  44. body, _ := ioutil.ReadAll(resp.Body)
  45. glog.Infoln("response Body:", string(body))
  46. }
  47. }