@cryptotaxi247 / kubo / commits / b84cbec2b

Make blocks.Block an interface.

License: MIT Signed-off-by: Kevin Atkinson <k@kevina.org>

Kevin Atkinson committed May 5, 2016 at 18:00 UTC b84cbec2b64fb9f50f973dd862cfde5c57f4a002
22 files changed +114 -98
blocks/blocks.go
+27 -11
@@ -11,40 +11,56 @@ import (
11 u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
12 )
13
14 +type Block interface {
15 + Multihash() mh.Multihash
16 + Data() []byte
17 + Key() key.Key
18 + String() string
19 + Loggable() map[string]interface{}
20 +}
21 +
22 // Block is a singular block of data in ipfs
15 -type Block struct {
16 - Multihash mh.Multihash
17 - Data []byte
23 +type RawBlock struct {
24 + multihash mh.Multihash
25 + data []byte
26 }
27
28 // NewBlock creates a Block object from opaque data. It will hash the data.
21 -func NewBlock(data []byte) *Block {
22 - return &Block{Data: data, Multihash: u.Hash(data)}
29 +func NewBlock(data []byte) *RawBlock {
30 + return &RawBlock{data: data, multihash: u.Hash(data)}
31 }
32
33 // NewBlockWithHash creates a new block when the hash of the data
34 // is already known, this is used to save time in situations where
35 // we are able to be confident that the data is correct
28 -func NewBlockWithHash(data []byte, h mh.Multihash) (*Block, error) {
36 +func NewBlockWithHash(data []byte, h mh.Multihash) (*RawBlock, error) {
37 if u.Debug {
38 chk := u.Hash(data)
39 if string(chk) != string(h) {
40 return nil, errors.New("Data did not match given hash!")
41 }
42 }
35 - return &Block{Data: data, Multihash: h}, nil
43 + return &RawBlock{data: data, multihash: h}, nil
44 +}
45 +
46 +func (b *RawBlock) Multihash() mh.Multihash {
47 + return b.multihash
48 +}
49 +
50 +func (b *RawBlock) Data() []byte {
51 + return b.data
52 }
53
54 // Key returns the block's Multihash as a Key value.
39 -func (b *Block) Key() key.Key {
40 - return key.Key(b.Multihash)
55 +func (b *RawBlock) Key() key.Key {
56 + return key.Key(b.multihash)
57 }
58
43 -func (b *Block) String() string {
59 +func (b *RawBlock) String() string {
60 return fmt.Sprintf("[Block %s]", b.Key())
61 }
62
47 -func (b *Block) Loggable() map[string]interface{} {
63 +func (b *RawBlock) Loggable() map[string]interface{} {
64 return map[string]interface{}{
65 "block": b.Key().String(),
66 }
blocks/blockstore/blockstore.go
+8 -8
@@ -30,9 +30,9 @@ var ErrNotFound = errors.New("blockstore: block not found")
30 type Blockstore interface {
31 DeleteBlock(key.Key) error
32 Has(key.Key) (bool, error)
33 - Get(key.Key) (*blocks.Block, error)
34 - Put(*blocks.Block) error
35 - PutMany([]*blocks.Block) error
33 + Get(key.Key) (blocks.Block, error)
34 + Put(blocks.Block) error
35 + PutMany([]blocks.Block) error
36
37 AllKeysChan(ctx context.Context) (<-chan key.Key, error)
38 }
@@ -73,7 +73,7 @@ type blockstore struct {
73 gcreqlk sync.Mutex
74 }
75
76 -func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
76 +func (bs *blockstore) Get(k key.Key) (blocks.Block, error) {
77 maybeData, err := bs.datastore.Get(k.DsKey())
78 if err == ds.ErrNotFound {
79 return nil, ErrNotFound
@@ -89,7 +89,7 @@ func (bs *blockstore) Get(k key.Key) (*blocks.Block, error) {
89 return blocks.NewBlockWithHash(bdata, mh.Multihash(k))
90 }
91
92 -func (bs *blockstore) Put(block *blocks.Block) error {
92 +func (bs *blockstore) Put(block blocks.Block) error {
93 k := block.Key().DsKey()
94
95 // Has is cheaper than Put, so see if we already have it
@@ -97,10 +97,10 @@ func (bs *blockstore) Put(block *blocks.Block) error {
97 if err == nil && exists {
98 return nil // already stored.
99 }
100 - return bs.datastore.Put(k, block.Data)
100 + return bs.datastore.Put(k, block.Data())
101 }
102
103 -func (bs *blockstore) PutMany(blocks []*blocks.Block) error {
103 +func (bs *blockstore) PutMany(blocks []blocks.Block) error {
104 t, err := bs.datastore.Batch()
105 if err != nil {
106 return err
@@ -112,7 +112,7 @@ func (bs *blockstore) PutMany(blocks []*blocks.Block) error {
112 continue
113 }
114
115 - err = t.Put(k, b.Data)
115 + err = t.Put(k, b.Data())
116 if err != nil {
117 return err
118 }
blocks/blockstore/blockstore_test.go
+1 -1
@@ -40,7 +40,7 @@ func TestPutThenGetBlock(t *testing.T) {
40 if err != nil {
41 t.Fatal(err)
42 }
43 - if !bytes.Equal(block.Data, blockFromBlockstore.Data) {
43 + if !bytes.Equal(block.Data(), blockFromBlockstore.Data()) {
44 t.Fail()
45 }
46 }
blocks/blockstore/write_cache.go
+4 -4
@@ -34,11 +34,11 @@ func (w *writecache) Has(k key.Key) (bool, error) {
34 return w.blockstore.Has(k)
35 }
36
37 -func (w *writecache) Get(k key.Key) (*blocks.Block, error) {
37 +func (w *writecache) Get(k key.Key) (blocks.Block, error) {
38 return w.blockstore.Get(k)
39 }
40
41 -func (w *writecache) Put(b *blocks.Block) error {
41 +func (w *writecache) Put(b blocks.Block) error {
42 k := b.Key()
43 if _, ok := w.cache.Get(k); ok {
44 return nil
@@ -49,8 +49,8 @@ func (w *writecache) Put(b *blocks.Block) error {
49 return w.blockstore.Put(b)
50 }
51
52 -func (w *writecache) PutMany(bs []*blocks.Block) error {
53 - var good []*blocks.Block
52 +func (w *writecache) PutMany(bs []blocks.Block) error {
53 + var good []blocks.Block
54 for _, b := range bs {
55 if _, ok := w.cache.Get(b.Key()); !ok {
56 good = append(good, b)
blocks/blocksutil/block_generator.go
+3 -3
@@ -10,13 +10,13 @@ type BlockGenerator struct {
10 seq int
11 }
12
13 -func (bg *BlockGenerator) Next() *blocks.Block {
13 +func (bg *BlockGenerator) Next() blocks.Block {
14 bg.seq++
15 return blocks.NewBlock([]byte(string(bg.seq)))
16 }
17
18 -func (bg *BlockGenerator) Blocks(n int) []*blocks.Block {
19 - blocks := make([]*blocks.Block, 0)
18 +func (bg *BlockGenerator) Blocks(n int) []blocks.Block {
19 + blocks := make([]blocks.Block, 0)
20 for i := 0; i < n; i++ {
21 b := bg.Next()
22 blocks = append(blocks, b)
blockservice/blockservice.go
+5 -5
@@ -41,7 +41,7 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
41
42 // AddBlock adds a particular block to the service, Putting it into the datastore.
43 // 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) {
44 +func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
45 k := b.Key()
46 err := s.Blockstore.Put(b)
47 if err != nil {
@@ -53,7 +53,7 @@ func (s *BlockService) AddBlock(b *blocks.Block) (key.Key, error) {
53 return k, nil
54 }
55
56 -func (s *BlockService) AddBlocks(bs []*blocks.Block) ([]key.Key, error) {
56 +func (s *BlockService) AddBlocks(bs []blocks.Block) ([]key.Key, error) {
57 err := s.Blockstore.PutMany(bs)
58 if err != nil {
59 return nil, err
@@ -71,7 +71,7 @@ func (s *BlockService) AddBlocks(bs []*blocks.Block) ([]key.Key, error) {
71
72 // GetBlock retrieves a particular block from the service,
73 // Getting it from the datastore using the key (hash).
74 -func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (*blocks.Block, error) {
74 +func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (blocks.Block, error) {
75 log.Debugf("BlockService GetBlock: '%s'", k)
76 block, err := s.Blockstore.Get(k)
77 if err == nil {
@@ -103,8 +103,8 @@ func (s *BlockService) GetBlock(ctx context.Context, k key.Key) (*blocks.Block,
103 // GetBlocks gets a list of blocks asynchronously and returns through
104 // the returned channel.
105 // NB: No guarantees are made about order.
106 -func (s *BlockService) GetBlocks(ctx context.Context, ks []key.Key) <-chan *blocks.Block {
107 - out := make(chan *blocks.Block, 0)
106 +func (s *BlockService) GetBlocks(ctx context.Context, ks []key.Key) <-chan blocks.Block {
107 + out := make(chan blocks.Block, 0)
108 go func() {
109 defer close(out)
110 var misses []key.Key
blockservice/test/blocks_test.go
+3 -3
@@ -24,7 +24,7 @@ func TestBlocks(t *testing.T) {
24
25 b := blocks.NewBlock([]byte("beep boop"))
26 h := u.Hash([]byte("beep boop"))
27 - if !bytes.Equal(b.Multihash, h) {
27 + if !bytes.Equal(b.Multihash(), h) {
28 t.Error("Block Multihash and data multihash not equal")
29 }
30
@@ -54,7 +54,7 @@ func TestBlocks(t *testing.T) {
54 t.Error("Block keys not equal.")
55 }
56
57 - if !bytes.Equal(b.Data, b2.Data) {
57 + if !bytes.Equal(b.Data(), b2.Data()) {
58 t.Error("Block data is not equal.")
59 }
60 }
@@ -79,7 +79,7 @@ func TestGetBlocksSequential(t *testing.T) {
79 ctx, cancel := context.WithTimeout(context.Background(), time.Second*50)
80 defer cancel()
81 out := servs[i].GetBlocks(ctx, keys)
82 - gotten := make(map[key.Key]*blocks.Block)
82 + gotten := make(map[key.Key]blocks.Block)
83 for blk := range out {
84 if _, ok := gotten[blk.Key()]; ok {
85 t.Fatal("Got duplicate block!")
core/commands/block.go
+3 -3
@@ -66,7 +66,7 @@ on raw ipfs blocks. It outputs the following to stdout:
66
67 res.SetOutput(&BlockStat{
68 Key: b.Key().B58String(),
69 - Size: len(b.Data),
69 + Size: len(b.Data()),
70 })
71 },
72 Type: BlockStat{},
@@ -97,7 +97,7 @@ It outputs to stdout, and <key> is a base58 encoded multihash.
97 return
98 }
99
100 - res.SetOutput(bytes.NewReader(b.Data))
100 + res.SetOutput(bytes.NewReader(b.Data()))
101 },
102 }
103
@@ -161,7 +161,7 @@ It reads from stdin, and <key> is a base58 encoded multihash.
161 Type: BlockStat{},
162 }
163
164 -func getBlockForKey(req cmds.Request, skey string) (*blocks.Block, error) {
164 +func getBlockForKey(req cmds.Request, skey string) (blocks.Block, error) {
165 n, err := req.InvocContext().GetNode()
166 if err != nil {
167 return nil, err
exchange/bitswap/bitswap.go
+10 -10
@@ -90,7 +90,7 @@ func New(parent context.Context, p peer.ID, network bsnet.BitSwapNetwork,
90 network: network,
91 findKeys: make(chan *wantlist.Entry, sizeBatchRequestChan),
92 process: px,
93 - newBlocks: make(chan *blocks.Block, HasBlockBufferSize),
93 + newBlocks: make(chan blocks.Block, HasBlockBufferSize),
94 provideKeys: make(chan key.Key, provideKeysBufferSize),
95 wm: NewWantManager(ctx, network),
96 }
@@ -137,7 +137,7 @@ type Bitswap struct {
137
138 process process.Process
139
140 - newBlocks chan *blocks.Block
140 + newBlocks chan blocks.Block
141
142 provideKeys chan key.Key
143
@@ -154,7 +154,7 @@ type blockRequest struct {
154
155 // GetBlock attempts to retrieve a particular block from peers within the
156 // deadline enforced by the context.
157 -func (bs *Bitswap) GetBlock(parent context.Context, k key.Key) (*blocks.Block, error) {
157 +func (bs *Bitswap) GetBlock(parent context.Context, k key.Key) (blocks.Block, error) {
158
159 // Any async work initiated by this function must end when this function
160 // returns. To ensure this, derive a new context. Note that it is okay to
@@ -209,9 +209,9 @@ func (bs *Bitswap) WantlistForPeer(p peer.ID) []key.Key {
209 // NB: Your request remains open until the context expires. To conserve
210 // resources, provide a context with a reasonably short deadline (ie. not one
211 // that lasts throughout the lifetime of the server)
212 -func (bs *Bitswap) GetBlocks(ctx context.Context, keys []key.Key) (<-chan *blocks.Block, error) {
212 +func (bs *Bitswap) GetBlocks(ctx context.Context, keys []key.Key) (<-chan blocks.Block, error) {
213 if len(keys) == 0 {
214 - out := make(chan *blocks.Block)
214 + out := make(chan blocks.Block)
215 close(out)
216 return out, nil
217 }
@@ -251,7 +251,7 @@ func (bs *Bitswap) CancelWants(ks []key.Key) {
251
252 // HasBlock announces the existance of a block to this bitswap service. The
253 // service will potentially notify its peers.
254 -func (bs *Bitswap) HasBlock(blk *blocks.Block) error {
254 +func (bs *Bitswap) HasBlock(blk blocks.Block) error {
255 select {
256 case <-bs.process.Closing():
257 return errors.New("bitswap is closed")
@@ -277,7 +277,7 @@ func (bs *Bitswap) HasBlock(blk *blocks.Block) error {
277 return nil
278 }
279
280 -func (bs *Bitswap) tryPutBlock(blk *blocks.Block, attempts int) error {
280 +func (bs *Bitswap) tryPutBlock(blk blocks.Block, attempts int) error {
281 var err error
282 for i := 0; i < attempts; i++ {
283 if err = bs.blockstore.Put(blk); err == nil {
@@ -316,7 +316,7 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
316 wg := sync.WaitGroup{}
317 for _, block := range iblocks {
318 wg.Add(1)
319 - go func(b *blocks.Block) {
319 + go func(b blocks.Block) {
320 defer wg.Done()
321
322 if err := bs.updateReceiveCounters(b); err != nil {
@@ -337,7 +337,7 @@ func (bs *Bitswap) ReceiveMessage(ctx context.Context, p peer.ID, incoming bsmsg
337
338 var ErrAlreadyHaveBlock = errors.New("already have block")
339
340 -func (bs *Bitswap) updateReceiveCounters(b *blocks.Block) error {
340 +func (bs *Bitswap) updateReceiveCounters(b blocks.Block) error {
341 bs.counterLk.Lock()
342 defer bs.counterLk.Unlock()
343 bs.blocksRecvd++
@@ -348,7 +348,7 @@ func (bs *Bitswap) updateReceiveCounters(b *blocks.Block) error {
348 }
349 if err == nil && has {
350 bs.dupBlocksRecvd++
351 - bs.dupDataRecvd += uint64(len(b.Data))
351 + bs.dupDataRecvd += uint64(len(b.Data()))
352 }
353
354 if has {
exchange/bitswap/bitswap_test.go
+2 -2
@@ -85,7 +85,7 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
85 t.Fatal("Expected to succeed")
86 }
87
88 - if !bytes.Equal(block.Data, received.Data) {
88 + if !bytes.Equal(block.Data(), received.Data()) {
89 t.Fatal("Data doesn't match")
90 }
91 }
@@ -218,7 +218,7 @@ func PerformDistributionTest(t *testing.T, numInstances, numBlocks int) {
218 }
219 }
220
221 -func getOrFail(bitswap Instance, b *blocks.Block, t *testing.T, wg *sync.WaitGroup) {
221 +func getOrFail(bitswap Instance, b blocks.Block, t *testing.T, wg *sync.WaitGroup) {
222 if _, err := bitswap.Blockstore().Get(b.Key()); err != nil {
223 _, err := bitswap.Exchange.GetBlock(context.Background(), b.Key())
224 if err != nil {
exchange/bitswap/decision/engine.go
+6 -6
@@ -58,7 +58,7 @@ type Envelope struct {
58 Peer peer.ID
59
60 // Block is the payload
61 - Block *blocks.Block
61 + Block blocks.Block
62
63 // A callback to notify the decision queue that the task is complete
64 Sent func()
@@ -226,13 +226,13 @@ func (e *Engine) MessageReceived(p peer.ID, m bsmsg.BitSwapMessage) error {
226 }
227
228 for _, block := range m.Blocks() {
229 - log.Debugf("got block %s %d bytes", block.Key(), len(block.Data))
230 - l.ReceivedBytes(len(block.Data))
229 + log.Debugf("got block %s %d bytes", block.Key(), len(block.Data()))
230 + l.ReceivedBytes(len(block.Data()))
231 }
232 return nil
233 }
234
235 -func (e *Engine) addBlock(block *blocks.Block) {
235 +func (e *Engine) addBlock(block blocks.Block) {
236 work := false
237
238 for _, l := range e.ledgerMap {
@@ -247,7 +247,7 @@ func (e *Engine) addBlock(block *blocks.Block) {
247 }
248 }
249
250 -func (e *Engine) AddBlock(block *blocks.Block) {
250 +func (e *Engine) AddBlock(block blocks.Block) {
251 e.lock.Lock()
252 defer e.lock.Unlock()
253
@@ -266,7 +266,7 @@ func (e *Engine) MessageSent(p peer.ID, m bsmsg.BitSwapMessage) error {
266
267 l := e.findOrCreate(p)
268 for _, block := range m.Blocks() {
269 - l.SentBytes(len(block.Data))
269 + l.SentBytes(len(block.Data()))
270 l.wantList.Remove(block.Key())
271 e.peerRequestQueue.Remove(block.Key(), p)
272 }
exchange/bitswap/decision/engine_test.go
+1 -1
@@ -188,7 +188,7 @@ func checkHandledInOrder(t *testing.T, e *Engine, keys []string) error {
188 received := envelope.Block
189 expected := blocks.NewBlock([]byte(k))
190 if received.Key() != expected.Key() {
191 - return errors.New(fmt.Sprintln("received", string(received.Data), "expected", string(expected.Data)))
191 + return errors.New(fmt.Sprintln("received", string(received.Data()), "expected", string(expected.Data())))
192 }
193 }
194 return nil
exchange/bitswap/message/message.go
+8 -8
@@ -22,7 +22,7 @@ type BitSwapMessage interface {
22 Wantlist() []Entry
23
24 // Blocks returns a slice of unique blocks
25 - Blocks() []*blocks.Block
25 + Blocks() []blocks.Block
26
27 // AddEntry adds an entry to the Wantlist.
28 AddEntry(key key.Key, priority int)
@@ -34,7 +34,7 @@ type BitSwapMessage interface {
34 // A full wantlist is an authoritative copy, a 'non-full' wantlist is a patch-set
35 Full() bool
36
37 - AddBlock(*blocks.Block)
37 + AddBlock(blocks.Block)
38 Exportable
39
40 Loggable() map[string]interface{}
@@ -48,7 +48,7 @@ type Exportable interface {
48 type impl struct {
49 full bool
50 wantlist map[key.Key]Entry
51 - blocks map[key.Key]*blocks.Block
51 + blocks map[key.Key]blocks.Block
52 }
53
54 func New(full bool) BitSwapMessage {
@@ -57,7 +57,7 @@ func New(full bool) BitSwapMessage {
57
58 func newMsg(full bool) *impl {
59 return &impl{
60 - blocks: make(map[key.Key]*blocks.Block),
60 + blocks: make(map[key.Key]blocks.Block),
61 wantlist: make(map[key.Key]Entry),
62 full: full,
63 }
@@ -96,8 +96,8 @@ func (m *impl) Wantlist() []Entry {
96 return out
97 }
98
99 -func (m *impl) Blocks() []*blocks.Block {
100 - bs := make([]*blocks.Block, 0, len(m.blocks))
99 +func (m *impl) Blocks() []blocks.Block {
100 + bs := make([]blocks.Block, 0, len(m.blocks))
101 for _, block := range m.blocks {
102 bs = append(bs, block)
103 }
@@ -129,7 +129,7 @@ func (m *impl) addEntry(k key.Key, priority int, cancel bool) {
129 }
130 }
131
132 -func (m *impl) AddBlock(b *blocks.Block) {
132 +func (m *impl) AddBlock(b blocks.Block) {
133 m.blocks[b.Key()] = b
134 }
135
@@ -156,7 +156,7 @@ func (m *impl) ToProto() *pb.Message {
156 })
157 }
158 for _, b := range m.Blocks() {
159 - pbm.Blocks = append(pbm.Blocks, b.Data)
159 + pbm.Blocks = append(pbm.Blocks, b.Data())
160 }
161 return pbm
162 }
exchange/bitswap/notifications/notifications.go
+6 -6
@@ -10,8 +10,8 @@ import (
10 const bufferSize = 16
11
12 type PubSub interface {
13 - Publish(block *blocks.Block)
14 - Subscribe(ctx context.Context, keys ...key.Key) <-chan *blocks.Block
13 + Publish(block blocks.Block)
14 + Subscribe(ctx context.Context, keys ...key.Key) <-chan blocks.Block
15 Shutdown()
16 }
17
@@ -23,7 +23,7 @@ type impl struct {
23 wrapped pubsub.PubSub
24 }
25
26 -func (ps *impl) Publish(block *blocks.Block) {
26 +func (ps *impl) Publish(block blocks.Block) {
27 topic := string(block.Key())
28 ps.wrapped.Pub(block, topic)
29 }
@@ -35,9 +35,9 @@ func (ps *impl) Shutdown() {
35 // Subscribe returns a channel of blocks for the given |keys|. |blockChannel|
36 // is closed if the |ctx| times out or is cancelled, or after sending len(keys)
37 // blocks.
38 -func (ps *impl) Subscribe(ctx context.Context, keys ...key.Key) <-chan *blocks.Block {
38 +func (ps *impl) Subscribe(ctx context.Context, keys ...key.Key) <-chan blocks.Block {
39
40 - blocksCh := make(chan *blocks.Block, len(keys))
40 + blocksCh := make(chan blocks.Block, len(keys))
41 valuesCh := make(chan interface{}, len(keys)) // provide our own channel to control buffer, prevent blocking
42 if len(keys) == 0 {
43 close(blocksCh)
@@ -55,7 +55,7 @@ func (ps *impl) Subscribe(ctx context.Context, keys ...key.Key) <-chan *blocks.B
55 if !ok {
56 return
57 }
58 - block, ok := val.(*blocks.Block)
58 + block, ok := val.(blocks.Block)
59 if !ok {
60 return
61 }
exchange/bitswap/notifications/notifications_test.go
+3 -3
@@ -151,15 +151,15 @@ func TestDoesNotDeadLockIfContextCancelledBeforePublish(t *testing.T) {
151 t.Log("publishing the large number of blocks to the ignored channel must not deadlock")
152 }
153
154 -func assertBlockChannelNil(t *testing.T, blockChannel <-chan *blocks.Block) {
154 +func assertBlockChannelNil(t *testing.T, blockChannel <-chan blocks.Block) {
155 _, ok := <-blockChannel
156 if ok {
157 t.Fail()
158 }
159 }
160
161 -func assertBlocksEqual(t *testing.T, a, b *blocks.Block) {
162 - if !bytes.Equal(a.Data, b.Data) {
161 +func assertBlocksEqual(t *testing.T, a, b blocks.Block) {
162 + if !bytes.Equal(a.Data(), b.Data()) {
163 t.Fatal("blocks aren't equal")
164 }
165 if a.Key() != b.Key() {
exchange/bitswap/testnet/network_test.go
+1 -1
@@ -44,7 +44,7 @@ func TestSendMessageAsyncButWaitForResponse(t *testing.T) {
44 // TODO assert that this came from the correct peer and that the message contents are as expected
45 ok := false
46 for _, b := range msgFromResponder.Blocks() {
47 - if string(b.Data) == expectedStr {
47 + if string(b.Data()) == expectedStr {
48 wg.Done()
49 ok = true
50 }
exchange/bitswap/workers.go
+1 -1
@@ -61,7 +61,7 @@ func (bs *Bitswap) taskWorker(ctx context.Context, id int) {
61 log.Event(ctx, "Bitswap.TaskWorker.Work", logging.LoggableMap{
62 "ID": id,
63 "Target": envelope.Peer.Pretty(),
64 - "Block": envelope.Block.Multihash.B58String(),
64 + "Block": envelope.Block.Multihash().B58String(),
65 })
66
67 bs.wm.SendBlock(ctx, envelope)
exchange/interface.go
+3 -3
@@ -13,13 +13,13 @@ import (
13 // exchange protocol.
14 type Interface interface { // type Exchanger interface
15 // GetBlock returns the block associated with a given key.
16 - GetBlock(context.Context, key.Key) (*blocks.Block, error)
16 + GetBlock(context.Context, key.Key) (blocks.Block, error)
17
18 - GetBlocks(context.Context, []key.Key) (<-chan *blocks.Block, error)
18 + GetBlocks(context.Context, []key.Key) (<-chan blocks.Block, error)
19
20 // TODO Should callers be concerned with whether the block was made
21 // available on the network?
22 - HasBlock(*blocks.Block) error
22 + HasBlock(blocks.Block) error
23
24 io.Closer
25 }
exchange/offline/offline.go
+4 -4
@@ -23,12 +23,12 @@ type offlineExchange struct {
23 // GetBlock returns nil to signal that a block could not be retrieved for the
24 // given key.
25 // NB: This function may return before the timeout expires.
26 -func (e *offlineExchange) GetBlock(_ context.Context, k key.Key) (*blocks.Block, error) {
26 +func (e *offlineExchange) GetBlock(_ context.Context, k key.Key) (blocks.Block, error) {
27 return e.bs.Get(k)
28 }
29
30 // HasBlock always returns nil.
31 -func (e *offlineExchange) HasBlock(b *blocks.Block) error {
31 +func (e *offlineExchange) HasBlock(b blocks.Block) error {
32 return e.bs.Put(b)
33 }
34
@@ -39,8 +39,8 @@ func (_ *offlineExchange) Close() error {
39 return nil
40 }
41
42 -func (e *offlineExchange) GetBlocks(ctx context.Context, ks []key.Key) (<-chan *blocks.Block, error) {
43 - out := make(chan *blocks.Block, 0)
42 +func (e *offlineExchange) GetBlocks(ctx context.Context, ks []key.Key) (<-chan blocks.Block, error) {
43 + out := make(chan blocks.Block, 0)
44 go func() {
45 defer close(out)
46 var misses []key.Key
importer/chunk/rabin_test.go
+2 -2
@@ -39,10 +39,10 @@ func TestRabinChunking(t *testing.T) {
39 }
40 }
41
42 -func chunkData(t *testing.T, data []byte) map[key.Key]*blocks.Block {
42 +func chunkData(t *testing.T, data []byte) map[key.Key]blocks.Block {
43 r := NewRabin(bytes.NewReader(data), 1024*256)
44
45 - blkmap := make(map[key.Key]*blocks.Block)
45 + blkmap := make(map[key.Key]blocks.Block)
46
47 for {
48 blk, err := r.NextBytes()
merkledag/merkledag.go
+11 -11
@@ -52,13 +52,13 @@ func (n *dagService) Add(nd *Node) (key.Key, error) {
52 return "", err
53 }
54
55 - b := new(blocks.Block)
56 - b.Data = d
57 - b.Multihash, err = nd.Multihash()
55 + mh, err := nd.Multihash()
56 if err != nil {
57 return "", err
58 }
59
60 + b, _ := blocks.NewBlockWithHash(d, mh)
61 +
62 return n.Blocks.AddBlock(b)
63 }
64
@@ -82,7 +82,7 @@ func (n *dagService) Get(ctx context.Context, k key.Key) (*Node, error) {
82 return nil, fmt.Errorf("Failed to get block for %s: %v", k.B58String(), err)
83 }
84
85 - res, err := DecodeProtobuf(b.Data)
85 + res, err := DecodeProtobuf(b.Data())
86 if err != nil {
87 return nil, fmt.Errorf("Failed to decode Protocol Buffers: %v", err)
88 }
@@ -135,7 +135,7 @@ func (ds *dagService) GetMany(ctx context.Context, keys []key.Key) <-chan *NodeO
135 }
136 return
137 }
138 - nd, err := DecodeProtobuf(b.Data)
138 + nd, err := DecodeProtobuf(b.Data())
139 if err != nil {
140 out <- &NodeOption{Err: err}
141 return
@@ -316,7 +316,7 @@ func (np *nodePromise) Get(ctx context.Context) (*Node, error) {
316 type Batch struct {
317 ds *dagService
318
319 - blocks []*blocks.Block
319 + blocks []blocks.Block
320 size int
321 MaxSize int
322 }
@@ -327,17 +327,17 @@ func (t *Batch) Add(nd *Node) (key.Key, error) {
327 return "", err
328 }
329
330 - b := new(blocks.Block)
331 - b.Data = d
332 - b.Multihash, err = nd.Multihash()
330 + mh, err := nd.Multihash()
331 if err != nil {
332 return "", err
333 }
334
337 - k := key.Key(b.Multihash)
335 + b, _ := blocks.NewBlockWithHash(d, mh)
336 +
337 + k := key.Key(mh)
338
339 t.blocks = append(t.blocks, b)
340 - t.size += len(b.Data)
340 + t.size += len(b.Data())
341 if t.size > t.MaxSize {
342 return k, t.Commit()
343 }
test/integration/bitswap_wo_routing_test.go
+2 -2
@@ -71,7 +71,7 @@ func TestBitswapWithoutRouting(t *testing.T) {
71 b, err := n.Blocks.GetBlock(ctx, block0.Key())
72 if err != nil {
73 t.Error(err)
74 - } else if !bytes.Equal(b.Data, block0.Data) {
74 + } else if !bytes.Equal(b.Data(), block0.Data()) {
75 t.Error("byte comparison fail")
76 } else {
77 log.Debug("got block: %s", b.Key())
@@ -88,7 +88,7 @@ func TestBitswapWithoutRouting(t *testing.T) {
88 b, err := n.Blocks.GetBlock(ctx, block1.Key())
89 if err != nil {
90 t.Error(err)
91 - } else if !bytes.Equal(b.Data, block1.Data) {
91 + } else if !bytes.Equal(b.Data(), block1.Data()) {
92 t.Error("byte comparison fail")
93 } else {
94 log.Debug("got block: %s", b.Key())