Skip to content

Commit

Permalink
fix: Cosmos SDK v0.46.8 state sync patch (#155)
Browse files Browse the repository at this point in the history
Merged with `github.com/chillyvee/cosmos-sdk@cv0.46.7`

Co-authored-by: Tasos Derisiotis <50984242+Eengineer1@users.noreply.github.com>
  • Loading branch information
ankurdotb and Eengineer1 committed Feb 28, 2023
1 parent 3a051e1 commit 9fb19e6
Show file tree
Hide file tree
Showing 9 changed files with 154 additions and 71 deletions.
2 changes: 2 additions & 0 deletions proto/cosmos/base/snapshots/v1beta1/snapshot.proto
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ message SnapshotItem {
// Since: cosmos-sdk 0.46
message SnapshotStoreItem {
string name = 1;
// version is ideally block height, but not guaranteed
uint64 version = 2;
}

// SnapshotIAVLItem is an exported IAVL node.
Expand Down
3 changes: 3 additions & 0 deletions snapshots/types/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ package types
// CurrentFormat is the currently used format for snapshots. Snapshots using the same format
// must be identical across all nodes for a given height, so this must be bumped when the binary
// snapshot output changes.

// Format #1 - Assumes all stores in multistore have version == height
// Format #2 - Transmits actual store version within multistore
const CurrentFormat uint32 = 2
106 changes: 69 additions & 37 deletions snapshots/types/snapshot.pb.go

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

1 change: 1 addition & 0 deletions store/cachekv/benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func BenchmarkDeepContextStack1(b *testing.B) {
func BenchmarkDeepContextStack3(b *testing.B) {
DoBenchmarkDeepContextStack(b, 3)
}

func BenchmarkDeepContextStack10(b *testing.B) {
DoBenchmarkDeepContextStack(b, 10)
}
Expand Down
55 changes: 50 additions & 5 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package rootmulti

import (
"encoding/hex"
"fmt"
"io"
"math"
Expand Down Expand Up @@ -193,6 +194,7 @@ func (rs *Store) LoadVersion(ver int64) error {
func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
infos := make(map[string]types.StoreInfo)

rs.logger.Debug("loadVersion", "ver", ver)
cInfo := &types.CommitInfo{}

// load old data if we are not version 0
Expand Down Expand Up @@ -229,6 +231,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
for _, key := range storesKeys {
storeParams := rs.storesParams[key]
commitID := rs.getCommitID(infos, key.Name())
rs.logger.Debug("loadVersion", "key", key, "commitID.Version", commitID.Version, "commitId.Hash", hex.EncodeToString(commitID.Hash))

// If it has been added, set the initial version
if upgrades.IsAdded(key.Name()) {
Expand Down Expand Up @@ -452,7 +455,6 @@ func (rs *Store) Commit() types.CommitID {
}
}

// CacheWrap implements CacheWrapper/Store/CommitStore.
func (rs *Store) CacheWrap() types.CacheWrap {
return rs.CacheMultiStore().(types.CacheWrap)
}
Expand Down Expand Up @@ -601,6 +603,7 @@ func (rs *Store) PruneStores(clearPruningManager bool, pruningHeights []int64) (
continue
}

// CV todo: && errCause != iavl.ErrVersionUnpruneable
if errCause := errors.Cause(err); errCause != nil && errCause != iavltree.ErrVersionDoesNotExist {
return err
}
Expand Down Expand Up @@ -747,30 +750,63 @@ func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error {
return strings.Compare(stores[i].name, stores[j].name) == -1
})

// CV Load the commit to get actual version ids for each store
infos := make(map[string]types.StoreInfo)
cInfo := &types.CommitInfo{}

var err error
cInfo, err = getCommitInfo(rs.db, int64(height))
if err != nil {
return err
}

// convert StoreInfos slice to map
for _, storeInfo := range cInfo.StoreInfos {
infos[storeInfo.Name] = storeInfo
}

storesKeys := make([]types.StoreKey, 0, len(rs.storesParams))
for key := range rs.storesParams {
storesKeys = append(storesKeys, key)
}
// Export each IAVL store. Stores are serialized as a stream of SnapshotItem Protobuf
// messages. The first item contains a SnapshotStore with store metadata (i.e. name),
// and the following messages contain a SnapshotNode (i.e. an ExportNode). Store changes
// are demarcated by new SnapshotStore items.
for _, store := range stores {
exporter, err := store.Export(int64(height))
commitID := rs.getCommitID(infos, store.name)
storeVersion := commitID.Version
rs.logger.Debug("Snapshot Begin", "store", store.name, "version", storeVersion, "hash", hex.EncodeToString(commitID.Hash))

exporter, err := store.Export(int64(storeVersion))
if exporter == nil {
rs.logger.Error("Snapshot Failed - exporter nil", "store", store.name)
// CV Skip stores that fail to get an exporter
// For example, when iavl/immutable_tree.ndb (nodedb) is nil
continue
}
if err != nil {
return err
}
defer exporter.Close()
err = protoWriter.WriteMsg(&snapshottypes.SnapshotItem{
Item: &snapshottypes.SnapshotItem_Store{
Store: &snapshottypes.SnapshotStoreItem{
Name: store.name,
Name: store.name,
Version: uint64(storeVersion),
},
},
})
if err != nil {
return err
}

nodeCount := 0
for {
node, err := exporter.Next()
if err == iavltree.ExportDone {
rs.logger.Debug("Snapshot Done", "store", store.name, "nodeCount", nodeCount)
nodeCount = 0
break
} else if err != nil {
return err
Expand All @@ -785,9 +821,11 @@ func (rs *Store) Snapshot(height uint64, protoWriter protoio.Writer) error {
},
},
})

if err != nil {
return err
}
nodeCount++
}
exporter.Close()
}
Expand Down Expand Up @@ -828,14 +866,21 @@ loop:
if !ok || store == nil {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrapf(sdkerrors.ErrLogic, "cannot import into non-IAVL store %q", item.Store.Name)
}
importer, err = store.Import(int64(height))
// Importer height must reflect the node height (which usually matches the block height, but not always)
rs.logger.Debug("Restore", "store", item.Store.Name, "version", item.Store.Version)
if item.Store.Version == 0 {
fmt.Printf("!!!!!!!!!!!!!!!!! Restore Error, Snapshot Missing Version, Use Another Peer !!!!!!!!!!!!!!!!")
rs.logger.Error("Restore Error, Snapshot Missing Version, Use Another Peer", "store", item.Store.Name, "version", item.Store.Version)
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(sdkerrors.ErrLogic, "Snapshot Without Store Version Not Supported, Use Another Peer")
}
importer, err = store.Import(int64(item.Store.Version))
if err != nil {
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(err, "import failed")
}
defer importer.Close()

case *snapshottypes.SnapshotItem_IAVL:
if importer == nil {
rs.logger.Error("Restore Error, Unexepected IAVL Node")
return snapshottypes.SnapshotItem{}, sdkerrors.Wrap(sdkerrors.ErrLogic, "received IAVL node item before store item")
}
if item.IAVL.Height > math.MaxInt8 {
Expand Down
1 change: 0 additions & 1 deletion store/streaming/constructor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ type fakeOptions struct{}
func (f *fakeOptions) Get(key string) interface{} {
if key == "streamers.file.write_dir" {
return "data/file_streamer"

}
return nil
}
Expand Down
6 changes: 4 additions & 2 deletions types/tx/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ import (
const MaxGasWanted = uint64((1 << 63) - 1)

// Interface implementation checks.
var _, _, _, _ codectypes.UnpackInterfacesMessage = &Tx{}, &TxBody{}, &AuthInfo{}, &SignerInfo{}
var _ sdk.Tx = &Tx{}
var (
_, _, _, _ codectypes.UnpackInterfacesMessage = &Tx{}, &TxBody{}, &AuthInfo{}, &SignerInfo{}
_ sdk.Tx = &Tx{}
)

// GetMsgs implements the GetMsgs method on sdk.Tx.
func (t *Tx) GetMsgs() []sdk.Msg {
Expand Down
50 changes: 24 additions & 26 deletions x/bank/migrations/v046/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,32 @@ import (
"github.com/cosmos/cosmos-sdk/x/bank/types"
)

var (
metaData = []types.Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
{"matom", uint32(3), []string{"milliatom"}},
{"atom", uint32(6), nil},
},
Base: "uatom",
Display: "atom",
var metaData = []types.Metadata{
{
Name: "Cosmos Hub Atom",
Symbol: "ATOM",
Description: "The native staking token of the Cosmos Hub.",
DenomUnits: []*types.DenomUnit{
{"uatom", uint32(0), []string{"microatom"}},
{"matom", uint32(3), []string{"milliatom"}},
{"atom", uint32(6), nil},
},
{
Name: "Token",
Symbol: "TOKEN",
Description: "The native staking token of the Token Hub.",
DenomUnits: []*types.DenomUnit{
{"1token", uint32(5), []string{"decitoken"}},
{"2token", uint32(4), []string{"centitoken"}},
{"3token", uint32(7), []string{"dekatoken"}},
},
Base: "utoken",
Display: "token",
Base: "uatom",
Display: "atom",
},
{
Name: "Token",
Symbol: "TOKEN",
Description: "The native staking token of the Token Hub.",
DenomUnits: []*types.DenomUnit{
{"1token", uint32(5), []string{"decitoken"}},
{"2token", uint32(4), []string{"centitoken"}},
{"3token", uint32(7), []string{"dekatoken"}},
},
}
)
Base: "utoken",
Display: "token",
},
}

func TestMigrateStore(t *testing.T) {
encCfg := simapp.MakeTestEncodingConfig()
Expand Down
Loading

0 comments on commit 9fb19e6

Please sign in to comment.