Skip to content

Commit

Permalink
Merge pull request #9 from application-research/ar-test-example
Browse files Browse the repository at this point in the history
chore: add ar test peer
  • Loading branch information
alvin-reyes committed Dec 1, 2022
2 parents d7552e3 + 3f0670f commit 1d56f42
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 30 deletions.
49 changes: 49 additions & 0 deletions examples/artest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package main

import (
"context"
"fmt"
whypfs "github.com/application-research/whypfs-core"
"io"

"github.com/ipfs/go-cid"
)

// Creating a new whypfs node, bootstrapping it with the default bootstrap peers, adding a file to the whypfs network, and
// then retrieving the file from the whypfs network.
func ArTest() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

whypfsPeer, err := whypfs.NewNode(whypfs.NewNodeParams{
Ctx: ctx,
Datastore: whypfs.NewInMemoryDatastore(),
})

whypfsPeer.BootstrapPeers(whypfs.DefaultBootstrapPeers())

c1, _ := cid.Decode("QmbJGGJkjGfYCmHqwoMjLTbUA6bdcFBbNdWChFY6dKNRWx")
rsc1, err := whypfsPeer.GetFile(ctx, c1)
if err != nil {
panic(err)
}
defer rsc1.Close()
content1, err := io.ReadAll(rsc1)
if err != nil {
panic(err)
}

c2, _ := cid.Decode("QmPecv5jpAPVG3LUN8fbptnaPHFzt3SRtxC8e1XZCVMiSM")
rsc2, err := whypfsPeer.GetFile(ctx, c2)
if err != nil {
panic(err)
}
defer rsc2.Close()
content2, err := io.ReadAll(rsc2)
if err != nil {
panic(err)
}

fmt.Println(string(content1))
fmt.Println(string(content2))
}
2 changes: 1 addition & 1 deletion examples/basicpeer.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (

// Creating a new whypfs node, bootstrapping it with the default bootstrap peers, adding a file to the whypfs network, and
// then retrieving the file from the whypfs network.
func main() {
func BasicPeer() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

Expand Down
28 changes: 28 additions & 0 deletions examples/dirtest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import (
"context"
whypfs "github.com/application-research/whypfs-core"
"github.com/ipfs/go-cid"
)

// Creating a new whypfs node, bootstrapping it with the default bootstrap peers, adding a file to the whypfs network, and
// then retrieving the file from the whypfs network.
func DirTest() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

whypfsPeer, err := whypfs.NewNode(whypfs.NewNodeParams{
Ctx: ctx,
Datastore: whypfs.NewInMemoryDatastore(),
})

whypfsPeer.BootstrapPeers(whypfs.DefaultBootstrapPeers())

c1, _ := cid.Decode("bafybeigvgzoolc3drupxhlevdp2ugqcrbcsqfmcek2zxiw5wctk3xjpjwy")
rsc1, err := whypfsPeer.GetDirectoryWithCid(ctx, c1)
if err != nil {
panic(err)
}
rsc1.GetNode()
}
8 changes: 8 additions & 0 deletions examples/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package main

import "fmt"

func main() {
fmt.Println("Testing Grounds Only!")

}
1 change: 1 addition & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ var defaultTestBootstrapPeers []multiaddr.Multiaddr
func DefaultBootstrapPeers() []peer.AddrInfo {

for _, s := range []string{
"/ip4/145.40.90.135/tcp/6746/p2p/12D3KooWNTiHg8eQsTRx8XV7TiJbq3379EgwG6Mo3V3MdwAfThsx",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmNnooDu7bfjPFoTZYxMNLWUQJyrVwtbZg5gBMjTezGAJN",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmQCU2EcMqAqQPR2i9bChDtGNJchTbq5TbXJJ16u19uLTa",
"/dnsaddr/bootstrap.libp2p.io/p2p/QmbLHAnMoJPWSCR5Zhtx6BHJX9KiKNN6tpvbUcqanj75Nb",
Expand Down
26 changes: 11 additions & 15 deletions whypfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,19 +398,19 @@ func (p *Node) setupDatastore() error {
}
p.Datastore = ds // data store.

dhtopts := fullrt.DHTOption(
dht.Datastore(p.Datastore),
dht.BootstrapPeers(DefaultBootstrapPeers()...),
dht.BucketSize(20),
)
}

frt, err := fullrt.NewFullRT(p.Host, dht.DefaultPrefix, dhtopts)
if err != nil {
return xerrors.Errorf("constructing fullrt: %w", err)
}
p.FullRt = frt // full routing table
dhtopts := fullrt.DHTOption(
dht.Datastore(p.Datastore),
dht.BootstrapPeers(DefaultBootstrapPeers()...),
dht.BucketSize(20),
)

frt, err := fullrt.NewFullRT(p.Host, dht.DefaultPrefix, dhtopts)
if err != nil {
return xerrors.Errorf("constructing fullrt: %w", err)
}
p.FullRt = frt // full routing table

// no ipfs Dht, let's set it up for them.
if p.Dht == nil {
Expand Down Expand Up @@ -704,15 +704,11 @@ func (p *Node) GetFile(ctx context.Context, c cid.Cid) (ufsio.ReadSeekCloser, er

// Getting the directory with the cid.
func (p *Node) GetDirectoryWithCid(ctx context.Context, c cid.Cid) (ufsio.Directory, error) {

//links, _ :=
//nodes := ipld.GetNodes(ctx, p, []cid.Cid{c})

node, err := p.Get(ctx, c)

if err != nil {
return nil, err
}
fmt.Println("node", node)
directory, err := ufsio.NewDirectoryFromNode(p.DAGService, node)
if err != nil {
return nil, err
Expand Down
20 changes: 6 additions & 14 deletions whypfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ package whypfs
import (
"bytes"
"context"
"github.com/ipfs/go-cid"
leveldb "github.com/ipfs/go-ds-leveldb"
cbor "github.com/ipfs/go-ipld-cbor"
"github.com/libp2p/go-libp2p/core/peer"
Expand Down Expand Up @@ -268,24 +269,15 @@ func TestGetDirectory(t *testing.T) {
}

func TestGetDirectoryWithCid(t *testing.T) {
assert.Equal(t, true, true)
return

p1, err := NewNode(NewNodeParams{
Ctx: context.Background(),
Datastore: NewInMemoryDatastore(),
})
pinfo1 := peer.AddrInfo{
ID: p1.Host.ID(),
Addrs: p1.Host.Addrs(),
}
p1.BootstrapPeers([]peer.AddrInfo{pinfo1})
node, err := p1.AddPinDirectory(context.Background(), "./test/test_directory")
if err != nil {
t.Fatal(err)
}
t.Log("uploaded", node.Cid())
p1.BootstrapPeers(DefaultBootstrapPeers())

rsc, err := p1.GetDirectoryWithCid(context.Background(), node.Cid())
cid, err := cid.Decode("bafybeigvgzoolc3drupxhlevdp2ugqcrbcsqfmcek2zxiw5wctk3xjpjwy")
rsc, err := p1.GetDirectoryWithCid(context.Background(), cid)
if err != nil {
t.Fatal(err)
}
Expand All @@ -295,7 +287,7 @@ func TestGetDirectoryWithCid(t *testing.T) {
t.Fatal(err)
}
t.Log("retrieved root node", retrieveNode.Cid())
assert.Equal(t, "bafybeihnhfwlfvq6eplc4i5cnj2of2whk6aab6kc4xeryr3ttfcaawjiyi", retrieveNode.Cid().String())
assert.Equal(t, "bafybeigvgzoolc3drupxhlevdp2ugqcrbcsqfmcek2zxiw5wctk3xjpjwy", retrieveNode.Cid().String())
assert.GreaterOrEqual(t, len(retrieveNode.Links()), 1)
}

Expand Down

0 comments on commit 1d56f42

Please sign in to comment.