This repository was 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
Feature: Improved CW Calculation #1451
Merged
+219
−211
Merged
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
a419063
added recursive weight calc
855ec6a
Added ignore to failed test and changed cum-weight test to recursive …
9951d39
Changed to queue
676eef4
Added some map optimizations and decreased db usage
2966d99
Implement recursive in codebase
ccd5536
Merge branch 'dev' into daf-cw
fbaf71d
Merge branch 'dev' into daf-cw
10e3f16
Removed backup cache
77fde23
Merge branch 'daf-cw' of https://github.com/kwek20/iri into daf-cw
ed951f1
Removed recursiveness
34ee23b
Merge branch 'dev' into daf-cw
4953955
Review comments
525d687
Merge branch 'dev' into daf-cw
dba6828
Merge branch 'daf-cw' of https://github.com/kwek20/iri into daf-cw
1ae6a80
Renamed and used Cum weight calc, added some javadoc, improved the al…
4d067a1
single depth search, removed hasAll
1b0aaff
Simplified check
29c40a0
Fixed circular -> self referencing
babc42f
Updated description
d410622
Removed link
dfa4599
Removed HashId need
3f5d176
Fixed Transformingmap usage in tests
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you can use regular |
||
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)) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Get rid of |
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe call it
hashToWeight
orhashWeightMap
.This variable has an integer name
There was a problem hiding this comment.
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