From 8d15217bc4575440f0326f66aff9e8ce3fbe7384 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Tue, 6 Jun 2023 10:48:33 +0530 Subject: [PATCH 01/28] feat(parachain): add types (WIP) --- parachain/compact_statement.go | 39 ++++++ parachain/statement.go | 130 ++++++++++++++++++++ parachain/statement_distribution_message.go | 73 +++++++++++ 3 files changed, 242 insertions(+) create mode 100644 parachain/compact_statement.go create mode 100644 parachain/statement.go create mode 100644 parachain/statement_distribution_message.go diff --git a/parachain/compact_statement.go b/parachain/compact_statement.go new file mode 100644 index 0000000000..365ee27d7c --- /dev/null +++ b/parachain/compact_statement.go @@ -0,0 +1,39 @@ +package parachain + +import ( + "fmt" + + "github.com/ChainSafe/gossamer/pkg/scale" +) + +type CompactStatement scale.VaryingDataType + +// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +func (cs *CompactStatement) Set(val scale.VaryingDataTypeValue) (err error) { + vdt := scale.VaryingDataType(*cs) + err = vdt.Set(val) + if err != nil { + return fmt.Errorf("setting value to varying data type: %w", err) + } + + *cs = CompactStatement(vdt) + return nil +} + +// Value returns the value from the underlying VaryingDataType +func (cs *CompactStatement) Value() (scale.VaryingDataTypeValue, error) { + vdt := scale.VaryingDataType(*cs) + return vdt.Value() +} + +func NewCompactStatement() CompactStatement { + vdt := scale.MustNewVaryingDataType(ParachainCandidateProposal{}, Valid{}) + return CompactStatement(vdt) +} + +// Proposal of a parachain candidate. +type ParachainCandidateProposal CandidateHash + +func (p ParachainCandidateProposal) Index() uint { + return 1 +} diff --git a/parachain/statement.go b/parachain/statement.go new file mode 100644 index 0000000000..af0addda00 --- /dev/null +++ b/parachain/statement.go @@ -0,0 +1,130 @@ +package parachain + +import ( + "fmt" + + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/lib/crypto/sr25519" + "github.com/ChainSafe/gossamer/pkg/scale" +) + +type Statement scale.VaryingDataType + +func NewStatement() Statement { + vdt := scale.MustNewVaryingDataType(Seconded{}, Valid{}) + return Statement(vdt) +} + +// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +func (s *Statement) 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) + return nil +} + +// Value returns the value from the underlying VaryingDataType +func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { + vdt := scale.VaryingDataType(*s) + return vdt.Value() +} + +type Seconded CommittedCandidateReceipt + +func (s Seconded) Index() uint { + return 1 +} + +type Valid CandidateHash + +func (v Valid) Index() uint { + return 2 +} + +type CommittedCandidateReceipt struct { + Descriptor CandidateDescriptor `scale:"1"` + Commitments CandidateCommitments `scale:"2"` +} + +type CandidateHash struct { + Value common.Hash `scale:"1"` +} + +// CandidateDescriptor is a unique descriptor of the candidate receipt. +type CandidateDescriptor struct { + // The ID of the para this is a candidate for. + ParaID uint32 `scale:"1"` + + // RelayParent is the hash of the relay-chain block this should be executed in + // the context of. + // NOTE: the fact that the hash includes this value means that code depends + // on this for deduplication. Removing this field is likely to break things. + RelayParent common.Hash `scale:"2"` + + // Collator is the collator's sr25519 public key. + Collator CollatorID `scale:"3"` + + // PersistedValidationDataHash is the blake2-256 hash of the persisted validation data. This is extra data derived from + // relay-chain state which may vary based on bitfields included before the candidate. + // Thus, it cannot be derived entirely from the relay-parent. + PersistedValidationDataHash common.Hash `scale:"4"` + + // PovHash is the hash of the `pov-block`. + PovHash common.Hash `scale:"5"` + // ErasureRoot is the root of a block's erasure encoding Merkle tree. + ErasureRoot common.Hash `scale:"6"` + + // Signature on blake2-256 of components of this receipt: + // The parachain index, the relay parent, the validation data hash, and the `pov_hash`. + // this is basically sr25519::Signature + Signature CollatorSignature `scale:"7"` + + // ParaHead is the hash of the para header that is being generated by this candidate. + ParaHead common.Hash `scale:"8"` + // ValidationCodeHash is the blake2-256 hash of the validation code bytes. + ValidationCodeHash ValidationCodeHash `scale:"9"` +} + +// CollatorID represents the public key of a collator +type CollatorID [sr25519.PublicKeyLength]byte + +// CollatorSignature is the signature on a candidate's block data signed by a collator. +type CollatorSignature [sr25519.SignatureLength]byte + +// ValidationCodeHash is the blake2-256 hash of the validation code bytes. +type ValidationCodeHash common.Hash + +// CandidateCommitments are Commitments made in a `CandidateReceipt`. Many of these are outputs of validation. +type CandidateCommitments struct { + // Messages destined to be interpreted by the Relay chain itself. + UpwardMessages []UpwardMessage `scale:"1"` + // Horizontal messages sent by the parachain. + HorizontalMessages []OutboundHrmpMessage `scale:"2"` + // New validation code. + NewValidationCode ValidationCode `scale:"3"` + // The head-data produced as a result of execution. + HeadData headData `scale:"4"` + // The number of messages processed from the DMQ. + ProcessedDownwardMessages uint32 `scale:"5"` + // The mark which specifies the block number up to which all inbound HRMP messages are processed. + HrmpWatermark uint32 `scale:"6"` +} + +// UpwardMessage A message from a parachain to its Relay Chain. +type UpwardMessage []byte + +// OutboundHrmpMessage is an HRMP message seen from the perspective of a sender. +type OutboundHrmpMessage struct { + Recipient uint32 `scale:"1"` + Data []byte `scale:"2"` +} + +// ValidationCode is Parachain validation code. +type ValidationCode []byte + +// headData is Parachain head data included in the chain. +type headData []byte diff --git a/parachain/statement_distribution_message.go b/parachain/statement_distribution_message.go new file mode 100644 index 0000000000..081afbd133 --- /dev/null +++ b/parachain/statement_distribution_message.go @@ -0,0 +1,73 @@ +package parachain + +import ( + "fmt" + + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/pkg/scale" +) + +// Network messages used by the statement distribution subsystem. +type StatementDistributionMessage scale.VaryingDataType + +func NewStatementDistributionMessage() StatementDistributionMessage { + vdt := scale.MustNewVaryingDataType(SignedFullStatement{}, SecondedStatementWithLargePayload{}) + return StatementDistributionMessage(vdt) +} + +// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +func (sdm *StatementDistributionMessage) Set(val scale.VaryingDataTypeValue) (err error) { + vdt := scale.VaryingDataType(*sdm) + err = vdt.Set(val) + if err != nil { + return fmt.Errorf("setting value to varying data type: %w", err) + } + + *sdm = StatementDistributionMessage(vdt) + return nil +} + +// Value returns the value from the underlying VaryingDataType +func (sdm *StatementDistributionMessage) Value() (scale.VaryingDataTypeValue, error) { + vdt := scale.VaryingDataType(*sdm) + return vdt.Value() +} + +// A signed full statement under a given relay-parent. +type SignedFullStatement struct { + Hash common.Hash `scale:"1"` + UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` +} + +func (s SignedFullStatement) Index() uint { + return 0 +} + +type SecondedStatementWithLargePayload StatementMetadata + +func (l SecondedStatementWithLargePayload) Index() uint { + return 1 +} + +type UncheckedSignedFullStatement struct { + Payload Statement `scale:"1"` + ValidatorIndex ValidatorIndex `scale:"2"` + Signature ValidatorSignature `scale:"3"` + RealPayload CompactStatement `scale:"4"` // changes needed +} + +type PhantomData struct{} + +type ValidatorIndex struct { + Value uint32 +} + +type StatementMetadata struct { + RelayParent common.Hash `scale:"1"` + CandidateHash CandidateHash `scale:"2"` + SignedBy ValidatorIndex `scale:"3"` + Signature ValidatorSignature `scale:"4"` +} + +type ValidatorSignature Signature +type Signature [64]byte From 8503e7527fa74adb4af83d489efdd9a61fca8295 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Fri, 9 Jun 2023 17:56:18 +0530 Subject: [PATCH 02/28] draft1 --- parachain/statement_test.go | 50 +++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 parachain/statement_test.go diff --git a/parachain/statement_test.go b/parachain/statement_test.go new file mode 100644 index 0000000000..d4bccf1752 --- /dev/null +++ b/parachain/statement_test.go @@ -0,0 +1,50 @@ +package parachain + +import ( + "testing" + + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/stretchr/testify/require" +) + +func TestStatement(t *testing.T) { + hash1 := common.Hash{} + for i := 0; i < 32; i++ { + hash1[i] = 1 + } + + testCases := []struct { + name string + enumValue scale.VaryingDataTypeValue + encodingValue []byte + }{ + { + name: "Seconded", + enumValue: Seconded{}, + encodingValue: []byte{}, + }, + { + name: "Valid", + enumValue: Valid{Value: hash1}, + encodingValue: []byte{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + }, + } + + for _, c := range testCases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + + vtd := NewStatement() + + err := vtd.Set(c.enumValue) + require.NoError(t, err) + + bytes, err := scale.Marshal(vtd) + require.NoError(t, err) + + require.Equal(t, c.encodingValue, bytes) + }) + } +} From a82b3101af1f8cee63578391e8d9266c002eedbd Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 12 Jun 2023 17:41:53 +0530 Subject: [PATCH 03/28] add statement varying type --- parachain/statement.go | 2 +- parachain/statement_test.go | 29 +++++++++++++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/parachain/statement.go b/parachain/statement.go index af0addda00..d0ceb7cae8 100644 --- a/parachain/statement.go +++ b/parachain/statement.go @@ -105,7 +105,7 @@ type CandidateCommitments struct { // Horizontal messages sent by the parachain. HorizontalMessages []OutboundHrmpMessage `scale:"2"` // New validation code. - NewValidationCode ValidationCode `scale:"3"` + NewValidationCode *ValidationCode `scale:"3"` // The head-data produced as a result of execution. HeadData headData `scale:"4"` // The number of messages processed from the DMQ. diff --git a/parachain/statement_test.go b/parachain/statement_test.go index d4bccf1752..7ef02b4a2f 100644 --- a/parachain/statement_test.go +++ b/parachain/statement_test.go @@ -14,6 +14,31 @@ func TestStatement(t *testing.T) { hash1[i] = 1 } + var dummyCollatorID CollatorID + var dummyCollatorSignature CollatorSignature + + secondedEnumValue := Seconded{ + Descriptor: CandidateDescriptor{ + ParaID: uint32(1), + RelayParent: hash1, + Collator: dummyCollatorID, + PersistedValidationDataHash: hash1, + PovHash: hash1, + ErasureRoot: hash1, + Signature: dummyCollatorSignature, + ParaHead: hash1, + ValidationCodeHash: ValidationCodeHash(hash1), + }, + Commitments: CandidateCommitments{ + UpwardMessages: []UpwardMessage{{1, 2, 3}}, + HorizontalMessages: []OutboundHrmpMessage{}, + NewValidationCode: &ValidationCode{1, 2, 3}, + HeadData: headData{1, 2, 3}, + ProcessedDownwardMessages: uint32(5), + HrmpWatermark: uint32(0), + }, + } + testCases := []struct { name string enumValue scale.VaryingDataTypeValue @@ -21,8 +46,8 @@ func TestStatement(t *testing.T) { }{ { name: "Seconded", - enumValue: Seconded{}, - encodingValue: []byte{}, + enumValue: secondedEnumValue, + encodingValue: []byte{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0}, }, { name: "Valid", From fac5dd47d4096a339ea4b40fa2241b956de84b77 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Tue, 13 Jun 2023 18:43:54 +0530 Subject: [PATCH 04/28] add test --- parachain/compact_statement.go | 39 ------- parachain/statement_distribution_message.go | 2 +- .../statement_distribution_message_test.go | 110 ++++++++++++++++++ parachain/statement_test.go | 13 ++- 4 files changed, 120 insertions(+), 44 deletions(-) delete mode 100644 parachain/compact_statement.go create mode 100644 parachain/statement_distribution_message_test.go diff --git a/parachain/compact_statement.go b/parachain/compact_statement.go deleted file mode 100644 index 365ee27d7c..0000000000 --- a/parachain/compact_statement.go +++ /dev/null @@ -1,39 +0,0 @@ -package parachain - -import ( - "fmt" - - "github.com/ChainSafe/gossamer/pkg/scale" -) - -type CompactStatement scale.VaryingDataType - -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType -func (cs *CompactStatement) Set(val scale.VaryingDataTypeValue) (err error) { - vdt := scale.VaryingDataType(*cs) - err = vdt.Set(val) - if err != nil { - return fmt.Errorf("setting value to varying data type: %w", err) - } - - *cs = CompactStatement(vdt) - return nil -} - -// Value returns the value from the underlying VaryingDataType -func (cs *CompactStatement) Value() (scale.VaryingDataTypeValue, error) { - vdt := scale.VaryingDataType(*cs) - return vdt.Value() -} - -func NewCompactStatement() CompactStatement { - vdt := scale.MustNewVaryingDataType(ParachainCandidateProposal{}, Valid{}) - return CompactStatement(vdt) -} - -// Proposal of a parachain candidate. -type ParachainCandidateProposal CandidateHash - -func (p ParachainCandidateProposal) Index() uint { - return 1 -} diff --git a/parachain/statement_distribution_message.go b/parachain/statement_distribution_message.go index 081afbd133..705ce5d951 100644 --- a/parachain/statement_distribution_message.go +++ b/parachain/statement_distribution_message.go @@ -53,7 +53,7 @@ type UncheckedSignedFullStatement struct { Payload Statement `scale:"1"` ValidatorIndex ValidatorIndex `scale:"2"` Signature ValidatorSignature `scale:"3"` - RealPayload CompactStatement `scale:"4"` // changes needed + // RealPayload CompactStatement `scale:"4"` // changes needed } type PhantomData struct{} diff --git a/parachain/statement_distribution_message_test.go b/parachain/statement_distribution_message_test.go new file mode 100644 index 0000000000..2a398fdb3e --- /dev/null +++ b/parachain/statement_distribution_message_test.go @@ -0,0 +1,110 @@ +package parachain + +import ( + "testing" + + "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/stretchr/testify/require" +) + +func TestStatementDistributionMessage(t *testing.T) { + // TODO: fill the value + var dummyValidatorSignature ValidatorSignature + var dummyCollatorID CollatorID + var dummyCollatorSignature CollatorSignature + + hash1 := getDummyHash(1) + + statementWithValid := NewStatement() + err := statementWithValid.Set(Valid{hash1}) + require.NoError(t, err) + + secondedEnumValue := Seconded{ + Descriptor: CandidateDescriptor{ + ParaID: uint32(1), + RelayParent: hash1, + Collator: dummyCollatorID, + PersistedValidationDataHash: hash1, + PovHash: hash1, + ErasureRoot: hash1, + Signature: dummyCollatorSignature, + ParaHead: hash1, + ValidationCodeHash: ValidationCodeHash(hash1), + }, + Commitments: CandidateCommitments{ + UpwardMessages: []UpwardMessage{{1, 2, 3}}, + HorizontalMessages: []OutboundHrmpMessage{}, + NewValidationCode: &ValidationCode{1, 2, 3}, + HeadData: headData{1, 2, 3}, + ProcessedDownwardMessages: uint32(5), + HrmpWatermark: uint32(0), + }, + } + + statementWithSeconded := NewStatement() + err = statementWithSeconded.Set(secondedEnumValue) + require.NoError(t, err) + + signedFullStatementWithValid := SignedFullStatement{ + Hash: hash1, + UncheckedSignedFullStatement: UncheckedSignedFullStatement{ + Payload: statementWithValid, + ValidatorIndex: ValidatorIndex{5}, + Signature: dummyValidatorSignature, + }, + } + + signedFullStatementWithSeconded := SignedFullStatement{ + Hash: hash1, + UncheckedSignedFullStatement: UncheckedSignedFullStatement{ + Payload: statementWithSeconded, + ValidatorIndex: ValidatorIndex{5}, + Signature: dummyValidatorSignature, + }, + } + + secondedStatementWithLargePayload := SecondedStatementWithLargePayload{ + RelayParent: hash1, + CandidateHash: CandidateHash{hash1}, + SignedBy: ValidatorIndex{5}, + Signature: dummyValidatorSignature, + } + testCases := []struct { + name string + enumValue scale.VaryingDataTypeValue + encodingValue []byte + }{ + { + name: "SignedFullStatement with valid statement", + enumValue: signedFullStatementWithValid, + encodingValue: []byte{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + { + name: "SignedFullStatement with Seconded statement", + enumValue: signedFullStatementWithSeconded, + encodingValue: []byte{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + { + name: "SecondedStatementWithLargePayload", + enumValue: secondedStatementWithLargePayload, + encodingValue: []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + }, + } + + for _, c := range testCases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + + vtd := NewStatementDistributionMessage() + + err := vtd.Set(c.enumValue) + require.NoError(t, err) + + bytes, err := scale.Marshal(vtd) + require.NoError(t, err) + + require.Equal(t, c.encodingValue, bytes) + }) + } +} diff --git a/parachain/statement_test.go b/parachain/statement_test.go index 7ef02b4a2f..5b9f4f285e 100644 --- a/parachain/statement_test.go +++ b/parachain/statement_test.go @@ -8,14 +8,19 @@ import ( "github.com/stretchr/testify/require" ) -func TestStatement(t *testing.T) { - hash1 := common.Hash{} +func getDummyHash(num byte) common.Hash { + hash := common.Hash{} for i := 0; i < 32; i++ { - hash1[i] = 1 + hash[i] = num } + return hash +} +func TestStatement(t *testing.T) { + // TODO: fill the value var dummyCollatorID CollatorID var dummyCollatorSignature CollatorSignature + hash1 := getDummyHash(1) secondedEnumValue := Seconded{ Descriptor: CandidateDescriptor{ @@ -51,7 +56,7 @@ func TestStatement(t *testing.T) { }, { name: "Valid", - enumValue: Valid{Value: hash1}, + enumValue: Valid{hash1}, encodingValue: []byte{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, }, } From 4fcb2d00ab2e25ad33eacea3751b93120add6641 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Wed, 14 Jun 2023 18:57:07 +0530 Subject: [PATCH 05/28] use actual values instead of empty array in test --- .../statement_distribution_message_test.go | 23 ++++++++++++++----- parachain/statement_test.go | 17 ++++++++++---- 2 files changed, 30 insertions(+), 10 deletions(-) diff --git a/parachain/statement_distribution_message_test.go b/parachain/statement_distribution_message_test.go index 2a398fdb3e..334ea4bd08 100644 --- a/parachain/statement_distribution_message_test.go +++ b/parachain/statement_distribution_message_test.go @@ -3,17 +3,28 @@ package parachain import ( "testing" + "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" ) func TestStatementDistributionMessage(t *testing.T) { - // TODO: fill the value + var dummyCollatorSignature CollatorSignature + tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") + for i, b := range tempSignature { + dummyCollatorSignature[i] = b + } + var dummyValidatorSignature ValidatorSignature + copy(dummyValidatorSignature[:], tempSignature) + var dummyCollatorID CollatorID - var dummyCollatorSignature CollatorSignature + tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") + for i, b := range tempCollatID { + dummyCollatorID[i] = b + } - hash1 := getDummyHash(1) + hash1 := getDummyHash(5) statementWithValid := NewStatement() err := statementWithValid.Set(Valid{hash1}) @@ -77,17 +88,17 @@ func TestStatementDistributionMessage(t *testing.T) { { name: "SignedFullStatement with valid statement", enumValue: signedFullStatementWithValid, - encodingValue: []byte{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + encodingValue: []byte{0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, }, { name: "SignedFullStatement with Seconded statement", enumValue: signedFullStatementWithSeconded, - encodingValue: []byte{0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + encodingValue: []byte{0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 72, 33, 91, 157, 50, 38, 1, 229, 177, 169, 81, 100, 206, 160, 220, 70, 38, 245, 69, 249, 131, 67, 208, 127, 21, 81, 235, 149, 67, 196, 177, 71, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, }, { name: "SecondedStatementWithLargePayload", enumValue: secondedStatementWithLargePayload, - encodingValue: []byte{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, + encodingValue: []byte{1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, }, } diff --git a/parachain/statement_test.go b/parachain/statement_test.go index 5b9f4f285e..6c0cdfbb5f 100644 --- a/parachain/statement_test.go +++ b/parachain/statement_test.go @@ -17,10 +17,19 @@ func getDummyHash(num byte) common.Hash { } func TestStatement(t *testing.T) { - // TODO: fill the value var dummyCollatorID CollatorID + tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") + for i, b := range tempCollatID { + dummyCollatorID[i] = b + } + var dummyCollatorSignature CollatorSignature - hash1 := getDummyHash(1) + tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") + for i, b := range tempSignature { + dummyCollatorSignature[i] = b + } + + hash1 := getDummyHash(5) secondedEnumValue := Seconded{ Descriptor: CandidateDescriptor{ @@ -52,12 +61,12 @@ func TestStatement(t *testing.T) { { name: "Seconded", enumValue: secondedEnumValue, - encodingValue: []byte{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0}, + encodingValue: []byte{1, 1, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 72, 33, 91, 157, 50, 38, 1, 229, 177, 169, 81, 100, 206, 160, 220, 70, 38, 245, 69, 249, 131, 67, 208, 127, 21, 81, 235, 149, 67, 196, 177, 71, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0}, }, { name: "Valid", enumValue: Valid{hash1}, - encodingValue: []byte{2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, + encodingValue: []byte{2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, }, } From cd96dafbfe7f0053f3a9021a033b7794266a93ac Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Thu, 15 Jun 2023 13:20:39 +0530 Subject: [PATCH 06/28] lint --- .../statement_distribution_message_test.go | 36 +++++++++---------- parachain/statement_test.go | 24 ++++++------- 2 files changed, 28 insertions(+), 32 deletions(-) diff --git a/parachain/statement_distribution_message_test.go b/parachain/statement_distribution_message_test.go index 334ea4bd08..b20e8c9131 100644 --- a/parachain/statement_distribution_message_test.go +++ b/parachain/statement_distribution_message_test.go @@ -9,20 +9,18 @@ import ( ) func TestStatementDistributionMessage(t *testing.T) { - var dummyCollatorSignature CollatorSignature - tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") - for i, b := range tempSignature { - dummyCollatorSignature[i] = b - } + t.Parallel() + + var collatorSignature CollatorSignature + tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll + copy(collatorSignature[:], tempSignature) - var dummyValidatorSignature ValidatorSignature - copy(dummyValidatorSignature[:], tempSignature) + var validatorSignature ValidatorSignature + copy(validatorSignature[:], tempSignature) - var dummyCollatorID CollatorID + var collatorID CollatorID tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") - for i, b := range tempCollatID { - dummyCollatorID[i] = b - } + copy(collatorID[:], tempCollatID) hash1 := getDummyHash(5) @@ -34,11 +32,11 @@ func TestStatementDistributionMessage(t *testing.T) { Descriptor: CandidateDescriptor{ ParaID: uint32(1), RelayParent: hash1, - Collator: dummyCollatorID, + Collator: collatorID, PersistedValidationDataHash: hash1, PovHash: hash1, ErasureRoot: hash1, - Signature: dummyCollatorSignature, + Signature: collatorSignature, ParaHead: hash1, ValidationCodeHash: ValidationCodeHash(hash1), }, @@ -61,7 +59,7 @@ func TestStatementDistributionMessage(t *testing.T) { UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithValid, ValidatorIndex: ValidatorIndex{5}, - Signature: dummyValidatorSignature, + Signature: validatorSignature, }, } @@ -70,7 +68,7 @@ func TestStatementDistributionMessage(t *testing.T) { UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithSeconded, ValidatorIndex: ValidatorIndex{5}, - Signature: dummyValidatorSignature, + Signature: validatorSignature, }, } @@ -78,7 +76,7 @@ func TestStatementDistributionMessage(t *testing.T) { RelayParent: hash1, CandidateHash: CandidateHash{hash1}, SignedBy: ValidatorIndex{5}, - Signature: dummyValidatorSignature, + Signature: validatorSignature, } testCases := []struct { name string @@ -88,17 +86,17 @@ func TestStatementDistributionMessage(t *testing.T) { { name: "SignedFullStatement with valid statement", enumValue: signedFullStatementWithValid, - encodingValue: []byte{0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, + encodingValue: []byte{0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, //nolint:lll }, { name: "SignedFullStatement with Seconded statement", enumValue: signedFullStatementWithSeconded, - encodingValue: []byte{0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 72, 33, 91, 157, 50, 38, 1, 229, 177, 169, 81, 100, 206, 160, 220, 70, 38, 245, 69, 249, 131, 67, 208, 127, 21, 81, 235, 149, 67, 196, 177, 71, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, + encodingValue: []byte{0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 72, 33, 91, 157, 50, 38, 1, 229, 177, 169, 81, 100, 206, 160, 220, 70, 38, 245, 69, 249, 131, 67, 208, 127, 21, 81, 235, 149, 67, 196, 177, 71, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, //nolint:lll }, { name: "SecondedStatementWithLargePayload", enumValue: secondedStatementWithLargePayload, - encodingValue: []byte{1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, + encodingValue: []byte{1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, //nolint:lll }, } diff --git a/parachain/statement_test.go b/parachain/statement_test.go index 6c0cdfbb5f..f0b3633252 100644 --- a/parachain/statement_test.go +++ b/parachain/statement_test.go @@ -17,17 +17,15 @@ func getDummyHash(num byte) common.Hash { } func TestStatement(t *testing.T) { - var dummyCollatorID CollatorID + t.Parallel() + + var collatorID CollatorID tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") - for i, b := range tempCollatID { - dummyCollatorID[i] = b - } + copy(collatorID[:], tempCollatID) - var dummyCollatorSignature CollatorSignature - tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") - for i, b := range tempSignature { - dummyCollatorSignature[i] = b - } + var collatorSignature CollatorSignature + tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll + copy(collatorSignature[:], tempSignature) hash1 := getDummyHash(5) @@ -35,11 +33,11 @@ func TestStatement(t *testing.T) { Descriptor: CandidateDescriptor{ ParaID: uint32(1), RelayParent: hash1, - Collator: dummyCollatorID, + Collator: collatorID, PersistedValidationDataHash: hash1, PovHash: hash1, ErasureRoot: hash1, - Signature: dummyCollatorSignature, + Signature: collatorSignature, ParaHead: hash1, ValidationCodeHash: ValidationCodeHash(hash1), }, @@ -61,12 +59,12 @@ func TestStatement(t *testing.T) { { name: "Seconded", enumValue: secondedEnumValue, - encodingValue: []byte{1, 1, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 72, 33, 91, 157, 50, 38, 1, 229, 177, 169, 81, 100, 206, 160, 220, 70, 38, 245, 69, 249, 131, 67, 208, 127, 21, 81, 235, 149, 67, 196, 177, 71, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0}, + encodingValue: []byte{1, 1, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 72, 33, 91, 157, 50, 38, 1, 229, 177, 169, 81, 100, 206, 160, 220, 70, 38, 245, 69, 249, 131, 67, 208, 127, 21, 81, 235, 149, 67, 196, 177, 71, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0}, //nolint:lll }, { name: "Valid", enumValue: Valid{hash1}, - encodingValue: []byte{2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, + encodingValue: []byte{2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, //nolint:lll }, } From 9fa5fd817f3c3d3117f0bb022620c8372a7ec585 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Fri, 16 Jun 2023 16:38:22 +0530 Subject: [PATCH 07/28] add CollationProtocol varying type --- parachain/collation_protocol.go | 27 +++++++ parachain/collation_protocol_test.go | 102 +++++++++++++++++++++++++ parachain/collator_protocol_message.go | 63 +++++++++++++++ 3 files changed, 192 insertions(+) create mode 100644 parachain/collation_protocol.go create mode 100644 parachain/collation_protocol_test.go create mode 100644 parachain/collator_protocol_message.go diff --git a/parachain/collation_protocol.go b/parachain/collation_protocol.go new file mode 100644 index 0000000000..78723196d0 --- /dev/null +++ b/parachain/collation_protocol.go @@ -0,0 +1,27 @@ +package parachain + +import "github.com/ChainSafe/gossamer/pkg/scale" + +// All network messages on the collation peer-set. +type CollationProtocol scale.VaryingDataType + +func NewCollationProtocol() CollationProtocol { + vdt := scale.MustNewVaryingDataType(NewCollatorProtocolMessage()) + return CollationProtocol(vdt) +} + +func (c *CollationProtocol) Set(val scale.VaryingDataTypeValue) (err error) { + vdt := scale.VaryingDataType(*c) + err = vdt.Set(val) + if err != nil { + return + } + *c = CollationProtocol(vdt) + return +} + +func (c *CollationProtocol) Value() (val scale.VaryingDataTypeValue, err error) { + vdt := scale.VaryingDataType(*c) + return vdt.Value() +} + diff --git a/parachain/collation_protocol_test.go b/parachain/collation_protocol_test.go new file mode 100644 index 0000000000..8c434d0a1d --- /dev/null +++ b/parachain/collation_protocol_test.go @@ -0,0 +1,102 @@ +package parachain + +import ( + "testing" + + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/pkg/scale" + "github.com/stretchr/testify/require" +) + +func TestCollationProtocol(t *testing.T) { + t.Parallel() + + var collatorID CollatorID + tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") + copy(collatorID[:], tempCollatID) + + var collatorSignature CollatorSignature + tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll + copy(collatorSignature[:], tempSignature) + + var validatorSignature ValidatorSignature + copy(validatorSignature[:], tempSignature) + + hash1 := getDummyHash(1) + + secondedEnumValue := Seconded{ + Descriptor: CandidateDescriptor{ + ParaID: uint32(1), + RelayParent: hash1, + Collator: collatorID, + PersistedValidationDataHash: hash1, + PovHash: hash1, + ErasureRoot: hash1, + Signature: collatorSignature, + ParaHead: hash1, + ValidationCodeHash: ValidationCodeHash(hash1), + }, + Commitments: CandidateCommitments{ + UpwardMessages: []UpwardMessage{{1, 2, 3}}, + HorizontalMessages: []OutboundHrmpMessage{}, + NewValidationCode: &ValidationCode{1, 2, 3}, + HeadData: headData{1, 2, 3}, + ProcessedDownwardMessages: uint32(5), + HrmpWatermark: uint32(0), + }, + } + + statementWithSeconded := NewStatement() + err := statementWithSeconded.Set(secondedEnumValue) + require.NoError(t, err) + + testCases := []struct { + name string + enumValue scale.VaryingDataTypeValue + encodingValue []byte + }{ + { + name: "Declare", + enumValue: Declare{ + CollatorId: collatorID, + ParaId: uint32(5), + CollatorSignature: collatorSignature, + }, + encodingValue: []byte{}, + }, + { + name: "AdvertiseCollation", + enumValue: AdvertiseCollation(hash1), + encodingValue: []byte{}, + }, + { + name: "CollationSeconded", + enumValue: CollationSeconded{ + Hash: hash1, + UncheckedSignedFullStatement: UncheckedSignedFullStatement{ + Payload: statementWithSeconded, + ValidatorIndex: ValidatorIndex{5}, + Signature: validatorSignature, + }, + }, + encodingValue: []byte{}, + }, + } + + for _, c := range testCases { + c := c + t.Run(c.name, func(t *testing.T) { + t.Parallel() + + vtd := NewCollatorProtocolMessage() + + err := vtd.Set(c.enumValue) + require.NoError(t, err) + + bytes, err := scale.Marshal(vtd) + require.NoError(t, err) + + require.Equal(t, c.encodingValue, bytes) + }) + } +} diff --git a/parachain/collator_protocol_message.go b/parachain/collator_protocol_message.go new file mode 100644 index 0000000000..115116411d --- /dev/null +++ b/parachain/collator_protocol_message.go @@ -0,0 +1,63 @@ +package parachain + +import ( + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/pkg/scale" +) + +// Network messages used by the collator protocol subsystem +type CollatorProtocolMessage scale.VaryingDataType + +func (c CollatorProtocolMessage) Index() uint { + return 0 +} + +func NewCollatorProtocolMessage() CollatorProtocolMessage { + vdt := scale.MustNewVaryingDataType(Declare{}, AdvertiseCollation{}, CollationSeconded{}) + return CollatorProtocolMessage(vdt) +} + +func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error) { + vdt := scale.VaryingDataType(*c) + err = vdt.Set(val) + if err != nil { + return + } + *c = CollatorProtocolMessage(vdt) + return +} + +func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err error) { + vdt := scale.VaryingDataType(*c) + return vdt.Value() +} + +// Declare the intent to advertise collations under a collator ID, attaching a +// signature of the `PeerId` of the node using the given collator ID key. +type Declare struct { + CollatorId CollatorID `scale:"1"` + ParaId uint32 `scale:"2"` + CollatorSignature CollatorSignature `scale:"3"` +} + +func (d Declare) Index() uint { + return 0 +} + +// Advertise a collation to a validator. Can only be sent once the peer has +// declared that they are a collator with given ID. +type AdvertiseCollation common.Hash + +func (a AdvertiseCollation) Index() uint { + return 1 +} + +// A collation sent to a validator was seconded. +type CollationSeconded struct { + Hash common.Hash `scale:"1"` + UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` +} + +func (c CollationSeconded) Index() uint { + return 4 +} From 4e2ddd14c05560e12c547555c480dfbe2f95de5a Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Fri, 16 Jun 2023 19:21:11 +0530 Subject: [PATCH 08/28] add comments --- parachain/statement.go | 10 +++- parachain/statement_distribution_message.go | 38 ++++++++++---- .../statement_distribution_message_test.go | 49 +++++++++++++++++++ 3 files changed, 86 insertions(+), 11 deletions(-) diff --git a/parachain/statement.go b/parachain/statement.go index d0ceb7cae8..92d996b202 100644 --- a/parachain/statement.go +++ b/parachain/statement.go @@ -8,6 +8,7 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) +// A statement, where the candidate receipt is included in the `Seconded` variant. type Statement scale.VaryingDataType func NewStatement() Statement { @@ -33,23 +34,30 @@ func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { return vdt.Value() } +// A statement that a validator seconds a candidate. type Seconded CommittedCandidateReceipt func (s Seconded) Index() uint { return 1 } +// A statement that a validator has deemed a candidate valid. type Valid CandidateHash func (v Valid) Index() uint { return 2 } +// A candidate-receipt with commitments directly included. type CommittedCandidateReceipt struct { - Descriptor CandidateDescriptor `scale:"1"` + // The descriptor of the candidate. + Descriptor CandidateDescriptor `scale:"1"` + + // The commitments of the candidate receipt. Commitments CandidateCommitments `scale:"2"` } +// This type makes it easy to enforce that a hash is a candidate hash on the type level. type CandidateHash struct { Value common.Hash `scale:"1"` } diff --git a/parachain/statement_distribution_message.go b/parachain/statement_distribution_message.go index 705ce5d951..0c40a2f784 100644 --- a/parachain/statement_distribution_message.go +++ b/parachain/statement_distribution_message.go @@ -43,31 +43,49 @@ func (s SignedFullStatement) Index() uint { return 0 } +// 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 func (l SecondedStatementWithLargePayload) Index() uint { return 1 } +// Variant of `SignedFullStatement` where the signature has not yet been verified. type UncheckedSignedFullStatement struct { - Payload Statement `scale:"1"` - ValidatorIndex ValidatorIndex `scale:"2"` - Signature ValidatorSignature `scale:"3"` - // RealPayload CompactStatement `scale:"4"` // changes needed -} + // 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"` + + // The index of the validator signing this statement. + ValidatorIndex ValidatorIndex `scale:"2"` -type PhantomData struct{} + // The signature by the validator of the signed payload. + Signature ValidatorSignature `scale:"3"` +} +// Index of the validator. type ValidatorIndex struct { Value uint32 } +// Data that makes a statement unique. type StatementMetadata struct { - RelayParent common.Hash `scale:"1"` - CandidateHash CandidateHash `scale:"2"` - SignedBy ValidatorIndex `scale:"3"` - Signature ValidatorSignature `scale:"4"` + // Relay parent this statement is relevant under. + RelayParent common.Hash `scale:"1"` + + // Hash of the candidate that got validated. + CandidateHash CandidateHash `scale:"2"` + + // Validator that attested the validity. + SignedBy ValidatorIndex `scale:"3"` + + // Signature of seconding validator. + Signature ValidatorSignature `scale:"4"` } +// Signature with which parachain validators sign blocks. type ValidatorSignature Signature type Signature [64]byte diff --git a/parachain/statement_distribution_message_test.go b/parachain/statement_distribution_message_test.go index b20e8c9131..1307f2f920 100644 --- a/parachain/statement_distribution_message_test.go +++ b/parachain/statement_distribution_message_test.go @@ -83,6 +83,55 @@ func TestStatementDistributionMessage(t *testing.T) { enumValue scale.VaryingDataTypeValue encodingValue []byte }{ + // expected encoding is generated by running rust test code: + // fn statement_distribution_message_encode() { + // let hash1 = Hash::repeat_byte(5); + // let candidate_hash = CandidateHash(hash1); + // let statement_valid = Statement::Valid(candidate_hash); + // let val_sign = ValidatorSignature::from(sr25519::Signature([198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134])); + // let unchecked_signed_full_statement_valid = UncheckedSignedFullStatement::new(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()); + + // let collator_result = sr25519::Public::from_string("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147"); + // let collator = collator_result.unwrap(); + // let collsign = CollatorSignature::from(sr25519::Signature([198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134])); + // let candidate_descriptor = CandidateDescriptor{ + // para_id: 1.into(), + // relay_parent: hash1, + // collator: CollatorId::from(collator), + // persisted_validation_data_hash: hash1, + // pov_hash: hash1, + // erasure_root: hash1, + // signature: collsign, + // para_head: hash1, + // validation_code_hash: ValidationCodeHash::from(hash1) + // }; + // let commitments_new = CandidateCommitments{ + // upward_messages: vec![vec![1, 2, 3]].try_into().expect("error - upward_messages"), + // horizontal_messages: vec![].try_into().expect("error - horizontal_messages"), + // head_data: HeadData(vec![1, 2, 3]), + // hrmp_watermark: 0_u32, + // new_validation_code: ValidationCode(vec![1, 2, 3]).try_into().expect("error - new_validation_code"), + // processed_downward_messages: 5 + // }; + // let committed_candidate_receipt = CommittedCandidateReceipt{ + // descriptor: candidate_descriptor, + // commitments : commitments_new + // }; + // let statement_second = Statement::Seconded(committed_candidate_receipt); + // let unchecked_signed_full_statement_second = UncheckedSignedFullStatement::new(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()); + + // 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()); + // } { name: "SignedFullStatement with valid statement", enumValue: signedFullStatementWithValid, From 8852e43b23b0b21faa88d66f4ceb0266c30861ba Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Fri, 16 Jun 2023 19:57:40 +0530 Subject: [PATCH 09/28] move files to lib/parachain --- go.mod | 2 +- lib/parachain/statement.go | 53 +++++++ .../statement_distribution_message.go | 5 - .../statement_distribution_message_test.go | 16 +- .../parachain}/statement_test.go | 0 parachain/statement.go | 138 ------------------ 6 files changed, 63 insertions(+), 151 deletions(-) create mode 100644 lib/parachain/statement.go rename {parachain => lib/parachain}/statement_distribution_message.go (97%) rename {parachain => lib/parachain}/statement_distribution_message_test.go (70%) rename {parachain => lib/parachain}/statement_test.go (100%) delete mode 100644 parachain/statement.go diff --git a/go.mod b/go.mod index 6d04872665..66139cd85e 100644 --- a/go.mod +++ b/go.mod @@ -44,6 +44,7 @@ require ( golang.org/x/term v0.8.0 golang.org/x/text v0.9.0 google.golang.org/protobuf v1.30.0 + gopkg.in/yaml.v3 v3.0.1 ) require ( @@ -192,7 +193,6 @@ require ( gonum.org/v1/gonum v0.11.0 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/natefinch/npipe.v2 v2.0.0-20160621034901-c1b8fa8bdcce // indirect - gopkg.in/yaml.v3 v3.0.1 // indirect lukechampine.com/blake3 v1.1.7 // indirect nhooyr.io/websocket v1.8.7 // indirect ) diff --git a/lib/parachain/statement.go b/lib/parachain/statement.go new file mode 100644 index 0000000000..caea38e5d1 --- /dev/null +++ b/lib/parachain/statement.go @@ -0,0 +1,53 @@ +package parachain + +import ( + "fmt" + + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/pkg/scale" +) + +// A statement, where the candidate receipt is included in the `Seconded` variant. +type Statement scale.VaryingDataType + +func NewStatement() Statement { + vdt := scale.MustNewVaryingDataType(Seconded{}, Valid{}) + return Statement(vdt) +} + +// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +func (s *Statement) 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) + return nil +} + +// Value returns the value from the underlying VaryingDataType +func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { + vdt := scale.VaryingDataType(*s) + return vdt.Value() +} + +// A statement that a validator seconds a candidate. +type Seconded CommittedCandidateReceipt + +func (s Seconded) Index() uint { + return 1 +} + +// A statement that a validator has deemed a candidate valid. +type Valid CandidateHash + +func (v Valid) Index() uint { + return 2 +} + +// This type makes it easy to enforce that a hash is a candidate hash on the type level. +type CandidateHash struct { + Value common.Hash `scale:"1"` +} diff --git a/parachain/statement_distribution_message.go b/lib/parachain/statement_distribution_message.go similarity index 97% rename from parachain/statement_distribution_message.go rename to lib/parachain/statement_distribution_message.go index 0c40a2f784..3129b52e15 100644 --- a/parachain/statement_distribution_message.go +++ b/lib/parachain/statement_distribution_message.go @@ -66,11 +66,6 @@ type UncheckedSignedFullStatement struct { Signature ValidatorSignature `scale:"3"` } -// Index of the validator. -type ValidatorIndex struct { - Value uint32 -} - // Data that makes a statement unique. type StatementMetadata struct { // Relay parent this statement is relevant under. diff --git a/parachain/statement_distribution_message_test.go b/lib/parachain/statement_distribution_message_test.go similarity index 70% rename from parachain/statement_distribution_message_test.go rename to lib/parachain/statement_distribution_message_test.go index 1307f2f920..94307c32d2 100644 --- a/parachain/statement_distribution_message_test.go +++ b/lib/parachain/statement_distribution_message_test.go @@ -12,7 +12,7 @@ func TestStatementDistributionMessage(t *testing.T) { t.Parallel() var collatorSignature CollatorSignature - tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll + tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") // nolint:lll copy(collatorSignature[:], tempSignature) var validatorSignature ValidatorSignature @@ -58,7 +58,7 @@ func TestStatementDistributionMessage(t *testing.T) { Hash: hash1, UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithValid, - ValidatorIndex: ValidatorIndex{5}, + ValidatorIndex: ValidatorIndex(5), Signature: validatorSignature, }, } @@ -67,7 +67,7 @@ func TestStatementDistributionMessage(t *testing.T) { Hash: hash1, UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithSeconded, - ValidatorIndex: ValidatorIndex{5}, + ValidatorIndex: ValidatorIndex(5), Signature: validatorSignature, }, } @@ -75,9 +75,10 @@ func TestStatementDistributionMessage(t *testing.T) { secondedStatementWithLargePayload := SecondedStatementWithLargePayload{ RelayParent: hash1, CandidateHash: CandidateHash{hash1}, - SignedBy: ValidatorIndex{5}, + SignedBy: ValidatorIndex(5), Signature: validatorSignature, } + testCases := []struct { name string enumValue scale.VaryingDataTypeValue @@ -132,20 +133,21 @@ func TestStatementDistributionMessage(t *testing.T) { // }); // println!("encode SecondedStatementWithLargePayload => {:?}\n\n", sdm_large_statement.encode()); // } + { name: "SignedFullStatement with valid statement", enumValue: signedFullStatementWithValid, - encodingValue: []byte{0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, //nolint:lll + encodingValue: common.MustHexToBytes("0x00050505050505050505050505050505050505050505050505050505050505050502050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), // nolint:lll }, { name: "SignedFullStatement with Seconded statement", enumValue: signedFullStatementWithSeconded, - encodingValue: []byte{0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 72, 33, 91, 157, 50, 38, 1, 229, 177, 169, 81, 100, 206, 160, 220, 70, 38, 245, 69, 249, 131, 67, 208, 127, 21, 81, 235, 149, 67, 196, 177, 71, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, //nolint:lll + encodingValue: common.MustHexToBytes("0x0005050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), // nolint:lll }, { name: "SecondedStatementWithLargePayload", enumValue: secondedStatementWithLargePayload, - encodingValue: []byte{1, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 0, 0, 0, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134}, //nolint:lll + encodingValue: common.MustHexToBytes("0x010505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), // nolint:lll }, } diff --git a/parachain/statement_test.go b/lib/parachain/statement_test.go similarity index 100% rename from parachain/statement_test.go rename to lib/parachain/statement_test.go diff --git a/parachain/statement.go b/parachain/statement.go deleted file mode 100644 index 92d996b202..0000000000 --- a/parachain/statement.go +++ /dev/null @@ -1,138 +0,0 @@ -package parachain - -import ( - "fmt" - - "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/lib/crypto/sr25519" - "github.com/ChainSafe/gossamer/pkg/scale" -) - -// A statement, where the candidate receipt is included in the `Seconded` variant. -type Statement scale.VaryingDataType - -func NewStatement() Statement { - vdt := scale.MustNewVaryingDataType(Seconded{}, Valid{}) - return Statement(vdt) -} - -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType -func (s *Statement) 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) - return nil -} - -// Value returns the value from the underlying VaryingDataType -func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { - vdt := scale.VaryingDataType(*s) - return vdt.Value() -} - -// A statement that a validator seconds a candidate. -type Seconded CommittedCandidateReceipt - -func (s Seconded) Index() uint { - return 1 -} - -// A statement that a validator has deemed a candidate valid. -type Valid CandidateHash - -func (v Valid) Index() uint { - return 2 -} - -// A candidate-receipt with commitments directly included. -type CommittedCandidateReceipt struct { - // The descriptor of the candidate. - Descriptor CandidateDescriptor `scale:"1"` - - // The commitments of the candidate receipt. - Commitments CandidateCommitments `scale:"2"` -} - -// This type makes it easy to enforce that a hash is a candidate hash on the type level. -type CandidateHash struct { - Value common.Hash `scale:"1"` -} - -// CandidateDescriptor is a unique descriptor of the candidate receipt. -type CandidateDescriptor struct { - // The ID of the para this is a candidate for. - ParaID uint32 `scale:"1"` - - // RelayParent is the hash of the relay-chain block this should be executed in - // the context of. - // NOTE: the fact that the hash includes this value means that code depends - // on this for deduplication. Removing this field is likely to break things. - RelayParent common.Hash `scale:"2"` - - // Collator is the collator's sr25519 public key. - Collator CollatorID `scale:"3"` - - // PersistedValidationDataHash is the blake2-256 hash of the persisted validation data. This is extra data derived from - // relay-chain state which may vary based on bitfields included before the candidate. - // Thus, it cannot be derived entirely from the relay-parent. - PersistedValidationDataHash common.Hash `scale:"4"` - - // PovHash is the hash of the `pov-block`. - PovHash common.Hash `scale:"5"` - // ErasureRoot is the root of a block's erasure encoding Merkle tree. - ErasureRoot common.Hash `scale:"6"` - - // Signature on blake2-256 of components of this receipt: - // The parachain index, the relay parent, the validation data hash, and the `pov_hash`. - // this is basically sr25519::Signature - Signature CollatorSignature `scale:"7"` - - // ParaHead is the hash of the para header that is being generated by this candidate. - ParaHead common.Hash `scale:"8"` - // ValidationCodeHash is the blake2-256 hash of the validation code bytes. - ValidationCodeHash ValidationCodeHash `scale:"9"` -} - -// CollatorID represents the public key of a collator -type CollatorID [sr25519.PublicKeyLength]byte - -// CollatorSignature is the signature on a candidate's block data signed by a collator. -type CollatorSignature [sr25519.SignatureLength]byte - -// ValidationCodeHash is the blake2-256 hash of the validation code bytes. -type ValidationCodeHash common.Hash - -// CandidateCommitments are Commitments made in a `CandidateReceipt`. Many of these are outputs of validation. -type CandidateCommitments struct { - // Messages destined to be interpreted by the Relay chain itself. - UpwardMessages []UpwardMessage `scale:"1"` - // Horizontal messages sent by the parachain. - HorizontalMessages []OutboundHrmpMessage `scale:"2"` - // New validation code. - NewValidationCode *ValidationCode `scale:"3"` - // The head-data produced as a result of execution. - HeadData headData `scale:"4"` - // The number of messages processed from the DMQ. - ProcessedDownwardMessages uint32 `scale:"5"` - // The mark which specifies the block number up to which all inbound HRMP messages are processed. - HrmpWatermark uint32 `scale:"6"` -} - -// UpwardMessage A message from a parachain to its Relay Chain. -type UpwardMessage []byte - -// OutboundHrmpMessage is an HRMP message seen from the perspective of a sender. -type OutboundHrmpMessage struct { - Recipient uint32 `scale:"1"` - Data []byte `scale:"2"` -} - -// ValidationCode is Parachain validation code. -type ValidationCode []byte - -// headData is Parachain head data included in the chain. -type headData []byte From ab8b0fe414e3179d4284fe4d29d911f620458b1b Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Fri, 16 Jun 2023 20:15:25 +0530 Subject: [PATCH 10/28] lint --- .../statement_distribution_message_test.go | 35 +++++++++++++------ 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/lib/parachain/statement_distribution_message_test.go b/lib/parachain/statement_distribution_message_test.go index 94307c32d2..a1343cf47c 100644 --- a/lib/parachain/statement_distribution_message_test.go +++ b/lib/parachain/statement_distribution_message_test.go @@ -12,7 +12,7 @@ func TestStatementDistributionMessage(t *testing.T) { t.Parallel() var collatorSignature CollatorSignature - tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") // nolint:lll + tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll copy(collatorSignature[:], tempSignature) var validatorSignature ValidatorSignature @@ -89,14 +89,25 @@ func TestStatementDistributionMessage(t *testing.T) { // let hash1 = Hash::repeat_byte(5); // let candidate_hash = CandidateHash(hash1); // let statement_valid = Statement::Valid(candidate_hash); - // let val_sign = ValidatorSignature::from(sr25519::Signature([198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134])); - // let unchecked_signed_full_statement_valid = UncheckedSignedFullStatement::new(statement_valid, ValidatorIndex(5), val_sign.clone()); - // let sdm_statement_valid = StatementDistributionMessage::Statement(hash1, unchecked_signed_full_statement_valid); + // let val_sign = ValidatorSignature::from( + // sr25519::Signature([198, 124, 185, 59, 240, 163, 111, 206, 227, + // 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, + // 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, + // 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134])); //nolint:lll + // let unchecked_signed_full_statement_valid = UncheckedSignedFullStatement::new( + // 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()); - // let collator_result = sr25519::Public::from_string("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147"); + // let collator_result = sr25519::Public::from_string( + // "0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147"); // let collator = collator_result.unwrap(); - // let collsign = CollatorSignature::from(sr25519::Signature([198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134])); + // let collsign = CollatorSignature::from(sr25519::Signature( + // [198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, + // 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, + // 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, + // 190, 116, 184, 148, 207, 27, 134])); //nolint:lll // let candidate_descriptor = CandidateDescriptor{ // para_id: 1.into(), // relay_parent: hash1, @@ -121,8 +132,10 @@ func TestStatementDistributionMessage(t *testing.T) { // commitments : commitments_new // }; // let statement_second = Statement::Seconded(committed_candidate_receipt); - // let unchecked_signed_full_statement_second = UncheckedSignedFullStatement::new(statement_second, ValidatorIndex(5), val_sign.clone()); - // let sdm_statement_second = StatementDistributionMessage::Statement(hash1, unchecked_signed_full_statement_second); + // let unchecked_signed_full_statement_second = UncheckedSignedFullStatement::new( + // 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()); // let sdm_large_statement = StatementDistributionMessage::LargeStatement(StatementMetadata{ @@ -137,17 +150,17 @@ func TestStatementDistributionMessage(t *testing.T) { { name: "SignedFullStatement with valid statement", enumValue: signedFullStatementWithValid, - encodingValue: common.MustHexToBytes("0x00050505050505050505050505050505050505050505050505050505050505050502050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), // nolint:lll + encodingValue: common.MustHexToBytes("0x00050505050505050505050505050505050505050505050505050505050505050502050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll }, { name: "SignedFullStatement with Seconded statement", enumValue: signedFullStatementWithSeconded, - encodingValue: common.MustHexToBytes("0x0005050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), // nolint:lll + encodingValue: common.MustHexToBytes("0x0005050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll }, { name: "SecondedStatementWithLargePayload", enumValue: secondedStatementWithLargePayload, - encodingValue: common.MustHexToBytes("0x010505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), // nolint:lll + encodingValue: common.MustHexToBytes("0x010505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll }, } From 53fb7e7b8bfa2ae85aefdb15b807709434c3b64d Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Fri, 16 Jun 2023 20:17:11 +0530 Subject: [PATCH 11/28] clean up --- lib/parachain/statement_distribution_message_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/parachain/statement_distribution_message_test.go b/lib/parachain/statement_distribution_message_test.go index a1343cf47c..e75d3c8c95 100644 --- a/lib/parachain/statement_distribution_message_test.go +++ b/lib/parachain/statement_distribution_message_test.go @@ -93,7 +93,7 @@ func TestStatementDistributionMessage(t *testing.T) { // sr25519::Signature([198, 124, 185, 59, 240, 163, 111, 206, 227, // 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, // 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, - // 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134])); //nolint:lll + // 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134])); // let unchecked_signed_full_statement_valid = UncheckedSignedFullStatement::new( // statement_valid, ValidatorIndex(5), val_sign.clone()); // let sdm_statement_valid = StatementDistributionMessage::Statement( @@ -107,7 +107,7 @@ func TestStatementDistributionMessage(t *testing.T) { // [198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, // 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, // 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, - // 190, 116, 184, 148, 207, 27, 134])); //nolint:lll + // 190, 116, 184, 148, 207, 27, 134])); // let candidate_descriptor = CandidateDescriptor{ // para_id: 1.into(), // relay_parent: hash1, From 8503aaae63820c9bd9c7ea5b3c717b0f27515247 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 12:55:58 +0530 Subject: [PATCH 12/28] add test for CollationProtocol type --- lib/parachain/collation_protocol.go | 1 - lib/parachain/collation_protocol_test.go | 38 +++++++++++++----------- 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/lib/parachain/collation_protocol.go b/lib/parachain/collation_protocol.go index 78723196d0..3a25cd156e 100644 --- a/lib/parachain/collation_protocol.go +++ b/lib/parachain/collation_protocol.go @@ -24,4 +24,3 @@ func (c *CollationProtocol) Value() (val scale.VaryingDataTypeValue, err error) vdt := scale.VaryingDataType(*c) return vdt.Value() } - diff --git a/lib/parachain/collation_protocol_test.go b/lib/parachain/collation_protocol_test.go index 8c434d0a1d..53128f8272 100644 --- a/lib/parachain/collation_protocol_test.go +++ b/lib/parachain/collation_protocol_test.go @@ -22,19 +22,19 @@ func TestCollationProtocol(t *testing.T) { var validatorSignature ValidatorSignature copy(validatorSignature[:], tempSignature) - hash1 := getDummyHash(1) + hash5 := getDummyHash(5) secondedEnumValue := Seconded{ Descriptor: CandidateDescriptor{ ParaID: uint32(1), - RelayParent: hash1, + RelayParent: hash5, Collator: collatorID, - PersistedValidationDataHash: hash1, - PovHash: hash1, - ErasureRoot: hash1, + PersistedValidationDataHash: hash5, + PovHash: hash5, + ErasureRoot: hash5, Signature: collatorSignature, - ParaHead: hash1, - ValidationCodeHash: ValidationCodeHash(hash1), + ParaHead: hash5, + ValidationCodeHash: ValidationCodeHash(hash5), }, Commitments: CandidateCommitments{ UpwardMessages: []UpwardMessage{{1, 2, 3}}, @@ -53,7 +53,7 @@ func TestCollationProtocol(t *testing.T) { testCases := []struct { name string enumValue scale.VaryingDataTypeValue - encodingValue []byte + encodingValue []byte // encoding of CollationProtocol value }{ { name: "Declare", @@ -62,24 +62,24 @@ func TestCollationProtocol(t *testing.T) { ParaId: uint32(5), CollatorSignature: collatorSignature, }, - encodingValue: []byte{}, + encodingValue: common.MustHexToBytes("0x000048215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b14705000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll }, { name: "AdvertiseCollation", - enumValue: AdvertiseCollation(hash1), - encodingValue: []byte{}, + enumValue: AdvertiseCollation(hash5), + encodingValue: common.MustHexToBytes("0x00010505050505050505050505050505050505050505050505050505050505050505"), }, { name: "CollationSeconded", enumValue: CollationSeconded{ - Hash: hash1, + Hash: hash5, UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithSeconded, - ValidatorIndex: ValidatorIndex{5}, + ValidatorIndex: ValidatorIndex(5), Signature: validatorSignature, }, }, - encodingValue: []byte{}, + encodingValue: common.MustHexToBytes("0x000405050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll }, } @@ -88,12 +88,16 @@ func TestCollationProtocol(t *testing.T) { t.Run(c.name, func(t *testing.T) { t.Parallel() - vtd := NewCollatorProtocolMessage() + vdt_parent := NewCollationProtocol() + vdt_child := NewCollatorProtocolMessage() - err := vtd.Set(c.enumValue) + err := vdt_child.Set(c.enumValue) require.NoError(t, err) - bytes, err := scale.Marshal(vtd) + err = vdt_parent.Set(vdt_child) + require.NoError(t, err) + + bytes, err := scale.Marshal(vdt_parent) require.NoError(t, err) require.Equal(t, c.encodingValue, bytes) From 42c389191b42e3c62c603ac8cd720e76ab718850 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 12:58:09 +0530 Subject: [PATCH 13/28] rename variable --- .../statement_distribution_message_test.go | 24 +++++++++---------- lib/parachain/statement_test.go | 16 ++++++------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/lib/parachain/statement_distribution_message_test.go b/lib/parachain/statement_distribution_message_test.go index e75d3c8c95..b6d25c659d 100644 --- a/lib/parachain/statement_distribution_message_test.go +++ b/lib/parachain/statement_distribution_message_test.go @@ -22,23 +22,23 @@ func TestStatementDistributionMessage(t *testing.T) { tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147") copy(collatorID[:], tempCollatID) - hash1 := getDummyHash(5) + hash5 := getDummyHash(5) statementWithValid := NewStatement() - err := statementWithValid.Set(Valid{hash1}) + err := statementWithValid.Set(Valid{hash5}) require.NoError(t, err) secondedEnumValue := Seconded{ Descriptor: CandidateDescriptor{ ParaID: uint32(1), - RelayParent: hash1, + RelayParent: hash5, Collator: collatorID, - PersistedValidationDataHash: hash1, - PovHash: hash1, - ErasureRoot: hash1, + PersistedValidationDataHash: hash5, + PovHash: hash5, + ErasureRoot: hash5, Signature: collatorSignature, - ParaHead: hash1, - ValidationCodeHash: ValidationCodeHash(hash1), + ParaHead: hash5, + ValidationCodeHash: ValidationCodeHash(hash5), }, Commitments: CandidateCommitments{ UpwardMessages: []UpwardMessage{{1, 2, 3}}, @@ -55,7 +55,7 @@ func TestStatementDistributionMessage(t *testing.T) { require.NoError(t, err) signedFullStatementWithValid := SignedFullStatement{ - Hash: hash1, + Hash: hash5, UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithValid, ValidatorIndex: ValidatorIndex(5), @@ -64,7 +64,7 @@ func TestStatementDistributionMessage(t *testing.T) { } signedFullStatementWithSeconded := SignedFullStatement{ - Hash: hash1, + Hash: hash5, UncheckedSignedFullStatement: UncheckedSignedFullStatement{ Payload: statementWithSeconded, ValidatorIndex: ValidatorIndex(5), @@ -73,8 +73,8 @@ func TestStatementDistributionMessage(t *testing.T) { } secondedStatementWithLargePayload := SecondedStatementWithLargePayload{ - RelayParent: hash1, - CandidateHash: CandidateHash{hash1}, + RelayParent: hash5, + CandidateHash: CandidateHash{hash5}, SignedBy: ValidatorIndex(5), Signature: validatorSignature, } diff --git a/lib/parachain/statement_test.go b/lib/parachain/statement_test.go index f0b3633252..4dfc7496b4 100644 --- a/lib/parachain/statement_test.go +++ b/lib/parachain/statement_test.go @@ -27,19 +27,19 @@ func TestStatement(t *testing.T) { tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll copy(collatorSignature[:], tempSignature) - hash1 := getDummyHash(5) + hash5 := getDummyHash(5) secondedEnumValue := Seconded{ Descriptor: CandidateDescriptor{ ParaID: uint32(1), - RelayParent: hash1, + RelayParent: hash5, Collator: collatorID, - PersistedValidationDataHash: hash1, - PovHash: hash1, - ErasureRoot: hash1, + PersistedValidationDataHash: hash5, + PovHash: hash5, + ErasureRoot: hash5, Signature: collatorSignature, - ParaHead: hash1, - ValidationCodeHash: ValidationCodeHash(hash1), + ParaHead: hash5, + ValidationCodeHash: ValidationCodeHash(hash5), }, Commitments: CandidateCommitments{ UpwardMessages: []UpwardMessage{{1, 2, 3}}, @@ -63,7 +63,7 @@ func TestStatement(t *testing.T) { }, { name: "Valid", - enumValue: Valid{hash1}, + enumValue: Valid{hash5}, encodingValue: []byte{2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, //nolint:lll }, } From 9e3fc0174d528b95e283925b412b9d7a445949c9 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 15:00:44 +0530 Subject: [PATCH 14/28] improve comments --- lib/parachain/statement.go | 11 +++++++---- lib/parachain/statement_distribution_message.go | 15 ++++++++++----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/parachain/statement.go b/lib/parachain/statement.go index caea38e5d1..09412e2a98 100644 --- a/lib/parachain/statement.go +++ b/lib/parachain/statement.go @@ -7,9 +7,10 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) -// A statement, where the candidate receipt is included in the `Seconded` variant. +// Statement is a result of candidate validation. It could be either `Valid` or `Seconded`. type Statement scale.VaryingDataType +// NewStatement returns a new Statement VaryingDataType func NewStatement() Statement { vdt := scale.MustNewVaryingDataType(Seconded{}, Valid{}) return Statement(vdt) @@ -33,21 +34,23 @@ func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { return vdt.Value() } -// A statement that a validator seconds a candidate. +// Seconded represents a statement that a validator seconds a candidate. type Seconded CommittedCandidateReceipt +// Index returns the VaryingDataType Index func (s Seconded) Index() uint { return 1 } -// A statement that a validator has deemed a candidate valid. +// Valid represents a statement that a validator has deemed a candidate valid. type Valid CandidateHash +// Index returns the VaryingDataType Index func (v Valid) Index() uint { return 2 } -// This type makes it easy to enforce that a hash is a candidate hash on the type level. +// CandidateHash makes it easy to enforce that a hash is a candidate hash on the type level. type CandidateHash struct { Value common.Hash `scale:"1"` } diff --git a/lib/parachain/statement_distribution_message.go b/lib/parachain/statement_distribution_message.go index 3129b52e15..0d6d91fbf9 100644 --- a/lib/parachain/statement_distribution_message.go +++ b/lib/parachain/statement_distribution_message.go @@ -7,9 +7,10 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) -// Network messages used by the statement distribution subsystem. +// StatementDistributionMessage represents network messages used by the statement distribution subsystem type StatementDistributionMessage scale.VaryingDataType +// NewStatement returns a new Statement VaryingDataType func NewStatementDistributionMessage() StatementDistributionMessage { vdt := scale.MustNewVaryingDataType(SignedFullStatement{}, SecondedStatementWithLargePayload{}) return StatementDistributionMessage(vdt) @@ -33,12 +34,13 @@ func (sdm *StatementDistributionMessage) Value() (scale.VaryingDataTypeValue, er return vdt.Value() } -// A signed full statement under a given relay-parent. +// SignedFullStatement represents a signed full statement under a given relay-parent. type SignedFullStatement struct { Hash common.Hash `scale:"1"` UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` } +// Index returns the VaryingDataType Index func (s SignedFullStatement) Index() uint { return 0 } @@ -49,11 +51,12 @@ func (s SignedFullStatement) Index() uint { // via request/response. type SecondedStatementWithLargePayload StatementMetadata +// Index returns the VaryingDataType Index func (l SecondedStatementWithLargePayload) Index() uint { return 1 } -// Variant of `SignedFullStatement` where the signature has not yet been verified. +// 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. @@ -66,7 +69,7 @@ type UncheckedSignedFullStatement struct { Signature ValidatorSignature `scale:"3"` } -// Data that makes a statement unique. +// StatementMetadata represents the data that makes a statement unique. type StatementMetadata struct { // Relay parent this statement is relevant under. RelayParent common.Hash `scale:"1"` @@ -81,6 +84,8 @@ type StatementMetadata struct { Signature ValidatorSignature `scale:"4"` } -// Signature with which parachain validators sign blocks. +// ValidatorSignature represents the signature with which parachain validators sign blocks. type ValidatorSignature Signature + +// Signature represents a cryptographic signature. type Signature [64]byte From 462025326033b943a06d823f1384c8aa80ce488e Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 16:05:48 +0530 Subject: [PATCH 15/28] clean up --- lib/parachain/statement_distribution_message.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/parachain/statement_distribution_message.go b/lib/parachain/statement_distribution_message.go index 0d6d91fbf9..8690fcaa61 100644 --- a/lib/parachain/statement_distribution_message.go +++ b/lib/parachain/statement_distribution_message.go @@ -10,7 +10,7 @@ import ( // StatementDistributionMessage represents network messages used by the statement distribution subsystem type StatementDistributionMessage scale.VaryingDataType -// NewStatement returns a new Statement VaryingDataType +// NewStatementDistributionMessage returns a new StatementDistributionMessage VaryingDataType func NewStatementDistributionMessage() StatementDistributionMessage { vdt := scale.MustNewVaryingDataType(SignedFullStatement{}, SecondedStatementWithLargePayload{}) return StatementDistributionMessage(vdt) From a9b0464daf0b5f70c0134204d52e2a425c972577 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 16:14:29 +0530 Subject: [PATCH 16/28] add comments --- lib/parachain/collation_protocol.go | 5 ++++- lib/parachain/collator_protocol_message.go | 15 +++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lib/parachain/collation_protocol.go b/lib/parachain/collation_protocol.go index 3a25cd156e..afe5227bc6 100644 --- a/lib/parachain/collation_protocol.go +++ b/lib/parachain/collation_protocol.go @@ -2,14 +2,16 @@ package parachain import "github.com/ChainSafe/gossamer/pkg/scale" -// All network messages on the collation peer-set. +// CollationProtocol represents all network messages on the collation peer-set. type CollationProtocol scale.VaryingDataType +// NewCollationProtocol returns a new CollationProtocol VaryingDataType func NewCollationProtocol() CollationProtocol { vdt := scale.MustNewVaryingDataType(NewCollatorProtocolMessage()) return CollationProtocol(vdt) } +// Set will set a VaryingDataTypeValue using the underlying VaryingDataType func (c *CollationProtocol) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*c) err = vdt.Set(val) @@ -20,6 +22,7 @@ func (c *CollationProtocol) Set(val scale.VaryingDataTypeValue) (err error) { return } +// Value returns the value from the underlying VaryingDataType func (c *CollationProtocol) Value() (val scale.VaryingDataTypeValue, err error) { vdt := scale.VaryingDataType(*c) return vdt.Value() diff --git a/lib/parachain/collator_protocol_message.go b/lib/parachain/collator_protocol_message.go index 115116411d..f8bf461619 100644 --- a/lib/parachain/collator_protocol_message.go +++ b/lib/parachain/collator_protocol_message.go @@ -5,18 +5,21 @@ import ( "github.com/ChainSafe/gossamer/pkg/scale" ) -// Network messages used by the collator protocol subsystem +// CollatorProtocolMessage represents Network messages used by the collator protocol subsystem type CollatorProtocolMessage scale.VaryingDataType +// Index returns the VaryingDataType Index func (c CollatorProtocolMessage) Index() uint { return 0 } +// NewCollatorProtocolMessage returns a new CollatorProtocolMessage VaryingDataType func NewCollatorProtocolMessage() CollatorProtocolMessage { vdt := scale.MustNewVaryingDataType(Declare{}, AdvertiseCollation{}, CollationSeconded{}) return CollatorProtocolMessage(vdt) } +// Set will set a VaryingDataTypeValue using the underlying VaryingDataType func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*c) err = vdt.Set(val) @@ -27,6 +30,7 @@ func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error return } +// Value returns the value from the underlying VaryingDataType func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err error) { vdt := scale.VaryingDataType(*c) return vdt.Value() @@ -40,24 +44,27 @@ type Declare struct { CollatorSignature CollatorSignature `scale:"3"` } +// Index returns the VaryingDataType Index func (d Declare) Index() uint { return 0 } -// Advertise a collation to a validator. Can only be sent once the peer has -// declared that they are a collator with given ID. +// AdvertiseCollation used to Advertise a collation to a validator. +// Can only be sent once the peer has declared that they are a collator with given ID. type AdvertiseCollation common.Hash +// Index returns the VaryingDataType Index func (a AdvertiseCollation) Index() uint { return 1 } -// A collation sent to a validator was seconded. +// CollationSeconded represents a collation sent to a validator was seconded. type CollationSeconded struct { Hash common.Hash `scale:"1"` UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` } +// Index returns the VaryingDataType Index func (c CollationSeconded) Index() uint { return 4 } From 627562ed16682b40ba850ee4792bfc5232ba0c92 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 18:27:25 +0530 Subject: [PATCH 17/28] use separate yaml file to store long hex --- .../statement_distribution_message_test.go | 24 +++++++++++++++---- lib/parachain/statement_test.go | 4 ++-- .../statement_distribution_message.yaml | 11 +++++++++ 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 lib/parachain/testdata/statement_distribution_message.yaml diff --git a/lib/parachain/statement_distribution_message_test.go b/lib/parachain/statement_distribution_message_test.go index b6d25c659d..f8946292dc 100644 --- a/lib/parachain/statement_distribution_message_test.go +++ b/lib/parachain/statement_distribution_message_test.go @@ -1,13 +1,29 @@ package parachain import ( + _ "embed" + "fmt" "testing" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) +//go:embed testdata/statement_distribution_message.yaml +var expectedHexRaw string + +var expectedHex map[string]string + +func init() { + err := yaml.Unmarshal([]byte(expectedHexRaw), &expectedHex) + if err != nil { + fmt.Printf("Error unmarshaling test data: %s\n", err) + return + } +} + func TestStatementDistributionMessage(t *testing.T) { t.Parallel() @@ -150,17 +166,17 @@ func TestStatementDistributionMessage(t *testing.T) { { name: "SignedFullStatement with valid statement", enumValue: signedFullStatementWithValid, - encodingValue: common.MustHexToBytes("0x00050505050505050505050505050505050505050505050505050505050505050502050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll + encodingValue: common.MustHexToBytes(expectedHex["sfsValid"]), }, { name: "SignedFullStatement with Seconded statement", enumValue: signedFullStatementWithSeconded, - encodingValue: common.MustHexToBytes("0x0005050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll + encodingValue: common.MustHexToBytes(expectedHex["sfsSeconded"]), }, { - name: "SecondedStatementWithLargePayload", + name: "Seconded Statement With LargePayload", enumValue: secondedStatementWithLargePayload, - encodingValue: common.MustHexToBytes("0x010505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll + encodingValue: common.MustHexToBytes(expectedHex["statementWithLargePayload"]), }, } diff --git a/lib/parachain/statement_test.go b/lib/parachain/statement_test.go index 4dfc7496b4..8ba31f4b8a 100644 --- a/lib/parachain/statement_test.go +++ b/lib/parachain/statement_test.go @@ -59,12 +59,12 @@ func TestStatement(t *testing.T) { { name: "Seconded", enumValue: secondedEnumValue, - encodingValue: []byte{1, 1, 0, 0, 0, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 72, 33, 91, 157, 50, 38, 1, 229, 177, 169, 81, 100, 206, 160, 220, 70, 38, 245, 69, 249, 131, 67, 208, 127, 21, 81, 235, 149, 67, 196, 177, 71, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 198, 124, 185, 59, 240, 163, 111, 206, 227, 210, 157, 232, 166, 166, 154, 117, 150, 89, 104, 10, 207, 72, 100, 117, 224, 162, 85, 42, 95, 190, 216, 126, 69, 173, 206, 95, 41, 6, 152, 216, 89, 96, 149, 114, 43, 51, 89, 146, 39, 247, 70, 31, 81, 175, 134, 23, 200, 190, 116, 184, 148, 207, 27, 134, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 4, 12, 1, 2, 3, 0, 1, 12, 1, 2, 3, 12, 1, 2, 3, 5, 0, 0, 0, 0, 0, 0, 0}, //nolint:lll + encodingValue: common.MustHexToBytes(expectedHex["statementSeconded"]), // stored in statement_distribution_message.yaml }, { name: "Valid", enumValue: Valid{hash5}, - encodingValue: []byte{2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5}, //nolint:lll + encodingValue: common.MustHexToBytes("0x020505050505050505050505050505050505050505050505050505050505050505"), }, } diff --git a/lib/parachain/testdata/statement_distribution_message.yaml b/lib/parachain/testdata/statement_distribution_message.yaml new file mode 100644 index 0000000000..9ef41ae370 --- /dev/null +++ b/lib/parachain/testdata/statement_distribution_message.yaml @@ -0,0 +1,11 @@ +# Seconded +statementSeconded: "0x0101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c0102030500000000000000" + +# SignedFullStatement with valid statement +sfsValid: "0x00050505050505050505050505050505050505050505050505050505050505050502050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86" + +# SignedFullStatement with Seconded statement +sfsSeconded: "0x0005050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86" + +# Seconded Statement With LargePayload +statementWithLargePayload: "0x010505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86" \ No newline at end of file From 362237f09f7314fd7c05b5b33854a21abd53934e Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 18:42:05 +0530 Subject: [PATCH 18/28] lint --- lib/parachain/statement_test.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/parachain/statement_test.go b/lib/parachain/statement_test.go index 8ba31f4b8a..73037e8a19 100644 --- a/lib/parachain/statement_test.go +++ b/lib/parachain/statement_test.go @@ -59,7 +59,8 @@ func TestStatement(t *testing.T) { { name: "Seconded", enumValue: secondedEnumValue, - encodingValue: common.MustHexToBytes(expectedHex["statementSeconded"]), // stored in statement_distribution_message.yaml + encodingValue: common.MustHexToBytes(expectedHex["statementSeconded"]), + // expected Hex stored in statement_distribution_message.yaml }, { name: "Valid", From 557cd1908d01bf170aa06708caae7632c7baf6f0 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 18:55:48 +0530 Subject: [PATCH 19/28] rename variable --- lib/parachain/statement_distribution_message_test.go | 12 ++++++------ lib/parachain/statement_test.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/parachain/statement_distribution_message_test.go b/lib/parachain/statement_distribution_message_test.go index f8946292dc..e214068931 100644 --- a/lib/parachain/statement_distribution_message_test.go +++ b/lib/parachain/statement_distribution_message_test.go @@ -12,12 +12,12 @@ import ( ) //go:embed testdata/statement_distribution_message.yaml -var expectedHexRaw string +var expectedSDMHexRaw string -var expectedHex map[string]string +var expectedSDMHex map[string]string func init() { - err := yaml.Unmarshal([]byte(expectedHexRaw), &expectedHex) + err := yaml.Unmarshal([]byte(expectedSDMHexRaw), &expectedSDMHex) if err != nil { fmt.Printf("Error unmarshaling test data: %s\n", err) return @@ -166,17 +166,17 @@ func TestStatementDistributionMessage(t *testing.T) { { name: "SignedFullStatement with valid statement", enumValue: signedFullStatementWithValid, - encodingValue: common.MustHexToBytes(expectedHex["sfsValid"]), + encodingValue: common.MustHexToBytes(expectedSDMHex["sfsValid"]), }, { name: "SignedFullStatement with Seconded statement", enumValue: signedFullStatementWithSeconded, - encodingValue: common.MustHexToBytes(expectedHex["sfsSeconded"]), + encodingValue: common.MustHexToBytes(expectedSDMHex["sfsSeconded"]), }, { name: "Seconded Statement With LargePayload", enumValue: secondedStatementWithLargePayload, - encodingValue: common.MustHexToBytes(expectedHex["statementWithLargePayload"]), + encodingValue: common.MustHexToBytes(expectedSDMHex["statementWithLargePayload"]), }, } diff --git a/lib/parachain/statement_test.go b/lib/parachain/statement_test.go index 73037e8a19..727555ba91 100644 --- a/lib/parachain/statement_test.go +++ b/lib/parachain/statement_test.go @@ -59,7 +59,7 @@ func TestStatement(t *testing.T) { { name: "Seconded", enumValue: secondedEnumValue, - encodingValue: common.MustHexToBytes(expectedHex["statementSeconded"]), + encodingValue: common.MustHexToBytes(expectedSDMHex["statementSeconded"]), // expected Hex stored in statement_distribution_message.yaml }, { From 4b94860859f912820d80f43247a72483b99e938a Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 19:34:00 +0530 Subject: [PATCH 20/28] use separate yaml file for long hex --- lib/parachain/collation_protocol_test.go | 20 +++++++++++++++++-- lib/parachain/collator_protocol_message.go | 4 ++-- .../testdata/collation_protocol.yaml | 4 ++++ 3 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 lib/parachain/testdata/collation_protocol.yaml diff --git a/lib/parachain/collation_protocol_test.go b/lib/parachain/collation_protocol_test.go index 53128f8272..9db1866fd3 100644 --- a/lib/parachain/collation_protocol_test.go +++ b/lib/parachain/collation_protocol_test.go @@ -1,13 +1,29 @@ package parachain import ( + _ "embed" + "fmt" "testing" "github.com/ChainSafe/gossamer/lib/common" "github.com/ChainSafe/gossamer/pkg/scale" "github.com/stretchr/testify/require" + "gopkg.in/yaml.v3" ) +//go:embed testdata/collation_protocol.yaml +var expectedCollationProtocolHexRaw string + +var expectedCollationProtocolHex map[string]string + +func init() { + err := yaml.Unmarshal([]byte(expectedCollationProtocolHexRaw), &expectedCollationProtocolHex) + if err != nil { + fmt.Println("Error unmarshaling test data:", err) + return + } +} + func TestCollationProtocol(t *testing.T) { t.Parallel() @@ -62,7 +78,7 @@ func TestCollationProtocol(t *testing.T) { ParaId: uint32(5), CollatorSignature: collatorSignature, }, - encodingValue: common.MustHexToBytes("0x000048215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b14705000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll + encodingValue: common.MustHexToBytes(expectedCollationProtocolHex["declare"]), }, { name: "AdvertiseCollation", @@ -79,7 +95,7 @@ func TestCollationProtocol(t *testing.T) { Signature: validatorSignature, }, }, - encodingValue: common.MustHexToBytes("0x000405050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"), //nolint:lll + encodingValue: common.MustHexToBytes(expectedCollationProtocolHex["collationSeconded"]), }, } diff --git a/lib/parachain/collator_protocol_message.go b/lib/parachain/collator_protocol_message.go index f8bf461619..749bfe7bd0 100644 --- a/lib/parachain/collator_protocol_message.go +++ b/lib/parachain/collator_protocol_message.go @@ -49,8 +49,8 @@ func (d Declare) Index() uint { return 0 } -// AdvertiseCollation used to Advertise a collation to a validator. -// Can only be sent once the peer has declared that they are a collator with given ID. +// AdvertiseCollation used to Advertise a collation to a validator +// Can only be sent once the peer has declared that they are a collator with given ID type AdvertiseCollation common.Hash // Index returns the VaryingDataType Index diff --git a/lib/parachain/testdata/collation_protocol.yaml b/lib/parachain/testdata/collation_protocol.yaml new file mode 100644 index 0000000000..f312759fc2 --- /dev/null +++ b/lib/parachain/testdata/collation_protocol.yaml @@ -0,0 +1,4 @@ + +declare: "0x000048215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b14705000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86" + +collationSeconded: "0x000405050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86" From 24fad1842f03aa0b10405979307c4e25ea277abc Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 19 Jun 2023 22:08:25 +0530 Subject: [PATCH 21/28] move collator signature hex to yaml --- .../statement_distribution_message_test.go | 14 +++++++------- lib/parachain/statement_test.go | 4 ++-- .../testdata/statement_distribution_message.yaml | 2 ++ 3 files changed, 11 insertions(+), 9 deletions(-) diff --git a/lib/parachain/statement_distribution_message_test.go b/lib/parachain/statement_distribution_message_test.go index e214068931..8721ff15de 100644 --- a/lib/parachain/statement_distribution_message_test.go +++ b/lib/parachain/statement_distribution_message_test.go @@ -12,12 +12,12 @@ import ( ) //go:embed testdata/statement_distribution_message.yaml -var expectedSDMHexRaw string +var testSDMHexRaw string -var expectedSDMHex map[string]string +var testSDMHex map[string]string func init() { - err := yaml.Unmarshal([]byte(expectedSDMHexRaw), &expectedSDMHex) + err := yaml.Unmarshal([]byte(testSDMHexRaw), &testSDMHex) if err != nil { fmt.Printf("Error unmarshaling test data: %s\n", err) return @@ -28,7 +28,7 @@ func TestStatementDistributionMessage(t *testing.T) { t.Parallel() var collatorSignature CollatorSignature - tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll + tempSignature := common.MustHexToBytes(testSDMHex["collatorSignature"]) copy(collatorSignature[:], tempSignature) var validatorSignature ValidatorSignature @@ -166,17 +166,17 @@ func TestStatementDistributionMessage(t *testing.T) { { name: "SignedFullStatement with valid statement", enumValue: signedFullStatementWithValid, - encodingValue: common.MustHexToBytes(expectedSDMHex["sfsValid"]), + encodingValue: common.MustHexToBytes(testSDMHex["sfsValid"]), }, { name: "SignedFullStatement with Seconded statement", enumValue: signedFullStatementWithSeconded, - encodingValue: common.MustHexToBytes(expectedSDMHex["sfsSeconded"]), + encodingValue: common.MustHexToBytes(testSDMHex["sfsSeconded"]), }, { name: "Seconded Statement With LargePayload", enumValue: secondedStatementWithLargePayload, - encodingValue: common.MustHexToBytes(expectedSDMHex["statementWithLargePayload"]), + encodingValue: common.MustHexToBytes(testSDMHex["statementWithLargePayload"]), }, } diff --git a/lib/parachain/statement_test.go b/lib/parachain/statement_test.go index 727555ba91..ed5f8eb3c1 100644 --- a/lib/parachain/statement_test.go +++ b/lib/parachain/statement_test.go @@ -24,7 +24,7 @@ func TestStatement(t *testing.T) { copy(collatorID[:], tempCollatID) var collatorSignature CollatorSignature - tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll + tempSignature := common.MustHexToBytes(testSDMHex["collatorSignature"]) copy(collatorSignature[:], tempSignature) hash5 := getDummyHash(5) @@ -59,7 +59,7 @@ func TestStatement(t *testing.T) { { name: "Seconded", enumValue: secondedEnumValue, - encodingValue: common.MustHexToBytes(expectedSDMHex["statementSeconded"]), + encodingValue: common.MustHexToBytes(testSDMHex["statementSeconded"]), // expected Hex stored in statement_distribution_message.yaml }, { diff --git a/lib/parachain/testdata/statement_distribution_message.yaml b/lib/parachain/testdata/statement_distribution_message.yaml index 9ef41ae370..e43dd59bb8 100644 --- a/lib/parachain/testdata/statement_distribution_message.yaml +++ b/lib/parachain/testdata/statement_distribution_message.yaml @@ -1,3 +1,5 @@ +collatorSignature: "0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86" + # Seconded statementSeconded: "0x0101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c0102030500000000000000" From 7bb2c1689d55046413c2182adb6572c598242c66 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Tue, 20 Jun 2023 19:59:31 +0530 Subject: [PATCH 22/28] clean up --- lib/parachain/collation_protocol_test.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/parachain/collation_protocol_test.go b/lib/parachain/collation_protocol_test.go index 9db1866fd3..6daf2cf9fe 100644 --- a/lib/parachain/collation_protocol_test.go +++ b/lib/parachain/collation_protocol_test.go @@ -12,12 +12,12 @@ import ( ) //go:embed testdata/collation_protocol.yaml -var expectedCollationProtocolHexRaw string +var testCollationProtocolHexRaw string -var expectedCollationProtocolHex map[string]string +var testCollationProtocolHex map[string]string func init() { - err := yaml.Unmarshal([]byte(expectedCollationProtocolHexRaw), &expectedCollationProtocolHex) + err := yaml.Unmarshal([]byte(testCollationProtocolHexRaw), &testCollationProtocolHex) if err != nil { fmt.Println("Error unmarshaling test data:", err) return @@ -32,7 +32,7 @@ func TestCollationProtocol(t *testing.T) { copy(collatorID[:], tempCollatID) var collatorSignature CollatorSignature - tempSignature := common.MustHexToBytes("0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86") //nolint:lll + tempSignature := common.MustHexToBytes(testSDMHex["collatorSignature"]) copy(collatorSignature[:], tempSignature) var validatorSignature ValidatorSignature @@ -69,7 +69,7 @@ func TestCollationProtocol(t *testing.T) { testCases := []struct { name string enumValue scale.VaryingDataTypeValue - encodingValue []byte // encoding of CollationProtocol value + encodingValue []byte }{ { name: "Declare", @@ -78,7 +78,7 @@ func TestCollationProtocol(t *testing.T) { ParaId: uint32(5), CollatorSignature: collatorSignature, }, - encodingValue: common.MustHexToBytes(expectedCollationProtocolHex["declare"]), + encodingValue: common.MustHexToBytes(testCollationProtocolHex["declare"]), }, { name: "AdvertiseCollation", @@ -95,7 +95,7 @@ func TestCollationProtocol(t *testing.T) { Signature: validatorSignature, }, }, - encodingValue: common.MustHexToBytes(expectedCollationProtocolHex["collationSeconded"]), + encodingValue: common.MustHexToBytes(testCollationProtocolHex["collationSeconded"]), }, } From bbbde986a62525dd5acbac130a535ded89881190 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Tue, 20 Jun 2023 20:12:43 +0530 Subject: [PATCH 23/28] resolve deepsource errors --- lib/parachain/collator_protocol_message.go | 8 ++++---- lib/parachain/statement.go | 4 ++-- lib/parachain/statement_distribution_message.go | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/parachain/collator_protocol_message.go b/lib/parachain/collator_protocol_message.go index 749bfe7bd0..14e5cb6777 100644 --- a/lib/parachain/collator_protocol_message.go +++ b/lib/parachain/collator_protocol_message.go @@ -9,7 +9,7 @@ import ( type CollatorProtocolMessage scale.VaryingDataType // Index returns the VaryingDataType Index -func (c CollatorProtocolMessage) Index() uint { +func (CollatorProtocolMessage) Index() uint { return 0 } @@ -45,7 +45,7 @@ type Declare struct { } // Index returns the VaryingDataType Index -func (d Declare) Index() uint { +func (Declare) Index() uint { return 0 } @@ -54,7 +54,7 @@ func (d Declare) Index() uint { type AdvertiseCollation common.Hash // Index returns the VaryingDataType Index -func (a AdvertiseCollation) Index() uint { +func (AdvertiseCollation) Index() uint { return 1 } @@ -65,6 +65,6 @@ type CollationSeconded struct { } // Index returns the VaryingDataType Index -func (c CollationSeconded) Index() uint { +func (CollationSeconded) Index() uint { return 4 } diff --git a/lib/parachain/statement.go b/lib/parachain/statement.go index 09412e2a98..4b2cf17cc7 100644 --- a/lib/parachain/statement.go +++ b/lib/parachain/statement.go @@ -38,7 +38,7 @@ func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { type Seconded CommittedCandidateReceipt // Index returns the VaryingDataType Index -func (s Seconded) Index() uint { +func (Seconded) Index() uint { return 1 } @@ -46,7 +46,7 @@ func (s Seconded) Index() uint { type Valid CandidateHash // Index returns the VaryingDataType Index -func (v Valid) Index() uint { +func (Valid) Index() uint { return 2 } diff --git a/lib/parachain/statement_distribution_message.go b/lib/parachain/statement_distribution_message.go index 8690fcaa61..2639c8ef90 100644 --- a/lib/parachain/statement_distribution_message.go +++ b/lib/parachain/statement_distribution_message.go @@ -41,18 +41,18 @@ type SignedFullStatement struct { } // Index returns the VaryingDataType Index -func (s SignedFullStatement) Index() uint { +func (SignedFullStatement) Index() uint { return 0 } -// Seconded statement with large payload (e.g. containing a runtime upgrade). +// SecondedStatementWithLargePayload 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 // Index returns the VaryingDataType Index -func (l SecondedStatementWithLargePayload) Index() uint { +func (SecondedStatementWithLargePayload) Index() uint { return 1 } From fb866e9401fd31ce581bec03b296da568c0b28a0 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Wed, 21 Jun 2023 17:45:48 +0530 Subject: [PATCH 24/28] improve comments --- lib/parachain/collation_protocol.go | 4 ++-- lib/parachain/collation_protocol_test.go | 2 +- lib/parachain/collator_protocol_message.go | 22 ++++++++++--------- lib/parachain/statement.go | 8 +++---- .../statement_distribution_message.go | 8 +++---- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/lib/parachain/collation_protocol.go b/lib/parachain/collation_protocol.go index afe5227bc6..13ab188a95 100644 --- a/lib/parachain/collation_protocol.go +++ b/lib/parachain/collation_protocol.go @@ -11,7 +11,7 @@ func NewCollationProtocol() CollationProtocol { return CollationProtocol(vdt) } -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +// Set will set a value using the underlying varying data type func (c *CollationProtocol) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*c) err = vdt.Set(val) @@ -22,7 +22,7 @@ func (c *CollationProtocol) Set(val scale.VaryingDataTypeValue) (err error) { return } -// Value returns the value from the underlying VaryingDataType +// Value returns the value from the underlying varying data type func (c *CollationProtocol) Value() (val scale.VaryingDataTypeValue, err error) { vdt := scale.VaryingDataType(*c) return vdt.Value() diff --git a/lib/parachain/collation_protocol_test.go b/lib/parachain/collation_protocol_test.go index 6daf2cf9fe..2e9cdc1a7e 100644 --- a/lib/parachain/collation_protocol_test.go +++ b/lib/parachain/collation_protocol_test.go @@ -75,7 +75,7 @@ func TestCollationProtocol(t *testing.T) { name: "Declare", enumValue: Declare{ CollatorId: collatorID, - ParaId: uint32(5), + ParaID: uint32(5), CollatorSignature: collatorSignature, }, encodingValue: common.MustHexToBytes(testCollationProtocolHex["declare"]), diff --git a/lib/parachain/collator_protocol_message.go b/lib/parachain/collator_protocol_message.go index 14e5cb6777..c5868a17cc 100644 --- a/lib/parachain/collator_protocol_message.go +++ b/lib/parachain/collator_protocol_message.go @@ -8,7 +8,7 @@ import ( // CollatorProtocolMessage represents Network messages used by the collator protocol subsystem type CollatorProtocolMessage scale.VaryingDataType -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (CollatorProtocolMessage) Index() uint { return 0 } @@ -19,7 +19,7 @@ func NewCollatorProtocolMessage() CollatorProtocolMessage { return CollatorProtocolMessage(vdt) } -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +// Set will set a value using the underlying varying data type func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*c) err = vdt.Set(val) @@ -30,7 +30,7 @@ func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error return } -// Value returns the value from the underlying VaryingDataType +// Value returns the value from the underlying varying data type func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err error) { vdt := scale.VaryingDataType(*c) return vdt.Value() @@ -40,31 +40,33 @@ func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err e // signature of the `PeerId` of the node using the given collator ID key. type Declare struct { CollatorId CollatorID `scale:"1"` - ParaId uint32 `scale:"2"` + ParaID uint32 `scale:"2"` CollatorSignature CollatorSignature `scale:"3"` } -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (Declare) Index() uint { return 0 } -// AdvertiseCollation used to Advertise a collation to a validator -// Can only be sent once the peer has declared that they are a collator with given ID +// AdvertiseCollation contains a relay parent hash and is used to advertise a collation to a validator. +// This will only advertise a collation if there exists one for the given relay parent and the given peer is +// set as validator for our para at the given relay parent. +// It can only be sent once the peer has declared that they are a collator with given ID type AdvertiseCollation common.Hash -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (AdvertiseCollation) Index() uint { return 1 } -// CollationSeconded represents a collation sent to a validator was seconded. +// CollationSeconded represents that a collation sent to a validator was seconded. type CollationSeconded struct { Hash common.Hash `scale:"1"` UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` } -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (CollationSeconded) Index() uint { return 4 } diff --git a/lib/parachain/statement.go b/lib/parachain/statement.go index 4b2cf17cc7..7336231411 100644 --- a/lib/parachain/statement.go +++ b/lib/parachain/statement.go @@ -16,7 +16,7 @@ func NewStatement() Statement { return Statement(vdt) } -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +// Set will set a value using the underlying varying data type func (s *Statement) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*s) err = vdt.Set(val) @@ -28,7 +28,7 @@ func (s *Statement) Set(val scale.VaryingDataTypeValue) (err error) { return nil } -// Value returns the value from the underlying VaryingDataType +// Value returns the value from the underlying varying data type func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { vdt := scale.VaryingDataType(*s) return vdt.Value() @@ -37,7 +37,7 @@ func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { // Seconded represents a statement that a validator seconds a candidate. type Seconded CommittedCandidateReceipt -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (Seconded) Index() uint { return 1 } @@ -45,7 +45,7 @@ func (Seconded) Index() uint { // Valid represents a statement that a validator has deemed a candidate valid. type Valid CandidateHash -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (Valid) Index() uint { return 2 } diff --git a/lib/parachain/statement_distribution_message.go b/lib/parachain/statement_distribution_message.go index 2639c8ef90..d3f1b47ce3 100644 --- a/lib/parachain/statement_distribution_message.go +++ b/lib/parachain/statement_distribution_message.go @@ -16,7 +16,7 @@ func NewStatementDistributionMessage() StatementDistributionMessage { return StatementDistributionMessage(vdt) } -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +// Set will set a value using the underlying varying data type func (sdm *StatementDistributionMessage) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*sdm) err = vdt.Set(val) @@ -28,7 +28,7 @@ func (sdm *StatementDistributionMessage) Set(val scale.VaryingDataTypeValue) (er return nil } -// Value returns the value from the underlying VaryingDataType +// Value returns the value from the underlying varying data type func (sdm *StatementDistributionMessage) Value() (scale.VaryingDataTypeValue, error) { vdt := scale.VaryingDataType(*sdm) return vdt.Value() @@ -40,7 +40,7 @@ type SignedFullStatement struct { UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` } -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (SignedFullStatement) Index() uint { return 0 } @@ -51,7 +51,7 @@ func (SignedFullStatement) Index() uint { // via request/response. type SecondedStatementWithLargePayload StatementMetadata -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (SecondedStatementWithLargePayload) Index() uint { return 1 } From 458402a564528fdf8d328e668902f354d562c051 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Wed, 21 Jun 2023 17:45:48 +0530 Subject: [PATCH 25/28] improve comments --- lib/parachain/collation_protocol.go | 6 ++--- lib/parachain/collation_protocol_test.go | 2 +- lib/parachain/collator_protocol_message.go | 24 ++++++++++--------- lib/parachain/statement.go | 10 ++++---- .../statement_distribution_message.go | 10 ++++---- 5 files changed, 27 insertions(+), 25 deletions(-) diff --git a/lib/parachain/collation_protocol.go b/lib/parachain/collation_protocol.go index afe5227bc6..4802447d0c 100644 --- a/lib/parachain/collation_protocol.go +++ b/lib/parachain/collation_protocol.go @@ -5,13 +5,13 @@ import "github.com/ChainSafe/gossamer/pkg/scale" // CollationProtocol represents all network messages on the collation peer-set. type CollationProtocol scale.VaryingDataType -// NewCollationProtocol returns a new CollationProtocol VaryingDataType +// NewCollationProtocol returns a new collation protocol varying data type func NewCollationProtocol() CollationProtocol { vdt := scale.MustNewVaryingDataType(NewCollatorProtocolMessage()) return CollationProtocol(vdt) } -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +// Set will set a value using the underlying varying data type func (c *CollationProtocol) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*c) err = vdt.Set(val) @@ -22,7 +22,7 @@ func (c *CollationProtocol) Set(val scale.VaryingDataTypeValue) (err error) { return } -// Value returns the value from the underlying VaryingDataType +// Value returns the value from the underlying varying data type func (c *CollationProtocol) Value() (val scale.VaryingDataTypeValue, err error) { vdt := scale.VaryingDataType(*c) return vdt.Value() diff --git a/lib/parachain/collation_protocol_test.go b/lib/parachain/collation_protocol_test.go index 6daf2cf9fe..2e9cdc1a7e 100644 --- a/lib/parachain/collation_protocol_test.go +++ b/lib/parachain/collation_protocol_test.go @@ -75,7 +75,7 @@ func TestCollationProtocol(t *testing.T) { name: "Declare", enumValue: Declare{ CollatorId: collatorID, - ParaId: uint32(5), + ParaID: uint32(5), CollatorSignature: collatorSignature, }, encodingValue: common.MustHexToBytes(testCollationProtocolHex["declare"]), diff --git a/lib/parachain/collator_protocol_message.go b/lib/parachain/collator_protocol_message.go index 14e5cb6777..d822fa1f5b 100644 --- a/lib/parachain/collator_protocol_message.go +++ b/lib/parachain/collator_protocol_message.go @@ -8,18 +8,18 @@ import ( // CollatorProtocolMessage represents Network messages used by the collator protocol subsystem type CollatorProtocolMessage scale.VaryingDataType -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (CollatorProtocolMessage) Index() uint { return 0 } -// NewCollatorProtocolMessage returns a new CollatorProtocolMessage VaryingDataType +// NewCollatorProtocolMessage returns a new collator protocol message varying data type func NewCollatorProtocolMessage() CollatorProtocolMessage { vdt := scale.MustNewVaryingDataType(Declare{}, AdvertiseCollation{}, CollationSeconded{}) return CollatorProtocolMessage(vdt) } -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +// Set will set a value using the underlying varying data type func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*c) err = vdt.Set(val) @@ -30,7 +30,7 @@ func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error return } -// Value returns the value from the underlying VaryingDataType +// Value returns the value from the underlying varying data type func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err error) { vdt := scale.VaryingDataType(*c) return vdt.Value() @@ -40,31 +40,33 @@ func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err e // signature of the `PeerId` of the node using the given collator ID key. type Declare struct { CollatorId CollatorID `scale:"1"` - ParaId uint32 `scale:"2"` + ParaID uint32 `scale:"2"` CollatorSignature CollatorSignature `scale:"3"` } -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (Declare) Index() uint { return 0 } -// AdvertiseCollation used to Advertise a collation to a validator -// Can only be sent once the peer has declared that they are a collator with given ID +// AdvertiseCollation contains a relay parent hash and is used to advertise a collation to a validator. +// This will only advertise a collation if there exists one for the given relay parent and the given peer is +// set as validator for our para at the given relay parent. +// It can only be sent once the peer has declared that they are a collator with given ID type AdvertiseCollation common.Hash -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (AdvertiseCollation) Index() uint { return 1 } -// CollationSeconded represents a collation sent to a validator was seconded. +// CollationSeconded represents that a collation sent to a validator was seconded. type CollationSeconded struct { Hash common.Hash `scale:"1"` UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` } -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (CollationSeconded) Index() uint { return 4 } diff --git a/lib/parachain/statement.go b/lib/parachain/statement.go index 4b2cf17cc7..168b8f0f59 100644 --- a/lib/parachain/statement.go +++ b/lib/parachain/statement.go @@ -10,13 +10,13 @@ import ( // Statement is a result of candidate validation. It could be either `Valid` or `Seconded`. type Statement scale.VaryingDataType -// NewStatement returns a new Statement VaryingDataType +// NewStatement returns a new statement varying data type func NewStatement() Statement { vdt := scale.MustNewVaryingDataType(Seconded{}, Valid{}) return Statement(vdt) } -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +// Set will set a value using the underlying varying data type func (s *Statement) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*s) err = vdt.Set(val) @@ -28,7 +28,7 @@ func (s *Statement) Set(val scale.VaryingDataTypeValue) (err error) { return nil } -// Value returns the value from the underlying VaryingDataType +// Value returns the value from the underlying varying data type func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { vdt := scale.VaryingDataType(*s) return vdt.Value() @@ -37,7 +37,7 @@ func (s *Statement) Value() (scale.VaryingDataTypeValue, error) { // Seconded represents a statement that a validator seconds a candidate. type Seconded CommittedCandidateReceipt -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (Seconded) Index() uint { return 1 } @@ -45,7 +45,7 @@ func (Seconded) Index() uint { // Valid represents a statement that a validator has deemed a candidate valid. type Valid CandidateHash -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (Valid) Index() uint { return 2 } diff --git a/lib/parachain/statement_distribution_message.go b/lib/parachain/statement_distribution_message.go index 2639c8ef90..b8ee373969 100644 --- a/lib/parachain/statement_distribution_message.go +++ b/lib/parachain/statement_distribution_message.go @@ -10,13 +10,13 @@ import ( // StatementDistributionMessage represents network messages used by the statement distribution subsystem type StatementDistributionMessage scale.VaryingDataType -// NewStatementDistributionMessage returns a new StatementDistributionMessage VaryingDataType +// NewStatementDistributionMessage returns a new statement distribution message varying data type func NewStatementDistributionMessage() StatementDistributionMessage { vdt := scale.MustNewVaryingDataType(SignedFullStatement{}, SecondedStatementWithLargePayload{}) return StatementDistributionMessage(vdt) } -// Set will set a VaryingDataTypeValue using the underlying VaryingDataType +// Set will set a value using the underlying varying data type func (sdm *StatementDistributionMessage) Set(val scale.VaryingDataTypeValue) (err error) { vdt := scale.VaryingDataType(*sdm) err = vdt.Set(val) @@ -28,7 +28,7 @@ func (sdm *StatementDistributionMessage) Set(val scale.VaryingDataTypeValue) (er return nil } -// Value returns the value from the underlying VaryingDataType +// Value returns the value from the underlying varying data type func (sdm *StatementDistributionMessage) Value() (scale.VaryingDataTypeValue, error) { vdt := scale.VaryingDataType(*sdm) return vdt.Value() @@ -40,7 +40,7 @@ type SignedFullStatement struct { UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` } -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (SignedFullStatement) Index() uint { return 0 } @@ -51,7 +51,7 @@ func (SignedFullStatement) Index() uint { // via request/response. type SecondedStatementWithLargePayload StatementMetadata -// Index returns the VaryingDataType Index +// Index returns the index of varying data type func (SecondedStatementWithLargePayload) Index() uint { return 1 } From 5715676175365fe615d5def422dae27ce9d56357 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Thu, 22 Jun 2023 16:10:10 +0530 Subject: [PATCH 26/28] lint --- lib/parachain/statement_distribution_message.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/parachain/statement_distribution_message.go b/lib/parachain/statement_distribution_message.go index b8ee373969..68eb3707f2 100644 --- a/lib/parachain/statement_distribution_message.go +++ b/lib/parachain/statement_distribution_message.go @@ -45,7 +45,8 @@ func (SignedFullStatement) Index() uint { return 0 } -// SecondedStatementWithLargePayload represents Seconded statement with large payload (e.g. containing a runtime upgrade). +// SecondedStatementWithLargePayload 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. From 7563e9286fcaf83d9837da8c75f6efada5013e3f Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Sun, 25 Jun 2023 17:57:37 +0530 Subject: [PATCH 27/28] rename variable --- lib/parachain/collation_protocol_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/parachain/collation_protocol_test.go b/lib/parachain/collation_protocol_test.go index 2e9cdc1a7e..7ac800a431 100644 --- a/lib/parachain/collation_protocol_test.go +++ b/lib/parachain/collation_protocol_test.go @@ -12,12 +12,12 @@ import ( ) //go:embed testdata/collation_protocol.yaml -var testCollationProtocolHexRaw string +var testDataCollationProtocolRaw string -var testCollationProtocolHex map[string]string +var testDataCollationProtocol map[string]string func init() { - err := yaml.Unmarshal([]byte(testCollationProtocolHexRaw), &testCollationProtocolHex) + err := yaml.Unmarshal([]byte(testDataCollationProtocolRaw), &testDataCollationProtocol) if err != nil { fmt.Println("Error unmarshaling test data:", err) return @@ -78,7 +78,7 @@ func TestCollationProtocol(t *testing.T) { ParaID: uint32(5), CollatorSignature: collatorSignature, }, - encodingValue: common.MustHexToBytes(testCollationProtocolHex["declare"]), + encodingValue: common.MustHexToBytes(testDataCollationProtocol["declare"]), }, { name: "AdvertiseCollation", @@ -95,7 +95,7 @@ func TestCollationProtocol(t *testing.T) { Signature: validatorSignature, }, }, - encodingValue: common.MustHexToBytes(testCollationProtocolHex["collationSeconded"]), + encodingValue: common.MustHexToBytes(testDataCollationProtocol["collationSeconded"]), }, } From 310ec0e3062a8fa421dc41415300e6b6a21eb101 Mon Sep 17 00:00:00 2001 From: axaysagathiya Date: Mon, 26 Jun 2023 13:42:16 +0530 Subject: [PATCH 28/28] put both types in a single file --- lib/parachain/collation_protocol.go | 71 ++++++++++++++++++++- lib/parachain/collator_protocol_message.go | 72 ---------------------- 2 files changed, 70 insertions(+), 73 deletions(-) delete mode 100644 lib/parachain/collator_protocol_message.go diff --git a/lib/parachain/collation_protocol.go b/lib/parachain/collation_protocol.go index 4802447d0c..8809a8316f 100644 --- a/lib/parachain/collation_protocol.go +++ b/lib/parachain/collation_protocol.go @@ -1,6 +1,9 @@ package parachain -import "github.com/ChainSafe/gossamer/pkg/scale" +import ( + "github.com/ChainSafe/gossamer/lib/common" + "github.com/ChainSafe/gossamer/pkg/scale" +) // CollationProtocol represents all network messages on the collation peer-set. type CollationProtocol scale.VaryingDataType @@ -27,3 +30,69 @@ func (c *CollationProtocol) Value() (val scale.VaryingDataTypeValue, err error) vdt := scale.VaryingDataType(*c) return vdt.Value() } + +// CollatorProtocolMessage represents Network messages used by the collator protocol subsystem +type CollatorProtocolMessage scale.VaryingDataType + +// Index returns the index of varying data type +func (CollatorProtocolMessage) Index() uint { + return 0 +} + +// NewCollatorProtocolMessage returns a new collator protocol message varying data type +func NewCollatorProtocolMessage() CollatorProtocolMessage { + vdt := scale.MustNewVaryingDataType(Declare{}, AdvertiseCollation{}, CollationSeconded{}) + return CollatorProtocolMessage(vdt) +} + +// Set will set a value using the underlying varying data type +func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error) { + vdt := scale.VaryingDataType(*c) + err = vdt.Set(val) + if err != nil { + return + } + *c = CollatorProtocolMessage(vdt) + return +} + +// Value returns the value from the underlying varying data type +func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err error) { + vdt := scale.VaryingDataType(*c) + return vdt.Value() +} + +// Declare the intent to advertise collations under a collator ID, attaching a +// signature of the `PeerId` of the node using the given collator ID key. +type Declare struct { + CollatorId CollatorID `scale:"1"` + ParaID uint32 `scale:"2"` + CollatorSignature CollatorSignature `scale:"3"` +} + +// Index returns the index of varying data type +func (Declare) Index() uint { + return 0 +} + +// AdvertiseCollation contains a relay parent hash and is used to advertise a collation to a validator. +// This will only advertise a collation if there exists one for the given relay parent and the given peer is +// set as validator for our para at the given relay parent. +// It can only be sent once the peer has declared that they are a collator with given ID +type AdvertiseCollation common.Hash + +// Index returns the index of varying data type +func (AdvertiseCollation) Index() uint { + return 1 +} + +// CollationSeconded represents that a collation sent to a validator was seconded. +type CollationSeconded struct { + Hash common.Hash `scale:"1"` + UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` +} + +// Index returns the index of varying data type +func (CollationSeconded) Index() uint { + return 4 +} diff --git a/lib/parachain/collator_protocol_message.go b/lib/parachain/collator_protocol_message.go deleted file mode 100644 index d822fa1f5b..0000000000 --- a/lib/parachain/collator_protocol_message.go +++ /dev/null @@ -1,72 +0,0 @@ -package parachain - -import ( - "github.com/ChainSafe/gossamer/lib/common" - "github.com/ChainSafe/gossamer/pkg/scale" -) - -// CollatorProtocolMessage represents Network messages used by the collator protocol subsystem -type CollatorProtocolMessage scale.VaryingDataType - -// Index returns the index of varying data type -func (CollatorProtocolMessage) Index() uint { - return 0 -} - -// NewCollatorProtocolMessage returns a new collator protocol message varying data type -func NewCollatorProtocolMessage() CollatorProtocolMessage { - vdt := scale.MustNewVaryingDataType(Declare{}, AdvertiseCollation{}, CollationSeconded{}) - return CollatorProtocolMessage(vdt) -} - -// Set will set a value using the underlying varying data type -func (c *CollatorProtocolMessage) Set(val scale.VaryingDataTypeValue) (err error) { - vdt := scale.VaryingDataType(*c) - err = vdt.Set(val) - if err != nil { - return - } - *c = CollatorProtocolMessage(vdt) - return -} - -// Value returns the value from the underlying varying data type -func (c *CollatorProtocolMessage) Value() (val scale.VaryingDataTypeValue, err error) { - vdt := scale.VaryingDataType(*c) - return vdt.Value() -} - -// Declare the intent to advertise collations under a collator ID, attaching a -// signature of the `PeerId` of the node using the given collator ID key. -type Declare struct { - CollatorId CollatorID `scale:"1"` - ParaID uint32 `scale:"2"` - CollatorSignature CollatorSignature `scale:"3"` -} - -// Index returns the index of varying data type -func (Declare) Index() uint { - return 0 -} - -// AdvertiseCollation contains a relay parent hash and is used to advertise a collation to a validator. -// This will only advertise a collation if there exists one for the given relay parent and the given peer is -// set as validator for our para at the given relay parent. -// It can only be sent once the peer has declared that they are a collator with given ID -type AdvertiseCollation common.Hash - -// Index returns the index of varying data type -func (AdvertiseCollation) Index() uint { - return 1 -} - -// CollationSeconded represents that a collation sent to a validator was seconded. -type CollationSeconded struct { - Hash common.Hash `scale:"1"` - UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` -} - -// Index returns the index of varying data type -func (CollationSeconded) Index() uint { - return 4 -}