Skip to content

Commit

Permalink
encode keys to datastore with base32 standard encoding
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Jeromy <why@ipfs.io>
  • Loading branch information
whyrusleeping committed Jun 25, 2016
1 parent 2a4bbb7 commit 6950edd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 42 deletions.
17 changes: 11 additions & 6 deletions blocks/blockstore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,26 +148,31 @@ func (bs *blockstore) AllKeysChan(ctx context.Context) (<-chan key.Key, error) {
}

// this function is here to compartmentalize
get := func() (k key.Key, ok bool) {
get := func() (key.Key, bool) {
select {
case <-ctx.Done():
return k, false
return "", false
case e, more := <-res.Next():
if !more {
return k, false
return "", false
}
if e.Error != nil {
log.Debug("blockstore.AllKeysChan got err:", e.Error)
return k, false
return "", false
}

// need to convert to key.Key using key.KeyFromDsKey.
k = key.KeyFromDsKey(ds.NewKey(e.Key))
k, err := key.KeyFromDsKey(ds.NewKey(e.Key))
if err != nil {
log.Warningf("error parsing key from DsKey: ", err)
return "", true
}
log.Debug("blockstore: query got key", k)

// key must be a multihash. else ignore it.
_, err := mh.Cast([]byte(k))
_, err = mh.Cast([]byte(k))
if err != nil {
log.Warningf("key from datastore was not a multihash: ", err)
return "", true
}

Expand Down
36 changes: 7 additions & 29 deletions blocks/key/key.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package key

import (
"encoding/base32"
"encoding/json"
"fmt"

Expand Down Expand Up @@ -38,7 +39,7 @@ func B58KeyEncode(k Key) string {

// DsKey returns a Datastore key
func (k Key) DsKey() ds.Key {
return ds.NewKey(string(k))
return ds.NewKey(base32.StdEncoding.EncodeToString([]byte(k)))
}

// UnmarshalJSON returns a JSON-encoded Key (string)
Expand Down Expand Up @@ -68,36 +69,13 @@ func (k *Key) Loggable() map[string]interface{} {
}

// KeyFromDsKey returns a Datastore key
func KeyFromDsKey(dsk ds.Key) Key {
return Key(dsk.String()[1:])
}

// B58KeyConverter -- for KeyTransform datastores
// (static as only one obj needed)
var B58KeyConverter = b58KeyConverter{}

type b58KeyConverter struct{}

// ConvertKey returns a B58 encoded Datastore key
// TODO: this is hacky because it encodes every path component. some
// path components may be proper strings already...
func (b58KeyConverter) ConvertKey(dsk ds.Key) ds.Key {
k := ds.NewKey("/")
for _, n := range dsk.Namespaces() {
k = k.ChildString(b58.Encode([]byte(n)))
func KeyFromDsKey(dsk ds.Key) (Key, error) {
dec, err := base32.StdEncoding.DecodeString(dsk.String()[1:])
if err != nil {
return "", err
}
return k
}

// InvertKey returns a b58 decoded Datastore key
// TODO: this is hacky because it encodes every path component. some
// path components may be proper strings already...
func (b58KeyConverter) InvertKey(dsk ds.Key) ds.Key {
k := ds.NewKey("/")
for _, n := range dsk.Namespaces() {
k = k.ChildString(string(b58.Decode(n)))
}
return k
return Key(dec), nil
}

// KeySlice is used for sorting Keys
Expand Down
2 changes: 1 addition & 1 deletion repo/fsrepo/fsrepo.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (
var log = logging.Logger("fsrepo")

// version number that we are currently expecting to see
var RepoVersion = "3"
var RepoVersion = "4"

var migrationInstructions = `See https://github.com/ipfs/fs-repo-migrations/blob/master/run.md
Sorry for the inconvenience. In the future, these will run automatically.`
Expand Down
12 changes: 6 additions & 6 deletions test/sharness/t0080-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -108,14 +108,14 @@ test_expect_success "'ipfs repo gc' removes file" '
grep "removed $PATCH_ROOT" actual7
'

# TODO: there seems to be a serious bug with leveldb not returning a key.
test_expect_failure "'ipfs refs local' no longer shows file" '
test_expect_success "'ipfs refs local' no longer shows file" '
EMPTY_DIR=QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn &&
echo "$EMPTY_DIR" >expected8 &&
echo "$HASH_WELCOME_DOCS" >>expected8 &&
ipfs refs -r "$HASH_WELCOME_DOCS" >>expected8 &&
ipfs refs local >actual8 &&
test_sort_cmp expected8 actual8
grep "QmYCvbfNbCwFR45HiNP45rwJgvatpiW38D961L5qAhUM5Y" actual8 &&
grep "$EMPTY_DIR" actual8 &&
grep "$HASH_WELCOME_DOCS" actual8 &&
test_must_fail grep "$HASH" actual8 &&
test_must_fail grep "$PATCH_ROOT" actual8
'

test_expect_success "adding multiblock random file succeeds" '
Expand Down

0 comments on commit 6950edd

Please sign in to comment.