bad_block.go 2.4 KB

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