use of com.iota.iri.model.Hash in project iri by iotaledger.
the class API method getTransactionToApproveStatement.
public synchronized Hash[] getTransactionToApproveStatement(int depth, final String reference, final int numWalks) throws Exception {
int tipsToApprove = 2;
Hash[] tips = new Hash[tipsToApprove];
final SecureRandom random = new SecureRandom();
final int randomWalkCount = numWalks > maxRandomWalks || numWalks < 1 ? maxRandomWalks : numWalks;
Hash referenceHash = null;
int maxDepth = instance.tipsManager.getMaxDepth();
if (depth > maxDepth) {
depth = maxDepth;
}
if (reference != null) {
referenceHash = new Hash(reference);
if (!TransactionViewModel.exists(instance.tangle, referenceHash)) {
throw new RuntimeException(REFERENCE_TRANSACTION_NOT_FOUND);
} else {
TransactionViewModel transactionViewModel = TransactionViewModel.fromHash(instance.tangle, referenceHash);
if (transactionViewModel.snapshotIndex() != 0 && transactionViewModel.snapshotIndex() < instance.milestone.latestSolidSubtangleMilestoneIndex - depth) {
throw new RuntimeException(REFERENCE_TRANSACTION_TOO_OLD);
}
}
}
instance.milestone.latestSnapshot.rwlock.readLock().lock();
try {
Set<Hash> visitedHashes = new HashSet<>();
Map<Hash, Long> diff = new HashMap<>();
for (int i = 0; i < tipsToApprove; i++) {
tips[i] = instance.tipsManager.transactionToApprove(visitedHashes, diff, referenceHash, tips[0], depth, randomWalkCount, random);
// update world view, so next tips selected will be inter-consistent
if (tips[i] == null || !instance.ledgerValidator.updateDiff(visitedHashes, diff, tips[i])) {
return null;
}
}
API.incCounter_getTxToApprove();
if ((getCounter_getTxToApprove() % 100) == 0) {
String sb = "Last 100 getTxToApprove consumed " + API.getEllapsedTime_getTxToApprove() / 1000000000L + " seconds processing time.";
log.info(sb);
counter_getTxToApprove = 0;
ellapsedTime_getTxToApprove = 0L;
}
if (instance.ledgerValidator.checkConsistency(Arrays.asList(tips))) {
return tips;
}
} finally {
instance.milestone.latestSnapshot.rwlock.readLock().unlock();
}
throw new RuntimeException("inconsistent tips pair selected");
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class API method readPreviousEpochsSpentAddresses.
private void readPreviousEpochsSpentAddresses() {
if (!SignedFiles.isFileSignatureValid("/previousEpochsSpentAddresses.txt", "/previousEpochsSpentAddresses.sig", Snapshot.SNAPSHOT_PUBKEY, Snapshot.SNAPSHOT_PUBKEY_DEPTH, Snapshot.SPENT_ADDRESSES_INDEX)) {
throw new RuntimeException("Failed to load previousEpochsSpentAddresses - signature failed.");
}
InputStream in = Snapshot.class.getResourceAsStream("/previousEpochsSpentAddresses.txt");
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
String line;
try {
while ((line = reader.readLine()) != null) {
previousEpochsSpentAddresses.put(new Hash(line), true);
}
} catch (IOException e) {
log.error("Failed to load previousEpochsSpentAddresses.");
}
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class API method getBalancesStatement.
private AbstractResponse getBalancesStatement(final List<String> addrss, final List<String> tips, final int threshold) throws Exception {
if (threshold <= 0 || threshold > 100) {
return ErrorResponse.create("Illegal 'threshold'");
}
final List<Hash> addresses = addrss.stream().map(address -> (new Hash(address))).collect(Collectors.toCollection(LinkedList::new));
final List<Hash> hashes;
final Map<Hash, Long> balances = new HashMap<>();
instance.milestone.latestSnapshot.rwlock.readLock().lock();
final int index = instance.milestone.latestSnapshot.index();
if (tips == null || tips.size() == 0) {
hashes = Collections.singletonList(instance.milestone.latestSolidSubtangleMilestone);
} else {
hashes = tips.stream().map(address -> (new Hash(address))).collect(Collectors.toCollection(LinkedList::new));
}
try {
for (final Hash address : addresses) {
Long value = instance.milestone.latestSnapshot.getBalance(address);
if (value == null) {
value = 0L;
}
balances.put(address, value);
}
final Set<Hash> visitedHashes;
final Map<Hash, Long> diff;
visitedHashes = new HashSet<>();
diff = new HashMap<>();
for (Hash tip : hashes) {
if (!TransactionViewModel.exists(instance.tangle, tip)) {
return ErrorResponse.create("Tip not found: " + tip.toString());
}
if (!instance.ledgerValidator.updateDiff(visitedHashes, diff, tip)) {
return ErrorResponse.create("Tips are not consistent");
}
}
diff.forEach((key, value) -> balances.computeIfPresent(key, (hash, aLong) -> value + aLong));
} finally {
instance.milestone.latestSnapshot.rwlock.readLock().unlock();
}
final List<String> elements = addresses.stream().map(address -> balances.get(address).toString()).collect(Collectors.toCollection(LinkedList::new));
return GetBalancesResponse.create(elements, hashes.stream().map(h -> h.toString()).collect(Collectors.toList()), index);
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class TipsManager method scanTipsForSolidity.
private void scanTipsForSolidity() throws Exception {
int size = tipsViewModel.nonSolidSize();
if (size != 0) {
Hash hash = tipsViewModel.getRandomNonSolidTipHash();
boolean isTip = true;
if (hash != null && TransactionViewModel.fromHash(tangle, hash).getApprovers(tangle).size() != 0) {
tipsViewModel.removeTipHash(hash);
isTip = false;
}
if (hash != null && isTip && transactionValidator.checkSolidity(hash, false)) {
// if(hash != null && TransactionViewModel.fromHash(hash).isSolid() && isTip) {
tipsViewModel.setSolid(hash);
}
}
}
use of com.iota.iri.model.Hash in project iri by iotaledger.
the class TipsManager method markovChainMonteCarlo.
Hash markovChainMonteCarlo(final Set<Hash> visitedHashes, final Map<Hash, Long> diff, final Hash tip, final Hash extraTip, final Map<Hash, Long> ratings, final int iterations, final int maxDepth, final Set<Hash> maxDepthOk, final Random seed) throws Exception {
Map<Hash, Integer> monteCarloIntegrations = new HashMap<>();
Hash tail;
for (int i = iterations; i-- > 0; ) {
tail = randomWalk(visitedHashes, diff, tip, extraTip, ratings, maxDepth, maxDepthOk, seed);
if (monteCarloIntegrations.containsKey(tail)) {
monteCarloIntegrations.put(tail, monteCarloIntegrations.get(tail) + 1);
} else {
monteCarloIntegrations.put(tail, 1);
}
}
return monteCarloIntegrations.entrySet().stream().reduce((a, b) -> {
if (a.getValue() > b.getValue()) {
return a;
} else if (a.getValue() < b.getValue()) {
return b;
} else if (seed.nextBoolean()) {
return a;
} else {
return b;
}
}).map(Map.Entry::getKey).orElse(null);
}
Aggregations