| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468 |
- package vaa
- import (
- "bytes"
- "encoding/hex"
- "errors"
- "reflect"
- "testing"
- "time"
- "github.com/ethereum/go-ethereum/common"
- "github.com/holiman/uint256"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- )
- 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}
- 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}
- func TestCoreModule(t *testing.T) {
- hexifiedCoreModule := "00000000000000000000000000000000000000000000000000000000436f7265"
- assert.Equal(t, hex.EncodeToString(CoreModule), hexifiedCoreModule)
- }
- func TestBodyContractUpgrade(t *testing.T) {
- test := BodyContractUpgrade{ChainID: 1, NewContract: addr}
- assert.Equal(t, test.ChainID, ChainID(1))
- assert.Equal(t, test.NewContract, addr)
- }
- func TestBodyGuardianSetUpdate(t *testing.T) {
- keys := []common.Address{
- common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"),
- common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"),
- }
- test := BodyGuardianSetUpdate{Keys: keys, NewIndex: uint32(1)}
- assert.Equal(t, test.Keys, keys)
- assert.Equal(t, test.NewIndex, uint32(1))
- }
- func TestBodyTokenBridgeRegisterChain(t *testing.T) {
- module := "test"
- test := BodyTokenBridgeRegisterChain{Module: module, ChainID: 1, EmitterAddress: addr}
- assert.Equal(t, test.Module, module)
- assert.Equal(t, test.ChainID, ChainID(1))
- assert.Equal(t, test.EmitterAddress, addr)
- }
- func TestBodyTokenBridgeUpgradeContract(t *testing.T) {
- module := "test"
- test := BodyTokenBridgeUpgradeContract{Module: module, TargetChainID: 1, NewContract: addr}
- assert.Equal(t, test.Module, module)
- assert.Equal(t, test.TargetChainID, ChainID(1))
- assert.Equal(t, test.NewContract, addr)
- }
- func TestBodyContractUpgradeSerialize(t *testing.T) {
- bodyContractUpgrade := BodyContractUpgrade{ChainID: 1, NewContract: addr}
- expected := "00000000000000000000000000000000000000000000000000000000436f72650100010000000000000000000000000000000000000000000000000000000000000004"
- serializedBodyContractUpgrade, err := bodyContractUpgrade.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(serializedBodyContractUpgrade))
- }
- func TestBodyGuardianSetUpdateSerialize(t *testing.T) {
- keys := []common.Address{
- common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaed"),
- common.HexToAddress("0x5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"),
- }
- bodyGuardianSetUpdate := BodyGuardianSetUpdate{Keys: keys, NewIndex: uint32(1)}
- expected := "00000000000000000000000000000000000000000000000000000000436f726502000000000001025aaeb6053f3e94c9b9a09f33669435e7ef1beaed5aaeb6053f3e94c9b9a09f33669435e7ef1beaee"
- serializedBodyGuardianSetUpdate, err := bodyGuardianSetUpdate.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(serializedBodyGuardianSetUpdate))
- }
- func TestBodyTokenBridgeRegisterChainSerialize(t *testing.T) {
- module := "test"
- tests := []struct {
- name string
- expected string
- object BodyTokenBridgeRegisterChain
- err error
- }{
- {
- name: "working_as_expected",
- err: nil,
- object: BodyTokenBridgeRegisterChain{Module: module, ChainID: 1, EmitterAddress: addr},
- expected: "000000000000000000000000000000000000000000000000000000007465737401000000010000000000000000000000000000000000000000000000000000000000000004",
- },
- {
- name: "panic_at_the_disco!",
- err: errors.New("payload longer than 32 bytes"),
- object: BodyTokenBridgeRegisterChain{Module: "123456789012345678901234567890123", ChainID: 1, EmitterAddress: addr},
- expected: "payload longer than 32 bytes",
- },
- }
- for _, testCase := range tests {
- t.Run(testCase.name, func(t *testing.T) {
- buf, err := testCase.object.Serialize()
- if testCase.err != nil {
- require.ErrorContains(t, err, testCase.err.Error())
- assert.Nil(t, buf)
- } else {
- require.NoError(t, err)
- assert.Equal(t, testCase.expected, hex.EncodeToString(buf))
- }
- })
- }
- }
- func TestBodyTokenBridgeUpgradeContractSerialize(t *testing.T) {
- module := "test"
- bodyTokenBridgeUpgradeContract := BodyTokenBridgeUpgradeContract{Module: module, TargetChainID: 1, NewContract: addr}
- expected := "00000000000000000000000000000000000000000000000000000000746573740200010000000000000000000000000000000000000000000000000000000000000004"
- serializedBodyTokenBridgeUpgradeContract, err := bodyTokenBridgeUpgradeContract.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(serializedBodyTokenBridgeUpgradeContract))
- }
- func TestBodyWormchainStoreCodeSerialize(t *testing.T) {
- expected := "0000000000000000000000000000000000000000005761736d644d6f64756c65010c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
- bodyWormchainStoreCode := BodyWormchainStoreCode{WasmHash: dummyBytes}
- buf, err := bodyWormchainStoreCode.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyWormchainInstantiateContractSerialize(t *testing.T) {
- actual := BodyWormchainInstantiateContract{InstantiationParamsHash: dummyBytes}
- expected := "0000000000000000000000000000000000000000005761736d644d6f64756c65020c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
- buf, err := actual.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyWormchainMigrateContractSerialize(t *testing.T) {
- actual := BodyWormchainMigrateContract{MigrationParamsHash: dummyBytes}
- expected := "0000000000000000000000000000000000000000005761736d644d6f64756c65030c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
- buf, err := actual.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyWormchainWasmAllowlistInstantiateSerialize(t *testing.T) {
- actual := BodyWormchainWasmAllowlistInstantiate{ContractAddr: dummyBytes, CodeId: uint64(42)}
- expected := "0000000000000000000000000000000000000000005761736d644d6f64756c65040c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20000000000000002a"
- buf, err := actual.Serialize(ActionAddWasmInstantiateAllowlist)
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- const BodyWormchainWasmAllowlistInstantiateBuf = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20000000000000002a"
- func TestBodyWormchainWasmAllowlistInstantiateDeserialize(t *testing.T) {
- expected := BodyWormchainWasmAllowlistInstantiate{ContractAddr: dummyBytes, CodeId: uint64(42)}
- buf, err := hex.DecodeString(BodyWormchainWasmAllowlistInstantiateBuf)
- require.NoError(t, err)
- var actual BodyWormchainWasmAllowlistInstantiate
- err = actual.Deserialize(buf)
- require.NoError(t, err)
- assert.True(t, reflect.DeepEqual(expected, actual))
- }
- func TestBodyWormchainWasmAllowlistInstantiateDeserializeFailureTooShort(t *testing.T) {
- buf, err := hex.DecodeString(BodyWormchainWasmAllowlistInstantiateBuf[0 : len(BodyWormchainWasmAllowlistInstantiateBuf)-2])
- require.NoError(t, err)
- var actual BodyWormchainWasmAllowlistInstantiate
- err = actual.Deserialize(buf)
- require.ErrorContains(t, err, "incorrect payload length, should be 40, is 39")
- }
- func TestBodyWormchainWasmAllowlistInstantiateDeserializeFailureTooLong(t *testing.T) {
- buf, err := hex.DecodeString(BodyWormchainWasmAllowlistInstantiateBuf + "00")
- require.NoError(t, err)
- var actual BodyWormchainWasmAllowlistInstantiate
- err = actual.Deserialize(buf)
- require.ErrorContains(t, err, "incorrect payload length, should be 40, is 41")
- }
- func TestBodyCircleIntegrationUpdateWormholeFinalitySerialize(t *testing.T) {
- expected := "000000000000000000000000000000436972636c65496e746567726174696f6e0100022a"
- bodyCircleIntegrationUpdateWormholeFinality := BodyCircleIntegrationUpdateWormholeFinality{TargetChainID: ChainIDEthereum, Finality: 42}
- buf, err := bodyCircleIntegrationUpdateWormholeFinality.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyCircleIntegrationRegisterEmitterAndDomainSerialize(t *testing.T) {
- expected := "000000000000000000000000000000436972636c65496e746567726174696f6e020002000600000000000000000000000000000000000000000000000000000000000000040000002a"
- bodyCircleIntegrationRegisterEmitterAndDomain := BodyCircleIntegrationRegisterEmitterAndDomain{
- TargetChainID: ChainIDEthereum,
- ForeignEmitterChainId: ChainIDAvalanche,
- ForeignEmitterAddress: addr,
- CircleDomain: 42,
- }
- buf, err := bodyCircleIntegrationRegisterEmitterAndDomain.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyCircleIntegrationUpgradeContractImplementationSerialize(t *testing.T) {
- expected := "000000000000000000000000000000436972636c65496e746567726174696f6e0300020000000000000000000000000000000000000000000000000000000000000004"
- bodyCircleIntegrationUpgradeContractImplementation := BodyCircleIntegrationUpgradeContractImplementation{
- TargetChainID: ChainIDEthereum,
- NewImplementationAddress: addr,
- }
- buf, err := bodyCircleIntegrationUpgradeContractImplementation.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyIbcReceiverUpdateChannelChain(t *testing.T) {
- expected := "0000000000000000000000000000000000000000004962635265636569766572010c20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006368616e6e656c2d300013"
- channelId, err := LeftPadIbcChannelId("channel-0")
- require.NoError(t, err)
- bodyIbcReceiverUpdateChannelChain := BodyIbcUpdateChannelChain{
- TargetChainId: ChainIDWormchain,
- ChannelId: channelId,
- ChainId: ChainIDInjective,
- }
- buf, err := bodyIbcReceiverUpdateChannelChain.Serialize(IbcReceiverModuleStr)
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyIbcReceiverUpdateChannelChainBadModuleName(t *testing.T) {
- channelId, err := LeftPadIbcChannelId("channel-0")
- require.NoError(t, err)
- bodyIbcReceiverUpdateChannelChain := BodyIbcUpdateChannelChain{
- TargetChainId: ChainIDWormchain,
- ChannelId: channelId,
- ChainId: ChainIDInjective,
- }
- buf, err := bodyIbcReceiverUpdateChannelChain.Serialize(IbcReceiverModuleStr + "ExtraJunk")
- require.ErrorContains(t, err, "module for BodyIbcUpdateChannelChain must be either IbcReceiver or IbcTranslator")
- assert.Nil(t, buf)
- }
- func TestLeftPadIbcChannelId(t *testing.T) {
- channelId, err := LeftPadIbcChannelId("channel-0")
- require.NoError(t, err)
- assert.Equal(t, "000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006368616e6e656c2d30", hex.EncodeToString(channelId[:]))
- }
- func TestLeftPadIbcChannelIdFailureTooLong(t *testing.T) {
- channelId, err := LeftPadIbcChannelId("channel-ThatIsTooLong!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!")
- require.ErrorContains(t, err, "failed to left pad module: payload longer than 64 bytes")
- expected := [64]byte{}
- assert.True(t, bytes.Equal(expected[:], channelId[:]))
- }
- func TestLeftPadBytes(t *testing.T) {
- payload := "AAAA"
- paddedPayload, err := LeftPadBytes(payload, int(8))
- require.NoError(t, err)
- buf := &bytes.Buffer{}
- buf.WriteByte(0x00)
- buf.WriteByte(0x00)
- buf.WriteByte(0x00)
- buf.WriteByte(0x00)
- buf.Write([]byte(payload))
- assert.Equal(t, paddedPayload, buf)
- }
- func TestLeftPadBytesFailures(t *testing.T) {
- payload := "AAAA"
- paddedPayload, err := LeftPadBytes(payload, int(-2))
- require.ErrorContains(t, err, "cannot prepend bytes to a negative length buffer")
- assert.Nil(t, paddedPayload)
- paddedPayload, err = LeftPadBytes(payload, int(2))
- require.ErrorContains(t, err, "payload longer than 2 bytes")
- assert.Nil(t, paddedPayload)
- }
- func TestSerializeBridgeGovernanceVaaModuleTooLong(t *testing.T) {
- buf, err := serializeBridgeGovernanceVaa("ModuleNameIsMoreThanThirtyTwoCharacters", ActionRegisterChain, 1, []byte{0, 1, 2})
- require.ErrorContains(t, err, "failed to left pad module: payload longer than 32 bytes")
- assert.Nil(t, buf)
- }
- func FuzzLeftPadBytes(f *testing.F) {
- // Add examples to our fuzz corpus
- f.Add("FOO", 8)
- f.Add("123", 8)
- f.Fuzz(func(t *testing.T, payload string, length int) {
- // We know length could be negative, but we panic if it is in the implementation
- if length < 0 {
- t.Skip()
- }
- // We know we cannot left pad something shorter than the payload being provided, but we panic if it is
- if len(payload) > length {
- t.Skip()
- }
- paddedPayload, err := LeftPadBytes(payload, length)
- require.NoError(t, err)
- // paddedPayload must always be equal to length
- assert.Equal(t, paddedPayload.Len(), length)
- })
- }
- func TestBodyWormholeRelayerSetDefaultDeliveryProviderSerialize(t *testing.T) {
- expected := "0000000000000000000000000000000000576f726d686f6c6552656c617965720300040000000000000000000000000000000000000000000000000000000000000004"
- bodyWormholeRelayerSetDefaultDeliveryProvider := BodyWormholeRelayerSetDefaultDeliveryProvider{
- ChainID: 4,
- NewDefaultDeliveryProviderAddress: addr,
- }
- buf, err := bodyWormholeRelayerSetDefaultDeliveryProvider.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyGatewayIbcComposabilityMwContractSerialize(t *testing.T) {
- expected := "00000000000000000000000000000000000000476174657761794d6f64756c65030c200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
- bodyGatewayIbcComposabilityMwContract := BodyGatewayIbcComposabilityMwContract{
- ContractAddr: dummyBytes,
- }
- buf, err := bodyGatewayIbcComposabilityMwContract.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- const BodyGatewayIbcComposabilityMwContractBuf = "0102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f20"
- func TestBodyGatewayIbcComposabilityMwContractDeserialize(t *testing.T) {
- expected := BodyGatewayIbcComposabilityMwContract{
- ContractAddr: dummyBytes,
- }
- var payloadBody BodyGatewayIbcComposabilityMwContract
- err := payloadBody.Deserialize(dummyBytes[:])
- require.NoError(t, err)
- assert.Equal(t, expected, payloadBody)
- }
- func TestBodyGatewayIbcComposabilityMwContractDeserializeFailureTooShort(t *testing.T) {
- buf, err := hex.DecodeString(BodyGatewayIbcComposabilityMwContractBuf[0 : len(BodyGatewayIbcComposabilityMwContractBuf)-2])
- require.NoError(t, err)
- var actual BodyGatewayIbcComposabilityMwContract
- err = actual.Deserialize(buf)
- require.ErrorContains(t, err, "incorrect payload length, should be 32, is 31")
- }
- func TestBodyGatewayIbcComposabilityMwContractDeserializeFailureTooLong(t *testing.T) {
- buf, err := hex.DecodeString(BodyGatewayIbcComposabilityMwContractBuf + "00")
- require.NoError(t, err)
- var actual BodyGatewayIbcComposabilityMwContract
- err = actual.Deserialize(buf)
- require.ErrorContains(t, err, "incorrect payload length, should be 32, is 33")
- }
- func TestBodySlashingParamsUpdateSerialize(t *testing.T) {
- signedBlocksWindow := uint64(100)
- minSignedPerWindow := uint64(500000000000000000)
- downtimeJailDuration := uint64(600 * time.Second)
- slashFractionDoubleSign := uint64(50000000000000000)
- slashFractionDowntime := uint64(10000000000000000)
- bodySlashingParamsUpdate := BodyGatewaySlashingParamsUpdate{
- SignedBlocksWindow: signedBlocksWindow,
- MinSignedPerWindow: minSignedPerWindow,
- DowntimeJailDuration: downtimeJailDuration,
- SlashFractionDoubleSign: slashFractionDoubleSign,
- SlashFractionDowntime: slashFractionDowntime,
- }
- serializedBody, err := bodySlashingParamsUpdate.Serialize()
- require.NoError(t, err)
- expected := "00000000000000000000000000000000000000476174657761794d6f64756c65040c20000000000000006406f05b59d3b200000000008bb2c9700000b1a2bc2ec50000002386f26fc10000"
- assert.Equal(t, expected, hex.EncodeToString(serializedBody))
- }
- const BodySlashingParamsUpdateBuf = "000000000000006406f05b59d3b200000000008bb2c9700000b1a2bc2ec50000002386f26fc10000"
- func TestBodySlashingParamsUpdateDeserialize(t *testing.T) {
- expected := BodyGatewaySlashingParamsUpdate{
- SignedBlocksWindow: 100,
- MinSignedPerWindow: 500000000000000000,
- DowntimeJailDuration: uint64(600 * time.Second),
- SlashFractionDoubleSign: 50000000000000000,
- SlashFractionDowntime: 10000000000000000,
- }
- var payloadBody BodyGatewaySlashingParamsUpdate
- bz, err := hex.DecodeString(BodySlashingParamsUpdateBuf)
- require.NoError(t, err)
- err = payloadBody.Deserialize(bz)
- require.NoError(t, err)
- assert.Equal(t, expected, payloadBody)
- }
- func TestBodySlashingParamsUpdateDeserializeFailureTooShort(t *testing.T) {
- buf, err := hex.DecodeString(BodySlashingParamsUpdateBuf[0 : len(BodySlashingParamsUpdateBuf)-2])
- require.NoError(t, err)
- var actual BodyGatewaySlashingParamsUpdate
- err = actual.Deserialize(buf)
- require.ErrorContains(t, err, "incorrect payload length, should be 40, is 39")
- }
- func TestBodySlashingParamsUpdateDeserializeFailureTooLong(t *testing.T) {
- buf, err := hex.DecodeString(BodySlashingParamsUpdateBuf + "00")
- require.NoError(t, err)
- var actual BodyGatewaySlashingParamsUpdate
- err = actual.Deserialize(buf)
- require.ErrorContains(t, err, "incorrect payload length, should be 40, is 41")
- }
- func TestBodyCoreRecoverChainIdSerialize(t *testing.T) {
- expected := "00000000000000000000000000000000000000000000000000000000436f72650500000000000000000000000000000000000000000000000000000000000000010fa0"
- BodyRecoverChainId := BodyRecoverChainId{
- Module: "Core",
- EvmChainID: uint256.NewInt(1),
- NewChainID: 4000,
- }
- buf, err := BodyRecoverChainId.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyTokenBridgeRecoverChainIdSerialize(t *testing.T) {
- expected := "000000000000000000000000000000000000000000546f6b656e4272696467650300000000000000000000000000000000000000000000000000000000000000010fa0"
- BodyRecoverChainId := BodyRecoverChainId{
- Module: "TokenBridge",
- EvmChainID: uint256.NewInt(1),
- NewChainID: 4000,
- }
- buf, err := BodyRecoverChainId.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
- func TestBodyRecoverChainIdModuleTooLong(t *testing.T) {
- BodyRecoverChainId := BodyRecoverChainId{
- Module: "ModuleNameIsMoreThanThirtyTwoCharacters",
- EvmChainID: uint256.NewInt(1),
- NewChainID: 4000,
- }
- buf, err := BodyRecoverChainId.Serialize()
- require.ErrorContains(t, err, "failed to left pad module: payload longer than 32 bytes")
- assert.Nil(t, buf)
- }
- func TestBodyCoreBridgeSetMessageFeeSerialize(t *testing.T) {
- expected := "00000000000000000000000000000000000000000000000000000000436f72650304560000000000000000000000000000000000000000000000000000000000000123"
- bodyCoreBridgeSetMessageFee := BodyCoreBridgeSetMessageFee{
- ChainID: 0x456,
- MessageFee: uint256.NewInt(0x123),
- }
- buf, err := bodyCoreBridgeSetMessageFee.Serialize()
- require.NoError(t, err)
- assert.Equal(t, expected, hex.EncodeToString(buf))
- }
|