payloads_test.go 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. package vaa
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "testing"
  6. "github.com/ethereum/go-ethereum/common"
  7. "github.com/stretchr/testify/assert"
  8. )
  9. var addr = Address{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4}
  10. var dummyBytes = [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32}
  11. func TestCoreModule(t *testing.T) {
  12. hexifiedCoreModule := "00000000000000000000000000000000000000000000000000000000436f7265"
  13. assert.Equal(t, hex.EncodeToString(CoreModule), hexifiedCoreModule)
  14. }
  15. func TestBodyContractUpgrade(t *testing.T) {
  16. test := BodyContractUpgrade{ChainID: 1, NewContract: addr}
  17. assert.Equal(t, test.ChainID, ChainID(1))
  18. assert.Equal(t, test.NewContract, addr)
  19. }
  20. func TestBodyGuardianSetUpdate(t *testing.T) {
  21. keys := []common.Address{
  22. common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"),
  23. common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"),
  24. }
  25. test := BodyGuardianSetUpdate{Keys: keys, NewIndex: uint32(1)}
  26. assert.Equal(t, test.Keys, keys)
  27. assert.Equal(t, test.NewIndex, uint32(1))
  28. }
  29. func TestBodyTokenBridgeRegisterChain(t *testing.T) {
  30. module := "test"
  31. test := BodyTokenBridgeRegisterChain{Module: module, ChainID: 1, EmitterAddress: addr}
  32. assert.Equal(t, test.Module, module)
  33. assert.Equal(t, test.ChainID, ChainID(1))
  34. assert.Equal(t, test.EmitterAddress, addr)
  35. }
  36. func TestBodyTokenBridgeUpgradeContract(t *testing.T) {
  37. module := "test"
  38. test := BodyTokenBridgeUpgradeContract{Module: module, TargetChainID: 1, NewContract: addr}
  39. assert.Equal(t, test.Module, module)
  40. assert.Equal(t, test.TargetChainID, ChainID(1))
  41. assert.Equal(t, test.NewContract, addr)
  42. }
  43. func TestBodyContractUpgradeSerialize(t *testing.T) {
  44. bodyContractUpgrade := BodyContractUpgrade{ChainID: 1, NewContract: addr}
  45. expected := "00000000000000000000000000000000000000000000000000000000436f72650100010000000000000000000000000000000000000000000000000000000000000004"
  46. serializedBodyContractUpgrade := bodyContractUpgrade.Serialize()
  47. assert.Equal(t, expected, hex.EncodeToString(serializedBodyContractUpgrade))
  48. }
  49. func TestBodyGuardianSetUpdateSerialize(t *testing.T) {
  50. keys := []common.Address{
  51. common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"),
  52. common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"),
  53. }
  54. bodyGuardianSetUpdate := BodyGuardianSetUpdate{Keys: keys, NewIndex: uint32(1)}
  55. expected := "00000000000000000000000000000000000000000000000000000000436f726502000000000001025aaeb6053f3e94c9b9a09f33669435e7ef1beaed5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"
  56. serializedBodyGuardianSetUpdate := bodyGuardianSetUpdate.Serialize()
  57. assert.Equal(t, expected, hex.EncodeToString(serializedBodyGuardianSetUpdate))
  58. }
  59. func TestBodyTokenBridgeRegisterChainSerialize(t *testing.T) {
  60. module := "test"
  61. tests := []struct {
  62. name string
  63. expected string
  64. object BodyTokenBridgeRegisterChain
  65. panic bool
  66. }{
  67. {
  68. name: "working_as_expected",
  69. panic: false,
  70. object: BodyTokenBridgeRegisterChain{Module: module, ChainID: 1, EmitterAddress: addr},
  71. expected: "000000000000000000000000000000000000000000000000000000007465737401000000010000000000000000000000000000000000000000000000000000000000000004",
  72. },
  73. {
  74. name: "panic_at_the_disco!",
  75. panic: true,
  76. object: BodyTokenBridgeRegisterChain{Module: "123456789012345678901234567890123", ChainID: 1, EmitterAddress: addr},
  77. expected: "payload longer than 32 bytes",
  78. },
  79. }
  80. for _, testCase := range tests {
  81. t.Run(testCase.name, func(t *testing.T) {
  82. if testCase.panic {
  83. assert.PanicsWithValue(t, testCase.expected, func() { testCase.object.Serialize() })
  84. } else {
  85. assert.Equal(t, testCase.expected, hex.EncodeToString(testCase.object.Serialize()))
  86. }
  87. })
  88. }
  89. }
  90. func TestBodyTokenBridgeUpgradeContractSerialize(t *testing.T) {
  91. module := "test"
  92. bodyTokenBridgeUpgradeContract := BodyTokenBridgeUpgradeContract{Module: module, TargetChainID: 1, NewContract: addr}
  93. expected := "00000000000000000000000000000000000000000000000000000000746573740200010000000000000000000000000000000000000000000000000000000000000004"
  94. serializedBodyTokenBridgeUpgradeContract := bodyTokenBridgeUpgradeContract.Serialize()
  95. assert.Equal(t, expected, hex.EncodeToString(serializedBodyTokenBridgeUpgradeContract))
  96. }
  97. func TestBodyWormchainStoreCodeSerialize(t *testing.T) {
  98. expected := "0000000000000000000000000000000000000000005761736d644d6f64756c65010c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
  99. bodyWormchainStoreCode := BodyWormchainStoreCode{WasmHash: dummyBytes}
  100. assert.Equal(t, expected, hex.EncodeToString(bodyWormchainStoreCode.Serialize()))
  101. }
  102. func TestBodyWormchainInstantiateContractSerialize(t *testing.T) {
  103. actual := BodyWormchainInstantiateContract{InstantiationParamsHash: dummyBytes}
  104. expected := "0000000000000000000000000000000000000000005761736d644d6f64756c65020c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
  105. assert.Equal(t, expected, hex.EncodeToString(actual.Serialize()))
  106. }
  107. func TestBodyWormchainMigrateContractSerialize(t *testing.T) {
  108. actual := BodyWormchainMigrateContract{MigrationParamsHash: dummyBytes}
  109. expected := "0000000000000000000000000000000000000000005761736d644d6f64756c65030c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
  110. assert.Equal(t, expected, hex.EncodeToString(actual.Serialize()))
  111. }
  112. func TestBodyCircleIntegrationUpdateWormholeFinalitySerialize(t *testing.T) {
  113. expected := "000000000000000000000000000000436972636c65496e746567726174696f6e0100022a"
  114. bodyCircleIntegrationUpdateWormholeFinality := BodyCircleIntegrationUpdateWormholeFinality{TargetChainID: ChainIDEthereum, Finality: 42}
  115. assert.Equal(t, expected, hex.EncodeToString(bodyCircleIntegrationUpdateWormholeFinality.Serialize()))
  116. }
  117. func TestBodyCircleIntegrationRegisterEmitterAndDomainSerialize(t *testing.T) {
  118. expected := "000000000000000000000000000000436972636c65496e746567726174696f6e020002000600000000000000000000000000000000000000000000000000000000000000040000002a"
  119. bodyCircleIntegrationRegisterEmitterAndDomain := BodyCircleIntegrationRegisterEmitterAndDomain{
  120. TargetChainID: ChainIDEthereum,
  121. ForeignEmitterChainId: ChainIDAvalanche,
  122. ForeignEmitterAddress: addr,
  123. CircleDomain: 42,
  124. }
  125. assert.Equal(t, expected, hex.EncodeToString(bodyCircleIntegrationRegisterEmitterAndDomain.Serialize()))
  126. }
  127. func TestBodyCircleIntegrationUpgradeContractImplementationSerialize(t *testing.T) {
  128. expected := "000000000000000000000000000000436972636c65496e746567726174696f6e0300020000000000000000000000000000000000000000000000000000000000000004"
  129. bodyCircleIntegrationUpgradeContractImplementation := BodyCircleIntegrationUpgradeContractImplementation{
  130. TargetChainID: ChainIDEthereum,
  131. NewImplementationAddress: addr,
  132. }
  133. assert.Equal(t, expected, hex.EncodeToString(bodyCircleIntegrationUpgradeContractImplementation.Serialize()))
  134. }
  135. func TestBodyIbcReceiverUpdateChannelChain(t *testing.T) {
  136. expected := "0000000000000000000000000000000000000000004962635265636569766572010c20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006368616e6e656c2d300013"
  137. channelId := LeftPadIbcChannelId("channel-0")
  138. bodyIbcReceiverUpdateChannelChain := BodyIbcUpdateChannelChain{
  139. TargetChainId: ChainIDWormchain,
  140. ChannelId: channelId,
  141. ChainId: ChainIDInjective,
  142. }
  143. assert.Equal(t, expected, hex.EncodeToString(bodyIbcReceiverUpdateChannelChain.Serialize(IbcReceiverModuleStr)))
  144. }
  145. func TestLeftPadBytes(t *testing.T) {
  146. payload := "AAAA"
  147. paddedPayload := LeftPadBytes(payload, int(8))
  148. buf := &bytes.Buffer{}
  149. buf.WriteByte(0x00)
  150. buf.WriteByte(0x00)
  151. buf.WriteByte(0x00)
  152. buf.WriteByte(0x00)
  153. buf.Write([]byte(payload))
  154. assert.Equal(t, paddedPayload, buf)
  155. }
  156. func FuzzLeftPadBytes(f *testing.F) {
  157. // Add examples to our fuzz corpus
  158. f.Add("FOO", 8)
  159. f.Add("123", 8)
  160. f.Fuzz(func(t *testing.T, payload string, length int) {
  161. // We know length could be negative, but we panic if it is in the implementation
  162. if length < 0 {
  163. t.Skip()
  164. }
  165. // We know we cannot left pad something shorter than the payload being provided, but we panic if it is
  166. if len(payload) > length {
  167. t.Skip()
  168. }
  169. paddedPayload := LeftPadBytes(payload, length)
  170. // paddedPayload must always be equal to length
  171. assert.Equal(t, paddedPayload.Len(), length)
  172. })
  173. }
  174. func TestBodyWormholeRelayerSetDefaultDeliveryProviderSerialize(t *testing.T) {
  175. expected := "0000000000000000000000000000000000576f726d686f6c6552656c617965720300040000000000000000000000000000000000000000000000000000000000000004"
  176. bodyWormholeRelayerSetDefaultDeliveryProvider := BodyWormholeRelayerSetDefaultDeliveryProvider{
  177. ChainID: 4,
  178. NewDefaultDeliveryProviderAddress: addr,
  179. }
  180. assert.Equal(t, expected, hex.EncodeToString(bodyWormholeRelayerSetDefaultDeliveryProvider.Serialize()))
  181. }
  182. func TestBodyGatewayIbcComposabilityMwContractSerialize(t *testing.T) {
  183. expected := "00000000000000000000000000000000000000476174657761794d6f64756c65010c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
  184. bodyGatewayIbcComposabilityMwContract := BodyGatewayIbcComposabilityMwContract{
  185. ContractAddr: dummyBytes,
  186. }
  187. assert.Equal(t, expected, hex.EncodeToString(bodyGatewayIbcComposabilityMwContract.Serialize()))
  188. }
  189. func TestBodyGatewayIbcComposabilityMwContractDeserialize(t *testing.T) {
  190. expected := BodyGatewayIbcComposabilityMwContract{
  191. ContractAddr: dummyBytes,
  192. }
  193. var payloadBody BodyGatewayIbcComposabilityMwContract
  194. payloadBody.Deserialize(dummyBytes[:])
  195. assert.Equal(t, expected, payloadBody)
  196. }