github.com/klaytn/klaytn@v1.12.1/api/api_private_debug.go (about)

     1  // Modifications Copyright 2019 The klaytn Authors
     2  // Copyright 2015 The go-ethereum Authors
     3  // This file is part of the go-ethereum library.
     4  //
     5  // The go-ethereum library is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Lesser General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // The go-ethereum library is distributed in the hope that it will be useful,
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  // GNU Lesser General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Lesser General Public License
    16  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    17  //
    18  // This file is derived from internal/ethapi/api.go (2018/06/04).
    19  // Modified and improved for the klaytn development.
    20  
    21  package api
    22  
    23  import (
    24  	"context"
    25  	"errors"
    26  	"fmt"
    27  	"time"
    28  
    29  	"github.com/davecgh/go-spew/spew"
    30  	"github.com/klaytn/klaytn/common"
    31  	"github.com/klaytn/klaytn/networks/rpc"
    32  )
    33  
    34  // PrivateDebugAPI is the collection of Klaytn APIs exposed over the private
    35  // debugging endpoint.
    36  type PrivateDebugAPI struct {
    37  	b Backend
    38  }
    39  
    40  // NewPrivateDebugAPI creates a new API definition for the private debug methods
    41  // of the Klaytn service.
    42  func NewPrivateDebugAPI(b Backend) *PrivateDebugAPI {
    43  	return &PrivateDebugAPI{b: b}
    44  }
    45  
    46  // ChaindbProperty returns leveldb properties of the chain database.
    47  func (api *PrivateDebugAPI) ChaindbProperty(property string) (string, error) {
    48  	return api.b.ChainDB().Stat(property)
    49  }
    50  
    51  // ChaindbCompact flattens the entire key-value database into a single level,
    52  // removing all unused slots and merging all keys.
    53  func (api *PrivateDebugAPI) ChaindbCompact() error {
    54  	for b := 0; b <= 255; b++ {
    55  		var (
    56  			start = []byte{byte(b)}
    57  			end   = []byte{byte(b + 1)}
    58  		)
    59  		if b == 255 {
    60  			end = nil
    61  		}
    62  		logger.Info("Compacting database started", "range", fmt.Sprintf("%#X-%#X", start, end))
    63  		cstart := time.Now()
    64  		if err := api.b.ChainDB().Compact(start, end); err != nil {
    65  			logger.Error("Database compaction failed", "err", err)
    66  			return err
    67  		}
    68  		logger.Info("Compacting database completed", "range", fmt.Sprintf("%#X-%#X", start, end), "elapsed", common.PrettyDuration(time.Since(cstart)))
    69  	}
    70  	return nil
    71  }
    72  
    73  // SetHead rewinds the head of the blockchain to a previous block.
    74  func (api *PrivateDebugAPI) SetHead(number rpc.BlockNumber) error {
    75  	if number == rpc.PendingBlockNumber ||
    76  		number == rpc.LatestBlockNumber ||
    77  		number.Uint64() > api.b.CurrentBlock().NumberU64() {
    78  		return errors.New("Cannot rewind to future")
    79  	}
    80  	return api.b.SetHead(uint64(number))
    81  }
    82  
    83  // PrintBlock retrieves a block and returns its pretty printed form.
    84  func (api *PrivateDebugAPI) PrintBlock(ctx context.Context, blockNrOrHash rpc.BlockNumberOrHash) (string, error) {
    85  	block, _ := api.b.BlockByNumberOrHash(ctx, blockNrOrHash)
    86  	if block == nil {
    87  		blockNumberOrHashString, _ := blockNrOrHash.NumberOrHashString()
    88  		return "", fmt.Errorf("block %v not found", blockNumberOrHashString)
    89  	}
    90  	return spew.Sdump(block), nil
    91  }