-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.cs
More file actions
111 lines (110 loc) · 3.29 KB
/
Copy pathUtilities.cs
File metadata and controls
111 lines (110 loc) · 3.29 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BlockchainTestCase
{
class Utilities
{
//Code thanks to Mike Danes <Wyck>
public static byte[] AddLittleEndian(byte[] a, byte[] b)
{
List<byte> result = new List<byte>();
if (a.Length < b.Length)
{
byte[] t = a;
a = b;
b = t;
}
int carry = 0;
for (int i = 0; i < b.Length; ++i)
{
int sum = a[i] + b[i] + carry;
result.Add((byte)(sum & 0xFF));
carry = sum >> 8;
}
for (int i = b.Length; i < a.Length; ++i)
{
int sum = a[i] + carry;
result.Add((byte)(sum & 0xFF));
carry = sum >> 8;
}
if (carry > 0)
{
result.Add((byte)carry);
}
return result.ToArray();
}
public static void PrintByteArray(byte[] a)
{
Console.Write("[");
for (int i = a.Length-1; i >= 0; i--)
{
if (i != a.Length-1)
{
Console.Write(", ");
}
byte item = a[i];
Console.Write(item);
}
Console.Write("]");
Console.WriteLine("");
}
public static String ByteArrayToString(byte[] a)
{
String rt = "[";
for (int i = a.Length - 1; i >= 0; i--)
{
if (i != a.Length - 1)
{
rt += ", ";
}
byte item = a[i];
rt += item;
}
rt += "]";
return rt;
}
public static bool BlockchainContainsTransaction(List<Block> blockchain, int id)
{
bool inside = false;
foreach(Block bl in blockchain)
{
if (TransactionListContainsTransaction(bl.transactions, id))
{
inside = true;
break;
}
}
return inside;
}
public static bool TransactionListContainsTransaction(List<Transaction> transactionList, int id)
{
bool inside = false;
foreach(Transaction tr in transactionList)
{
if (tr.id==id)
{
inside = true;
break;
}
}
return inside;
}
public static void PrintBlockchain(List<Block> blockchain)
{
foreach(Block bl in blockchain)
{
Console.WriteLine("Hash: "+ ByteArrayToString(bl.GetHash()));
Console.WriteLine("Previous: "+ ByteArrayToString(bl.prevHash));
Console.WriteLine("With transactions: ");
foreach(Transaction tr in bl.transactions)
{
Console.WriteLine(tr.id+": "+ByteArrayToString(tr.from)+" --> "+ByteArrayToString(tr.to)+ " , amount: "+tr.amount);
}
Console.WriteLine();
}
}
}
}