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:
79  std::vector<std::pair<std::string, Link>> links_;
80  std::shared_ptr<Client> api_;
81 
84  ResolveResult CallChild(ResolutionState&);
85 
87  ResolveResult CallChild(ResolutionState&,
88  std::function<NodePtr(std::string_view)> gen_child);
89 
92  ResolveResult CallChild(ResolutionState&, std::string_view link_key);
93 
95  ResolveResult CallChild(ResolutionState&,
96  std::string_view link_key,
97  std::string_view block_key);
98 
99  public:
106  ResolveResult Resolve(ResolutionState& params);
107 
108  static NodePtr fromBytes(std::shared_ptr<Client> const& api,
109  Cid const&,
110  ByteView bytes);
111  static NodePtr fromBytes(std::shared_ptr<Client> const& api,
112  Cid const&,
113  std::string_view bytes);
114  static NodePtr fromBlock(PbDag const&);
115 
116  virtual ~DagNode() noexcept;
117 
122  virtual NodePtr rooted();
125  virtual NodePtr deroot();
126 
134  virtual DnsLinkName const* as_dnslink() const { return nullptr; }
137  virtual DirShard* as_hamt() { return nullptr; }
140  virtual IpnsName const* as_ipns() const { return nullptr; }
141 
147  virtual bool expired() const;
151  virtual bool PreferOver(DagNode const& another) const;
152 
156  void set_api(std::shared_ptr<Client> api);
157  void source(BlockSource src) { source_ = src; }
158 };
159 } // namespace ipfs::ipld
160 
161 std::ostream& operator<<(std::ostream&, ipfs::ipld::PathChange const&);
162 
163 #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:80
std::vector< std::pair< std::string, Link > > links_
Definition: dag_node.h:79
Definition: directory_shard.h:9
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:140
virtual DirShard * as_hamt()
Definition: dag_node.h:137
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