Skip to content

Commit 347a28f

Browse files
authored
Merge pull request #715 from fluree/fix/ipfs-ns
2 parents 1507c85 + a870d34 commit 347a28f

File tree

4 files changed

+112
-32
lines changed

4 files changed

+112
-32
lines changed

dev/ipfs_demo.clj

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
(ns ipfs-demo
2+
(:require [fluree.db.json-ld.api :as fluree]
3+
[fluree.db.did :as did]))
4+
5+
;; dev namespace for combining ledgers/dbs using :include option
6+
7+
(comment
8+
9+
(def ipfs-conn @(fluree/connect-ipfs
10+
{:server nil ;; use default
11+
:ipns "Fluree1"
12+
:context {:id "@id"
13+
:type "@type"
14+
:schema "http://schema.org/"
15+
:rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
16+
:rdfs "http://www.w3.org/2000/01/rdf-schema#"
17+
:wiki "https://www.wikidata.org/wiki/"
18+
:skos "http://www.w3.org/2008/05/skos#"
19+
:f "https://ns.flur.ee/ledger#"}
20+
:did (did/private->did-map "8ce4eca704d653dec594703c81a84c403c39f262e54ed014ed857438933a2e1c")}))
21+
22+
(def ledger @(fluree/create ipfs-conn "my/test-ledger"))
23+
(def db (fluree/db ledger))
24+
25+
(def default-context
26+
{:id "@id"
27+
:type "@type"
28+
:graph "@graph"
29+
:xsd "http://www.w3.org/2001/XMLSchema#"
30+
:rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#"
31+
:rdfs "http://www.w3.org/2000/01/rdf-schema#"
32+
:sh "http://www.w3.org/ns/shacl#"
33+
:schema "http://schema.org/"
34+
:skos "http://www.w3.org/2008/05/skos#"
35+
:wiki "https://www.wikidata.org/wiki/"
36+
:f "https://ns.flur.ee/ledger#"
37+
:ex "http://example.org/"})
38+
39+
40+
(def tx-1 [{:id :ex/brian,
41+
:type :ex/User,
42+
:schema/name "Brian"
43+
:ex/last "Smith"
44+
:schema/email "[email protected]"
45+
:schema/age 50
46+
:ex/favNums 7
47+
:ex/scores [76 80 15]}
48+
{:id :ex/alice,
49+
:type :ex/User,
50+
:schema/name "Alice"
51+
:ex/last "Smith"
52+
:schema/email "[email protected]"
53+
:ex/favColor "Green"
54+
:schema/age 42
55+
:ex/favNums [42, 76, 9]
56+
:ex/scores [102 92.5 90]}
57+
{:id :ex/cam,
58+
:type :ex/User,
59+
:schema/name "Cam"
60+
:ex/last "Jones"
61+
:ex/email "[email protected]"
62+
:schema/age 34
63+
:ex/favNums [5, 10]
64+
:ex/scores [97.2 100 80]
65+
:ex/friend [:ex/brian :ex/alice]}])
66+
67+
(def db1 @(fluree/stage db {"@context" default-context
68+
"insert" tx-1}))
69+
70+
@(fluree/query db1 {:context default-context
71+
:select {:ex/cam [:*]}})
72+
73+
(def c1 @(fluree/commit! ledger db1))
74+
75+
(fluree/status ledger)
76+
77+
)

src/fluree/db/conn/ipfs.cljc

+30-31
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require [fluree.db.storage :as storage]
33
[fluree.db.index :as index]
44
[fluree.db.util.context :as ctx-util]
5+
[fluree.db.util.core :as util]
56
#?(:clj [fluree.db.full-text :as full-text])
67
[fluree.db.util.log :as log :include-macros true]
78
[fluree.db.conn.proto :as conn-proto]
@@ -12,6 +13,7 @@
1213
[fluree.db.serde.json :refer [json-serde]]
1314
[fluree.db.method.ipfs.keys :as ipfs-keys]
1415
[fluree.db.indexer.default :as idx-default]
16+
[fluree.db.nameservice.ipns :as ns-ipns]
1517
[fluree.db.conn.cache :as conn-cache])
1618
#?(:clj (:import (java.io Writer))))
1719

@@ -26,7 +28,7 @@
2628

2729
(defrecord IPFSConnection [id state ledger-defaults lru-cache-atom
2830
serializer parallelism msg-in-ch msg-out-ch
29-
ipfs-endpoint]
31+
nameservices ipfs-endpoint]
3032

3133
conn-proto/iStorage
3234
(-c-read [_ commit-key]
@@ -56,6 +58,7 @@
5658
;; ;; TODO - register/submit event
5759
;; (log/warn "-msg-out: " msg)
5860
;; :TODO))
61+
(-nameservices [_] nameservices)
5962
(-state [_] @state)
6063
(-state [_ ledger] (get @state ledger))
6164

@@ -92,45 +95,40 @@
9295
(pr (conn-core/printer-map conn)))))
9396

9497
(defn ledger-defaults
95-
"Normalizes ledger defaults settings"
96-
[ipfs-endpoint {:keys [ipns context context-type did indexer] :as _defaults}]
97-
(go-try
98-
(let [ipns-default-key (or (:key ipns) "self")
99-
ipns-default-address (<? (ipfs-keys/address ipfs-endpoint ipns-default-key))
100-
new-indexer-fn (cond
101-
(fn? indexer)
102-
indexer
103-
104-
(or (map? indexer) (nil? indexer))
105-
(fn [opts]
106-
(idx-default/create (merge indexer opts)))
107-
108-
:else
109-
(throw (ex-info (str "Expected an indexer constructor fn or "
110-
"default indexer options map. Provided: " indexer)
111-
{:status 400 :error :db/invalid-ipfs-connection})))]
112-
(when-not ipns-default-address
113-
(throw (ex-info (str "IPNS publishing appears to have an issue. No corresponding ipns address found for key: "
114-
ipns-default-key)
115-
{:status 400 :error :db/ipfs-keys})))
116-
{:ipns {:key ipns-default-key
117-
:address ipns-default-address}
118-
:did did
119-
:indexer new-indexer-fn})))
98+
[{:keys [did indexer]}]
99+
{:did did
100+
:indexer (cond
101+
(fn? indexer)
102+
indexer
103+
104+
(or (map? indexer) (nil? indexer))
105+
(fn [opts]
106+
(idx-default/create (merge indexer opts)))
107+
108+
:else
109+
(throw (ex-info (str "Expected an indexer constructor fn or "
110+
"default indexer options map. Provided: " indexer)
111+
{:status 400 :error :db/invalid-file-connection})))})
112+
113+
(defn default-ipns-nameservice
114+
[ipfs-endpoint ipns-profile]
115+
(ns-ipns/initialize ipfs-endpoint ipns-profile))
120116

121117

122118
(defn connect
123119
"Creates a new IPFS connection."
124-
[{:keys [server parallelism lru-cache-atom memory defaults serializer]
120+
[{:keys [server parallelism lru-cache-atom memory ipns defaults serializer nameservices]
125121
:or {server "http://127.0.0.1:5001/"
126-
serializer (json-serde)}}]
122+
serializer (json-serde)
123+
ipns "self"}}]
127124
(go-try
128125
(let [ipfs-endpoint (or server "http://127.0.0.1:5001/") ;; TODO - validate endpoint looks like a good URL and ends in a '/' or add it
129-
ledger-defaults (<? (ledger-defaults ipfs-endpoint defaults))
126+
ledger-defaults (ledger-defaults defaults)
130127
memory (or memory 1000000) ;; default 1MB memory
131128
conn-id (str (random-uuid))
132129
state (conn-core/blank-state)
133-
130+
nameservices* (util/sequential
131+
(or nameservices (<? (default-ipns-nameservice ipfs-endpoint ipns))))
134132
cache-size (conn-cache/memory->cache-size memory)
135133
lru-cache-atom (or lru-cache-atom (atom (conn-cache/create-lru-cache cache-size)))]
136134
;; TODO - need to set up monitor loops for async chans
@@ -142,4 +140,5 @@
142140
:msg-in-ch (chan)
143141
:msg-out-ch (chan)
144142
:state state
145-
:lru-cache-atom lru-cache-atom}))))
143+
:lru-cache-atom lru-cache-atom
144+
:nameservices nameservices*}))))

src/fluree/db/method/ipfs/core.cljc

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@
6060
[ipfs-endpoint commit-map]
6161
(go-try
6262
(let [{:keys [meta db ledger-state ns]} commit-map
63-
my-ns-iri (some #(when (re-matches #"^fluree:file:.+" (:id %)) (:id %)) ns)
63+
my-ns-iri (some #(when (re-matches #"^fluree:ipns:.+" (:id %)) (:id %)) ns)
6464
{:keys [t]} db
6565
{:keys [hash size]} meta
6666
{:keys [ipns-address relative-address]} (address-parts my-ns-iri)

src/fluree/db/nameservice/ipns.cljc

+4
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@
8585
[ipfs-endpoint ipns-key]
8686
(go-try
8787
(let [base-address (<? (ipfs-keys/address ipfs-endpoint ipns-key))]
88+
(when-not base-address
89+
(throw (ex-info (str "IPNS publishing appears to have an issue. No corresponding ipns address found for key: "
90+
ipns-key)
91+
{:status 400 :error :db/ipfs-keys})))
8892
(map->IpnsNameService {:ipfs-endpoint ipfs-endpoint
8993
:ipns-key ipns-key
9094
:base-address base-address

0 commit comments

Comments
 (0)