blockListeners addListener new Listener Block final class Entry implem

 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
blockListeners.addListener(new Listener<Block>() {
final class Entry implements Comparable<Entry> {
final Account account;
final long time;
Entry(Account account, long time) {
this.account = account;
this.time = time;
}
@Override
public int compareTo(Entry entry) {
if (this.time < entry.time) {
return -1;
} else if (this.time > entry.time) {
return 1;
} else {
long thisAccountId = this.account.getId(), thatAccountId = entry.account.getId();
if (thisAccountId < thatAccountId) {
return -1;
} else if (thisAccountId > thatAccountId) {
return 1;
} else {
return 0;
}
}
}
}
@Override
public void notify(Block block) {
List<Entry> entries = new LinkedList<>();
Block curBlock = Nxt.getBlockchain().getLastBlock();
for (Account account : Account.getAllAccounts(0, Integer.MAX_VALUE)) {
if (account.getEffectiveBalanceNXT() > 0) {
if (account.getPublicKey() != null) {
long time = Generator.getHitTime(account, curBlock);
entries.add(new Entry(account, time));
}
}
}
Entry[] sortedEntries = entries.toArray(new Entry[entries.size()]);
Arrays.sort(sortedEntries);
StringBuilder sb = new StringBuilder("Block "+block.getHeight());
sb.append(", forger: " + Convert.rsAccount(block.getGeneratorId()));
sb.append("\t\tNext 10 forgers: ");
for(int i=0; i<10; i++){
sb.append(Convert.rsAccount(sortedEntries[i].account.getId()));
if(i<9) {
sb.append(",");
}
}
System.out.println(sb);
}
}, Event.AFTER_BLOCK_APPLY);