Skip to content

Commit

Permalink
feat(lib/parachain): Create and integrate Validation Protocol struct (#…
Browse files Browse the repository at this point in the history
…3382)

Co-authored-by: Kanishka <kanishkatn@gmail.com>
Co-authored-by: Kishan Mohanbhai Sagathiya <kishansagathiya@gmail.com>
Co-authored-by: Axay Sagathiya <axaysagathiya@gmail.com>
  • Loading branch information
4 people authored Aug 18, 2023
1 parent 7c50d39 commit b9d545c
Show file tree
Hide file tree
Showing 9 changed files with 470 additions and 18 deletions.
12 changes: 6 additions & 6 deletions dot/parachain/approval_distribution_message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
"github.com/stretchr/testify/require"
)

var hash = common.MustHexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
var hashA = common.MustHexToHash("0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")

func TestEncodeApprovalDistributionMessageAssignmentModulo(t *testing.T) {
approvalDistributionMessage := NewApprovalDistributionMessageVDT()
Expand All @@ -37,7 +37,7 @@ func TestEncodeApprovalDistributionMessageAssignmentModulo(t *testing.T) {

approvalDistributionMessage.Set(Assignments{
Assignment{
IndirectAssignmentCert: fakeAssignmentCert(hash, parachaintypes.ValidatorIndex(1), false),
IndirectAssignmentCert: fakeAssignmentCert(hashA, parachaintypes.ValidatorIndex(1), false),
CandidateIndex: 4,
},
})
Expand All @@ -64,7 +64,7 @@ func TestEncodeApprovalDistributionMessageAssignmentDelay(t *testing.T) {

approvalDistributionMessage.Set(Assignments{
Assignment{
IndirectAssignmentCert: fakeAssignmentCert(hash, parachaintypes.ValidatorIndex(2), true),
IndirectAssignmentCert: fakeAssignmentCert(hashA, parachaintypes.ValidatorIndex(2), true),
CandidateIndex: 2,
},
})
Expand Down Expand Up @@ -113,7 +113,7 @@ func TestEncodeApprovalDistributionMessageApprovals(t *testing.T) {

approvalDistributionMessage.Set(Approvals{
IndirectSignedApprovalVote{
BlockHash: hash,
BlockHash: hashA,
CandidateIndex: CandidateIndex(2),
ValidatorIndex: parachaintypes.ValidatorIndex(3),
Signature: ValidatorSignature{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Expand Down Expand Up @@ -141,7 +141,7 @@ func TestDecodeApprovalDistributionMessageAssignmentModulo(t *testing.T) {
expectedApprovalDistributionMessage := NewApprovalDistributionMessageVDT()
expectedApprovalDistributionMessage.Set(Assignments{
Assignment{
IndirectAssignmentCert: fakeAssignmentCert(hash, parachaintypes.ValidatorIndex(2), false),
IndirectAssignmentCert: fakeAssignmentCert(hashA, parachaintypes.ValidatorIndex(2), false),
CandidateIndex: 4,
},
})
Expand All @@ -161,7 +161,7 @@ func TestDecodeApprovalDistributionMessageApprovals(t *testing.T) {
expectedApprovalDistributionMessage := NewApprovalDistributionMessageVDT()
expectedApprovalDistributionMessage.Set(Approvals{
IndirectSignedApprovalVote{
BlockHash: hash,
BlockHash: hashA,
CandidateIndex: CandidateIndex(2),
ValidatorIndex: parachaintypes.ValidatorIndex(3),
Signature: ValidatorSignature{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
Expand Down
20 changes: 19 additions & 1 deletion dot/parachain/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/ChainSafe/gossamer/dot/network"
"github.com/ChainSafe/gossamer/internal/log"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/protocol"
Expand All @@ -22,6 +23,8 @@ type Service struct {
Network Network
}

var logger = log.NewFromGlobal(log.AddContext("pkg", "parachain"))

func NewService(net Network, forkID string, genesisHash common.Hash) (*Service, error) {
validationProtocolID := GeneratePeersetProtocolName(
ValidationProtocolName, forkID, genesisHash, ValidationProtocolVersion)
Expand Down Expand Up @@ -120,7 +123,22 @@ func (s Service) run() {
collationMessage := CollationProtocolV1{}
s.Network.GossipMessage(&collationMessage)

validationMessage := ValidationProtocolV1{}
statementDistributionLargeStatement := StatementDistribution{NewStatementDistributionMessage()}
err := statementDistributionLargeStatement.Set(SecondedStatementWithLargePayload{
RelayParent: common.Hash{},
CandidateHash: CandidateHash{Value: common.Hash{}},
SignedBy: 5,
Signature: ValidatorSignature{},
})
if err != nil {
logger.Errorf("creating test statement message: %w\n", err)
}

validationMessage := NewValidationProtocolVDT()
err = validationMessage.Set(statementDistributionLargeStatement)
if err != nil {
logger.Errorf("creating test validation message: %w\n", err)
}
s.Network.GossipMessage(&validationMessage)

}
Expand Down
Empty file.
13 changes: 13 additions & 0 deletions dot/parachain/testdata/validation_protocol.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
validatorSignature: "0xc67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"

collatorID : "0xd43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27d"

approvalDistributionMessageAssignments: "0x040004aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0100000000020000000102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f200102030405060708090a0b0c0d0e0f101112131415161718191a1b1c1d1e1f202122232425262728292a2b2c2d2e2f303132333435363738393a3b3c3d3e3f4004000000"

approvalDistributionMessageApprovals: "0x040104aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0a0000000b000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"

statementDistributionMessageLargeStatement: "0x0301aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa05000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"

statementDistributionMessageStatement: "0x0300aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0101000000aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaad43593c715fdd31c61141abd04a99fd6822c8558854ccde39a5684e7a56da27daaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaac67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa040c01020300010c0102030c010203050000000000000005000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"

bitfieldDistribution: "0x0100aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa80ffffffff00000000c67cb93bf0a36fcee3d29de8a6a69a759659680acf486475e0a2552a5fbed87e45adce5f290698d8596095722b33599227f7461f51af8617c8be74b894cf1b86"
131 changes: 125 additions & 6 deletions dot/parachain/validation_protocol.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,143 @@ import (
"fmt"

"github.com/ChainSafe/gossamer/dot/network"
parachaintypes "github.com/ChainSafe/gossamer/dot/parachain/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/pkg/scale"
"github.com/libp2p/go-libp2p/core/peer"
)

const MaxValidationMessageSize uint64 = 100 * 1024

type ValidationProtocolV1 struct {
// TODO: Implement this struct https://github.com/ChainSafe/gossamer/issues/3318
// UncheckedSignedAvailabilityBitfield a signed bitfield with signature not yet checked
type UncheckedSignedAvailabilityBitfield struct {
// The payload is part of the signed data. The rest is the signing context,
// which is known both at signing and at validation.
Payload scale.BitVec `scale:"1"`

// The index of the validator signing this statement.
ValidatorIndex parachaintypes.ValidatorIndex `scale:"2"`

// The signature by the validator of the signed payload.
Signature ValidatorSignature `scale:"3"`
}

// Bitfield avalibility bitfield for given relay-parent hash
type Bitfield struct {
Hash common.Hash `scale:"1"`
UncheckedSignedAvailabilityBitfield UncheckedSignedAvailabilityBitfield `scale:"2"`
}

// Index returns the VaryingDataType Index
func (Bitfield) Index() uint {
return 0
}

// BitfieldDistributionMessage Network messages used by bitfield distribution subsystem
type BitfieldDistributionMessage scale.VaryingDataType

// NewBitfieldDistributionMessageVDT returns a new BitfieldDistributionMessage VaryingDataType
func NewBitfieldDistributionMessageVDT() BitfieldDistributionMessage {
vdt := scale.MustNewVaryingDataType(Bitfield{})
return BitfieldDistributionMessage(vdt)
}

// New creates new BitfieldDistributionMessage
func (BitfieldDistributionMessage) New() BitfieldDistributionMessage {
return NewBitfieldDistributionMessageVDT()
}

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

*bdm = BitfieldDistributionMessage(vdt)
return nil
}

// Value returns the value from the underlying VaryingDataType
func (bdm *BitfieldDistributionMessage) Value() (scale.VaryingDataTypeValue, error) {
vdt := scale.VaryingDataType(*bdm)
return vdt.Value()
}

// BitfieldDistribution struct holding BitfieldDistributionMessage
type BitfieldDistribution struct {
BitfieldDistributionMessage
}

// Index VaryingDataType index of Bitfield Distribution
func (BitfieldDistribution) Index() uint {
return 1
}

// ApprovalDistribution struct holding ApprovalDistributionMessage
type ApprovalDistribution struct {
ApprovalDistributionMessage
}

// Index VaryingDataType index of ApprovalDistribution
func (ApprovalDistribution) Index() uint {
return 4
}

// StatementDistribution struct holding StatementDistributionMessage
type StatementDistribution struct {
StatementDistributionMessage
}

// Index VaryingDataType index for StatementDistribution
func (StatementDistribution) Index() uint {
return 3
}

// ValidationProtocol VaryingDataType for ValidationProtocol
type ValidationProtocol scale.VaryingDataType

// NewValidationProtocolVDT constructor or ValidationProtocol VaryingDataType
func NewValidationProtocolVDT() ValidationProtocol {
vdt, err := scale.NewVaryingDataType(BitfieldDistribution{}, StatementDistribution{}, ApprovalDistribution{})
if err != nil {
panic(err)
}
return ValidationProtocol(vdt)
}

// New returns new ValidationProtocol VDT
func (ValidationProtocol) New() ValidationProtocol {
return NewValidationProtocolVDT()
}

// Value returns the value from the underlying VaryingDataType
func (vp *ValidationProtocol) Value() (scale.VaryingDataTypeValue, error) {
vdt := scale.VaryingDataType(*vp)
return vdt.Value()
}

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

*vp = ValidationProtocol(vdt)
return nil

}

// Type returns ValidationMsgType
func (*ValidationProtocolV1) Type() network.MessageType {
func (*ValidationProtocol) Type() network.MessageType {
return network.ValidationMsgType
}

// Hash returns the hash of the CollationProtocolV1
func (vp *ValidationProtocolV1) Hash() (common.Hash, error) {
func (vp *ValidationProtocol) Hash() (common.Hash, error) {
encMsg, err := vp.Encode()
if err != nil {
return common.Hash{}, fmt.Errorf("cannot encode message: %w", err)
Expand All @@ -34,7 +153,7 @@ func (vp *ValidationProtocolV1) Hash() (common.Hash, error) {
}

// Encode a collator protocol message using scale encode
func (vp *ValidationProtocolV1) Encode() ([]byte, error) {
func (vp *ValidationProtocol) Encode() ([]byte, error) {
enc, err := scale.Marshal(*vp)
if err != nil {
return enc, err
Expand All @@ -43,7 +162,7 @@ func (vp *ValidationProtocolV1) Encode() ([]byte, error) {
}

func decodeValidationMessage(in []byte) (network.NotificationsMessage, error) {
validationMessage := ValidationProtocolV1{}
validationMessage := ValidationProtocol{}

err := scale.Unmarshal(in, &validationMessage)
if err != nil {
Expand Down
Loading

0 comments on commit b9d545c

Please sign in to comment.