Unverified 提交 454a1700 作者: Whyrusleeping 提交者: GitHub

Merge pull request #5245 from ipfs/fix/5241

add trickle-dag support to the urlstore
...@@ -136,7 +136,7 @@ var rootSubcommands = map[string]*cmds.Command{ ...@@ -136,7 +136,7 @@ var rootSubcommands = map[string]*cmds.Command{
"tar": lgc.NewCommand(TarCmd), "tar": lgc.NewCommand(TarCmd),
"file": lgc.NewCommand(unixfs.UnixFSCmd), "file": lgc.NewCommand(unixfs.UnixFSCmd),
"update": lgc.NewCommand(ExternalBinary()), "update": lgc.NewCommand(ExternalBinary()),
"urlstore": lgc.NewCommand(urlStoreCmd), "urlstore": urlStoreCmd,
"version": lgc.NewCommand(VersionCmd), "version": lgc.NewCommand(VersionCmd),
"shutdown": lgc.NewCommand(daemonShutdownCmd), "shutdown": lgc.NewCommand(daemonShutdownCmd),
} }
......
...@@ -4,13 +4,13 @@ import ( ...@@ -4,13 +4,13 @@ import (
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
"strings"
cmds "github.com/ipfs/go-ipfs/commands"
filestore "github.com/ipfs/go-ipfs/filestore" filestore "github.com/ipfs/go-ipfs/filestore"
balanced "github.com/ipfs/go-ipfs/importer/balanced" balanced "github.com/ipfs/go-ipfs/importer/balanced"
ihelper "github.com/ipfs/go-ipfs/importer/helpers" 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" mh "gx/ipfs/QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8/go-multihash"
chunk "gx/ipfs/QmVDjhUMtkRskBFAVNwyXuLSKbeAya7JKPnzAxMKDaK4x4/go-ipfs-chunker" chunk "gx/ipfs/QmVDjhUMtkRskBFAVNwyXuLSKbeAya7JKPnzAxMKDaK4x4/go-ipfs-chunker"
cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid" cid "gx/ipfs/QmYVNvtQkeZ6AKSwDrjQTs432QtL6umrrK41EBq3cu7iSP/go-cid"
...@@ -18,7 +18,6 @@ import ( ...@@ -18,7 +18,6 @@ import (
) )
var urlStoreCmd = &cmds.Command{ var urlStoreCmd = &cmds.Command{
Subcommands: map[string]*cmds.Command{ Subcommands: map[string]*cmds.Command{
"add": urlAdd, "add": urlAdd,
}, },
...@@ -44,14 +43,17 @@ found. It may disappear or the semantics can change at any ...@@ -44,14 +43,17 @@ found. It may disappear or the semantics can change at any
time. time.
`, `,
}, },
Options: []cmdkit.Option{
cmdkit.BoolOption(trickleOptionName, "t", "Use trickle-dag format for dag generation."),
},
Arguments: []cmdkit.Argument{ Arguments: []cmdkit.Argument{
cmdkit.StringArg("url", true, false, "URL to add to IPFS"), cmdkit.StringArg("url", true, false, "URL to add to IPFS"),
}, },
Type: BlockStat{}, Type: BlockStat{},
Run: func(req cmds.Request, res cmds.Response) { Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) {
url := req.Arguments()[0] url := req.Arguments[0]
n, err := req.InvocContext().GetNode() n, err := GetNode(env)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
return return
...@@ -73,6 +75,8 @@ time. ...@@ -73,6 +75,8 @@ time.
return return
} }
useTrickledag, _ := req.Options[trickleOptionName].(bool)
hreq, err := http.NewRequest("GET", url, nil) hreq, err := http.NewRequest("GET", url, nil)
if err != nil { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
...@@ -100,23 +104,25 @@ time. ...@@ -100,23 +104,25 @@ time.
URL: url, 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 { if err != nil {
res.SetError(err, cmdkit.ErrNormal) res.SetError(err, cmdkit.ErrNormal)
return return
} }
res.SetOutput(BlockStat{ cmds.EmitOnce(res, BlockStat{
Key: blc.Cid().String(), Key: root.Cid().String(),
Size: int(hres.ContentLength), Size: int(hres.ContentLength),
}) })
}, },
Marshalers: cmds.MarshalerMap{ Encoders: cmds.EncoderMap{
cmds.Text: func(res cmds.Response) (io.Reader, error) { cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, bs *BlockStat) error {
ch := res.Output().(<-chan interface{}) _, err := fmt.Fprintln(w, bs.Key)
bs0 := <-ch return err
bs := bs0.(*BlockStat) }),
return strings.NewReader(bs.Key + "\n"), nil
},
}, },
} }
...@@ -124,6 +124,12 @@ test_expect_success "get large file via urlstore" ' ...@@ -124,6 +124,12 @@ test_expect_success "get large file via urlstore" '
test_cmp file3 file3.actual 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_kill_ipfs_daemon
test_expect_success "files can not be retrieved via the urlstore" ' test_expect_success "files can not be retrieved via the urlstore" '
...@@ -132,4 +138,13 @@ test_expect_success "files can not be retrieved via the urlstore" ' ...@@ -132,4 +138,13 @@ test_expect_success "files can not be retrieved via the urlstore" '
test_must_fail ipfs cat $HASH3 > /dev/null 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 test_done
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论