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 211e375 commit b4444e5
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 102 deletions.
18 changes: 0 additions & 18 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,24 +373,6 @@ func (n *IpfsNode) setupIpnsRepublisher() error {
}

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)
Expand Down
34 changes: 0 additions & 34 deletions keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"strings"

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

type Keystore interface {
Expand All @@ -18,8 +17,6 @@ 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 @@ -133,37 +130,6 @@ 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
83 changes: 50 additions & 33 deletions namesys/republisher/repub.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,21 +48,21 @@ type Republisher struct {

func NewRepublisher(r routing.ValueStore, ds ds.Datastore, self ic.PrivKey, ks keystore.Keystore) *Republisher {
return &Republisher{
r: r,
ds: ds,
self: self,
ks: ks,
entries: make(map[peer.ID]struct{}),
r: r,
ds: ds,
self: self,
ks: ks,
//entries: make(map[peer.ID]struct{}),
Interval: DefaultRebroadcastInterval,
RecordLifetime: DefaultRecordLifetime,
}
}

func (rp *Republisher) AddName(id peer.ID) {
rp.entrylock.Lock()
defer rp.entrylock.Unlock()
rp.entries[id] = struct{}{}
}
//func (rp *Republisher) AddName(id peer.ID) {
// rp.entrylock.Lock()
// defer rp.entrylock.Unlock()
// rp.entries[id] = struct{}{}
//}

func (rp *Republisher) Run(proc goprocess.Process) {
tick := time.NewTicker(rp.Interval)
Expand All @@ -85,39 +85,56 @@ func (rp *Republisher) republishEntries(p goprocess.Process) error {
ctx, cancel := context.WithCancel(gpctx.OnClosingContext(p))
defer cancel()

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

if rp.ks != nil {
keyNames, err := rp.ks.List()
if err != nil {
return err
}
if id == selfId {
priv = rp.self
} else {
priv, err = rp.ks.GetById(id)
for _, name := range keyNames {
priv, err := rp.ks.Get(name)
if err != nil {
return err
}
}

// Look for it locally only
_, ipnskey := namesys.IpnsKeysForID(id)
p, seq, err := rp.getLastVal(ipnskey)
if err != nil {
if err == errNoEntry {
continue
err = rp.republishEntry(ctx, priv)
if err != nil {
return err
}
return err

}
}

// update record with same sequence number
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
return nil
}

func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) error {
id, err := peer.IDFromPrivateKey(priv)
if err != nil {
return err
}

log.Debugf("republishing ipns entry for %s", id)

// Look for it locally only
_, ipnskey := namesys.IpnsKeysForID(id)
p, seq, err := rp.getLastVal(ipnskey)
if err != nil {
if err == errNoEntry {
return nil
}
return err
}

// update record with same sequence number
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
}

return nil
Expand Down
3 changes: 1 addition & 2 deletions namesys/republisher/repub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,9 @@ func TestRepublish(t *testing.T) {
// The republishers that are contained within the nodes have their timeout set
// to 12 hours. Instead of trying to tweak those, we're just going to pretend
// they dont exist and make our own.
repub := NewRepublisher(publisher.Routing, publisher.Repo.Datastore(), publisher.Peerstore)
repub := NewRepublisher(publisher.Routing, publisher.Repo.Datastore(), publisher.PrivateKey, publisher.Repo.Keystore())
repub.Interval = time.Second
repub.RecordLifetime = time.Second * 5
repub.AddName(publisher.Identity)

proc := goprocess.Go(repub.Run)
defer proc.Close()
Expand Down
33 changes: 18 additions & 15 deletions test/sharness/t0240-republisher.sh
Original file line number Diff line number Diff line change
Expand Up @@ -95,29 +95,32 @@ 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 "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 -t 5s -k "$KEY2" $HASH
'

#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"

#verify_can_resolve "$num_test_nodes" "$KEY2" "$HASH" "just after publishing"
go-sleep 5s

#go-sleep 5s
verify_cannot_resolve "$num_test_nodes" "$KEY2" "cannot resolve after 5 seconds"

#verify_cannot_resolve "$num_test_nodes" "$KEY2" "cannot resolve after 5 seconds"
go-sleep 15s

test_expect_success "restart cluser" '
iptb restart
'
verify_can_resolve "$num_test_nodes" "$KEY2" "$HASH" "can resolve again after republish"

#test_expect_success "restart cluser" '
#iptb restart
#'

go-sleep 22s
#go-sleep 22s

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

teardown_iptb

Expand Down

0 comments on commit b4444e5

Please sign in to comment.