Skip to content

Commit

Permalink
chore(dot/parachain): rename statement (#3470)
Browse files Browse the repository at this point in the history
  • Loading branch information
axaysagathiya authored and edwardmack committed Jun 26, 2024
1 parent 8895cd8 commit cb0ff05
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 18 deletions.
6 changes: 3 additions & 3 deletions dot/parachain/collation_protocol_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ func TestCollationProtocol(t *testing.T) {
},
}

statementWithSeconded := NewStatement()
err := statementWithSeconded.Set(secondedEnumValue)
statementVDTWithSeconded := NewStatementVDT()
err := statementVDTWithSeconded.Set(secondedEnumValue)
require.NoError(t, err)

testCases := []struct {
Expand All @@ -96,7 +96,7 @@ func TestCollationProtocol(t *testing.T) {
enumValue: CollationSeconded{
Hash: hash5,
UncheckedSignedFullStatement: UncheckedSignedFullStatement{
Payload: statementWithSeconded,
Payload: statementVDTWithSeconded,
ValidatorIndex: parachaintypes.ValidatorIndex(5),
Signature: validatorSignature,
},
Expand Down
20 changes: 10 additions & 10 deletions dot/parachain/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,34 +11,34 @@ import (
"github.com/ChainSafe/gossamer/pkg/scale"
)

// Statement is a result of candidate validation. It could be either `Valid` or `Seconded`.
type Statement scale.VaryingDataType
// StatementVDT is a result of candidate validation. It could be either `Valid` or `Seconded`.
type StatementVDT scale.VaryingDataType

// NewStatement returns a new statement varying data type
func NewStatement() Statement {
// NewStatementVDT returns a new statement varying data type
func NewStatementVDT() StatementVDT {
vdt := scale.MustNewVaryingDataType(Seconded{}, Valid{})
return Statement(vdt)
return StatementVDT(vdt)
}

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

// Set will set a value using the underlying varying data type
func (s *Statement) Set(val scale.VaryingDataTypeValue) (err error) {
func (s *StatementVDT) Set(val scale.VaryingDataTypeValue) (err error) {
vdt := scale.VaryingDataType(*s)
err = vdt.Set(val)
if err != nil {
return fmt.Errorf("setting value to varying data type: %w", err)
}

*s = Statement(vdt)
*s = StatementVDT(vdt)
return nil
}

// Value returns the value from the underlying varying data type
func (s *Statement) Value() (scale.VaryingDataTypeValue, error) {
func (s *StatementVDT) Value() (scale.VaryingDataTypeValue, error) {
vdt := scale.VaryingDataType(*s)
return vdt.Value()
}
Expand Down
37 changes: 32 additions & 5 deletions dot/parachain/statement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package parachain

import (
"errors"
"math"
"testing"

parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"
Expand All @@ -12,6 +14,15 @@ import (
"github.com/stretchr/testify/require"
)

var ErrInvalidVayingDataTypeValue = errors.New(
"setting value to varying data type: unsupported VaryingDataTypeValue: {} (parachain.invalidVayingDataTypeValue)")

type invalidVayingDataTypeValue struct{}

func (invalidVayingDataTypeValue) Index() uint {
return math.MaxUint
}

func getDummyHash(num byte) common.Hash {
hash := common.Hash{}
for i := 0; i < 32; i++ {
Expand All @@ -20,7 +31,7 @@ func getDummyHash(num byte) common.Hash {
return hash
}

func TestStatement(t *testing.T) {
func TestStatementVDT(t *testing.T) {
t.Parallel()

var collatorID parachaintypes.CollatorID
Expand Down Expand Up @@ -60,16 +71,24 @@ func TestStatement(t *testing.T) {
name string
enumValue scale.VaryingDataTypeValue
encodingValue []byte
expectedErr error
}{
{
name: "Seconded",
enumValue: secondedEnumValue,
encodingValue: common.MustHexToBytes(testDataStatement["statementSeconded"]),
encodingValue: common.MustHexToBytes(testDataStatement["statementVDTSeconded"]),
expectedErr: nil,
},
{
name: "Valid",
enumValue: Valid{hash5},
encodingValue: common.MustHexToBytes("0x020505050505050505050505050505050505050505050505050505050505050505"),
expectedErr: nil,
},
{
name: "invalid struct",
enumValue: invalidVayingDataTypeValue{},
expectedErr: ErrInvalidVayingDataTypeValue,
},
}

Expand All @@ -80,10 +99,15 @@ func TestStatement(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
t.Parallel()

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

if c.expectedErr != nil {
require.EqualError(t, err, c.expectedErr.Error())
return
}

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

Expand All @@ -92,8 +116,11 @@ func TestStatement(t *testing.T) {

t.Run("unmarshal", func(t *testing.T) {
t.Parallel()
if c.expectedErr != nil {
return
}

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

Expand Down

0 comments on commit cb0ff05

Please sign in to comment.