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

Commit ced7194

Browse files
committed
[#1800] Extract ipfs.Get|PutCachedPubkey from app
1 parent a22744d commit ced7194

File tree

6 files changed

+50
-31
lines changed

6 files changed

+50
-31
lines changed

api/jsonapi.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import (
2424
ipnspath "gx/ipfs/QmQAgv6Gaoe2tQpcabqwKXKChp2MZ7i3UXv9DqTTaxCaTR/go-path"
2525
files "gx/ipfs/QmQmhotPUzVrMEWNK3x1R5jQ5ZHWyL7tVUrmRPjrBrvyCb/go-ipfs-files"
2626
cid "gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
27-
datastore "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
2827
ipns "gx/ipfs/QmUwMnKKjH3JwGKNVZ3TcP37W93xzqNA4ECFFiMo6sXkkc/go-ipns"
2928
iface "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core"
3029
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
@@ -3778,6 +3777,7 @@ func (i *jsonAPIHandler) GETWalletStatus(w http.ResponseWriter, r *http.Request)
37783777
}
37793778

37803779
func (i *jsonAPIHandler) GETIPNS(w http.ResponseWriter, r *http.Request) {
3780+
ipfsStore := i.node.IpfsNode.Repo.Datastore()
37813781
_, peerID := path.Split(r.URL.Path)
37823782

37833783
pid, err := peer.IDB58Decode(peerID)
@@ -3786,7 +3786,7 @@ func (i *jsonAPIHandler) GETIPNS(w http.ResponseWriter, r *http.Request) {
37863786
return
37873787
}
37883788

3789-
val, err := i.node.IpfsNode.Repo.Datastore().Get(namesys.IpnsDsKey(pid))
3789+
val, err := ipfsStore.Get(namesys.IpnsDsKey(pid))
37903790
if err != nil { // No record in datastore
37913791
ErrorResponse(w, http.StatusNotFound, err.Error())
37923792
return
@@ -3795,7 +3795,7 @@ func (i *jsonAPIHandler) GETIPNS(w http.ResponseWriter, r *http.Request) {
37953795
var keyBytes []byte
37963796
pubkey := i.node.IpfsNode.Peerstore.PubKey(pid)
37973797
if pubkey == nil || !pid.MatchesPublicKey(pubkey) {
3798-
keyval, err := i.node.IpfsNode.Repo.Datastore().Get(datastore.NewKey(core.KeyCachePrefix + peerID))
3798+
keyval, err := ipfs.GetCachedPubkey(ipfsStore, peerID)
37993799
if err != nil {
38003800
ErrorResponse(w, http.StatusNotFound, err.Error())
38013801
return

core/core.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
libp2p "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
1111
ma "gx/ipfs/QmTZBfrPJmjWsCvHEtX5FE6KimVJhsJg5sBbqEFYf4UZtL/go-multiaddr"
1212
cid "gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
13-
"gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
1413
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
1514
routing "gx/ipfs/QmYxUdYY9S6yg5tSPVin5GFTvtfsLauVcr7reHDD3dM8xf/go-libp2p-routing"
1615

@@ -332,8 +331,11 @@ func (n *OpenBazaarNode) EncryptMessage(peerID peer.ID, peerKey *libp2p.PubKey,
332331
ctx, cancel := context.WithTimeout(context.Background(), n.OfflineMessageFailoverTimeout)
333332
defer cancel()
334333
if peerKey == nil {
335-
var pubKey libp2p.PubKey
336-
keyval, err := n.IpfsNode.Repo.Datastore().Get(datastore.NewKey(KeyCachePrefix + peerID.Pretty()))
334+
var (
335+
pubKey libp2p.PubKey
336+
store = n.IpfsNode.Repo.Datastore()
337+
)
338+
keyval, err := ipfs.GetCachedPubkey(store, peerID.Pretty())
337339
if err != nil {
338340
pubKey, err = routing.GetPublicKey(n.IpfsNode.Routing, ctx, peerID)
339341
if err != nil {

core/profile.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ import (
2424
"github.com/imdario/mergo"
2525
)
2626

27-
// KeyCachePrefix - cache prefix for public key
28-
const KeyCachePrefix = "/pubkey/"
29-
3027
// ErrorProfileNotFound - profile not found error
3128
var ErrorProfileNotFound = errors.New("profile not found")
3229

ipfs/resolve.go

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,23 @@ package ipfs
22

33
import (
44
"context"
5-
ds "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
6-
"gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
7-
85
"time"
96

107
ipath "gx/ipfs/QmQAgv6Gaoe2tQpcabqwKXKChp2MZ7i3UXv9DqTTaxCaTR/go-path"
8+
ds "gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
119
ipnspb "gx/ipfs/QmUwMnKKjH3JwGKNVZ3TcP37W93xzqNA4ECFFiMo6sXkkc/go-ipns/pb"
10+
nameopts "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core/options/namesys"
11+
"gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
1212
"gx/ipfs/QmddjPSGZb3ieihSseFeCfVRpZzcqczPNsD2DvarSwnjJB/gogo-protobuf/proto"
1313
"gx/ipfs/QmfVj3x4D6Jkq9SEoi5n2NmoUomLwoeiwnYz2KQa15wRw6/base32"
1414

1515
"github.com/ipfs/go-ipfs/core"
1616
"github.com/ipfs/go-ipfs/namesys"
17-
18-
nameopts "gx/ipfs/QmXLwxifxwfc2bAwq6rdjbYqAsGzWsDE9RM5TWMGtykyj6/interface-go-ipfs-core/options/namesys"
1917
)
2018

2119
const (
2220
persistentCacheDbPrefix = "/ipns/persistentcache/"
21+
pubkeyCacheDbPrefix = "/pubkey/"
2322
)
2423

2524
// Resolve an IPNS record. This is a multi-step process.
@@ -94,14 +93,14 @@ func ResolveAltRoot(n *core.IpfsNode, p peer.ID, altRoot string, timeout time.Du
9493
}
9594

9695
// getFromDatastore looks in two places in the database for a record. First is
97-
// under the /ipfs/<peerID> key which is sometimes used by the DHT. The value
96+
// under the /ipns/<peerID> key which is sometimes used by the DHT. The value
9897
// returned by this location is a serialized protobuf record. The second is
99-
// under /ipfs/persistentcache/<peerID> which returns only the value (the path)
98+
// under /ipns/persistentcache/<peerID> which returns only the value (the path)
10099
// inside the protobuf record.
101100
func getFromDatastore(datastore ds.Datastore, p peer.ID) (ipath.Path, error) {
102-
ival, err := datastore.Get(namesys.IpnsDsKey(p))
101+
ival, err := datastore.Get(ipnsRecordCacheKey(p))
103102
if err != nil {
104-
pth, err := datastore.Get(ipnsCacheDsKey(p))
103+
pth, err := datastore.Get(persistentCacheKey(p))
105104
if err != nil {
106105
if err == ds.ErrNotFound {
107106
return "", namesys.ErrResolveFailed
@@ -120,9 +119,29 @@ func getFromDatastore(datastore ds.Datastore, p peer.ID) (ipath.Path, error) {
120119
}
121120

122121
func putToDatastoreCache(datastore ds.Datastore, p peer.ID, pth ipath.Path) error {
123-
return datastore.Put(ipnsCacheDsKey(p), []byte(pth.String()))
122+
return datastore.Put(persistentCacheKey(p), []byte(pth.String()))
124123
}
125124

126-
func ipnsCacheDsKey(id peer.ID) ds.Key {
125+
// PutCachedPubkey persists the pubkey using the appropriate key prefix
126+
// from the provided datastore
127+
func PutCachedPubkey(store ds.Datastore, peerID string, pubkey []byte) error {
128+
return store.Put(pubkeyCacheKey(peerID), pubkey)
129+
}
130+
131+
// GetCachedPubkey retrieves the pubkey using the appropriate key prefix
132+
// from the provided Datastore
133+
func GetCachedPubkey(store ds.Datastore, peerID string) ([]byte, error) {
134+
return store.Get(pubkeyCacheKey(peerID))
135+
}
136+
137+
func pubkeyCacheKey(id string) ds.Key {
138+
return ds.NewKey(pubkeyCacheDbPrefix + id)
139+
}
140+
141+
func persistentCacheKey(id peer.ID) ds.Key {
127142
return ds.NewKey(persistentCacheDbPrefix + base32.RawStdEncoding.EncodeToString([]byte(id)))
128143
}
144+
145+
func ipnsRecordCacheKey(id peer.ID) ds.Key {
146+
return namesys.IpnsDsKey(id)
147+
}

net/retriever/retriever.go

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
libp2p "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
99
ma "gx/ipfs/QmTZBfrPJmjWsCvHEtX5FE6KimVJhsJg5sBbqEFYf4UZtL/go-multiaddr"
1010
"gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
11-
"gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
1211
"gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
1312
ps "gx/ipfs/QmaCTz9RkrU13bm9kMB54f7atgqM4qkjDZpRwRoJiWXEqs/go-libp2p-peerstore"
1413
"gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"
@@ -29,10 +28,7 @@ import (
2928
"golang.org/x/net/proxy"
3029
)
3130

32-
const (
33-
DefaultPointerPrefixLength = 14
34-
KeyCachePrefix = "/pubkey/"
35-
)
31+
const DefaultPointerPrefixLength = 14
3632

3733
var log = logging.MustGetLogger("retriever")
3834

@@ -348,8 +344,13 @@ func (m *MessageRetriever) attemptDecrypt(ciphertext []byte, pid peer.ID, addr m
348344
return
349345
}
350346

351-
m.node.Peerstore.AddPubKey(id, pubkey)
352-
m.node.Repo.Datastore().Put(datastore.NewKey(KeyCachePrefix+id.Pretty()), env.Pubkey)
347+
if err := m.node.Peerstore.AddPubKey(id, pubkey); err != nil {
348+
log.Errorf("adding pubkey to peerstore: %s", err.Error())
349+
}
350+
store := m.node.Repo.Datastore()
351+
if err := ipfs.PutCachedPubkey(store, id.Pretty(), env.Pubkey); err != nil {
352+
log.Errorf("caching pubkey: %s", err.Error())
353+
}
353354

354355
// Respond with an ACK
355356
if env.Message.MessageType != pb.Message_OFFLINE_ACK {

net/service/handlers.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ import (
99

1010
libp2p "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
1111
"gx/ipfs/QmTbxNB1NwDesLmKTscr4udL2tVP7MaxvXnD1D9yX7g3PN/go-cid"
12-
"gx/ipfs/QmUadX5EcvrBmxAV9sE7wUWtWSqxns5K84qKJBixmcT1w9/go-datastore"
1312
peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
1413
blocks "gx/ipfs/QmYYLnAzR28nAQ4U5MFniLprnktu6eTFKibeNt96V21EZK/go-block-format"
1514

1615
"github.com/OpenBazaar/openbazaar-go/core"
16+
"github.com/OpenBazaar/openbazaar-go/ipfs"
1717
"github.com/OpenBazaar/openbazaar-go/net"
1818
"github.com/OpenBazaar/openbazaar-go/pb"
1919
"github.com/OpenBazaar/openbazaar-go/repo"
@@ -258,9 +258,9 @@ func (service *OpenBazaarService) handleOfflineRelay(p peer.ID, pmes *pb.Message
258258
if err != nil {
259259
log.Errorf("handleOfflineRelayError: %s", err.Error())
260260
}
261-
err = service.node.IpfsNode.Repo.Datastore().Put(datastore.NewKey(core.KeyCachePrefix+id.Pretty()), env.Pubkey)
262-
if err != nil {
263-
log.Errorf("handleOfflineRelayError: %s", err.Error())
261+
store := service.node.IpfsNode.Repo.Datastore()
262+
if err := ipfs.PutCachedPubkey(store, id.Pretty(), env.Pubkey); err != nil {
263+
log.Errorf("caching pubkey: %s", err.Error())
264264
}
265265

266266
// Get handler for this message type

0 commit comments

Comments
 (0)