From f8e18e78790ea9b5f692376d3e81072fa106a09b Mon Sep 17 00:00:00 2001 From: Nate Maninger Date: Sun, 19 Nov 2023 13:56:19 -0800 Subject: [PATCH] ipfs, sia: broadcast pinned CIDs --- ipfs/node.go | 5 +++++ sia/sia.go | 18 +++++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/ipfs/node.go b/ipfs/node.go index 032a84e..145b568 100644 --- a/ipfs/node.go +++ b/ipfs/node.go @@ -117,6 +117,11 @@ func mustParsePeer(s string) peer.AddrInfo { return *info } +// Provide broadcasts a CID to the network +func (n *Node) Provide(c cid.Cid) error { + return n.provider.Provide(c) +} + // NewNode creates a new IPFS node func NewNode(ctx context.Context, privateKey crypto.PrivKey, cfg config.IPFS, ds datastore.Batching, bs blockstore.Blockstore) (*Node, error) { cmgr, err := connmgr.NewConnManager(600, 900) diff --git a/sia/sia.go b/sia/sia.go index e7b6171..0b90b53 100644 --- a/sia/sia.go +++ b/sia/sia.go @@ -29,10 +29,16 @@ type ( AllKeysChan(ctx context.Context) (<-chan cid.Cid, error) } + // An IPFSProvider broadcasts CIDs to the IPFS network + IPFSProvider interface { + Provide(cid.Cid) error + } + // A Node is a specialized IPFS gateway that retrieves data from a renterd // node Node struct { store Store + ipfs IPFSProvider log *zap.Logger renterd config.Renterd @@ -107,10 +113,20 @@ func (n *Node) UploadCID(ctx context.Context, c cid.Cid, r io.Reader) error { default: } + blocks := dagSvc.Blocks() + if rootNode.Cid().Hash().B58String() != c.Hash().B58String() { return fmt.Errorf("unexpected root cid: %s", rootNode.Cid().Hash().B58String()) + } else if err := n.store.AddBlocks(ctx, blocks); err != nil { + return fmt.Errorf("failed to add blocks to store: %w", err) } - return n.store.AddBlocks(ctx, dagSvc.Blocks()) + + for _, b := range blocks { + if err := n.ipfs.Provide(b.CID); err != nil { + return fmt.Errorf("failed to provide block: %w", err) + } + } + return nil } // ProxyHTTPDownload proxies an http download request to the renterd node