Commit be6f25b0 authored by Jeromy's avatar Jeromy

blockservice: don't store blocks we already have

License: MIT
Signed-off-by: default avatarJeromy <why@ipfs.io>
parent 63cbf6e4
......@@ -43,7 +43,15 @@ func New(bs blockstore.Blockstore, rem exchange.Interface) *BlockService {
// TODO pass a context into this if the remote.HasBlock is going to remain here.
func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
k := b.Key()
err := s.Blockstore.Put(b)
has, err := s.Blockstore.Has(k)
if err != nil {
return k, err
}
if has {
return k, nil
}
err = s.Blockstore.Put(b)
if err != nil {
return k, err
}
......@@ -54,13 +62,27 @@ func (s *BlockService) AddBlock(b blocks.Block) (key.Key, error) {
}
func (s *BlockService) AddBlocks(bs []blocks.Block) ([]key.Key, error) {
err := s.Blockstore.PutMany(bs)
var toput []blocks.Block
for _, b := range bs {
has, err := s.Blockstore.Has(b.Key())
if err != nil {
return nil, err
}
if has {
continue
}
toput = append(toput, b)
}
err := s.Blockstore.PutMany(toput)
if err != nil {
return nil, err
}
var ks []key.Key
for _, b := range bs {
for _, b := range toput {
if err := s.Exchange.HasBlock(b); err != nil {
return nil, errors.New("blockservice is closed")
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment