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

BTFS-2419 #782

Open
wants to merge 16 commits into
base: master-v1.5.1
Choose a base branch
from
2 changes: 1 addition & 1 deletion cmd/btfs/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -557,7 +557,7 @@ func daemonFunc(req *cmds.Request, re cmds.ResponseEmitter, env cmds.Environment
return err
}
spin.Analytics(api, cctx.ConfigRoot, node, version.CurrentVersionNumber, hValue)
spin.Hosts(node, env)
spin.Hosts(node, req, re, env)
spin.Contracts(node, req, env, nodepb.ContractStat_HOST.String())
if params, err := helper.ExtractContextParams(req, env); err == nil {
spin.NewWalletWrap(params).UpdateStatus()
Expand Down
183 changes: 101 additions & 82 deletions core/commands/storage/challenge/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,49 +41,12 @@ still store a piece of file (usually a shard) as agreed in storage contract.`,
}, StorageChallengeResponseCmd.Arguments...), // append pass-through arguments
RunTimeout: 20 * time.Second,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cfg, err := cmdenv.GetConfig(env)
scr, err := ReqChallengeStorage(req, res, env, req.Arguments[0], req.Arguments[2], req.Arguments[3], req.Arguments[4], req.Arguments[5]) //(*StorageChallengeRes, error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not fully sure for this part, my understanding is decentral challenge will start another command, it seems the command you are changing still need in the future. But I am not sure.

Copy link
Contributor Author

@NathanQiu666 NathanQiu666 Jan 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The function is involved in the spin package, quite like heart beat, challengers request challenge jobs for every 60 mins.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还是没搞懂你为啥要改写这个命令,感觉上这个命令未来还是得用的,我们的router node还是得做challenge的,如果这个decentral challenge没成功的话。

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

另外,这个地方你每60分钟启动一次可能有问题;理论上这个challenge应该是完成工作以后就继续要下一个,60分钟最多check一下如果出了问题去trigger一下。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As discussed, it is ok for now.

if err != nil {
return err
}
if !cfg.Experimental.StorageClientEnabled {
return fmt.Errorf("storage client api not enabled")
}
res.RecordEvent("GetConfig")

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

res.RecordEvent("GetNode")

api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
res.RecordEvent("GetApi")
// Check if peer is reachable
pi, err := remote.FindPeer(req.Context, n, req.Arguments[0])
if err != nil {
return err
}
res.RecordEvent("FindPeer")
// Pass arguments through to host response endpoint
resp, err := remote.P2PCallStrings(req.Context, n, api, pi.ID, "/storage/challenge/response",
req.Arguments[1:]...)
if err != nil {
return err
}

res.RecordEvent("P2PCall")
var scr StorageChallengeRes
err = json.Unmarshal(resp, &scr)
if err != nil {
return err
}
res.RecordEvent("Unmarshall")
scr.TimeEvaluate = append(scr.TimeEvaluate, res.ShowEventReport())
return cmds.EmitOnce(res, &scr)

},
Type: StorageChallengeRes{},
}
Expand All @@ -109,58 +72,114 @@ the challenge request back to the caller.`,
},
RunTimeout: 1 * time.Minute,
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
cfg, err := cmdenv.GetConfig(env)
scr, err := respChallengeStorage(req, res, env, req.Arguments[1], req.Arguments[2], req.Arguments[3], req.Arguments[4])
if err != nil {
return err
}
if !cfg.Experimental.StorageHostEnabled {
return fmt.Errorf("storage host api not enabled")
}
res.RecordEvent("HGetConfig")

n, err := cmdenv.GetNode(env)
if err != nil {
return err
}
res.RecordEvent("HGetNode")
api, err := cmdenv.GetApi(env, req)
if err != nil {
return err
}
res.RecordEvent("HGetApi")
fileHash, err := cidlib.Parse(req.Arguments[1])
if err != nil {
return err
}
res.RecordEvent("HParseFileCid")
return cmds.EmitOnce(res, &scr)
},
Type: StorageChallengeRes{},
}

sh := req.Arguments[2]
shardHash, err := cidlib.Parse(sh)
func ReqChallengeStorage(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment, peerId string, fileHash string, shardHash string, chunkIndex string, nonce string) (*StorageChallengeRes, error) {
cfg, err := cmdenv.GetConfig(env)
if err != nil {
return nil, err
}
if !cfg.Experimental.StorageClientEnabled {
return nil, fmt.Errorf("storage client api not enabled")
}
res.RecordEvent("GetConfig")
n, err := cmdenv.GetNode(env)
if err != nil {
return nil, err
}
res.RecordEvent("GetNode")
api, err := cmdenv.GetApi(env, req)
if err != nil {
return nil, err
}
res.RecordEvent("GetApi")
// Check if peer is reachable
pi, err := remote.FindPeer(req.Context, n, peerId)
if err != nil {
return nil, err
}
res.RecordEvent("FindPeer")

var scr *StorageChallengeRes
if peerId == n.Identity.Pretty() {
scr, err = respChallengeStorage(req, res, env, fileHash, shardHash, chunkIndex, nonce)
if err != nil {
return err
return nil, err
}
res.RecordEvent("HParseShardCid")
chunkIndex, err := strconv.Atoi(req.Arguments[3])
} else {
resp, err := remote.P2PCallStrings(req.Context, n, api, pi.ID, "/storage/challenge/response",
req.Arguments[1:]...)
if err != nil {
return err
return nil, err
}
nonce := req.Arguments[4]
// Get (cached) challenge response object and solve challenge
sc, err := NewStorageChallengeResponse(req.Context, n, api, fileHash, shardHash, "", false, 0)
res.RecordEvent("P2PCall")
err = json.Unmarshal(resp, &scr)
if err != nil {
return err
return nil, err
}
res.RecordEvent("HNewResponse")
res.RecordEvent("Unmarshall")
}
scr.TimeEvaluate = append(scr.TimeEvaluate, res.ShowEventReport())
return scr, nil
}

err = sc.SolveChallenge(chunkIndex, nonce)
if err != nil {
return err
}
res.RecordEvent("HSolveChallenge")
return cmds.EmitOnce(res, &StorageChallengeRes{
Answer: sc.Hash,
TimeEvaluate: []string{res.ShowEventReport()},
})
},
Type: StorageChallengeRes{},
func respChallengeStorage(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment, fileHashStr string, shardHashStr string, chunkIndexStr string, nonce string) (*StorageChallengeRes, error) {
cfg, err := cmdenv.GetConfig(env)
if err != nil {
return nil, err
}
if !cfg.Experimental.StorageHostEnabled {
return nil, fmt.Errorf("storage host api not enabled")
}
res.RecordEvent("HGetConfig")

n, err := cmdenv.GetNode(env)
if err != nil {
return nil, err
}
res.RecordEvent("HGetNode")
api, err := cmdenv.GetApi(env, req)
if err != nil {
return nil, err
}
res.RecordEvent("HGetApi")
fileHash, err := cidlib.Parse(fileHashStr)
if err != nil {
return nil, err
}
res.RecordEvent("HParseFileCid")
shardHash, err := cidlib.Parse(shardHashStr)
if err != nil {
return nil, err
}
res.RecordEvent("HParseShardCid")
chunkIndex, err := strconv.Atoi(chunkIndexStr)
if err != nil {
return nil, err
}
// Get (cached) challenge response object and solve challenge
sc, err := NewStorageChallengeResponse(req.Context, n, api, fileHash, shardHash, "", false, 0)
if err != nil {
return nil, err
}
res.RecordEvent("HNewResponse")

err = sc.SolveChallenge(chunkIndex, nonce)
if err != nil {
return nil, err
}
res.RecordEvent("HSolveChallenge")

scr := &StorageChallengeRes{
Answer: sc.Hash,
TimeEvaluate: []string{res.ShowEventReport()},
}
return scr, nil
}
Loading