Skip to content

Commit

Permalink
feat(test): Add ipfs example
Browse files Browse the repository at this point in the history
  • Loading branch information
gfanton committed Jun 12, 2019
1 parent 531c1ba commit 1e09332
Show file tree
Hide file tree
Showing 5 changed files with 815 additions and 25 deletions.
150 changes: 150 additions & 0 deletions example/example_log_append_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
package example

import (
"context"
"fmt"
"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/log"
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"
ipfs_libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
ipfs_repo "github.com/ipfs/go-ipfs/repo"
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"
)

func buildHostOverrideExample(ctx context.Context, id peer.ID, ps pstore.Peerstore, options ...libp2p.Option) (host.Host, error) {
return ipfs_libp2p.DefaultHostOption(ctx, id, ps, options...)
}

func newRepo() (ipfs_repo.Repo, error) {
// Generating config
cfg, err := config.Init(ioutil.Discard, 2048)
if err != nil {
return nil, err
}

// Listen on local interface only
cfg.Addresses.Swarm = []string{
"/ip4/127.0.0.1/tcp/0",
}

// Do not bootstrap on ipfs node
cfg.Bootstrap = []string{}

return &ipfs_repo.Mock{
D: dssync.MutexWrap(datastore.NewMapDatastore()),
C: *cfg,
}, nil
}

func buildNode(ctx context.Context) (*ipfs_core.IpfsNode, error) {
r, err := newRepo()
if err != nil {
return nil, err
}

cfg := &ipfs_core.BuildCfg{
Online: true,
Repo: r,
Host: buildHostOverrideExample,
}

return ipfs_core.NewNode(ctx, cfg)
}

func ExampleLogAppend() {
ctx := context.Background()

// Build Ipfs Node A
nodeA, err := buildNode(ctx)
if err != nil {
panic(err)
}

// Build Ipfs Node B
nodeB, err := buildNode(ctx)
if err != nil {
panic(err)
}

nodeBInfo := pstore.PeerInfo{
ID: nodeB.Identity,
Addrs: nodeB.PeerHost.Addrs(),
}

// Connecting NodeA with NodeB
if err := nodeA.PeerHost.Connect(ctx, nodeBInfo); err != nil {
panic(fmt.Errorf("connect error: %s", err))
}

mdsA := datastore.NewMapDatastore()
serviceA := log_io.FromIpfsNode(nodeA, mdsA)

mdsB := datastore.NewMapDatastore()
serviceB := log_io.FromIpfsNode(nodeB, mdsB)

// Fill up datastore with identities
ds := dssync.MutexWrap(datastore.NewMapDatastore())
ks, err := keystore.NewKeystore(ds)
if err != nil {
panic(err)
}

// Create identity A
identityA, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
Keystore: ks,
ID: "userA",
Type: "orbitdb",
})

if err != nil {
panic(err)
}

// Create identity B
identityB, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
Keystore: ks,
ID: "userB",
Type: "orbitdb",
})

if err != nil {
panic(err)
}

// creating log
logA, err := log.NewLog(serviceA, identityA, &log.NewLogOptions{ID: "A"})
if err != nil {
panic(err)
}

// nodeA Append data (hello world)"
_, err = logA.Append([]byte("hello world"), 1)
if err != nil {
panic(fmt.Errorf("append error: %s", err))
}

h, err := logA.ToMultihash()
if err != nil {
panic(fmt.Errorf("ToMultihash error: %s", err))
}

res, err := log.NewFromMultihash(serviceB, identityB, h, &log.NewLogOptions{}, &log.FetchOptions{})
if err != nil {
panic(fmt.Errorf("NewFromMultihash error: %s", err))
}

// nodeB lookup logA
fmt.Println(res.ToString(nil))

// Output: hello world
}
83 changes: 78 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,93 @@ module berty.tech/go-ipfs-log
go 1.12

require (
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32
cloud.google.com/go v0.40.0 // indirect
github.com/OpenPeeDeeP/depguard v0.0.0-20181229194401-1f388ab2d810 // indirect
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c
github.com/coreos/bbolt v1.3.3 // indirect
github.com/coreos/etcd v3.3.13+incompatible // indirect
github.com/go-critic/go-critic v0.3.4 // indirect
github.com/go-ole/go-ole v1.2.4 // indirect
github.com/go-toolsmith/astcast v1.0.0 // indirect
github.com/go-toolsmith/astcopy v1.0.0 // indirect
github.com/go-toolsmith/astfmt v1.0.0 // indirect
github.com/go-toolsmith/astp v1.0.0 // indirect
github.com/go-toolsmith/pkgload v1.0.0 // indirect
github.com/go-toolsmith/typep v1.0.0 // indirect
github.com/golang/mock v1.3.1 // indirect
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 // indirect
github.com/golangci/go-tools v0.0.0-20190124090046-35a9f45a5db0 // indirect
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d // indirect
github.com/golangci/gofmt v0.0.0-20181222123516-0b8337e80d98 // indirect
github.com/golangci/golangci-lint v1.17.1 // indirect
github.com/golangci/gosec v0.0.0-20180901114220-8afd9cbb6cfb // indirect
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219 // indirect
github.com/golangci/revgrep v0.0.0-20180812185044-276a5c0a1039 // indirect
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
github.com/gostaticanalysis/analysisutil v0.0.0-20190329151158-56bca42c7635 // indirect
github.com/hashicorp/golang-lru v0.5.1
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0
github.com/ipfs/go-blockservice v0.0.3
github.com/ipfs/go-cid v0.0.1
github.com/ipfs/go-cid v0.0.2
github.com/ipfs/go-datastore v0.0.5
github.com/ipfs/go-ipfs v0.4.20
github.com/ipfs/go-ipfs v0.4.21
github.com/ipfs/go-ipfs-blockstore v0.0.1
github.com/ipfs/go-ipfs-cmdkit v0.0.1 // indirect
github.com/ipfs/go-ipfs-config v0.0.3
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
github.com/ipfs/go-ipld-cbor v0.0.1
github.com/ipfs/go-ipld-format v0.0.1
github.com/ipfs/go-ipld-cbor v0.0.2
github.com/ipfs/go-ipld-format v0.0.2
github.com/ipfs/go-merkledag v0.0.3
github.com/klauspost/compress v1.7.0 // indirect
github.com/klauspost/cpuid v1.2.1 // indirect
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
github.com/kr/pty v1.1.4 // indirect
github.com/libp2p/go-libp2p v0.0.28
github.com/libp2p/go-libp2p-core v0.0.3
github.com/libp2p/go-libp2p-crypto v0.0.2
github.com/libp2p/go-libp2p-host v0.0.3
github.com/libp2p/go-libp2p-peer v0.1.1
github.com/libp2p/go-libp2p-peerstore v0.0.6
github.com/libp2p/go-libp2p-record v0.0.1
github.com/libp2p/go-libp2p-testing v0.0.4 // indirect
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946 // indirect
github.com/magiconair/properties v1.8.1 // indirect
github.com/mattn/go-colorable v0.1.2 // indirect
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d // indirect
github.com/pelletier/go-toml v1.4.0 // indirect
github.com/pkg/errors v0.8.1
github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992
github.com/prometheus/client_golang v0.9.4 // indirect
github.com/rogpeppe/fastuuid v1.1.0 // indirect
github.com/russross/blackfriday v2.0.0+incompatible // indirect
github.com/shirou/gopsutil v2.18.12+incompatible // indirect
github.com/shurcooL/go v0.0.0-20190330031554-6713ea532688 // indirect
github.com/sirupsen/logrus v1.4.2 // indirect
github.com/smartystreets/assertions v0.0.0-20190401211740-f487f9de1cd3 // indirect
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa
github.com/spf13/afero v1.2.2 // indirect
github.com/spf13/cobra v0.0.5 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.4.0 // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/ugorji/go v1.1.5-pre // indirect
github.com/valyala/fasthttp v1.3.0 // indirect
github.com/whyrusleeping/yamux v1.2.0 // indirect
go.etcd.io/bbolt v1.3.3 // indirect
go.opencensus.io v0.22.0 // indirect
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522 // indirect
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff // indirect
golang.org/x/mobile v0.0.0-20190607214518-6fa95d984e88 // indirect
golang.org/x/mod v0.1.0 // indirect
golang.org/x/net v0.0.0-20190611141213-3f473d35a33a // indirect
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae // indirect
golang.org/x/tools v0.0.0-20190611150145-4bfb4c74ac90 // indirect
google.golang.org/appengine v1.6.1 // indirect
google.golang.org/genproto v0.0.0-20190605220351-eb0b1bdb6ae6 // indirect
google.golang.org/grpc v1.21.1 // indirect
honnef.co/go/tools v0.0.0-20190607181801-497c8f037f5a // indirect
mvdan.cc/unparam v0.0.0-20190310220240-1b9ccfa71afe // indirect
sourcegraph.com/sqs/pbtypes v1.0.0 // indirect
)
Loading

0 comments on commit 1e09332

Please sign in to comment.