message.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. package processor
  2. import (
  3. "context"
  4. "encoding/hex"
  5. "github.com/prometheus/client_golang/prometheus"
  6. "github.com/ethereum/go-ethereum/crypto"
  7. "go.uber.org/zap"
  8. "github.com/certusone/wormhole/bridge/pkg/common"
  9. "github.com/certusone/wormhole/bridge/pkg/supervisor"
  10. "github.com/certusone/wormhole/bridge/pkg/vaa"
  11. )
  12. var (
  13. // SECURITY: source_chain/target_chain are untrusted uint8 values. An attacker could cause a maximum of 255**2 label
  14. // pairs to be created, which is acceptable.
  15. lockupsObservedTotal = prometheus.NewCounterVec(
  16. prometheus.CounterOpts{
  17. Name: "wormhole_lockups_observed_total",
  18. Help: "Total number of lockups received on-chain",
  19. },
  20. []string{"source_chain", "target_chain"})
  21. lockupsSignedTotal = prometheus.NewCounterVec(
  22. prometheus.CounterOpts{
  23. Name: "wormhole_lockups_signed_total",
  24. Help: "Total number of lockups that were successfully signed",
  25. },
  26. []string{"source_chain", "target_chain"})
  27. )
  28. func init() {
  29. prometheus.MustRegister(lockupsObservedTotal)
  30. prometheus.MustRegister(lockupsSignedTotal)
  31. }
  32. // handleLockup processes a lockup received from a chain and instantiates our deterministic copy of the VAA. A lockup
  33. // event may be received multiple times until it has been successfully completed.
  34. func (p *Processor) handleLockup(ctx context.Context, k *common.MessagePublication) {
  35. supervisor.Logger(ctx).Info("message publication confirmed",
  36. zap.Stringer("emitter_chain", k.EmitterChain),
  37. zap.Stringer("emitter_address", k.EmitterAddress),
  38. zap.Uint32("nonce", k.Nonce),
  39. zap.Stringer("txhash", k.TxHash),
  40. zap.Time("timestamp", k.Timestamp),
  41. )
  42. lockupsObservedTotal.With(prometheus.Labels{
  43. "emitter_chain": k.EmitterChain.String(),
  44. }).Add(1)
  45. // All nodes will create the exact same VAA and sign its digest.
  46. // Consensus is established on this digest.
  47. v := &vaa.VAA{
  48. Version: vaa.SupportedVAAVersion,
  49. GuardianSetIndex: p.gs.Index,
  50. Signatures: nil,
  51. Timestamp: k.Timestamp,
  52. Nonce: k.Nonce,
  53. EmitterChain: k.EmitterChain,
  54. EmitterAddress: k.EmitterAddress,
  55. Payload: k.Payload,
  56. }
  57. // Generate digest of the unsigned VAA.
  58. digest, err := v.SigningMsg()
  59. if err != nil {
  60. panic(err)
  61. }
  62. // Sign the digest using our node's guardian key.
  63. s, err := crypto.Sign(digest.Bytes(), p.gk)
  64. if err != nil {
  65. panic(err)
  66. }
  67. p.logger.Info("observed and signed confirmed message publication",
  68. zap.Stringer("source_chain", k.EmitterChain),
  69. zap.Stringer("txhash", k.TxHash),
  70. zap.String("digest", hex.EncodeToString(digest.Bytes())),
  71. zap.String("signature", hex.EncodeToString(s)))
  72. lockupsSignedTotal.With(prometheus.Labels{
  73. "emitter_chain": k.EmitterChain.String()}).Add(1)
  74. p.broadcastSignature(v, s)
  75. }