Skip to content

Commit ea99d8a

Browse files
committed
feat(test): Add ipfs example
1 parent 27327c5 commit ea99d8a

File tree

5 files changed

+786
-16
lines changed

5 files changed

+786
-16
lines changed

example/example_log_append_test.go

+150
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
package example
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"io/ioutil"
7+
8+
idp "berty.tech/go-ipfs-log/identityprovider"
9+
log_io "berty.tech/go-ipfs-log/io"
10+
keystore "berty.tech/go-ipfs-log/keystore"
11+
"berty.tech/go-ipfs-log/log"
12+
datastore "github.com/ipfs/go-datastore"
13+
dssync "github.com/ipfs/go-datastore/sync"
14+
config "github.com/ipfs/go-ipfs-config"
15+
ipfs_core "github.com/ipfs/go-ipfs/core"
16+
ipfs_libp2p "github.com/ipfs/go-ipfs/core/node/libp2p"
17+
ipfs_repo "github.com/ipfs/go-ipfs/repo"
18+
libp2p "github.com/libp2p/go-libp2p"
19+
host "github.com/libp2p/go-libp2p-host"
20+
peer "github.com/libp2p/go-libp2p-peer"
21+
pstore "github.com/libp2p/go-libp2p-peerstore"
22+
)
23+
24+
func buildHostOverrideExample(ctx context.Context, id peer.ID, ps pstore.Peerstore, options ...libp2p.Option) (host.Host, error) {
25+
return ipfs_libp2p.DefaultHostOption(ctx, id, ps, options...)
26+
}
27+
28+
func newRepo() (ipfs_repo.Repo, error) {
29+
// Generating config
30+
cfg, err := config.Init(ioutil.Discard, 2048)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
// Listen on local interface only
36+
cfg.Addresses.Swarm = []string{
37+
"/ip4/127.0.0.1/tcp/0",
38+
}
39+
40+
// Do not bootstrap on ipfs node
41+
cfg.Bootstrap = []string{}
42+
43+
return &ipfs_repo.Mock{
44+
D: dssync.MutexWrap(datastore.NewMapDatastore()),
45+
C: *cfg,
46+
}, nil
47+
}
48+
49+
func buildNode(ctx context.Context) (*ipfs_core.IpfsNode, error) {
50+
r, err := newRepo()
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
cfg := &ipfs_core.BuildCfg{
56+
Online: true,
57+
Repo: r,
58+
Host: buildHostOverrideExample,
59+
}
60+
61+
return ipfs_core.NewNode(ctx, cfg)
62+
}
63+
64+
func ExampleLogAppend() {
65+
ctx := context.Background()
66+
67+
// Build Ipfs Node A
68+
nodeA, err := buildNode(ctx)
69+
if err != nil {
70+
panic(err)
71+
}
72+
73+
// Build Ipfs Node B
74+
nodeB, err := buildNode(ctx)
75+
if err != nil {
76+
panic(err)
77+
}
78+
79+
nodeBInfo := pstore.PeerInfo{
80+
ID: nodeB.Identity,
81+
Addrs: nodeB.PeerHost.Addrs(),
82+
}
83+
84+
// Connecting NodeA with NodeB
85+
if err := nodeA.PeerHost.Connect(ctx, nodeBInfo); err != nil {
86+
panic(fmt.Errorf("connect error: %s", err))
87+
}
88+
89+
mdsA := datastore.NewMapDatastore()
90+
serviceA := log_io.FromIpfsNode(nodeA, mdsA)
91+
92+
mdsB := datastore.NewMapDatastore()
93+
serviceB := log_io.FromIpfsNode(nodeB, mdsB)
94+
95+
// Fill up datastore with identities
96+
ds := dssync.MutexWrap(datastore.NewMapDatastore())
97+
ks, err := keystore.NewKeystore(ds)
98+
if err != nil {
99+
panic(err)
100+
}
101+
102+
// Create identity A
103+
identityA, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
104+
Keystore: ks,
105+
ID: "userA",
106+
Type: "orbitdb",
107+
})
108+
109+
if err != nil {
110+
panic(err)
111+
}
112+
113+
// Create identity B
114+
identityB, err := idp.CreateIdentity(&idp.CreateIdentityOptions{
115+
Keystore: ks,
116+
ID: "userB",
117+
Type: "orbitdb",
118+
})
119+
120+
if err != nil {
121+
panic(err)
122+
}
123+
124+
// creating log
125+
logA, err := log.NewLog(serviceA, identityA, &log.NewLogOptions{ID: "A"})
126+
if err != nil {
127+
panic(err)
128+
}
129+
130+
// nodeA Append data (hello world)"
131+
_, err = logA.Append([]byte("hello world"), 1)
132+
if err != nil {
133+
panic(fmt.Errorf("append error: %s", err))
134+
}
135+
136+
h, err := logA.ToMultihash()
137+
if err != nil {
138+
panic(fmt.Errorf("ToMultihash error: %s", err))
139+
}
140+
141+
res, err := log.NewFromMultihash(serviceB, identityB, h, &log.NewLogOptions{}, &log.FetchOptions{})
142+
if err != nil {
143+
panic(fmt.Errorf("NewFromMultihash error: %s", err))
144+
}
145+
146+
// nodeB lookup logA
147+
fmt.Println(res.ToString(nil))
148+
149+
// Output: hello world
150+
}

go.mod

+78-6
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,93 @@ module berty.tech/go-ipfs-log
33
go 1.12
44

55
require (
6-
github.com/berty/go-ipfs-log v0.0.0-20190611082712-13c57636b017
7-
github.com/btcsuite/btcd v0.0.0-20190213025234-306aecffea32
6+
cloud.google.com/go v0.40.0 // indirect
7+
github.com/OpenPeeDeeP/depguard v0.0.0-20181229194401-1f388ab2d810 // indirect
8+
github.com/StackExchange/wmi v0.0.0-20190523213315-cbe66965904d // indirect
9+
github.com/btcsuite/btcd v0.0.0-20190523000118-16327141da8c
10+
github.com/coreos/bbolt v1.3.3 // indirect
11+
github.com/coreos/etcd v3.3.13+incompatible // indirect
12+
github.com/go-critic/go-critic v0.3.4 // indirect
13+
github.com/go-ole/go-ole v1.2.4 // indirect
14+
github.com/go-toolsmith/astcast v1.0.0 // indirect
15+
github.com/go-toolsmith/astcopy v1.0.0 // indirect
16+
github.com/go-toolsmith/astfmt v1.0.0 // indirect
17+
github.com/go-toolsmith/astp v1.0.0 // indirect
18+
github.com/go-toolsmith/pkgload v1.0.0 // indirect
19+
github.com/go-toolsmith/typep v1.0.0 // indirect
20+
github.com/golang/mock v1.3.1 // indirect
21+
github.com/golangci/errcheck v0.0.0-20181223084120-ef45e06d44b6 // indirect
22+
github.com/golangci/go-tools v0.0.0-20190124090046-35a9f45a5db0 // indirect
23+
github.com/golangci/gocyclo v0.0.0-20180528144436-0a533e8fa43d // indirect
24+
github.com/golangci/gofmt v0.0.0-20181222123516-0b8337e80d98 // indirect
25+
github.com/golangci/golangci-lint v1.17.1 // indirect
26+
github.com/golangci/gosec v0.0.0-20180901114220-8afd9cbb6cfb // indirect
27+
github.com/golangci/lint-1 v0.0.0-20181222135242-d2cdd8c08219 // indirect
28+
github.com/golangci/revgrep v0.0.0-20180812185044-276a5c0a1039 // indirect
29+
github.com/google/pprof v0.0.0-20190515194954-54271f7e092f // indirect
30+
github.com/gopherjs/gopherjs v0.0.0-20190430165422-3e4dfb77656c // indirect
31+
github.com/gostaticanalysis/analysisutil v0.0.0-20190329151158-56bca42c7635 // indirect
832
github.com/hashicorp/golang-lru v0.5.1
933
github.com/iancoleman/orderedmap v0.0.0-20190318233801-ac98e3ecb4b0
1034
github.com/ipfs/go-blockservice v0.0.3
11-
github.com/ipfs/go-cid v0.0.1
35+
github.com/ipfs/go-cid v0.0.2
1236
github.com/ipfs/go-datastore v0.0.5
13-
github.com/ipfs/go-ipfs v0.4.20
37+
github.com/ipfs/go-ipfs v0.4.21
1438
github.com/ipfs/go-ipfs-blockstore v0.0.1
39+
github.com/ipfs/go-ipfs-cmdkit v0.0.1 // indirect
40+
github.com/ipfs/go-ipfs-config v0.0.3
1541
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
16-
github.com/ipfs/go-ipld-cbor v0.0.1
17-
github.com/ipfs/go-ipld-format v0.0.1
42+
github.com/ipfs/go-ipld-cbor v0.0.2
43+
github.com/ipfs/go-ipld-format v0.0.2
1844
github.com/ipfs/go-merkledag v0.0.3
45+
github.com/klauspost/compress v1.7.0 // indirect
46+
github.com/klauspost/cpuid v1.2.1 // indirect
47+
github.com/konsorten/go-windows-terminal-sequences v1.0.2 // indirect
48+
github.com/kr/pty v1.1.4 // indirect
49+
github.com/libp2p/go-libp2p v0.0.28
50+
github.com/libp2p/go-libp2p-core v0.0.3
1951
github.com/libp2p/go-libp2p-crypto v0.0.2
52+
github.com/libp2p/go-libp2p-host v0.0.3
53+
github.com/libp2p/go-libp2p-peer v0.1.1
54+
github.com/libp2p/go-libp2p-peerstore v0.0.6
55+
github.com/libp2p/go-libp2p-record v0.0.1
56+
github.com/libp2p/go-libp2p-testing v0.0.4 // indirect
57+
github.com/logrusorgru/aurora v0.0.0-20190428105938-cea283e61946 // indirect
58+
github.com/magiconair/properties v1.8.1 // indirect
59+
github.com/mattn/go-colorable v0.1.2 // indirect
60+
github.com/nbutton23/zxcvbn-go v0.0.0-20180912185939-ae427f1e4c1d // indirect
61+
github.com/pelletier/go-toml v1.4.0 // indirect
2062
github.com/pkg/errors v0.8.1
2163
github.com/polydawn/refmt v0.0.0-20190221155625-df39d6c2d992
64+
github.com/prometheus/client_golang v0.9.4 // indirect
65+
github.com/rogpeppe/fastuuid v1.1.0 // indirect
66+
github.com/russross/blackfriday v2.0.0+incompatible // indirect
67+
github.com/shirou/gopsutil v2.18.12+incompatible // indirect
68+
github.com/shurcooL/go v0.0.0-20190330031554-6713ea532688 // indirect
69+
github.com/sirupsen/logrus v1.4.2 // indirect
70+
github.com/smartystreets/assertions v0.0.0-20190401211740-f487f9de1cd3 // indirect
2271
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa
72+
github.com/spf13/afero v1.2.2 // indirect
73+
github.com/spf13/cobra v0.0.5 // indirect
74+
github.com/spf13/jwalterweatherman v1.1.0 // indirect
75+
github.com/spf13/viper v1.4.0 // indirect
76+
github.com/stretchr/objx v0.2.0 // indirect
77+
github.com/ugorji/go v1.1.5-pre // indirect
78+
github.com/valyala/fasthttp v1.3.0 // indirect
79+
github.com/whyrusleeping/yamux v1.2.0 // indirect
80+
go.etcd.io/bbolt v1.3.3 // indirect
81+
go.opencensus.io v0.22.0 // indirect
82+
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522 // indirect
83+
golang.org/x/image v0.0.0-20190523035834-f03afa92d3ff // indirect
84+
golang.org/x/mobile v0.0.0-20190607214518-6fa95d984e88 // indirect
85+
golang.org/x/mod v0.1.0 // indirect
86+
golang.org/x/net v0.0.0-20190611141213-3f473d35a33a // indirect
87+
golang.org/x/sys v0.0.0-20190610200419-93c9922d18ae // indirect
88+
golang.org/x/tools v0.0.0-20190611150145-4bfb4c74ac90 // indirect
89+
google.golang.org/appengine v1.6.1 // indirect
90+
google.golang.org/genproto v0.0.0-20190605220351-eb0b1bdb6ae6 // indirect
91+
google.golang.org/grpc v1.21.1 // indirect
92+
honnef.co/go/tools v0.0.0-20190607181801-497c8f037f5a // indirect
93+
mvdan.cc/unparam v0.0.0-20190310220240-1b9ccfa71afe // indirect
94+
sourcegraph.com/sqs/pbtypes v1.0.0 // indirect
2395
)

0 commit comments

Comments
 (0)