-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
240 lines (188 loc) · 6.78 KB
/
Program.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace DAGLabs_Ex
{
public class Block
{
public string ID { get; set; }
public Block[] Parents { get; set; }
public override string ToString()
{
return ID.ToString();
}
}
public class DAG
{
public Block[] Tips { get; set; }
/// <summary>
/// Used for synchronizing access when manipulating DAG
/// </summary>
public object SyncObject { get; set; } = new object();
}
public class Miner
{
public int Index { get; set; }
public DAG DAG { get; set; }
public Miner[] ConnectedMiners { get; set; }
public override string ToString()
{
return Index.ToString();
}
}
class Network
{
readonly Random rand = new Random(42); // Setting seed for testability
readonly int BlockCount = 10;
readonly int MinerCount = 10;
readonly TimeSpan CreationRate = TimeSpan.FromSeconds(3);
readonly TimeSpan PropagationDelay = TimeSpan.FromSeconds(3);
readonly Block GenesisBlock = new Block { ID = "00" };
DateTime lastBlockCreationTime = DateTime.Now;
// Used to populate new block ID.
// Currently limited to 26 English characters, but is easy to extend.
readonly Queue<string> blockIDs = new Queue<string>();
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public void Start()
{
// init block IDs for verbosity (instead of using int or guid)
foreach (char c in chars)
blockIDs.Enqueue(c.ToString());
// spawn miners
var miners = new Miner[MinerCount];
for (int i = 0; i < miners.Length; i++)
{
// create the same DAG for every miner - only genesis block
var dag = new DAG
{
Tips = new Block[] { GenesisBlock }
};
miners[i] = new Miner { Index = i, DAG = dag };
Console.WriteLine("Spawned miner #{0}", i);
}
// Set ConnectedMiners for every miner.
// Currently every miner knows all the others, but it is easy to plug-in any other
// network layout here (i.e. every miner is connected to 50% of the miners, etc.)
for (int i = 0; i < miners.Length; i++)
{
var miner = miners[i];
miner.ConnectedMiners = miners
.Except(new[] { miner })
.ToArray();
}
// only genesis block exists at this point
int currentBlockCount = 1;
while (currentBlockCount < BlockCount)
{
if (lastBlockCreationTime.Add(CreationRate) > DateTime.Now)
{
Thread.Sleep(500);
continue;
}
int minerIndex = rand.Next(MinerCount);
var miner = miners[minerIndex];
CreateNewBlock(miner);
currentBlockCount++;
lastBlockCreationTime = DateTime.Now;
}
}
void CreateNewBlock(Miner miner)
{
var dag = miner.DAG;
var tips = dag.Tips;
var parents = new Block[0];
// keep generating random subset until non-empty is returned
while(parents.Length == 0)
{
parents = GetRandomSubset(tips);
}
var newBlock = new Block
{
ID = blockIDs.Dequeue(),
Parents = parents
};
Console.WriteLine("Miner #{0} created new block with ID = '{1}' and parents: {2}",
miner.Index, newBlock.ID, string.Join<Block>(", ", newBlock.Parents));
// Add the new block to the current miner's DAG
AddBlock(dag, newBlock);
foreach (var knownMiner in miner.ConnectedMiners)
{
SendNewBlockToMiner(knownMiner, newBlock);
}
}
void SendNewBlockToMiner(Miner miner, Block newBlock)
{
// Randomize offset in the interval [-0.1, 0.1)
double offset = (rand.NextDouble() - 0.5) / 5;
var delay = PropagationDelay * (1 + offset);
Task.Factory.StartNew(() =>
{
Thread.Sleep(delay);
AddBlock(miner.DAG, newBlock);
Console.WriteLine("Added block with ID = {0} to miner #{1} after {2} seconds",
newBlock.ID, miner.Index, delay.TotalSeconds);
});
}
/// <summary>
/// Returns a random subset of blocks
/// </summary>
/// <param name="blocks"></param>
/// <returns></returns>
Block[] GetRandomSubset(Block[] blocks)
{
if (blocks.Length == 1)
return blocks;
byte[] buffer = new byte[blocks.Length];
rand.NextBytes(buffer);
var parents = new List<Block>();
for (int i = 0; i < buffer.Length; i++)
{
byte b = buffer[i];
// this makes about half the blocks become parents of the new block
if (b > 127)
{
parents.Add(blocks[i]);
}
}
return parents.ToArray();
}
void AddBlock(DAG dag, Block newBlock)
{
lock (dag.SyncObject)
{
var oldTips = dag.Tips;
// create the new Tips list and add 'newBlock'
var newTips = new List<Block> { newBlock };
// Add every tip that is not a parent of 'newBlock'
foreach (var block in oldTips)
{
bool isParentOfNewBlock = false;
// check if 'block' is a parent of 'newBlock'
foreach (var newBlockParent in newBlock.Parents)
{
if (block.ID == newBlockParent.ID)
{
isParentOfNewBlock = true;
break;
}
}
if (isParentOfNewBlock == false)
{
// only add blocks that are not parent of 'newBlock'
newTips.Add(block);
}
}
dag.Tips = newTips.ToArray();
}
}
}
class Program
{
static void Main(string[] args)
{
new Network().Start();
}
}
}