Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem: version mismatch happen occasionally #1759

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* [#1724](https://github.com/crypto-org-chain/cronos/pull/1724) Include the fix of nonce management in batch tx in ethermint.
* [#1748](https://github.com/crypto-org-chain/cronos/pull/1748) Query with GetCFWithTS to compare both timestamp and key to avoid run fixdata multiple times.
* (versiondb) [#1751](https://github.com/crypto-org-chain/cronos/pull/1751) Add missing Destroy for read options to properly hold and release options reference.
* [#1759](https://github.com/crypto-org-chain/cronos/pull/1759) Fix version mismatch happen occasionally.

### Improvements

Expand Down
33 changes: 26 additions & 7 deletions app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,16 +1041,28 @@ func New(
}

if loadLatest {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
var qmsVersion int64
if app.qms != nil {
qmsVersion = app.qms.LatestVersion()
}

if app.qms != nil {
v1 := app.qms.LatestVersion()
v2 := app.LastBlockHeight()
if v1 > 0 && v1 < v2 {
if qmsVersion == 0 {
if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}
} else {
// make sure iavl version is not ahead of versiondb version
app.SetStoreLoader(VersionStoreLoader(qmsVersion))

if err := app.LoadLatestVersion(); err != nil {
tmos.Exit(err.Error())
}

// still keep the check for safety
iavlVersion := app.LastBlockHeight()
if qmsVersion < iavlVersion {
// try to prevent gap being created in versiondb
tmos.Exit(fmt.Sprintf("versiondb version %d lag behind iavl version %d", v1, v2))
tmos.Exit(fmt.Sprintf("versiondb version %d lag behind iavl version %d", qmsVersion, iavlVersion))
}
}

Expand Down Expand Up @@ -1491,3 +1503,10 @@ func (app *App) CheckTx(req *abci.RequestCheckTx) (*abci.ResponseCheckTx, error)

return app.BaseApp.CheckTx(req)
}

// VersionStoreLoader will be used when there's versiondb
func VersionStoreLoader(version int64) baseapp.StoreLoader {
return func(ms storetypes.CommitMultiStore) error {
return ms.LoadVersion(version)
}
}
27 changes: 24 additions & 3 deletions memiavl/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -560,9 +560,18 @@
if err != nil {
return 0, err
}
if err := db.wal.Write(entry.index, bz); err != nil {

lastIndex, err := db.wal.LastIndex()
if err != nil {
return 0, err
}
if entry.index < lastIndex+1 {
db.logger.Error("commit old version idempotently", "expected", lastIndex+1, "actual", entry.index)

Check warning on line 569 in memiavl/db.go

View check run for this annotation

Codecov / codecov/patch

memiavl/db.go#L569

Added line #L569 was not covered by tests
} else {
if err := db.wal.Write(entry.index, bz); err != nil {
return 0, err
}

Check warning on line 573 in memiavl/db.go

View check run for this annotation

Codecov / codecov/patch

memiavl/db.go#L572-L573

Added lines #L572 - L573 were not covered by tests
}
}
}

Expand Down Expand Up @@ -591,13 +600,25 @@
break
}

for _, entry := range entries {
lastIndex, err := db.wal.LastIndex()
if err != nil {
walQuit <- err
return
}

Check warning on line 607 in memiavl/db.go

View check run for this annotation

Codecov / codecov/patch

memiavl/db.go#L605-L607

Added lines #L605 - L607 were not covered by tests

for i, entry := range entries {
bz, err := entry.data.Marshal()
if err != nil {
walQuit <- err
return
}
batch.Write(entry.index, bz)

if entry.index < lastIndex+uint64(i+1) {
db.logger.Error("commit old version idempotently", "expected", lastIndex+uint64(i+1), "actual", entry.index)
continue

Check warning on line 618 in memiavl/db.go

View check run for this annotation

Codecov / codecov/patch

memiavl/db.go#L617-L618

Added lines #L617 - L618 were not covered by tests
} else {
batch.Write(entry.index, bz)
}
}

if err := db.wal.WriteBatch(&batch); err != nil {
Expand Down
Loading