Skip to content

Commit

Permalink
feat!: Add hooks to allow app modules to add things to state-sync (ba…
Browse files Browse the repository at this point in the history
…ckport cosmos#10961) (cosmos#11267)

* feat!: Add hooks to allow app modules to add things to state-sync (cosmos#10961)

Closes: cosmos#7340

- Support registering multiple snapshotters in snapshot manager.
- Append the extension snapshotters to existing snapshot stream.

~TODO: testing.~
- existing tests are fixed

---

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

(cherry picked from commit 7e18e9f)

* fix conflicts

* avoid api breakage

* changelog

* fix: rootmulti's Restore don't return the next unknown item as expected (cosmos#11286)

Solution:
- return the next unknown item and add a unit test to ensure that.

---

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)

Co-authored-by: yihuang <huang@crypto.com>
  • Loading branch information
2 people authored and Eengineer1 committed Aug 26, 2022
1 parent a8cd042 commit bf23ab5
Show file tree
Hide file tree
Showing 14 changed files with 387 additions and 1,388 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Ref: https://keepachangelog.com/en/1.0.0/

* [\#9576](https://github.com/cosmos/cosmos-sdk/pull/9576) Add debug error message to query result when enabled
* (types) [\#11200](https://github.com/cosmos/cosmos-sdk/pull/11200) Added `Min()` and `Max()` operations on sdk.Coins.
* [#11267](https://github.com/cosmos/cosmos-sdk/pull/11267) Add hooks to allow app modules to add things to state-sync (backport #10961).

## [v0.45.1](https://github.com/cosmos/cosmos-sdk/releases/tag/v0.45.1) - 2022-02-03

Expand Down
16 changes: 16 additions & 0 deletions baseapp/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,22 @@ func DefaultStoreLoader(ms sdk.CommitMultiStore) error {
return ms.LoadLatestVersion()
}

// CommitMultiStore returns the root multi-store.
// App constructor can use this to access the `cms`.
// UNSAFE: only safe to use during app initialization.
func (app *BaseApp) CommitMultiStore() sdk.CommitMultiStore {
if app.sealed {
panic("cannot call CommitMultiStore() after baseapp is sealed")
}
return app.cms
}

// SnapshotManager returns the snapshot manager.
// application use this to register extra extension snapshotters.
func (app *BaseApp) SnapshotManager() *snapshots.Manager {
return app.snapshotManager
}

// LoadVersion loads the BaseApp application version. It will panic if called
// more than once on a running baseapp.
func (app *BaseApp) LoadVersion(version int64) error {
Expand Down
33 changes: 3 additions & 30 deletions proto/cosmos/base/snapshots/v1beta1/snapshot.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,65 +20,38 @@ message Metadata {
}

// SnapshotItem is an item contained in a rootmulti.Store snapshot.
//
// Since: cosmos-sdk 0.46
message SnapshotItem {
// item is the specific type of snapshot item.
oneof item {
SnapshotStoreItem store = 1;
SnapshotIAVLItem iavl = 2 [(gogoproto.customname) = "IAVL"];
SnapshotExtensionMeta extension = 3;
SnapshotExtensionPayload extension_payload = 4;
SnapshotKVItem kv = 5 [(gogoproto.customname) = "KV"];
SnapshotSchema schema = 6;
}
}

// SnapshotStoreItem contains metadata about a snapshotted store.
//
// Since: cosmos-sdk 0.46
message SnapshotStoreItem {
string name = 1;
}

// SnapshotIAVLItem is an exported IAVL node.
//
// Since: cosmos-sdk 0.46
message SnapshotIAVLItem {
bytes key = 1;
bytes value = 2;
bytes key = 1;
bytes value = 2;
// version is block height
int64 version = 3;
// height is depth of the tree.
int32 height = 4;
int32 height = 4;
}

// SnapshotExtensionMeta contains metadata about an external snapshotter.
//
// Since: cosmos-sdk 0.46
message SnapshotExtensionMeta {
string name = 1;
uint32 format = 2;
}

// SnapshotExtensionPayload contains payloads of an external snapshotter.
//
// Since: cosmos-sdk 0.46
message SnapshotExtensionPayload {
bytes payload = 1;
}

// SnapshotKVItem is an exported Key/Value Pair
//
// Since: cosmos-sdk 0.46
message SnapshotKVItem {
bytes key = 1;
bytes value = 2;
}

// SnapshotSchema is an exported schema of smt store
//
// Since: cosmos-sdk 0.46
message SnapshotSchema {
repeated bytes keys = 1;
}
63 changes: 25 additions & 38 deletions server/mock/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,15 @@ import (
protoio "github.com/gogo/protobuf/io"
dbm "github.com/tendermint/tm-db"

pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
store "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)

var _ sdk.MultiStore = multiStore{}

type multiStore struct {
kv map[storetypes.StoreKey]kvStore
kv map[sdk.StoreKey]kvStore
}

func (ms multiStore) CacheMultiStore() sdk.CacheMultiStore {
Expand All @@ -26,15 +25,15 @@ func (ms multiStore) CacheMultiStoreWithVersion(_ int64) (sdk.CacheMultiStore, e
panic("not implemented")
}

func (ms multiStore) CacheWrap() storetypes.CacheWrap {
func (ms multiStore) CacheWrap() sdk.CacheWrap {
panic("not implemented")
}

func (ms multiStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) storetypes.CacheWrap {
func (ms multiStore) CacheWrapWithTrace(_ io.Writer, _ sdk.TraceContext) sdk.CacheWrap {
panic("not implemented")
}

func (ms multiStore) CacheWrapWithListeners(_ storetypes.StoreKey, _ []storetypes.WriteListener) storetypes.CacheWrap {
func (ms multiStore) CacheWrapWithListeners(_ store.StoreKey, _ []store.WriteListener) store.CacheWrap {
panic("not implemented")
}

Expand All @@ -50,75 +49,67 @@ func (ms multiStore) SetTracer(w io.Writer) sdk.MultiStore {
panic("not implemented")
}

func (ms multiStore) AddListeners(key storetypes.StoreKey, listeners []storetypes.WriteListener) {
func (ms multiStore) AddListeners(key store.StoreKey, listeners []store.WriteListener) {
panic("not implemented")
}

func (ms multiStore) ListeningEnabled(key storetypes.StoreKey) bool {
func (ms multiStore) ListeningEnabled(key store.StoreKey) bool {
panic("not implemented")
}

func (ms multiStore) Commit() storetypes.CommitID {
func (ms multiStore) Commit() sdk.CommitID {
panic("not implemented")
}

func (ms multiStore) LastCommitID() storetypes.CommitID {
func (ms multiStore) LastCommitID() sdk.CommitID {
panic("not implemented")
}

func (ms multiStore) SetPruning(opts pruningtypes.PruningOptions) {
func (ms multiStore) SetPruning(opts sdk.PruningOptions) {
panic("not implemented")
}

func (ms multiStore) GetPruning() pruningtypes.PruningOptions {
func (ms multiStore) GetPruning() sdk.PruningOptions {
panic("not implemented")
}

func (ms multiStore) GetCommitKVStore(key storetypes.StoreKey) storetypes.CommitKVStore {
func (ms multiStore) GetCommitKVStore(key sdk.StoreKey) sdk.CommitKVStore {
panic("not implemented")
}

func (ms multiStore) GetCommitStore(key storetypes.StoreKey) storetypes.CommitStore {
func (ms multiStore) GetCommitStore(key sdk.StoreKey) sdk.CommitStore {
panic("not implemented")
}

func (ms multiStore) MountStoreWithDB(key storetypes.StoreKey, typ storetypes.StoreType, db dbm.DB) {
func (ms multiStore) MountStoreWithDB(key sdk.StoreKey, typ sdk.StoreType, db dbm.DB) {
ms.kv[key] = kvStore{store: make(map[string][]byte)}
}

func (ms multiStore) LoadLatestVersion() error {
return nil
}

func (ms multiStore) LoadLatestVersionAndUpgrade(upgrades *storetypes.StoreUpgrades) error {
func (ms multiStore) LoadLatestVersionAndUpgrade(upgrades *store.StoreUpgrades) error {
return nil
}

func (ms multiStore) LoadVersionAndUpgrade(ver int64, upgrades *storetypes.StoreUpgrades) error {
func (ms multiStore) LoadVersionAndUpgrade(ver int64, upgrades *store.StoreUpgrades) error {
panic("not implemented")
}

func (ms multiStore) LoadVersion(ver int64) error {
panic("not implemented")
}

func (ms multiStore) GetKVStore(key storetypes.StoreKey) sdk.KVStore {
func (ms multiStore) GetKVStore(key sdk.StoreKey) sdk.KVStore {
return ms.kv[key]
}

func (ms multiStore) GetStore(key storetypes.StoreKey) sdk.Store {
func (ms multiStore) GetStore(key sdk.StoreKey) sdk.Store {
panic("not implemented")
}

func (ms multiStore) GetStoreType() storetypes.StoreType {
panic("not implemented")
}

func (ms multiStore) PruneSnapshotHeight(height int64) {
panic("not implemented")
}

func (ms multiStore) SetSnapshotInterval(snapshotInterval uint64) {
func (ms multiStore) GetStoreType() sdk.StoreType {
panic("not implemented")
}

Expand All @@ -129,10 +120,6 @@ func (ms multiStore) SetIAVLCacheSize(size int) {
panic("not implemented")
}

func (ms multiStore) SetIAVLCacheSize(size int) {
panic("not implemented")
}

func (ms multiStore) SetInitialVersion(version int64) error {
panic("not implemented")
}
Expand All @@ -153,19 +140,19 @@ type kvStore struct {
store map[string][]byte
}

func (kv kvStore) CacheWrap() storetypes.CacheWrap {
func (kv kvStore) CacheWrap() sdk.CacheWrap {
panic("not implemented")
}

func (kv kvStore) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) storetypes.CacheWrap {
func (kv kvStore) CacheWrapWithTrace(w io.Writer, tc sdk.TraceContext) sdk.CacheWrap {
panic("not implemented")
}

func (kv kvStore) CacheWrapWithListeners(_ storetypes.StoreKey, _ []storetypes.WriteListener) storetypes.CacheWrap {
func (kv kvStore) CacheWrapWithListeners(_ store.StoreKey, _ []store.WriteListener) store.CacheWrap {
panic("not implemented")
}

func (kv kvStore) GetStoreType() storetypes.StoreType {
func (kv kvStore) GetStoreType() sdk.StoreType {
panic("not implemented")
}

Expand All @@ -183,7 +170,7 @@ func (kv kvStore) Has(key []byte) bool {
}

func (kv kvStore) Set(key, value []byte) {
storetypes.AssertValidKey(key)
store.AssertValidKey(key)
kv.store[string(key)] = value
}

Expand Down Expand Up @@ -216,5 +203,5 @@ func (kv kvStore) ReverseSubspaceIterator(prefix []byte) sdk.Iterator {
}

func NewCommitMultiStore() sdk.CommitMultiStore {
return multiStore{kv: make(map[storetypes.StoreKey]kvStore)}
return multiStore{kv: make(map[sdk.StoreKey]kvStore)}
}
Loading

0 comments on commit bf23ab5

Please sign in to comment.