-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add authorship module. grandpa additions
- Loading branch information
Showing
58 changed files
with
2,682 additions
and
173 deletions.
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
Binary file not shown.
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,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, | ||
} | ||
} |
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,7 @@ | ||
package authorship | ||
|
||
type consts struct{} | ||
|
||
func newConstants() *consts { | ||
return &consts{} | ||
} |
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,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, | ||
} | ||
} |
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,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) {} |
Oops, something went wrong.