-
Notifications
You must be signed in to change notification settings - Fork 0
/
Blockchain.java
91 lines (77 loc) · 3.63 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
import java.util.HashMap;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
public class Blockchain {
private static Node buildTree(int depth) {
Node root = new Node(0);
addChildren(root, 0, depth);
return root;
}
private static void addChildren(Node node, int currentDepth, int maxDepth) {
if (currentDepth < maxDepth) {
node.left = new Node(node.number * 2 + 1);
node.right = new Node(node.number * 2 + 2);
addChildren(node.left, currentDepth + 1, maxDepth);
addChildren(node.right, currentDepth + 1, maxDepth);
}
}
private static Map<Node, Set<Integer>> Voting(List<Validator> validators, Node current_node) {
Map<Node, Integer> votes = new HashMap<>();
Map<Node, Set<Integer>> validatorVotes = new HashMap<>();
for (Validator validator : validators) {
Node target = validator.vote(current_node);
votes.put(target, votes.getOrDefault(target, 0) + validator.deposit);
validatorVotes.computeIfAbsent(target, k -> new HashSet<>()).add(validator.id);
}
return validatorVotes;
}
private static Map.Entry<Node, Set<Integer>> superMajorityLink(List<Validator> validators, Node current_node, int totalDeposit) {
Map<Node, Set<Integer>> validatorVotes = Voting(validators, current_node);
int supermajorityThreshold = totalDeposit / 2;
for (Map.Entry<Node, Set<Integer>> entry : validatorVotes.entrySet()) {
int totalVotes = entry.getValue().stream().mapToInt(id -> validators.get(id).deposit).sum();
if (totalVotes > supermajorityThreshold) {
return entry;
}
}
return null;
}
public static void main(String[] args) {
// Constants
final int NUM_VALIDATORS = 10;
final int[] DEPOSITS = {500, 100, 300, 250, 150, 500, 600, 350, 200, 150};
// Initialize validators
List<Validator> validators = new ArrayList<>();
int totalDeposit = 0;
for (int i = 0; i < NUM_VALIDATORS; i++) {
validators.add(new Validator(i, DEPOSITS[i]));
totalDeposit += DEPOSITS[i];
}
// Building a binary tree of depth 10
Node checkpointTree = buildTree(10);
// Simulating the voting process and tracking validators who finalized each checkpoint
Map<Integer, Set<Integer>> finalizingValidatorsByCheckpoint = new HashMap<>();
Node current_node = checkpointTree;
List<Integer> blockchain = new ArrayList<>();
for (int i = 0; i < 10; i++) { // Simulating 10 rounds
Map.Entry<Node, Set<Integer>> supermajorityLink = superMajorityLink(validators, current_node, totalDeposit);
if (supermajorityLink != null) {
current_node = supermajorityLink.getKey();
blockchain.add(current_node.number);
finalizingValidatorsByCheckpoint.put(current_node.number, supermajorityLink.getValue());
} else {
break;
}
}
// Priting the completed blockchain
System.out.println("Congratulations! Your blockchain is complete.\nBlockchain: " + blockchain);
System.out.println("\nHere are all the validators that validated each checkpoint:");
// Printing the validators for each checkpoint
for (Map.Entry<Integer, Set<Integer>> entry : finalizingValidatorsByCheckpoint.entrySet()) {
System.out.println("Checkpoint: " + entry.getKey() + ", Validators: " + entry.getValue());
}
}
}