Skip to content

Commit

Permalink
Move namesys republisher records into their own place in the datastore
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Dirk McCormick <[email protected]>
  • Loading branch information
dirkmc committed Mar 1, 2018
1 parent 5202f76 commit d68b5ce
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 9 deletions.
4 changes: 2 additions & 2 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -462,7 +462,7 @@ func (n *IpfsNode) startOnlineServicesWithHost(ctx context.Context, host p2phost
}

// setup name system
n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), size)
n.Namesys = namesys.NewRepublishingNameSystem(n.Routing, n.Repo.Datastore(), size)

// setup ipns republishing
return n.setupIpnsRepublisher()
Expand Down Expand Up @@ -798,7 +798,7 @@ func (n *IpfsNode) SetupOfflineRouting() error {
return err
}

n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), size)
n.Namesys = namesys.NewRepublishingNameSystem(n.Routing, n.Repo.Datastore(), size)

return nil
}
Expand Down
9 changes: 5 additions & 4 deletions namesys/republisher/repub.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,10 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro
log.Debugf("republishing ipns entry for %s", id)

// Look for it locally only
_, ipnskey := namesys.IpnsKeysForID(id)
p, seq, err := rp.getLastVal(ipnskey)
p, seq, err := rp.getLastVal(id)
if err != nil {
if err == errNoEntry {
log.Debugf("ipns entry for %s not found", id)
return nil
}
return err
Expand All @@ -138,14 +138,15 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro
eol := time.Now().Add(rp.RecordLifetime)
err = namesys.PutRecordToRouting(ctx, priv, p, seq, eol, rp.r, id)
if err != nil {
println("put record to routing error: " + err.Error())
log.Errorf("put record to routing error %v" + err)
return err
}

return nil
}

func (rp *Republisher) getLastVal(k string) (path.Path, uint64, error) {
func (rp *Republisher) getLastVal(id peer.ID) (path.Path, uint64, error) {
k := namesys.RepubKeyForID(id)
ival, err := rp.ds.Get(dshelp.NewKeyFromBinary([]byte(k)))
if err != nil {
// not found means we dont have a previously published entry
Expand Down
5 changes: 2 additions & 3 deletions namesys/republisher/repub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestRepublish(t *testing.T) {
t.Fatal(err)
}

nd.Namesys = namesys.NewNameSystem(nd.Routing, nd.Repo.Datastore(), 0)
nd.Namesys = namesys.NewRepublishingNameSystem(nd.Routing, nd.Repo.Datastore(), 0)

nodes = append(nodes, nd)
}
Expand All @@ -58,8 +58,7 @@ func TestRepublish(t *testing.T) {
// have one node publish a record that is valid for 1 second
publisher := nodes[3]
p := path.FromString("/ipfs/QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn") // does not need to be valid
rp := namesys.NewRoutingPublisher(publisher.Routing, publisher.Repo.Datastore())
err := rp.PublishWithEOL(ctx, publisher.PrivateKey, p, time.Now().Add(time.Second))
err := publisher.Namesys.PublishWithEOL(ctx, publisher.PrivateKey, p, time.Now().Add(time.Second))
if err != nil {
t.Fatal(err)
}
Expand Down
62 changes: 62 additions & 0 deletions namesys/repubns.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package namesys

import (
"context"
"time"

path "github.com/ipfs/go-ipfs/path"

ds "gx/ipfs/QmPpegoMqhAEqjncrzArm7KVWAkCm78rqL2DPuNjhPrshg/go-datastore"
routing "gx/ipfs/QmTiWLZ6Fo5j4KcTVutZJ5KWRRJrbxzmxA4td8NfEdrPh7/go-libp2p-routing"
peer "gx/ipfs/QmZoWKhxUmZ2seW4BzX6fJkNR8hh9PsGModr7q171yq2SS/go-libp2p-peer"
ci "gx/ipfs/QmaPbCnUMBohSGo3KnxEa2bHqyJVVeEEcwtqJAYxerieBo/go-libp2p-crypto"
dshelp "gx/ipfs/QmdQTPWduSeyveSxeCAte33M592isSW5Z979g81aJphrgn/go-ipfs-ds-help"
)

// repubns extends mpns to store published records in the datastore so
// that they will be picked up by the republisher
type repubns struct {
mpns
dstore ds.Datastore
}

// NewRepublishingNameSystem will construct an extension of the naming system that
// also republishes nodes
func NewRepublishingNameSystem(r routing.ValueStore, ds ds.Datastore, cachesize int) NameSystem {
ns := NewNameSystem(r, ds, cachesize)
return &repubns{*ns.(*mpns), ds}
}

// Publish publishes a name / value
func (ns *repubns) Publish(ctx context.Context, name ci.PrivKey, value path.Path) error {
return ns.PublishWithEOL(ctx, name, value, time.Now().Add(DefaultRecordTTL))
}

// Publish publishes a name / value with an EOL
func (ns *repubns) PublishWithEOL(ctx context.Context, name ci.PrivKey, value path.Path, eol time.Time) error {
err := ns.mpns.PublishWithEOL(ctx, name, value, eol)
if err != nil {
return err
}

id, err := peer.IDFromPrivateKey(name)
if err != nil {
return err
}

_, ipnskey := IpnsKeysForID(id)
rec, err := ns.dstore.Get(dshelp.NewKeyFromBinary([]byte(ipnskey)))
if err != nil {
log.Errorf("Republisher could not retrieve record from datastore that was just published")
return err
}

repubkey := RepubKeyForID(id)
log.Debugf("PublishWithEOL %v to %s", id, repubkey)
return ns.dstore.Put(dshelp.NewKeyFromBinary([]byte(repubkey)), rec)
}

// RepubKeyForID gets the key in the datastore at which a peer's record will be stored
func RepubKeyForID(id peer.ID) string {
return "/repub/" + string(id)
}

0 comments on commit d68b5ce

Please sign in to comment.