Skip to content

Commit

Permalink
support repair snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
giskook committed Jun 7, 2023
1 parent c836620 commit 1dcd331
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 20 deletions.
3 changes: 2 additions & 1 deletion app/repair_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ func repairStateOnStart(ctx *server.Context) {
func RepairState(ctx *server.Context, onStart bool) {
sm.SetIgnoreSmbCheck(true)
iavl.SetIgnoreVersionCheck(true)
rootmulti.SetRepair()
global.SetRepairState(true)
defer global.SetRepairState(false)

// load latest block height
dataDir := filepath.Join(ctx.Config.RootDir, "data")
Expand Down
4 changes: 0 additions & 4 deletions cmd/okbchaind/repair_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"fmt"
"github.com/okx/okbchain/libs/cosmos-sdk/store/mpt"
"log"
"net/http"
_ "net/http/pprof"
Expand Down Expand Up @@ -64,7 +63,4 @@ func setExternalPackageValue() {
tmiavl.SetEnableFastStorage(true)
tmiavl.SetIgnoreAutoUpgrade(true)
}
if !viper.GetBool(tmiavl.FlagIavlDiscardFastStorage) {
mpt.SetSnapshotRebuild(true)
}
}
15 changes: 13 additions & 2 deletions libs/cosmos-sdk/store/mpt/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/core/state/snapshot"
mpttypes "github.com/okx/okbchain/libs/cosmos-sdk/store/mpt/types"
"github.com/okx/okbchain/libs/tendermint/global"
)

var (
Expand All @@ -15,8 +16,10 @@ var (

const (
// snapshotMemoryLayerCount snapshot memory layer count
// as we dont rollback transactions so we only keep 1 memory layer
snapshotMemoryLayerCount = 1
// snapshotMemoryLayerCount controls the snapshot Journal height,
// if repair start-height is lower than snapshot Journal height,
// snapshot will not be repaired anymore
snapshotMemoryLayerCount = 10
)

func DisableSnapshot() {
Expand All @@ -42,6 +45,9 @@ func (ms *MptStore) openSnapshot() error {
ms.logger.Error("Enabling snapshot recovery", "chainhead", version, "diskbase", *layer)
recovery = true
}
if global.GetRepairState() {
recovery = true
}
var err error
ms.snaps, err = snapshot.NewCustom(ms.db.TrieDB().DiskDB(), ms.db.TrieDB(), 256, ms.originalRoot, false, gSnapshotRebuild, recovery, ms.retriever)
if err != nil {
Expand Down Expand Up @@ -74,6 +80,8 @@ func (ms *MptStore) prepareSnap(root common.Hash) {
ms.snapDestructs = make(map[common.Hash]struct{})
ms.snapAccounts = make(map[common.Hash][]byte)
ms.snapStorage = make(map[common.Hash]map[common.Hash][]byte)
} else {
ms.logger.Error("prepare snapshot error", "root", root)
}
}

Expand All @@ -94,6 +102,9 @@ func (ms *MptStore) commitSnap(root common.Hash) {
if err := ms.snaps.Cap(root, snapshotMemoryLayerCount); err != nil {
ms.logger.Error("Failed to cap snapshot tree", "root", root, "layers", snapshotMemoryLayerCount, "err", err)
}
if _, err := ms.snaps.Journal(root); err != nil {
ms.logger.Error("Failed to journal snapshot tree", "root", root, "err", err)
}
}
ms.snap, ms.snapDestructs, ms.snapAccounts, ms.snapStorage = nil, nil, nil, nil

Expand Down
15 changes: 2 additions & 13 deletions libs/cosmos-sdk/store/rootmulti/rootmulti_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package rootmulti
import (
"encoding/binary"
"fmt"
"github.com/okx/okbchain/libs/tendermint/global"
"io"
"log"
"path/filepath"
Expand Down Expand Up @@ -47,18 +48,6 @@ const (
maxPruneHeightsLength = 100
)

var (
repairing bool
)

func SetRepair() {
repairing = true
}

func getRepair() bool {
return repairing
}

// Store is composed of many CommitStores. Name contrasts with
// cacheMultiStore which is for cache-wrapping other MultiStores. It implements
// the CommitMultiStore interface.
Expand Down Expand Up @@ -408,7 +397,7 @@ func (rs *Store) loadVersion(ver int64, upgrades *types.StoreUpgrades) error {
// we can not get the upgrade version before the expect height,
// and we should not use the original 0 too, because 0 means the latest height,
// so when we repair data before the milestone. we open a empty tree by cur version.
if getRepair() && version == 0 {
if global.GetRepairState() && version == 0 {
param.upgradeVersion = uint64(ver)
}
rs.storesParams[key] = param
Expand Down
11 changes: 11 additions & 0 deletions libs/tendermint/global/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package global

var repairState bool

func SetRepairState(state bool) {
repairState = state
}

func GetRepairState() bool {
return repairState
}

0 comments on commit 1dcd331

Please sign in to comment.