p2p.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. package p2p
  2. import (
  3. "context"
  4. "crypto/ecdsa"
  5. "errors"
  6. "fmt"
  7. node_common "github.com/certusone/wormhole/node/pkg/common"
  8. "github.com/certusone/wormhole/node/pkg/vaa"
  9. "github.com/certusone/wormhole/node/pkg/version"
  10. "github.com/ethereum/go-ethereum/common"
  11. ethcrypto "github.com/ethereum/go-ethereum/crypto"
  12. "github.com/prometheus/client_golang/prometheus"
  13. "github.com/prometheus/client_golang/prometheus/promauto"
  14. "strings"
  15. "time"
  16. "github.com/libp2p/go-libp2p-core/peer"
  17. "github.com/multiformats/go-multiaddr"
  18. "github.com/libp2p/go-libp2p"
  19. connmgr "github.com/libp2p/go-libp2p-connmgr"
  20. "github.com/libp2p/go-libp2p-core/crypto"
  21. "github.com/libp2p/go-libp2p-core/host"
  22. "github.com/libp2p/go-libp2p-core/protocol"
  23. "github.com/libp2p/go-libp2p-core/routing"
  24. dht "github.com/libp2p/go-libp2p-kad-dht"
  25. pubsub "github.com/libp2p/go-libp2p-pubsub"
  26. libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
  27. libp2ptls "github.com/libp2p/go-libp2p-tls"
  28. "go.uber.org/zap"
  29. "google.golang.org/protobuf/proto"
  30. gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
  31. "github.com/certusone/wormhole/node/pkg/supervisor"
  32. )
  33. var (
  34. p2pHeartbeatsSent = promauto.NewCounter(
  35. prometheus.CounterOpts{
  36. Name: "wormhole_p2p_heartbeats_sent_total",
  37. Help: "Total number of p2p heartbeats sent",
  38. })
  39. p2pMessagesSent = promauto.NewCounter(
  40. prometheus.CounterOpts{
  41. Name: "wormhole_p2p_broadcast_messages_sent_total",
  42. Help: "Total number of p2p pubsub broadcast messages sent",
  43. })
  44. p2pMessagesReceived = promauto.NewCounterVec(
  45. prometheus.CounterOpts{
  46. Name: "wormhole_p2p_broadcast_messages_received_total",
  47. Help: "Total number of p2p pubsub broadcast messages received",
  48. }, []string{"type"})
  49. )
  50. var heartbeatMessagePrefix = []byte("heartbeat|")
  51. var signedObservationRequestPrefix = []byte("signed_observation_request|")
  52. func heartbeatDigest(b []byte) common.Hash {
  53. return ethcrypto.Keccak256Hash(append(heartbeatMessagePrefix, b...))
  54. }
  55. func signedObservationRequestDigest(b []byte) common.Hash {
  56. return ethcrypto.Keccak256Hash(append(signedObservationRequestPrefix, b...))
  57. }
  58. func Run(obsvC chan *gossipv1.SignedObservation, obsvReqC chan *gossipv1.ObservationRequest, obsvReqSendC chan *gossipv1.ObservationRequest, sendC chan []byte, signedInC chan *gossipv1.SignedVAAWithQuorum, priv crypto.PrivKey, gk *ecdsa.PrivateKey, gst *node_common.GuardianSetState, port uint, networkID string, bootstrapPeers string, nodeName string, disableHeartbeatVerify bool, rootCtxCancel context.CancelFunc) func(ctx context.Context) error {
  59. return func(ctx context.Context) (re error) {
  60. logger := supervisor.Logger(ctx)
  61. h, err := libp2p.New(ctx,
  62. // Use the keypair we generated
  63. libp2p.Identity(priv),
  64. // Multiple listen addresses
  65. libp2p.ListenAddrStrings(
  66. // Listen on QUIC only.
  67. // https://github.com/libp2p/go-libp2p/issues/688
  68. fmt.Sprintf("/ip4/0.0.0.0/udp/%d/quic", port),
  69. fmt.Sprintf("/ip6/::/udp/%d/quic", port),
  70. ),
  71. // Enable TLS security as the only security protocol.
  72. libp2p.Security(libp2ptls.ID, libp2ptls.New),
  73. // Enable QUIC transport as the only transport.
  74. libp2p.Transport(libp2pquic.NewTransport),
  75. // Let's prevent our peer from having too many
  76. // connections by attaching a connection manager.
  77. libp2p.ConnectionManager(connmgr.NewConnManager(
  78. 100, // Lowwater
  79. 400, // HighWater,
  80. time.Minute, // GracePeriod
  81. )),
  82. // Let this host use the DHT to find other hosts
  83. libp2p.Routing(func(h host.Host) (routing.PeerRouting, error) {
  84. // TODO(leo): Persistent data store (i.e. address book)
  85. idht, err := dht.New(ctx, h, dht.Mode(dht.ModeServer),
  86. // This intentionally makes us incompatible with the global IPFS DHT
  87. dht.ProtocolPrefix(protocol.ID("/"+networkID)),
  88. )
  89. return idht, err
  90. }),
  91. )
  92. if err != nil {
  93. panic(err)
  94. }
  95. defer func() {
  96. // TODO: libp2p cannot be cleanly restarted (https://github.com/libp2p/go-libp2p/issues/992)
  97. logger.Error("p2p routine has exited, cancelling root context...", zap.Error(re))
  98. rootCtxCancel()
  99. }()
  100. logger.Info("Connecting to bootstrap peers", zap.String("bootstrap_peers", bootstrapPeers))
  101. topic := fmt.Sprintf("%s/%s", networkID, "broadcast")
  102. logger.Info("Subscribing pubsub topic", zap.String("topic", topic))
  103. ps, err := pubsub.NewGossipSub(ctx, h)
  104. if err != nil {
  105. panic(err)
  106. }
  107. th, err := ps.Join(topic)
  108. if err != nil {
  109. return fmt.Errorf("failed to join topic: %w", err)
  110. }
  111. sub, err := th.Subscribe()
  112. if err != nil {
  113. return fmt.Errorf("failed to subscribe topic: %w", err)
  114. }
  115. // Add our own bootstrap nodes
  116. // Count number of successful connection attempts. If we fail to connect to any bootstrap peer, kill
  117. // the service and have supervisor retry it.
  118. successes := 0
  119. // Are we a bootstrap node? If so, it's okay to not have any peers.
  120. bootstrapNode := false
  121. for _, addr := range strings.Split(bootstrapPeers, ",") {
  122. if addr == "" {
  123. continue
  124. }
  125. ma, err := multiaddr.NewMultiaddr(addr)
  126. if err != nil {
  127. logger.Error("Invalid bootstrap address", zap.String("peer", addr), zap.Error(err))
  128. continue
  129. }
  130. pi, err := peer.AddrInfoFromP2pAddr(ma)
  131. if err != nil {
  132. logger.Error("Invalid bootstrap address", zap.String("peer", addr), zap.Error(err))
  133. continue
  134. }
  135. if pi.ID == h.ID() {
  136. logger.Info("We're a bootstrap node")
  137. bootstrapNode = true
  138. continue
  139. }
  140. if err = h.Connect(ctx, *pi); err != nil {
  141. logger.Error("Failed to connect to bootstrap peer", zap.String("peer", addr), zap.Error(err))
  142. } else {
  143. successes += 1
  144. }
  145. }
  146. // TODO: continually reconnect to bootstrap nodes?
  147. if successes == 0 && !bootstrapNode {
  148. return fmt.Errorf("failed to connect to any bootstrap peer")
  149. } else {
  150. logger.Info("Connected to bootstrap peers", zap.Int("num", successes))
  151. }
  152. logger.Info("Node has been started", zap.String("peer_id", h.ID().String()),
  153. zap.String("addrs", fmt.Sprintf("%v", h.Addrs())))
  154. bootTime := time.Now()
  155. // Periodically run guardian state set cleanup.
  156. go func() {
  157. ticker := time.NewTicker(15 * time.Second)
  158. defer ticker.Stop()
  159. for {
  160. select {
  161. case <-ticker.C:
  162. gst.Cleanup()
  163. case <-ctx.Done():
  164. return
  165. }
  166. }
  167. }()
  168. go func() {
  169. // Disable heartbeat when no node name is provided (spy mode)
  170. if nodeName == "" {
  171. return
  172. }
  173. ctr := int64(0)
  174. tick := time.NewTicker(15 * time.Second)
  175. defer tick.Stop()
  176. for {
  177. select {
  178. case <-ctx.Done():
  179. return
  180. case <-tick.C:
  181. DefaultRegistry.mu.Lock()
  182. networks := make([]*gossipv1.Heartbeat_Network, 0, len(DefaultRegistry.networkStats))
  183. for _, v := range DefaultRegistry.networkStats {
  184. errCtr := DefaultRegistry.GetErrorCount(vaa.ChainID(v.Id))
  185. v.ErrorCount = errCtr
  186. networks = append(networks, v)
  187. }
  188. heartbeat := &gossipv1.Heartbeat{
  189. NodeName: nodeName,
  190. Counter: ctr,
  191. Timestamp: time.Now().UnixNano(),
  192. Networks: networks,
  193. Version: version.Version(),
  194. GuardianAddr: DefaultRegistry.guardianAddress,
  195. BootTimestamp: bootTime.UnixNano(),
  196. }
  197. ourAddr := ethcrypto.PubkeyToAddress(gk.PublicKey)
  198. if err := gst.SetHeartbeat(ourAddr, h.ID(), heartbeat); err != nil {
  199. panic(err)
  200. }
  201. collectNodeMetrics(ourAddr, h.ID(), heartbeat)
  202. b, err := proto.Marshal(heartbeat)
  203. if err != nil {
  204. panic(err)
  205. }
  206. DefaultRegistry.mu.Unlock()
  207. // Sign the heartbeat using our node's guardian key.
  208. digest := heartbeatDigest(b)
  209. sig, err := ethcrypto.Sign(digest.Bytes(), gk)
  210. if err != nil {
  211. panic(err)
  212. }
  213. msg := gossipv1.GossipMessage{Message: &gossipv1.GossipMessage_SignedHeartbeat{
  214. SignedHeartbeat: &gossipv1.SignedHeartbeat{
  215. Heartbeat: b,
  216. Signature: sig,
  217. GuardianAddr: ourAddr.Bytes(),
  218. }}}
  219. b, err = proto.Marshal(&msg)
  220. if err != nil {
  221. panic(err)
  222. }
  223. err = th.Publish(ctx, b)
  224. if err != nil {
  225. logger.Warn("failed to publish heartbeat message", zap.Error(err))
  226. }
  227. p2pHeartbeatsSent.Inc()
  228. ctr += 1
  229. }
  230. }
  231. }()
  232. go func() {
  233. for {
  234. select {
  235. case <-ctx.Done():
  236. return
  237. case msg := <-sendC:
  238. err := th.Publish(ctx, msg)
  239. p2pMessagesSent.Inc()
  240. if err != nil {
  241. logger.Error("failed to publish message from queue", zap.Error(err))
  242. }
  243. case msg := <-obsvReqSendC:
  244. b, err := proto.Marshal(msg)
  245. if err != nil {
  246. panic(err)
  247. }
  248. // Sign the observation request using our node's guardian key.
  249. digest := signedObservationRequestDigest(b)
  250. sig, err := ethcrypto.Sign(digest.Bytes(), gk)
  251. if err != nil {
  252. panic(err)
  253. }
  254. sReq := &gossipv1.SignedObservationRequest{
  255. ObservationRequest: b,
  256. Signature: sig,
  257. GuardianAddr: ethcrypto.PubkeyToAddress(gk.PublicKey).Bytes(),
  258. }
  259. envelope := &gossipv1.GossipMessage{
  260. Message: &gossipv1.GossipMessage_SignedObservationRequest{
  261. SignedObservationRequest: sReq}}
  262. b, err = proto.Marshal(envelope)
  263. if err != nil {
  264. panic(err)
  265. }
  266. // Send to local observation request queue (the loopback message is ignored)
  267. obsvReqC <- msg
  268. err = th.Publish(ctx, b)
  269. p2pMessagesSent.Inc()
  270. if err != nil {
  271. logger.Error("failed to publish observation request", zap.Error(err))
  272. } else {
  273. logger.Info("published signed observation request", zap.Any("signed_observation_request", sReq))
  274. }
  275. }
  276. }
  277. }()
  278. for {
  279. envelope, err := sub.Next(ctx)
  280. if err != nil {
  281. return fmt.Errorf("failed to receive pubsub message: %w", err)
  282. }
  283. var msg gossipv1.GossipMessage
  284. err = proto.Unmarshal(envelope.Data, &msg)
  285. if err != nil {
  286. logger.Info("received invalid message",
  287. zap.Binary("data", envelope.Data),
  288. zap.String("from", envelope.GetFrom().String()))
  289. p2pMessagesReceived.WithLabelValues("invalid").Inc()
  290. continue
  291. }
  292. if envelope.GetFrom() == h.ID() {
  293. logger.Debug("received message from ourselves, ignoring",
  294. zap.Any("payload", msg.Message))
  295. p2pMessagesReceived.WithLabelValues("loopback").Inc()
  296. continue
  297. }
  298. logger.Debug("received message",
  299. zap.Any("payload", msg.Message),
  300. zap.Binary("raw", envelope.Data),
  301. zap.String("from", envelope.GetFrom().String()))
  302. switch m := msg.Message.(type) {
  303. case *gossipv1.GossipMessage_SignedHeartbeat:
  304. s := m.SignedHeartbeat
  305. gs := gst.Get()
  306. if gs == nil {
  307. // No valid guardian set yet - dropping heartbeat
  308. logger.Debug("skipping heartbeat - no guardian set",
  309. zap.Any("value", s),
  310. zap.String("from", envelope.GetFrom().String()))
  311. break
  312. }
  313. if heartbeat, err := processSignedHeartbeat(envelope.GetFrom(), s, gs, gst, disableHeartbeatVerify); err != nil {
  314. p2pMessagesReceived.WithLabelValues("invalid_heartbeat").Inc()
  315. logger.Debug("invalid signed heartbeat received",
  316. zap.Error(err),
  317. zap.Any("payload", msg.Message),
  318. zap.Any("value", s),
  319. zap.Binary("raw", envelope.Data),
  320. zap.String("from", envelope.GetFrom().String()))
  321. } else {
  322. p2pMessagesReceived.WithLabelValues("valid_heartbeat").Inc()
  323. logger.Debug("valid signed heartbeat received",
  324. zap.Any("value", heartbeat),
  325. zap.String("from", envelope.GetFrom().String()))
  326. }
  327. case *gossipv1.GossipMessage_SignedObservation:
  328. obsvC <- m.SignedObservation
  329. p2pMessagesReceived.WithLabelValues("observation").Inc()
  330. case *gossipv1.GossipMessage_SignedVaaWithQuorum:
  331. signedInC <- m.SignedVaaWithQuorum
  332. p2pMessagesReceived.WithLabelValues("signed_vaa_with_quorum").Inc()
  333. case *gossipv1.GossipMessage_SignedObservationRequest:
  334. s := m.SignedObservationRequest
  335. gs := gst.Get()
  336. if gs == nil {
  337. logger.Debug("dropping SignedObservationRequest - no guardian set",
  338. zap.Any("value", s),
  339. zap.String("from", envelope.GetFrom().String()))
  340. break
  341. }
  342. r, err := processSignedObservationRequest(s, gs)
  343. if err != nil {
  344. p2pMessagesReceived.WithLabelValues("invalid_signed_observation_request").Inc()
  345. logger.Debug("invalid signed observation request received",
  346. zap.Error(err),
  347. zap.Any("payload", msg.Message),
  348. zap.Any("value", s),
  349. zap.Binary("raw", envelope.Data),
  350. zap.String("from", envelope.GetFrom().String()))
  351. } else {
  352. p2pMessagesReceived.WithLabelValues("signed_observation_request").Inc()
  353. logger.Info("valid signed observation request received",
  354. zap.Any("value", r),
  355. zap.String("from", envelope.GetFrom().String()))
  356. obsvReqC <- r
  357. }
  358. default:
  359. p2pMessagesReceived.WithLabelValues("unknown").Inc()
  360. logger.Warn("received unknown message type (running outdated software?)",
  361. zap.Any("payload", msg.Message),
  362. zap.Binary("raw", envelope.Data),
  363. zap.String("from", envelope.GetFrom().String()))
  364. }
  365. }
  366. }
  367. }
  368. func processSignedHeartbeat(from peer.ID, s *gossipv1.SignedHeartbeat, gs *node_common.GuardianSet, gst *node_common.GuardianSetState, disableVerify bool) (*gossipv1.Heartbeat, error) {
  369. envelopeAddr := common.BytesToAddress(s.GuardianAddr)
  370. idx, ok := gs.KeyIndex(envelopeAddr)
  371. var pk common.Address
  372. if !ok {
  373. if !disableVerify {
  374. return nil, fmt.Errorf("invalid message: %s not in guardian set", envelopeAddr)
  375. }
  376. } else {
  377. pk = gs.Keys[idx]
  378. }
  379. digest := heartbeatDigest(s.Heartbeat)
  380. pubKey, err := ethcrypto.Ecrecover(digest.Bytes(), s.Signature)
  381. if err != nil {
  382. return nil, errors.New("failed to recover public key")
  383. }
  384. signerAddr := common.BytesToAddress(ethcrypto.Keccak256(pubKey[1:])[12:])
  385. if pk != signerAddr && !disableVerify {
  386. return nil, fmt.Errorf("invalid signer: %v", signerAddr)
  387. }
  388. var h gossipv1.Heartbeat
  389. err = proto.Unmarshal(s.Heartbeat, &h)
  390. if err != nil {
  391. return nil, fmt.Errorf("failed to unmarshal heartbeat: %w", err)
  392. }
  393. // Store verified heartbeat in global guardian set state.
  394. if err := gst.SetHeartbeat(signerAddr, from, &h); err != nil {
  395. return nil, fmt.Errorf("failed to store in guardian set state: %w", err)
  396. }
  397. collectNodeMetrics(signerAddr, from, &h)
  398. return &h, nil
  399. }
  400. func processSignedObservationRequest(s *gossipv1.SignedObservationRequest, gs *node_common.GuardianSet) (*gossipv1.ObservationRequest, error) {
  401. envelopeAddr := common.BytesToAddress(s.GuardianAddr)
  402. idx, ok := gs.KeyIndex(envelopeAddr)
  403. var pk common.Address
  404. if !ok {
  405. return nil, fmt.Errorf("invalid message: %s not in guardian set", envelopeAddr)
  406. } else {
  407. pk = gs.Keys[idx]
  408. }
  409. digest := signedObservationRequestDigest(s.ObservationRequest)
  410. pubKey, err := ethcrypto.Ecrecover(digest.Bytes(), s.Signature)
  411. if err != nil {
  412. return nil, errors.New("failed to recover public key")
  413. }
  414. signerAddr := common.BytesToAddress(ethcrypto.Keccak256(pubKey[1:])[12:])
  415. if pk != signerAddr {
  416. return nil, fmt.Errorf("invalid signer: %v", signerAddr)
  417. }
  418. var h gossipv1.ObservationRequest
  419. err = proto.Unmarshal(s.ObservationRequest, &h)
  420. if err != nil {
  421. return nil, fmt.Errorf("failed to unmarshal observation request: %w", err)
  422. }
  423. // For now, this supports Solana only. Once we add more chains, we'll have to add a
  424. // multiplexer/router in node.go.
  425. if h.ChainId != uint32(vaa.ChainIDSolana) {
  426. return nil, fmt.Errorf("unsupported chain id: %d", h.ChainId)
  427. }
  428. // TODO: implement per-guardian rate limiting
  429. return &h, nil
  430. }