Skip to content

Commit

Permalink
Btfs 4824 hub v5 (bittorrent#229)
Browse files Browse the repository at this point in the history
* feat: add score level when get stats from hub

* feat: visit hub score

* feat: visit hub score

* add ./btfs statuscontract report_online_server

* chore:add cmd of reprot status

* feat: add cmd /statuscontract/report_status_contract

* chore: mod cmdss for statuscontract

* chore: mod cmdenvv for statuscontract

* chore: mod cmdss for statuscontract

* chore: test

Co-authored-by: fish <[email protected]>
  • Loading branch information
2 people authored and daniel-tron committed Sep 1, 2022
1 parent f7b9cb9 commit 36ff32a
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 66 deletions.
44 changes: 42 additions & 2 deletions cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -472,9 +472,10 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
}

// init report status contract
err = reportstatus.Init(chainInfo.TransactionService, cfg, configRoot, chainCfg.StatusAddress, chainInfo.ChainID)
reportStatusServ := reportstatus.Init(chainInfo.TransactionService, cfg, chainCfg.StatusAddress)
err = CheckExistLastOnlineReport(cfg, configRoot, chainid, reportStatusServ)
if err != nil {
fmt.Println("init report status, err: ", err)
fmt.Println("check report status, err: ", err)
return err
}

Expand Down Expand Up @@ -1307,3 +1308,42 @@ func doIfNeedUpgradeFactoryToV2(chainid int64, chainCfg *chainconfig.ChainConfig
fmt.Println("will re-deploy a vault contract for you")
return
}

// CheckExistLastOnlineReport sync conf and lastOnlineInfo
func CheckExistLastOnlineReport(cfg *config.Config, configRoot string, chainId int64, reportStatusServ reportstatus.Service) error {
lastOnline, err := chain.GetLastOnline()
if err != nil {
return err
}

// if nil, set config online status config
if lastOnline == nil {
var reportOnline bool
var reportStatusContract bool
if cfg.Experimental.StorageHostEnabled {
reportOnline = true
reportStatusContract = true
}

var onlineServerDomain string
if chainId == 199 {
onlineServerDomain = config.DefaultServicesConfig().OnlineServerDomain
} else {
onlineServerDomain = config.DefaultServicesConfigTestnet().OnlineServerDomain
}

err = commands.SyncConfigOnlineCfg(configRoot, onlineServerDomain, reportOnline, reportStatusContract)
if err != nil {
return err
}
}

// if nil, set last online info
if lastOnline == nil {
err = reportStatusServ.CheckLastOnlineInfo(cfg.Identity.PeerID, cfg.Identity.BttcAddr)
if err != nil {
return err
}
}
return nil
}
2 changes: 2 additions & 0 deletions core/commands/commands_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,8 @@ func TestCommands(t *testing.T) {
"/statuscontract/reportlist",
"/statuscontract/lastinfo",
"/statuscontract/config",
"/statuscontract/report_online_server",
"/statuscontract/report_status_contract",
}

cmdSet := make(map[string]struct{})
Expand Down
53 changes: 47 additions & 6 deletions core/commands/statuscontract.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import (
"strconv"
"time"

"github.com/bittorrent/go-btfs/core/commands/cmdenv"

cmds "github.com/bittorrent/go-btfs-cmds"
"github.com/bittorrent/go-btfs/chain"
"github.com/bittorrent/go-btfs/core/commands/cmdenv"
"github.com/bittorrent/go-btfs/reportstatus"
"github.com/bittorrent/go-btfs/spin"
)

var StatusContractCmd = &cmds.Command{
Expand All @@ -22,10 +23,12 @@ var StatusContractCmd = &cmds.Command{
report status-contract cmd, total cmd and list cmd.`,
},
Subcommands: map[string]*cmds.Command{
"total": TotalCmd,
"reportlist": ReportListCmd,
"lastinfo": LastInfoCmd,
"config": StatusConfigCmd,
"total": TotalCmd,
"reportlist": ReportListCmd,
"lastinfo": LastInfoCmd,
"config": StatusConfigCmd,
"report_online_server": ReportOnlineServerCmd,
"report_status_contract": ReportStatusContractCmd,
},
}

Expand Down Expand Up @@ -150,6 +153,7 @@ var ReportListCmd = &cmds.Command{
list = list[from:]
}
}

return cmds.EmitOnce(res, &ReportListCmdRet{
Records: list,
Total: total,
Expand Down Expand Up @@ -226,3 +230,40 @@ var StatusConfigCmd = &cmds.Command{
}),
},
}

var ReportOnlineServerCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "report online server. ",
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}

cfg, err := cmdenv.GetConfig(env)
if err != nil {
return err
}

spin.DC.SendDataOnline(node, cfg)

return cmds.EmitOnce(res, "report online server ok!")
},
}

var ReportStatusContractCmd = &cmds.Command{
Helptext: cmds.HelpText{
Tagline: "report status-contract. ",
},
RunTimeout: 5 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
err := reportstatus.CmdReportStatus()
if err != nil {
return err
}

return cmds.EmitOnce(res, "report status contract ok!")
},
}
46 changes: 0 additions & 46 deletions reportstatus/checkreport.go

This file was deleted.

26 changes: 16 additions & 10 deletions reportstatus/reportstatus.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,8 @@ const (
//ReportStatusTime = 60 * time.Second // 10 * time.Minute
)

func Init(transactionService transaction.Service, cfg *config.Config, configRoot string, statusAddress common.Address, chainId int64) error {
New(statusAddress, transactionService, cfg)

err := CheckExistLastOnline(cfg, configRoot, chainId)
if err != nil {
return err
}
return nil
func Init(transactionService transaction.Service, cfg *config.Config, statusAddress common.Address) Service {
return New(statusAddress, transactionService, cfg)
}

func isReportStatusEnabled(cfg *config.Config) bool {
Expand All @@ -56,6 +50,9 @@ type Service interface {

// CheckReportStatus check report status heart info to statusContract
CheckReportStatus() error

// CheckLastOnlineInfo check last online info.
CheckLastOnlineInfo(peerId, bttcAddr string) error
}

func New(statusAddress common.Address, transactionService transaction.Service, cfg *config.Config) Service {
Expand Down Expand Up @@ -153,8 +150,17 @@ func getGasPrice(request *transaction.TxRequest) *big.Int {
return gasPrice
}

// report heart status
func (s *service) checkLastOnlineInfo(peerId, bttcAddr string) error {
func CmdReportStatus() error {
_, err := serv.ReportStatus()
if err != nil {
log.Errorf("ReportStatus err:%+v", err)
return err
}
return nil
}

// CheckLastOnlineInfo report heart status
func (s *service) CheckLastOnlineInfo(peerId, bttcAddr string) error {
callData, err := statusABI.Pack("getStatus", peerId)
if err != nil {
return err
Expand Down
2 changes: 2 additions & 0 deletions spin/analytics.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type dcWrap struct {
//Server URL for data collection
var (
log = logging.Logger("spin")
DC *dcWrap
)

// other constants
Expand Down Expand Up @@ -84,6 +85,7 @@ func Analytics(api iface.CoreAPI, cfgRoot string, node *core.IpfsNode, BTFSVersi
dc.api = api
dc.pn = new(nodepb.Node)
dc.config = configuration
DC = dc

if isAnalyticsEnabled(dc.config) {
if dc.config.Experimental.Analytics != dc.config.Experimental.StorageHostEnabled {
Expand Down
4 changes: 2 additions & 2 deletions spin/analytics_online.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (dc *dcWrap) doSendDataOnline(ctx context.Context, config *config.Config, s
})
}

func (dc *dcWrap) sendDataOnline(node *core.IpfsNode, config *config.Config) {
func (dc *dcWrap) SendDataOnline(node *core.IpfsNode, config *config.Config) {
sm, errs, err := dc.doPrepDataOnline(node)
if errs == nil {
errs = make([]error, 0)
Expand Down Expand Up @@ -176,7 +176,7 @@ func (dc *dcWrap) collectionAgentOnline(node *core.IpfsNode) {
//fmt.Println("")
//fmt.Println("--- online agent ---")

dc.sendDataOnline(node, cfg)
dc.SendDataOnline(node, cfg)
}
}
}

0 comments on commit 36ff32a

Please sign in to comment.