Skip to content

Commit

Permalink
feat(lib/runtime/wasmer): report grandpa equivocations (#3007)
Browse files Browse the repository at this point in the history
Co-authored-by: Quentin McGaw <quentin.mcgaw@gmail.com>
  • Loading branch information
jimjbrettj and qdm12 authored Feb 9, 2023
1 parent 507cd2e commit e63aeea
Show file tree
Hide file tree
Showing 28 changed files with 1,764 additions and 61 deletions.
6 changes: 6 additions & 0 deletions dot/core/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
rtstorage "github.com/ChainSafe/gossamer/lib/runtime/storage"
Expand Down Expand Up @@ -54,6 +55,11 @@ type RuntimeInstance interface {
RandomSeed()
OffchainWorker()
GenerateSessionKeys()
GrandpaGenerateKeyOwnershipProof(authSetID uint64, authorityID ed25519.PublicKeyBytes) (
types.GrandpaOpaqueKeyOwnershipProof, error)
GrandpaSubmitReportEquivocationUnsignedExtrinsic(
equivocationProof types.GrandpaEquivocationProof, keyOwnershipProof types.GrandpaOpaqueKeyOwnershipProof,
) error
}

// BlockState interface for block state methods
Expand Down
30 changes: 30 additions & 0 deletions dot/core/mocks_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dot/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/services"
Expand Down Expand Up @@ -69,4 +70,9 @@ type runtimeInterface interface {
RandomSeed()
OffchainWorker()
GenerateSessionKeys()
GrandpaGenerateKeyOwnershipProof(authSetID uint64, authorityID ed25519.PublicKeyBytes) (
types.GrandpaOpaqueKeyOwnershipProof, error)
GrandpaSubmitReportEquivocationUnsignedExtrinsic(
equivocationProof types.GrandpaEquivocationProof, keyOwnershipProof types.GrandpaOpaqueKeyOwnershipProof,
) error
}
6 changes: 6 additions & 0 deletions dot/rpc/modules/interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package modules
import (
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
Expand Down Expand Up @@ -39,4 +40,9 @@ type Runtime interface {
RandomSeed()
OffchainWorker()
GenerateSessionKeys()
GrandpaGenerateKeyOwnershipProof(authSetID uint64, authorityID ed25519.PublicKeyBytes) (
types.GrandpaOpaqueKeyOwnershipProof, error)
GrandpaSubmitReportEquivocationUnsignedExtrinsic(
equivocationProof types.GrandpaEquivocationProof, keyOwnershipProof types.GrandpaOpaqueKeyOwnershipProof,
) error
}
6 changes: 6 additions & 0 deletions dot/state/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/ChainSafe/chaindb"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/ed25519"
"github.com/ChainSafe/gossamer/lib/keystore"
"github.com/ChainSafe/gossamer/lib/runtime"
"github.com/ChainSafe/gossamer/lib/transaction"
Expand Down Expand Up @@ -94,6 +95,11 @@ type Runtime interface {
RandomSeed()
OffchainWorker()
GenerateSessionKeys()
GrandpaGenerateKeyOwnershipProof(authSetID uint64, authorityID ed25519.PublicKeyBytes) (
types.GrandpaOpaqueKeyOwnershipProof, error)
GrandpaSubmitReportEquivocationUnsignedExtrinsic(
equivocationProof types.GrandpaEquivocationProof, keyOwnershipProof types.GrandpaOpaqueKeyOwnershipProof,
) error
}

// BabeConfigurer returns the babe configuration of the runtime.
Expand Down
30 changes: 30 additions & 0 deletions dot/sync/mock_runtime_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

63 changes: 63 additions & 0 deletions dot/types/grandpa.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,66 @@ type GrandpaVote struct {
func (v GrandpaVote) String() string {
return fmt.Sprintf("hash=%s number=%d", v.Hash, v.Number)
}

// GrandpaEquivocation is used to create a proof of equivocation
// https://github.com/paritytech/finality-grandpa/blob/19d251d0b0105d51a79d3c4532a9aae75a5035bd/src/lib.rs#L213 //nolint:lll
type GrandpaEquivocation struct {
RoundNumber uint64
ID [32]byte
FirstVote GrandpaVote
FirstSignature [64]byte
SecondVote GrandpaVote
SecondSignature [64]byte
}

// GrandpaEquivocationEnum is a wrapper object for GRANDPA equivocation proofs, useful for unifying prevote
// and precommit equivocations under a common type.
// https://github.com/paritytech/substrate/blob/fb22096d2ec6bf38e67ce811ad2c31415237a9a5/primitives/finality-grandpa/src/lib.rs#L272 //nolint:lll
type GrandpaEquivocationEnum scale.VaryingDataType

// Set sets a VaryingDataTypeValue using the underlying VaryingDataType
func (ge *GrandpaEquivocationEnum) Set(value scale.VaryingDataTypeValue) (err error) {
vdt := scale.VaryingDataType(*ge)
err = vdt.Set(value)
if err != nil {
return err
}
*ge = GrandpaEquivocationEnum(vdt)
return nil
}

// Value will return the value from the underlying VaryingDataType
func (ge *GrandpaEquivocationEnum) Value() (value scale.VaryingDataTypeValue, err error) {
vdt := scale.VaryingDataType(*ge)
return vdt.Value()
}

// NewGrandpaEquivocation returns a new VaryingDataType to represent a grandpa Equivocation
func NewGrandpaEquivocation() *GrandpaEquivocationEnum {
vdt := scale.MustNewVaryingDataType(PreVote{}, PreCommit{})
ge := GrandpaEquivocationEnum(vdt)
return &ge
}

// PreVote equivocation type for a prevote
type PreVote GrandpaEquivocation

// Index returns VDT index
func (PreVote) Index() uint { return 0 }

// PreCommit equivocation type for a precommit
type PreCommit GrandpaEquivocation

// Index returns VDT index
func (PreCommit) Index() uint { return 1 }

// GrandpaOpaqueKeyOwnershipProof contains a key ownership proof for reporting equivocations
// https://github.com/paritytech/substrate/blob/fb22096d2ec6bf38e67ce811ad2c31415237a9a5/primitives/finality-grandpa/src/lib.rs#L533 //nolint:lll
type GrandpaOpaqueKeyOwnershipProof []byte

// GrandpaEquivocationProof is used to report grandpa equivocations
// https://github.com/paritytech/substrate/blob/fb22096d2ec6bf38e67ce811ad2c31415237a9a5/primitives/finality-grandpa/src/lib.rs#L238 //nolint:lll
type GrandpaEquivocationProof struct {
SetID uint64
Equivocation GrandpaEquivocationEnum
}
Loading

0 comments on commit e63aeea

Please sign in to comment.