func (rs *s) Providers(c *cid.Cid) []pstore.PeerInfo { rs.delayConf.Query.Wait() // before locking rs.lock.RLock() defer rs.lock.RUnlock() k := c.KeyString() var ret []pstore.PeerInfo records, ok := rs.providers[k] if !ok { return ret } for _, r := range records { if time.Now().Sub(r.Created) > rs.delayConf.ValueVisibility.Get() { ret = append(ret, r.Peer) } } for i := range ret { j := rand.Intn(i + 1) ret[i], ret[j] = ret[j], ret[i] } return ret }
func (bs *blockstore) Get(k *cid.Cid) (blocks.Block, error) { if k == nil { log.Error("nil cid in blockstore") return nil, ErrNotFound } maybeData, err := bs.datastore.Get(dshelp.CidToDsKey(k)) if err == ds.ErrNotFound { return nil, ErrNotFound } if err != nil { return nil, err } bdata, ok := maybeData.([]byte) if !ok { return nil, ValueTypeMismatch } if bs.rehash { rbcid, err := k.Prefix().Sum(bdata) if err != nil { return nil, err } if !rbcid.Equals(k) { return nil, ErrHashMismatch } return blocks.NewBlockWithCid(bdata, rbcid) } else { return blocks.NewBlockWithCid(bdata, k) } }
// FindLinks searches this nodes links for the given key, // returns the indexes of any links pointing to it func FindLinks(links []*cid.Cid, c *cid.Cid, start int) []int { var out []int for i, lnk_c := range links[start:] { if c.Equals(lnk_c) { out = append(out, i+start) } } return out }
func (n *dagService) GetLinks(ctx context.Context, c *cid.Cid) ([]*node.Link, error) { if c.Type() == cid.Raw { return nil, nil } node, err := n.Get(ctx, c) if err != nil { return nil, err } return node.Links(), nil }
// NewBlockWithHash creates a new block when the hash of the data // is already known, this is used to save time in situations where // we are able to be confident that the data is correct func NewBlockWithCid(data []byte, c *cid.Cid) (*BasicBlock, error) { if u.Debug { chkc, err := c.Prefix().Sum(data) if err != nil { return nil, err } if !chkc.Equals(c) { return nil, ErrWrongHash } } return &BasicBlock{data: data, cid: c}, nil }
func (w *Wantlist) Remove(c *cid.Cid) bool { k := c.KeyString() e, ok := w.set[k] if !ok { return false } e.RefCnt-- if e.RefCnt <= 0 { delete(w.set, k) return true } return false }
func (w *Wantlist) Add(c *cid.Cid, priority int) bool { k := c.KeyString() if e, ok := w.set[k]; ok { e.RefCnt++ return false } w.set[k] = &Entry{ Cid: c, Priority: priority, RefCnt: 1, } return true }
func (m *impl) addEntry(c *cid.Cid, priority int, cancel bool) { k := c.KeyString() e, exists := m.wantlist[k] if exists { e.Priority = priority e.Cancel = cancel } else { m.wantlist[k] = Entry{ Entry: &wantlist.Entry{ Cid: c, Priority: priority, }, Cancel: cancel, } } }
// if ok == false has is inconclusive // if ok == true then has respons to question: is it contained func (b *arccache) hasCached(k *cid.Cid) (has bool, ok bool) { b.total.Inc() if k == nil { log.Error("nil cid in arccache") // Return cache invalid so the call to blockstore happens // in case of invalid key and correct error is created. return false, false } h, ok := b.arc.Get(k.KeyString()) if ok { b.hits.Inc() return h.(bool), true } return false, false }
func (rs *s) Announce(p pstore.PeerInfo, c *cid.Cid) error { rs.lock.Lock() defer rs.lock.Unlock() k := c.KeyString() _, ok := rs.providers[k] if !ok { rs.providers[k] = make(map[peer.ID]providerRecord) } rs.providers[k][p.ID] = providerRecord{ Created: time.Now(), Peer: p, } return nil }
// if ok == false has is inconclusive // if ok == true then has respons to question: is it contained func (b *bloomcache) hasCached(k *cid.Cid) (has bool, ok bool) { b.total.Inc() if k == nil { log.Error("nil cid in bloom cache") // Return cache invalid so call to blockstore // in case of invalid key is forwarded deeper return false, false } if b.BloomActive() { blr := b.bloom.HasTS(k.Bytes()) if blr == false { // not contained in bloom is only conclusive answer bloom gives b.hits.Inc() return false, true } } return false, false }
// Write one edge func (rw *RefWriter) WriteEdge(from, to *cid.Cid, linkname string) error { if rw.Ctx != nil { select { case <-rw.Ctx.Done(): // just in case. return rw.Ctx.Err() default: } } var s string switch { case rw.PrintFmt != "": s = rw.PrintFmt s = strings.Replace(s, "<src>", from.String(), -1) s = strings.Replace(s, "<dst>", to.String(), -1) s = strings.Replace(s, "<linkname>", linkname, -1) default: s += to.String() } rw.out <- &RefWrapper{Ref: s} return nil }
func CidToDsKey(k *cid.Cid) ds.Key { return NewKeyFromBinary(k.KeyString()) }
func (b *arccache) addCache(c *cid.Cid, has bool) { b.arc.Add(c.KeyString(), has) }
func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) { // TODO(cryptix): move me to ServeHTTP and pass into all handlers ctx, cancel := context.WithCancel(i.node.Context()) defer cancel() rootPath, err := path.ParsePath(r.URL.Path) if err != nil { webError(w, "putHandler: IPFS path not valid", err, http.StatusBadRequest) return } rsegs := rootPath.Segments() if rsegs[0] == ipnsPathPrefix { webError(w, "putHandler: updating named entries not supported", errors.New("WritableGateway: ipns put not supported"), http.StatusBadRequest) return } var newnode node.Node if rsegs[len(rsegs)-1] == "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn" { newnode = ft.EmptyDirNode() } else { putNode, err := i.newDagFromReader(r.Body) if err != nil { webError(w, "putHandler: Could not create DAG from request", err, http.StatusInternalServerError) return } newnode = putNode } var newPath string if len(rsegs) > 1 { newPath = path.Join(rsegs[2:]) } var newcid *cid.Cid rnode, err := core.Resolve(ctx, i.node.Namesys, i.node.Resolver, rootPath) switch ev := err.(type) { case path.ErrNoLink: // ev.Node < node where resolve failed // ev.Name < new link // but we need to patch from the root c, err := cid.Decode(rsegs[1]) if err != nil { webError(w, "putHandler: bad input path", err, http.StatusBadRequest) return } rnode, err := i.node.DAG.Get(ctx, c) if err != nil { webError(w, "putHandler: Could not create DAG from request", err, http.StatusInternalServerError) return } pbnd, ok := rnode.(*dag.ProtoNode) if !ok { webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest) return } e := dagutils.NewDagEditor(pbnd, i.node.DAG) err = e.InsertNodeAtPath(ctx, newPath, newnode, ft.EmptyDirNode) if err != nil { webError(w, "putHandler: InsertNodeAtPath failed", err, http.StatusInternalServerError) return } nnode, err := e.Finalize(i.node.DAG) if err != nil { webError(w, "putHandler: could not get node", err, http.StatusInternalServerError) return } newcid = nnode.Cid() case nil: pbnd, ok := rnode.(*dag.ProtoNode) if !ok { webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest) return } pbnewnode, ok := newnode.(*dag.ProtoNode) if !ok { webError(w, "Cannot read non protobuf nodes through gateway", dag.ErrNotProtobuf, http.StatusBadRequest) return } // object set-data case pbnd.SetData(pbnewnode.Data()) newcid, err = i.node.DAG.Add(pbnd) if err != nil { nnk := newnode.Cid() rk := pbnd.Cid() webError(w, fmt.Sprintf("putHandler: Could not add newnode(%q) to root(%q)", nnk.String(), rk.String()), err, http.StatusInternalServerError) return } default: log.Warningf("putHandler: unhandled resolve error %T", ev) webError(w, "could not resolve root DAG", ev, http.StatusInternalServerError) return } i.addUserHeaders(w) // ok, _now_ write user's headers. w.Header().Set("IPFS-Hash", newcid.String()) http.Redirect(w, r, gopath.Join(ipfsPathPrefix, newcid.String(), newPath), http.StatusCreated) }
func (w *Wantlist) Contains(k *cid.Cid) (*Entry, bool) { e, ok := w.set[k.KeyString()] return e, ok }
// FromCid safely converts a cid.Cid type to a Path type func FromCid(c *cid.Cid) Path { return Path("/ipfs/" + c.String()) }
func (m *impl) Cancel(k *cid.Cid) { delete(m.wantlist, k.KeyString()) m.addEntry(k, 0, true) }