@cryptotaxi247 / kubo / commits / 2944360f5

New NameSystem interface

type NameSystem interface { Resolver Publisher } should say it all. cc @whyrusleeping

Juan Batiz-Benet committed Oct 8, 2014 at 04:14 UTC 2944360f5c2f667a887e98cccf2162475f5be7f3
13 files changed +138 -112
core/commands/publish.go
+1 -1
@@ -19,7 +19,7 @@ func Publish(n *core.IpfsNode, args []string, opts map[string]interface{}, out i
19
20 k := n.Identity.PrivKey
21
22 - pub := nsys.NewPublisher(n.DAG, n.Routing)
22 + pub := nsys.NewRoutingPublisher(n.Routing)
23 err := pub.Publish(k, args[0])
24 if err != nil {
25 return err
core/core.go
+3 -8
@@ -63,10 +63,7 @@ type IpfsNode struct {
63 Resolver *path.Resolver
64
65 // the name system, resolves paths to hashes
66 - Namesys namesys.Resolver
67 -
68 - // the routing publisher
69 - Publisher namesys.Publisher
66 + Namesys namesys.NameSystem
67 }
68
69 // NewIpfsNode constructs a new IpfsNode based on the given config.
@@ -148,8 +145,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
145 }
146
147 dag := &merkledag.DAGService{Blocks: bs}
151 - resolve := namesys.NewMasterResolver(route, dag)
152 - publisher := namesys.NewPublisher(dag, route)
148 + ns := namesys.NewNameSystem(route)
149
150 success = true
151 return &IpfsNode{
@@ -162,8 +158,7 @@ func NewIpfsNode(cfg *config.Config, online bool) (*IpfsNode, error) {
158 Exchange: exchangeSession,
159 Identity: local,
160 Routing: route,
165 - Namesys: resolve,
166 - Publisher: publisher,
161 + Namesys: ns,
162 }, nil
163 }
164
core/mock.go
+1 -4
@@ -43,10 +43,7 @@ func NewMockNode() (*IpfsNode, error) {
43 nd.DAG = &mdag.DAGService{bserv}
44
45 // Namespace resolver
46 - nd.Namesys = nsys.NewMasterResolver(dht, nd.DAG)
47 -
48 - // Publisher
49 - nd.Publisher = nsys.NewPublisher(nd.DAG, dht)
46 + nd.Namesys = nsys.NewNameSystem(dht)
47
48 // Path resolver
49 nd.Resolver = &path.Resolver{nd.DAG}
fuse/ipns/ipns_unix.go
+1 -1
@@ -397,7 +397,7 @@ func (n *Node) republishRoot() error {
397 }
398 log.Debug("Publishing changes!")
399
400 - err = n.Ipfs.Publisher.Publish(root.key, ndkey.Pretty())
400 + err = n.Ipfs.Namesys.Publish(root.key, ndkey.Pretty())
401 if err != nil {
402 log.Error("ipns: Publish Failed: %s", err)
403 return err
namesys/dns.go
+4 -3
@@ -4,9 +4,10 @@ import (
4 "net"
5
6 b58 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-base58"
7 + isd "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-is-domain"
8 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
9 +
10 u "github.com/jbenet/go-ipfs/util"
9 - isd "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-is-domain"
11 )
12
13 // DNSResolver implements a Resolver on DNS domains
@@ -15,8 +16,8 @@ type DNSResolver struct {
16 // cache would need a timeout
17 }
18
18 -// Matches implements Resolver
19 -func (r *DNSResolver) Matches(name string) bool {
19 +// CanResolve implements Resolver
20 +func (r *DNSResolver) CanResolve(name string) bool {
21 return isd.IsDomain(name)
22 }
23
namesys/interface.go new
+43
@@ -0,0 +1,43 @@
1 +package namesys
2 +
3 +import (
4 + "errors"
5 +
6 + ci "github.com/jbenet/go-ipfs/crypto"
7 +)
8 +
9 +// ErrResolveFailed signals an error when attempting to resolve.
10 +var ErrResolveFailed = errors.New("could not resolve name.")
11 +
12 +// ErrPublishFailed signals an error when attempting to publish.
13 +var ErrPublishFailed = errors.New("could not publish name.")
14 +
15 +// Namesys represents a cohesive name publishing and resolving system.
16 +//
17 +// Publishing a name is the process of establishing a mapping, a key-value
18 +// pair, according to naming rules and databases.
19 +//
20 +// Resolving a name is the process of looking up the value associated with the
21 +// key (name).
22 +type NameSystem interface {
23 + Resolver
24 + Publisher
25 +}
26 +
27 +// Resolver is an object capable of resolving names.
28 +type Resolver interface {
29 +
30 + // Resolve looks up a name, and returns the value previously published.
31 + Resolve(name string) (value string, err error)
32 +
33 + // CanResolve checks whether this Resolver can resolve a name
34 + CanResolve(name string) bool
35 +}
36 +
37 +// Publisher is an object capable of publishing particular names.
38 +type Publisher interface {
39 +
40 + // Publish establishes a name-value mapping.
41 + // TODO make this not PrivKey specific.
42 + Publish(name ci.PrivKey, value string) error
43 +}
namesys/namesys.go new
+57
@@ -0,0 +1,57 @@
1 +package namesys
2 +
3 +import (
4 + ci "github.com/jbenet/go-ipfs/crypto"
5 + routing "github.com/jbenet/go-ipfs/routing"
6 +)
7 +
8 +// ipnsNameSystem implements IPNS naming.
9 +//
10 +// Uses three Resolvers:
11 +// (a) ipfs routing naming: SFS-like PKI names.
12 +// (b) dns domains: resolves using links in DNS TXT records
13 +// (c) proquints: interprets string as the raw byte data.
14 +//
15 +// It can only publish to: (a) ipfs routing naming.
16 +//
17 +type ipns struct {
18 + resolvers []Resolver
19 + publisher Publisher
20 +}
21 +
22 +// NewNameSystem will construct the IPFS naming system based on Routing
23 +func NewNameSystem(r routing.IpfsRouting) NameSystem {
24 + return &ipns{
25 + resolvers: []Resolver{
26 + new(DNSResolver),
27 + new(ProquintResolver),
28 + NewRoutingResolver(r),
29 + },
30 + publisher: NewRoutingPublisher(r),
31 + }
32 +}
33 +
34 +// Resolve implements Resolver
35 +func (ns *ipns) Resolve(name string) (string, error) {
36 + for _, r := range ns.resolvers {
37 + if r.CanResolve(name) {
38 + return r.Resolve(name)
39 + }
40 + }
41 + return "", ErrResolveFailed
42 +}
43 +
44 +// CanResolve implements Resolver
45 +func (ns *ipns) CanResolve(name string) bool {
46 + for _, r := range ns.resolvers {
47 + if r.CanResolve(name) {
48 + return true
49 + }
50 + }
51 + return false
52 +}
53 +
54 +// Publish implements Publisher
55 +func (ns *ipns) Publish(name ci.PrivKey, value string) error {
56 + return ns.publisher.Publish(name, value)
57 +}
namesys/nsresolver.go deleted
-8
@@ -1,8 +0,0 @@
1 -package namesys
2 -
3 -type Resolver interface {
4 - // Resolve returns a base58 encoded string
5 - Resolve(string) (string, error)
6 -
7 - Matches(string) bool
8 -}
namesys/proquint.go
+4 -2
@@ -8,13 +8,15 @@ import (
8
9 type ProquintResolver struct{}
10
11 -func (r *ProquintResolver) Matches(name string) bool {
11 +// CanResolve implements Resolver. Checks whether the name is a proquint string.
12 +func (r *ProquintResolver) CanResolve(name string) bool {
13 ok, err := proquint.IsProquint(name)
14 return err == nil && ok
15 }
16
17 +// Resolve implements Resolver. Decodes the proquint string.
18 func (r *ProquintResolver) Resolve(name string) (string, error) {
17 - ok := r.Matches(name)
19 + ok := r.CanResolve(name)
20 if !ok {
21 return "", errors.New("not a valid proquint string")
22 }
namesys/publisher.go
+8 -14
@@ -7,32 +7,26 @@ import (
7 "github.com/jbenet/go-ipfs/Godeps/_workspace/src/code.google.com/p/goprotobuf/proto"
8
9 ci "github.com/jbenet/go-ipfs/crypto"
10 - mdag "github.com/jbenet/go-ipfs/merkledag"
10 routing "github.com/jbenet/go-ipfs/routing"
11 u "github.com/jbenet/go-ipfs/util"
12 )
13
14 +// ipnsPublisher is capable of publishing and resolving names to the IPFS
15 +// routing system.
16 type ipnsPublisher struct {
16 - dag *mdag.DAGService
17 routing routing.IpfsRouting
18 }
19
20 -type Publisher interface {
21 - Publish(ci.PrivKey, string) error
20 +// NewRoutingPublisher constructs a publisher for the IPFS Routing name system.
21 +func NewRoutingPublisher(route routing.IpfsRouting) Publisher {
22 + return &ipnsPublisher{routing: route}
23 }
24
24 -func NewPublisher(dag *mdag.DAGService, route routing.IpfsRouting) Publisher {
25 - return &ipnsPublisher{
26 - dag: dag,
27 - routing: route,
28 - }
29 -}
30 -
31 -// Publish accepts a keypair and a value,
25 +// Publish implements Publisher. Accepts a keypair and a value,
26 func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error {
27 log.Debug("namesys: Publish %s", value)
28 ctx := context.TODO()
35 - data, err := CreateEntryData(k, value)
29 + data, err := createRoutingEntryData(k, value)
30 if err != nil {
31 return err
32 }
@@ -63,7 +57,7 @@ func (p *ipnsPublisher) Publish(k ci.PrivKey, value string) error {
57 return nil
58 }
59
66 -func CreateEntryData(pk ci.PrivKey, val string) ([]byte, error) {
60 +func createRoutingEntryData(pk ci.PrivKey, val string) ([]byte, error) {
61 entry := new(IpnsEntry)
62 sig, err := pk.Sign([]byte(val))
63 if err != nil {
namesys/resolve_test.go
+4 -17
@@ -4,9 +4,7 @@ import (
4 "testing"
5
6 ds "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/datastore.go"
7 - bs "github.com/jbenet/go-ipfs/blockservice"
7 ci "github.com/jbenet/go-ipfs/crypto"
9 - mdag "github.com/jbenet/go-ipfs/merkledag"
8 "github.com/jbenet/go-ipfs/peer"
9 mock "github.com/jbenet/go-ipfs/routing/mock"
10 u "github.com/jbenet/go-ipfs/util"
@@ -19,26 +17,15 @@ func TestRoutingResolve(t *testing.T) {
17 lds := ds.NewMapDatastore()
18 d := mock.NewMockRouter(local, lds)
19
22 - bserv, err := bs.NewBlockService(lds, nil)
23 - if err != nil {
24 - t.Fatal(err)
25 - }
26 -
27 - dag := &mdag.DAGService{Blocks: bserv}
28 -
29 - resolve := NewMasterResolver(d, dag)
30 -
31 - pub := ipnsPublisher{
32 - dag: dag,
33 - routing: d,
34 - }
20 + resolver := NewRoutingResolver(d)
21 + publisher := NewRoutingPublisher(d)
22
23 privk, pubk, err := ci.GenerateKeyPair(ci.RSA, 512)
24 if err != nil {
25 t.Fatal(err)
26 }
27
41 - err = pub.Publish(privk, "Hello")
28 + err = publisher.Publish(privk, "Hello")
29 if err != nil {
30 t.Fatal(err)
31 }
@@ -49,7 +36,7 @@ func TestRoutingResolve(t *testing.T) {
36 }
37
38 pkhash := u.Hash(pubkb)
52 - res, err := resolve.Resolve(u.Key(pkhash).Pretty())
39 + res, err := resolver.Resolve(u.Key(pkhash).Pretty())
40 if err != nil {
41 t.Fatal(err)
42 }
namesys/resolver.go deleted
-42
@@ -1,42 +0,0 @@
1 -package namesys
2 -
3 -import (
4 - "errors"
5 -
6 - mdag "github.com/jbenet/go-ipfs/merkledag"
7 - "github.com/jbenet/go-ipfs/routing"
8 -)
9 -
10 -var ErrCouldntResolve = errors.New("could not resolve name.")
11 -
12 -type masterResolver struct {
13 - res []Resolver
14 -}
15 -
16 -func NewMasterResolver(r routing.IpfsRouting, dag *mdag.DAGService) Resolver {
17 - mr := new(masterResolver)
18 - mr.res = []Resolver{
19 - new(DNSResolver),
20 - new(ProquintResolver),
21 - NewRoutingResolver(r, dag),
22 - }
23 - return mr
24 -}
25 -
26 -func (mr *masterResolver) Resolve(name string) (string, error) {
27 - for _, r := range mr.res {
28 - if r.Matches(name) {
29 - return r.Resolve(name)
30 - }
31 - }
32 - return "", ErrCouldntResolve
33 -}
34 -
35 -func (mr *masterResolver) Matches(name string) bool {
36 - for _, r := range mr.res {
37 - if r.Matches(name) {
38 - return true
39 - }
40 - }
41 - return false
42 -}
namesys/routing.go
+12 -12
@@ -8,32 +8,32 @@ import (
8
9 mh "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-multihash"
10 ci "github.com/jbenet/go-ipfs/crypto"
11 - mdag "github.com/jbenet/go-ipfs/merkledag"
12 - "github.com/jbenet/go-ipfs/routing"
11 + routing "github.com/jbenet/go-ipfs/routing"
12 u "github.com/jbenet/go-ipfs/util"
13 )
14
15 var log = u.Logger("namesys")
16
18 -// RoutingResolver implements NSResolver for the main IPFS SFS-like naming
19 -type RoutingResolver struct {
17 +// routingResolver implements NSResolver for the main IPFS SFS-like naming
18 +type routingResolver struct {
19 routing routing.IpfsRouting
21 - dag *mdag.DAGService
20 }
21
24 -func NewRoutingResolver(route routing.IpfsRouting, dagservice *mdag.DAGService) *RoutingResolver {
25 - return &RoutingResolver{
26 - routing: route,
27 - dag: dagservice,
28 - }
22 +// NewRoutingResolver constructs a name resolver using the IPFS Routing system
23 +// to implement SFS-like naming on top.
24 +func NewRoutingResolver(route routing.IpfsRouting) Resolver {
25 + return &routingResolver{routing: route}
26 }
27
31 -func (r *RoutingResolver) Matches(name string) bool {
28 +// CanResolve implements Resolver. Checks whether name is a b58 encoded string.
29 +func (r *routingResolver) CanResolve(name string) bool {
30 _, err := mh.FromB58String(name)
31 return err == nil
32 }
33
36 -func (r *RoutingResolver) Resolve(name string) (string, error) {
34 +// Resolve implements Resolver. Uses the IPFS routing system to resolve SFS-like
35 +// names.
36 +func (r *routingResolver) Resolve(name string) (string, error) {
37 log.Debug("RoutingResolve: '%s'", name)
38 ctx := context.TODO()
39 hash, err := mh.FromB58String(name)