-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockChain.java
123 lines (99 loc) · 4.06 KB
/
BlockChain.java
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
import java.util.*;
class BlockChain{
List<Block> chain =new ArrayList<Block>();
Queue<transaction> pending_transactions = new LinkedList<transaction>();
public int diff = 1;
public double mineReward = 50;
private Block getLatestBlock(){
return chain.get(chain.size() - 1);
}
public Block getBlock(int index){
return chain.get(index);
}
//Creating genesis block for the chain
public void createGenesis(transaction trans){
Block newBlock = new Block(trans);
newBlock.pre_hash = null;
newBlock.mineBlock(this.diff);
chain.add(newBlock);
}
public void minePendingTransaction(String minerAddress){
//Setting miner reward transaction
transaction reward = new transaction(null, minerAddress, this.mineReward);
//Checking if there is any pending transactions
if (pending_transactions.size() > 0){
transaction trans = pending_transactions.remove();
//Setting previous hash of the block
Block newBlock = new Block(trans);
if (chain.size() == 0){
newBlock.pre_hash = null;
} else {
newBlock.pre_hash = getLatestBlock().hash;
}
//Mining the block and adding it to the chain
newBlock.mineBlock(this.diff);
newBlock.blockInfo();
chain.add(newBlock);
//Adding reward block to the chain
Block rewardBlock = new Block(reward);
rewardBlock.blockInfo();
chain.add(rewardBlock);
} else {
System.out.println("No pending transactions\n");
}
}
public void addTransaction(transaction trans){
Boolean verify = false;
double sender_balance = getBalance(trans.sender);
//Checking trans validity
try{
verify = trans.verify(trans.signature, trans.sender);
}catch(Exception e){}
if (trans.sender == null || trans.reciever == null || !verify){
System.out.println("Invalid transaction\n");
//Checking balance sufficiency
} else if ((sender_balance - trans.amount) < 0){
System.out.println("Insufficient Balance to complete the transaction:\n{");
System.out.println(" Sender: " + trans.sender);
System.out.println(" Reciever: " + trans.reciever);
System.out.println(" Amount: " + String.format("%.02f", trans.amount) + " $");
System.out.println(" Current Balance: " + sender_balance + " $\n}\n");
} else {
pending_transactions.add(trans);
}
}
public double getBalance(String address){
double balance = 0;
//Iterating through all blocks in the chain
for (int i = 0; i < chain.size(); i++){
if (chain.get(i).trans.sender == address){
balance -= chain.get(i).trans.amount;
} else if (chain.get(i).trans.reciever == address){
balance += chain.get(i).trans.amount;
} else {
continue;
}
}
String balanceStr = String.format("%.02f", balance);
balance = Double.parseDouble(balanceStr);
return balance;
}
public Boolean isChainValid(){
//Iterating through all blocks in the chain
for (int i = 1 ; i < chain.size(); i++){
//Checking for hash equality
if (!chain.get(i).hash.equals(chain.get(i).hashCalculate())){
return false;
}
//Checking for previous hash equality
if (chain.get(i).pre_hash != null && !chain.get(i).pre_hash.equals(chain.get(i-1).hashCalculate())){
return false;
}
//Checking block validity
if (!chain.get(i).isValid()){
return false;
}
}
return true;
}
}