From 6d8ecdbbf27f2999296e481c289b645dac918e1a Mon Sep 17 00:00:00 2001 From: Fabio Berger Date: Thu, 11 Apr 2019 22:32:40 -0500 Subject: [PATCH] ethapi: Move PrivateDebugAPI into a separate file excluded from WASM compilation to enable importing signer/core in WASM projects --- internal/ethapi/api.go | 46 -------------------- internal/ethapi/private_debug_api.go | 63 ++++++++++++++++++++++++++++ 2 files changed, 63 insertions(+), 46 deletions(-) create mode 100644 internal/ethapi/private_debug_api.go diff --git a/internal/ethapi/api.go b/internal/ethapi/api.go index 473026606f..3d3a45e4dd 100644 --- a/internal/ethapi/api.go +++ b/internal/ethapi/api.go @@ -22,7 +22,6 @@ import ( "errors" "fmt" "math/big" - "strings" "time" "github.com/davecgh/go-spew/spew" @@ -44,7 +43,6 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" - "github.com/syndtr/goleveldb/leveldb" "github.com/tyler-smith/go-bip39" ) @@ -1585,50 +1583,6 @@ func (api *PublicDebugAPI) SeedHash(ctx context.Context, number uint64) (string, return fmt.Sprintf("0x%x", ethash.SeedHash(number)), nil } -// PrivateDebugAPI is the collection of Ethereum APIs exposed over the private -// debugging endpoint. -type PrivateDebugAPI struct { - b Backend -} - -// NewPrivateDebugAPI creates a new API definition for the private debug methods -// of the Ethereum service. -func NewPrivateDebugAPI(b Backend) *PrivateDebugAPI { - return &PrivateDebugAPI{b: b} -} - -// ChaindbProperty returns leveldb properties of the chain database. -func (api *PrivateDebugAPI) ChaindbProperty(property string) (string, error) { - ldb, ok := api.b.ChainDb().(interface { - LDB() *leveldb.DB - }) - if !ok { - return "", fmt.Errorf("chaindbProperty does not work for memory databases") - } - if property == "" { - property = "leveldb.stats" - } else if !strings.HasPrefix(property, "leveldb.") { - property = "leveldb." + property - } - return ldb.LDB().GetProperty(property) -} - -func (api *PrivateDebugAPI) ChaindbCompact() error { - for b := byte(0); b < 255; b++ { - log.Info("Compacting chain database", "range", fmt.Sprintf("0x%0.2X-0x%0.2X", b, b+1)) - if err := api.b.ChainDb().Compact([]byte{b}, []byte{b + 1}); err != nil { - log.Error("Database compaction failed", "err", err) - return err - } - } - return nil -} - -// SetHead rewinds the head of the blockchain to a previous block. -func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) { - api.b.SetHead(uint64(number)) -} - // PublicNetAPI offers network related RPC methods type PublicNetAPI struct { net *p2p.Server diff --git a/internal/ethapi/private_debug_api.go b/internal/ethapi/private_debug_api.go new file mode 100644 index 0000000000..f65b04e127 --- /dev/null +++ b/internal/ethapi/private_debug_api.go @@ -0,0 +1,63 @@ +// +build !js +package ethapi + +import ( + "fmt" + "strings" + + "github.com/ethereum/go-ethereum/common/hexutil" + "github.com/ethereum/go-ethereum/log" + "github.com/syndtr/goleveldb/leveldb" + "github.com/syndtr/goleveldb/leveldb/util" +) + +// PrivateDebugAPI is the collection of Ethereum APIs exposed over the private +// debugging endpoint. +type PrivateDebugAPI struct { + b Backend +} + +// NewPrivateDebugAPI creates a new API definition for the private debug methods +// of the Ethereum service. +func NewPrivateDebugAPI(b Backend) *PrivateDebugAPI { + return &PrivateDebugAPI{b: b} +} + +// ChaindbProperty returns leveldb properties of the chain database. +func (api *PrivateDebugAPI) ChaindbProperty(property string) (string, error) { + ldb, ok := api.b.ChainDb().(interface { + LDB() *leveldb.DB + }) + if !ok { + return "", fmt.Errorf("chaindbProperty does not work for memory databases") + } + if property == "" { + property = "leveldb.stats" + } else if !strings.HasPrefix(property, "leveldb.") { + property = "leveldb." + property + } + return ldb.LDB().GetProperty(property) +} + +func (api *PrivateDebugAPI) ChaindbCompact() error { + ldb, ok := api.b.ChainDb().(interface { + LDB() *leveldb.DB + }) + if !ok { + return fmt.Errorf("chaindbCompact does not work for memory databases") + } + for b := byte(0); b < 255; b++ { + log.Info("Compacting chain database", "range", fmt.Sprintf("0x%0.2X-0x%0.2X", b, b+1)) + err := ldb.LDB().CompactRange(util.Range{Start: []byte{b}, Limit: []byte{b + 1}}) + if err != nil { + log.Error("Database compaction failed", "err", err) + return err + } + } + return nil +} + +// SetHead rewinds the head of the blockchain to a previous block. +func (api *PrivateDebugAPI) SetHead(number hexutil.Uint64) { + api.b.SetHead(uint64(number)) +} \ No newline at end of file