-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockchain.java
More file actions
37 lines (30 loc) · 834 Bytes
/
Blockchain.java
File metadata and controls
37 lines (30 loc) · 834 Bytes
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
import java.util.ArrayList;
import java.util.List;
public class Blockchain {
private List<Block> blocks;
public Blockchain() {
this.blocks = new ArrayList<>();
}
public void addBlock(Block block) {
blocks.add(block);
}
public Block getBlock(int index) {
return blocks.get(index);
}
public int getSize() {
return blocks.size();
}
public String getLatestBlockHash() {
if (blocks.isEmpty()) {
return "0000000000000000"; // Genesis block hash
} else {
Block latestBlock = blocks.get(blocks.size() - 1);
return latestBlock.getHash();
}
}
public void setLatestBlockHash(String hash) {
if (!blocks.isEmpty()) {
blocks.get(blocks.size() - 1).setHash(hash);
}
}
}