-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(parachain): Add StatementDistributionMessage varingDataType (#3316)
- Loading branch information
1 parent
8a9ae61
commit 79b0c99
Showing
6 changed files
with
498 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package parachain | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/pkg/scale" | ||
) | ||
|
||
// Statement is a result of candidate validation. It could be either `Valid` or `Seconded`. | ||
type StatementValues interface { | ||
Valid | Seconded | ||
} | ||
|
||
type Statement struct { | ||
inner any | ||
} | ||
|
||
func setStatement[Value StatementValues](mvdt *Statement, value Value) { | ||
mvdt.inner = value | ||
} | ||
|
||
func (mvdt *Statement) SetValue(value any) (err error) { | ||
switch value := value.(type) { | ||
case Valid: | ||
setStatement(mvdt, value) | ||
return | ||
|
||
case Seconded: | ||
setStatement(mvdt, value) | ||
return | ||
|
||
default: | ||
return fmt.Errorf("unsupported type") | ||
} | ||
} | ||
|
||
func (mvdt Statement) IndexValue() (index uint, value any, err error) { | ||
switch mvdt.inner.(type) { | ||
case Valid: | ||
return 2, mvdt.inner, nil | ||
|
||
case Seconded: | ||
return 1, mvdt.inner, nil | ||
|
||
} | ||
return 0, nil, scale.ErrUnsupportedVaryingDataTypeValue | ||
} | ||
|
||
func (mvdt Statement) Value() (value any, err error) { | ||
_, value, err = mvdt.IndexValue() | ||
return | ||
} | ||
|
||
func (mvdt Statement) ValueAt(index uint) (value any, err error) { | ||
switch index { | ||
case 2: | ||
return *new(Valid), nil | ||
|
||
case 1: | ||
return *new(Seconded), nil | ||
|
||
} | ||
return nil, scale.ErrUnknownVaryingDataTypeValue | ||
} | ||
|
||
// NewStatement returns a new Statement VaryingDataType | ||
func NewStatement() Statement { | ||
return Statement{} | ||
} | ||
|
||
// Seconded represents a statement that a validator seconds a candidate. | ||
type Seconded CommittedCandidateReceipt | ||
|
||
// Valid represents a statement that a validator has deemed a candidate valid. | ||
type Valid CandidateHash | ||
|
||
// 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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
package parachain | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ChainSafe/gossamer/lib/common" | ||
"github.com/ChainSafe/gossamer/pkg/scale" | ||
) | ||
|
||
type StatementDistributionMessageValues interface { | ||
SignedFullStatement | SecondedStatementWithLargePayload | ||
} | ||
|
||
// StatementDistributionMessage represents network messages used by the statement distribution subsystem | ||
type StatementDistributionMessage struct { | ||
inner any | ||
} | ||
|
||
func setStatementDistributionMessage[Value StatementDistributionMessageValues](mvdt *StatementDistributionMessage, value Value) { | ||
mvdt.inner = value | ||
} | ||
|
||
func (mvdt *StatementDistributionMessage) SetValue(value any) (err error) { | ||
switch value := value.(type) { | ||
case SignedFullStatement: | ||
setStatementDistributionMessage(mvdt, value) | ||
return | ||
|
||
case SecondedStatementWithLargePayload: | ||
setStatementDistributionMessage(mvdt, value) | ||
return | ||
|
||
default: | ||
return fmt.Errorf("unsupported type") | ||
} | ||
} | ||
|
||
func (mvdt StatementDistributionMessage) IndexValue() (index uint, value any, err error) { | ||
switch mvdt.inner.(type) { | ||
case SignedFullStatement: | ||
return 0, mvdt.inner, nil | ||
|
||
case SecondedStatementWithLargePayload: | ||
return 1, mvdt.inner, nil | ||
|
||
} | ||
return 0, nil, scale.ErrUnsupportedVaryingDataTypeValue | ||
} | ||
|
||
func (mvdt StatementDistributionMessage) Value() (value any, err error) { | ||
_, value, err = mvdt.IndexValue() | ||
return | ||
} | ||
|
||
func (mvdt StatementDistributionMessage) ValueAt(index uint) (value any, err error) { | ||
switch index { | ||
case 0: | ||
return *new(SignedFullStatement), nil | ||
|
||
case 1: | ||
return *new(SecondedStatementWithLargePayload), nil | ||
|
||
} | ||
return nil, scale.ErrUnknownVaryingDataTypeValue | ||
} | ||
|
||
// NewStatementDistributionMessage returns a new StatementDistributionMessage VaryingDataType | ||
func NewStatementDistributionMessage() StatementDistributionMessage { | ||
return StatementDistributionMessage{} | ||
} | ||
|
||
// SignedFullStatement represents a signed full statement under a given relay-parent. | ||
type SignedFullStatement struct { | ||
Hash common.Hash `scale:"1"` | ||
UncheckedSignedFullStatement UncheckedSignedFullStatement `scale:"2"` | ||
} | ||
|
||
// 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 | ||
|
||
// UncheckedSignedFullStatement is a Variant of `SignedFullStatement` where the signature has not yet been verified. | ||
type UncheckedSignedFullStatement struct { | ||
// The payload is part of the signed data. The rest is the signing context, | ||
// which is known both at signing and at validation. | ||
Payload Statement `scale:"1"` | ||
|
||
// The index of the validator signing this statement. | ||
ValidatorIndex ValidatorIndex `scale:"2"` | ||
|
||
// The signature by the validator of the signed payload. | ||
Signature ValidatorSignature `scale:"3"` | ||
} | ||
|
||
// StatementMetadata represents the data that makes a statement unique. | ||
type StatementMetadata struct { | ||
// 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"` | ||
} | ||
|
||
// ValidatorSignature represents the signature with which parachain validators sign blocks. | ||
type ValidatorSignature Signature | ||
|
||
// Signature represents a cryptographic signature. | ||
type Signature [64]byte |
Oops, something went wrong.