// HasBlock announces the existance of a block to this bitswap service. The // service will potentially notify its peers. func (bs *Bitswap) HasBlock(blk blocks.Block) error { select { case <-bs.process.Closing(): return errors.New("bitswap is closed") default: } err := bs.blockstore.Put(blk) if err != nil { log.Errorf("Error writing block to datastore: %s", err) return err } // NOTE: There exists the possiblity for a race condition here. If a user // creates a node, then adds it to the dagservice while another goroutine // is waiting on a GetBlock for that object, they will receive a reference // to the same node. We should address this soon, but i'm not going to do // it now as it requires more thought and isnt causing immediate problems. bs.notifications.Publish(blk) bs.engine.AddBlock(blk) select { case bs.newBlocks <- blk.Cid(): // send block off to be reprovided case <-bs.process.Closing(): return bs.process.Close() } return nil }
func assertBlocksEqual(t *testing.T, a, b blocks.Block) { if !bytes.Equal(a.RawData(), b.RawData()) { t.Fatal("blocks aren't equal") } if a.Cid() != b.Cid() { t.Fatal("block keys aren't equal") } }
func getOrFail(bitswap Instance, b blocks.Block, t *testing.T, wg *sync.WaitGroup) { if _, err := bitswap.Blockstore().Get(b.Cid()); err != nil { _, err := bitswap.Exchange.GetBlock(context.Background(), b.Cid()) if err != nil { t.Fatal(err) } } wg.Done() }
func (bs *blockstore) Put(block blocks.Block) error { k := dshelp.CidToDsKey(block.Cid()) // Has is cheaper than Put, so see if we already have it exists, err := bs.datastore.Has(k) if err == nil && exists { return nil // already stored. } return bs.datastore.Put(k, block.RawData()) }
func (b *bloomcache) Put(bl blocks.Block) error { if has, ok := b.hasCached(bl.Cid()); ok && has { return nil } err := b.blockstore.Put(bl) if err == nil { b.bloom.AddTS(bl.Cid().Bytes()) } return err }
func (b *arccache) Put(bl blocks.Block) error { if has, ok := b.hasCached(bl.Cid()); ok && has { return nil } err := b.blockstore.Put(bl) if err == nil { b.addCache(bl.Cid(), true) } return err }
func (e *Engine) addBlock(block blocks.Block) { work := false for _, l := range e.ledgerMap { l.lk.Lock() if entry, ok := l.WantListContains(block.Cid()); ok { e.peerRequestQueue.Push(entry, l.Partner) work = true } l.lk.Unlock() } if work { e.signalNewWork() } }
func (bs *Bitswap) updateReceiveCounters(b blocks.Block) error { bs.counterLk.Lock() defer bs.counterLk.Unlock() bs.blocksRecvd++ has, err := bs.blockstore.Has(b.Cid()) if err != nil { log.Infof("blockstore.Has error: %s", err) return err } if err == nil && has { bs.dupBlocksRecvd++ bs.dupDataRecvd += uint64(len(b.RawData())) } if has { return ErrAlreadyHaveBlock } return nil }
func decodeBlock(b blocks.Block) (node.Node, error) { c := b.Cid() switch c.Type() { case cid.Protobuf: decnd, err := DecodeProtobuf(b.RawData()) if err != nil { if strings.Contains(err.Error(), "Unmarshal failed") { return nil, fmt.Errorf("The block referred to by '%s' was not a valid merkledag node", c) } return nil, fmt.Errorf("Failed to decode Protocol Buffers: %v", err) } decnd.cached = b.Cid() return decnd, nil case cid.Raw: return NewRawNode(b.RawData()), nil case cid.CBOR: return ipldcbor.Decode(b.RawData()) default: return nil, fmt.Errorf("unrecognized object type: %s", c.Type()) } }
// AddBlock adds a particular block to the service, Putting it into the datastore. // TODO pass a context into this if the remote.HasBlock is going to remain here. func (s *blockService) AddBlock(o blocks.Block) (*cid.Cid, error) { c := o.Cid() if s.checkFirst { has, err := s.blockstore.Has(c) if err != nil { return nil, err } if has { return c, nil } } err := s.blockstore.Put(o) if err != nil { return nil, err } if err := s.exchange.HasBlock(o); err != nil { return nil, errors.New("blockservice is closed") } return c, nil }
// DeleteBlock deletes a block in the blockservice from the datastore func (s *blockService) DeleteBlock(o blocks.Block) error { return s.blockstore.DeleteBlock(o.Cid()) }
func (ps *impl) Publish(block blocks.Block) { ps.wrapped.Pub(block, block.Cid().KeyString()) }
func (m *impl) AddBlock(b blocks.Block) { m.blocks[b.Cid().KeyString()] = b }