Skip to content

Commit

Permalink
add authorship module. grandpa additions
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed May 20, 2024
1 parent cfafad2 commit b5c54b9
Show file tree
Hide file tree
Showing 58 changed files with 2,682 additions and 173 deletions.
50 changes: 50 additions & 0 deletions api/grandpa/module.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package grandpa

import (
"bytes"

sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/constants/metadata"
"github.com/LimeChain/gosemble/frame/grandpa"
grandpatypes "github.com/LimeChain/gosemble/primitives/grandpa"
"github.com/LimeChain/gosemble/primitives/hashing"
"github.com/LimeChain/gosemble/primitives/log"
"github.com/LimeChain/gosemble/primitives/session"
primitives "github.com/LimeChain/gosemble/primitives/types"
"github.com/LimeChain/gosemble/utils"
)
Expand Down Expand Up @@ -65,6 +69,52 @@ func (m Module) CurrentSetId() int64 {
return m.memUtils.BytesToOffsetAndSize(setId.Bytes())
}

func (m Module) SubmitReportEquivocationUnsignedExtrinsic(dataPtr int32, dataLen int32) int64 {
b := m.memUtils.GetWasmMemorySlice(dataPtr, dataLen)
buffer := bytes.NewBuffer(b)

equivocationProof, err := grandpatypes.DecodeEquivocationProof(buffer)
if err != nil {
m.logger.Critical(err.Error())
}

opaqueKeyOwnershipProof, err := sc.DecodeSequence[sc.U8](buffer)
if err != nil {
m.logger.Critical(err.Error())
}

keyOwnerProof, err := session.DecodeMembershipProof(bytes.NewBuffer(sc.SequenceU8ToBytes(opaqueKeyOwnershipProof)))
if err != nil {
m.logger.Critical(err.Error())
}

res, err := m.grandpa.SubmitUnsignedEquivocationReport(equivocationProof, keyOwnerProof)
if err != nil {
m.logger.Critical(err.Error())
}

return m.memUtils.BytesToOffsetAndSize(res.Bytes())
}

func (m Module) GenerateKeyOwnershipProof(dataPtr int32, dataLen int32) int64 {
b := m.memUtils.GetWasmMemorySlice(dataPtr, dataLen)
buffer := bytes.NewBuffer(b)

_, err := sc.DecodeU64(buffer)
if err != nil {
m.logger.Critical(err.Error())
}

authorityId, err := primitives.DecodeAccountId(buffer)
if err != nil {
m.logger.Critical(err.Error())
}

res := m.grandpa.HistoricalKeyOwnershipProof(authorityId)
return m.memUtils.BytesToOffsetAndSize(res.Bytes())

}

// Metadata returns the runtime api metadata of the module.
func (m Module) Metadata() primitives.RuntimeApiMetadata {
methods := sc.Sequence[primitives.RuntimeApiMethodMetadata]{
Expand Down
Binary file modified build/runtime.wasm
Binary file not shown.
20 changes: 20 additions & 0 deletions frame/authorship/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package authorship

import (
"github.com/LimeChain/gosemble/frame/system"
primitives "github.com/LimeChain/gosemble/primitives/types"
)

type Config struct {
FindAuthor primitives.FindAuthor[primitives.AccountId]
EventHandler EventHandler
SystemModule system.Module
}

func NewConfig(findAuthor primitives.FindAuthor[primitives.AccountId], eventHandler EventHandler, systemModule system.Module) *Config {
return &Config{
FindAuthor: findAuthor,
EventHandler: eventHandler,
SystemModule: systemModule,
}
}
7 changes: 7 additions & 0 deletions frame/authorship/constants.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package authorship

type consts struct{}

func newConstants() *consts {
return &consts{}
}
41 changes: 41 additions & 0 deletions frame/authorship/metadata.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package authorship

import (
sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/constants/metadata"
primitives "github.com/LimeChain/gosemble/primitives/types"
)

func (m module) metadataStorage() sc.Option[primitives.MetadataModuleStorage] {
return sc.NewOption[primitives.MetadataModuleStorage](primitives.MetadataModuleStorage{
Prefix: m.name(),
Items: sc.Sequence[primitives.MetadataModuleStorageEntry]{
primitives.NewMetadataModuleStorageEntry(
"Author",
primitives.MetadataModuleStorageEntryModifierOptional,
primitives.NewMetadataModuleStorageEntryDefinitionPlain(sc.ToCompact(metadata.TypesAddress32)),
"Author of current block.",
),
},
})
}

func (m module) Metadata() primitives.MetadataModule {
dataV14 := primitives.MetadataModuleV14{
Name: m.name(),
Storage: m.metadataStorage(),
Call: sc.NewOption[sc.Compact](nil),
CallDef: sc.NewOption[primitives.MetadataDefinitionVariant](nil),
Event: sc.NewOption[sc.Compact](nil),
EventDef: sc.NewOption[primitives.MetadataDefinitionVariant](nil),
Constants: sc.Sequence[primitives.MetadataModuleConstant]{},
Error: sc.NewOption[sc.Compact](nil),
ErrorDef: sc.NewOption[primitives.MetadataDefinitionVariant](nil),
Index: m.index,
}

return primitives.MetadataModule{
Version: primitives.ModuleVersion14,
ModuleV14: dataV14,
}
}
125 changes: 125 additions & 0 deletions frame/authorship/module.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package authorship

import (
"bytes"

"github.com/LimeChain/gosemble/frame/system"
"github.com/LimeChain/gosemble/primitives/log"

sc "github.com/LimeChain/goscale"
"github.com/LimeChain/gosemble/hooks"
primitives "github.com/LimeChain/gosemble/primitives/types"
)

const name = sc.Str("Authorship")

type Module interface {
primitives.Module

Author() sc.Option[primitives.AccountId]
}

type module struct {
primitives.DefaultInherentProvider
hooks.DefaultDispatchModule
index sc.U8
config *Config
storage *storage
constants *consts
functions map[sc.U8]primitives.Call
systemModule system.Module
mdGenerator *primitives.MetadataTypeGenerator
logger log.Logger
}

func New(index sc.U8, config *Config, mdGenerator *primitives.MetadataTypeGenerator, logger log.Logger) Module {
storage := newStorage()

return module{
index: index,
config: config,
storage: storage,
constants: newConstants(),
mdGenerator: mdGenerator,
logger: logger,
}
}

func (m module) GetIndex() sc.U8 {
return m.index
}

func (m module) name() sc.Str {
return name
}

func (m module) Functions() map[sc.U8]primitives.Call {
return m.functions
}

func (m module) PreDispatch(_ primitives.Call) (sc.Empty, error) {
return sc.Empty{}, nil
}

func (m module) ValidateUnsigned(_ primitives.TransactionSource, _ primitives.Call) (primitives.ValidTransaction, error) {
return primitives.ValidTransaction{}, primitives.NewTransactionValidityError(primitives.NewUnknownTransactionNoUnsignedValidator())
}

func (m module) OnInitialize(_ sc.U64) (primitives.Weight, error) {
if author := m.Author(); author.HasValue {
m.config.EventHandler.NoteAuthor(author.Value)
}
return primitives.WeightZero(), nil
}

func (m module) OnFinalize(_ sc.U64) error {
// ensure we never go to trie with these values.
m.storage.Author.Clear()
return nil
}

// Fetch the author of the block.
//
// This is safe to invoke in `on_initialize` implementations, as well
// as afterwards.
func (m module) Author() sc.Option[primitives.AccountId] {
// Check the memorized storage value.
author, err := m.storage.Author.GetBytes()
if err != nil {
return sc.NewOption[primitives.AccountId](nil)
}

if author.HasValue {
author, err := primitives.DecodeAccountId(bytes.NewBuffer(sc.SequenceU8ToBytes(author.Value)))
if err != nil {
return sc.NewOption[primitives.AccountId](nil)
}
return sc.NewOption[primitives.AccountId](author)
}

digest, err := m.config.SystemModule.StorageDigest()
if err != nil {
return sc.NewOption[primitives.AccountId](nil)
}

preRuntimeDigests, err := digest.PreRuntimes()
if err != nil {
return sc.NewOption[primitives.AccountId](nil)
}

authorId, err := m.config.FindAuthor.FindAuthor(preRuntimeDigests)
if err != nil {
return sc.NewOption[primitives.AccountId](nil)
}

m.storage.Author.Put(authorId.Value)
return authorId
}

type EventHandler interface {
NoteAuthor(author primitives.AccountId)
}

type DefaulthEventHandler struct{}

func (d DefaulthEventHandler) NoteAuthor(author primitives.AccountId) {}
Loading

0 comments on commit b5c54b9

Please sign in to comment.