Skip to content

Commit

Permalink
simpliy ApplyChangeSet
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Jun 15, 2023
1 parent b7020ed commit 954006e
Showing 1 changed file with 17 additions and 28 deletions.
45 changes: 17 additions & 28 deletions memiavl/multitree.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"cosmossdk.io/errors"

storetypes "github.com/cosmos/cosmos-sdk/store/types"
"github.com/cosmos/iavl"
"github.com/tidwall/wal"
"golang.org/x/exp/slices"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -253,47 +252,37 @@ func (t *MultiTree) ApplyUpgrades(upgrades []*TreeNameUpgrade) error {
func (t *MultiTree) ApplyChangeSet(changeSets []*NamedChangeSet, updateCommitInfo bool) ([]byte, int64, error) {
version := nextVersion(t.lastCommitInfo.Version, t.initialVersion)

var (
infos []storetypes.StoreInfo
csIndex int
)
for _, entry := range t.trees {
var changeSet iavl.ChangeSet
for _, cs := range changeSets {
tree := t.trees[t.treesByName[cs.Name]].tree

if csIndex < len(changeSets) && entry.name == changeSets[csIndex].Name {
changeSet = changeSets[csIndex].Changeset
csIndex++
}
hash, v, err := entry.tree.ApplyChangeSet(changeSet, updateCommitInfo)
_, v, err := tree.ApplyChangeSet(cs.Changeset, updateCommitInfo)
if err != nil {
return nil, 0, err
}
if v != version {
return nil, 0, fmt.Errorf("multi tree version don't match(%d != %d)", v, version)
}
if updateCommitInfo {
infos = append(infos, storetypes.StoreInfo{
}

var hash []byte
if updateCommitInfo {
infos := make([]storetypes.StoreInfo, len(t.trees))
for i, entry := range t.trees {
infos[i] = storetypes.StoreInfo{
Name: entry.name,
CommitId: storetypes.CommitID{
Version: v,
Hash: hash,
Version: entry.tree.Version(),
Hash: entry.tree.RootHash(),
},
})
}
}
}

if csIndex != len(changeSets) {
return nil, 0, fmt.Errorf("non-exhaustive change sets")
}

t.lastCommitInfo.Version = version
t.lastCommitInfo.StoreInfos = infos

var hash []byte
if updateCommitInfo {
t.lastCommitInfo.Version = version
t.lastCommitInfo.StoreInfos = infos
hash = t.lastCommitInfo.Hash()
}
return hash, t.lastCommitInfo.Version, nil

return hash, version, nil
}

// UpdateCommitInfo update lastCommitInfo based on current status of trees.
Expand Down

0 comments on commit 954006e

Please sign in to comment.