observation.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. package processor
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "fmt"
  6. bridge_common "github.com/certusone/wormhole/node/pkg/common"
  7. "github.com/certusone/wormhole/node/pkg/reporter"
  8. "github.com/prometheus/client_golang/prometheus"
  9. "github.com/prometheus/client_golang/prometheus/promauto"
  10. "time"
  11. "github.com/ethereum/go-ethereum/common"
  12. "github.com/ethereum/go-ethereum/crypto"
  13. "go.uber.org/zap"
  14. gossipv1 "github.com/certusone/wormhole/node/pkg/proto/gossip/v1"
  15. "github.com/certusone/wormhole/node/pkg/vaa"
  16. )
  17. var (
  18. observationsReceivedTotal = promauto.NewCounter(
  19. prometheus.CounterOpts{
  20. Name: "wormhole_observations_received_total",
  21. Help: "Total number of raw VAA observations received from gossip",
  22. })
  23. observationsReceivedByGuardianAddressTotal = promauto.NewCounterVec(
  24. prometheus.CounterOpts{
  25. Name: "wormhole_observations_signed_by_guardian_total",
  26. Help: "Total number of signed and verified VAA observations grouped by guardian address",
  27. }, []string{"addr"})
  28. observationsFailedTotal = promauto.NewCounterVec(
  29. prometheus.CounterOpts{
  30. Name: "wormhole_observations_verification_failures_total",
  31. Help: "Total number of observations verification failure, grouped by failure reason",
  32. }, []string{"cause"})
  33. observationsUnknownTotal = promauto.NewCounter(
  34. prometheus.CounterOpts{
  35. Name: "wormhole_observations_unknown_total",
  36. Help: "Total number of verified observations we haven't seen ourselves",
  37. })
  38. observationsDirectSubmissionsTotal = promauto.NewCounterVec(
  39. prometheus.CounterOpts{
  40. Name: "wormhole_observations_direct_submissions_queued_total",
  41. Help: "Total number of observations for a specific target chain that were queued for direct submission",
  42. }, []string{"target_chain"})
  43. observationsDirectSubmissionSuccessTotal = promauto.NewCounterVec(
  44. prometheus.CounterOpts{
  45. Name: "wormhole_observations_direct_submission_success_total",
  46. Help: "Total number of observations for a specific target chain that succeeded",
  47. }, []string{"target_chain"})
  48. )
  49. // handleObservation processes a remote VAA observation, verifies it, checks whether the VAA has met quorum,
  50. // and assembles and submits a valid VAA if possible.
  51. func (p *Processor) handleObservation(ctx context.Context, m *gossipv1.SignedObservation) {
  52. // SECURITY: at this point, observations received from the p2p network are fully untrusted (all fields!)
  53. //
  54. // Note that observations are never tied to the (verified) p2p identity key - the p2p network
  55. // identity is completely decoupled from the guardian identity, p2p is just transport.
  56. hash := hex.EncodeToString(m.Hash)
  57. p.logger.Info("received observation",
  58. zap.String("digest", hash),
  59. zap.String("signature", hex.EncodeToString(m.Signature)),
  60. zap.String("addr", hex.EncodeToString(m.Addr)))
  61. observationsReceivedTotal.Inc()
  62. // Verify the Guardian's signature. This verifies that m.Signature matches m.Hash and recovers
  63. // the public key that was used to sign the payload.
  64. pk, err := crypto.Ecrecover(m.Hash, m.Signature)
  65. if err != nil {
  66. p.logger.Warn("failed to verify signature on observation",
  67. zap.String("digest", hash),
  68. zap.String("signature", hex.EncodeToString(m.Signature)),
  69. zap.String("addr", hex.EncodeToString(m.Addr)),
  70. zap.Error(err))
  71. observationsFailedTotal.WithLabelValues("invalid_signature").Inc()
  72. return
  73. }
  74. // Verify that m.Addr matches the public key that signed m.Hash.
  75. their_addr := common.BytesToAddress(m.Addr)
  76. signer_pk := common.BytesToAddress(crypto.Keccak256(pk[1:])[12:])
  77. if their_addr != signer_pk {
  78. p.logger.Info("invalid observation - address does not match pubkey",
  79. zap.String("digest", hash),
  80. zap.String("signature", hex.EncodeToString(m.Signature)),
  81. zap.String("addr", hex.EncodeToString(m.Addr)),
  82. zap.String("pk", signer_pk.Hex()))
  83. observationsFailedTotal.WithLabelValues("pubkey_mismatch").Inc()
  84. return
  85. }
  86. // Determine which guardian set to use. The following cases are possible:
  87. //
  88. // - We have already seen the message and generated ourVAA. In this case, use the guardian set valid at the time,
  89. // even if the guardian set was updated. Old guardian sets remain valid for longer than aggregation state,
  90. // and the guardians in the old set stay online and observe and sign messages for the transition period.
  91. //
  92. // - We have not yet seen the message. In this case, we assume the latest guardian set because that's what
  93. // we will store once we do see the message.
  94. //
  95. // This ensures that during a guardian set update, a node which observed a given message with either the old
  96. // or the new guardian set can achieve consensus, since both the old and the new set would achieve consensus,
  97. // assuming that 2/3+ of the old and the new guardian set have seen the message and will periodically attempt
  98. // to retransmit their observations such that nodes who initially dropped the signature will get a 2nd chance.
  99. //
  100. // During an update, vaaState.signatures can contain signatures from *both* guardian sets.
  101. //
  102. var gs *bridge_common.GuardianSet
  103. if p.state.vaaSignatures[hash] != nil && p.state.vaaSignatures[hash].gs != nil {
  104. gs = p.state.vaaSignatures[hash].gs
  105. } else {
  106. gs = p.gs
  107. }
  108. // We haven't yet observed the trusted guardian set on Ethereum, and therefore, it's impossible to verify it.
  109. // May as well not have received it/been offline - drop it and wait for the guardian set.
  110. if gs == nil {
  111. p.logger.Warn("dropping observations since we haven't initialized our guardian set yet",
  112. zap.String("digest", hash),
  113. zap.String("their_addr", their_addr.Hex()),
  114. )
  115. observationsFailedTotal.WithLabelValues("uninitialized_guardian_set").Inc()
  116. return
  117. }
  118. // Verify that m.Addr is included in the guardian set. If it's not, drop the message. In case it's us
  119. // who have the outdated guardian set, we'll just wait for the message to be retransmitted eventually.
  120. _, ok := gs.KeyIndex(their_addr)
  121. if !ok {
  122. p.logger.Warn("received observation by unknown guardian - is our guardian set outdated?",
  123. zap.String("digest", hash),
  124. zap.String("their_addr", their_addr.Hex()),
  125. zap.Uint32("index", gs.Index),
  126. zap.Any("keys", gs.KeysAsHexStrings()),
  127. )
  128. observationsFailedTotal.WithLabelValues("unknown_guardian").Inc()
  129. return
  130. }
  131. // Hooray! Now, we have verified all fields on SignedObservation and know that it includes
  132. // a valid signature by an active guardian. We still don't fully trust them, as they may be
  133. // byzantine, but now we know who we're dealing with.
  134. // We can now count events by guardian without worry about cardinality explosions:
  135. observationsReceivedByGuardianAddressTotal.WithLabelValues(their_addr.Hex()).Inc()
  136. // []byte isn't hashable in a map. Paying a small extra cost for encoding for easier debugging.
  137. if p.state.vaaSignatures[hash] == nil {
  138. // We haven't yet seen this event ourselves, and therefore do not know what the VAA looks like.
  139. // However, we have established that a valid guardian has signed it, and therefore we can
  140. // already start aggregating signatures for it.
  141. //
  142. // A malicious guardian can potentially DoS this by creating fake observations at a faster rate than they decay,
  143. // leading to a slow out-of-memory crash. We do not attempt to automatically mitigate spam attacks with valid
  144. // signatures - such byzantine behavior would be plainly visible and would be dealt with by kicking them.
  145. observationsUnknownTotal.Inc()
  146. p.state.vaaSignatures[hash] = &vaaState{
  147. firstObserved: time.Now(),
  148. signatures: map[common.Address][]byte{},
  149. source: "unknown",
  150. }
  151. }
  152. p.state.vaaSignatures[hash].signatures[their_addr] = m.Signature
  153. // Aggregate all valid signatures into a list of vaa.Signature and construct signed VAA.
  154. agg := make([]bool, len(gs.Keys))
  155. var sigs []*vaa.Signature
  156. for i, a := range gs.Keys {
  157. s, ok := p.state.vaaSignatures[hash].signatures[a]
  158. if ok {
  159. var bs [65]byte
  160. if n := copy(bs[:], s); n != 65 {
  161. panic(fmt.Sprintf("invalid sig len: %d", n))
  162. }
  163. sigs = append(sigs, &vaa.Signature{
  164. Index: uint8(i),
  165. Signature: bs,
  166. })
  167. }
  168. agg[i] = ok
  169. }
  170. if p.state.vaaSignatures[hash].ourVAA != nil {
  171. // We have seen it on chain!
  172. // Deep copy the VAA and add signatures
  173. v := p.state.vaaSignatures[hash].ourVAA
  174. signed := &vaa.VAA{
  175. Version: v.Version,
  176. GuardianSetIndex: v.GuardianSetIndex,
  177. Signatures: sigs,
  178. Timestamp: v.Timestamp,
  179. Nonce: v.Nonce,
  180. Sequence: v.Sequence,
  181. EmitterChain: v.EmitterChain,
  182. EmitterAddress: v.EmitterAddress,
  183. Payload: v.Payload,
  184. ConsistencyLevel: v.ConsistencyLevel,
  185. }
  186. // report the individual signature
  187. signatureReport := &reporter.VerifiedPeerSignature{
  188. GuardianAddress: their_addr,
  189. Signature: m.Signature,
  190. EmitterChain: v.EmitterChain,
  191. EmitterAddress: v.EmitterAddress,
  192. Sequence: v.Sequence,
  193. }
  194. p.attestationEvents.ReportVerifiedPeerSignature(signatureReport)
  195. // report the current VAAState
  196. p.attestationEvents.ReportVAAStateUpdate(signed)
  197. // 2/3+ majority required for VAA to be valid - wait until we have quorum to submit VAA.
  198. quorum := CalculateQuorum(len(gs.Keys))
  199. p.logger.Info("aggregation state for VAA",
  200. zap.String("digest", hash),
  201. zap.Any("set", gs.KeysAsHexStrings()),
  202. zap.Uint32("index", gs.Index),
  203. zap.Bools("aggregation", agg),
  204. zap.Int("required_sigs", quorum),
  205. zap.Int("have_sigs", len(sigs)),
  206. )
  207. if len(sigs) >= quorum && !p.state.vaaSignatures[hash].submitted {
  208. vaaBytes, err := signed.Marshal()
  209. if err != nil {
  210. panic(err)
  211. }
  212. // Store signed VAA in database.
  213. p.logger.Info("signed VAA with quorum",
  214. zap.String("digest", hash),
  215. zap.Any("vaa", signed),
  216. zap.String("bytes", hex.EncodeToString(vaaBytes)),
  217. zap.String("message_id", signed.MessageID()))
  218. if err := p.db.StoreSignedVAA(signed); err != nil {
  219. p.logger.Error("failed to store signed VAA", zap.Error(err))
  220. }
  221. p.attestationEvents.ReportVAAQuorum(signed)
  222. p.state.vaaSignatures[hash].submitted = true
  223. } else {
  224. p.logger.Info("quorum not met or already submitted, doing nothing",
  225. zap.String("digest", hash))
  226. }
  227. } else {
  228. p.logger.Info("we have not yet seen this VAA - temporarily storing signature",
  229. zap.String("digest", hash),
  230. zap.Bools("aggregation", agg))
  231. }
  232. }