Skip to content

Commit

Permalink
Merge branch 'master' into BTFS-1054PersistSession
Browse files Browse the repository at this point in the history
  • Loading branch information
ShellyWEI authored Dec 10, 2019
2 parents 75dcdbf + 4da6306 commit 821bdde
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
1 change: 1 addition & 0 deletions cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
// Spin jobs in the background
spin.Analytics(node, version.CurrentVersionNumber, hValue)
spin.Hosts(node)
spin.Settings(node)

// Give the user some immediate feedback when they hit C-c
go func() {
Expand Down
66 changes: 40 additions & 26 deletions core/commands/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
hubpb "github.com/tron-us/go-btfs-common/protos/hub"
ledgerpb "github.com/tron-us/go-btfs-common/protos/ledger"
guardPb "github.com/tron-us/go-btfs-common/protos/guard"
"github.com/tron-us/go-btfs-common/utils/grpc"

"github.com/gogo/protobuf/proto"
cidlib "github.com/ipfs/go-cid"
Expand Down Expand Up @@ -1121,56 +1122,69 @@ By default it shows local host node information.`,
if !cfg.Experimental.StorageClientEnabled {
return fmt.Errorf("storage client api not enabled")
}
// TODO: Implement syncing other peers' storage info
return fmt.Errorf("showing other peer's info not supported yet")
} else if !cfg.Experimental.StorageHostEnabled {
return fmt.Errorf("storage host api not enabled")
}

var peerID string

n, err := cmdenv.GetNode(env)
if err != nil {
return err
}

// Default to self
var peerID string
if len(req.Arguments) > 0 {
peerID = req.Arguments[0]
} else {
peerID = n.Identity.Pretty()
}

rds := n.Repo.Datastore()

b, err := rds.Get(storage.GetHostStorageKey(peerID))
data, err := GetSettings(req.Context, cfg.Services.HubDomain, peerID, n.Repo.Datastore())
if err != nil {
return err
}
return cmds.EmitOnce(res, data)
},
Type: hubpb.SettingsData{},
}

var ns info.NodeStorage
err = json.Unmarshal(b, &ns)
func GetSettings(ctx context.Context, addr string, peerId string, rds ds.Datastore) (*hubpb.SettingsData, error) {
// get from LevelDB
b, err := rds.Get(storage.GetHostStorageKey(peerId))
if err == nil {
data := new(hubpb.SettingsData)
err = proto.Unmarshal(b, data)
if err != nil {
return err
return nil, err
}
return data, nil
}

return cmds.EmitOnce(res, &StorageHostInfoRes{
StoragePrice: ns.StoragePriceAsk,
BandwidthPrice: ns.BandwidthPriceAsk,
CollateralPrice: ns.CollateralStake,
BandwidthLimit: ns.BandwidthLimit,
StorageTimeMin: ns.StorageTimeMin,
})
},
Type: StorageHostInfoRes{},
}
// get from remote
var (
resp *hubpb.SettingsResp
)
err = grpc.HubQueryClient(addr).WithContext(ctx, func(ctx context.Context, client hubpb.HubQueryServiceClient) error {
req := new(hubpb.SettingsReq)
req.Id = peerId
resp, err = client.GetSettings(ctx, req)
return err
})
if err != nil {
return nil, err
}

// save to rds
bytes, err := proto.Marshal(resp.SettingsData)
if err != nil {
return nil, err
}
err = rds.Put(storage.GetHostStorageKey(peerId), bytes)
if err != nil {
return nil, err
}

type StorageHostInfoRes struct {
StoragePrice uint64
BandwidthPrice uint64
CollateralPrice uint64
BandwidthLimit float64
StorageTimeMin uint64
return resp.SettingsData, nil
}

var storageAnnounceCmd = &cmds.Command{
Expand Down
26 changes: 26 additions & 0 deletions spin/settings.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package spin

import (
"github.com/TRON-US/go-btfs/core"
"github.com/TRON-US/go-btfs/core/commands"
)

func Settings(node *core.IpfsNode) {
c, err := node.Repo.Config()
if err != nil {
log.Errorf("Failed to get configuration %s", err)
return
}

if c.Experimental.StorageHostEnabled {
go func() {
_, err := commands.GetSettings(node.Context(), c.Services.HubDomain, node.Identity.Pretty(),
node.Repo.Datastore())
if err != nil {
log.Error("error occured when getting settings, error: ", err.Error())
}
}()
}

//TODO: spin
}

0 comments on commit 821bdde

Please sign in to comment.