Skip to content

Commit 3d4e275

Browse files
committed
refactor: use ipns.Name instead of peer.ID
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.
1 parent e34607c commit 3d4e275

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

ipns/name.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const (
2525
// [Multihash]: https://multiformats.io/multihash/
2626
// [IPNS Name]: https://specs.ipfs.tech/ipns/ipns-record/#ipns-name
2727
type Name struct {
28-
src []byte
28+
src string
2929
}
3030

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

5858
// NameFromPeer creates a [Name] from the given [peer.ID].
5959
func NameFromPeer(pid peer.ID) Name {
60-
return Name{src: []byte(pid)}
60+
return Name{src: string(pid)}
6161
}
6262

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

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

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

133133
// Equal returns whether the records are equal.
134134
func (n Name) Equal(other Name) bool {
135-
return bytes.Equal(n.src, other.src)
135+
return n.src == other.src
136136
}
137137

138138
// AsPath returns the IPNS Name as a [path.Path] prefixed by [path.IPNSNamespace].

namesys/ipns_publisher.go

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,16 @@ func (p *IPNSPublisher) Publish(ctx context.Context, priv crypto.PrivKey, value
5656

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

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

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

119-
value, err := p.ds.Get(ctx, IpnsDsKey(id))
119+
value, err := p.ds.Get(ctx, IpnsDsKey(name))
120120
switch err {
121121
case nil:
122122
case ds.ErrNotFound:
123123
if !checkRouting {
124124
return nil, nil
125125
}
126-
routingKey := ipns.NameFromPeer(id).RoutingKey()
126+
routingKey := name.RoutingKey()
127127
value, err = p.routing.GetValue(ctx, string(routingKey))
128128
if err != nil {
129129
// Not found or other network issue. Can't really do
130130
// anything about this case.
131131
if err != routing.ErrNotFound {
132-
log.Debugf("error when determining the last published IPNS record for %s: %s", id, err)
132+
log.Debugf("error when determining the last published IPNS record for %s: %s", name, err)
133133
}
134134

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

150151
p.mu.Lock()
151152
defer p.mu.Unlock()
152153

153154
// get previous records sequence number
154-
rec, err := p.GetPublished(ctx, id, true)
155+
rec, err := p.GetPublished(ctx, name, true)
155156
if err != nil {
156157
return nil, err
157158
}
@@ -188,7 +189,7 @@ func (p *IPNSPublisher) updateRecord(ctx context.Context, k crypto.PrivKey, valu
188189
}
189190

190191
// Put the new record.
191-
dsKey := IpnsDsKey(id)
192+
dsKey := IpnsDsKey(name)
192193
if err := p.ds.Put(ctx, dsKey, data); err != nil {
193194
return nil, err
194195
}

namesys/ipns_publisher_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func TestAsyncDS(t *testing.T) {
9999
err = publisher.Publish(ctx, ipnsFakeID.PrivateKey(), ipnsVal)
100100
require.NoError(t, err)
101101

102-
ipnsKey := IpnsDsKey(ipnsFakeID.ID())
102+
ipnsKey := IpnsDsKey(ipns.NameFromPeer(ipnsFakeID.ID()))
103103

104104
for k := range ds.syncKeys {
105105
if k.IsAncestorOf(ipnsKey) || k.Equal(ipnsKey) {

namesys/republisher/repub.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ func (rp *Republisher) republishEntry(ctx context.Context, priv ic.PrivKey) erro
141141
log.Debugf("republishing ipns entry for %s", id)
142142

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

176-
func (rp *Republisher) getLastIPNSRecord(ctx context.Context, id peer.ID) (*ipns.Record, error) {
176+
func (rp *Republisher) getLastIPNSRecord(ctx context.Context, name ipns.Name) (*ipns.Record, error) {
177177
// Look for it locally only
178-
val, err := rp.ds.Get(ctx, namesys.IpnsDsKey(id))
178+
val, err := rp.ds.Get(ctx, namesys.IpnsDsKey(name))
179179
switch err {
180180
case nil:
181181
case ds.ErrNotFound:

namesys/republisher/repub_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,17 +191,17 @@ func TestLongEOLRepublish(t *testing.T) {
191191
err = verifyResolution(nsystems, name, p)
192192
require.NoError(t, err)
193193

194-
rec, err := getLastIPNSRecord(ctx, publisher.store, publisher.h.ID())
194+
rec, err := getLastIPNSRecord(ctx, publisher.store, ipns.NameFromPeer(publisher.h.ID()))
195195
require.NoError(t, err)
196196

197197
finalEol, err := rec.Validity()
198198
require.NoError(t, err)
199199
require.Equal(t, expiration.UTC(), finalEol.UTC())
200200
}
201201

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

0 commit comments

Comments
 (0)