diff --git a/p2p/datasets.go b/p2p/datasets.go index 520cff112..41c74e04b 100644 --- a/p2p/datasets.go +++ b/p2p/datasets.go @@ -62,3 +62,35 @@ func (n *QriNode) RequestDatasetInfo(ref *repo.DatasetRef) (*repo.DatasetRef, er return resref, err } + +// DatasetLogParams encapsulates options for requesting datasets + +// RequestDatasetLog gets the log information of Peer's dataset +func (n *QriNode) RequestDatasetLog(ref *repo.DatasetRef) (*[]*repo.DatasetRef, error) { + id, err := n.Repo.Peers().IPFSPeerID(ref.Peername) + if err != nil { + return nil, fmt.Errorf("error getting peer IPFS id: %s", err.Error()) + } + res, err := n.SendMessage(id, &Message{ + Type: MtDatasetLog, + Phase: MpRequest, + Payload: ref, + }) + if err != nil { + fmt.Println("send dataset log message error:", err.Error()) + return nil, err + } + + data, err := json.Marshal(res.Payload) + if err != nil { + return nil, err + } + + resref := []*repo.DatasetRef{} + err = json.Unmarshal(data, &resref) + if len(resref) == 0 && err != nil { + err = fmt.Errorf("no log found") + } + + return &resref, err +} diff --git a/p2p/handlers.go b/p2p/handlers.go index d4180b432..d0e826a1b 100644 --- a/p2p/handlers.go +++ b/p2p/handlers.go @@ -3,11 +3,11 @@ package p2p import ( "encoding/json" "fmt" - "time" - + "github.com/ipfs/go-datastore" "github.com/ipfs/go-datastore/query" "github.com/qri-io/qri/repo" "github.com/qri-io/qri/repo/profile" + "time" pstore "gx/ipfs/QmPgDWmTmuzvP7QE5zwo1TmjbJme9pmZHNujB2453jkCTr/go-libp2p-peerstore" ma "gx/ipfs/QmXY77cVe7rVRQXZZQRioukUM7aRW3BTcAgJe12MCtb3Ji/go-multiaddr" @@ -338,9 +338,9 @@ func (n *QriNode) handleDatasetInfoRequest(r *Message) *Message { if err = json.Unmarshal(data, ref); err != nil { n.log.Infof(err.Error()) return &Message{ - Phase: MpError, Type: MtDatasetInfo, - Payload: ref, + Phase: MpError, + Payload: err, } } @@ -364,8 +364,8 @@ func (n *QriNode) handleDatasetInfoRequest(r *Message) *Message { ref.Dataset = ds return &Message{ - Phase: MpResponse, Type: MtDatasetInfo, + Phase: MpResponse, Payload: ref, } } @@ -373,3 +373,70 @@ func (n *QriNode) handleDatasetInfoRequest(r *Message) *Message { func (n *QriNode) handleDatasetInfoResponse(m *Message) error { return fmt.Errorf("not yet finished") } + +func (n *QriNode) handleDatasetLogRequest(r *Message) *Message { + data, err := json.Marshal(r.Payload) + + if err != nil { + n.log.Info(err.Error()) + return nil + } + + ref := &repo.DatasetRef{} + if err = json.Unmarshal(data, ref); err != nil { + n.log.Infof(err.Error()) + return &Message{ + Type: MtDatasetLog, + Phase: MpError, + Payload: err, + } + } + + path, err := n.Repo.GetPath(ref.Name) + if err != nil { + return &Message{ + Type: MtDatasetLog, + Phase: MpError, + Payload: err, + } + } + // TODO: probably shouldn't write over ref.Path if ref.Path is set, but + // until we make the changes to the way we use hashes to make them + // more consistent, this feels safer. + ref.Path = path.String() + + log := []*repo.DatasetRef{} + limit := 50 + + for { + ref.Dataset, err = n.Repo.GetDataset(datastore.NewKey(ref.Path)) + if err != nil { + return &Message{ + Type: MtDatasetLog, + Phase: MpError, + Payload: err, + } + } + log = append(log, ref) + + limit-- + if limit == 0 || ref.Dataset.PreviousPath == "" { + break + } + + ref, err = repo.ParseDatasetRef(ref.Dataset.PreviousPath) + + if err != nil { + return &Message{ + Type: MtDatasetLog, + Phase: MpError, + Payload: err, + } + } + } + return &Message{ + Type: MtDatasetLog, + Phase: MpResponse, + Payload: &log, + } +} diff --git a/p2p/message.go b/p2p/message.go index 1e24908f6..1cf3c4d0e 100644 --- a/p2p/message.go +++ b/p2p/message.go @@ -36,6 +36,8 @@ const ( MtNodes = MsgType("NODES") // MtDatasetInfo gets info on a dataset MtDatasetInfo = MsgType("DATASET_INFO") + // MtDatasetLog gets log of a dataset + MtDatasetLog = MsgType("DATASET_LOG") ) func (mt MsgType) String() string { @@ -239,6 +241,8 @@ func (n *QriNode) handleStream(ws *WrappedStream) { res = n.handleNodesRequest(r) case MtDatasetInfo: res = n.handleDatasetInfoRequest(r) + case MtDatasetLog: + res = n.handleDatasetLogRequest(r) } }