Skip to content

Commit

Permalink
Minor fixes in SDGroup code. (#270)
Browse files Browse the repository at this point in the history
(cherry picked from commit b026525)

Signed-off-by: Ryan Griffiths <[email protected]>
  • Loading branch information
mergify[bot] authored Mar 3, 2023
1 parent 48f402c commit 14dcda8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1016,14 +1016,14 @@ public Collection<SDEndorser> getEndorsers() {
boolean ignoreList(Collection<String> names) {
HashSet<String> bnames = new HashSet<>(names);
endorsers.removeIf(endorser -> bnames.contains(endorser.getEndpoint()));
return endorsers.size() >= required;
return endorsers.size() >= getStillRequired();
}

//returns true if there are still sufficent endorsers for this group.
boolean ignoreListSDEndorser(Collection<SDEndorser> sdEndorsers) {
HashSet<SDEndorser> bnames = new HashSet<>(sdEndorsers);
endorsers.removeIf(bnames::contains);
return endorsers.size() >= required;
return endorsers.size() >= getStillRequired();
}

// retrun true if th endorsements have been meet.
Expand All @@ -1041,7 +1041,7 @@ boolean endorsedList(Collection<SDEndorser> sdEndorsers) {

endorsers.removeIf(endorser -> {
if (enames.contains(endorser.getEndpoint())) {
endorsed = Math.min(required, endorsed++);
endorsed = Math.min(required, endorsed + 1);
return true; // remove it.
}
return false; // needs to stay in the list.
Expand Down
43 changes: 43 additions & 0 deletions src/test/java/org/hyperledger/fabric/sdk/ServiceDiscoveryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static org.hyperledger.fabric.sdk.ServiceDiscovery.SDEndorserState;
import static org.hyperledger.fabric.sdk.ServiceDiscovery.SDLayout;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -335,6 +336,48 @@ public void twoLayoutTwoTwoEachExtrasCommonRandom() throws Exception {

}

/**
* Test to ensure layouts and groups are correctly classified
* as (un)satisfied and (un)satisfiable during proposal response
* collection by the methods `endorsedList` and `ignoreListSDEndorser`.
*/
@Test
public void loopGoodAndBadTest() {

// Let's say we have a group with 3 endorsers of which we require 2.
final int requiredInGroup = 2;
SDEndorser endorser1 = new MockSDEndorser("org1", "localhost:81", 0);
SDEndorser endorser2 = new MockSDEndorser("org1", "localhost:82", 0);
SDEndorser endorser3 = new MockSDEndorser("org1", "localhost:83", 0);
LinkedList<SDEndorser> endorsers = new LinkedList<>();
endorsers.add(endorser1);
endorsers.add(endorser2);
endorsers.add(endorser3);
SDLayout layout = new SDLayout();
layout.addGroup("G0", requiredInGroup, endorsers);
SDLayout.SDGroup group = layout.getSDLGroups().iterator().next();

// If 'endorser1' endorses successfully call `endorsedList`,
// the group should not be satisfied as we still require 1 more endorser.
LinkedList<SDEndorser> loopGood = new LinkedList<>();
loopGood.add(endorser1);
assertFalse(layout.endorsedList(loopGood)); // false i.e. not yet satisfied.
assertEquals(1, group.getStillRequired());

// If 'endorser2' fails to endorse call `ignoreListSDEndorser`,
// the group should still be satisfiable as we still have 1 more endorser to try.
LinkedList<SDEndorser> loopBad = new LinkedList<>();
loopBad.add(endorser2);
assertTrue(layout.ignoreListSDEndorser(loopBad)); // true i.e. still possible to satisfy.
assertEquals(1, group.getStillRequired());

// If 'endorser3' endorses, then the group should be satisfied.
loopGood = new LinkedList<>();
loopGood.add(endorser3);
assertTrue(layout.endorsedList(loopGood)); // true i.e .satisfied.
assertEquals(0, group.getStillRequired());
}

private static class MockSDEndorser extends SDEndorser {
private MockSDEndorser(String mspid, String endpoint, long ledgerHeight) {
super();
Expand Down

0 comments on commit 14dcda8

Please sign in to comment.