Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add trickle-dag support to the urlstore #5245

Merged
merged 4 commits into from
Jul 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion core/commands/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var rootSubcommands = map[string]*cmds.Command{
"tar": lgc.NewCommand(TarCmd),
"file": lgc.NewCommand(unixfs.UnixFSCmd),
"update": lgc.NewCommand(ExternalBinary()),
"urlstore": lgc.NewCommand(urlStoreCmd),
"urlstore": urlStoreCmd,
"version": lgc.NewCommand(VersionCmd),
"shutdown": lgc.NewCommand(daemonShutdownCmd),
}
Expand Down
38 changes: 22 additions & 16 deletions core/commands/urlstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import (
"fmt"
"io"
"net/http"
"strings"

cmds "github.com/ipfs/go-ipfs/commands"
filestore "github.com/ipfs/go-ipfs/filestore"
balanced "github.com/ipfs/go-ipfs/importer/balanced"
ihelper "github.com/ipfs/go-ipfs/importer/helpers"
trickle "github.com/ipfs/go-ipfs/importer/trickle"

cmds "gx/ipfs/QmNueRyPRQiV7PUEpnP4GgGLuK1rKQLaRW7sfPvUetYig1/go-ipfs-cmds"
mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
chunk "gx/ipfs/QmXnzH7wowyLZy8XJxxaQCVTgLMcDXdMBznmsrmQWCyiQV/go-ipfs-chunker"
cid "gx/ipfs/QmapdYm1b22Frv3k17fqrBYTFRxwiaVJkB299Mfn33edeB/go-cid"
cmdkit "gx/ipfs/QmdE4gMduCKCGAcczM2F5ioYDfdeKuPix138wrES1YSr7f/go-ipfs-cmdkit"
)

var urlStoreCmd = &cmds.Command{

Subcommands: map[string]*cmds.Command{
"add": urlAdd,
},
Expand All @@ -44,14 +43,17 @@ found. It may disappear or the semantics can change at any
time.
`,
},
Options: []cmdkit.Option{
cmdkit.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
},
Arguments: []cmdkit.Argument{
cmdkit.StringArg("url", true, false, "URL to add to IPFS"),
},
Type: BlockStat{},

Run: func(req cmds.Request, res cmds.Response) {
url := req.Arguments()[0]
n, err := req.InvocContext().GetNode()
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
url := req.Arguments[0]
n, err := GetNode(env)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
Expand All @@ -73,6 +75,8 @@ time.
return
}

useTrickledag, _ := req.Options[trickleOptionName].(bool)

hreq, err := http.NewRequest("GET", url, nil)
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
Expand Down Expand Up @@ -100,23 +104,25 @@ time.
URL: url,
}

blc, err := balanced.Layout(dbp.New(chk))
layout := balanced.Layout
if useTrickledag {
layout = trickle.Layout
}
root, err := layout(dbp.New(chk))
if err != nil {
res.SetError(err, cmdkit.ErrNormal)
return
}

res.SetOutput(BlockStat{
Key: blc.Cid().String(),
cmds.EmitOnce(res, BlockStat{
Key: root.Cid().String(),
Size: int(hres.ContentLength),
})
},
Marshalers: cmds.MarshalerMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) {
ch := res.Output().(<-chan interface{})
bs0 := <-ch
bs := bs0.(*BlockStat)
return strings.NewReader(bs.Key + "\n"), nil
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
_, err := fmt.Fprintln(w, bs.Key)
return err
}),
},
}
15 changes: 15 additions & 0 deletions test/sharness/t0272-urlstore.sh
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ test_expect_success "get large file via urlstore" '
test_cmp file3 file3.actual
'

test_expect_success "check that the trickle option works" '
HASHat=$(ipfs add -q --cid-version=1 --raw-leaves=true -n --trickle file3) &&
HASHut=$(ipfs urlstore add --trickle http://127.0.0.1:$GWAY_PORT/ipfs/$HASH3a) &&
test $HASHat = $HASHut
'

test_kill_ipfs_daemon

test_expect_success "files can not be retrieved via the urlstore" '
Expand All @@ -132,4 +138,13 @@ test_expect_success "files can not be retrieved via the urlstore" '
test_must_fail ipfs cat $HASH3 > /dev/null
'

test_expect_success "check that the hashes were correct" '
HASH1e=$(ipfs add -q -n --cid-version=1 --raw-leaves=true file1) &&
HASH2e=$(ipfs add -q -n --cid-version=1 --raw-leaves=true file2) &&
HASH3e=$(ipfs add -q -n --cid-version=1 --raw-leaves=true file3) &&
test $HASH1e = $HASH1 &&
test $HASH2e = $HASH2 &&
test $HASH3e = $HASH3
'

test_done