-
Notifications
You must be signed in to change notification settings - Fork 58
/
Copy pathDagNode.cs
337 lines (311 loc) · 9.89 KB
/
DagNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
using Google.Protobuf;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace Ipfs
{
/// <summary>
/// A node in the IPFS Merkle DAG.
/// </summary>
/// <remarks>
/// A <b>DagNode</b> has opaque <see cref="DagNode.DataBytes"/>
/// and a set of navigable <see cref="DagNode.Links"/>.
/// </remarks>
[DataContract]
public class DagNode : IMerkleNode<IMerkleLink>
{
Cid id;
string hashAlgorithm = MultiHash.DefaultAlgorithmName;
long? size;
/// <summary>
/// Create a new instance of a <see cref="DagNode"/> with the specified
/// <see cref="DagNode.DataBytes"/> and <see cref="DagNode.Links"/>
/// </summary>
/// <param name="data">
/// The opaque data, can be <b>null</b>.
/// </param>
/// <param name="links">
/// The links to other nodes.
/// </param>
/// <param name="hashAlgorithm">
/// The name of the hashing algorithm to use; defaults to
/// <see cref="MultiHash.DefaultAlgorithmName"/>.
/// </param>
public DagNode(byte[] data, IEnumerable<IMerkleLink> links = null, string hashAlgorithm = MultiHash.DefaultAlgorithmName)
{
this.DataBytes = data ?? (new byte[0]);
this.Links = (links ?? (new DagLink[0]))
.OrderBy(link => link.Name ?? "");
this.hashAlgorithm = hashAlgorithm;
}
/// <summary>
/// Creates a new instance of the <see cref="DagNode"/> class from the
/// specified <see cref="Stream"/>.
/// </summary>
/// <param name="stream">
/// A <see cref="Stream"/> containing the binary representation of the
/// <b>DagNode</b>.
/// </param>
public DagNode(Stream stream)
{
Read(stream);
}
/// <summary>
/// Creates a new instance of the <see cref="DagNode"/> class from the
/// specified <see cref="CodedInputStream"/>.
/// </summary>
/// <param name="stream">(
/// A <see cref="CodedInputStream"/> containing the binary representation of the
/// <b>DagNode</b>.
/// </param>
public DagNode(CodedInputStream stream)
{
Read(stream);
}
/// <inheritdoc />
[DataMember]
public IEnumerable<IMerkleLink> Links { get; private set; }
/// <inheritdoc />
[DataMember]
public byte[] DataBytes { get; private set; }
/// <inheritdoc />
public Stream DataStream
{
get
{
return new MemoryStream(DataBytes, false);
}
}
/// <summary>
/// The serialised size in bytes of the node.
/// </summary>
[DataMember]
public long Size
{
get
{
if (!size.HasValue)
{
ComputeSize();
}
return size.Value;
}
}
/// <inheritdoc />
[DataMember]
public Cid Id
{
get
{
if (id == null)
{
ComputeHash();
}
return id;
}
set
{
id = value;
if (id != null)
{
hashAlgorithm = id.Hash.Algorithm.Name;
}
}
}
/// <inheritdoc />
public IMerkleLink ToLink(string name = "")
{
return new DagLink(name, Id, Size);
}
/// <summary>
/// Adds a link.
/// </summary>
/// <param name="link">
/// The link to add.
/// </param>
/// <returns>
/// A new <see cref="DagNode"/> with the existing and new
/// links.
/// </returns>
/// <remarks>
/// A <b>DagNode</b> is immutable.
/// </remarks>
public DagNode AddLink(IMerkleLink link)
{
return AddLinks(new[] { link });
}
/// <summary>
/// Adds a sequence of links.
/// </summary>
/// <param name="links">
/// The sequence of links to add.
/// </param>
/// <returns>
/// A new <see cref="DagNode"/> with the existing and new
/// links.
/// </returns>
/// <remarks>
/// A <b>DagNode</b> is immutable.
/// </remarks>
public DagNode AddLinks(IEnumerable<IMerkleLink> links)
{
var all = Links.Union(links);
return new DagNode(DataBytes, all, hashAlgorithm);
}
/// <summary>
/// Removes a link.
/// </summary>
/// <param name="link">
/// The <see cref="IMerkleLink"/> to remove.
/// </param>
/// <returns>
/// A new <see cref="DagNode"/> with the <paramref name="link"/>
/// removed.
/// </returns>
/// <remarks>
/// A <b>DagNode</b> is immutable.
/// <para>
/// No exception is raised if the <paramref name="link"/> does
/// not exist.
/// </para>
/// </remarks>
public DagNode RemoveLink(IMerkleLink link)
{
return RemoveLinks(new[] { link });
}
/// <summary>
/// Remove a sequence of links.
/// </summary>
/// <param name="links">
/// The sequence of <see cref="IMerkleLink"/> to remove.
/// </param>
/// <returns>
/// A new <see cref="DagNode"/> with the <paramref name="links"/>
/// removed.
/// </returns>
/// <remarks>
/// A <b>DagNode</b> is immutable.
/// <para>
/// No exception is raised if any of the <paramref name="links"/> do
/// not exist.
/// </para>
/// </remarks>
public DagNode RemoveLinks(IEnumerable<IMerkleLink> links)
{
var ignore = links.ToLookup(link => link.Id);
var some = Links.Where(link => !ignore.Contains(link.Id));
return new DagNode(DataBytes, some, hashAlgorithm);
}
/// <summary>
/// Writes the binary representation of the node to the specified <see cref="Stream"/>.
/// </summary>
/// <param name="stream">
/// The <see cref="Stream"/> to write to.
/// </param>
public void Write(Stream stream)
{
using (var cos = new CodedOutputStream(stream, true))
{
Write(cos);
}
}
/// <summary>
/// Writes the binary representation of the node to the specified <see cref="CodedOutputStream"/>.
/// </summary>
/// <param name="stream">
/// The <see cref="CodedOutputStream"/> to write to.
/// </param>
public void Write(CodedOutputStream stream)
{
if (stream == null)
throw new ArgumentNullException("stream");
foreach (var link in Links.Select(l => new DagLink(l)))
{
using (var linkStream = new MemoryStream())
{
link.Write(linkStream);
var msg = linkStream.ToArray();
stream.WriteTag(2, WireFormat.WireType.LengthDelimited);
stream.WriteLength(msg.Length);
stream.WriteSomeBytes(msg);
}
}
if (DataBytes.Length > 0)
{
stream.WriteTag(1, WireFormat.WireType.LengthDelimited);
stream.WriteLength(DataBytes.Length);
stream.WriteSomeBytes(DataBytes);
}
}
void Read(Stream stream)
{
using (var cis = new CodedInputStream(stream, true))
{
Read(cis);
}
}
void Read(CodedInputStream stream)
{
var links = new List<DagLink>();
bool done = false;
while (!stream.IsAtEnd && !done)
{
var tag = stream.ReadTag();
switch(WireFormat.GetTagFieldNumber(tag))
{
case 1:
DataBytes = stream.ReadSomeBytes(stream.ReadLength());
done = true;
break;
case 2:
using (var ms = new MemoryStream(stream.ReadSomeBytes(stream.ReadLength())))
{
links.Add(new DagLink(ms));
}
break;
default:
throw new InvalidDataException("Unknown field number");
}
}
if (DataBytes == null)
DataBytes = new byte[0];
Links = links.ToArray();
}
/// <summary>
/// Returns the IPFS binary representation as a byte array.
/// </summary>
/// <returns>
/// A byte array.
/// </returns>
public byte[] ToArray()
{
using (var ms = new MemoryStream())
{
Write(ms);
return ms.ToArray();
}
}
void ComputeHash()
{
using (var ms = new MemoryStream())
{
Write(ms);
size = ms.Position;
ms.Position = 0;
id = MultiHash.ComputeHash(ms, hashAlgorithm);
}
}
void ComputeSize()
{
using (var ms = new MemoryStream())
{
Write(ms);
size = ms.Position;
}
}
}
}