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

swarm: Debug API infrastructure and HasChunks() API endpoint #18980

Merged
merged 7 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
13 changes: 13 additions & 0 deletions cmd/swarm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const (
SWARM_ENV_STORE_CAPACITY = "SWARM_STORE_CAPACITY"
SWARM_ENV_STORE_CACHE_CAPACITY = "SWARM_STORE_CACHE_CAPACITY"
SWARM_ENV_BOOTNODE_MODE = "SWARM_BOOTNODE_MODE"
SWARM_ENV_DEBUG_API = "SWARM_DEBUG_API"
SWARM_ACCESS_PASSWORD = "SWARM_ACCESS_PASSWORD"
SWARM_AUTO_DEFAULTPATH = "SWARM_AUTO_DEFAULTPATH"
GETH_ENV_DATADIR = "GETH_DATADIR"
Expand Down Expand Up @@ -262,6 +263,10 @@ func cmdLineOverride(currentConfig *bzzapi.Config, ctx *cli.Context) *bzzapi.Con
currentConfig.BootnodeMode = ctx.GlobalBool(SwarmBootnodeModeFlag.Name)
}

if ctx.GlobalIsSet(SwarmDebugAPIFlag.Name) {
currentConfig.DebugAPI = ctx.GlobalBool(SwarmDebugAPIFlag.Name)
}

return currentConfig

}
Expand Down Expand Up @@ -375,6 +380,14 @@ func envVarsOverride(currentConfig *bzzapi.Config) (config *bzzapi.Config) {
currentConfig.BootnodeMode = bootnodeMode
}

if debugAPIenable := os.Getenv(SWARM_ENV_DEBUG_API); debugAPIenable != "" {
debug, err := strconv.ParseBool(debugAPIenable)
if err != nil {
utils.Fatalf("invalid environment variable %s: %v", SWARM_ENV_DEBUG_API, err)
}
currentConfig.DebugAPI = debug
}

return currentConfig
}

Expand Down
4 changes: 4 additions & 0 deletions cmd/swarm/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,8 @@ var (
Name: "user",
Usage: "Indicates the user who updates the feed",
}
SwarmDebugAPIFlag = cli.BoolFlag{
holisticode marked this conversation as resolved.
Show resolved Hide resolved
Name: "debug-api",
Usage: "Make debug APIs available",
}
)
2 changes: 2 additions & 0 deletions cmd/swarm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ func init() {
SwarmStorePath,
SwarmStoreCapacity,
SwarmStoreCacheCapacity,
// provide debug API endpoints
SwarmDebugAPIFlag,
}
rpcFlags := []cli.Flag{
utils.WSEnabledFlag,
Expand Down
2 changes: 2 additions & 0 deletions swarm/api/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type Config struct {
BzzKey string
NodeID string
NetworkID uint64
DebugAPI bool
SwapEnabled bool
SyncEnabled bool
SyncingSkipCheck bool
Expand Down Expand Up @@ -90,6 +91,7 @@ func NewConfig() (c *Config) {
EnsAPIs: nil,
EnsRoot: ens.TestNetAddress,
NetworkID: network.DefaultNetworkID,
DebugAPI: false,
SwapEnabled: false,
SyncEnabled: true,
SyncingSkipCheck: false,
Expand Down
31 changes: 31 additions & 0 deletions swarm/api/testapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
package api

import (
"context"

"github.com/ethereum/go-ethereum/rpc"
"github.com/ethereum/go-ethereum/swarm/network"
"github.com/ethereum/go-ethereum/swarm/storage"
)

type Control struct {
Expand All @@ -32,3 +36,30 @@ func NewControl(api *API, hive *network.Hive) *Control {
func (c *Control) Hive() string {
return c.hive.String()
}

// DebugAPI is a umbrella structure to provide additional debug API endpoints
holisticode marked this conversation as resolved.
Show resolved Hide resolved
type DebugAPI struct {
netStore *storage.NetStore
}

func NewDebugAPI(nstore *storage.NetStore) *DebugAPI {
return &DebugAPI{
netStore: nstore,
}
}

// HasChunk returns true if the underlying datastore has
// the chunk stored with the given address, false if it does not store it
func (dapi *DebugAPI) HasChunk(chunkAddress storage.Address) bool {
return dapi.netStore.HasChunk(context.Background(), chunkAddress)
}

// The description for the DebugAPI to add to the APIs if the flag is set
func GetDebugAPIDesc(nstore *storage.NetStore) rpc.API {
return rpc.API{
Namespace: "debugapi",
Version: "1.0",
Service: NewDebugAPI(nstore),
Public: false,
}
}
9 changes: 9 additions & 0 deletions swarm/storage/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ func (m *MapChunkStore) Get(_ context.Context, ref Address) (Chunk, error) {
return chunk, nil
}

// Need to implement Has from SyncChunkStore
func (m *MapChunkStore) Has(ctx context.Context, ref Address) bool {
m.mu.RLock()
defer m.mu.RUnlock()

_, has := m.chunks[ref.Hex()]
return has
}

func (m *MapChunkStore) Close() {
}

Expand Down
14 changes: 14 additions & 0 deletions swarm/storage/ldbstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -969,6 +969,20 @@ func (s *LDBStore) Get(_ context.Context, addr Address) (chunk Chunk, err error)
return s.get(addr)
}

// Has queries the underlying DB if a chunk with the given address is stored
// Returns true if the chunk is found, false if not
func (s *LDBStore) Has(_ context.Context, addr Address) bool {
s.lock.RLock()
defer s.lock.RUnlock()

ikey := getIndexKey(addr)
_, err := s.db.Get(ikey)
if err != nil {
return false
}
return true
holisticode marked this conversation as resolved.
Show resolved Hide resolved
}

// TODO: To conform with other private methods of this object indices should not be updated
func (s *LDBStore) get(addr Address) (chunk *chunk, err error) {
if s.closed {
Expand Down
7 changes: 7 additions & 0 deletions swarm/storage/localstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,13 @@ func (ls *LocalStore) Put(ctx context.Context, chunk Chunk) error {
return err
}

// Has queries the underlying DbStore if a chunk with the given address
// is being stored there.
// Returns true if it is stored, false if not
func (ls *LocalStore) Has(ctx context.Context, addr Address) bool {
return ls.DbStore.Has(ctx, addr)
}

// Get(chunk *Chunk) looks up a chunk in the local stores
// This method is blocking until the chunk is retrieved
// so additional timeout may be needed to wrap this call if
Expand Down
33 changes: 33 additions & 0 deletions swarm/storage/localstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,3 +209,36 @@ func setupLocalStore(t *testing.T, ldbCap int) (ls *LocalStore, cleanup func())

return store, cleanup
}

func TestHas(t *testing.T) {
ldbCap := defaultGCRatio
store, cleanup := setupLocalStore(t, ldbCap)
defer cleanup()

nonStoredAddr := GenerateRandomChunk(128).Address()

has := store.Has(context.Background(), nonStoredAddr)
if has {
t.Fatal("Expected Has() to return false, but returned true!")
}

storeChunks := GenerateRandomChunks(128, 3)
for _, ch := range storeChunks {
err := store.Put(context.Background(), ch)
if err != nil {
t.Fatalf("Expected store to store chunk, but it failed: %v", err)
}

has := store.Has(context.Background(), ch.Address())
if !has {
t.Fatal("Expected Has() to return true, but returned false!")
}
}

//let's be paranoic and test again that the non-existent chunk returns false
has = store.Has(context.Background(), nonStoredAddr)
if has {
t.Fatal("Expected Has() to return false, but returned true!")
}

}
5 changes: 5 additions & 0 deletions swarm/storage/memstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ func NewMemStore(params *StoreParams, _ *LDBStore) (m *MemStore) {
}
}

// Has needed to implement SyncChunkStore
func (m *MemStore) Has(_ context.Context, addr Address) bool {
return m.cache.Contains(addr)
}

func (m *MemStore) Get(_ context.Context, addr Address) (Chunk, error) {
if m.disabled {
return nil, ErrChunkNotFound
Expand Down
7 changes: 7 additions & 0 deletions swarm/storage/netstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,13 @@ func (n *NetStore) get(ctx context.Context, ref Address) (Chunk, func(context.Co
return chunk, nil, nil
}

// Has is the storage layer entry point to query the underlying
// database to return if it has a chunk or not.
// Called from the DebugAPI
func (n *NetStore) Has(ctx context.Context, ref Address) bool {
return n.store.Has(ctx, ref)
}

// getOrCreateFetcher attempts at retrieving an existing fetchers
// if none exists, creates one and saves it in the fetchers cache
// caller must hold the lock
Expand Down
8 changes: 7 additions & 1 deletion swarm/storage/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ func (v *ContentAddressValidator) Validate(chunk Chunk) bool {
type ChunkStore interface {
Put(ctx context.Context, ch Chunk) (err error)
Get(rctx context.Context, ref Address) (ch Chunk, err error)
Has(rctx context.Context, ref Address) bool
Close()
}

Expand All @@ -314,7 +315,12 @@ func (f *FakeChunkStore) Put(_ context.Context, ch Chunk) error {
return nil
}

// Gut doesn't store anything it is just here to implement ChunkStore
// Has doesn't do anything it is just here to implement ChunkStore
func (f *FakeChunkStore) Has(_ context.Context, ref Address) bool {
panic("FakeChunkStore doesn't support HasChunk")
}

// Get doesn't store anything it is just here to implement ChunkStore
func (f *FakeChunkStore) Get(_ context.Context, ref Address) (Chunk, error) {
panic("FakeChunkStore doesn't support Get")
}
Expand Down
6 changes: 6 additions & 0 deletions swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,12 @@ func (self *Swarm) APIs() []rpc.API {
apis = append(apis, self.ps.APIs()...)
}

// Only provide certain endpoints if the `debug-api` flag is set
if self.config.DebugAPI {
log.Info("Running node with debug APIs attached")
apis = append(apis, api.GetDebugAPIDesc(self.netStore))
}

return apis
}

Expand Down