Skip to content

Commit

Permalink
feat: add test peering
Browse files Browse the repository at this point in the history
  • Loading branch information
hacdias committed Apr 8, 2024
1 parent 92332e9 commit ee70d5d
Show file tree
Hide file tree
Showing 5 changed files with 261 additions and 81 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/ipfs-shipyard/nopfs v0.0.12
github.com/ipfs-shipyard/nopfs/ipfs v0.13.2-0.20231024163508-120e0c51ee3a
github.com/ipfs/boxo v0.18.1-0.20240408102328-6c1937446978
github.com/ipfs/go-block-format v0.2.0
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-datastore v0.6.0
github.com/ipfs/go-ds-badger4 v0.1.5
Expand Down Expand Up @@ -90,7 +91,6 @@ require (
github.com/huin/goupnp v1.3.0 // indirect
github.com/ipfs/bbloom v0.0.4 // indirect
github.com/ipfs/go-bitfield v1.1.0 // indirect
github.com/ipfs/go-block-format v0.2.0 // indirect
github.com/ipfs/go-ipfs-pq v0.0.3 // indirect
github.com/ipfs/go-ipfs-redirects-file v0.1.1 // indirect
github.com/ipfs/go-ipfs-util v0.0.3 // indirect
Expand Down
71 changes: 0 additions & 71 deletions handler_test.go
Original file line number Diff line number Diff line change
@@ -1,84 +1,13 @@
package main

import (
"bytes"
"context"
"net/http"
"net/http/httptest"
"testing"

chunker "github.com/ipfs/boxo/chunker"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/boxo/ipld/unixfs/importer/balanced"
uih "github.com/ipfs/boxo/ipld/unixfs/importer/helpers"
util "github.com/ipfs/boxo/util"
"github.com/ipfs/go-cid"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/multiformats/go-multicodec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func mustTestNode(t *testing.T, cfg Config) *Node {
cfg.DataDir = t.TempDir()
cfg.BlockstoreType = "flatfs"
cfg.DHTRouting = DHTStandard
cfg.RoutingV1Endpoints = []string{cidContactEndpoint}

ctx := context.Background()

sr := util.NewTimeSeededRand()
sk, _, err := ic.GenerateKeyPairWithReader(ic.Ed25519, 2048, sr)
require.NoError(t, err)

cdns := newCachedDNS(dnsCacheRefreshInterval)

t.Cleanup(func() {
_ = cdns.Close()
})

gnd, err := Setup(ctx, cfg, sk, cdns)
require.NoError(t, err)
return gnd
}

func mustTestServer(t *testing.T, cfg Config) (*httptest.Server, *Node) {
gnd := mustTestNode(t, cfg)

handler, err := setupGatewayHandler(cfg, gnd)
if err != nil {
require.NoError(t, err)
}

ts := httptest.NewServer(handler)

return ts, gnd
}

func mustAddFile(t *testing.T, gnd *Node, content []byte) cid.Cid {
dsrv := merkledag.NewDAGService(gnd.bsrv)

// Create a UnixFS graph from our file, parameters described here but can be visualized at https://dag.ipfs.tech/
ufsImportParams := uih.DagBuilderParams{
Maxlinks: uih.DefaultLinksPerBlock, // Default max of 174 links per block
RawLeaves: true, // Leave the actual file bytes untouched instead of wrapping them in a dag-pb protobuf wrapper
CidBuilder: cid.V1Builder{ // Use CIDv1 for all links
Codec: uint64(multicodec.DagPb),
MhType: uint64(multicodec.Sha2_256), // Use SHA2-256 as the hash function
MhLength: -1, // Use the default hash length for the given hash function (in this case 256 bits)
},
Dagserv: dsrv,
NoCopy: false,
}
ufsBuilder, err := ufsImportParams.New(chunker.NewSizeSplitter(bytes.NewReader(content), chunker.DefaultBlockSize)) // Split the file up into fixed sized 256KiB chunks
require.NoError(t, err)

nd, err := balanced.Layout(ufsBuilder) // Arrange the graph with a balanced layout
require.NoError(t, err)

return nd.Cid()
}

func TestTrustless(t *testing.T) {
t.Parallel()

Expand Down
94 changes: 94 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package main

import (
"bytes"
"context"
"crypto/rand"
"net/http/httptest"
"testing"

chunker "github.com/ipfs/boxo/chunker"
"github.com/ipfs/boxo/ipld/merkledag"
"github.com/ipfs/boxo/ipld/unixfs/importer/balanced"
uih "github.com/ipfs/boxo/ipld/unixfs/importer/helpers"
"github.com/ipfs/go-cid"
ic "github.com/libp2p/go-libp2p/core/crypto"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/multiformats/go-multicodec"
"github.com/stretchr/testify/require"
)

func mustTestPeer(t *testing.T) (ic.PrivKey, peer.ID) {
sk, _, err := ic.GenerateKeyPairWithReader(ic.Ed25519, 2048, rand.Reader)
require.NoError(t, err)

pid, err := peer.IDFromPrivateKey(sk)
require.NoError(t, err)

return sk, pid
}

func mustTestNode(t *testing.T, cfg Config) *Node {
sk, _ := mustTestPeer(t)
return mustTestNodeWithKey(t, cfg, sk)
}

func mustTestNodeWithKey(t *testing.T, cfg Config, sk ic.PrivKey) *Node {
// Set necessary fields if not defined.
if cfg.DataDir == "" {
cfg.DataDir = t.TempDir()
}
if cfg.BlockstoreType == "" {
cfg.BlockstoreType = "flatfs"
}
if cfg.DHTRouting == "" {
cfg.DHTRouting = DHTOff
}

ctx := context.Background()
cdns := newCachedDNS(dnsCacheRefreshInterval)

t.Cleanup(func() {
_ = cdns.Close()
})

nd, err := Setup(ctx, cfg, sk, cdns)
require.NoError(t, err)
return nd
}

func mustTestServer(t *testing.T, cfg Config) (*httptest.Server, *Node) {
nd := mustTestNode(t, cfg)

handler, err := setupGatewayHandler(cfg, nd)
if err != nil {
require.NoError(t, err)
}

ts := httptest.NewServer(handler)
return ts, nd
}

func mustAddFile(t *testing.T, gnd *Node, content []byte) cid.Cid {
dsrv := merkledag.NewDAGService(gnd.bsrv)

// Create a UnixFS graph from our file, parameters described here but can be visualized at https://dag.ipfs.tech/
ufsImportParams := uih.DagBuilderParams{
Maxlinks: uih.DefaultLinksPerBlock, // Default max of 174 links per block
RawLeaves: true, // Leave the actual file bytes untouched instead of wrapping them in a dag-pb protobuf wrapper
CidBuilder: cid.V1Builder{ // Use CIDv1 for all links
Codec: uint64(multicodec.DagPb),
MhType: uint64(multicodec.Sha2_256), // Use SHA2-256 as the hash function
MhLength: -1, // Use the default hash length for the given hash function (in this case 256 bits)
},
Dagserv: dsrv,
NoCopy: false,
}
ufsBuilder, err := ufsImportParams.New(chunker.NewSizeSplitter(bytes.NewReader(content), chunker.DefaultBlockSize)) // Split the file up into fixed sized 256KiB chunks
require.NoError(t, err)

nd, err := balanced.Layout(ufsBuilder) // Arrange the graph with a balanced layout
require.NoError(t, err)

return nd.Cid()
}
25 changes: 16 additions & 9 deletions setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,10 @@ func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cached
}
}

if len(routingV1Routers) == 0 && dhtRouter == nil {
return nil, errors.New("no routers available")
}
// Default router is no routing at all: can be especially useful during tests.
router = &routinghelpers.Null{}

if len(routingV1Routers) == 0 {
if len(routingV1Routers) == 0 && dhtRouter != nil {
router = dhtRouter

Check warning on line 280 in setup.go

View check run for this annotation

Codecov / codecov/patch

setup.go#L280

Added line #L280 was not covered by tests
} else {
var routers []*routinghelpers.ParallelRouter
Expand All @@ -301,7 +300,9 @@ func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cached
})

Check warning on line 300 in setup.go

View check run for this annotation

Codecov / codecov/patch

setup.go#L300

Added line #L300 was not covered by tests
}

router = routinghelpers.NewComposableParallel(routers)
if len(routers) > 0 {
router = routinghelpers.NewComposableParallel(routers)

Check warning on line 304 in setup.go

View check run for this annotation

Codecov / codecov/patch

setup.go#L304

Added line #L304 was not covered by tests
}
}

return router, nil
Expand All @@ -321,19 +322,24 @@ func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cached
}
}

var pbrf bsserver.PeerBlockRequestFilter
var (
provideEnabled bool
peerBlockRequestFilter bsserver.PeerBlockRequestFilter
)
if cfg.PeeringCache && len(cfg.Peering) > 0 {
peers := make(map[peer.ID]struct{}, len(cfg.Peering))
for _, a := range cfg.Peering {
peers[a.ID] = struct{}{}
}

pbrf = func(p peer.ID, c cid.Cid) bool {
provideEnabled = true
peerBlockRequestFilter = func(p peer.ID, c cid.Cid) bool {
_, ok := peers[p]
return ok
}
} else {
pbrf = func(p peer.ID, c cid.Cid) bool {
provideEnabled = false
peerBlockRequestFilter = func(p peer.ID, c cid.Cid) bool {
return false

Check warning on line 343 in setup.go

View check run for this annotation

Codecov / codecov/patch

setup.go#L343

Added line #L343 was not covered by tests
}
}
Expand All @@ -352,7 +358,8 @@ func Setup(ctx context.Context, cfg Config, key crypto.PrivKey, dnsCache *cached
bitswap.WithoutDuplicatedBlockStats(),

// ---- Server Options
bitswap.WithPeerBlockRequestFilter(pbrf),
bitswap.WithPeerBlockRequestFilter(peerBlockRequestFilter),
bitswap.ProvideEnabled(provideEnabled),
)
bn.Start(bswap)

Expand Down
Loading

0 comments on commit ee70d5d

Please sign in to comment.