Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
BlockEvent.cs
1using System;
2using System.Text;
3using System.Xml;
6
8{
12 public abstract class BlockEvent : NeuroLedgerPepEvent
13 {
14 private byte[] digest = null;
15 private byte[] signature = null;
16 private byte[] link = null;
17 private string collection = string.Empty;
18 private string creator = string.Empty;
19 private string url = string.Empty;
20 private ulong bytes = 0;
21 private DateTime created = DateTime.MinValue;
22 private DateTime updated = DateTime.MinValue;
23 private DateTime expires = DateTime.MaxValue;
24 private BlockStatus status = BlockStatus.Valid;
25
29 public BlockEvent()
30 : base()
31 {
32 }
33
37 public byte[] Digest
38 {
39 get => this.digest;
40 set => this.digest = value;
41 }
42
46 public byte[] Signature
47 {
48 get => this.signature;
49 set => this.signature = value;
50 }
51
55 public byte[] Link
56 {
57 get => this.link;
58 set => this.link = value;
59 }
60
64 public string Collection
65 {
66 get => this.collection;
67 set => this.collection = value;
68 }
69
73 public string Creator
74 {
75 get => this.creator;
76 set => this.creator = value;
77 }
78
82 public string Url
83 {
84 get => this.url;
85 set => this.url = value;
86 }
87
91 public DateTime Created
92 {
93 get => this.created;
94 set => this.created = value;
95 }
96
100 public DateTime Updated
101 {
102 get => this.updated;
103 set => this.updated = value;
104 }
105
109 public DateTime Expires
110 {
111 get => this.expires;
112 set => this.expires = value;
113 }
114
119 {
120 get => this.status;
121 set => this.status = value;
122 }
123
127 public ulong Bytes
128 {
129 get => this.bytes;
130 set => this.bytes = value;
131 }
132
137 protected void AddAttributes(StringBuilder Xml)
138 {
139 Xml.Append(" xmlns='");
141 Xml.Append("' d='");
142 Xml.Append(Convert.ToBase64String(this.digest));
143 Xml.Append("' s='");
144 Xml.Append(Convert.ToBase64String(this.signature));
145
146 if (!(this.link is null))
147 {
148 Xml.Append("' l='");
149 Xml.Append(Convert.ToBase64String(this.link));
150 }
151
152 Xml.Append("' cn='");
153 Xml.Append(XML.Encode(this.collection));
154 Xml.Append("' cr='");
155 Xml.Append(XML.Encode(this.creator));
156 Xml.Append("' ct='");
157 Xml.Append(XML.Encode(this.created));
158
159 if (this.updated != DateTime.MinValue)
160 {
161 Xml.Append("' u='");
162 Xml.Append(XML.Encode(this.updated));
163 }
164
165 if (this.expires != DateTime.MaxValue)
166 {
167 Xml.Append("' x='");
168 Xml.Append(XML.Encode(this.expires));
169 }
170
171 if (this.status != BlockStatus.Valid)
172 {
173 Xml.Append("' t='");
174 Xml.Append(this.status.ToString());
175 }
176
177 Xml.Append("' r='");
178 Xml.Append(this.url);
179 Xml.Append("' b='");
180 Xml.Append(this.bytes.ToString());
181 Xml.Append("'");
182 }
183
189 protected void Parse(XmlElement E, BlockEvent Event)
190 {
191 foreach (XmlAttribute Attr in E.Attributes)
192 {
193 switch (Attr.Name)
194 {
195 case "d":
196 Event.digest = Convert.FromBase64String(Attr.Value);
197 break;
198
199 case "s":
200 Event.signature = Convert.FromBase64String(Attr.Value);
201 break;
202
203 case "l":
204 Event.link = Convert.FromBase64String(Attr.Value);
205 break;
206
207 case "cn":
208 Event.collection = Attr.Value;
209 break;
210
211 case "cr":
212 Event.creator = Attr.Value;
213 break;
214
215 case "ct":
216 if (!XML.TryParse(Attr.Value, out DateTime DT))
217 throw new InvalidOperationException("Invalid creation time.");
218
219 Event.created = DT;
220 break;
221
222 case "u":
223 if (!XML.TryParse(Attr.Value, out DT))
224 throw new InvalidOperationException("Invalid update time.");
225
226 Event.updated = DT;
227 break;
228
229 case "x":
230 if (!XML.TryParse(Attr.Value, out DT))
231 throw new InvalidOperationException("Invalid expires time.");
232
233 Event.expires = DT;
234 break;
235
236 case "t":
237 if (!Enum.TryParse<BlockStatus>(Attr.Value, out BlockStatus Status))
238 throw new InvalidOperationException("Invalid block status.");
239
240 Event.status = Status;
241 break;
242
243 case "r":
244 Event.url = Attr.Value;
245 break;
246
247 case "b":
248 if (!ulong.TryParse(Attr.Value, out ulong Bytes))
249 throw new InvalidOperationException("Invalid block size.");
250
251 Event.bytes = Bytes;
252 break;
253
254 case "xmlns":
255 case "id":
256 break;
257
258 default:
259 if (Attr.Prefix != "xmlns")
260 throw new InvalidOperationException("Invalid attribute: " + Attr.Name);
261 break;
262 }
263 }
264 }
265 }
266}
Helps with common XML-related tasks.
Definition: XML.cs:19
static string Encode(string s)
Encodes a string for use in XML.
Definition: XML.cs:27
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:744
Abstract base class for Neuro-Ledger block PEP events.
Definition: BlockEvent.cs:13
DateTime Expires
When block expires (DateTime.MaxValue if it never expires.)
Definition: BlockEvent.cs:110
void Parse(XmlElement E, BlockEvent Event)
Parses Block attributes from a block element.
Definition: BlockEvent.cs:189
void AddAttributes(StringBuilder Xml)
Adds XML attributes related to the block.
Definition: BlockEvent.cs:137
DateTime Updated
When block was updated (DateTime.MinValue if not updated.)
Definition: BlockEvent.cs:101
byte[] Link
If block links to another block (with changes). null, if no link.
Definition: BlockEvent.cs:56
BlockEvent()
Abstract base class for Neuro-Ledger block PEP events.
Definition: BlockEvent.cs:29
Abstract base class for Neuro-Ledger PEP events.
BlockStatus
Status of the block.
Definition: BlockHeader.cs:12