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 kishansagathiya committed Jul 15, 2024
1 parent bc24da9 commit d602d6b
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 59 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.SetValue(secondedEnumValue)
statementVDTWithSeconded := NewStatementVDT()
err := statementVDTWithSeconded.SetValue(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
2 changes: 1 addition & 1 deletion dot/parachain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (s Service) run() {
s.Network.GossipMessage(&collationMessage)

statementDistributionLargeStatement := StatementDistribution{NewStatementDistributionMessage()}
err := statementDistributionLargeStatement.SetValue(SecondedStatementWithLargePayload{
err := statementDistributionLargeStatement.SetValue(LargePayload{
RelayParent: common.Hash{},
CandidateHash: CandidateHash{Value: common.Hash{}},
SignedBy: 5,
Expand Down
18 changes: 9 additions & 9 deletions dot/parachain/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ import (
)

// Statement is a result of candidate validation. It could be either `Valid` or `Seconded`.
type StatementValues interface {
type StatementVDTValues interface {
Valid | Seconded
}

type Statement struct {
type StatementVDT struct {
inner any
}

func setStatement[Value StatementValues](mvdt *Statement, value Value) {
func setStatement[Value StatementVDTValues](mvdt *StatementVDT, value Value) {
mvdt.inner = value
}

func (mvdt *Statement) SetValue(value any) (err error) {
func (mvdt *StatementVDT) SetValue(value any) (err error) {
switch value := value.(type) {
case Valid:
setStatement(mvdt, value)
Expand All @@ -39,7 +39,7 @@ func (mvdt *Statement) SetValue(value any) (err error) {
}
}

func (mvdt Statement) IndexValue() (index uint, value any, err error) {
func (mvdt StatementVDT) IndexValue() (index uint, value any, err error) {
switch mvdt.inner.(type) {
case Valid:
return 2, mvdt.inner, nil
Expand All @@ -51,12 +51,12 @@ func (mvdt Statement) IndexValue() (index uint, value any, err error) {
return 0, nil, scale.ErrUnsupportedVaryingDataTypeValue
}

func (mvdt Statement) Value() (value any, err error) {
func (mvdt StatementVDT) Value() (value any, err error) {
_, value, err = mvdt.IndexValue()
return
}

func (mvdt Statement) ValueAt(index uint) (value any, err error) {
func (mvdt StatementVDT) ValueAt(index uint) (value any, err error) {
switch index {
case 2:
return *new(Valid), nil
Expand All @@ -69,8 +69,8 @@ func (mvdt Statement) ValueAt(index uint) (value any, err error) {
}

// NewStatement returns a new statement varying data type
func NewStatement() Statement {
return Statement{}
func NewStatementVDT() StatementVDT {
return StatementVDT{}
}

// Seconded represents a statement that a validator seconds a candidate.
Expand Down
24 changes: 12 additions & 12 deletions dot/parachain/statement_distribution_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

type StatementDistributionMessageValues interface {
SignedFullStatement | SecondedStatementWithLargePayload
Statement | LargePayload
}

// StatementDistributionMessage represents network messages used by the statement distribution subsystem
Expand All @@ -26,11 +26,11 @@ func setStatementDistributionMessage[Value StatementDistributionMessageValues](m

func (mvdt *StatementDistributionMessage) SetValue(value any) (err error) {
switch value := value.(type) {
case SignedFullStatement:
case Statement:
setStatementDistributionMessage(mvdt, value)
return

case SecondedStatementWithLargePayload:
case LargePayload:
setStatementDistributionMessage(mvdt, value)
return

Expand All @@ -41,10 +41,10 @@ func (mvdt *StatementDistributionMessage) SetValue(value any) (err error) {

func (mvdt StatementDistributionMessage) IndexValue() (index uint, value any, err error) {
switch mvdt.inner.(type) {
case SignedFullStatement:
case Statement:
return 0, mvdt.inner, nil

case SecondedStatementWithLargePayload:
case LargePayload:
return 1, mvdt.inner, nil

}
Expand All @@ -59,10 +59,10 @@ func (mvdt StatementDistributionMessage) Value() (value any, err error) {
func (mvdt StatementDistributionMessage) ValueAt(index uint) (value any, err error) {
switch index {
case 0:
return *new(SignedFullStatement), nil
return *new(Statement), nil

case 1:
return *new(SecondedStatementWithLargePayload), nil
return *new(LargePayload), nil

}
return nil, scale.ErrUnknownVaryingDataTypeValue
Expand All @@ -73,24 +73,24 @@ func NewStatementDistributionMessage() StatementDistributionMessage {
return StatementDistributionMessage{}
}

// SignedFullStatement represents a signed full statement under a given relay-parent.
type SignedFullStatement struct {
// Statement represents a signed full statement under a given relay-parent.
type Statement struct {
Hash common.Hash `scale:"1"`
UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"`
}

// SecondedStatementWithLargePayload represents Seconded statement with large payload
// LargePayload represents Seconded statement with large payload
// (e.g. containing a runtime upgrade).
//
// We only gossip the hash in that case, actual payloads can be fetched from sending node
// via request/response.
type SecondedStatementWithLargePayload StatementMetadata
type LargePayload StatementMetadata

// UncheckedSignedFullStatement is a Variant of `SignedFullStatement` where the signature has not yet been verified.
type UncheckedSignedFullStatement struct {
// The payload is part of the signed data. The rest is the signing context,
// which is known both at signing and at validation.
Payload Statement `scale:"1"`
Payload StatementVDT `scale:"1"`

// The index of the validator signing this statement.
ValidatorIndex parachaintypes.ValidatorIndex `scale:"2"`
Expand Down
50 changes: 32 additions & 18 deletions dot/parachain/statement_distribution_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ func TestStatementDistributionMessage(t *testing.T) {

hash5 := getDummyHash(5)

statementWithValid := NewStatement()
err := statementWithValid.SetValue(Valid{hash5})
statementVDTWithValid := NewStatementVDT()
err := statementVDTWithValid.SetValue(Valid{hash5})
require.NoError(t, err)

secondedEnumValue := Seconded{
Expand All @@ -71,29 +71,29 @@ func TestStatementDistributionMessage(t *testing.T) {
},
}

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

signedFullStatementWithValid := SignedFullStatement{
signedFullStatementWithValid := Statement{
Hash: hash5,
UncheckedSignedFullStatement: UncheckedSignedFullStatement{
Payload: statementWithValid,
Payload: statementVDTWithValid,
ValidatorIndex: parachaintypes.ValidatorIndex(5),
Signature: validatorSignature,
},
}

signedFullStatementWithSeconded := SignedFullStatement{
signedFullStatementWithSeconded := Statement{
Hash: hash5,
UncheckedSignedFullStatement: UncheckedSignedFullStatement{
Payload: statementWithSeconded,
Payload: statementVDTWithSeconded,
ValidatorIndex: parachaintypes.ValidatorIndex(5),
Signature: validatorSignature,
},
}

secondedStatementWithLargePayload := SecondedStatementWithLargePayload{
largePayload := LargePayload{
RelayParent: hash5,
CandidateHash: CandidateHash{hash5},
SignedBy: parachaintypes.ValidatorIndex(5),
Expand All @@ -104,6 +104,7 @@ func TestStatementDistributionMessage(t *testing.T) {
name string
enumValue any
encodingValue []byte
expectedErr error
}{
// expected encoding is generated by running rust test code:
// fn statement_distribution_message_encode() {
Expand All @@ -119,7 +120,7 @@ func TestStatementDistributionMessage(t *testing.T) {
// statement_valid, ValidatorIndex(5), val_sign.clone());
// let sdm_statement_valid = StatementDistributionMessage::Statement(
// hash1, unchecked_signed_full_statement_valid);
// println!("encode SignedFullStatement with valid statement => {:?}\n\n", sdm_statement_valid.encode());
// println!("encode Statement with valid statementVDT => {:?}\n\n", sdm_statement_valid.encode());

// let collator_result = sr25519::Public::from_string(
// "0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147");
Expand Down Expand Up @@ -157,32 +158,37 @@ func TestStatementDistributionMessage(t *testing.T) {
// statement_second, ValidatorIndex(5), val_sign.clone());
// let sdm_statement_second = StatementDistributionMessage::Statement(
// hash1, unchecked_signed_full_statement_second);
// println!("encode SignedFullStatement with Seconded statement => {:?}\n\n", sdm_statement_second.encode());
// println!("encode Statement with Seconded statementVDT => {:?}\n\n", sdm_statement_second.encode());

// let sdm_large_statement = StatementDistributionMessage::LargeStatement(StatementMetadata{
// relay_parent: hash1,
// candidate_hash: CandidateHash(hash1),
// signed_by: ValidatorIndex(5_u32),
// signature: val_sign.clone(),
// });
// println!("encode SecondedStatementWithLargePayload => {:?}\n\n", sdm_large_statement.encode());
// println!("encode largePayload => {:?}\n\n", sdm_large_statement.encode());
// }

{
name: "SignedFullStatement with valid statement",
name: "Statement with valid statementVDT",
enumValue: signedFullStatementWithValid,
encodingValue: common.MustHexToBytes(testDataStatement["sfsValid"]),
encodingValue: common.MustHexToBytes(testDataStatement["statementValid"]),
},
{
name: "SignedFullStatement with Seconded statement",
name: "Statement with Seconded statementVDT",
enumValue: signedFullStatementWithSeconded,
encodingValue: common.MustHexToBytes(testDataStatement["sfsSeconded"]),
encodingValue: common.MustHexToBytes(testDataStatement["statementSeconded"]),
},
{
name: "Seconded Statement With LargePayload",
enumValue: secondedStatementWithLargePayload,
enumValue: largePayload,
encodingValue: common.MustHexToBytes(testDataStatement["statementWithLargePayload"]),
},
{
name: "invalid struct",
enumValue: invalidVayingDataTypeValue{},
expectedErr: ErrInvalidVayingDataTypeValue,
},
}

for _, c := range testCases {
Expand All @@ -194,8 +200,13 @@ func TestStatementDistributionMessage(t *testing.T) {

vdt := NewStatementDistributionMessage()
err := vdt.SetValue(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 @@ -204,6 +215,9 @@ func TestStatementDistributionMessage(t *testing.T) {

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

vdt := NewStatementDistributionMessage()
err := scale.Unmarshal(c.encodingValue, &vdt)
Expand Down
36 changes: 31 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,14 @@ import (
"github.com/stretchr/testify/require"
)

var ErrInvalidVayingDataTypeValue = errors.New("unsupported type")

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 +30,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 +70,24 @@ func TestStatement(t *testing.T) {
name string
enumValue any
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 +98,15 @@ func TestStatement(t *testing.T) {
t.Run("marshal", func(t *testing.T) {
t.Parallel()

vdt := NewStatement()
vdt := NewStatementVDT()
err := vdt.SetValue(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 +115,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
Loading

0 comments on commit d602d6b

Please sign in to comment.