Skip to content

Commit

Permalink
sdk46 compact WorkingHash
Browse files Browse the repository at this point in the history
  • Loading branch information
yihuang committed Sep 1, 2023
1 parent 7545feb commit 777e574
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 7 additions & 0 deletions memiavl/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,13 @@ func (db *DB) SaveVersion(updateCommitInfo bool) ([]byte, int64, error) {
return db.MultiTree.SaveVersion(updateCommitInfo)

Check warning on line 792 in memiavl/db.go

View check run for this annotation

Codecov / codecov/patch

memiavl/db.go#L792

Added line #L792 was not covered by tests
}

func (db *DB) WorkingCommitInfo() *storetypes.CommitInfo {
db.mtx.Lock()
defer db.mtx.Unlock()

return db.MultiTree.WorkingCommitInfo()

Check warning on line 799 in memiavl/db.go

View check run for this annotation

Codecov / codecov/patch

memiavl/db.go#L795-L799

Added lines #L795 - L799 were not covered by tests
}

func (db *DB) WorkingHash() []byte {
db.mtx.Lock()
defer db.mtx.Unlock()
Expand Down
15 changes: 10 additions & 5 deletions memiavl/multitree.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,9 +269,14 @@ func (t *MultiTree) ApplyChangeSets(changeSets []*NamedChangeSet) error {
return nil
}

func (t *MultiTree) WorkingHash() []byte {
// WorkingCommitInfo returns the commit info for the working tree
func (t *MultiTree) WorkingCommitInfo() *storetypes.CommitInfo {
version := nextVersion(t.lastCommitInfo.Version, t.initialVersion)
return t.buildCommitInfo(version).Hash()
return t.buildCommitInfo(version)

Check warning on line 275 in memiavl/multitree.go

View check run for this annotation

Codecov / codecov/patch

memiavl/multitree.go#L273-L275

Added lines #L273 - L275 were not covered by tests
}

func (t *MultiTree) WorkingHash() []byte {
return t.WorkingCommitInfo().Hash()

Check warning on line 279 in memiavl/multitree.go

View check run for this annotation

Codecov / codecov/patch

memiavl/multitree.go#L278-L279

Added lines #L278 - L279 were not covered by tests
}

// SaveVersion bumps the versions of all the stores and optionally returns the new app hash
Expand All @@ -294,7 +299,7 @@ func (t *MultiTree) SaveVersion(updateCommitInfo bool) ([]byte, int64, error) {
return hash, t.lastCommitInfo.Version, nil
}

func (t *MultiTree) buildCommitInfo(version int64) storetypes.CommitInfo {
func (t *MultiTree) buildCommitInfo(version int64) *storetypes.CommitInfo {
var infos []storetypes.StoreInfo
for _, entry := range t.trees {
infos = append(infos, storetypes.StoreInfo{
Expand All @@ -306,7 +311,7 @@ func (t *MultiTree) buildCommitInfo(version int64) storetypes.CommitInfo {
})
}

return storetypes.CommitInfo{
return &storetypes.CommitInfo{
Version: version,
StoreInfos: infos,
}
Expand All @@ -315,7 +320,7 @@ func (t *MultiTree) buildCommitInfo(version int64) storetypes.CommitInfo {
// UpdateCommitInfo update lastCommitInfo based on current status of trees.
// it's needed if `updateCommitInfo` is set to `false` in `ApplyChangeSet`.
func (t *MultiTree) UpdateCommitInfo() []byte {
t.lastCommitInfo = t.buildCommitInfo(t.lastCommitInfo.Version)
t.lastCommitInfo = *t.buildCommitInfo(t.lastCommitInfo.Version)
return t.lastCommitInfo.Hash()
}

Expand Down
7 changes: 5 additions & 2 deletions store/rootmulti/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,14 +90,17 @@ func (rs *Store) flush() error {
}

// WorkingHash returns the app hash of the working tree,
// it's only compatible with sdk 0.47 and later.
//
// Implements interface Committer.
func (rs *Store) WorkingHash() []byte {
if err := rs.flush(); err != nil {
panic(err)
}
return rs.db.WorkingHash()
commitInfo := rs.db.WorkingCommitInfo()
if rs.sdk46Compact {
commitInfo = amendCommitInfo(commitInfo, rs.storesParams)
}
return commitInfo.Hash()
}

// Implements interface Committer
Expand Down

0 comments on commit 777e574

Please sign in to comment.