-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathblockchaintransaction-lesson3.cs
More file actions
186 lines (133 loc) · 3.62 KB
/
blockchaintransaction-lesson3.cs
File metadata and controls
186 lines (133 loc) · 3.62 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
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
public class Block
{
public int Index;
public string Timestamp;
public List<Transaction> Transactions = new List<Transaction>();
public string Hash;
public string PrevHash;
public int Nonce {get;set;} = 0;
}
public class Blockchain
{
public List<Block> Chain = new List<Block>();
public int Difficulty {get; set;} = 2;
public List<Transaction> PendingTransactions = new List<Transaction>();
public int MiningReward {get; set;} = 100;
}
public class Transaction
{
public string FromAddress;
public string ToAddress;
public decimal Amount;
}
Blockchain blockChain = new Blockchain();
void Main()
{
//Create Genesis Block
CreateGenesisBlock();
var tran = new Transaction();
tran.FromAddress = "Aaron";
tran.ToAddress = "Taylor";
tran.Amount = 5;
CreateTransactions(tran);
blockChain.Dump();
MinePendingTransaction("Joe");
//List Blockchain
blockChain.Dump();
//Blockchain Validity Check
Console.WriteLine("Is Blockchain Valid? " + IsChainValid());
//Second Blockchain Validity Checks
Console.WriteLine("Is Blockchain Valid? " + IsChainValid());
}
public void CreateTransactions(Transaction trans)
{
blockChain.PendingTransactions.Add(trans);
}
public void MinePendingTransaction(string rewardAddress)
{
var block = new Block();
block.Index = GetNextIndex();
block.Timestamp = DateTime.Now.Ticks.ToString();
block.PrevHash = blockChain.Chain[GetLatestBlock()].Hash;
block.Transactions = blockChain.PendingTransactions;
var minedBlock = MineBlock(block);
blockChain.Chain.Add(minedBlock);
}
public Block MineBlock(Block block)
{
var diff = blockChain.Difficulty;
char[] diffChar = new char[1];
diffChar[0] = '0';
var alpha = new StringBuilder();
var diffVar = alpha.Append(diffChar[0], diff).ToString();
var jsonSerialiser = new JavaScriptSerializer();
var jsonTranList = jsonSerialiser.Serialize(block.Transactions);
var hashString = block.Index.ToString() + block.Timestamp + jsonTranList + block.PrevHash + block.Nonce;
var hash = CalculateHash(hashString);
block.Hash = hash;
while (block.Hash.Substring(0, diff) != diffVar)
{
block.Nonce += 1;
hashString = block.Index.ToString() + block.Timestamp + jsonTranList + block.PrevHash + block.Nonce;
hash = CalculateHash(hashString);
block.Hash = hash;
}
return 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 = "";
var hashString = block0.Index.ToString() + block0.Timestamp + 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 minedBlocked = MineBlock(x);
if (result != false)
{
if (currentBlock.Hash != minedBlocked.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();
}