Skip to content

Commit

Permalink
Checkpoint.
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Jun 1, 2017
1 parent c7462ac commit 211e375
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 8 deletions.
19 changes: 18 additions & 1 deletion core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,9 +372,26 @@ func (n *IpfsNode) setupIpnsRepublisher() error {
return err
}

n.IpnsRepub = ipnsrp.NewRepublisher(n.Routing, n.Repo.Datastore(), n.Peerstore)
n.IpnsRepub = ipnsrp.NewRepublisher(n.Routing, n.Repo.Datastore(), n.PrivateKey, n.Repo.Keystore())
n.IpnsRepub.AddName(n.Identity)

ks := n.Repo.Keystore()
keys, err := ks.List()
if err != nil {
return err
}
for _, keyName := range keys {
privKey, err := ks.Get(keyName)
if err != nil {
return err
}
peer, err := peer.IDFromPrivateKey(privKey)
if err != nil {
return err
}
n.IpnsRepub.AddName(peer)
}

if cfg.Ipns.RepublishPeriod != "" {
d, err := time.ParseDuration(cfg.Ipns.RepublishPeriod)
if err != nil {
Expand Down
34 changes: 34 additions & 0 deletions keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"strings"

ci "gx/ipfs/QmP1DfoUjiWH2ZBo1PBH6FupdBucbDepx3HpWmEY6JMUpY/go-libp2p-crypto"
peer "gx/ipfs/QmdS9KpbDyPrieswibZhkod1oXqRwZJrUPzxCofAMWpFGq/go-libp2p-peer"
)

type Keystore interface {
Expand All @@ -17,6 +18,8 @@ type Keystore interface {
Put(string, ci.PrivKey) error
// Get retrieve a key from the Keystore
Get(string) (ci.PrivKey, error)
// GetById retrieve gets private key assisted with the pubkeyhash
GetById(peer.ID) (ci.PrivKey, error)
// Delete remove a key from the Keystore
Delete(string) error
// List return a list of key identifier
Expand Down Expand Up @@ -130,6 +133,37 @@ func (ks *FSKeystore) Get(name string) (ci.PrivKey, error) {
return ci.UnmarshalPrivateKey(data)
}

// GetById retrieve gets private key assisted with the pubkeyhash
func (ks *FSKeystore) GetById(want peer.ID) (ci.PrivKey, error) {
names, err := ks.List()
if err != nil {
return nil, err
}
var lastErr error
for _, name := range names {
sk, err := ks.Get(name)
if err != nil {
// don't abort on error as we may still be able to find
// the key
lastErr = err
continue
}
id, err := peer.IDFromPrivateKey(sk)
if err != nil {
lastErr = err
continue
}
if want == id {
return sk, nil
}
}
if lastErr == nil {
return nil, ErrNoSuchKey
} else {
return nil, lastErr
}
}

// Delete remove a key from the Keystore
func (ks *FSKeystore) Delete(name string) error {
if err := validateName(name); err != nil {
Expand Down
30 changes: 23 additions & 7 deletions namesys/republisher/repub.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import (
"sync"
"time"

keystore "github.com/ipfs/go-ipfs/keystore"
namesys "github.com/ipfs/go-ipfs/namesys"
pb "github.com/ipfs/go-ipfs/namesys/pb"
path "github.com/ipfs/go-ipfs/path"
dshelp "github.com/ipfs/go-ipfs/thirdparty/ds-help"

pstore "gx/ipfs/QmNUVzEjq3XWJ89hegahPvyfJbTXgTaom48pLb7YBD9gHQ/go-libp2p-peerstore"
ic "gx/ipfs/QmP1DfoUjiWH2ZBo1PBH6FupdBucbDepx3HpWmEY6JMUpY/go-libp2p-crypto"
ds "gx/ipfs/QmRWDav6mzWseLWeYfVd5fvUKiVe9xNH29YfMF438fG364/go-datastore"
goprocess "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess"
gpctx "gx/ipfs/QmSF8fPo3jgVBAy8fpdjjYqgG87dkJgUprRBHRd2tmfgpP/goprocess/context"
Expand All @@ -31,9 +32,10 @@ var DefaultRebroadcastInterval = time.Hour * 4
const DefaultRecordLifetime = time.Hour * 24

type Republisher struct {
r routing.ValueStore
ds ds.Datastore
ps pstore.Peerstore
r routing.ValueStore
ds ds.Datastore
self ic.PrivKey
ks keystore.Keystore

Interval time.Duration

Expand All @@ -44,11 +46,12 @@ type Republisher struct {
entries map[peer.ID]struct{}
}

func NewRepublisher(r routing.ValueStore, ds ds.Datastore, ps pstore.Peerstore) *Republisher {
func NewRepublisher(r routing.ValueStore, ds ds.Datastore, self ic.PrivKey, ks keystore.Keystore) *Republisher {
return &Republisher{
r: r,
ps: ps,
ds: ds,
self: self,
ks: ks,
entries: make(map[peer.ID]struct{}),
Interval: DefaultRebroadcastInterval,
RecordLifetime: DefaultRecordLifetime,
Expand Down Expand Up @@ -84,7 +87,19 @@ func (rp *Republisher) republishEntries(p goprocess.Process) error {

for id, _ := range rp.entries {
log.Debugf("republishing ipns entry for %s", id)
priv := rp.ps.PrivKey(id)
var priv ic.PrivKey
selfId, err := peer.IDFromPrivateKey(rp.self)
if err != nil {
return err
}
if id == selfId {
priv = rp.self
} else {
priv, err = rp.ks.GetById(id)
if err != nil {
return err
}
}

// Look for it locally only
_, ipnskey := namesys.IpnsKeysForID(id)
Expand All @@ -100,6 +115,7 @@ func (rp *Republisher) republishEntries(p goprocess.Process) error {
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())
return err
}
}
Expand Down
27 changes: 27 additions & 0 deletions test/sharness/t0240-republisher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,33 @@ go-sleep 15s

verify_can_resolve "$num_test_nodes" "$id" "$HASH" "republisher fires after twenty seconds"



#test_expect_success "generate new key" '
#KEY2=`ipfsi 1 key gen beepboop --type ed25519`
#'

#test_expect_success "publish with new succeeds" '
# HASH=$(echo "barfoo" | ipfsi 1 add -q) &&
# ipfsi 1 name publish -k "$KEY2" $HASH
#'

#verify_can_resolve "$num_test_nodes" "$KEY2" "$HASH" "just after publishing"

#go-sleep 5s

#verify_cannot_resolve "$num_test_nodes" "$KEY2" "cannot resolve after 5 seconds"

test_expect_success "restart cluser" '
iptb restart
'

go-sleep 22s

verify_can_resolve "$num_test_nodes" "$KEY2" "$HASH" "can resolve after restarting"

teardown_iptb

exit 1

test_done

0 comments on commit 211e375

Please sign in to comment.