From 2d5b67c859150dc3307a9c4647e9d44f0c3d77b6 Mon Sep 17 00:00:00 2001 From: Aaron Craelius Date: Thu, 15 Dec 2022 14:24:44 -0500 Subject: [PATCH] feat(core): add genesis API (#14223) Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> --- core/appmodule/genesis.go | 41 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 core/appmodule/genesis.go diff --git a/core/appmodule/genesis.go b/core/appmodule/genesis.go new file mode 100644 index 000000000000..77940ce5fee3 --- /dev/null +++ b/core/appmodule/genesis.go @@ -0,0 +1,41 @@ +package appmodule + +import ( + "context" + "io" +) + +// HasGenesis is the extension interface that modules should implement to handle +// genesis data and state initialization. +type HasGenesis interface { + AppModule + + // DefaultGenesis writes the default genesis for this module to the target. + DefaultGenesis(GenesisTarget) error + + // ValidateGenesis validates the genesis data read from the source. + ValidateGenesis(GenesisSource) error + + // InitGenesis initializes module state from the genesis source. + InitGenesis(context.Context, GenesisSource) error + + // ExportGenesis exports module state to the genesis target. + ExportGenesis(context.Context, GenesisTarget) error +} + +// GenesisSource is a source for genesis data in JSON format. It may abstract over a +// single JSON object or separate files for each field in a JSON object that can +// be streamed over. Modules should open a separate io.ReadCloser for each field that +// is required. When fields represent arrays they can efficiently be streamed +// over. If there is no data for a field, this function should return nil, nil. It is +// important that the caller closes the reader when done with it. +type GenesisSource = func(field string) (io.ReadCloser, error) + +// GenesisTarget is a target for writing genesis data in JSON format. It may +// abstract over a single JSON object or JSON in separate files that can be +// streamed over. Modules should open a separate io.WriteCloser for each field +// and should prefer writing fields as arrays when possible to support efficient +// iteration. It is important the caller closers the writer AND checks the error +// when done with it. It is expected that a stream of JSON data is written +// to the writer. +type GenesisTarget = func(field string) (io.WriteCloser, error)