Skip to content

Commit

Permalink
test(parachain): types (#423)
Browse files Browse the repository at this point in the history
  • Loading branch information
failfmi authored Jun 24, 2024
1 parent e617bdd commit 4206f44
Show file tree
Hide file tree
Showing 24 changed files with 1,087 additions and 17 deletions.
3 changes: 0 additions & 3 deletions primitives/parachain/abridged_hrmp_channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package parachain
import (
"bytes"
"encoding/hex"
"fmt"
"testing"

"github.com/ChainSafe/gossamer/lib/common"
Expand Down Expand Up @@ -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())
}
Expand Down
62 changes: 62 additions & 0 deletions primitives/parachain/ancestor_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
40 changes: 40 additions & 0 deletions primitives/parachain/async_backing_params_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
51 changes: 51 additions & 0 deletions primitives/parachain/channel_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
55 changes: 55 additions & 0 deletions primitives/parachain/collation_info_test.go
Original file line number Diff line number Diff line change
@@ -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())
}
23 changes: 23 additions & 0 deletions primitives/parachain/digest_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
65 changes: 65 additions & 0 deletions primitives/parachain/horizontal_messages_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
64 changes: 64 additions & 0 deletions primitives/parachain/hrmp_channel_update_test.go
Original file line number Diff line number Diff line change
@@ -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)

}
Loading

0 comments on commit 4206f44

Please sign in to comment.