Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parachain): Add CollationProtocol VaryingDataType #3337

Merged
merged 36 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
8d15217
feat(parachain): add types (WIP)
axaysagathiya Jun 6, 2023
4819a1c
Merge branch 'development' into issue-3289
axaysagathiya Jun 8, 2023
8503e75
draft1
axaysagathiya Jun 9, 2023
a82b310
add statement varying type
axaysagathiya Jun 12, 2023
fac5dd4
add test
axaysagathiya Jun 13, 2023
4fcb2d0
use actual values instead of empty array in test
axaysagathiya Jun 14, 2023
cd96daf
lint
axaysagathiya Jun 15, 2023
9fa5fd8
add CollationProtocol varying type
axaysagathiya Jun 16, 2023
4e2ddd1
add comments
axaysagathiya Jun 16, 2023
5129d81
Merge branch 'feat/parachain' of github.com:ChainSafe/gossamer into i…
axaysagathiya Jun 16, 2023
8852e43
move files to lib/parachain
axaysagathiya Jun 16, 2023
ab8b0fe
lint
axaysagathiya Jun 16, 2023
53fb7e7
clean up
axaysagathiya Jun 16, 2023
90fe4bb
Merge branch 'issue-3289' into issue-3319
axaysagathiya Jun 19, 2023
8503aaa
add test for CollationProtocol type
axaysagathiya Jun 19, 2023
42c3891
rename variable
axaysagathiya Jun 19, 2023
9e3fc01
improve comments
axaysagathiya Jun 19, 2023
4620253
clean up
axaysagathiya Jun 19, 2023
a9b0464
add comments
axaysagathiya Jun 19, 2023
a1dd40e
Merge branch 'issue-3289' into issue-3319
axaysagathiya Jun 19, 2023
627562e
use separate yaml file to store long hex
axaysagathiya Jun 19, 2023
362237f
lint
axaysagathiya Jun 19, 2023
557cd19
rename variable
axaysagathiya Jun 19, 2023
4b94860
use separate yaml file for long hex
axaysagathiya Jun 19, 2023
24fad18
move collator signature hex to yaml
axaysagathiya Jun 19, 2023
3318be0
Merge branch 'issue-3289' into issue-3319
axaysagathiya Jun 20, 2023
7bb2c16
clean up
axaysagathiya Jun 20, 2023
f8dd7e6
Merge branch 'feat/parachain' of github.com:ChainSafe/gossamer into i…
axaysagathiya Jun 20, 2023
bbbde98
resolve deepsource errors
axaysagathiya Jun 20, 2023
fb866e9
improve comments
axaysagathiya Jun 21, 2023
458402a
improve comments
axaysagathiya Jun 21, 2023
9795564
Merge branch 'issue-3319' of github.com:axaysagathiya/gossamer into i…
axaysagathiya Jun 21, 2023
5715676
lint
axaysagathiya Jun 22, 2023
0da21b4
Merge branch 'feat/parachain' into issue-3319
axaysagathiya Jun 24, 2023
7563e92
rename variable
axaysagathiya Jun 25, 2023
310ec0e
put both types in a single file
axaysagathiya Jun 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 98 additions & 0 deletions lib/parachain/collation_protocol.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package parachain

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

// NewCollationProtocol returns a new collation protocol varying data type
func NewCollationProtocol() CollationProtocol {
vdt := scale.MustNewVaryingDataType(NewCollatorProtocolMessage())
return CollationProtocol(vdt)
}

// 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)
if err != nil {
return
}
*c = CollationProtocol(vdt)
return
}

// 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()
}

// 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
}
122 changes: 122 additions & 0 deletions lib/parachain/collation_protocol_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
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 testDataCollationProtocolRaw string

var testDataCollationProtocol map[string]string

func init() {
err := yaml.Unmarshal([]byte(testDataCollationProtocolRaw), &testDataCollationProtocol)
if err != nil {
fmt.Println("Error unmarshaling test data:", err)
return
}
}

func TestCollationProtocol(t *testing.T) {
t.Parallel()

var collatorID CollatorID
tempCollatID := common.MustHexToBytes("0x48215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147")
copy(collatorID[:], tempCollatID)

var collatorSignature CollatorSignature
tempSignature := common.MustHexToBytes(testSDMHex["collatorSignature"])
copy(collatorSignature[:], tempSignature)

var validatorSignature ValidatorSignature
copy(validatorSignature[:], tempSignature)

hash5 := getDummyHash(5)

secondedEnumValue := Seconded{
Descriptor: CandidateDescriptor{
ParaID: uint32(1),
RelayParent: hash5,
Collator: collatorID,
PersistedValidationDataHash: hash5,
PovHash: hash5,
ErasureRoot: hash5,
Signature: collatorSignature,
ParaHead: hash5,
ValidationCodeHash: ValidationCodeHash(hash5),
},
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: common.MustHexToBytes(testDataCollationProtocol["declare"]),
},
{
name: "AdvertiseCollation",
enumValue: AdvertiseCollation(hash5),
encodingValue: common.MustHexToBytes("0x00010505050505050505050505050505050505050505050505050505050505050505"),
},
{
name: "CollationSeconded",
enumValue: CollationSeconded{
Hash: hash5,
UncheckedSignedFullStatement: UncheckedSignedFullStatement{
Payload: statementWithSeconded,
ValidatorIndex: ValidatorIndex(5),
Signature: validatorSignature,
},
},
encodingValue: common.MustHexToBytes(testDataCollationProtocol["collationSeconded"]),
},
}

for _, c := range testCases {
c := c
t.Run(c.name, func(t *testing.T) {
t.Parallel()

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

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

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)
})
}
}
4 changes: 4 additions & 0 deletions lib/parachain/testdata/collation_protocol.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

declare: "0x000048215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b14705000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"

collationSeconded: "0x000405050505050505050505050505050505050505050505050505050505050505050101000000050505050505050505050505050505050505050505050505050505050505050548215b9d322601e5b1a95164cea0dc4626f545f98343d07f1551eb9543c4b147050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b8605050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505050505040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"