Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.

Feature: Improved CW Calculation #1451

Merged
merged 22 commits into from
Jul 31, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -5,9 +5,7 @@
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import org.apache.commons.collections4.CollectionUtils;

@@ -55,53 +53,49 @@ private UnIterableMap<HashId, Integer> calculateRatingDfs(Hash entryPoint) throw

// Estimated capacity per depth, assumes 5 minute gap in between milestones, at 3tps
UnIterableMap<HashId, Integer> hashWeight = createTxHashToCumulativeWeightMap( 5 * 60 * 3 * depth);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe call it hashToWeight or hashWeightMap.
This variable has an integer name

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you changed it in one place, but you didn't change it in the others


Deque<Hash> stack = new ArrayDeque<>();
Map<Hash, ArrayDeque<Hash>> txToDirectApprovers = new HashMap<>();

Map<Hash, HashSet<Hash>> txToDirectApprovers = new HashMap<>();

Deque<Hash> stack = new ArrayDeque<>();
stack.push(entryPoint);

while (CollectionUtils.isNotEmpty(stack)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can use regular !isEmpty() let's get rid of CollectionUtils

Hash txHash = stack.peek();
if (!hashWeight.containsKey(txHash)) {
Collection<Hash> appHashes = getTxDirectApproversHashes(txHash, txToDirectApprovers);
if (CollectionUtils.isNotEmpty(appHashes)) {
Hash txApp = getAndRemoveApprover(appHashes);
stack.push(txApp);
continue;
}
Hash txHash = stack.peekLast();

HashSet<Hash> approvers = getTxDirectApproversHashes(txHash, txToDirectApprovers);
if (null != approvers && (approvers.size() == 0 || hasAll(hashWeight, approvers, stack))) {
approvers.add(txHash);
hashWeight.put(txHash, getRating(approvers, txToDirectApprovers));
stack.removeLast();
} else {
stack.pop();
continue;
stack.addAll(approvers);
}

HashSet<HashId> set = new HashSet<>();
set.add(txHash);

int rating = txHash.equals(entryPoint) ? hashWeight.size() + 1 : getRating(txHash, set);
hashWeight.put(txHash, rating);
}

return hashWeight;
}

private int getRating(Hash hash, Set<HashId> seenHashes) throws Exception {
int weight = 1;

ArrayDeque<Hash> approvers = getTxDirectApproversHashes(hash, null);
for (Hash approver : approvers) {
if (!seenHashes.contains(approver)) {
seenHashes.add(approver);
weight += getRating(approver, seenHashes);
private int getRating(HashSet<Hash> nonDupes, Map<Hash, HashSet<Hash>> txToDirectApprovers) throws Exception {
Deque<Hash> stack = new ArrayDeque<>(nonDupes);
while (CollectionUtils.isNotEmpty(stack)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Get rid of CollectionUtils

HashSet<Hash> approvers = getTxDirectApproversHashes(stack.pollLast(), txToDirectApprovers);
for (Hash hash : approvers) {
if (nonDupes.add(hash)) {
stack.add(hash);
}
}
}
return weight;

return nonDupes.size();
}

private Hash getAndRemoveApprover(Collection<Hash> appHashes) {
Iterator<Hash> hashIterator = appHashes.iterator();
Hash txApp = hashIterator.next();
hashIterator.remove();
return txApp;

private boolean hasAll(UnIterableMap<HashId, Integer> source, HashSet<Hash> requester, Deque<Hash> stack) {
for (Hash h : requester) {
if (!source.containsKey(h) && !stack.contains(h)) {
return false;
}
}
return true;
}

/**
@@ -113,15 +107,14 @@ private Hash getAndRemoveApprover(Collection<Hash> appHashes) {
* @return
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this empty?

* @throws Exception
*/
private ArrayDeque<Hash> getTxDirectApproversHashes(Hash txHash,
Map<Hash, ArrayDeque<Hash>> txToDirectApprovers)
private HashSet<Hash> getTxDirectApproversHashes(Hash txHash, Map<Hash, HashSet<Hash>> txToDirectApprovers)
throws Exception {

ArrayDeque<Hash> txApprovers = txToDirectApprovers.get(txHash);
HashSet<Hash> txApprovers = txToDirectApprovers.get(txHash);
if (txApprovers == null) {
ApproveeViewModel approvers = ApproveeViewModel.load(tangle, txHash);
Collection<Hash> appHashes = CollectionUtils.emptyIfNull(approvers.getHashes());
txApprovers = new ArrayDeque<>(appHashes.size());
txApprovers = new HashSet<>(appHashes.size());
for (Hash appHash : appHashes) {
// if not genesis (the tx that confirms itself)
if (!snapshotProvider.getInitialSnapshot().hasSolidEntryPoint(appHash)) {
@@ -130,7 +123,8 @@ private ArrayDeque<Hash> getTxDirectApproversHashes(Hash txHash,
}
txToDirectApprovers.put(txHash, txApprovers);
}
return txApprovers;

return new HashSet<Hash>(txApprovers);
}

private static UnIterableMap<HashId, Integer> createTxHashToCumulativeWeightMap(int size) {
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
package com.iota.iri.service.tipselection.impl;


import static com.iota.iri.TransactionTestUtils.getTransactionHash;
import static com.iota.iri.TransactionTestUtils.getTransactionTrits;
import static com.iota.iri.TransactionTestUtils.getTransactionTritsWithTrunkAndBranch;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;

import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.iota.iri.conf.MainnetConfig;
import com.iota.iri.controllers.ApproveeViewModel;
import com.iota.iri.controllers.TransactionViewModel;
@@ -13,18 +36,6 @@
import com.iota.iri.storage.Tangle;
import com.iota.iri.storage.rocksDB.RocksDBPersistenceProvider;
import com.iota.iri.utils.collections.interfaces.UnIterableMap;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

import static com.iota.iri.TransactionTestUtils.*;


public class CumulativeWeightCalculatorTest {