This repository has been archived by the owner on Aug 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 370
/
Snapshot.java
175 lines (156 loc) · 6.93 KB
/
Snapshot.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
package com.iota.iri;
import com.iota.iri.hash.Curl;
import com.iota.iri.hash.ISS;
import com.iota.iri.hash.Sponge;
import com.iota.iri.hash.SpongeFactory;
import com.iota.iri.model.Hash;
import com.iota.iri.controllers.TransactionViewModel;
import com.iota.iri.utils.Converter;
import org.apache.commons.lang3.ArrayUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.*;
import java.util.stream.Collectors;
public class Snapshot {
private static final Logger log = LoggerFactory.getLogger(Snapshot.class);
private static String SNAPSHOT_PUBKEY = "TTXJUGKTNPOOEXSTQVVACENJOQUROXYKDRCVK9LHUXILCLABLGJTIPNF9REWHOIMEUKWQLUOKD9CZUYAC";
private static int SNAPSHOT_PUBKEY_DEPTH = 6;
private static int SNAPSHOT_INDEX = 1;
public static final Map<Hash, Long> initialState = new HashMap<>();
public static final Snapshot initialSnapshot;
static {
InputStream in = Snapshot.class.getResourceAsStream("/Snapshot.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
Sponge curl = SpongeFactory.create(SpongeFactory.Mode.KERL);
int[] trit_value;
int[] trits = new int[Curl.HASH_LENGTH*3];
try {
while((line = reader.readLine()) != null) {
trit_value = Converter.trits(Converter.asciiToTrytes(line));
System.arraycopy(trit_value, 0, trits, 0, trit_value.length);
curl.absorb(trits, 0, trits.length);
Arrays.fill(trits, 0);
String[] parts = line.split(";", 2);
if (parts.length >= 2)
{
String key = parts[0];
String value = parts[1];
initialState.put(new Hash(key), Long.valueOf(value));
}
}
{ // Check snapshot signature
trits = new int[Curl.HASH_LENGTH];
curl.squeeze(trits, 0, Curl.HASH_LENGTH);
SpongeFactory.Mode mode = SpongeFactory.Mode.CURLP81;
int[] digests = new int[0];
int[] bundle = ISS.normalizedBundle(trits);
int[] root = null;
int i;
in = Snapshot.class.getResourceAsStream("/Snapshot.sig");
reader = new BufferedReader(new InputStreamReader(in));
for(i = 0; i < 3 && (line = reader.readLine()) != null; i++) {
digests = ArrayUtils.addAll(
digests,
ISS.digest(mode
, Arrays.copyOfRange(bundle, i*ISS.NORMALIZED_FRAGMENT_LENGTH, (i+1)*ISS.NORMALIZED_FRAGMENT_LENGTH)
, Converter.trits(line)));
}
if((line = reader.readLine()) != null) {
root = ISS.getMerkleRoot(mode, ISS.address(mode, digests), Converter.trits(line), 0, SNAPSHOT_INDEX, SNAPSHOT_PUBKEY_DEPTH);
} else {
root = ISS.address(mode, digests);
}
if(!Arrays.equals(Converter.trits(SNAPSHOT_PUBKEY), root)) {
throw new RuntimeException("Snapshot signature failed.");
}
}
} catch (IOException e) {
System.out.println("Failed to load snapshot.");
System.exit(-1);
}
initialSnapshot = new Snapshot(initialState, 0);
if(!initialSnapshot.isConsistent()) {
System.out.println("Initial Snapshot inconsistent.");
System.exit(-1);
}
}
public static final Object latestSnapshotSyncObject = new Object();
private final Map<Hash, Long> state;
private int index;
public int index() {
return index;
}
public Snapshot(Snapshot snapshot) {
state = new HashMap<>(snapshot.state);
this.index = snapshot.index;
}
private Snapshot(Map<Hash, Long> initialState, int index) {
state = new HashMap<>(initialState);
this.index = index;
}
public Map<Hash, Long> getState() {
return state;
}
public Map<Hash, Long> diff(Map<Hash, Long> newState) {
return newState.entrySet().parallelStream()
.map(hashLongEntry ->
new HashMap.SimpleEntry<>(hashLongEntry.getKey(),
hashLongEntry.getValue() -
(state.containsKey(hashLongEntry.getKey()) ?
state.get(hashLongEntry.getKey()): 0) ))
.filter(e -> e.getValue() != 0L)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}
public Snapshot patch(Map<Hash, Long> diff, int index) {
Map<Hash, Long> patchedState = state.entrySet().parallelStream()
.map( hashLongEntry ->
new HashMap.SimpleEntry<>(hashLongEntry.getKey(),
hashLongEntry.getValue() +
(diff.containsKey(hashLongEntry.getKey()) ?
diff.get(hashLongEntry.getKey()) : 0)) )
.filter(e -> e.getValue() != 0L)
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
diff.entrySet().stream()
.filter(e -> e.getValue() > 0L)
.forEach(e -> patchedState.putIfAbsent(e.getKey(), e.getValue()));
return new Snapshot(patchedState, index);
}
void merge(Snapshot snapshot) {
state.clear();
state.putAll(snapshot.state);
index = snapshot.index;
}
boolean isConsistent() {
long stateValue = state.values().stream().reduce(Math::addExact).orElse(Long.MAX_VALUE);
if(stateValue != TransactionViewModel.SUPPLY) {
long difference = TransactionViewModel.SUPPLY - stateValue;
log.info("Transaction resolves to incorrect ledger balance: {}", difference);
return false;
}
final Iterator<Map.Entry<Hash, Long>> stateIterator = state.entrySet().iterator();
while (stateIterator.hasNext()) {
final Map.Entry<Hash, Long> entry = stateIterator.next();
if (entry.getValue() <= 0) {
if (entry.getValue() < 0) {
log.info("Skipping negative value for address: " + entry.getKey() + ": " + entry.getValue());
return false;
}
stateIterator.remove();
}
//////////// --Coo only--
/*
* if (entry.getValue() > 0) {
*
* System.out.ln("initialState.put(new Hash(\"" + entry.getKey()
* + "\"), " + entry.getValue() + "L);"); }
*/
////////////
}
return true;
}
}