Skip to content

Commit

Permalink
chore: now using an interface for ipfs services
Browse files Browse the repository at this point in the history
  • Loading branch information
glouvigny committed Jun 14, 2019
1 parent 2c0b3c2 commit c6d0f8a
Show file tree
Hide file tree
Showing 20 changed files with 137 additions and 121 deletions.
8 changes: 4 additions & 4 deletions entry/entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func init() {
cbornode.RegisterCborType(AtlasEntry)
}

func CreateEntry(ipfsInstance *io.IpfsServices, identity *identityprovider.Identity, data *Entry, clock *lamportclock.LamportClock) (*Entry, error) {
func CreateEntry(ipfsInstance io.IpfsServices, identity *identityprovider.Identity, data *Entry, clock *lamportclock.LamportClock) (*Entry, error) {
if ipfsInstance == nil {
return nil, errors.New("ipfs instance not defined")
}
Expand Down Expand Up @@ -179,7 +179,7 @@ func CreateEntry(ipfsInstance *io.IpfsServices, identity *identityprovider.Ident
}

ctx, _ := context.WithTimeout(context.Background(), time.Second*5)
err = ipfsInstance.DAG.Add(ctx, nd)
err = ipfsInstance.Dag().Add(ctx, nd)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -299,7 +299,7 @@ func Verify(identity identityprovider.Interface, entry *Entry) error {
return nil
}

func ToMultihash(ipfsInstance *io.IpfsServices, entry *Entry) (cid.Cid, error) {
func ToMultihash(ipfsInstance io.IpfsServices, entry *Entry) (cid.Cid, error) {
if entry == nil {
return cid.Cid{}, errors.New("entry is not defined")
}
Expand Down Expand Up @@ -334,7 +334,7 @@ func ToMultihash(ipfsInstance *io.IpfsServices, entry *Entry) (cid.Cid, error) {
return entryCID, err
}

func FromMultihash(ipfs *io.IpfsServices, hash cid.Cid, provider identityprovider.Interface) (*Entry, error) {
func FromMultihash(ipfs io.IpfsServices, hash cid.Cid, provider identityprovider.Interface) (*Entry, error) {
if ipfs == nil {
return nil, errors.New("ipfs instance not defined")
}
Expand Down
4 changes: 2 additions & 2 deletions entry/entry_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type FetchOptions struct {
Provider identityprovider.Interface
}

func FetchParallel(ipfs *io.IpfsServices, hashes []cid.Cid, options *FetchOptions) []*Entry {
func FetchParallel(ipfs io.IpfsServices, hashes []cid.Cid, options *FetchOptions) []*Entry {
var entries []*Entry

for _, h := range hashes {
Expand All @@ -32,7 +32,7 @@ func FetchParallel(ipfs *io.IpfsServices, hashes []cid.Cid, options *FetchOption
return NewOrderedMapFromEntries(entries).Slice()
}

func FetchAll(ipfs *io.IpfsServices, hashes []cid.Cid, options *FetchOptions) []*Entry {
func FetchAll(ipfs io.IpfsServices, hashes []cid.Cid, options *FetchOptions) []*Entry {
result := []*Entry{}
cache := NewOrderedMap()
loadingQueue := append(hashes[:0:0], hashes...)
Expand Down
20 changes: 12 additions & 8 deletions example/example_log_append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"io/ioutil"

idp "berty.tech/go-ipfs-log/identityprovider"
log_io "berty.tech/go-ipfs-log/io"
keystore "berty.tech/go-ipfs-log/keystore"
"berty.tech/go-ipfs-log/keystore"
"berty.tech/go-ipfs-log/log"
datastore "github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
config "github.com/ipfs/go-ipfs-config"
ipfs_core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/core/coreapi"
ipfs_libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
ipfs_repo "github.com/ipfs/go-ipfs/repo"
libp2p "github.com/libp2p/go-libp2p"
"github.com/libp2p/go-libp2p"
host "github.com/libp2p/go-libp2p-host"
peer "github.com/libp2p/go-libp2p-peer"
pstore "github.com/libp2p/go-libp2p-peerstore"
Expand Down Expand Up @@ -86,11 +86,15 @@ func Example_logAppend() {
panic(fmt.Errorf("connect error: %s", err))
}

mdsA := datastore.NewMapDatastore()
serviceA := log_io.FromIpfsNode(nodeA, mdsA)
serviceA, err := coreapi.NewCoreAPI(nodeA)
if err != nil {
panic(fmt.Errorf("coreapi error: %s", err))
}

mdsB := datastore.NewMapDatastore()
serviceB := log_io.FromIpfsNode(nodeB, mdsB)
serviceB, err := coreapi.NewCoreAPI(nodeB)
if err != nil {
panic(fmt.Errorf("coreapi error: %s", err))
}

// Fill up datastore with identities
ds := dssync.MutexWrap(datastore.NewMapDatastore())
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ require (
golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5 // indirect
golang.org/x/net v0.0.0-20190611141213-3f473d35a33a // indirect
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae // indirect
google.golang.org/appengine v1.4.0 // indirect
)
8 changes: 4 additions & 4 deletions io/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func SetDebug(val bool) {
debug = val
}

func WriteCBOR(ipfs *IpfsServices, obj interface{}) (cid.Cid, error) {
func WriteCBOR(ipfs IpfsServices, obj interface{}) (cid.Cid, error) {
cborNode, err := cbornode.WrapObject(obj, math.MaxUint64, -1)
if err != nil {
return cid.Cid{}, err
Expand All @@ -26,14 +26,14 @@ func WriteCBOR(ipfs *IpfsServices, obj interface{}) (cid.Cid, error) {
fmt.Printf("\nStr of cbor: %x\n", cborNode.RawData())
}

err = ipfs.DAG.Add(context.Background(), cborNode)
err = ipfs.Dag().Add(context.Background(), cborNode)
if err != nil {
return cid.Cid{}, err
}

return cborNode.Cid(), nil
}

func ReadCBOR(ipfs *IpfsServices, contentIdentifier cid.Cid) (format.Node, error) {
return ipfs.DAG.Get(context.Background(), contentIdentifier)
func ReadCBOR(ipfs IpfsServices, contentIdentifier cid.Cid) (format.Node, error) {
return ipfs.Dag().Get(context.Background(), contentIdentifier)
}
26 changes: 0 additions & 26 deletions io/io_test.go

This file was deleted.

47 changes: 3 additions & 44 deletions io/service.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,9 @@
package io // import "berty.tech/go-ipfs-log/io"

import (
bserv "github.com/ipfs/go-blockservice"
datastore "github.com/ipfs/go-datastore"
dssync "github.com/ipfs/go-datastore/sync"
bstore "github.com/ipfs/go-ipfs-blockstore"
offline "github.com/ipfs/go-ipfs-exchange-offline"
ipfs_core "github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/pin"
ipld "github.com/ipfs/go-ipld-format"
merkledag "github.com/ipfs/go-merkledag"
core_iface "github.com/ipfs/interface-go-ipfs-core"
)

type IpfsServices struct {
DAG ipld.DAGService
BlockStore bstore.Blockstore
DB datastore.Datastore
Blockserv bserv.BlockService
Pinner pin.Pinner
}

func NewMemoryServices() *IpfsServices {
dataStore := datastore.NewMapDatastore()
db := dssync.MutexWrap(dataStore)
bs := bstore.NewBlockstore(db)
blockserv := bserv.New(bs, offline.Exchange(bs))
dag := merkledag.NewDAGService(blockserv)
pinner := pin.NewPinner(db, dag, dag)

// var pinning pin.Pinner = pin.NewPinner()
// var blockstore bstore.GCBlockstore = bstore.NewBlockstore()
return &IpfsServices{
DAG: dag,
BlockStore: bs,
DB: db,
Blockserv: blockserv,
Pinner: pinner,
}
}

func FromIpfsNode(node *ipfs_core.IpfsNode, ds datastore.Datastore) *IpfsServices {
return &IpfsServices{
DAG: node.DAG,
BlockStore: node.Blockstore,
DB: ds,
Blockserv: node.Blocks,
Pinner: node.Pinning,
}
type IpfsServices interface {
Dag() core_iface.APIDagService
}
12 changes: 6 additions & 6 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type JSONLog struct {
}

type Log struct {
Storage *io.IpfsServices
Storage io.IpfsServices
ID string
AccessController accesscontroller.Interface
SortFn func(a *entry.Entry, b *entry.Entry) (int, error)
Expand Down Expand Up @@ -71,7 +71,7 @@ func maxClockTimeForEntries(entries []*entry.Entry, defValue int) int {
return max
}

func NewLog(services *io.IpfsServices, identity *identityprovider.Identity, options *NewLogOptions) (*Log, error) {
func NewLog(services io.IpfsServices, identity *identityprovider.Identity, options *NewLogOptions) (*Log, error) {
if services == nil {
return nil, errmsg.IPFSNotDefined
}
Expand Down Expand Up @@ -475,7 +475,7 @@ func (l *Log) ToMultihash() (cid.Cid, error) {
return ToMultihash(l.Storage, l)
}

func NewFromMultihash(services *io.IpfsServices, identity *identityprovider.Identity, hash cid.Cid, logOptions *NewLogOptions, fetchOptions *FetchOptions) (*Log, error) {
func NewFromMultihash(services io.IpfsServices, identity *identityprovider.Identity, hash cid.Cid, logOptions *NewLogOptions, fetchOptions *FetchOptions) (*Log, error) {
if services == nil {
return nil, errmsg.IPFSNotDefined
}
Expand Down Expand Up @@ -522,7 +522,7 @@ func NewFromMultihash(services *io.IpfsServices, identity *identityprovider.Iden
})
}

func NewFromEntryHash(services *io.IpfsServices, identity *identityprovider.Identity, hash cid.Cid, logOptions *NewLogOptions, fetchOptions *FetchOptions) (*Log, error) {
func NewFromEntryHash(services io.IpfsServices, identity *identityprovider.Identity, hash cid.Cid, logOptions *NewLogOptions, fetchOptions *FetchOptions) (*Log, error) {
if logOptions == nil {
return nil, errmsg.LogOptionsNotDefined
}
Expand All @@ -549,7 +549,7 @@ func NewFromEntryHash(services *io.IpfsServices, identity *identityprovider.Iden
})
}

func NewFromJSON(services *io.IpfsServices, identity *identityprovider.Identity, jsonLog *JSONLog, logOptions *NewLogOptions, fetchOptions *entry.FetchOptions) (*Log, error) {
func NewFromJSON(services io.IpfsServices, identity *identityprovider.Identity, jsonLog *JSONLog, logOptions *NewLogOptions, fetchOptions *entry.FetchOptions) (*Log, error) {
if logOptions == nil {
return nil, errmsg.LogOptionsNotDefined
}
Expand Down Expand Up @@ -577,7 +577,7 @@ func NewFromJSON(services *io.IpfsServices, identity *identityprovider.Identity,
})
}

func NewFromEntry(services *io.IpfsServices, identity *identityprovider.Identity, sourceEntries []*entry.Entry, logOptions *NewLogOptions, fetchOptions *entry.FetchOptions) (*Log, error) {
func NewFromEntry(services io.IpfsServices, identity *identityprovider.Identity, sourceEntries []*entry.Entry, logOptions *NewLogOptions, fetchOptions *entry.FetchOptions) (*Log, error) {
if logOptions == nil {
return nil, errmsg.LogOptionsNotDefined
}
Expand Down
10 changes: 5 additions & 5 deletions log/log_io.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ type FetchOptions struct {
Timeout time.Duration
}

func ToMultihash(services *io.IpfsServices, log *Log) (cid.Cid, error) {
func ToMultihash(services io.IpfsServices, log *Log) (cid.Cid, error) {
if log.Values().Len() < 1 {
return cid.Cid{}, errors.New(`Can't serialize an empty log`)
}

return io.WriteCBOR(services, log.ToJSON())
}

func FromMultihash(services *io.IpfsServices, hash cid.Cid, options *FetchOptions) (*Snapshot, error) {
func FromMultihash(services io.IpfsServices, hash cid.Cid, options *FetchOptions) (*Snapshot, error) {
result, err := io.ReadCBOR(services, hash)
if err != nil {
return nil, err
Expand Down Expand Up @@ -77,7 +77,7 @@ func FromMultihash(services *io.IpfsServices, hash cid.Cid, options *FetchOption
}, nil
}

func FromEntryHash(services *io.IpfsServices, hashes []cid.Cid, options *FetchOptions) ([]*entry.Entry, error) {
func FromEntryHash(services io.IpfsServices, hashes []cid.Cid, options *FetchOptions) ([]*entry.Entry, error) {
if services == nil {
return nil, errmsg.IPFSNotDefined
}
Expand Down Expand Up @@ -106,7 +106,7 @@ func FromEntryHash(services *io.IpfsServices, hashes []cid.Cid, options *FetchOp
return sliced, nil
}

func FromJSON(services *io.IpfsServices, jsonLog *JSONLog, options *entry.FetchOptions) (*Snapshot, error) {
func FromJSON(services io.IpfsServices, jsonLog *JSONLog, options *entry.FetchOptions) (*Snapshot, error) {
if services == nil {
return nil, errmsg.IPFSNotDefined
}
Expand All @@ -132,7 +132,7 @@ func FromJSON(services *io.IpfsServices, jsonLog *JSONLog, options *entry.FetchO
}, nil
}

func FromEntry(services *io.IpfsServices, sourceEntries []*entry.Entry, options *entry.FetchOptions) (*Snapshot, error) {
func FromEntry(services io.IpfsServices, sourceEntries []*entry.Entry, options *entry.FetchOptions) (*Snapshot, error) {
if services == nil {
return nil, errmsg.IPFSNotDefined
}
Expand Down
3 changes: 1 addition & 2 deletions test/entry_io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (

"berty.tech/go-ipfs-log/entry"
idp "berty.tech/go-ipfs-log/identityprovider"
"berty.tech/go-ipfs-log/io"
ks "berty.tech/go-ipfs-log/keystore"
"berty.tech/go-ipfs-log/log"
cid "github.com/ipfs/go-cid"
Expand All @@ -22,7 +21,7 @@ func TestEntryPersistence(t *testing.T) {
_, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

ipfs := io.NewMemoryServices()
ipfs := NewMemoryServices()

datastore := dssync.MutexWrap(NewIdentityDataStore())
keystore, err := ks.NewKeystore(datastore)
Expand Down
3 changes: 1 addition & 2 deletions test/entry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"berty.tech/go-ipfs-log/entry"
idp "berty.tech/go-ipfs-log/identityprovider"
"berty.tech/go-ipfs-log/io"
ks "berty.tech/go-ipfs-log/keystore"
cid "github.com/ipfs/go-cid"
dssync "github.com/ipfs/go-datastore/sync"
Expand All @@ -20,7 +19,7 @@ func TestEntry(t *testing.T) {
_, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

ipfs := io.NewMemoryServices()
ipfs := NewMemoryServices()

datastore := dssync.MutexWrap(NewIdentityDataStore())
keystore, err := ks.NewKeystore(datastore)
Expand Down
5 changes: 2 additions & 3 deletions test/log_append_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
"time"

idp "berty.tech/go-ipfs-log/identityprovider"
log_io "berty.tech/go-ipfs-log/io"
keystore "berty.tech/go-ipfs-log/keystore"
"berty.tech/go-ipfs-log/keystore"
"berty.tech/go-ipfs-log/log"
dssync "github.com/ipfs/go-datastore/sync"

Expand All @@ -19,7 +18,7 @@ func TestLogAppend(t *testing.T) {
_, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

ipfs := log_io.NewMemoryServices()
ipfs := NewMemoryServices()

datastore := dssync.MutexWrap(NewIdentityDataStore())
keystore, err := keystore.NewKeystore(datastore)
Expand Down
3 changes: 1 addition & 2 deletions test/log_heads_tails_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"time"

idp "berty.tech/go-ipfs-log/identityprovider"
"berty.tech/go-ipfs-log/io"
ks "berty.tech/go-ipfs-log/keystore"
"berty.tech/go-ipfs-log/log"
ds "github.com/ipfs/go-datastore"
Expand All @@ -20,7 +19,7 @@ func TestLogHeadsTails(t *testing.T) {
_, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

ipfs := io.NewMemoryServices()
ipfs := NewMemoryServices()

datastore := dssync.MutexWrap(ds.NewMapDatastore())
keystore, err := ks.NewKeystore(datastore)
Expand Down
3 changes: 1 addition & 2 deletions test/log_join_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"berty.tech/go-ipfs-log/entry"
"berty.tech/go-ipfs-log/errmsg"
idp "berty.tech/go-ipfs-log/identityprovider"
"berty.tech/go-ipfs-log/io"
ks "berty.tech/go-ipfs-log/keystore"
"berty.tech/go-ipfs-log/log"
"berty.tech/go-ipfs-log/utils/lamportclock"
Expand All @@ -24,7 +23,7 @@ func TestLogJoin(t *testing.T) {
_, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()

ipfs := io.NewMemoryServices()
ipfs := NewMemoryServices()

datastore := dssync.MutexWrap(NewIdentityDataStore())
keystore, err := ks.NewKeystore(datastore)
Expand Down
Loading

0 comments on commit c6d0f8a

Please sign in to comment.