Skip to content

Commit 44433f3

Browse files
committed
feat: report block count on dag import
1 parent fcfe793 commit 44433f3

File tree

3 files changed

+31
-12
lines changed

3 files changed

+31
-12
lines changed

core/commands/dag/dag.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ type ResolveOutput struct {
5555

5656
// CarImportOutput is the output type of the 'dag import' commands
5757
type CarImportOutput struct {
58-
Root RootMeta
58+
BlockCount uint64
59+
Root *RootMeta
5960
}
6061

6162
// RootMeta is the metadata for a root pinning response
@@ -157,8 +158,9 @@ var DagResolveCmd = &cmds.Command{
157158
}
158159

159160
type importResult struct {
160-
roots map[cid.Cid]struct{}
161-
err error
161+
blockCount uint64
162+
roots map[cid.Cid]struct{}
163+
err error
162164
}
163165

164166
// DagImportCmd is a command for importing a car to ipfs
@@ -203,6 +205,11 @@ Maximum supported CAR version: 1
203205
return nil
204206
}
205207

208+
if event.Root == nil {
209+
fmt.Fprintf(w, "Imported %d blocks\n", event.BlockCount)
210+
return nil
211+
}
212+
206213
enc, err := cmdenv.GetLowLevelCidEncoder(req)
207214
if err != nil {
208215
return err

core/commands/dag/import.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ func dagImport(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment
5555
return done.err
5656
}
5757

58+
if err := res.Emit(&CarImportOutput{BlockCount: done.blockCount}); err != nil {
59+
return err
60+
}
61+
5862
// It is not guaranteed that a root in a header is actually present in the same ( or any )
5963
// .car file. This is the case in version 1, and ideally in further versions too
6064
// Accumulate any root CID seen in a header, and supplement its actual node if/when encountered
@@ -101,7 +105,7 @@ func dagImport(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment
101105
failedPins++
102106
}
103107

104-
if err := res.Emit(&CarImportOutput{Root: ret}); err != nil {
108+
if err := res.Emit(&CarImportOutput{Root: &ret}); err != nil {
105109
return err
106110
}
107111
}
@@ -126,6 +130,7 @@ func importWorker(req *cmds.Request, re cmds.ResponseEmitter, api iface.CoreAPI,
126130
batch := ipld.NewBatch(req.Context, api.Dag())
127131

128132
roots := make(map[cid.Cid]struct{})
133+
var blockCount uint64
129134

130135
it := req.Files.Entries()
131136
for it.Next() {
@@ -176,6 +181,7 @@ func importWorker(req *cmds.Request, re cmds.ResponseEmitter, api iface.CoreAPI,
176181
if err := batch.Add(req.Context, nd); err != nil {
177182
return err
178183
}
184+
blockCount++
179185
}
180186

181187
return nil
@@ -197,5 +203,5 @@ func importWorker(req *cmds.Request, re cmds.ResponseEmitter, api iface.CoreAPI,
197203
return
198204
}
199205

200-
ret <- importResult{roots: roots}
206+
ret <- importResult{blockCount: blockCount, roots: roots}
201207
}

test/sharness/t0054-dag-car-import-export.sh

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,16 @@ run_online_imp_exp_tests() {
5656
reset_blockstore 1
5757

5858
cat > basic_import_expected <<EOE
59+
Imported 1198 blocks
5960
Pinned root${tab}bafkqaaa${tab}success
6061
Pinned root${tab}bafy2bzaceaxm23epjsmh75yvzcecsrbavlmkcxnva66bkdebdcnyw3bjrc74u${tab}success
6162
Pinned root${tab}bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy${tab}success
6263
EOE
6364

6465
cat >naked_root_import_json_expected <<EOE
65-
{"Root":{"Cid":{"/":"bafy2bzaceaxm23epjsmh75yvzcecsrbavlmkcxnva66bkdebdcnyw3bjrc74u"},"PinErrorMsg":""}}
66-
{"Root":{"Cid":{"/":"bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy"},"PinErrorMsg":""}}
66+
{"BlockCount":0,"Root":null}
67+
{"BlockCount":0,"Root":{"Cid":{"/":"bafy2bzaceaxm23epjsmh75yvzcecsrbavlmkcxnva66bkdebdcnyw3bjrc74u"},"PinErrorMsg":""}}
68+
{"BlockCount":0,"Root":{"Cid":{"/":"bafy2bzaced4ueelaegfs5fqu4tzsh6ywbbpfk3cxppupmxfdhbpbhzawfw5oy"},"PinErrorMsg":""}}
6769
EOE
6870

6971

@@ -170,9 +172,10 @@ test_expect_success "correct error" '
170172

171173

172174
cat >multiroot_import_json_expected <<EOE
173-
{"Root":{"Cid":{"/":"bafy2bzaceb55n7uxyfaelplulk3ev2xz7gnq6crncf3ahnvu46hqqmpucizcw"},"PinErrorMsg":""}}
174-
{"Root":{"Cid":{"/":"bafy2bzacebedrc4n2ac6cqdkhs7lmj5e4xiif3gu7nmoborihajxn3fav3vdq"},"PinErrorMsg":""}}
175-
{"Root":{"Cid":{"/":"bafy2bzacede2hsme6hparlbr4g2x6pylj43olp4uihwjq3plqdjyrdhrv7cp4"},"PinErrorMsg":""}}
175+
{"BlockCount":2825,"Root":null}
176+
{"BlockCount":0,"Root":{"Cid":{"/":"bafy2bzaceb55n7uxyfaelplulk3ev2xz7gnq6crncf3ahnvu46hqqmpucizcw"},"PinErrorMsg":""}}
177+
{"BlockCount":0,"Root":{"Cid":{"/":"bafy2bzacebedrc4n2ac6cqdkhs7lmj5e4xiif3gu7nmoborihajxn3fav3vdq"},"PinErrorMsg":""}}
178+
{"BlockCount":0,"Root":{"Cid":{"/":"bafy2bzacede2hsme6hparlbr4g2x6pylj43olp4uihwjq3plqdjyrdhrv7cp4"},"PinErrorMsg":""}}
176179
EOE
177180
test_expect_success "multiroot import works" '
178181
ipfs dag import --enc=json ../t0054-dag-car-import-export-data/lotus_testnet_export_256_multiroot.car > multiroot_import_json_actual
@@ -182,14 +185,17 @@ test_expect_success "multiroot import expected output" '
182185
'
183186

184187

188+
cat >pin_import_expected << EOE
189+
{"BlockCount":1198,"Root":null}
190+
EOE
185191
test_expect_success "pin-less import works" '
186192
ipfs dag import --enc=json --pin-roots=false \
187193
../t0054-dag-car-import-export-data/lotus_devnet_genesis.car \
188194
../t0054-dag-car-import-export-data/lotus_testnet_export_128.car \
189195
> no-pin_import_actual
190196
'
191-
test_expect_success "expected silence on --pin-roots=false" '
192-
test_cmp /dev/null no-pin_import_actual
197+
test_expect_success "expected no pins on --pin-roots=false" '
198+
test_cmp pin_import_expected no-pin_import_actual
193199
'
194200

195201

0 commit comments

Comments
 (0)