Skip to content

Commit

Permalink
refactor: use ipns.Name instead of peer.ID
Browse files Browse the repository at this point in the history
I made ipns.Name.src a string in order to make the struct comparable. It can now be used as a map key, but it's still protected
from being converted around.
  • Loading branch information
hacdias committed Oct 16, 2023
1 parent a39ebdf commit 18aab37
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 23 deletions.
10 changes: 5 additions & 5 deletions ipns/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
// [Multihash]: https://multiformats.io/multihash/
// [IPNS Name]: https://specs.ipfs.tech/ipns/ipns-record/#ipns-name
type Name struct {
src []byte
src string
}

// NameFromString creates a [Name] from the given IPNS Name in its [string representation].
Expand Down Expand Up @@ -57,7 +57,7 @@ func NameFromRoutingKey(data []byte) (Name, error) {

// NameFromPeer creates a [Name] from the given [peer.ID].
func NameFromPeer(pid peer.ID) Name {
return Name{src: []byte(pid)}
return Name{src: string(pid)}
}

// NameFromCid creates a [Name] from the given [cid.Cid].
Expand All @@ -66,7 +66,7 @@ func NameFromCid(c cid.Cid) (Name, error) {
if code != mc.Libp2pKey {
return Name{}, fmt.Errorf("CID codec %q is not allowed for IPNS Names, use %q instead", code, mc.Libp2pKey)
}
return Name{src: c.Hash()}, nil
return Name{src: string(c.Hash())}, nil
}

// RoutingKey returns the binary IPNS Routing Key for the given [Name]. Note that
Expand All @@ -77,7 +77,7 @@ func NameFromCid(c cid.Cid) (Name, error) {
func (n Name) RoutingKey() []byte {
var buffer bytes.Buffer
buffer.WriteString(NamespacePrefix)
buffer.Write(n.src) // Note: we append raw multihash bytes (no multibase)
buffer.WriteString(n.src) // Note: we append raw multihash bytes (no multibase)
return buffer.Bytes()
}

Expand Down Expand Up @@ -132,7 +132,7 @@ func (n Name) MarshalJSON() ([]byte, error) {

// Equal returns whether the records are equal.
func (n Name) Equal(other Name) bool {
return bytes.Equal(n.src, other.src)
return n.src == other.src
}

// AsPath returns the IPNS Name as a [path.Path] prefixed by [path.IPNSNamespace].
Expand Down
23 changes: 12 additions & 11 deletions namesys/ipns_publisher.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ func (p *IPNSPublisher) Publish(ctx context.Context, priv crypto.PrivKey, value

// IpnsDsKey returns a datastore key given an IPNS identifier (peer
// ID). Defines the storage key for IPNS records in the local datastore.
func IpnsDsKey(id peer.ID) ds.Key {
return ds.NewKey("/ipns/" + base32.RawStdEncoding.EncodeToString([]byte(id)))
func IpnsDsKey(name ipns.Name) ds.Key {
return ds.NewKey("/ipns/" + base32.RawStdEncoding.EncodeToString([]byte(name.Peer())))
}

// ListPublished returns the latest IPNS records published by this node and
// their expiration times.
//
// This method will not search the routing system for records published by other
// nodes.
func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[peer.ID]*ipns.Record, error) {
func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[ipns.Name]*ipns.Record, error) {
query, err := p.ds.Query(ctx, dsquery.Query{
Prefix: ipns.NamespacePrefix,
})
Expand All @@ -74,7 +74,7 @@ func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[peer.ID]*ipns.Re
}
defer query.Close()

records := make(map[peer.ID]*ipns.Record)
records := make(map[ipns.Name]*ipns.Record)
for {
select {
case result, ok := <-query.Next():
Expand All @@ -100,7 +100,7 @@ func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[peer.ID]*ipns.Re
log.Errorf("ipns ds key invalid: %s", result.Key)
continue
}
records[peer.ID(pid)] = rec
records[ipns.NameFromPeer(peer.ID(pid))] = rec
case <-ctx.Done():
return nil, ctx.Err()
}
Expand All @@ -112,24 +112,24 @@ func (p *IPNSPublisher) ListPublished(ctx context.Context) (map[peer.ID]*ipns.Re
//
// If `checkRouting` is true and we have no existing record, this method will
// check the routing system for any existing records.
func (p *IPNSPublisher) GetPublished(ctx context.Context, id peer.ID, checkRouting bool) (*ipns.Record, error) {
func (p *IPNSPublisher) GetPublished(ctx context.Context, name ipns.Name, checkRouting bool) (*ipns.Record, error) {
ctx, cancel := context.WithTimeout(ctx, time.Second*30)
defer cancel()

value, err := p.ds.Get(ctx, IpnsDsKey(id))
value, err := p.ds.Get(ctx, IpnsDsKey(name))
switch err {
case nil:
case ds.ErrNotFound:
if !checkRouting {
return nil, nil
}
routingKey := ipns.NameFromPeer(id).RoutingKey()
routingKey := name.RoutingKey()
value, err = p.routing.GetValue(ctx, string(routingKey))
if err != nil {
// Not found or other network issue. Can't really do
// anything about this case.
if err != routing.ErrNotFound {
log.Debugf("error when determining the last published IPNS record for %s: %s", id, err)
log.Debugf("error when determining the last published IPNS record for %s: %s", name, err)
}

return nil, nil
Expand All @@ -146,12 +146,13 @@ func (p *IPNSPublisher) updateRecord(ctx context.Context, k crypto.PrivKey, valu
if err != nil {
return nil, err
}
name := ipns.NameFromPeer(id)

p.mu.Lock()
defer p.mu.Unlock()

// get previous records sequence number
rec, err := p.GetPublished(ctx, id, true)
rec, err := p.GetPublished(ctx, name, true)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -188,7 +189,7 @@ func (p *IPNSPublisher) updateRecord(ctx context.Context, k crypto.PrivKey, valu
}

// Put the new record.
dsKey := IpnsDsKey(id)
dsKey := IpnsDsKey(name)
if err := p.ds.Put(ctx, dsKey, data); err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion namesys/ipns_publisher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func TestAsyncDS(t *testing.T) {
err = publisher.Publish(ctx, ipnsFakeID.PrivateKey(), ipnsVal)
require.NoError(t, err)

ipnsKey := IpnsDsKey(ipnsFakeID.ID())
ipnsKey := IpnsDsKey(ipns.NameFromPeer(ipnsFakeID.ID()))

for k := range ds.syncKeys {
if k.IsAncestorOf(ipnsKey) || k.Equal(ipnsKey) {
Expand Down
6 changes: 3 additions & 3 deletions namesys/republisher/repub.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro
log.Debugf("republishing ipns entry for %s", id)

// Look for it locally only
rec, err := rp.getLastIPNSRecord(ctx, id)
rec, err := rp.getLastIPNSRecord(ctx, ipns.NameFromPeer(id))
if err != nil {
if err == errNoEntry {
span.SetAttributes(attribute.Bool("NoEntry", true))
Expand Down Expand Up @@ -173,9 +173,9 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro
return err
}

func (rp *Republisher) getLastIPNSRecord(ctx context.Context, id peer.ID) (*ipns.Record, error) {
func (rp *Republisher) getLastIPNSRecord(ctx context.Context, name ipns.Name) (*ipns.Record, error) {
// Look for it locally only
val, err := rp.ds.Get(ctx, namesys.IpnsDsKey(id))
val, err := rp.ds.Get(ctx, namesys.IpnsDsKey(name))
switch err {
case nil:
case ds.ErrNotFound:
Expand Down
6 changes: 3 additions & 3 deletions namesys/republisher/repub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,17 +191,17 @@ func TestLongEOLRepublish(t *testing.T) {
err = verifyResolution(nsystems, name, p)
require.NoError(t, err)

rec, err := getLastIPNSRecord(ctx, publisher.store, publisher.h.ID())
rec, err := getLastIPNSRecord(ctx, publisher.store, ipns.NameFromPeer(publisher.h.ID()))
require.NoError(t, err)

finalEol, err := rec.Validity()
require.NoError(t, err)
require.Equal(t, expiration.UTC(), finalEol.UTC())
}

func getLastIPNSRecord(ctx context.Context, dstore ds.Datastore, id peer.ID) (*ipns.Record, error) {
func getLastIPNSRecord(ctx context.Context, dstore ds.Datastore, name ipns.Name) (*ipns.Record, error) {
// Look for it locally only
val, err := dstore.Get(ctx, namesys.IpnsDsKey(id))
val, err := dstore.Get(ctx, namesys.IpnsDsKey(name))
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 18aab37

Please sign in to comment.