-
Notifications
You must be signed in to change notification settings - Fork 0
/
Block.java
177 lines (148 loc) · 4.14 KB
/
Block.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
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
import java.sql.Timestamp;
import java.util.ArrayList;
public class Block{
private String uBlokckID;
private Timestamp creationTime;
public long createTime=0;
public long receiveTime=0;
private String creatorID;
private Block parentBlock;
private int depth = 0;
//********for the purpose of tracking receiver nodes appending time
private int receiverNode;
public long minTime=0;
private int receiverNodeWithMinTime;
//******************************************************************
//This list contains all the transactions included in the block
private ArrayList<String> childList = new ArrayList<String>();
private int numChild = 0;
public ArrayList<Transaction> txnList = new ArrayList<Transaction>();
Block(){
//default constructor
}
Block(String uBlokckID, Timestamp creationTime, String creatorID, Block parentBlock, ArrayList<Transaction> txnList){
// System.out.println("1111111");
this.uBlokckID = uBlokckID;
this.creationTime = creationTime;
this.creatorID = creatorID;
this.parentBlock = parentBlock;
this.depth = parentBlock.getDepth()+1;
if(txnList!=null){
this.txnList = txnList;
}
}
Block(String uBlokckID, Timestamp creationTime){
this.uBlokckID = uBlokckID;
this.creationTime = creationTime;
this.creatorID = "satoshi";
this.parentBlock = null;
this.depth = 0;
}
Block(Block b){
this.uBlokckID = b.getBlockID();
this.creationTime = b.getCreationTime();
this.creatorID = b.getCreatorID();
this.parentBlock = b.getParentBlock();
this.depth = b.depth;
this.childList = b.getChildList();
this.numChild = this.childList.size();
this.txnList = b.getTxnList();
}
//get transcation list;
public ArrayList<Transaction> getTxnList(){
return this.txnList;
}
//function to add txns to a block
public void addTxn(Transaction newTxn){
this.txnList.add(newTxn);
}
//returning a transaction from the block using correspondng transaction id
public Transaction getTxn(String txnID){
for(int i = 0; i< this.txnList.size(); i++){
if(txnList.get(i).getTxnID().equals(txnID)){
return txnList.get(i);
}
}
return null;
}
//to check whether a txn with particular id has been there in the list or not
public boolean containsTxn(String txnID){
for(int i = 0; i<this.txnList.size(); i++){
if(txnList.get(i).getTxnID().equals(txnID)){
return true;
}
}
return false;
}
//To store all list of the childIDs
public ArrayList<String> getChildList(){
return childList;
}
public void putChild(String newChildID){
childList.add(numChild++, newChildID);
}
public boolean checkChild(String childID){
for(int i=0; i<numChild; i++){
if(childID.equals(childList.get(i))){
return true;
}
}
return false;
}
//to return block ID
public String getBlockID(){
return uBlokckID;
}
//to return parent Block
public Block getParentBlock(){
return parentBlock;
}
//to return block id of the parent node
public String getParentBlockID(){
return this.parentBlock.getBlockID();
}
//to return id of the creator of the block
public String getCreatorID(){
return this.creatorID;
}
//to set a new parent block
public void setParentBlock(String newParentBlock){
this.parentBlock = parentBlock;
}
//to return creation time of the block
public Timestamp getCreationTime(){
return creationTime;
}
//to get the depth of the current block
public int getDepth(){
return this.depth;
}
public void setReceiverNode(int b)
{
this.receiverNode =b;
}
public int getReceiverNode()
{
return this.receiverNode;
}
//To check whether the block is genesys or not
public boolean checkGenesis(){
if(this.uBlokckID.equals("genesis")){
return true;
}
return false;
}
//Do not see any use of the follwing two function
public void printBlock(String ident){
System.out.println(ident+"Block UID:" + this.uBlokckID);
System.out.println(ident+"Creation Time:" + this.creationTime);
System.out.println(ident+"Creator ID:" + this.creatorID);
System.out.println(ident+"Previous Block UID:" + this.parentBlock.getBlockID());
}
public boolean matchBlockID(String newID){
if(this.uBlokckID.equals(newID)){
return true;
}
return false;
}
}