Skip to content

Commit

Permalink
feat(PeerID): add PeerID to datasetRef
Browse files Browse the repository at this point in the history
1) add PeerID to datasetRef
2) adjust all functions that compare datasetRefs to match
3) Alter CanonicalizePeername to CanonicalizePeer (doesn’t just
canonicalize Peername, also matched PeerID and Peername)
4) Fix any spots that use CanonicalizePeername to now use
CanonicalizePeer
5) Add CanonicalizePeer tests
6) fix any tests that change now that we have PeerID in the mix!
  • Loading branch information
ramfox authored and b5 committed Mar 6, 2018
1 parent 2ef413a commit 005814e
Show file tree
Hide file tree
Showing 7 changed files with 258 additions and 112 deletions.
75 changes: 0 additions & 75 deletions api/testdata/listResponse.json
Original file line number Diff line number Diff line change
Expand Up @@ -24020,81 +24020,6 @@
}
}
},
{
"peername": "peer",
"name": "flourinated_compounds_in_fast_food_packaging",
"path": "/map/QmU9YD3gLZjBLHhe9MEftyBtMEW6tky2XczTbCVq2q84cY",
"dataset": {
"abstract": "/map/QmPUAqhiqdGDZUourgARtmMqNKWmuTPcy5JSxasvRPdvor",
"commit": {
"qri": "cm:0",
"signature": "5LB6oyPr5VjMbTcwGRTqxmBkULqoJf3uYFM7ovoHkSLdfA94hPGa8uMveXE6YAxZYAB5kaKNWcEXg1oSwCKuXqVzvmjkPJPZs6J3LBYP1GdogZNMUxz9btrYAaS6qEcu4WwnTrhfr77wKc6mTDEzgnTR6tai78AjceXeEFpBppQ1xKXX3vn5ofmeRpoFVAKUN52FZra5x6swzncPaD8TzpFb4u59kHhVy9iKZ9xUKBmAAYPvSwJBJafYgSF5fR5sEYfjCbs6GXSDgpsRhdpGgqFpezUJL4u6ghCH5HbFtjEVGCiHFq5WRATDQMQSv1YNogKo9fhLPLkvKRqipRwsswSwTdEVXS",
"timestamp": "2001-01-01T01:01:01.000000001Z",
"title": "initial commit"
},
"dataPath": "/map/QmcN7FHgCbP9MfUmurGuNh3X1DNU4B9LbjXg3r2b3VBrQZ",
"meta": {
"description": "Paper samples, paper extracts (known), paper extracts (unknown). \n\nThis dataset is associated with the following publication:\nSchaider, L., S. Balan, A. Blum, D. Andrews, M. Strynar, M. Dickinson, D. Lunderberg, J. Lang, and G. Peaslee. Fluorinated Compounds in U.S. Fast Food Packaging. Environmental Science \u0026amp; Technology Letters. American Chemical Society, Washington, DC, USA, 4(3): 105\u0026ndash;111, (2017).",
"qri": "md:0",
"title": "Fluorinated Compounds in U.S. Fast Food Packaging"
},
"qri": "ds:0",
"structure": {
"checksum": "QmcN7FHgCbP9MfUmurGuNh3X1DNU4B9LbjXg3r2b3VBrQZ",
"entries": 25,
"errCount": 1,
"format": "cbor",
"length": 1785,
"qri": "st:0",
"schema": {
"items": {
"items": [
{
"title": "sample",
"type": "string"
},
{
"title": "comments",
"type": "string"
},
{
"description": "length of sample in cm",
"title": "length_cm",
"type": "integer"
},
{
"description": "width of sample in cm",
"title": "width_cm",
"type": "integer"
},
{
"description": "area of sample in cm2",
"title": "area_cm",
"type": "integer"
},
{
"description": "vial tare in grams",
"title": "vial_tare_g",
"type": "number"
},
{
"description": "mass of vial tare with paper in grams",
"title": "vial_with_paper_g",
"type": "number"
},
{
"description": "mass of paper in grams",
"title": "mass_g",
"type": "number"
}
],
"type": "array"
},
"type": "array"
}
}
}
},
{
"peername": "peer",
"name": "movies",
Expand Down
2 changes: 1 addition & 1 deletion api/testdata/profileResponse.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"data": {
"id": "",
"id": "QmZePf5LeXow3RW5U1AgEiNbW46YnRGhZ7HPvm1UmPFPwt",
"created": "0001-01-01T00:00:00Z",
"updated": "0001-01-01T00:00:00Z",
"peername": "peer",
Expand Down
31 changes: 14 additions & 17 deletions core/datasets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/qri-io/jsonschema"
"github.com/qri-io/qri/p2p"
"github.com/qri-io/qri/repo"
"github.com/qri-io/qri/repo/profile"
"github.com/qri-io/varName"
)

Expand Down Expand Up @@ -66,28 +65,25 @@ func (r *DatasetRequests) List(p *ListParams, res *[]repo.DatasetRef) error {
return r.cli.Call("DatasetRequests.List", p, res)
}

if err := repo.CanonicalizePeername(r.repo, &p.Peername); err != nil {
return fmt.Errorf("error canonicalizing peername: %s", err.Error())
ds := &repo.DatasetRef{
Peername: p.Peername,
PeerID: p.PeerID,
}

if p.PeerID != "" {
if pid, err := profile.NewB58PeerID(p.PeerID); err == nil {
if peer, err := r.repo.Peers().GetPeer(pid); err == nil {
p.Peername = peer.Peername
}
}
if err := repo.CanonicalizePeer(r.repo, ds); err != nil {
return fmt.Errorf("error canonicalizing peer: %s", err.Error())
}

if p.Peername != "" && r.Node != nil {
replies, err := r.Node.RequestDatasetsList(p.Peername)
if ds.Peername != "" && r.Node != nil {
replies, err := r.Node.RequestDatasetsList(ds.Peername)
*res = replies
return err
} else if p.Peername != "" {
} else if ds.Peername != "" {
pro, err := r.repo.Profile()
if err != nil {
return err
}
if pro.Peername != p.Peername && r.Node == nil {
if pro.Peername != ds.Peername && r.Node == nil {
return fmt.Errorf("cannot list remote datasets without p2p connection")
}
}
Expand Down Expand Up @@ -224,10 +220,6 @@ func (r *DatasetRequests) Init(p *InitParams, res *repo.DatasetRef) error {
filename = p.DataFilename
)

if err := repo.CanonicalizePeername(r.repo, &p.Peername); err != nil {
return fmt.Errorf("error canonicalizing peername: %s", err.Error())
}

if p.URL != "" {
res, err := http.Get(p.URL)
if err != nil {
Expand Down Expand Up @@ -323,6 +315,11 @@ func (r *DatasetRequests) Init(p *InitParams, res *repo.DatasetRef) error {
}

ref := repo.DatasetRef{Peername: p.Peername, Name: name, Path: dskey.String(), Dataset: ds}

if err := repo.CanonicalizePeer(r.repo, &ref); err != nil {
return fmt.Errorf("error canonicalizing peername: %s", err.Error())
}

if err = r.repo.PutRef(ref); err != nil {
return fmt.Errorf("error adding dataset name to repo: %s", err.Error())
}
Expand Down
10 changes: 4 additions & 6 deletions core/datasets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func TestDatasetRequestsInit(t *testing.T) {

func TestDatasetRequestsList(t *testing.T) {
var (
movies, counter, cities, craigslist, flourinated repo.DatasetRef
movies, counter, cities, craigslist repo.DatasetRef
)

mr, err := testrepo.NewTestRepo()
Expand All @@ -93,8 +93,6 @@ func TestDatasetRequestsList(t *testing.T) {
cities = ref
case "craigslist":
craigslist = ref
case "flourinated_compounds_in_fast_food_packaging":
flourinated = ref
}
}

Expand All @@ -105,9 +103,9 @@ func TestDatasetRequestsList(t *testing.T) {
}{
{&ListParams{OrderBy: "", Limit: 1, Offset: 0}, nil, ""},
{&ListParams{OrderBy: "chaos", Limit: 1, Offset: -50}, nil, ""},
{&ListParams{OrderBy: "", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, flourinated, movies}, ""},
{&ListParams{OrderBy: "timestamp", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, flourinated, movies}, ""},
{&ListParams{Peername: "me", OrderBy: "timestamp", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, flourinated, movies}, ""},
{&ListParams{OrderBy: "", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, movies}, ""},
{&ListParams{OrderBy: "timestamp", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, movies}, ""},
{&ListParams{Peername: "me", OrderBy: "timestamp", Limit: 30, Offset: 0}, []repo.DatasetRef{cities, counter, craigslist, movies}, ""},
// TODO: re-enable {&ListParams{OrderBy: "name", Limit: 30, Offset: 0}, []*repo.DatasetRef{cities, counter, movies}, ""},
}

Expand Down
110 changes: 98 additions & 12 deletions repo/ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/mr-tron/base58/base58"
"github.com/multiformats/go-multihash"
"github.com/qri-io/dataset"
"github.com/qri-io/qri/repo/profile"
)

// Refstore keeps a collection of dataset references
Expand All @@ -29,6 +30,8 @@ type Refstore interface {
// keep in mind that if the information is important at all, it should
// be stored as metadata within the dataset itself.
type DatasetRef struct {
// PeerID of dataset owner
PeerID string `json:"peerID,omitempty"`
// Peername of dataset owner
Peername string `json:"peername,omitempty"`
// Unique name reference for this dataset
Expand All @@ -42,6 +45,9 @@ type DatasetRef struct {
// String implements the Stringer interface for DatasetRef
func (r DatasetRef) String() (s string) {
s = r.Peername
if s == "" {
s = r.PeerID
}
if r.Name != "" {
s += "/" + r.Name
}
Expand All @@ -56,17 +62,17 @@ func (r DatasetRef) String() (s string) {
func (r DatasetRef) Match(b DatasetRef) bool {
// fmt.Printf("\nr.Peername: %s b.Peername: %s\n", r.Peername, b.Peername)
// fmt.Printf("\nr.Name: %s b.Name: %s\n", r.Name, b.Name)
return r.Peername == b.Peername && r.Name == b.Name || r.Path == b.Path
return (r.PeerID == b.PeerID || r.Peername == b.Peername) && r.Name == b.Name || r.Path == b.Path
}

// Equal returns true only if Peername Name and Path are equal
func (r DatasetRef) Equal(b DatasetRef) bool {
return r.Peername == b.Peername && r.Name == b.Name && r.Path == b.Path
return r.Peername == b.Peername && r.PeerID == b.PeerID && r.Name == b.Name && r.Path == b.Path
}

// IsPeerRef returns true if only Peername is set
func (r DatasetRef) IsPeerRef() bool {
return r.Peername != "" && r.Name == "" && r.Path == "" && r.Dataset == nil
return (r.Peername != "" || r.PeerID != "") && r.Name == "" && r.Path == "" && r.Dataset == nil
}

// IsEmpty returns true if none of it's fields are set
Expand Down Expand Up @@ -137,6 +143,7 @@ func ParseDatasetRef(ref string) (DatasetRef, error) {
pathRefString string
path string
peername string
peerID string
datasetname string
)
// if there is an @ symbol, we are dealing with a DatasetRef
Expand Down Expand Up @@ -181,17 +188,26 @@ func ParseDatasetRef(ref string) (DatasetRef, error) {
if nameRefString != "" {
if fullnameRegex.MatchString(nameRefString) {
matches := fullnameRegex.FindStringSubmatch(nameRefString)
peername = matches[1]
if isBase58Multihash(matches[1]) {
peerID = matches[1]
} else {
peername = matches[1]
}
datasetname = matches[2]
} else if simpleRegex.MatchString(nameRefString) {
matches := simpleRegex.FindStringSubmatch(nameRefString)
peername = matches[1]
if isBase58Multihash(matches[1]) {
peerID = matches[1]
} else {
peername = matches[1]
}
} else {
return DatasetRef{}, fmt.Errorf("malformed DatasetRef string: %s", ref)
}
}

return DatasetRef{
PeerID: peerID,
Peername: peername,
Name: datasetname,
Path: path,
Expand Down Expand Up @@ -233,7 +249,7 @@ func CanonicalizeDatasetRef(r Repo, ref *DatasetRef) error {
return nil
}

if err := CanonicalizePeername(r, &ref.Peername); err != nil {
if err := CanonicalizePeer(r, ref); err != nil {
return err
}

Expand All @@ -247,22 +263,92 @@ func CanonicalizeDatasetRef(r Repo, ref *DatasetRef) error {
return nil
}

// CanonicalizePeername uses a repo to replace aliases with
// CanonicalizePeer uses a repo to replace aliases with
// canonical peernames. basically, this thing replaces "me" with the proper peername.
func CanonicalizePeername(r Repo, peername *string) error {
if *peername == "me" {
p, err := r.Profile()
func CanonicalizePeer(r Repo, ref *DatasetRef) error {
if ref.Peername == "" && ref.PeerID == "" {
return nil
}

p, err := r.Profile()
if err != nil {
return err
}

if ref.Peername == "me" || ref.Peername == p.Peername || ref.PeerID == p.ID {
if ref.Peername == "me" {
ref.Peername = p.Peername
}

if ref.Peername != "" && ref.PeerID != "" {
if ref.Peername == p.Peername && ref.PeerID != p.ID {
return fmt.Errorf("Peername and PeerID combination not valid: Peername = %s, PeerID = %s, but was given PeerID = %s", p.Peername, p.ID, ref.PeerID)
}
if ref.PeerID == p.ID && ref.Peername != p.Peername {
return fmt.Errorf("Peername and PeerID combination not valid: PeerID = %s, Peername = %s, but was given Peername = %s", p.ID, p.Peername, ref.Peername)
}
if ref.Peername == p.Peername && ref.PeerID == p.ID {
return nil
}
}

if ref.Peername != "" {
if ref.Peername != p.Peername {
return nil
}
}

if ref.PeerID != "" {
if ref.PeerID != p.ID {
return nil
}
}

ref.Peername = p.Peername
ref.PeerID = p.ID
return nil
}
if ref.PeerID != "" {
pid, err := profile.NewB58PeerID(ref.PeerID)
if err != nil {
return err
return fmt.Errorf("error converting PeerID to base58 hash: %s", err)
}

peer, err := r.Peers().GetPeer(pid)
if err != nil {
return fmt.Errorf("error fetching peers from store: %s", err)
}

if ref.Peername == "" {
ref.Peername = peer.Peername
return nil
}
if ref.Peername != peer.Peername {
return fmt.Errorf("Peername and PeerID combination not valid: PeerID = %s, Peername = %s, but was given Peername = %s", peer.ID, peer.Peername, ref.Peername)
}
}
if ref.Peername != "" {
id, err := r.Peers().GetID(ref.Peername)
if err != nil {
return fmt.Errorf("error fetching peers from store: %s", err)
}
if ref.PeerID == "" {
ref.PeerID = id.String()
return nil
}
if ref.PeerID != id.String() {
return fmt.Errorf("Peername and PeerID combination not valid: Peername = %s, PeerID = %s, but was given PeerID = %s", ref.Peername, id.String(), ref.PeerID)
}
*peername = p.Peername
}
return nil
}

// CompareDatasetRef compares two Dataset References, returning an error
// describing any difference between the two references
func CompareDatasetRef(a, b DatasetRef) error {
if a.PeerID != b.PeerID {
return fmt.Errorf("peerID mismatch. %s != %s", a.PeerID, b.PeerID)
}
if a.Peername != b.Peername {
return fmt.Errorf("peername mismatch. %s != %s", a.Peername, b.Peername)
}
Expand Down
Loading

0 comments on commit 005814e

Please sign in to comment.