lotus/retrieval/discovery/local.go

55 lines
1.4 KiB
Go
Raw Normal View History

2019-08-26 13:45:36 +00:00
package discovery
import (
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
dshelp "github.com/ipfs/go-ipfs-ds-help"
cbor "github.com/ipfs/go-ipld-cbor"
logging "github.com/ipfs/go-log/v2"
2019-08-26 13:45:36 +00:00
"github.com/filecoin-project/lotus/node/modules/dtypes"
retrievalmarket "github.com/filecoin-project/lotus/retrieval"
2019-08-26 13:45:36 +00:00
)
var log = logging.Logger("ret-discovery")
type Local struct {
ds datastore.Datastore
}
func NewLocal(ds dtypes.MetadataDS) *Local {
return &Local{ds: namespace.Wrap(ds, datastore.NewKey("/deals/local"))}
}
func (l *Local) AddPeer(cid cid.Cid, peer retrievalmarket.RetrievalPeer) error {
2019-08-26 13:45:36 +00:00
// TODO: allow multiple peers here
// (implement an util for tracking map[thing][]otherThing, use in sectorBlockstore too)
log.Warn("Tracking multiple retrieval peers not implemented")
entry, err := cbor.DumpObject(peer)
if err != nil {
return err
}
return l.ds.Put(dshelp.CidToDsKey(cid), entry)
}
func (l *Local) GetPeers(data cid.Cid) ([]retrievalmarket.RetrievalPeer, error) {
2019-08-26 13:45:36 +00:00
entry, err := l.ds.Get(dshelp.CidToDsKey(data))
if err == datastore.ErrNotFound {
return []retrievalmarket.RetrievalPeer{}, nil
2019-08-26 13:45:36 +00:00
}
if err != nil {
return nil, err
}
var peer retrievalmarket.RetrievalPeer
2019-08-26 13:45:36 +00:00
if err := cbor.DecodeInto(entry, &peer); err != nil {
return nil, err
}
return []retrievalmarket.RetrievalPeer{peer}, nil
2019-08-26 13:45:36 +00:00
}
var _ retrievalmarket.PeerResolver = &Local{}