forked from planetarium/libplanet
/
BlockPolicyTest.cs
273 lines (251 loc) · 9.26 KB
/
BlockPolicyTest.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using Libplanet.Blockchain.Policies;
using Libplanet.Blocks;
using Libplanet.Tests.Common.Action;
using Libplanet.Tests.Store;
using Libplanet.Tx;
using Xunit;
using Xunit.Abstractions;
namespace Libplanet.Tests.Blockchain.Policies
{
public class BlockPolicyTest : IClassFixture<FileStoreFixture>
{
private static readonly DateTimeOffset FixtureEpoch =
new DateTimeOffset(2018, 1, 1, 0, 0, 0, TimeSpan.Zero);
private readonly ITestOutputHelper _output;
public BlockPolicyTest(ITestOutputHelper output)
{
_output = output;
}
[Fact]
public void Constructors()
{
var tenSec = new TimeSpan(0, 0, 10);
var a = new BlockPolicy<DumbAction>(tenSec);
Assert.Equal(tenSec, a.BlockInterval);
var b = new BlockPolicy<DumbAction>(65000);
Assert.Equal(
new TimeSpan(0, 1, 5),
b.BlockInterval
);
var c = new BlockPolicy<DumbAction>();
Assert.Equal(
new TimeSpan(0, 0, 5),
c.BlockInterval
);
Assert.Throws<ArgumentOutOfRangeException>(
() => new BlockPolicy<DumbAction>(tenSec.Negate())
);
Assert.Throws<ArgumentOutOfRangeException>(
() => new BlockPolicy<DumbAction>(-5)
);
}
[Fact]
public void GetNextBlockDifficulty()
{
var policy = new BlockPolicy<DumbAction>(new TimeSpan(3, 0, 0));
Block<DumbAction>[] blocks = MineBlocks(
new[] { (0, 0), (1, 1), (3, 2), (7, 3), (9, 2), (13, 3) }
).ToArray();
Assert.Equal(
0,
policy.GetNextBlockDifficulty(blocks.Take(0).ToList())
);
Assert.Equal(
1,
policy.GetNextBlockDifficulty(blocks.Take(1).ToList())
);
Assert.Equal(
2,
policy.GetNextBlockDifficulty(blocks.Take(2).ToList())
);
Assert.Equal(
3,
policy.GetNextBlockDifficulty(blocks.Take(3).ToList())
);
Assert.Equal(
2,
policy.GetNextBlockDifficulty(blocks.Take(4).ToList())
);
Assert.Equal(
3,
policy.GetNextBlockDifficulty(blocks.Take(5).ToList())
);
Assert.Equal(
2,
policy.GetNextBlockDifficulty(blocks)
);
}
[Fact]
public void ValidateBlocks()
{
var policy = new BlockPolicy<DumbAction>(new TimeSpan(3, 0, 0));
// The genesis block must has the index #0.
Assert.IsType<InvalidBlockIndexException>(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0) },
fields =>
{
fields.Index = 1;
return fields;
}).ToList(),
FixtureEpoch + new TimeSpan(0, 1, 0)
)
);
// The genesis block must not have PreviousHash
Assert.IsType<InvalidBlockPreviousHashException>(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0) },
fields =>
{
fields.PreviousHash = default(HashDigest<SHA256>);
return fields;
}).ToList(),
FixtureEpoch + new TimeSpan(0, 1, 0)
)
);
// Block indices must be continous.
Assert.IsType<InvalidBlockIndexException>(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0), (1, 1) },
fields =>
{
fields.Index = fields.Index == 0 ? 0 : 2;
return fields;
}).ToList(),
FixtureEpoch + new TimeSpan(1, 1, 0)
)
);
// Block difficulties depend on interval between previous block
// and previous of previous block. If an interval is shorter than
// BlockInterval (3 hours in this test case) it should be raised.
// Otherwise it should be dropped. It should be always raised or
// dropped by only 1.
Assert.Null(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0), (1, 1), (3, 2) }).ToList(),
FixtureEpoch + new TimeSpan(3, 1, 0)
)
);
Assert.IsType<InvalidBlockDifficultyException>(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0), (1, 1), (3, 1) }).ToList(),
FixtureEpoch + new TimeSpan(3, 1, 0)
)
);
Assert.Null(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0), (1, 1), (5, 2), (9, 1) }).ToList(),
FixtureEpoch + new TimeSpan(9, 1, 0)
)
);
Assert.Null(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0), (1, 1), (5, 2), (9, 5) }).ToList(),
FixtureEpoch + new TimeSpan(9, 1, 0)
)
);
// A block's previous hash must refer to the hash of the block
// appeared right before.
Assert.IsType<InvalidBlockPreviousHashException>(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0), (1, 1), (5, 2) },
fields =>
{
if (fields.Index >= 2)
{
fields.PreviousHash =
default(HashDigest<SHA256>);
}
return fields;
}).ToList(),
FixtureEpoch + new TimeSpan(5, 1, 0)
)
);
// Block timestamp should not be overlapped.
Assert.IsType<InvalidBlockTimestampException>(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0), (2, 1), (1, 2) }).ToList(),
FixtureEpoch + new TimeSpan(2, 1, 0)
)
);
// Returns null if blocks are valid.
Assert.Null(
policy.ValidateBlocks(
MineBlocks(
new[] { (0, 0), (1, 1), (3, 2), (7, 3), (9, 2) })
.ToList(),
FixtureEpoch + new TimeSpan(9, 1, 0)
)
);
}
private IEnumerable<Block<DumbAction>> MineBlocks(
(int, int)[] blockArgs,
Func<BlockFields, BlockFields> interprocess = null
)
{
_output.WriteLine($"MineBlocks({blockArgs.Length}):");
var miner = default(Address);
long i = 0;
HashDigest<SHA256>? previousHash = null;
foreach ((int timestampHour, int d) in blockArgs)
{
int difficulty = d;
var timestamp =
FixtureEpoch + TimeSpan.FromHours(timestampHour);
if (interprocess != null)
{
BlockFields fields = interprocess(
new BlockFields
{
Index = i,
Difficulty = difficulty,
PreviousHash = previousHash,
Timestamp = timestamp,
}
);
i = fields.Index;
difficulty = fields.Difficulty;
previousHash = fields.PreviousHash;
timestamp = fields.Timestamp;
}
Block<DumbAction> block = Block<DumbAction>.Mine(
i,
difficulty,
miner,
previousHash,
timestamp,
new Transaction<DumbAction>[0]
);
_output.WriteLine(
$"#{i}: {block.Hash}, difficulty={block.Difficulty}, " +
$"previousBlock={block.PreviousHash}, " +
$"timestamp={block.Timestamp}"
);
yield return block;
previousHash = block.Hash;
i++;
}
_output.WriteLine(string.Empty);
}
private struct BlockFields
{
internal long Index;
internal int Difficulty;
internal HashDigest<SHA256>? PreviousHash;
internal DateTimeOffset Timestamp;
}
}
}