Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
klim0v committed Dec 16, 2021
1 parent 553f40f commit 4205271
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 36 deletions.
2 changes: 1 addition & 1 deletion cmd/minter/cmd/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func runNode(cmd *cobra.Command) error {
}
app := minter.NewMinterBlockchain(storages, cfg, cmd.Context(), 720, 0, logger.With("module", "node"))

if cfg.SnapshotInterval > 0 {
if cfg.SnapshotInterval > 0 || cfg.StateSync.Enable {
snapshotDB, err := storages.InitSnapshotLevelDB("data/snapshots/metadata", minter.GetDbOpts(cfg.StateMemAvailable))
if err != nil {
return err
Expand Down
1 change: 1 addition & 0 deletions config/toml.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ fast_sync = {{ .BaseConfig.FastSync }}
# State sync snapshot interval
snapshot_interval = {{ .BaseConfig.SnapshotInterval }}
# State sync snapshot to keep
snapshot_keep_recent = {{ .BaseConfig.SnapshotKeepRecent }}
Expand Down
39 changes: 21 additions & 18 deletions coreV2/appdb/appdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ import (
"github.com/MinterTeam/minter-go-node/tree"
"github.com/cosmos/cosmos-sdk/snapshots"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
"log"
"sync"

//"github.com/cosmos/cosmos-sdk/store/iavl"
"github.com/cosmos/iavl"

"github.com/MinterTeam/minter-go-node/coreV2/appdb/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
iavltree "github.com/cosmos/iavl"
Expand Down Expand Up @@ -44,7 +42,8 @@ type AppDB struct {
//mu sync.RWMutex
WG sync.WaitGroup

store tree.MTree
store tree.MTree
stateDB db.DB

startHeight uint64
lastHeight uint64
Expand Down Expand Up @@ -393,6 +392,9 @@ func (appDB *AppDB) SaveVersions() {
func (appDB *AppDB) SetState(state tree.MTree) {
appDB.store = state
}
func (appDB *AppDB) SetStateDB(stateDB db.DB) {
appDB.stateDB = stateDB
}

// NewAppDB creates AppDB instance with given config
func NewAppDB(homeDir string, cfg *config.Config) *AppDB {
Expand All @@ -414,10 +416,10 @@ const (
snapshotMaxItemSize = int(64e6) // SDK has no key/value size limit, so we set an arbitrary limit
)

type Store interface {
Export(version int64) (*iavl.Exporter, error)
Import(version int64) (*iavl.Importer, error)
}
//type Store interface {
//Export(version int64) (*iavl.Exporter, error)
//Import(version int64) (*iavl.Importer, error)
//}

// Snapshot implements snapshottypes.Snapshotter. The snapshot output for a given format must be
// identical across nodes such that chunks from different sources fit together. If the output for a
Expand Down Expand Up @@ -606,17 +608,24 @@ func (appDB *AppDB) Restore(
}
importer.Close()
}
store, ok := appDB.store.(Store)
if !ok || store == nil {
return sdkerrors.Wrapf(sdkerrors.ErrLogic, "cannot import into non-IAVL store %q", item.Store.Name)
if appDB.store == nil {
startHeight := appDB.GetStartHeight()
log.Println(startHeight)
lastHeight := appDB.GetLastHeight()
log.Println(lastHeight)
appDB.store, err = tree.NewMutableTree(0, appDB.stateDB, 1000000, startHeight)
if err != nil {
return sdkerrors.Wrap(err, "create state failed")
}
}
importer, err = store.Import(int64(height))
importer, err = appDB.store.Import(int64(height))
if err != nil {
return sdkerrors.Wrap(err, "import failed")
}
defer importer.Close()

case validatorsPath, heightPath, hashPath, versionsPath, blocksTimePath, startHeightPath:
log.Println(item.Store.Name)
if err := appDB.db.Set([]byte(item.Store.Name), item.Store.Value); err != nil {
panic(err)
}
Expand Down Expand Up @@ -664,11 +673,5 @@ func (appDB *AppDB) Restore(
importer.Close()
}

//appDB.FlushValidators()
//appDB.SaveBlocksTime()
//appDB.SaveVersions()
//appDB.SaveBlocksTime()
// appDB.LoadLatestVersion()

return nil
}
3 changes: 2 additions & 1 deletion coreV2/minter/blockchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func (blockchain *Blockchain) GetCurrentRewards() *big.Int {
func NewMinterBlockchain(storages *utils.Storage, cfg *config.Config, ctx context.Context, updateStakePeriod uint64, expiredOrdersPeriod uint64, logger tmlog.Logger) *Blockchain {
// Initiate Application DB. Used for persisting data like current block, validators, etc.
applicationDB := appdb.NewAppDB(storages.GetMinterHome(), cfg)
applicationDB.SetStateDB(storages.StateDB())
if ctx == nil {
ctx = context.Background()
}
Expand Down Expand Up @@ -189,11 +190,11 @@ func (blockchain *Blockchain) initState() {
if err != nil {
panic(err)
}
blockchain.appDB.SetState(stateDeliver.Tree())

atomic.StoreUint64(&blockchain.height, currentHeight)
blockchain.stateDeliver = stateDeliver
blockchain.stateCheck = state.NewCheckState(stateDeliver)
blockchain.appDB.SetState(stateDeliver.Tree())

grace := upgrades.NewGrace()
grace.AddGracePeriods(upgrades.NewGracePeriod(initialHeight, initialHeight+120, true),
Expand Down
35 changes: 20 additions & 15 deletions coreV2/minter/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@ import (
"errors"
snapshottypes "github.com/cosmos/cosmos-sdk/snapshots/types"
abci "github.com/tendermint/tendermint/abci/types"
tmlog "github.com/tendermint/tendermint/libs/log"
"os"
)

var logger = tmlog.NewTMLogger(os.Stdout)

// List available snapshots
func (blockchain *Blockchain) ListSnapshots(req abci.RequestListSnapshots) abci.ResponseListSnapshots {
blockchain.logger.Debug("ListSnapshots")
resp := abci.ResponseListSnapshots{Snapshots: []*abci.Snapshot{}}
if blockchain.snapshotManager == nil {
return resp
Expand All @@ -34,14 +32,16 @@ func (blockchain *Blockchain) ListSnapshots(req abci.RequestListSnapshots) abci.
return resp
}

// Offer a snapshot to the application
func (blockchain *Blockchain) OfferSnapshot(req abci.RequestOfferSnapshot) abci.ResponseOfferSnapshot {
blockchain.logger.Info("Processing OfferSnapshot...", "Snapshot", req.Snapshot.String())
if blockchain.snapshotManager == nil {
//blockchain.logger.Error("snapshot manager not configured")
return abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_ABORT}
}

if req.Snapshot == nil {
//blockchain.logger.Error("received nil snapshot")
blockchain.logger.Error("received nil snapshot")
return abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_REJECT}
}

Expand All @@ -54,7 +54,7 @@ func (blockchain *Blockchain) OfferSnapshot(req abci.RequestOfferSnapshot) abci.
err = blockchain.snapshotManager.Restore(snapshot)
switch {
case err == nil:
blockchain.initState()
blockchain.logger.Info("Done OfferSnapshot!", "Snapshot", req.Snapshot.String())
return abci.ResponseOfferSnapshot{Result: abci.ResponseOfferSnapshot_ACCEPT}

case errors.Is(err, snapshottypes.ErrUnknownFormat):
Expand Down Expand Up @@ -83,34 +83,39 @@ func (blockchain *Blockchain) OfferSnapshot(req abci.RequestOfferSnapshot) abci.
}
}

// Load a snapshot chunk
func (blockchain *Blockchain) LoadSnapshotChunk(req abci.RequestLoadSnapshotChunk) abci.ResponseLoadSnapshotChunk {
blockchain.logger.Info("Processing LoadSnapshotChunk...", "req", req.String())
if blockchain.snapshotManager == nil {
return abci.ResponseLoadSnapshotChunk{}
}
chunk, err := blockchain.snapshotManager.LoadChunk(req.Height, req.Format, req.Chunk)
if err != nil {
//blockchain.logger.Error(
// "failed to load snapshot chunk",
// "height", req.Height,
// "format", req.Format,
// "chunk", req.Chunk,
// "err", err,
//)
blockchain.logger.Error(
"failed to load snapshot chunk",
"height", req.Height,
"format", req.Format,
"chunk", req.Chunk,
"err", err,
)
return abci.ResponseLoadSnapshotChunk{}
}
blockchain.logger.Debug("Done LoadSnapshotChunk!", "req", req.String())
return abci.ResponseLoadSnapshotChunk{Chunk: chunk}
}

// Apply a shapshot chunk
func (blockchain *Blockchain) ApplySnapshotChunk(req abci.RequestApplySnapshotChunk) abci.ResponseApplySnapshotChunk {
blockchain.logger.Info("Processing ApplySnapshotChunk...", "Index", req.Index, "Sender", req.Sender)
if blockchain.snapshotManager == nil {
//blockchain.logger.Error("snapshot manager not configured")
blockchain.logger.Error("snapshot manager not configured")
return abci.ResponseApplySnapshotChunk{Result: abci.ResponseApplySnapshotChunk_ABORT}
}

_, err := blockchain.snapshotManager.RestoreChunk(req.Chunk)
switch {
case err == nil:
blockchain.initState()
blockchain.logger.Info("Done ApplySnapshotChunk!", "Index", req.Index, "Sender", req.Sender)
return abci.ResponseApplySnapshotChunk{Result: abci.ResponseApplySnapshotChunk_ACCEPT}

case errors.Is(err, snapshottypes.ErrChunkHashMismatch):
Expand Down
2 changes: 1 addition & 1 deletion tree/tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (t *mutableTree) MutableTree() *iavl.MutableTree {
// NewMutableTree creates and returns new MutableTree using given db. Panics on error.
// If you want to get read-only state, you should use height = 0 and LazyLoadVersion (version), see NewImmutableTree
func NewMutableTree(height uint64, db dbm.DB, cacheSize int, initialVersion uint64) (MTree, error) {
tree, err := iavl.NewMutableTreeWithOpts(db, cacheSize, &iavl.Options{InitialVersion: initialVersion})
tree, err := iavl.NewMutableTree(db, cacheSize)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 4205271

Please sign in to comment.