Skip to content

Commit 30f7aa9

Browse files
committed
integrate CIDv0
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent 6fdfaaf commit 30f7aa9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+900
-974
lines changed

assets/assets.go

+12-8
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ import (
88
"fmt"
99
"path/filepath"
1010

11-
"github.com/ipfs/go-ipfs/blocks/key"
1211
"github.com/ipfs/go-ipfs/core"
1312
"github.com/ipfs/go-ipfs/core/coreunix"
1413
uio "github.com/ipfs/go-ipfs/unixfs/io"
14+
cid "gx/ipfs/QmfSc2xehWmWLnwwYR91Y8QF4xdASypTFVknutoKQS3GHp/go-cid"
1515
)
1616

1717
// initDocPaths lists the paths for the docs we want to seed during --init
@@ -25,7 +25,7 @@ var initDocPaths = []string{
2525
}
2626

2727
// SeedInitDocs adds the list of embedded init documentation to the passed node, pins it and returns the root key
28-
func SeedInitDocs(nd *core.IpfsNode) (*key.Key, error) {
28+
func SeedInitDocs(nd *core.IpfsNode) (*cid.Cid, error) {
2929
return addAssetList(nd, initDocPaths)
3030
}
3131

@@ -34,11 +34,11 @@ var initDirIndex = []string{
3434
filepath.Join("..", "vendor", "dir-index-html-v1.0.0", "dir-index.html"),
3535
}
3636

37-
func SeedInitDirIndex(nd *core.IpfsNode) (*key.Key, error) {
37+
func SeedInitDirIndex(nd *core.IpfsNode) (*cid.Cid, error) {
3838
return addAssetList(nd, initDirIndex)
3939
}
4040

41-
func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {
41+
func addAssetList(nd *core.IpfsNode, l []string) (*cid.Cid, error) {
4242
dirb := uio.NewDirectory(nd.DAG)
4343

4444
for _, p := range l {
@@ -53,14 +53,18 @@ func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {
5353
}
5454

5555
fname := filepath.Base(p)
56-
k := key.B58KeyDecode(s)
57-
if err := dirb.AddChild(nd.Context(), fname, k); err != nil {
56+
c, err := cid.Decode(s)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
if err := dirb.AddChild(nd.Context(), fname, c); err != nil {
5862
return nil, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err)
5963
}
6064
}
6165

6266
dir := dirb.GetNode()
63-
dkey, err := nd.DAG.Add(dir)
67+
dcid, err := nd.DAG.Add(dir)
6468
if err != nil {
6569
return nil, fmt.Errorf("assets: DAG.Add(dir) failed: %s", err)
6670
}
@@ -73,5 +77,5 @@ func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {
7377
return nil, fmt.Errorf("assets: Pinning flush failed: %s", err)
7478
}
7579

76-
return &dkey, nil
80+
return dcid, nil
7781
}

blocks/blocks.go

+8-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,17 @@ import (
77
"fmt"
88

99
key "github.com/ipfs/go-ipfs/blocks/key"
10+
1011
mh "gx/ipfs/QmYf7ng2hG5XBtJA3tN34DQ2GUN5HNksEw1rLDkmr6vGku/go-multihash"
1112
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
13+
cid "gx/ipfs/QmfSc2xehWmWLnwwYR91Y8QF4xdASypTFVknutoKQS3GHp/go-cid"
1214
)
1315

1416
var ErrWrongHash = errors.New("data did not match given hash!")
1517

1618
type Block interface {
1719
Multihash() mh.Multihash
18-
Data() []byte
20+
RawData() []byte
1921
Key() key.Key
2022
String() string
2123
Loggable() map[string]interface{}
@@ -49,10 +51,14 @@ func (b *BasicBlock) Multihash() mh.Multihash {
4951
return b.multihash
5052
}
5153

52-
func (b *BasicBlock) Data() []byte {
54+
func (b *BasicBlock) RawData() []byte {
5355
return b.data
5456
}
5557

58+
func (b *BasicBlock) Cid() *cid.Cid {
59+
return cid.NewCidV0(b.multihash)
60+
}
61+
5662
// Key returns the block's Multihash as a Key value.
5763
func (b *BasicBlock) Key() key.Key {
5864
return key.Key(b.multihash)

blocks/blocks_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func TestData(t *testing.T) {
2525
data := []byte("some data")
2626
block := NewBlock(data)
2727

28-
if !bytes.Equal(block.Data(), data) {
28+
if !bytes.Equal(block.RawData(), data) {
2929
t.Error("data is wrong")
3030
}
3131
}

blocks/blockstore/arc_cache.go

+5-3
Original file line numberDiff line numberDiff line change
@@ -99,12 +99,14 @@ func (b *arccache) PutMany(bs []blocks.Block) error {
9999
good = append(good, block)
100100
}
101101
}
102-
err := b.blockstore.PutMany(bs)
102+
103+
err := b.blockstore.PutMany(good)
103104
if err != nil {
104105
return err
105106
}
106-
for _, block := range bs {
107-
b.arc.Add(block.Key(), true)
107+
108+
for _, blk := range good {
109+
b.arc.Add(blk.Key(), true)
108110
}
109111
return nil
110112
}

blocks/blockstore/blockstore.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func (bs *blockstore) Put(block blocks.Block) error {
117117
if err == nil && exists {
118118
return nil // already stored.
119119
}
120-
return bs.datastore.Put(k, block.Data())
120+
return bs.datastore.Put(k, block.RawData())
121121
}
122122

123123
func (bs *blockstore) PutMany(blocks []blocks.Block) error {
@@ -132,7 +132,7 @@ func (bs *blockstore) PutMany(blocks []blocks.Block) error {
132132
continue
133133
}
134134

135-
err = t.Put(k, b.Data())
135+
err = t.Put(k, b.RawData())
136136
if err != nil {
137137
return err
138138
}

blocks/blockstore/blockstore_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestPutThenGetBlock(t *testing.T) {
4848
if err != nil {
4949
t.Fatal(err)
5050
}
51-
if !bytes.Equal(block.Data(), blockFromBlockstore.Data()) {
51+
if !bytes.Equal(block.RawData(), blockFromBlockstore.RawData()) {
5252
t.Fail()
5353
}
5454
}

blocks/blockstore/bloom_cache.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,11 @@ func (b *bloomcache) PutMany(bs []blocks.Block) error {
132132
good = append(good, block)
133133
}
134134
}
135-
err := b.blockstore.PutMany(bs)
135+
136+
err := b.blockstore.PutMany(good)
136137
if err == nil {
137-
for _, block := range bs {
138-
b.bloom.AddTS([]byte(block.Key()))
138+
for _, blk := range good {
139+
b.bloom.AddTS([]byte(blk.Key()))
139140
}
140141
}
141142
return err

blockservice/blockservice.go

+58-29
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ package blockservice
55

66
import (
77
"errors"
8+
"fmt"
89

910
blocks "github.com/ipfs/go-ipfs/blocks"
1011
"github.com/ipfs/go-ipfs/blocks/blockstore"
1112
key "github.com/ipfs/go-ipfs/blocks/key"
1213
exchange "github.com/ipfs/go-ipfs/exchange"
14+
1315
logging "gx/ipfs/QmSpJByNKFX1sCsHBEp3R73FL4NF6FnQTEGyNAXHm2GS52/go-log"
1416
context "gx/ipfs/QmZy2y8t9zQH2a1b8q2ZSLKp17ATuJoCNxxyMFG5qFExpt/go-net/context"
17+
cid "gx/ipfs/QmfSc2xehWmWLnwwYR91Y8QF4xdASypTFVknutoKQS3GHp/go-cid"
1518
)
1619

1720
var log = logging.Logger("blockservice")
@@ -27,6 +30,12 @@ type BlockService struct {
2730
Exchange exchange.Interface
2831
}
2932

33+
// an Object is simply a typed block
34+
type Object interface {
35+
Cid() *cid.Cid
36+
blocks.Block
37+
}
38+
3039
// NewBlockService creates a BlockService with given datastore instance.
3140
func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
3241
if rem == nil {
@@ -41,30 +50,41 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
4150

4251
// AddBlock adds a particular block to the service, Putting it into the datastore.
4352
// TODO pass a context into this if the remote.HasBlock is going to remain here.
44-
func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
45-
k := b.Key()
46-
has, err := s.Blockstore.Has(k)
53+
func (s *BlockService) AddObject(o Object) (*cid.Cid, error) {
54+
// TODO: while this is a great optimization, we should think about the
55+
// possibility of streaming writes directly to disk. If we can pass this object
56+
// all the way down to the datastore without having to 'buffer' its data,
57+
// we could implement a `WriteTo` method on it that could do a streaming write
58+
// of the content, saving us (probably) considerable memory.
59+
c := o.Cid()
60+
has, err := s.Blockstore.Has(key.Key(c.Hash()))
4761
if err != nil {
48-
return k, err
62+
return nil, err
4963
}
64+
5065
if has {
51-
return k, nil
66+
return c, nil
5267
}
5368

54-
err = s.Blockstore.Put(b)
69+
err = s.Blockstore.Put(o)
5570
if err != nil {
56-
return k, err
71+
return nil, err
5772
}
58-
if err := s.Exchange.HasBlock(b); err != nil {
59-
return "", errors.New("blockservice is closed")
73+
74+
if err := s.Exchange.HasBlock(o); err != nil {
75+
return nil, errors.New("blockservice is closed")
6076
}
61-
return k, nil
77+
78+
return c, nil
6279
}
6380

64-
func (s *BlockService) AddBlocks(bs []blocks.Block) ([]key.Key, error) {
81+
func (s *BlockService) AddObjects(bs []Object) ([]*cid.Cid, error) {
6582
var toput []blocks.Block
83+
var toputcids []*cid.Cid
6684
for _, b := range bs {
67-
has, err := s.Blockstore.Has(b.Key())
85+
c := b.Cid()
86+
87+
has, err := s.Blockstore.Has(key.Key(c.Hash()))
6888
if err != nil {
6989
return nil, err
7090
}
@@ -74,33 +94,33 @@ func (s *BlockService) AddBlocks(bs []blocks.Block) ([]key.Key, error) {
7494
}
7595

7696
toput = append(toput, b)
97+
toputcids = append(toputcids, c)
7798
}
7899

79100
err := s.Blockstore.PutMany(toput)
80101
if err != nil {
81102
return nil, err
82103
}
83104

84-
var ks []key.Key
85-
for _, b := range toput {
86-
if err := s.Exchange.HasBlock(b); err != nil {
87-
return nil, errors.New("blockservice is closed")
105+
var ks []*cid.Cid
106+
for _, o := range toput {
107+
if err := s.Exchange.HasBlock(o); err != nil {
108+
return nil, fmt.Errorf("blockservice is closed (%s)", err)
88109
}
89-
ks = append(ks, b.Key())
110+
111+
c := o.(Object).Cid() // cast is safe, we created these
112+
ks = append(ks, c)
90113
}
91114
return ks, nil
92115
}
93116

94117
// GetBlock retrieves a particular block from the service,
95118
// Getting it from the datastore using the key (hash).
96-
func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (blocks.Block, error) {
97-
if k == "" {
98-
log.Debug("BlockService GetBlock: Nil Key")
99-
return nil, ErrNotFound
100-
}
119+
func (s *BlockService) GetBlock(ctx context.Context, c *cid.Cid) (blocks.Block, error) {
120+
log.Debugf("BlockService GetBlock: '%s'", c)
101121

102-
log.Debugf("BlockService GetBlock: '%s'", k)
103-
block, err := s.Blockstore.Get(k)
122+
// TODO: blockstore shouldnt care about Cids, need an easier way to strip the abstraction
123+
block, err := s.Blockstore.Get(key.Key(c.Hash()))
104124
if err == nil {
105125
return block, nil
106126
}
@@ -109,7 +129,7 @@ func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (blocks.Block, e
109129
// TODO be careful checking ErrNotFound. If the underlying
110130
// implementation changes, this will break.
111131
log.Debug("Blockservice: Searching bitswap")
112-
blk, err := s.Exchange.GetBlock(ctx, k)
132+
blk, err := s.Exchange.GetBlock(ctx, key.Key(c.Hash()))
113133
if err != nil {
114134
if err == blockstore.ErrNotFound {
115135
return nil, ErrNotFound
@@ -130,12 +150,13 @@ func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (blocks.Block, e
130150
// GetBlocks gets a list of blocks asynchronously and returns through
131151
// the returned channel.
132152
// NB: No guarantees are made about order.
133-
func (s *BlockService) GetBlocks(ctx context.Context, ks []key.Key) <-chan blocks.Block {
153+
func (s *BlockService) GetBlocks(ctx context.Context, ks []*cid.Cid) <-chan blocks.Block {
134154
out := make(chan blocks.Block, 0)
135155
go func() {
136156
defer close(out)
137157
var misses []key.Key
138-
for _, k := range ks {
158+
for _, c := range ks {
159+
k := key.Key(c.Hash())
139160
hit, err := s.Blockstore.Get(k)
140161
if err != nil {
141162
misses = append(misses, k)
@@ -171,11 +192,19 @@ func (s *BlockService) GetBlocks(ctx context.Context, ks []key.Key) <-chan block
171192
}
172193

173194
// DeleteBlock deletes a block in the blockservice from the datastore
174-
func (s *BlockService) DeleteBlock(k key.Key) error {
175-
return s.Blockstore.DeleteBlock(k)
195+
func (s *BlockService) DeleteObject(o Object) error {
196+
return s.Blockstore.DeleteBlock(o.Key())
176197
}
177198

178199
func (s *BlockService) Close() error {
179200
log.Debug("blockservice is shutting down...")
180201
return s.Exchange.Close()
181202
}
203+
204+
type RawBlockObject struct {
205+
blocks.Block
206+
}
207+
208+
func (rob *RawBlockObject) Cid() *cid.Cid {
209+
return cid.NewCidV0(rob.Block.Multihash())
210+
}

0 commit comments

Comments
 (0)