Skip to content

Commit 582e5de

Browse files
committed
blockservice: don't store blocks we already have
License: MIT Signed-off-by: Jeromy <[email protected]>
1 parent 8830aae commit 582e5de

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

blockservice/blockservice.go

+25-3
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,15 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
4343
// TODO pass a context into this if the remote.HasBlock is going to remain here.
4444
func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
4545
k := b.Key()
46-
err := s.Blockstore.Put(b)
46+
has, err := s.Blockstore.Has(k)
47+
if err != nil {
48+
return k, err
49+
}
50+
if has {
51+
return k, nil
52+
}
53+
54+
err = s.Blockstore.Put(b)
4755
if err != nil {
4856
return k, err
4957
}
@@ -54,13 +62,27 @@ func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
5462
}
5563

5664
func (s *BlockService) AddBlocks(bs []blocks.Block) ([]key.Key, error) {
57-
err := s.Blockstore.PutMany(bs)
65+
var toput []blocks.Block
66+
for _, b := range bs {
67+
has, err := s.Blockstore.Has(b.Key())
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
if has {
73+
continue
74+
}
75+
76+
toput = append(toput, b)
77+
}
78+
79+
err := s.Blockstore.PutMany(toput)
5880
if err != nil {
5981
return nil, err
6082
}
6183

6284
var ks []key.Key
63-
for _, b := range bs {
85+
for _, b := range toput {
6486
if err := s.Exchange.HasBlock(b); err != nil {
6587
return nil, errors.New("blockservice is closed")
6688
}

exchange/bitswap/workers.go

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func (bs *Bitswap) provideCollector(ctx context.Context) {
133133
log.Debug("newBlocks channel closed")
134134
return
135135
}
136+
136137
if keysOut == nil {
137138
nextKey = blk.Key()
138139
keysOut = bs.provideKeys

0 commit comments

Comments
 (0)