ipfs-chromium
dag_node.h
1 #ifndef IPFS_DAG_NODE_H_
2 #define IPFS_DAG_NODE_H_
3 
4 #include "link.h"
5 #include "resolution_state.h"
6 
7 #include <ipfs_client/gw/gateway_request.h>
8 #include <ipfs_client/ipld/block_source.h>
9 
10 #include <ipfs_client/cid.h>
11 #include <ipfs_client/response.h>
12 #include <vocab/slash_delimited.h>
13 
14 #include <cstdint>
15 
16 #include <functional>
17 #include <memory>
18 #include <string>
19 #include <string_view>
20 #include <variant>
21 #include <vector>
22 
23 namespace ipfs {
24 class PbDag;
25 class Client;
26 struct ValidatedIpns;
27 } // namespace ipfs
28 namespace libp2p::multi {
29 struct ContentIdentifier;
30 }
31 namespace ipfs::ipld {
32 
33 using NodePtr = std::shared_ptr<DagNode>;
34 class DirShard;
35 class DnsLinkName;
36 class IpnsName;
37 
45  explicit MoreDataNeeded(std::string one) : ipfs_abs_paths_{{one}} {}
49  template <class Range>
50  explicit MoreDataNeeded(Range const& many)
51  : ipfs_abs_paths_(many.begin(), many.end()) {}
54  std::vector<std::string> ipfs_abs_paths_;
55 };
56 enum class ProvenAbsent {};
60 struct PathChange {
61  std::string new_path;
62 };
63 
64 using ResolveResult =
65  std::variant<MoreDataNeeded, Response, ProvenAbsent, PathChange>;
69 class DagNode : public std::enable_shared_from_this<DagNode> {
70  Link* FindChild(std::string_view);
71  BlockSource source_;
72 
73  virtual ResolveResult resolve(ResolutionState& params) = 0;
74 
75  protected:
76  std::vector<std::pair<std::string, Link>> links_;
77  std::shared_ptr<Client> api_;
78 
81  ResolveResult CallChild(ResolutionState&);
82 
84  ResolveResult CallChild(ResolutionState&,
85  std::function<NodePtr(std::string_view)> gen_child);
86 
89  ResolveResult CallChild(ResolutionState&, std::string_view link_key);
90 
92  ResolveResult CallChild(ResolutionState&,
93  std::string_view link_key,
94  std::string_view block_key);
95 
96  public:
103  ResolveResult Resolve(ResolutionState& params);
104 
105  static NodePtr fromBytes(std::shared_ptr<Client> const& api,
106  Cid const&,
107  ByteView bytes);
108  static NodePtr fromBytes(std::shared_ptr<Client> const& api,
109  Cid const&,
110  std::string_view bytes);
111  static NodePtr fromBlock(PbDag const&);
112 
113  virtual ~DagNode() noexcept;
114 
119  virtual NodePtr rooted();
122  virtual NodePtr deroot();
123 
131  virtual DnsLinkName const* as_dnslink() const { return nullptr; }
134  virtual DirShard* as_hamt() { return nullptr; }
137  virtual IpnsName const* as_ipns() const { return nullptr; }
138 
144  virtual bool expired() const;
148  virtual bool PreferOver(DagNode const& another) const;
149 
150  void set_api(std::shared_ptr<Client>);
151  void source(BlockSource src) { source_ = src; }
152 };
153 } // namespace ipfs::ipld
154 
155 std::ostream& operator<<(std::ostream&, ipfs::ipld::PathChange const&);
156 
157 #endif // IPFS_DAG_NODE_H_
Definition: cid.h:16
Something to which a CID may refer directly.
Definition: pb_dag.h:24
A block, an IPNS record, etc.
Definition: dag_node.h:69
std::shared_ptr< Client > api_
Definition: dag_node.h:77
Definition: directory_shard.h:7
Definition: dnslink_name.h:9
Definition: ipns_name.h:11
Definition: resolution_state.h:21
virtual IpnsName const * as_ipns() const
Definition: dag_node.h:137
virtual DirShard * as_hamt()
Definition: dag_node.h:134
Definition: dag_node.h:28
Definition: block_source.h:12
Definition: dag_node.h:41
MoreDataNeeded(Range const &many)
Definition: dag_node.h:50
MoreDataNeeded(std::string one)
Definition: dag_node.h:45
std::vector< std::string > ipfs_abs_paths_
Definition: dag_node.h:54
Definition: dag_node.h:60