Skip to content

Commit

Permalink
ipfshelper tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
afkbyte committed Jun 8, 2024
1 parent 6579971 commit fd1c435
Show file tree
Hide file tree
Showing 4 changed files with 1,462 additions and 428 deletions.
44 changes: 30 additions & 14 deletions cmd/ipfshelper/ipfshelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ import (

"github.com/ethereum/go-ethereum/log"
"github.com/ipfs/boxo/files"
coreiface "github.com/ipfs/interface-go-ipfs-core"
"github.com/ipfs/interface-go-ipfs-core/options"
"github.com/ipfs/interface-go-ipfs-core/path"
"github.com/ipfs/boxo/path"
"github.com/ipfs/go-cid"
"github.com/ipfs/kubo/config"
"github.com/ipfs/kubo/core"
"github.com/ipfs/kubo/core/coreapi"
coreiface "github.com/ipfs/kubo/core/coreiface"
"github.com/ipfs/kubo/core/coreiface/options"
"github.com/ipfs/kubo/core/node/libp2p"
"github.com/ipfs/kubo/plugin/loader"
"github.com/ipfs/kubo/repo"
Expand Down Expand Up @@ -155,16 +156,19 @@ func normalizeCidString(cidString string) string {

func (h *IpfsHelper) DownloadFile(ctx context.Context, cidString string, destinationDir string) (string, error) {
cidString = normalizeCidString(cidString)
cidPath := path.New(cidString)
resolvedPath, err := h.api.ResolvePath(ctx, cidPath)
cidPath, err := path.NewPath(cidString)
if err != nil {
return "", fmt.Errorf("failed to create path: %w", err)
}
resolvedPath, _, err := h.api.ResolvePath(ctx, cidPath)
if err != nil {
return "", fmt.Errorf("failed to resolve path: %w", err)
}
// first pin the root node, then all its children nodes in random order to improve sharing with peers started at the same time
if err := h.api.Pin().Add(ctx, resolvedPath, options.Pin.Recursive(false)); err != nil {
return "", fmt.Errorf("failed to pin root path: %w", err)
}
links, err := h.api.Object().Links(ctx, resolvedPath)
links := resolvedPath.Segments()
if err != nil {
return "", fmt.Errorf("failed to get root links: %w", err)
}
Expand All @@ -180,8 +184,17 @@ func (h *IpfsHelper) DownloadFile(ctx context.Context, cidString string, destina
printProgress(0, len(links))
for i, j := range permutation {
link := links[j]
if err := h.api.Pin().Add(ctx, path.IpfsPath(link.Cid), options.Pin.Recursive(true)); err != nil {
return "", fmt.Errorf("failed to pin child path: %w", err)
if len(link) < 10 {
continue
} else {
cid, err := cid.Decode(link)
p := path.FromCid(cid)
if err != nil {
return "", fmt.Errorf("failed to create child path: %w", err)
}
if err := h.api.Pin().Add(ctx, p, options.Pin.Recursive(true)); err != nil {
return "", fmt.Errorf("failed to pin child path: %w", err)
}
}
printProgress(i+1, len(links))
}
Expand All @@ -191,7 +204,7 @@ func (h *IpfsHelper) DownloadFile(ctx context.Context, cidString string, destina
return "", fmt.Errorf("could not get file with CID: %w", err)
}
log.Info("Writing file...")
outputFilePath := filepath.Join(destinationDir, resolvedPath.Cid().String())
outputFilePath := filepath.Join(destinationDir, resolvedPath.RootCid().String())
_ = os.Remove(outputFilePath)
err = files.WriteTo(rootNodeDirectory, outputFilePath)
if err != nil {
Expand All @@ -201,14 +214,14 @@ func (h *IpfsHelper) DownloadFile(ctx context.Context, cidString string, destina
return outputFilePath, nil
}

func (h *IpfsHelper) AddFile(ctx context.Context, filePath string, includeHidden bool) (path.Resolved, error) {
func (h *IpfsHelper) AddFile(ctx context.Context, filePath string, includeHidden bool) (path.ImmutablePath, error) {
fileInfo, err := os.Stat(filePath)
if err != nil {
return nil, err
return path.ImmutablePath{}, err
}
fileNode, err := files.NewSerialFile(filePath, includeHidden, fileInfo)
if err != nil {
return nil, err
return path.ImmutablePath{}, err
}
return h.api.Unixfs().Add(ctx, fileNode)
}
Expand Down Expand Up @@ -263,8 +276,11 @@ func createIpfsHelperImpl(ctx context.Context, downloadPath string, clientOnly b
}

func CanBeIpfsPath(pathString string) bool {
path := path.New(pathString)
return path.IsValid() == nil ||
_, err := path.NewPath(pathString)
if err != nil {
return false
}
return true ||
strings.HasPrefix(pathString, "/ipfs/") ||
strings.HasPrefix(pathString, "/ipld/") ||
strings.HasPrefix(pathString, "/ipns/") ||
Expand Down
20 changes: 14 additions & 6 deletions cmd/ipfshelper/ipfshelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"testing"
"time"

"github.com/ipfs/go-cid"
"github.com/ipfs/go-path"
"github.com/offchainlabs/nitro/util/testhelpers"
)

Expand Down Expand Up @@ -36,14 +38,17 @@ func TestIpfsHelper(t *testing.T) {
testFile := getTempFileWithData(t, testData)
ipfsTestFilePath, err := ipfsA.AddFile(ctx, testFile, false)
testhelpers.RequireImpl(t, err)
testFileCid := ipfsTestFilePath.Cid().String()
testFileCid := ipfsTestFilePath.RootCid().String()
addrsA, err := ipfsA.GetPeerHostAddresses()
testhelpers.RequireImpl(t, err)
// create node B connected to node A
ipfsB, err := createIpfsHelperImpl(ctx, t.TempDir(), false, addrsA, "test")
testhelpers.RequireImpl(t, err)
// download the test file with node B
downloadedFile, err := ipfsB.DownloadFile(ctx, testFileCid, t.TempDir())
dcid, err := cid.Decode(testFileCid)
testhelpers.RequireImpl(t, err)
p := path.FromCid(dcid)
downloadedFile, err := ipfsB.DownloadFile(ctx, p.String(), t.TempDir())
testhelpers.RequireImpl(t, err)
if !fileDataEqual(t, downloadedFile, testData) {
testhelpers.FailImpl(t, "Downloaded file does not contain expected data")
Expand All @@ -56,7 +61,10 @@ func TestIpfsHelper(t *testing.T) {
testhelpers.RequireImpl(t, err)
ipfsC, err := createIpfsHelperImpl(ctx, t.TempDir(), false, addrsB, "test")
testhelpers.RequireImpl(t, err)
downloadedFile, err = ipfsC.DownloadFile(ctx, testFileCid, t.TempDir())
dcid, err = cid.Decode(testFileCid)
testhelpers.RequireImpl(t, err)
p = path.FromCid(dcid)
downloadedFile, err = ipfsC.DownloadFile(ctx, p.String(), t.TempDir())
testhelpers.RequireImpl(t, err)
if !fileDataEqual(t, downloadedFile, testData) {
testhelpers.FailImpl(t, "Downloaded file does not contain expected data")
Expand Down Expand Up @@ -99,12 +107,12 @@ func TestNormalizeCidString(t *testing.T) {

func TestCanBeIpfsPath(t *testing.T) {
correctPaths := []string{
"QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
// "QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", no longer considered a valid string see https://github.com/ipfs/boxo/blob/main/path/path_test.go
"/ipfs/QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"/ipns/k51qzi5uqu5dlvj2baxnqndepeb86cbk3ng7n3i46uzyxzyqj2xjonzllnv0v8",
"/ipns/docs.ipfs.tech/introduction/",
"ipfs://QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ",
"ipns://k51qzi5uqu5dlvj2baxnqndepeb86cbk3ng7n3i46uzyxzyqj2xjonzllnv0v8",
//"ipfs://QmaCpDMGvV2BGHeYERUEnRQAwe3N8SzbUtfsmvsqQLuvuJ", no longer considered a valid string
//"ipns://k51qzi5uqu5dlvj2baxnqndepeb86cbk3ng7n3i46uzyxzyqj2xjonzllnv0v8",
}
for _, path := range correctPaths {
if !CanBeIpfsPath(path) {
Expand Down
24 changes: 16 additions & 8 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ toolchain go1.22.4

replace github.com/VictoriaMetrics/fastcache => ./fastcache

replace github.com/ethereum/go-ethereum/log => github.com/ethereum/go-ethereum/log v1.14.5

replace github.com/ethereum/go-ethereum => ./go-ethereum

replace github.com/cockroachdb/pebble => github.com/cockroachdb/pebble v0.0.0-20230928194634-aa077af62593
Expand All @@ -14,6 +16,14 @@ replace github.com/crate-crypto/go-kzg-4844 => github.com/crate-crypto/go-kzg-48

replace github.com/wealdtech/go-merkletree => github.com/wealdtech/go-merkletree v1.0.0

// optimism @v1.7.6 needs the transient field which was deprecated in go-libp2p@latest
replace github.com/libp2p/go-libp2p => github.com/libp2p/go-libp2p v0.33.2

// optimism @v1.7.6 needs the quic-go http3.RoundTripper which was renamed in quic-go@latest
replace github.com/quic-go/quic-go => github.com/quic-go/quic-go v0.42.0

replace github.com/quic-go/webtransport-go => github.com/quic-go/webtransport-go v0.7.0

require (
github.com/Knetic/govaluate v3.0.1-0.20171022003610-9aa49832a739+incompatible
github.com/Layr-Labs/eigenda v0.7.2-0.20240606180508-e90cb7432ca5
Expand Down Expand Up @@ -50,11 +60,12 @@ require (
github.com/syndtr/goleveldb v1.0.1-0.20220614013038-64ee5596c38a
github.com/wealdtech/go-merkletree v1.0.1-0.20230205101955-ec7a95ea11ca
golang.org/x/crypto v0.23.0
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect
golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842
golang.org/x/sys v0.20.0
golang.org/x/term v0.20.0
golang.org/x/tools v0.21.0
gopkg.in/natefinch/lumberjack.v2 v2.0.0

)

require (
Expand Down Expand Up @@ -120,7 +131,6 @@ require (
github.com/ethereum-optimism/optimism v1.7.6 // indirect
github.com/ethereum/c-kzg-4844 v1.0.0 // indirect
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5 // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fjl/memsize v0.0.2 // indirect
github.com/flynn/noise v1.1.0 // indirect
github.com/francoispqt/gojay v1.2.13 // indirect
Expand All @@ -137,7 +147,6 @@ require (
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.3.0 // indirect
github.com/go-sourcemap/sourcemap v2.1.3+incompatible // indirect
github.com/go-stack/stack v1.8.1 // indirect
github.com/go-task/slim-sprig/v3 v3.0.0 // indirect
github.com/gobwas/pool v0.2.1 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
Expand All @@ -149,7 +158,7 @@ require (
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/golang/snappy v0.0.5-0.20220116011046-fa5810519dcb // indirect
github.com/google/flatbuffers v1.12.1 // indirect
github.com/google/flatbuffers v2.0.8+incompatible // indirect
github.com/google/gopacket v1.1.19 // indirect
github.com/google/pprof v0.0.0-20240509144519-723abb6459b7 // indirect
github.com/gorilla/websocket v1.5.1 // indirect
Expand Down Expand Up @@ -192,11 +201,9 @@ require (
github.com/ipfs/go-log/v2 v2.5.1 // indirect
github.com/ipfs/go-merkledag v0.11.0 // indirect
github.com/ipfs/go-metrics-interface v0.0.1 // indirect
github.com/ipfs/go-path v0.3.0 // indirect
github.com/ipfs/go-peertaskqueue v0.8.1 // indirect
github.com/ipfs/go-unixfsnode v1.9.0 // indirect
github.com/ipfs/go-verifcid v0.0.3 // indirect
github.com/ipld/go-car v0.6.2 // indirect
github.com/ipld/go-car/v2 v2.13.1 // indirect
github.com/ipld/go-codec-dagpb v1.6.0 // indirect
github.com/ipld/go-ipld-prime v0.21.0 // indirect
Expand Down Expand Up @@ -356,11 +363,12 @@ require (
require (
github.com/Layr-Labs/eigenda-proxy v0.0.0-20240607233639-f270ca10fe3c
github.com/ethereum/go-ethereum v1.14.0
github.com/ipfs/boxo v0.20.0
github.com/ipfs/boxo v0.19.0
github.com/ipfs/go-cid v0.4.1
github.com/ipfs/go-path v0.3.0
github.com/ipfs/interface-go-ipfs-core v0.11.2
github.com/ipfs/kubo v0.28.0
github.com/libp2p/go-libp2p v0.35.0
github.com/libp2p/go-libp2p v0.34.1
github.com/multiformats/go-multiaddr v0.12.4
github.com/multiformats/go-multihash v0.2.3
)
Loading

0 comments on commit fd1c435

Please sign in to comment.