本文整理汇总了Golang中github.com/ipfs/go-ipfs/blocks.Block.Cid方法的典型用法代码示例。如果您正苦于以下问题:Golang Block.Cid方法的具体用法?Golang Block.Cid怎么用?Golang Block.Cid使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类github.com/ipfs/go-ipfs/blocks.Block
的用法示例。
在下文中一共展示了Block.Cid方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: HasBlock
// 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
}
示例2: assertBlocksEqual
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")
}
}
示例3: getOrFail
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()
}
示例4: Put
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())
}
示例5: Put
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
}
示例6: Put
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
}
示例7: addBlock
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()
}
}
示例8: updateReceiveCounters
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
}
示例9: decodeBlock
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())
}
}
示例10: AddBlock
// 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
}
示例11: DeleteBlock
// DeleteBlock deletes a block in the blockservice from the datastore
func (s *blockService) DeleteBlock(o blocks.Block) error {
return s.blockstore.DeleteBlock(o.Cid())
}
示例12: Publish
func (ps *impl) Publish(block blocks.Block) {
ps.wrapped.Pub(block, block.Cid().KeyString())
}
示例13: AddBlock
func (m *impl) AddBlock(b blocks.Block) {
m.blocks[b.Cid().KeyString()] = b
}