Skip to content

Commit

Permalink
ignore clique proposals for address 0
Browse files Browse the repository at this point in the history
  • Loading branch information
jframe committed Feb 11, 2019
1 parent d6bfbdb commit cca4b15
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,12 @@ public String getName() {
public JsonRpcResponse response(final JsonRpcRequest request) {
final Address address = parameters.required(request.getParams(), 0, Address.class);
final Boolean auth = parameters.required(request.getParams(), 1, Boolean.class);
if (auth) {
proposer.auth(address);
} else {
proposer.drop(address);
if (!address.isZero()) {
if (auth) {
proposer.auth(address);
} else {
proposer.drop(address);
}
}
// Return true regardless, the vote is always recorded
return new JsonRpcSuccessResponse(request.getId(), true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,55 @@ public class ProposeTest {

@Test
public void testAuth() {
final VoteProposer proposer = new VoteProposer();
final Propose propose = new Propose(proposer, new JsonRpcParameter());
final Address a1 = Address.fromHexString("1");

final JsonRpcResponse response = propose.response(requestWithParams(a1, true));

assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.ADD));
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}

@Test
public void testAuthWithAddressZeroIsIgnored() {
final VoteProposer proposer = new VoteProposer();
final Propose propose = new Propose(proposer, new JsonRpcParameter());
final Address a0 = Address.fromHexString("0");

final JsonRpcResponse response = propose.response(requestWithParams(a0, true));

assertThat(proposer.get(a0)).isEqualTo(Optional.of(VoteType.ADD));
assertThat(proposer.get(a0)).isEqualTo(Optional.empty());
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}

@Test
public void testDrop() {
final VoteProposer proposer = new VoteProposer();
final Propose propose = new Propose(proposer, new JsonRpcParameter());
final Address a1 = Address.fromHexString("1");

final JsonRpcResponse response = propose.response(requestWithParams(a1, false));

assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.DROP));
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
}

@Test
public void testDropWithAddressZeroIsIgnored() {
final VoteProposer proposer = new VoteProposer();
final Propose propose = new Propose(proposer, new JsonRpcParameter());
final Address a0 = Address.fromHexString("0");

final JsonRpcResponse response = propose.response(requestWithParams(a0, false));

assertThat(proposer.get(a0)).isEqualTo(Optional.of(VoteType.DROP));
assertThat(proposer.get(a0)).isEqualTo(Optional.empty());
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
Expand All @@ -63,12 +91,12 @@ public void testDrop() {
public void testRepeatAuth() {
final VoteProposer proposer = new VoteProposer();
final Propose propose = new Propose(proposer, new JsonRpcParameter());
final Address a0 = Address.fromHexString("0");
final Address a1 = Address.fromHexString("1");

proposer.auth(a0);
final JsonRpcResponse response = propose.response(requestWithParams(a0, true));
proposer.auth(a1);
final JsonRpcResponse response = propose.response(requestWithParams(a1, true));

assertThat(proposer.get(a0)).isEqualTo(Optional.of(VoteType.ADD));
assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.ADD));
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
Expand All @@ -78,12 +106,12 @@ public void testRepeatAuth() {
public void testRepeatDrop() {
final VoteProposer proposer = new VoteProposer();
final Propose propose = new Propose(proposer, new JsonRpcParameter());
final Address a0 = Address.fromHexString("0");
final Address a1 = Address.fromHexString("1");

proposer.drop(a0);
final JsonRpcResponse response = propose.response(requestWithParams(a0, false));
proposer.drop(a1);
final JsonRpcResponse response = propose.response(requestWithParams(a1, false));

assertThat(proposer.get(a0)).isEqualTo(Optional.of(VoteType.DROP));
assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.DROP));
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
Expand All @@ -93,12 +121,12 @@ public void testRepeatDrop() {
public void testChangeToAuth() {
final VoteProposer proposer = new VoteProposer();
final Propose propose = new Propose(proposer, new JsonRpcParameter());
final Address a0 = Address.fromHexString("0");
final Address a1 = Address.fromHexString("1");

proposer.drop(a0);
final JsonRpcResponse response = propose.response(requestWithParams(a0, true));
proposer.drop(a1);
final JsonRpcResponse response = propose.response(requestWithParams(a1, true));

assertThat(proposer.get(a0)).isEqualTo(Optional.of(VoteType.ADD));
assertThat(proposer.get(a1)).isEqualTo(Optional.of(VoteType.ADD));
assertThat(response.getType()).isEqualTo(JsonRpcResponseType.SUCCESS);
final JsonRpcSuccessResponse successResponse = (JsonRpcSuccessResponse) response;
assertThat(successResponse.getResult()).isEqualTo(true);
Expand All @@ -108,7 +136,7 @@ public void testChangeToAuth() {
public void testChangeToDrop() {
final VoteProposer proposer = new VoteProposer();
final Propose propose = new Propose(proposer, new JsonRpcParameter());
final Address a0 = Address.fromHexString("0");
final Address a0 = Address.fromHexString("1");

proposer.auth(a0);
final JsonRpcResponse response = propose.response(requestWithParams(a0, false));
Expand Down

0 comments on commit cca4b15

Please sign in to comment.