-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblockchain-lesson1.cs
More file actions
141 lines (101 loc) · 2.79 KB
/
blockchain-lesson1.cs
File metadata and controls
141 lines (101 loc) · 2.79 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
137
138
139
140
141
public class Block
{
public int Index;
public string Timestamp;
public string Data;
public string Hash;
public string PrevHash;
}
public class Blockchain
{
public List<Block> Chain = new List<Block>();
}
Blockchain blockChain = new Blockchain();
void Main()
{
//Create Genesis Block
CreateGenesisBlock();
AddBlock(DateTime.Now.Ticks.ToString(), blockChain.Chain[GetLatestBlock()].Hash, "A");
AddBlock(DateTime.Now.Ticks.ToString(), blockChain.Chain[GetLatestBlock()].Hash, "B");
AddBlock(DateTime.Now.Ticks.ToString(), blockChain.Chain[GetLatestBlock()].Hash, "C");
//List Blockchain
blockChain.Dump();
//Blockchain Validity Check
Console.WriteLine("Is Blockchain Valid? " + IsChainValid());
//Modify Blockchain
blockChain.Chain[1].Data = "B";
//Second Blockchain Validity Checks
Console.WriteLine("Is Blockchain Valid? " + IsChainValid());
}
public void AddBlock(string timestamp, string prevHash, string data)
{
var block = new Block();
block.Index = GetNextIndex();
block.Timestamp = timestamp;
block.PrevHash = prevHash;
block.Data = data;
var hashString = block.Index.ToString() + block.Timestamp + block.Data + block.PrevHash;
var hash = CalculateHash(hashString);
block.Hash = hash;
blockChain.Chain.Add(block);
}
public int GetLatestBlock()
{
return blockChain.Chain.Count() - 1;
}
public int GetNextIndex()
{
return blockChain.Chain.Count();
}
public void CreateGenesisBlock()
{
//Genesis Block
var block0 = new Block();
block0.Index = 0;
block0.Timestamp = DateTime.Now.Ticks.ToString();
block0.PrevHash = "";
block0.Data = "Genesis Block";
var hashString = block0.Index.ToString() + block0.Timestamp + block0.Data + block0.PrevHash;
var hash = CalculateHash(hashString);
block0.Hash = hash;
blockChain.Chain.Add(block0);
}
public bool IsChainValid()
{
var result = true;
blockChain.Chain.Where(x => x.Index != 0).ToList().ForEach(x => {
var currentBlock = x;
var previousBlock = blockChain.Chain[x.Index - 1];
var hashString = currentBlock.Index.ToString() + currentBlock.Timestamp + currentBlock.Data + currentBlock.PrevHash;
var hash = CalculateHash(hashString);
if (result != false)
{
if (currentBlock.Hash != hash)
{
result = false;
}
if(currentBlock.PrevHash != previousBlock.Hash)
{
result = false;
}
}
});
return result;
}
public static string CalculateHash(string inputString)
{
SHA256 sha256 = SHA256Managed.Create();
byte[] bytes = Encoding.UTF8.GetBytes(inputString);
byte[] hash = sha256.ComputeHash(bytes);
return GetStringFromHash(hash);
}
private static string GetStringFromHash(byte[] hash)
{
StringBuilder result = new StringBuilder();
for (int i = 0; i < hash.Length; i++)
{
result.Append(hash[i].ToString("X2"));
}
return result.ToString();
}
// Define other methods and classes here