quorum_protocol.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package eth
  2. import (
  3. "errors"
  4. "github.com/ethereum/go-ethereum/eth/protocols/eth"
  5. "github.com/ethereum/go-ethereum/p2p"
  6. "github.com/ethereum/go-ethereum/p2p/enode"
  7. )
  8. // Quorum: quorum_protocol enables the eth service to return two different protocols, one for the eth mainnet "eth" service,
  9. // and one for the quorum specific consensus algo, obtained from engine.consensus
  10. // 2021 Jan in the future consensus (istanbul) may run from its own service and use a single subprotocol there,
  11. // instead of overloading the eth service.
  12. var (
  13. // errEthPeerNil is returned when no eth peer is found to be associated with a p2p peer.
  14. errEthPeerNil = errors.New("eth peer was nil")
  15. errEthPeerNotRegistered = errors.New("eth peer was not registered")
  16. )
  17. // quorum consensus Protocol variables are optionally set in addition to the "eth" protocol variables (eth/protocol.go).
  18. var quorumConsensusProtocolName = ""
  19. // ProtocolVersions are the supported versions of the quorum consensus protocol (first is primary), e.g. []uint{Istanbul64, Istanbul99, Istanbul100}.
  20. var quorumConsensusProtocolVersions []uint
  21. // protocol Length describe the number of messages support by the protocol/version map[uint]uint64{Istanbul64: 18, Istanbul99: 18, Istanbul100: 18}
  22. var quorumConsensusProtocolLengths map[uint]uint64
  23. func (s *Ethereum) quorumConsensusProtocols(backend eth.Backend, network uint64, dnsdisc enode.Iterator) []p2p.Protocol {
  24. protos := make([]p2p.Protocol, len(quorumConsensusProtocolVersions))
  25. for i, vsn := range quorumConsensusProtocolVersions {
  26. // if we have a legacy protocol, e.g. istanbul/99, istanbul/64 then the protocol handler is will be the "eth"
  27. // protocol handler, and the subprotocol "eth" will not be used, but rather the legacy subprotocol will handle
  28. // both eth messages and consensus messages.
  29. if isLegacyProtocol(quorumConsensusProtocolName, vsn) {
  30. length, ok := quorumConsensusProtocolLengths[vsn]
  31. if !ok {
  32. panic("makeProtocol for unknown version")
  33. }
  34. lp := s.handler.makeLegacyProtocol(quorumConsensusProtocolName, vsn, length, backend, network, dnsdisc)
  35. protos[i] = lp
  36. } else {
  37. length, ok := quorumConsensusProtocolLengths[vsn]
  38. if !ok {
  39. panic("makeQuorumConsensusProtocol for unknown version")
  40. }
  41. protos[i] = s.handler.makeQuorumConsensusProtocol(quorumConsensusProtocolName, vsn, length, backend, network, dnsdisc)
  42. }
  43. }
  44. return protos
  45. }
  46. // istanbul/64, istanbul/99, clique/63, clique/64 all override the "eth" subprotocol.
  47. func isLegacyProtocol(name string, version uint) bool {
  48. // protocols that override "eth" subprotocol and run only the quorum subprotocol.
  49. quorumLegacyProtocols := map[string][]uint{"istanbul": {64, 99}, "clique": {63, 64}}
  50. for lpName, lpVersions := range quorumLegacyProtocols {
  51. if lpName == name {
  52. for _, v := range lpVersions {
  53. if v == version {
  54. return true
  55. }
  56. }
  57. }
  58. }
  59. return false
  60. }