-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (109 loc) · 3.86 KB
/
Program.cs
File metadata and controls
136 lines (109 loc) · 3.86 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
namespace blockshy
{
class Block
{
public byte[] Data {get;}
public byte[] Hash { get; set; }
public byte[] PrevHash { get; set; }
public int Nonce { get; set; }
public DateTime Timestamp {get;}
public Block(byte[] data)
{
Data = data;
Timestamp = DateTime.UtcNow;
Nonce = 0;
PrevHash = new byte[] {0x00};
}
public byte[] GenerateHash()
{
using(SHA512 sha = new SHA512Managed())
using(MemoryStream ms = new MemoryStream())
using(BinaryWriter bw = new BinaryWriter(ms))
{
bw.Write(Data);
bw.Write(Nonce);
bw.Write(Timestamp.ToBinary());
bw.Write(PrevHash);
var arr = ms.ToArray();
return sha.ComputeHash(arr);
}
}
public byte[] MineHash(byte[] difficulty)
{
byte[] hash = GenerateHash();
int size = difficulty.Length;
while(!hash.Take(size).SequenceEqual(difficulty))
{
Nonce++;
hash = GenerateHash();
}
return hash;
}
public bool IsValid()
{
return GenerateHash().SequenceEqual(Hash);
}
public bool IsPreviousValid(Block prevBlock)
{
return prevBlock.IsValid() && PrevHash.SequenceEqual(prevBlock.Hash);
}
public override string ToString()
{
return $"{BitConverter.ToString(Hash.Take(9).ToArray()).Replace("-", "")}..{BitConverter.ToString(Hash.TakeLast(9).ToArray()).Replace("-", "")}:\n Prev: {BitConverter.ToString(PrevHash.Take(9).ToArray()).Replace("-", "")}..{BitConverter.ToString(PrevHash.TakeLast(9).ToArray()).Replace("-", "")}\n Nonce: {Nonce} Timestamp: {Timestamp}";
}
}
class Blockchain
{
private List<Block> _items;
private byte[] _difficulty;
public Blockchain(byte[] difficulty, Block genesis)
{
_items = new List<Block>();
_difficulty = difficulty;
AddBlock(genesis);
}
public Block AddBlock(Block block)
{
block.PrevHash = _items.LastOrDefault()?.Hash ?? new byte[] { 0x00 };
block.Hash = block.MineHash(_difficulty);
_items.Add(block);
return block;
}
public bool IsValid()
{
var iter = _items.GetEnumerator();
bool isValid = iter.MoveNext() && (iter.Current?.IsValid() ?? false);
Block previous = iter.Current;
while(iter.MoveNext() && isValid)
{
isValid = iter.Current.IsPreviousValid(previous) && iter.Current.IsValid();
previous = iter.Current;
}
return isValid;
}
}
static class Program
{
static void Main(string[] args)
{
Block genesis = new Block(new byte[] {0x00, 0x00, 0x00, 0x00, 0x00});
Blockchain blockchain = new Blockchain(new byte[] {0x00, 0x00}, genesis);
Console.WriteLine($"Starting! Chain brand new is valid: {blockchain.IsValid()}\n\n");
Random rnd = new Random(DateTime.UtcNow.Millisecond);
for(int i = 0; i < 10; i++)
{
byte[] data = Enumerable.Range(0, 256).Select(x => (byte)rnd.Next(256)).ToArray();
Block block = new Block(data);
blockchain.AddBlock(block);
Console.WriteLine(block);
Console.WriteLine($"Chain is valid: {blockchain.IsValid()}\n\n");
}
Console.WriteLine("Done!");
}
}
}