Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit 209d6cb

Browse files
committed
[#1800] Extract ipfs.GetCachedIPNSRecord and ipfs.DeleteCachedIPNSRecord
1 parent acbc078 commit 209d6cb

File tree

5 files changed

+46
-41
lines changed

5 files changed

+46
-41
lines changed

api/jsonapi.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
3030
routing "gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing"
3131
ps "gx/ipfs/QmaCTz9RkrU13bm9kMB54f7atgqM4qkjDZpRwRoJiWXEqs/go-libp2p-peerstore"
32+
ggproto "gx/ipfs/QmddjPSGZb3ieihSseFeCfVRpZzcqczPNsD2DvarSwnjJB/gogo-protobuf/proto"
3233
mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
3334

3435
"github.com/OpenBazaar/jsonpb"
@@ -44,7 +45,6 @@ import (
4445
"github.com/golang/protobuf/proto"
4546
"github.com/golang/protobuf/ptypes"
4647
"github.com/ipfs/go-ipfs/core/coreapi"
47-
"github.com/ipfs/go-ipfs/namesys"
4848
"github.com/ipfs/go-ipfs/repo/fsrepo"
4949
)
5050

@@ -3786,7 +3786,7 @@ func (i *jsonAPIHandler) GETIPNS(w http.ResponseWriter, r *http.Request) {
37863786
return
37873787
}
37883788

3789-
val, err := ipfsStore.Get(namesys.IpnsDsKey(pid))
3789+
peerIPNSRecord, err := ipfs.GetCachedIPNSRecord(ipfsStore, pid)
37903790
if err != nil { // No record in datastore
37913791
ErrorResponse(w, http.StatusNotFound, err.Error())
37923792
return
@@ -3813,8 +3813,13 @@ func (i *jsonAPIHandler) GETIPNS(w http.ResponseWriter, r *http.Request) {
38133813
Pubkey string `json:"pubkey"`
38143814
Record string `json:"record"`
38153815
}
3816+
peerIPNSBytes, err := ggproto.Marshal(peerIPNSRecord)
3817+
if err != nil {
3818+
ErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("marshaling IPNS record: %s", err.Error()))
3819+
return
3820+
}
38163821

3817-
ret := KeyAndRecord{hex.EncodeToString(keyBytes), string(val)}
3822+
ret := KeyAndRecord{hex.EncodeToString(keyBytes), string(peerIPNSBytes)}
38183823
retBytes, err := json.MarshalIndent(ret, "", " ")
38193824
if err != nil {
38203825
ErrorResponse(w, http.StatusInternalServerError, err.Error())
@@ -3839,9 +3844,14 @@ func (i *jsonAPIHandler) GETResolveIPNS(w http.ResponseWriter, r *http.Request)
38393844
var response = respType{PeerID: peerID}
38403845

38413846
if i.node.IpfsNode.Identity.Pretty() == peerID {
3842-
ipnsBytes, err := i.node.IpfsNode.Repo.Datastore().Get(namesys.IpnsDsKey(i.node.IpfsNode.Identity))
3847+
rec, err := ipfs.GetCachedIPNSRecord(i.node.IpfsNode.Repo.Datastore(), i.node.IpfsNode.Identity)
3848+
if err != nil {
3849+
ErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("retrieving self: %s", err))
3850+
return
3851+
}
3852+
ipnsBytes, err := proto.Marshal(rec)
38433853
if err != nil {
3844-
ErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("retrieving self from datastore: %s", err))
3854+
ErrorResponse(w, http.StatusInternalServerError, fmt.Sprintf("marshaling self: %s", err))
38453855
return
38463856
}
38473857
response.Record.Hex = hex.EncodeToString(ipnsBytes)

cmd/start.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,10 @@ import (
2323
dht "gx/ipfs/QmSY3nkMNLzh9GdbFKK5tT7YMfLpf52iUZ8ZRkr29MJaa5/go-libp2p-kad-dht"
2424
ma "gx/ipfs/QmTZBfrPJmjWsCvHEtX5FE6KimVJhsJg5sBbqEFYf4UZtL/go-multiaddr"
2525
config "gx/ipfs/QmUAuYuiafnJRZxDDX7MuruMNsicYNuyub5vUeAcupUBNs/go-ipfs-config"
26-
ipnspb "gx/ipfs/QmUwMnKKjH3JwGKNVZ3TcP37W93xzqNA4ECFFiMo6sXkkc/go-ipns/pb"
2726
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
2827
oniontp "gx/ipfs/QmYv2MbwHn7qcvAPFisZ94w85crQVpwUuv8G7TuUeBnfPb/go-onion-transport"
2928
ipfslogging "gx/ipfs/QmbkT7eMTyXfpeyB3ZMxxcxg7XH8t6uXp49jqzz4HB7BGF/go-log/writer"
3029
manet "gx/ipfs/Qmc85NSvmSG4Frn9Vb2cBc1rMyULH6D3TNVEfCzSKoUpip/go-multiaddr-net"
31-
proto "gx/ipfs/QmddjPSGZb3ieihSseFeCfVRpZzcqczPNsD2DvarSwnjJB/gogo-protobuf/proto"
3230

3331
"github.com/OpenBazaar/openbazaar-go/api"
3432
"github.com/OpenBazaar/openbazaar-go/core"
@@ -53,7 +51,6 @@ import (
5351
"github.com/ipfs/go-ipfs/commands"
5452
ipfscore "github.com/ipfs/go-ipfs/core"
5553
"github.com/ipfs/go-ipfs/core/corehttp"
56-
"github.com/ipfs/go-ipfs/namesys"
5754
"github.com/ipfs/go-ipfs/repo/fsrepo"
5855
"github.com/natefinch/lumberjack"
5956
"github.com/op/go-logging"
@@ -416,21 +413,19 @@ func (x *Start) Execute(args []string) error {
416413
}
417414

418415
// Get current directory root hash
419-
ipnskey := namesys.IpnsDsKey(nd.Identity)
420-
ival, hasherr := nd.Repo.Datastore().Get(ipnskey)
416+
cachedIPNSRecord, hasherr := ipfs.GetCachedIPNSRecord(nd.Repo.Datastore(), nd.Identity)
421417
if hasherr != nil {
422-
log.Error("get ipns key:", hasherr)
423-
}
424-
ourIpnsRecord := new(ipnspb.IpnsEntry)
425-
err = proto.Unmarshal(ival, ourIpnsRecord)
426-
if err != nil {
427-
log.Error("unmarshal record value", err)
428-
nd.Repo.Datastore().Delete(ipnskey)
418+
log.Warning(err)
419+
if err := ipfs.DeleteCachedIPNSRecord(nd.Repo.Datastore(), nd.Identity); err != nil {
420+
log.Errorf("deleting invalid IPNS record: %s", err.Error())
421+
}
429422
}
430423

431424
if x.ForceKeyCachePurge {
432425
log.Infof("forcing key purge from namesys cache...")
433-
nd.Repo.Datastore().Delete(ipnskey)
426+
if err := ipfs.DeleteCachedIPNSRecord(nd.Repo.Datastore(), nd.Identity); err != nil {
427+
log.Errorf("force-purging IPNS record: %s", err.Error())
428+
}
434429
}
435430

436431
// Wallet
@@ -583,6 +578,11 @@ func (x *Start) Execute(args []string) error {
583578
subscriber := ipfs.NewPubsubSubscriber(context.Background(), nd.PeerHost, nd.Routing, nd.Repo.Datastore(), nd.PubSub)
584579
ps := ipfs.Pubsub{Publisher: publisher, Subscriber: subscriber}
585580

581+
var rootHash string
582+
if cachedIPNSRecord != nil {
583+
rootHash = string(cachedIPNSRecord.Value)
584+
}
585+
586586
// OpenBazaar node setup
587587
core.Node = &core.OpenBazaarNode{
588588
AcceptStoreRequests: dataSharing.AcceptStoreRequests,
@@ -597,7 +597,7 @@ func (x *Start) Execute(args []string) error {
597597
PushNodes: pushNodes,
598598
RegressionTestEnable: x.Regtest,
599599
RepoPath: repoPath,
600-
RootHash: string(ourIpnsRecord.Value),
600+
RootHash: rootHash,
601601
TestnetEnable: x.Testnet,
602602
TorDialer: torDialer,
603603
UserAgent: core.USERAGENT,

ipfs/pubsub.go

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,7 @@ import (
1616
"time"
1717
)
1818

19-
const (
20-
globalIPNSTopic = "IPNS"
21-
globalBlockTopic = "BLOCK"
22-
globalCIDTopic = "CID"
23-
messageTopicPrefix = "/offlinemessage/"
24-
)
19+
const messageTopicPrefix = "/offlinemessage/"
2520

2621
type Pubsub struct {
2722
Subscriber *PubsubSubscriber

ipfs/resolve.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func resolve(n *core.IpfsNode, p peer.ID, timeout time.Duration, quorum uint) (i
7777

7878
// TODO [cp]: we should load the record count from our config and set it here. We'll need a
7979
// migration for this.
80-
pth, err := n.Namesys.Resolve(cctx, string(obIPNSCacheKey(p.Pretty())), nameopts.DhtRecordCount(quorum))
80+
pth, err := n.Namesys.Resolve(cctx, obIPNSCacheKey(p.Pretty()), nameopts.DhtRecordCount(quorum))
8181
if err != nil {
8282
return pth, err
8383
}
@@ -101,7 +101,7 @@ func ResolveAltRoot(n *core.IpfsNode, p peer.ID, altRoot string, timeout time.Du
101101
// under /ipns/persistentcache/<peerID> which returns only the value (the path)
102102
// inside the protobuf record.
103103
func getFromDatastore(datastore ds.Datastore, p peer.ID) (ipath.Path, error) {
104-
rec, err := getCachedIPNSRecord(datastore, p)
104+
rec, err := GetCachedIPNSRecord(datastore, p)
105105
if err == nil {
106106
return ipath.ParsePath(string(rec.Value))
107107
}
@@ -120,8 +120,8 @@ func putToDatastoreCache(datastore ds.Datastore, p peer.ID, pth ipath.Path) erro
120120
return datastore.Put(persistentCacheKey(p), []byte(pth.String()))
121121
}
122122

123-
// getCachedIPNSRecord retrieves the full IPNSEntry from the provided datastore if present
124-
func getCachedIPNSRecord(store ds.Datastore, id peer.ID) (*ipnspb.IpnsEntry, error) {
123+
// GetCachedIPNSRecord retrieves the full IPNSEntry from the provided datastore if present
124+
func GetCachedIPNSRecord(store ds.Datastore, id peer.ID) (*ipnspb.IpnsEntry, error) {
125125
ival, err := store.Get(nativeIPNSRecordCacheKey(id))
126126
if err != nil {
127127
return nil, fmt.Errorf("getting cached ipns record: %s", err.Error())
@@ -136,6 +136,11 @@ func getCachedIPNSRecord(store ds.Datastore, id peer.ID) (*ipnspb.IpnsEntry, err
136136
return rec, nil
137137
}
138138

139+
// DeleteCachedIPNSRecord removes the cached record associated with the provided peer.ID
140+
func DeleteCachedIPNSRecord(store ds.Datastore, id peer.ID) error {
141+
return store.Delete(nativeIPNSRecordCacheKey(id))
142+
}
143+
139144
// PutCachedPubkey persists the pubkey using the appropriate key prefix
140145
// from the provided datastore
141146
func PutCachedPubkey(store ds.Datastore, peerID string, pubkey []byte) error {

mobile/node.go

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,12 @@ import (
1919
ma "gx/ipfs/QmTZBfrPJmjWsCvHEtX5FE6KimVJhsJg5sBbqEFYf4UZtL/go-multiaddr"
2020
ipfsconfig "gx/ipfs/QmUAuYuiafnJRZxDDX7MuruMNsicYNuyub5vUeAcupUBNs/go-ipfs-config"
2121
ds "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
22-
ipnspb "gx/ipfs/QmUwMnKKjH3JwGKNVZ3TcP37W93xzqNA4ECFFiMo6sXkkc/go-ipns/pb"
2322
"gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
2423
p2phost "gx/ipfs/QmYrWiWM4qtrnCeT3R14jY3ZZyirDNJgwK57q4qFYePgbd/go-libp2p-host"
2524
"gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing"
2625
"gx/ipfs/QmbeHtaBy9nZsW4cHRcvgVY4CnDhXudE2Dr6qDxS7yg9rX/go-libp2p-record"
2726
ipfslogging "gx/ipfs/QmbkT7eMTyXfpeyB3ZMxxcxg7XH8t6uXp49jqzz4HB7BGF/go-log/writer"
2827
"gx/ipfs/Qmc85NSvmSG4Frn9Vb2cBc1rMyULH6D3TNVEfCzSKoUpip/go-multiaddr-net"
29-
"gx/ipfs/QmddjPSGZb3ieihSseFeCfVRpZzcqczPNsD2DvarSwnjJB/gogo-protobuf/proto"
3028

3129
"github.com/OpenBazaar/openbazaar-go/api"
3230
"github.com/OpenBazaar/openbazaar-go/core"
@@ -49,7 +47,6 @@ import (
4947
"github.com/ipfs/go-ipfs/commands"
5048
ipfscore "github.com/ipfs/go-ipfs/core"
5149
"github.com/ipfs/go-ipfs/core/corehttp"
52-
"github.com/ipfs/go-ipfs/namesys"
5350
"github.com/ipfs/go-ipfs/repo/fsrepo"
5451
"github.com/natefinch/lumberjack"
5552
"github.com/op/go-logging"
@@ -385,17 +382,15 @@ func (n *Node) start() error {
385382
n.OpenBazaarNode.DHT = dhtRouting
386383

387384
// Get current directory root hash
388-
ipnskey := namesys.IpnsDsKey(nd.Identity)
389-
ival, hasherr := nd.Repo.Datastore().Get(ipnskey)
390-
if hasherr != nil {
391-
log.Error("get ipns key:", hasherr)
392-
}
393-
ourIpnsRecord := new(ipnspb.IpnsEntry)
394-
err = proto.Unmarshal(ival, ourIpnsRecord)
385+
rec, err := ipfs.GetCachedIPNSRecord(nd.Repo.Datastore(), nd.Identity)
395386
if err != nil {
396-
log.Error("unmarshal record value", err)
387+
log.Warning(err)
388+
if err := ipfs.DeleteCachedIPNSRecord(nd.Repo.Datastore(), nd.Identity); err != nil {
389+
log.Errorf("deleting invalid IPNS record: %s", err.Error())
390+
}
391+
} else {
392+
n.OpenBazaarNode.RootHash = string(rec.Value)
397393
}
398-
n.OpenBazaarNode.RootHash = string(ourIpnsRecord.Value)
399394

400395
configFile, err := ioutil.ReadFile(path.Join(n.OpenBazaarNode.RepoPath, "config"))
401396
if err != nil {

0 commit comments

Comments
 (0)