Skip to content

Commit

Permalink
chore(lib/parachain): add tests to decode varying data types regardin…
Browse files Browse the repository at this point in the history
…g statement and collation (#3374)

added New() method and decode test for below varying data types. 
- statement
- statement distribution message
- collation protocol
- collator protocol message
  • Loading branch information
axaysagathiya authored and kishansagathiya committed Jan 23, 2024
1 parent 3bce992 commit 137ad65
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 24 deletions.
10 changes: 10 additions & 0 deletions lib/parachain/collation_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ func NewCollationProtocol() CollationProtocol {
return CollationProtocol(vdt)
}

// New will enable scale to create new instance when needed
func (CollationProtocol) New() CollationProtocol {
return NewCollationProtocol()
}

// Set will set a value using the underlying varying data type
func (c *CollationProtocol) Set(val scale.VaryingDataTypeValue) (err error) {
vdt := scale.VaryingDataType(*c)
Expand Down Expand Up @@ -45,6 +50,11 @@ func NewCollatorProtocolMessage() CollatorProtocolMessage {
return CollatorProtocolMessage(vdt)
}

// New will enable scale to create new instance when needed
func (CollatorProtocolMessage) New() CollatorProtocolMessage {
return NewCollatorProtocolMessage()
}

// Set will set a value using the underlying varying data type
func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error) {
vdt := scale.VaryingDataType(*c)
Expand Down
43 changes: 33 additions & 10 deletions lib/parachain/collation_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ func TestCollationProtocol(t *testing.T) {
},
Commitments: CandidateCommitments{
UpwardMessages: []UpwardMessage{{1, 2, 3}},
HorizontalMessages: []OutboundHrmpMessage{},
NewValidationCode: &ValidationCode{1, 2, 3},
HeadData: headData{1, 2, 3},
ProcessedDownwardMessages: uint32(5),
Expand Down Expand Up @@ -103,20 +102,44 @@ func TestCollationProtocol(t *testing.T) {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
t.Run("marshal", func(t *testing.T) {
t.Parallel()

vdt_parent := NewCollationProtocol()
vdt_child := NewCollatorProtocolMessage()
vdt_parent := NewCollationProtocol()
vdt_child := NewCollatorProtocolMessage()

err := vdt_child.Set(c.enumValue)
require.NoError(t, err)
err := vdt_child.Set(c.enumValue)
require.NoError(t, err)

err = vdt_parent.Set(vdt_child)
require.NoError(t, err)
err = vdt_parent.Set(vdt_child)
require.NoError(t, err)

bytes, err := scale.Marshal(vdt_parent)
require.NoError(t, err)
bytes, err := scale.Marshal(vdt_parent)
require.NoError(t, err)

require.Equal(t, c.encodingValue, bytes)
require.Equal(t, c.encodingValue, bytes)
})

t.Run("unmarshal", func(t *testing.T) {
t.Parallel()

vdt_parent := NewCollationProtocol()
err := scale.Unmarshal(c.encodingValue, &vdt_parent)
require.NoError(t, err)

vdt_child_temp, err := vdt_parent.Value()
require.NoError(t, err)
require.Equal(t, uint(0), vdt_child_temp.Index())

vdt_child := vdt_child_temp.(CollatorProtocolMessage)
require.NoError(t, err)

actualData, err := vdt_child.Value()
require.NoError(t, err)

require.Equal(t, c.enumValue.Index(), actualData.Index())
require.EqualValues(t, c.enumValue, actualData)
})
})
}
}
5 changes: 5 additions & 0 deletions lib/parachain/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func NewStatement() Statement {
return Statement(vdt)
}

// New will enable scale to create new instance when needed
func (Statement) New() Statement {
return NewStatement()
}

// Set will set a value using the underlying varying data type
func (s *Statement) Set(val scale.VaryingDataTypeValue) (err error) {
vdt := scale.VaryingDataType(*s)
Expand Down
5 changes: 5 additions & 0 deletions lib/parachain/statement_distribution_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ func NewStatementDistributionMessage() StatementDistributionMessage {
return StatementDistributionMessage(vdt)
}

// New will enable scale to create new instance when needed
func (StatementDistributionMessage) New() StatementDistributionMessage {
return NewStatementDistributionMessage()
}

// Set will set a value using the underlying varying data type
func (sdm *StatementDistributionMessage) Set(val scale.VaryingDataTypeValue) (err error) {
vdt := scale.VaryingDataType(*sdm)
Expand Down
28 changes: 21 additions & 7 deletions lib/parachain/statement_distribution_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func TestStatementDistributionMessage(t *testing.T) {
},
Commitments: CandidateCommitments{
UpwardMessages: []UpwardMessage{{1, 2, 3}},
HorizontalMessages: []OutboundHrmpMessage{},
NewValidationCode: &ValidationCode{1, 2, 3},
HeadData: headData{1, 2, 3},
ProcessedDownwardMessages: uint32(5),
Expand Down Expand Up @@ -184,16 +183,31 @@ func TestStatementDistributionMessage(t *testing.T) {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
t.Run("marshal", func(t *testing.T) {
t.Parallel()

vtd := NewStatementDistributionMessage()
vdt := NewStatementDistributionMessage()
err := vdt.Set(c.enumValue)
require.NoError(t, err)

err := vtd.Set(c.enumValue)
require.NoError(t, err)
bytes, err := scale.Marshal(vdt)
require.NoError(t, err)

bytes, err := scale.Marshal(vtd)
require.NoError(t, err)
require.Equal(t, c.encodingValue, bytes)
})

require.Equal(t, c.encodingValue, bytes)
t.Run("unmarshal", func(t *testing.T) {
t.Parallel()

vdt := NewStatementDistributionMessage()
err := scale.Unmarshal(c.encodingValue, &vdt)
require.NoError(t, err)

actualData, err := vdt.Value()
require.NoError(t, err)

require.EqualValues(t, c.enumValue, actualData)
})
})
}
}
28 changes: 21 additions & 7 deletions lib/parachain/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func TestStatement(t *testing.T) {
},
Commitments: CandidateCommitments{
UpwardMessages: []UpwardMessage{{1, 2, 3}},
HorizontalMessages: []OutboundHrmpMessage{},
NewValidationCode: &ValidationCode{1, 2, 3},
HeadData: headData{1, 2, 3},
ProcessedDownwardMessages: uint32(5),
Expand Down Expand Up @@ -72,16 +71,31 @@ func TestStatement(t *testing.T) {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()
t.Run("marshal", func(t *testing.T) {
t.Parallel()

vtd := NewStatement()
vdt := NewStatement()
err := vdt.Set(c.enumValue)
require.NoError(t, err)

err := vtd.Set(c.enumValue)
require.NoError(t, err)
bytes, err := scale.Marshal(vdt)
require.NoError(t, err)

bytes, err := scale.Marshal(vtd)
require.NoError(t, err)
require.Equal(t, c.encodingValue, bytes)
})

require.Equal(t, c.encodingValue, bytes)
t.Run("unmarshal", func(t *testing.T) {
t.Parallel()

vdt := NewStatement()
err := scale.Unmarshal(c.encodingValue, &vdt)
require.NoError(t, err)

actualData, err := vdt.Value()
require.NoError(t, err)

require.EqualValues(t, c.enumValue, actualData)
})
})
}
}

0 comments on commit 137ad65

Please sign in to comment.