diff --git a/primitives/parachain/abridged_hrmp_channel_test.go b/primitives/parachain/abridged_hrmp_channel_test.go index 588f3239..36a3dd83 100644 --- a/primitives/parachain/abridged_hrmp_channel_test.go +++ b/primitives/parachain/abridged_hrmp_channel_test.go @@ -3,7 +3,6 @@ package parachain import ( "bytes" "encoding/hex" - "fmt" "testing" "github.com/ChainSafe/gossamer/lib/common" @@ -36,8 +35,6 @@ func Test_AbridgedHrmpChannel_Encode(t *testing.T) { err := targetAbridgedHrmpChannel.Encode(buffer) - fmt.Println(hex.EncodeToString(buffer.Bytes())) - assert.NoError(t, err) assert.Equal(t, expectedBytesAbridgedHrmpChannel, buffer.Bytes()) } diff --git a/primitives/parachain/ancestor_test.go b/primitives/parachain/ancestor_test.go new file mode 100644 index 00000000..054e646b --- /dev/null +++ b/primitives/parachain/ancestor_test.go @@ -0,0 +1,62 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + "github.com/ChainSafe/gossamer/lib/common" + sc "github.com/LimeChain/goscale" + primitives "github.com/LimeChain/gosemble/primitives/types" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesAncestor, _ = hex.DecodeString("0500000006000000040a0000000500000006000000013aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c0105") +) + +var ( + paraHeadHash = common.MustHexToHash("0x3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c").ToBytes() + targetAncestor = Ancestor{ + UsedBandwidth: UsedBandwidth{ + UmpMsgCount: 5, + UmpTotalBytes: 6, + HrmpOutgoing: sc.Dictionary[sc.U32, HrmpChannelUpdate]{ + 10: { + MsgCount: 5, + TotalBytes: 6, + }, + }, + }, + ParaHeadHash: sc.Option[primitives.H256]{ + HasValue: true, + Value: primitives.H256{FixedSequence: sc.BytesToFixedSequenceU8(paraHeadHash)}, + }, + ConsumedGoAheadSignal: sc.Option[sc.U8]{ + HasValue: true, + Value: 5, + }, + } +) + +func Test_Ancestor_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetAncestor.Encode(buffer) + + assert.NoError(t, err) + assert.Equal(t, expectedBytesAncestor, buffer.Bytes()) +} + +func Test_Ancestor_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesAncestor) + + result, err := DecodeAncestor(buf) + assert.NoError(t, err) + + assert.Equal(t, targetAncestor, result) +} + +func Test_Ancestor_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesAncestor, targetAncestor.Bytes()) +} diff --git a/primitives/parachain/async_backing_params_test.go b/primitives/parachain/async_backing_params_test.go new file mode 100644 index 00000000..c742fbc4 --- /dev/null +++ b/primitives/parachain/async_backing_params_test.go @@ -0,0 +1,40 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesAsyncBackingParams, _ = hex.DecodeString("0400000005000000") +) + +var ( + targetAsyncBackingParams = AsyncBackingParams{ + MaxCandidateDepth: 4, + AllowedAncestryLen: 5, + } +) + +func Test_AsyncBackingParams_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetAsyncBackingParams.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesAsyncBackingParams, buffer.Bytes()) +} + +func Test_AsyncBackingParams_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesAsyncBackingParams) + + result, err := DecodeAsyncBackingParams(buf) + assert.NoError(t, err) + assert.Equal(t, targetAsyncBackingParams, result) +} + +func Test_AsyncBackingParams_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesAsyncBackingParams, targetAsyncBackingParams.Bytes()) +} diff --git a/primitives/parachain/channel_test.go b/primitives/parachain/channel_test.go new file mode 100644 index 00000000..21d7e143 --- /dev/null +++ b/primitives/parachain/channel_test.go @@ -0,0 +1,51 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + primitives "github.com/LimeChain/gosemble/primitives/types" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesChannel, _ = hex.DecodeString("03000000010000000200000003000000040000000500000000") +) + +var ( + targetChannel = Channel{ + ParachainId: 3, + AbridgedHRMPChannel: AbridgedHRMPChannel{ + MaxCapacity: 1, + MaxTotalSize: 2, + MaxMessageSize: 3, + MsgCount: 4, + TotalSize: 5, + MqcHead: sc.Option[primitives.H256]{ + HasValue: false, + }, + }, + } +) + +func Test_Channel_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetChannel.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesChannel, buffer.Bytes()) +} + +func Test_Channel_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesChannel) + + result, err := DecodeChannel(buf) + assert.NoError(t, err) + assert.Equal(t, targetChannel, result) +} + +func Test_Channel_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesChannel, targetChannel.Bytes()) +} diff --git a/primitives/parachain/collation_info_test.go b/primitives/parachain/collation_info_test.go new file mode 100644 index 00000000..01fd918a --- /dev/null +++ b/primitives/parachain/collation_info_test.go @@ -0,0 +1,55 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesCollationInfo, _ = hex.DecodeString("040c01020304030000000c030405010c060708010000000200000008090a") +) + +var ( + targetCollationInfo = CollationInfo{ + UpwardMessages: sc.Sequence[UpwardMessage]{ + sc.Sequence[sc.U8]{1, 2, 3}, + }, + HorizontalMessages: sc.Sequence[OutboundHrmpMessage]{ + { + Id: 3, + Data: sc.Sequence[sc.U8]{3, 4, 5}, + }, + }, + ValidationCode: sc.Option[sc.Sequence[sc.U8]]{ + HasValue: true, + Value: sc.Sequence[sc.U8]{6, 7, 8}, + }, + ProcessedDownwardMessages: 1, + HrmpWatermark: 2, + HeadData: sc.Sequence[sc.U8]{9, 10}, + } +) + +func Test_CollationInfo_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetCollationInfo.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesCollationInfo, buffer.Bytes()) +} + +func Test_CollationInfo_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesCollationInfo) + + result, err := DecodeCollationInfo(buf) + assert.NoError(t, err) + assert.Equal(t, targetCollationInfo, result) +} + +func Test_CollationInfo_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesCollationInfo, targetCollationInfo.Bytes()) +} diff --git a/primitives/parachain/digest_test.go b/primitives/parachain/digest_test.go new file mode 100644 index 00000000..f781869b --- /dev/null +++ b/primitives/parachain/digest_test.go @@ -0,0 +1,23 @@ +package parachain + +import ( + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/LimeChain/gosemble/constants" + primitives "github.com/LimeChain/gosemble/primitives/types" + "github.com/stretchr/testify/assert" +) + +func Test_NewDigestRelayParentStorageRoot(t *testing.T) { + storageRoot := primitives.H256{FixedSequence: constants.ZeroAccountId.FixedSequence} + number := sc.NewU32(5) + + expect := primitives.NewDigestItemConsensusMessage( + sc.BytesToFixedSequenceU8(ConsensusTypeId[:]), + sc.BytesToSequenceU8(sc.NewVaryingData(storageRoot, number).Bytes()), + ) + + result := NewDigestRelayParentStorageRoot(storageRoot, number) + assert.Equal(t, expect, result) +} diff --git a/primitives/parachain/horizontal_messages_test.go b/primitives/parachain/horizontal_messages_test.go new file mode 100644 index 00000000..3b1ea7d4 --- /dev/null +++ b/primitives/parachain/horizontal_messages_test.go @@ -0,0 +1,65 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesHorizontalMessages, _ = hex.DecodeString("040500000004040000000c010203") +) + +var ( + targetHorizontalMessages = HorizontalMessages{ + messages: sc.Dictionary[sc.U32, sc.Sequence[InboundDownwardMessage]]{ + 5: sc.Sequence[InboundDownwardMessage]{ + { + SentAt: 4, + Msg: sc.Sequence[sc.U8]{1, 2, 3}, + }, + }, + }, + } +) + +func Test_HorizontalMessages_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetHorizontalMessages.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesHorizontalMessages, buffer.Bytes()) +} + +func Test_HorizontalMessages_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesHorizontalMessages) + + result, err := DecodeHorizontalMessages(buf) + assert.NoError(t, err) + assert.Equal(t, targetHorizontalMessages, result) +} + +func Test_HorizontalMessages_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesHorizontalMessages, targetHorizontalMessages.Bytes()) +} + +func Test_HorizontalMessages_UnprocessedMessages(t *testing.T) { + expect := targetHorizontalMessages + + result := targetHorizontalMessages.UnprocessedMessages(1) + + assert.Equal(t, expect, result) +} + +func Test_HorizontalMessages_UnprocessedMessages_Empty(t *testing.T) { + expect := HorizontalMessages{sc.Dictionary[sc.U32, sc.Sequence[InboundDownwardMessage]]{ + 5: sc.Sequence[InboundDownwardMessage]{}, + }} + + result := targetHorizontalMessages.UnprocessedMessages(10) + + assert.Equal(t, expect, result) +} diff --git a/primitives/parachain/hrmp_channel_update_test.go b/primitives/parachain/hrmp_channel_update_test.go new file mode 100644 index 00000000..03b9a852 --- /dev/null +++ b/primitives/parachain/hrmp_channel_update_test.go @@ -0,0 +1,64 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + expectedHrmpChannelUpdate, _ = hex.DecodeString("0500000006000000") +) + +var ( + targetHrmpChannelUpdate = HrmpChannelUpdate{ + MsgCount: 5, + TotalBytes: 6, + } +) + +func Test_HrmpChannelUpdate_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetHrmpChannelUpdate.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedHrmpChannelUpdate, buffer.Bytes()) +} + +func Test_HrmpChannelUpdate_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedHrmpChannelUpdate) + + result, err := DecodeHrmpChannelUpdate(buf) + assert.NoError(t, err) + assert.Equal(t, targetHrmpChannelUpdate, result) +} + +func Test_HrmpChannelUpdate_Bytes(t *testing.T) { + assert.Equal(t, expectedHrmpChannelUpdate, targetHrmpChannelUpdate.Bytes()) +} + +func Test_HrmpChannelUpdate_IsEmpty(t *testing.T) { + assert.False(t, targetHrmpChannelUpdate.IsEmpty()) +} + +func Test_HrmpChannelUpdate_Subtract(t *testing.T) { + target := HrmpChannelUpdate{ + MsgCount: 2, + TotalBytes: 3, + } + other := HrmpChannelUpdate{ + MsgCount: 1, + TotalBytes: 2, + } + expect := HrmpChannelUpdate{ + MsgCount: 1, + TotalBytes: 1, + } + + target.Subtract(other) + + assert.Equal(t, expect, target) + +} diff --git a/primitives/parachain/hrmp_outbound_limits_test.go b/primitives/parachain/hrmp_outbound_limits_test.go new file mode 100644 index 00000000..41c853a2 --- /dev/null +++ b/primitives/parachain/hrmp_outbound_limits_test.go @@ -0,0 +1,40 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesHrmpOutboundLimits, _ = hex.DecodeString("0100000002000000") +) + +var ( + targetHrmpOutboundLimits = HrmpOutboundLimits{ + BytesRemaining: 1, + MessagesRemaining: 2, + } +) + +func Test_HrmpOutboundLimits_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetHrmpOutboundLimits.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesHrmpOutboundLimits, buffer.Bytes()) +} + +func Test_HrmpOutboundLimits_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesHrmpOutboundLimits) + + result, err := DecodeHrmpOutboundLimits(buf) + assert.NoError(t, err) + assert.Equal(t, targetHrmpOutboundLimits, result) +} + +func Test_HrmpOutboundLimits_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesHrmpOutboundLimits, targetHrmpOutboundLimits.Bytes()) +} diff --git a/primitives/parachain/inbound_downward_message_test.go b/primitives/parachain/inbound_downward_message_test.go new file mode 100644 index 00000000..e701e0e0 --- /dev/null +++ b/primitives/parachain/inbound_downward_message_test.go @@ -0,0 +1,41 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesInboundDownwardMessage, _ = hex.DecodeString("0a000000080506") +) + +var ( + targetInboundDownwardMessage = InboundDownwardMessage{ + SentAt: 10, + Msg: sc.Sequence[sc.U8]{5, 6}, + } +) + +func Test_InboundDownwardMessage_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetInboundDownwardMessage.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesInboundDownwardMessage, buffer.Bytes()) +} + +func Test_InboundDownwardMessage_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesInboundDownwardMessage) + + result, err := DecodeInboundDownwardMessage(buf) + assert.NoError(t, err) + assert.Equal(t, targetInboundDownwardMessage, result) +} + +func Test_InboundDownwardMessage_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesInboundDownwardMessage, targetInboundDownwardMessage.Bytes()) +} diff --git a/primitives/parachain/inherent_data_test.go b/primitives/parachain/inherent_data_test.go new file mode 100644 index 00000000..992a2dac --- /dev/null +++ b/primitives/parachain/inherent_data_test.go @@ -0,0 +1,71 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + "github.com/ChainSafe/gossamer/lib/common" + sc "github.com/LimeChain/goscale" + primitives "github.com/LimeChain/gosemble/primitives/types" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesInherentData, _ = hex.DecodeString("0c05060a040000003aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c02000000040c010203040a0000000c020304040a000000040b0000000c0b0c0d") +) + +var ( + relayParentStorageRoot = common.MustHexToHash("0x3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c").ToBytes() + targetInherentData = InherentData{ + ValidationData: PersistedValidationData{ + ParentHead: sc.Sequence[sc.U8]{5, 6, 10}, + RelayParentNumber: 4, + RelayParentStorageRoot: primitives.H256{FixedSequence: sc.BytesToFixedSequenceU8(relayParentStorageRoot)}, + MaxPovSize: 2, + }, + RelayChainState: StorageProof{ + TrieNodes: sc.Sequence[sc.Sequence[sc.U8]]{ + sc.Sequence[sc.U8]{ + 1, 2, 3, + }, + }, + }, + DownwardMessages: sc.Sequence[InboundDownwardMessage]{ + { + SentAt: 10, + Msg: sc.Sequence[sc.U8]{2, 3, 4}, + }, + }, + HorizontalMessages: HorizontalMessages{ + messages: sc.Dictionary[sc.U32, sc.Sequence[InboundDownwardMessage]]{ + 10: sc.Sequence[InboundDownwardMessage]{ + { + SentAt: 11, + Msg: sc.Sequence[sc.U8]{11, 12, 13}, + }, + }, + }, + }, + } +) + +func Test_InherentData_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetInherentData.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesInherentData, buffer.Bytes()) +} + +func Test_InherentData_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesInherentData) + + result, err := DecodeInherentData(buf) + assert.NoError(t, err) + assert.Equal(t, targetInherentData, result) +} + +func Test_InherentData_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesInherentData, targetInherentData.Bytes()) +} diff --git a/primitives/parachain/message_queue_chain_test.go b/primitives/parachain/message_queue_chain_test.go new file mode 100644 index 00000000..c753b8a5 --- /dev/null +++ b/primitives/parachain/message_queue_chain_test.go @@ -0,0 +1,43 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + "github.com/ChainSafe/gossamer/lib/common" + sc "github.com/LimeChain/goscale" + primitives "github.com/LimeChain/gosemble/primitives/types" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesMessageQueueChain, _ = hex.DecodeString("3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c") +) + +var ( + messageQueueChainRelayHash = common.MustHexToHash("0x3aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c").ToBytes() + targetMessageQueueChain = MessageQueueChain{ + RelayHash: primitives.H256{FixedSequence: sc.BytesToFixedSequenceU8(messageQueueChainRelayHash)}, + } +) + +func Test_MessageQueueChain_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetMessageQueueChain.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesMessageQueueChain, buffer.Bytes()) +} + +func Test_MessageQueueChain_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesMessageQueueChain) + + result, err := DecodeMessageQueueChain(buf) + assert.NoError(t, err) + assert.Equal(t, targetMessageQueueChain, result) +} + +func Test_MessageQueueChain_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesMessageQueueChain, targetMessageQueueChain.Bytes()) +} diff --git a/primitives/parachain/messaging_state_snapshot_test.go b/primitives/parachain/messaging_state_snapshot_test.go index 46ee4988..91fd8fd7 100644 --- a/primitives/parachain/messaging_state_snapshot_test.go +++ b/primitives/parachain/messaging_state_snapshot_test.go @@ -2,27 +2,71 @@ package parachain import ( "bytes" - "fmt" + "encoding/hex" "testing" + sc "github.com/LimeChain/goscale" "github.com/LimeChain/gosemble/constants" primitives "github.com/LimeChain/gosemble/primitives/types" + "github.com/stretchr/testify/assert" ) -func Test_MessagingStateSnapshot_Encode(t *testing.T) { - a := MessagingStateSnapshot{ - DmqMqcHead: primitives.H256{FixedSequence: constants.ZeroAccountId.FixedSequence}, - RelayDispatchQueueRemainingCapacity: RelayDispatchQueueRemainingCapacity{}, - IngressChannels: nil, - EgressChannels: nil, - } - bytesMss := a.Bytes() - fmt.Println(len(bytesMss)) +var ( + expectedBytesMessagingStateSnapshot, _ = hex.DecodeString("0000000000000000000000000000000000000000000000000000000000000000030000000400000004020000000100000002000000030000000400000005000000000401000000060000000500000004000000030000000200000000") +) - mss, err := DecodeMessagingStateSnapshot(bytes.NewBuffer(bytesMss)) - if err != nil { - panic(err) +var ( + targetMessagingSnapshot = MessagingStateSnapshot{ + DmqMqcHead: primitives.H256{FixedSequence: constants.ZeroAccountId.FixedSequence}, + RelayDispatchQueueRemainingCapacity: RelayDispatchQueueRemainingCapacity{ + RemainingCount: 3, + RemainingSize: 4, + }, + IngressChannels: sc.Sequence[Channel]{ + { + ParachainId: 2, + AbridgedHRMPChannel: AbridgedHRMPChannel{ + MaxCapacity: 1, + MaxTotalSize: 2, + MaxMessageSize: 3, + MsgCount: 4, + TotalSize: 5, + MqcHead: sc.Option[primitives.H256]{}, + }, + }, + }, + EgressChannels: sc.Sequence[Channel]{ + { + ParachainId: 1, + AbridgedHRMPChannel: AbridgedHRMPChannel{ + MaxCapacity: 6, + MaxTotalSize: 5, + MaxMessageSize: 4, + MsgCount: 3, + TotalSize: 2, + MqcHead: sc.Option[primitives.H256]{}, + }, + }, + }, } +) + +func Test_MessagingStateSnapshot_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetMessagingSnapshot.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesMessagingStateSnapshot, buffer.Bytes()) +} + +func Test_MessagingStateSnapshot_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesMessagingStateSnapshot) + + result, err := DecodeMessagingStateSnapshot(buf) + assert.NoError(t, err) + assert.Equal(t, targetMessagingSnapshot, result) +} - fmt.Println(mss) +func Test_MessagingStateSnapshot_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesMessagingStateSnapshot, targetMessagingSnapshot.Bytes()) } diff --git a/primitives/parachain/outbound_bandwith_limits.go b/primitives/parachain/outbound_bandwidth_limits.go similarity index 100% rename from primitives/parachain/outbound_bandwith_limits.go rename to primitives/parachain/outbound_bandwidth_limits.go diff --git a/primitives/parachain/outbound_bandwidth_limits_test.go b/primitives/parachain/outbound_bandwidth_limits_test.go new file mode 100644 index 00000000..31dd6feb --- /dev/null +++ b/primitives/parachain/outbound_bandwidth_limits_test.go @@ -0,0 +1,39 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesOutboundBandwidthLimits, _ = hex.DecodeString("040000000400000004010000000300000004000000") +) + +var ( + targetOutboundBandwidthLimits = OutboundBandwidthLimits{ + UmpMessagesRemaining: 3, + UmpBytesRemaining: 4, + HrmpOutgoing: sc.Dictionary[sc.U32, HrmpOutboundLimits]{ + 1: HrmpOutboundLimits{ + BytesRemaining: 3, + MessagesRemaining: 4, + }, + }, + } +) + +func Test_OutboundBandwidthLimits_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetOutboundBandwidthLimits.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesOutboundBandwidthLimits, buffer.Bytes()) +} + +func Test_OutboundBandwidthLimits_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesOutboundBandwidthLimits, targetOutboundBandwidthLimits.Bytes()) +} diff --git a/primitives/parachain/outbound_hrmp_message_test.go b/primitives/parachain/outbound_hrmp_message_test.go new file mode 100644 index 00000000..da6a3dc3 --- /dev/null +++ b/primitives/parachain/outbound_hrmp_message_test.go @@ -0,0 +1,41 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesOutboundHrmpMessage, _ = hex.DecodeString("050000000c050607") +) + +var ( + targetOutboundHrmpMessage = OutboundHrmpMessage{ + Id: 5, + Data: sc.Sequence[sc.U8]{5, 6, 7}, + } +) + +func Test_OutboundHrmpMessage_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetOutboundHrmpMessage.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesOutboundHrmpMessage, buffer.Bytes()) +} + +func Test_OutboundHrmpMessage_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesOutboundHrmpMessage) + + result, err := DecodeOutboundHrmpMessage(buf) + assert.NoError(t, err) + assert.Equal(t, targetOutboundHrmpMessage, result) +} + +func Test_OutboundHrmpMessage_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesOutboundHrmpMessage, targetOutboundHrmpMessage.Bytes()) +} diff --git a/primitives/parachain/persisted_validation_data_test.go b/primitives/parachain/persisted_validation_data_test.go new file mode 100644 index 00000000..965515e9 --- /dev/null +++ b/primitives/parachain/persisted_validation_data_test.go @@ -0,0 +1,45 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/LimeChain/gosemble/constants" + primitives "github.com/LimeChain/gosemble/primitives/types" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesPersistedValidationData, _ = hex.DecodeString("0c01020301000000000000000000000000000000000000000000000000000000000000000000000102000000") +) + +var ( + targetPersistedValidationData = PersistedValidationData{ + ParentHead: sc.Sequence[sc.U8]{1, 2, 3}, + RelayParentNumber: 1, + RelayParentStorageRoot: primitives.H256{FixedSequence: constants.OneAccountId.FixedSequence}, + MaxPovSize: 2, + } +) + +func Test_PersistedValidationData_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetPersistedValidationData.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesPersistedValidationData, buffer.Bytes()) +} + +func Test_PersistedValidationData_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesPersistedValidationData) + + result, err := DecodePersistedValidationData(buf) + assert.NoError(t, err) + assert.Equal(t, targetPersistedValidationData, result) +} + +func Test_PersistedValidationData_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesPersistedValidationData, targetPersistedValidationData.Bytes()) +} diff --git a/primitives/parachain/relay_dispatch_queue_remaining_capacity_test.go b/primitives/parachain/relay_dispatch_queue_remaining_capacity_test.go new file mode 100644 index 00000000..5669917a --- /dev/null +++ b/primitives/parachain/relay_dispatch_queue_remaining_capacity_test.go @@ -0,0 +1,40 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesRelayDispatchQueueRemainingCapacity, _ = hex.DecodeString("0600000007000000") +) + +var ( + targetRelayDispatchQueueRemainingCapacity = RelayDispatchQueueRemainingCapacity{ + RemainingCount: 6, + RemainingSize: 7, + } +) + +func Test_RelayDispatchQueueRemainingCapacity_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetRelayDispatchQueueRemainingCapacity.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesRelayDispatchQueueRemainingCapacity, buffer.Bytes()) +} + +func Test_RelayDispatchQueueRemainingCapacity_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesRelayDispatchQueueRemainingCapacity) + + result, err := DecodeRelayDispatchQueueRemainingCapacity(buf) + assert.NoError(t, err) + assert.Equal(t, targetRelayDispatchQueueRemainingCapacity, result) +} + +func Test_RelayDispatchQueueRemainingCapacity_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesRelayDispatchQueueRemainingCapacity, targetRelayDispatchQueueRemainingCapacity.Bytes()) +} diff --git a/primitives/parachain/segment_tracker_test.go b/primitives/parachain/segment_tracker_test.go new file mode 100644 index 00000000..532738a0 --- /dev/null +++ b/primitives/parachain/segment_tracker_test.go @@ -0,0 +1,52 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesSegmentTracker, _ = hex.DecodeString("01000000020000000001050000000101") +) + +var ( + targetSegmentTracker = SegmentTracker{ + UsedBandwidth: UsedBandwidth{ + UmpMsgCount: 1, + UmpTotalBytes: 2, + HrmpOutgoing: sc.Dictionary[sc.U32, HrmpChannelUpdate]{}, + }, + HrmpWatermark: sc.Option[RelayChainBlockNumber]{ + HasValue: true, + Value: 5, + }, + ConsumedGoAheadSignal: sc.Option[sc.U8]{ + HasValue: true, + Value: 1, + }, + } +) + +func Test_SegmentTracker_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetSegmentTracker.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesSegmentTracker, buffer.Bytes()) +} + +func Test_SegmentTracker_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesSegmentTracker) + + result, err := DecodeSegmentTracker(buf) + assert.NoError(t, err) + assert.Equal(t, targetSegmentTracker, result) +} + +func Test_SegmentTracker_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesSegmentTracker, targetSegmentTracker.Bytes()) +} diff --git a/primitives/parachain/storage_proof_test.go b/primitives/parachain/storage_proof_test.go new file mode 100644 index 00000000..9b9d902f --- /dev/null +++ b/primitives/parachain/storage_proof_test.go @@ -0,0 +1,48 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesStorageProof, _ = hex.DecodeString("08100405060718010203040506") +) + +var ( + targetStorageProof = StorageProof{ + TrieNodes: sc.Sequence[sc.Sequence[sc.U8]]{ + sc.Sequence[sc.U8]{ + 4, 5, 6, 7, + }, + sc.Sequence[sc.U8]{ + 1, 2, 3, 4, 5, 6, + }, + }, + } +) + +func Test_StorageProof_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetStorageProof.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesStorageProof, buffer.Bytes()) +} + +func Test_StorageProof_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesStorageProof) + + result, err := DecodeStorageProof(buf) + assert.NoError(t, err) + assert.Equal(t, targetStorageProof, result) +} + +func Test_StorageProof_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesStorageProof, targetStorageProof.Bytes()) +} diff --git a/primitives/parachain/unincluded_segment_test.go b/primitives/parachain/unincluded_segment_test.go new file mode 100644 index 00000000..4daf9350 --- /dev/null +++ b/primitives/parachain/unincluded_segment_test.go @@ -0,0 +1,62 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + primitives "github.com/LimeChain/gosemble/primitives/types" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesUnincludedSegment, _ = hex.DecodeString("040500000006000000040a0000000500000006000000013aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c0105") +) + +var ( + targetUnincludedSegment = UnincludedSegment{ + Ancestors: sc.Sequence[Ancestor]{ + { + UsedBandwidth: UsedBandwidth{ + UmpMsgCount: 5, + UmpTotalBytes: 6, + HrmpOutgoing: sc.Dictionary[sc.U32, HrmpChannelUpdate]{ + 10: { + MsgCount: 5, + TotalBytes: 6, + }, + }, + }, + ParaHeadHash: sc.Option[primitives.H256]{ + HasValue: true, + Value: primitives.H256{FixedSequence: sc.BytesToFixedSequenceU8(paraHeadHash)}, + }, + ConsumedGoAheadSignal: sc.Option[sc.U8]{ + HasValue: true, + Value: 5, + }, + }, + }, + } +) + +func Test_UnincludedSegment_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetUnincludedSegment.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesUnincludedSegment, buffer.Bytes()) +} + +func Test_UnincludedSegment_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesUnincludedSegment) + + result, err := DecodeUnincludedSegment(buf) + assert.NoError(t, err) + assert.Equal(t, targetUnincludedSegment, result) +} + +func Test_UnincludedSegment_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesUnincludedSegment, targetUnincludedSegment.Bytes()) +} diff --git a/primitives/parachain/user_bandwith_test.go b/primitives/parachain/user_bandwith_test.go new file mode 100644 index 00000000..5750dc78 --- /dev/null +++ b/primitives/parachain/user_bandwith_test.go @@ -0,0 +1,47 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesUserBandwidth, _ = hex.DecodeString("0500000006000000040a0000000500000006000000") +) + +var ( + targetUserBandwidth = UsedBandwidth{ + UmpMsgCount: 5, + UmpTotalBytes: 6, + HrmpOutgoing: sc.Dictionary[sc.U32, HrmpChannelUpdate]{ + 10: { + MsgCount: 5, + TotalBytes: 6, + }, + }, + } +) + +func Test_UserBandwidth_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetUserBandwidth.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesUserBandwidth, buffer.Bytes()) +} + +func Test_UserBandwidth_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesUserBandwidth) + + result, err := DecodeUsedBandwidth(buf) + assert.NoError(t, err) + assert.Equal(t, targetUserBandwidth, result) +} + +func Test_UserBandwidth_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesUserBandwidth, targetUserBandwidth.Bytes()) +} diff --git a/primitives/parachain/validation_params_test.go b/primitives/parachain/validation_params_test.go new file mode 100644 index 00000000..90cb2801 --- /dev/null +++ b/primitives/parachain/validation_params_test.go @@ -0,0 +1,45 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + primitives "github.com/LimeChain/gosemble/primitives/types" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesValidationParams, _ = hex.DecodeString("100304050610050607080a0000003aa96b0149b6ca3688878bdbd19464448624136398e3ce45b9e755d3ab61355c") +) + +var ( + targetValidationParams = ValidationParams{ + ParentHead: sc.Sequence[sc.U8]{3, 4, 5, 6}, + BlockData: sc.Sequence[sc.U8]{5, 6, 7, 8}, + RelayParentBlockNumber: 10, + RelayParentStorageRoot: primitives.H256{FixedSequence: sc.BytesToFixedSequenceU8(relayParentStorageRoot)}, + } +) + +func Test_ValidationParams_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetValidationParams.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesValidationParams, buffer.Bytes()) +} + +func Test_ValidationParams_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesValidationParams) + + result, err := DecodeValidationParams(buf) + assert.NoError(t, err) + assert.Equal(t, targetValidationParams, result) +} + +func Test_ValidationParams_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesValidationParams, targetValidationParams.Bytes()) +} diff --git a/primitives/parachain/validation_result_test.go b/primitives/parachain/validation_result_test.go new file mode 100644 index 00000000..12d69cb1 --- /dev/null +++ b/primitives/parachain/validation_result_test.go @@ -0,0 +1,55 @@ +package parachain + +import ( + "bytes" + "encoding/hex" + "testing" + + sc "github.com/LimeChain/goscale" + "github.com/stretchr/testify/assert" +) + +var ( + expectedBytesValidationResult, _ = hex.DecodeString("140102030305011002030405041001020304040b000000140a0b0c0d0e0a0000000b000000") +) + +var ( + targetValidationResult = ValidationResult{ + HeadData: sc.Sequence[sc.U8]{1, 2, 3, 3, 5}, + NewValidationCode: sc.Option[sc.Sequence[sc.U8]]{ + HasValue: true, + Value: sc.Sequence[sc.U8]{2, 3, 4, 5}, + }, + UpwardMessages: sc.Sequence[UpwardMessage]{ + sc.Sequence[sc.U8]{1, 2, 3, 4}, + }, + HorizontalMessages: sc.Sequence[OutboundHrmpMessage]{ + { + Id: 11, + Data: sc.Sequence[sc.U8]{10, 11, 12, 13, 14}, + }, + }, + ProcessedDownwardMessages: 10, + HrmpWatermark: 11, + } +) + +func Test_ValidationResult_Encode(t *testing.T) { + buffer := &bytes.Buffer{} + + err := targetValidationResult.Encode(buffer) + assert.NoError(t, err) + assert.Equal(t, expectedBytesValidationResult, buffer.Bytes()) +} + +func Test_ValidationResult_Decode(t *testing.T) { + buf := bytes.NewBuffer(expectedBytesValidationResult) + + result, err := DecodeValidationResult(buf) + assert.NoError(t, err) + assert.Equal(t, targetValidationResult, result) +} + +func Test_ValidationResult_Bytes(t *testing.T) { + assert.Equal(t, expectedBytesValidationResult, targetValidationResult.Bytes()) +}