Skip to content

Commit 0f90f7b

Browse files
committed
Changed project structure and added Verify and AddHelpers
1 parent 4bd4162 commit 0f90f7b

File tree

121 files changed

+1984
-24
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

121 files changed

+1984
-24
lines changed

.idea/.idea.BlockChain/.idea/contentModel.xml

+14-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/.idea.BlockChain/.idea/workspace.xml

+117-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

BlockChain.Core.Test/BlockChainTest.cs renamed to BlockChain.Core.Test/BlockChain/BlockChainTest.cs

+7-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
using BlockChain.Core.Test.Block;
88
using NUnit.Framework;
99

10-
namespace BlockChain.Core.Test
10+
namespace BlockChain.Core.Test.BlockChain
1111
{
1212
public class BlockChainTest
1313
{
@@ -38,11 +38,15 @@ public void AddAndReadTest()
3838

3939
var token = new CancellationTokenSource();
4040
token.CancelAfter(TimeSpan.FromSeconds(TimeoutInSec));
41-
block.Mine(token.Token);
41+
var m =block.Mine(token.Token);
42+
m.Wait();
43+
block = m.Result;
4244

4345
blockChain.Add(block);
4446

45-
Assert.IsTrue(TestHelper.ArrayEquals(block.ToArray(),blockChain.First().ToArray()),"Block is not added to the blockchain, or could not read the blockchain");
47+
Assert.IsTrue(TestHelper.ArrayEquals(block.ToArray(),blockChain.First().ToArray()),
48+
"Block is not added to the blockchain, or could not read the blockchain");
49+
Assert.IsTrue(blockChain.First().IsValid(prevBlock.Hash(sha256),sha256),"Added block is not valid");
4650

4751
}
4852
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.IO;
3+
using System.Linq;
4+
using System.Threading;
5+
using BlockChain.Core.Block;
6+
using BlockChain.Core.Test.Block;
7+
using NUnit.Framework;
8+
9+
namespace BlockChain.Core.Test.BlockChain.Helpers
10+
{
11+
public class AddHelpersTest
12+
{
13+
const string file = "blockchain3.db";
14+
const int TimeoutInSec = 30;
15+
[Test]
16+
public void AddTest()
17+
{
18+
if(File.Exists(file)) File.Delete(file);
19+
var blockChain = new BlockChain<TestBlockData>(file);
20+
21+
var data = new TestBlockData("12345678910");
22+
var token = new CancellationTokenSource();
23+
token.CancelAfter(TimeSpan.FromSeconds(TimeoutInSec));
24+
25+
blockChain.Add(data, token: token.Token).Wait();
26+
27+
Assert.IsTrue(TestHelper.ArrayEquals(data.ToArray(),blockChain.First().GetData().ToArray()),"Block is not added to the blockchain, or could not read the blockchain");
28+
Assert.IsTrue(blockChain.First().IsValid(blockChain.First().GetBlockHeader().HashPrevBlock,blockChain.GetHashingAlgorithm()),"Added block is not valid");
29+
30+
}
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
using System;
2+
using System.IO;
3+
using System.Threading;
4+
using System.Threading.Tasks;
5+
using BlockChain.Core.Block;
6+
using BlockChain.Core.Test.Block;
7+
using NUnit.Framework;
8+
9+
namespace BlockChain.Core.Test.BlockChain.Helpers
10+
{
11+
public class VerifyHelperTest
12+
{
13+
const string file = "blockchain4.db";
14+
const int TimeoutInSec = 30;
15+
16+
[Test]
17+
public async Task VerifyTest()
18+
{
19+
if(File.Exists(file)) File.Delete(file);
20+
var blockChain = new BlockChain<TestBlockData>(file);
21+
22+
var data = new[] { new TestBlockData("12345678910"),new TestBlockData("12345678911230"),new TestBlockData("123455678910"),new TestBlockData("1234567891120") };
23+
var token = new CancellationTokenSource();
24+
token.CancelAfter(TimeSpan.FromSeconds(TimeoutInSec));
25+
foreach (var d in data) await blockChain.Add(d, token: token.Token);
26+
27+
//Check blockchain
28+
var x = await blockChain.Verify(token.Token);
29+
Assert.IsTrue(x,"Valid blockchain was marked invalid");
30+
31+
//Make blockchain invalid
32+
MakeBlockChainInvalid(blockChain);
33+
foreach (var d in data) await blockChain.Add(d, token: token.Token); //Add some random blocks
34+
35+
//Check blockchain again
36+
x = await blockChain.Verify(token.Token);
37+
Assert.IsFalse(x,"Invalid blockchain was marked valid");
38+
}
39+
40+
private void MakeBlockChainInvalid(BlockChain<TestBlockData> blockChain)
41+
{
42+
var blockWithInvalidMerkleRoot = new Block<TestBlockData>(Convert.FromBase64String(
43+
"AQAAAAmKCD7tIp5wWCHSYMEE9XHhXHA4AQsH0gMQiiqQkIjwHgCynzH++oBbPhLPtQl1PNwX9KnuriDiafYwR6WMlRz5B3jtcrfXCB4AAP/yBwEAMTIzNDY1NDIzNTE0M2UAAAA="));
44+
byte[] data = blockWithInvalidMerkleRoot.ToArray();
45+
using var stream = new FileStream(file, FileMode.Append);
46+
stream.Write(data, 0, data.Length);
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)