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

tool to read tip of state db #3659

Merged
merged 2 commits into from
Oct 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ BUILD_TARGET_XCTL=xctl
BUILD_TARGET_NEWXCTL=newxctl
BUILD_TARGET_MINICLUSTER=minicluster
BUILD_TARGET_RECOVER=recover
BUILD_TARGET_READTIP=readtip
BUILD_TARGET_IOMIGRATER=iomigrater
BUILD_TARGET_OS=$(shell go env GOOS)
BUILD_TARGET_ARCH=$(shell go env GOARCH)
Expand Down Expand Up @@ -78,7 +79,7 @@ build: ioctl
$(GOBUILD) -ldflags "$(PackageFlags)" -o ./bin/$(BUILD_TARGET_SERVER) -v ./$(BUILD_TARGET_SERVER)

.PHONY: build-all
build-all: build build-actioninjector build-addrgen build-minicluster build-staterecoverer
build-all: build build-actioninjector build-addrgen build-minicluster build-staterecoverer build-readtip

.PHONY: build-actioninjector
build-actioninjector:
Expand All @@ -96,6 +97,10 @@ build-minicluster:
build-staterecoverer:
$(GOBUILD) -o ./bin/$(BUILD_TARGET_RECOVER) -v ./tools/staterecoverer

.PHONY: build-readtip
build-readtip:
$(GOBUILD) -o ./bin/$(BUILD_TARGET_READTIP) -v ./tools/readtip

.PHONY: fmt
fmt:
$(GOCMD) fmt ./...
Expand Down
2 changes: 2 additions & 0 deletions db/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ type Config struct {
SplitDBHeight uint64 `yaml:"splitDBHeight"`
// HistoryStateRetention is the number of blocks account/contract state will be retained
HistoryStateRetention uint64 `yaml:"historyStateRetention"`
// ReadOnly is set db to be opened in read only mode
ReadOnly bool `yaml:"readOnly"`
}

// SplitDBSize returns the configured SplitDBSizeMB
Expand Down
6 changes: 5 additions & 1 deletion db/db_bolt.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,11 @@ func NewBoltDB(cfg Config) *BoltDB {

// Start opens the BoltDB (creates new file if not existing yet)
func (b *BoltDB) Start(_ context.Context) error {
db, err := bolt.Open(b.path, _fileMode, nil)
opts := *bolt.DefaultOptions
if b.config.ReadOnly {
opts.ReadOnly = true
}
db, err := bolt.Open(b.path, _fileMode, &opts)
if err != nil {
return errors.Wrap(ErrIO, err.Error())
}
Expand Down
58 changes: 58 additions & 0 deletions tools/readtip/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright (c) 2019 IoTeX Foundation
// This is an alpha (internal) release and is not suitable for production. This source code is provided 'as is' and no
// warranties are given as to title or non-infringement, merchantability or fitness for purpose and, to the extent
// permitted by law, all liability for your use of the code is disclaimed. This source code is governed by Apache
// License 2.0 that can be found in the LICENSE file.

// This is a recovery tool that recovers a corrupted or missing state database.
// To use, run "make recover"
package main

import (
"flag"
"fmt"
"os"

"go.uber.org/zap"

"github.com/iotexproject/iotex-core/config"
"github.com/iotexproject/iotex-core/db"
"github.com/iotexproject/iotex-core/pkg/log"
"github.com/iotexproject/iotex-core/pkg/util/byteutil"
"github.com/iotexproject/iotex-core/state/factory"
)

var (
// overwritePath is the path to the config file which overwrite default values
_overwritePath string
// secretPath is the path to the config file store secret values
_secretPath string
)

func init() {
flag.StringVar(&_overwritePath, "config-path", "", "Config path")
flag.StringVar(&_secretPath, "secret-path", "", "Secret path")
flag.Usage = func() {
_, _ = fmt.Fprintf(os.Stderr, "usage: readtip -config-path=[string]\n")
flag.PrintDefaults()
os.Exit(2)
}
flag.Parse()
}

func main() {
cfg, err := config.New([]string{_overwritePath, _secretPath}, []string{})
if err != nil {
log.S().Panic("failed to new config.", zap.Error(err))
}
cfg.DB.ReadOnly = true
store, err := db.CreateKVStore(cfg.DB, cfg.Chain.TrieDBPath)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add new func db.CreateKVStoreWithOption, and use readonly option open the DB

if err != nil {
log.S().Panic("failed to load state db", zap.Error(err))
}
h, err := store.Get(factory.AccountKVNamespace, []byte(factory.CurrentHeightKey))
if err != nil {
log.S().Panic("failed to read state db", zap.Error(err))
}
fmt.Println(byteutil.BytesToUint64(h))
}