observation.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. package processor
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "fmt"
  6. "strings"
  7. "time"
  8. "github.com/certusone/wormhole/bridge/pkg/terra"
  9. "github.com/ethereum/go-ethereum/common"
  10. "github.com/ethereum/go-ethereum/crypto"
  11. "go.uber.org/zap"
  12. "github.com/certusone/wormhole/bridge/pkg/devnet"
  13. gossipv1 "github.com/certusone/wormhole/bridge/pkg/proto/gossip/v1"
  14. "github.com/certusone/wormhole/bridge/pkg/vaa"
  15. )
  16. // handleObservation processes a remote VAA observation, verifies it, checks whether the VAA has met quorum,
  17. // and assembles and submits a valid VAA if possible.
  18. func (p *Processor) handleObservation(ctx context.Context, m *gossipv1.LockupObservation) {
  19. // SECURITY: at this point, observations received from the p2p network are fully untrusted (all fields!)
  20. //
  21. // Note that observations are never tied to the (verified) p2p identity key - the p2p network
  22. // identity is completely decoupled from the guardian identity, p2p is just transport.
  23. p.logger.Info("received lockup observation",
  24. zap.String("digest", hex.EncodeToString(m.Hash)),
  25. zap.String("signature", hex.EncodeToString(m.Signature)),
  26. zap.String("addr", hex.EncodeToString(m.Addr)))
  27. // Verify the Guardian's signature. This verifies that m.Signature matches m.Hash and recovers
  28. // the public key that was used to sign the payload.
  29. pk, err := crypto.Ecrecover(m.Hash, m.Signature)
  30. if err != nil {
  31. p.logger.Warn("failed to verify signature on lockup observation",
  32. zap.String("digest", hex.EncodeToString(m.Hash)),
  33. zap.String("signature", hex.EncodeToString(m.Signature)),
  34. zap.String("addr", hex.EncodeToString(m.Addr)),
  35. zap.Error(err))
  36. return
  37. }
  38. // Verify that m.Addr matches the public key that signed m.Hash.
  39. their_addr := common.BytesToAddress(m.Addr)
  40. signer_pk := common.BytesToAddress(crypto.Keccak256(pk[1:])[12:])
  41. if their_addr != signer_pk {
  42. p.logger.Info("invalid lockup observation - address does not match pubkey",
  43. zap.String("digest", hex.EncodeToString(m.Hash)),
  44. zap.String("signature", hex.EncodeToString(m.Signature)),
  45. zap.String("addr", hex.EncodeToString(m.Addr)),
  46. zap.String("pk", signer_pk.Hex()))
  47. return
  48. }
  49. // Verify that m.Addr is included in the current guardian set.
  50. _, ok := p.gs.KeyIndex(their_addr)
  51. if !ok {
  52. p.logger.Warn("received observation by unknown guardian - is our guardian set outdated?",
  53. zap.String("their_addr", their_addr.Hex()),
  54. zap.Any("current_set", p.gs.KeysAsHexStrings()),
  55. )
  56. return
  57. }
  58. // Hooray! Now, we have verified all fields on LockupObservation and know that it includes
  59. // a valid signature by an active guardian. We still don't fully trust them, as they may be
  60. // byzantine, but now we know who we're dealing with.
  61. // []byte isn't hashable in a map. Paying a small extra cost for encoding for easier debugging.
  62. hash := hex.EncodeToString(m.Hash)
  63. if p.state.vaaSignatures[hash] == nil {
  64. // We haven't yet seen this lockup ourselves, and therefore do not know what the VAA looks like.
  65. // However, we have established that a valid guardian has signed it, and therefore we can
  66. // already start aggregating signatures for it.
  67. //
  68. // TODO: a malicious guardian can DoS this by creating fake lockups
  69. p.state.vaaSignatures[hash] = &vaaState{
  70. firstObserved: time.Now(),
  71. signatures: map[common.Address][]byte{},
  72. }
  73. }
  74. p.state.vaaSignatures[hash].signatures[their_addr] = m.Signature
  75. // Aggregate all valid signatures into a list of vaa.Signature and construct signed VAA.
  76. agg := make([]bool, len(p.gs.Keys))
  77. var sigs []*vaa.Signature
  78. for i, a := range p.gs.Keys {
  79. s, ok := p.state.vaaSignatures[hash].signatures[a]
  80. if ok {
  81. var bs [65]byte
  82. if n := copy(bs[:], s); n != 65 {
  83. panic(fmt.Sprintf("invalid sig len: %d", n))
  84. }
  85. sigs = append(sigs, &vaa.Signature{
  86. Index: uint8(i),
  87. Signature: bs,
  88. })
  89. }
  90. agg[i] = ok
  91. }
  92. if p.state.vaaSignatures[hash].ourVAA != nil {
  93. // We have seen it on chain!
  94. // Deep copy the VAA and add signatures
  95. v := p.state.vaaSignatures[hash].ourVAA
  96. signed := &vaa.VAA{
  97. Version: v.Version,
  98. GuardianSetIndex: v.GuardianSetIndex,
  99. Signatures: sigs,
  100. Timestamp: v.Timestamp,
  101. Payload: v.Payload,
  102. }
  103. // 2/3+ majority required for VAA to be valid - wait until we have quorum to submit VAA.
  104. quorum := CalculateQuorum(len(p.gs.Keys))
  105. p.logger.Info("aggregation state for VAA",
  106. zap.String("digest", hash),
  107. zap.Any("set", p.gs.KeysAsHexStrings()),
  108. zap.Uint32("index", p.gs.Index),
  109. zap.Bools("aggregation", agg),
  110. zap.Int("required_sigs", quorum),
  111. zap.Int("have_sigs", len(sigs)),
  112. )
  113. if len(sigs) >= quorum && !p.state.vaaSignatures[hash].submitted {
  114. vaaBytes, err := signed.Marshal()
  115. if err != nil {
  116. panic(err)
  117. }
  118. // Submit every VAA to Solana for data availability.
  119. p.logger.Info("submitting signed VAA to Solana",
  120. zap.String("digest", hash),
  121. zap.Any("vaa", signed),
  122. zap.String("bytes", hex.EncodeToString(vaaBytes)))
  123. p.vaaC <- signed
  124. switch t := v.Payload.(type) {
  125. case *vaa.BodyTransfer:
  126. // Depending on the target chain, guardians submit VAAs directly to the chain.
  127. switch t.TargetChain {
  128. case vaa.ChainIDSolana:
  129. // No-op.
  130. case vaa.ChainIDEthereum:
  131. // Ethereum is special because it's expensive, and guardians cannot
  132. // be expected to pay the fees. We only submit to Ethereum in devnet mode.
  133. p.devnetVAASubmission(ctx, signed, hash)
  134. case vaa.ChainIDTerra:
  135. p.terraVAASubmission(ctx, signed, hash)
  136. default:
  137. p.logger.Error("unknown target chain ID",
  138. zap.String("digest", hash),
  139. zap.Any("vaa", signed),
  140. zap.String("bytes", hex.EncodeToString(vaaBytes)),
  141. zap.Stringer("target_chain", t.TargetChain))
  142. }
  143. case *vaa.BodyGuardianSetUpdate:
  144. // A guardian set update is broadcast to every chain that we talk to.
  145. p.devnetVAASubmission(ctx, signed, hash)
  146. p.terraVAASubmission(ctx, signed, hash)
  147. default:
  148. panic(fmt.Sprintf("unknown VAA payload type: %+v", v))
  149. }
  150. p.state.vaaSignatures[hash].submitted = true
  151. } else {
  152. p.logger.Info("quorum not met or already submitted, doing nothing",
  153. zap.String("digest", hash))
  154. }
  155. } else {
  156. p.logger.Info("we have not yet seen this VAA - temporarily storing signature",
  157. zap.String("digest", hash))
  158. }
  159. }
  160. // devnetVAASubmission submits VAA to a local Ethereum devnet. For production, the bridge won't
  161. // have an Ethereum account and the user retrieves the VAA and submits the transactions themselves.
  162. func (p *Processor) devnetVAASubmission(ctx context.Context, signed *vaa.VAA, hash string) {
  163. if p.devnetMode {
  164. timeout, cancel := context.WithTimeout(ctx, 15*time.Second)
  165. tx, err := devnet.SubmitVAA(timeout, p.devnetEthRPC, signed)
  166. cancel()
  167. if err != nil {
  168. if strings.Contains(err.Error(), "VAA was already executed") {
  169. p.logger.Info("lockup already submitted to Ethereum by another node, ignoring",
  170. zap.Error(err), zap.String("digest", hash))
  171. } else {
  172. p.logger.Error("failed to submit lockup to Ethereum",
  173. zap.Error(err), zap.String("digest", hash))
  174. }
  175. return
  176. }
  177. p.logger.Info("lockup submitted to Ethereum", zap.Any("tx", tx), zap.String("digest", hash))
  178. }
  179. }
  180. // Submit VAA to Terra.
  181. func (p *Processor) terraVAASubmission(ctx context.Context, signed *vaa.VAA, hash string) {
  182. // Terra support is not yet ready for production.
  183. // - https://github.com/certusone/wormhole/issues/83
  184. // - https://github.com/certusone/wormhole/issues/95
  185. // - https://github.com/certusone/wormhole/issues/97
  186. //
  187. // Roadmap: https://github.com/certusone/wormhole/milestone/4
  188. if !p.devnetMode || p.terraChaidID == "" {
  189. p.logger.Warn("ignoring terra VAA submission",
  190. zap.String("digest", hash))
  191. return
  192. }
  193. tx, err := terra.SubmitVAA(ctx, p.terraLCD, p.terraChaidID, p.terraContract, p.terraFeePayer, signed)
  194. if err != nil {
  195. if strings.Contains(err.Error(), "VaaAlreadyExecuted") {
  196. p.logger.Info("lockup already submitted to Terra by another node, ignoring",
  197. zap.Error(err), zap.String("digest", hash))
  198. } else {
  199. p.logger.Error("failed to submit lockup to Terra",
  200. zap.Error(err), zap.String("digest", hash))
  201. }
  202. return
  203. }
  204. p.logger.Info("lockup submitted to Terra", zap.Any("tx", tx), zap.String("digest", hash))
  205. }