Refactoring the code of voter-registry according to Arbel Peled CR

Voter-Registry
Vladimir Eliezer Tokarev 2016-02-19 06:17:43 -08:00
parent 91e41af7a0
commit bb4cc5b087
13 changed files with 2608 additions and 301 deletions

7
.gitignore vendored
View File

@ -13,8 +13,5 @@ out
*.prefs
*.project
*.classpath
bulletin-board-server/local-instances/meerkat.db
/SQLiteDBTest.db
/bulletin-board-server/SQLiteDBTest.db
arcanist/*
libphutil/*
*.db
Wombat Code And Documentation Conventions

View File

@ -1,10 +0,0 @@
This file describes Wombat Code & Documentation Conventions:
Code Conventions:
* Code- The good old classic java code conventions
camelCase convention, constants should be capital letters with underscore etc...
Documentation Conventions:
* Comments- The good old classic java code documentation, Block Comments (to describe
method/class/interface etc...) // comments too describe complex code
(only if the code block complicated)

View File

@ -1,8 +1,4 @@
/protoc.exe
/bin/
/comment-info.txt
/main/java/meerkat/manualTests.*
/SQLiteDBTest.db
/bulletin-board-server/SQLiteDBTest.db
*.db
/src/main/java/meerkat/manualTests.java
/.gitignore

View File

@ -1,47 +0,0 @@
package meerkat;
import java.util.List;
/**
* Created by Vladimir Eliezer Tokarev on 1/22/2016.
* Those methods will be called when their parallel methods ends running
* parallel means for instance first some registry will post voter then call HandleVoterPosted
*/
public interface RegistryCallBack {
/**
* Runs logic after voter have been posted
* @param succeeded shows if the adding of the voter succeeded
*/
void HandleVoterAdded(boolean succeeded);
/**
* Runs logic after voter have been removed form group
* @param succeeded shows if the removing of the voter succeeded
*/
void HandleVoterAddedToGroup(boolean succeeded);
/**
* Runs logic after voter have been added form group
* @param succeeded shows if the adding of the voter succeeded
*/
void HandleVoterRemovedFromGroup(boolean succeeded);
/**
* Runs logic after voter have voted
* @param succeeded shows if the voter voting have been documented successfully
*/
void HandleSetVoted(boolean succeeded);
/**
* Handles the voter information
* @param voterInformation list of information of the voter (its id, and personal data)
*/
void HandleVoterInfo(List<String> voterInformation);
/**
* Handles the groups the voter in
* @param groupsVoterIn list of groups ids of the voter
*/
void HandleVoterGroups(List<String> groupsVoterIn);
}

View File

@ -1,66 +0,0 @@
package meerkat;
/**
* Created by Vladimir Eliezer Tokarev on 1/22/2016.
* provides voters management options
*/
public interface RegistryInstance {
/**
* Adds new voter to the bulletin-board
*
* @param voterID id of the new user
* @param personalData for example residence location
* @param callBack when the adding voter done callBack.HandleVoterAdded will be called
* @return void
*/
void AddVoter(String voterID, String personalData, RegistryCallBack callBack);
/**
* Adding given voter to given group
*
* @param voterID the voter id
* @param groupID the group id to which we want add the voter
* @param callBack when the adding voter done callBack.HandleVoterAddedToGroup will be called
* @return true if the adding action succeeded else return false
*/
void AddToGroup(String voterID, String groupID, RegistryCallBack callBack);
/**
* Removes given voter from given group
*
* @param voterID the voter id
* @param groupID the group from which we want to remove the user
* @param callBack when the adding voter done callBack.HandleVoterRemovedFromGroup will be called
* @return true if the removing action succeeded else return false
*/
void RemoveFromGroup(String voterID, String groupID, RegistryCallBack callBack);
/**
* Sets that the voter have voted
*
* @param id id tag string
* @param callBack when the adding voter done callBack.HandleSetVoted will be called
* @return true if the set voted succeed else false
*/
void SetVoted(String id, RegistryCallBack callBack);
/**
* Requests all the groups that the given id voter is in
*
* @param id id tag string
* @param callBack when the adding voter done callBack.HandleVoterGroups will be called
* @return list of groups ids (or names), if the method fails its empty
*/
void GetGroups(String id, RegistryCallBack callBack);
/**
* Retrieves list of strings that represents voter
*
* @param id id tag string
* @param callBack when the adding voter done callBack.HandleVoterInfo will be called
* @return list of strings (empty list if the lookup failed)
*/
void GetPersonIDDetails(String id, RegistryCallBack callBack);
}

File diff suppressed because it is too large Load Diff

View File

@ -1,11 +1,7 @@
package meerkat;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import meerkat.bulletinboard.SimpleBulletinBoardClient;
import meerkat.comm.CommunicationException;
import meerkat.crypto.Encryption;
import meerkat.bulletinboard.BulletinBoardClient;
import meerkat.crypto.DigitalSignature;
import meerkat.protobuf.BulletinBoardAPI;
import meerkat.protobuf.BulletinBoardAPI.BulletinBoardMessage;
import meerkat.protobuf.BulletinBoardAPI.MessageFilter;
@ -15,27 +11,23 @@ import meerkat.protobuf.Crypto;
import util.AccurateTimestamp;
import util.RegistryTags;
import java.io.IOException;
import java.text.ParseException;
import java.security.SignatureException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Random;
import static util.CollectionMessagesUtils.*;
/**
* Created by Vladimir Eliezer Tokarev on 1/8/2016.
* Gives the ability to synchronously manage voters information
*/
public class SimpleRegistry implements RegistryInstance{
public class SimpleRegistry implements VoterRegistry{
protected Encryption signatory;
protected SimpleBulletinBoardClient communicator;
protected DigitalSignature signatory;
protected BulletinBoardClient bulletinBoardClient ;
protected RegistryCallBack callback;
public SimpleRegistry(Encryption signatory, SimpleBulletinBoardClient communicator) {
public SimpleRegistry(DigitalSignature signatory, BulletinBoardClient communicator) {
this.signatory = signatory;
this.communicator = communicator;
this.bulletinBoardClient = communicator;
}
/**
@ -49,19 +41,15 @@ public class SimpleRegistry implements RegistryInstance{
BulletinBoardMessage.Builder bulletinBoardMessage =
BulletinBoardMessage.newBuilder();
Crypto.RerandomizableEncryptedMessage encryptedMessage =
signatory.encrypt(basicMessage, signatory.generateRandomness(new Random()));
Crypto.Signature.Builder messageSignature = Crypto.Signature.newBuilder();
messageSignature.setType(Crypto.SignatureType.ECDSA)
.setData(encryptedMessage.toByteString());
signatory.updateContent(basicMessage);
Crypto.Signature signature = signatory.sign();
bulletinBoardMessage.setMsg(basicMessage);
bulletinBoardMessage.addSig(messageSignature);
bulletinBoardMessage.addSig(signature);
return bulletinBoardMessage.build();
} catch (IOException e) {
} catch (SignatureException e) {
e.printStackTrace();
return null;
}
}
@ -72,7 +60,7 @@ public class SimpleRegistry implements RegistryInstance{
* @param tags the tags based on which the messages will be filtered
* @return MessageFilterList.
*/
public MessageFilterList GetRelevantMessages(List<String> tags) {
public MessageFilterList GetRelevantMessagesFilters(List<String> tags) {
MessageFilterList.Builder filters = MessageFilterList.newBuilder();
if (tags == null){
@ -89,105 +77,61 @@ public class SimpleRegistry implements RegistryInstance{
return filters.build();
}
/**
* Gets Relevant bulletinBoard messages from communicator than converts them to VoterRegistryMessages
*
* @param tags list of tags that will be used as filters
* @return List<VoterRegistryMessage>
*/
private List<VoterRegistryMessage> GetRelevantVoterRegistryMessages(List<String> tags) throws InvalidProtocolBufferException {
List<BulletinBoardMessage> relevantMessages = communicator.readMessages(GetRelevantMessages(tags));
List<UnsignedBulletinBoardMessage> messages = GetUnsignedBulletinBoardMessages(relevantMessages);
return ConvertToVoterRegistryMessages(messages);
}
/**
* Tries to post basicMessage and return true if its been successfully posted
*
* @param Message the massage to post
* @return true when the post was successful else false
*/
private boolean SafePost(BulletinBoardMessage Message) {
try {
communicator.postMessage(Message);
return true;
} catch (CommunicationException e) {
return false;
}
private void SafePost(BulletinBoardMessage Message, BulletinBoardClient.ClientCallback callback) {
bulletinBoardClient .postMessage(Message, callback);
}
public void AddVoter(String voterID, String personalData, RegistryCallBack callback) {
public void AddVoter(RegistryMessages.VoterInfo voterInfo, RegistryCallBack callback) {
UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder().
addTag(RegistryTags.ID_TAG + voterID)
addTag(RegistryTags.ID_TAG + voterInfo.getId().getId())
.addTag(RegistryTags.VOTER_ENTRY_TAG.toString())
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString());
basicMessage.setData(ByteString.copyFrom(personalData.getBytes()));
basicMessage.setData(voterInfo.getInfoBytes());
callback.HandleVoterAdded(SafePost(CreateBulletinBoardMessage(basicMessage.build())));
SafePost(CreateBulletinBoardMessage(basicMessage.build()), new BooleanHandler(callback));
}
public void AddToGroup(String voterID, String groupID, RegistryCallBack callback) {
public void AddToGroup(RegistryMessages.VoterGroup voterGroup, RegistryCallBack callback) {
UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + voterID)
.addTag(RegistryTags.GROUP_ID_TAG + groupID)
.addTag(RegistryTags.ID_TAG + voterGroup.getVoterId().getId())
.addTag(RegistryTags.GROUP_ID_TAG + voterGroup.getGroupId().getId())
.addTag(RegistryTags.GROUP_ACTION_TAG .toString() + RegistryTags.ADD_TO_GROUP_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString());
callback.HandleVoterAddedToGroup(SafePost(CreateBulletinBoardMessage(basicMessage.build())));
SafePost(CreateBulletinBoardMessage(basicMessage.build()), new BooleanHandler(callback));
}
public void RemoveFromGroup(String voterID, String groupID, RegistryCallBack callback) {
public void SetVoted(RegistryMessages.VoterID voterId, RegistryCallBack callback) {
UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + voterID)
.addTag(RegistryTags.GROUP_ID_TAG + groupID)
.addTag(RegistryTags.GROUP_ACTION_TAG.toString()+ RegistryTags.REMOVE_FROM_GROUP_TAG)
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString());
callback.HandleVoterRemovedFromGroup(SafePost(CreateBulletinBoardMessage(basicMessage.build())));
}
public void SetVoted(String id, RegistryCallBack callback) {
UnsignedBulletinBoardMessage.Builder basicMessage =
UnsignedBulletinBoardMessage.newBuilder()
.addTag(RegistryTags.ID_TAG + id)
.addTag(RegistryTags.ID_TAG + voterId.getId())
.addTag(RegistryTags.VOTE_ACTION_TAG.toString())
.addTag(RegistryTags.ACTION_TIMESTAMP_TAG + AccurateTimestamp.GetCurrentTimestampString());
callback.HandleVoterAddedToGroup(SafePost(CreateBulletinBoardMessage(basicMessage.build())));
SafePost(CreateBulletinBoardMessage(basicMessage.build()), new BooleanHandler(callback));
}
public void GetGroups(String id, RegistryCallBack callback) {
try {
List<String> GroupsActionsTags = new ArrayList<String>() {{
add(RegistryTags.GROUP_ID_TAG + id);
}};
List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
Map<String, VoterRegistryMessage> groupIdToMessage = GetLatestGroupsActions(voterRegistryMessages);
callback.HandleVoterGroups(GetListOfGroupIds(groupIdToMessage));
} catch (ParseException | InvalidProtocolBufferException e) {
callback.HandleVoterGroups(null);
}
public void GetGroups(RegistryMessages.GroupID groupID, RegistryCallBack callback) {
List<String> GroupsActionsTags = new ArrayList<String>() {{
add(RegistryTags.GROUP_ID_TAG + groupID.getId());
}};
bulletinBoardClient.readMessages(GetRelevantMessagesFilters(GroupsActionsTags), new RelevantDataCallBack(callback));
}
public void GetPersonIDDetails(String id, RegistryCallBack callback) {
try {
List<String> GroupsActionsTags = new ArrayList<String>() {{
add(RegistryTags.ID_TAG + id);
add(RegistryTags.VOTER_ENTRY_TAG.toString());
}};
List<VoterRegistryMessage> voterRegistryMessages = GetRelevantVoterRegistryMessages(GroupsActionsTags);
VoterRegistryMessage LatestMessage = GetLatestMessage(voterRegistryMessages);
callback.HandleVoterInfo(LatestMessage.tagsToStringList());
} catch (InvalidProtocolBufferException | ParseException e ){
callback.HandleVoterInfo(null);
}
public void GetPersonIDDetails(RegistryMessages.VoterID voterID, RegistryCallBack callback) {
List<String> GroupsActionsTags = new ArrayList<String>() {{
add(RegistryTags.ID_TAG + voterID.getId());
add(RegistryTags.VOTER_ENTRY_TAG.toString());
}};
bulletinBoardClient.readMessages(GetRelevantMessagesFilters(GroupsActionsTags), new PersonalDataCallBack(callback));
}
}

View File

@ -0,0 +1,66 @@
package meerkat;
/**
* Created by Vladimir Eliezer Tokarev on 1/22/2016.
* provides voters management options
*/
public interface VoterRegistry {
/**
* Created by Vladimir Eliezer Tokarev on 1/22/2016.
* This interface will handle the end of methods of RegistryInstance
*/
interface RegistryCallBack<T> {
void HandleResult(T result);
}
/**
* Adds new voter to the bulletin-board
* Passes true to callBack.HandleResult if the actions succeeded else false
*
* @param voterInfo protobuff object that represents voter information
* @param callBack when the adding voter done callBack.HandleResult will be called
* @return void
*/
void AddVoter(RegistryMessages.VoterInfo voterInfo, RegistryCallBack callBack);
/**
* Adding given voter to given group
* Passes the group to callBack.HandleResult if the actions succeeded else null
*
* @param voterGroup protobuff object that is coupling of voterId to groupId
* @param callBack when the adding voter done callBack.HandleResult will be called
* @return true if the adding action succeeded else return false
*/
void AddToGroup(RegistryMessages.VoterGroup voterGroup, RegistryCallBack callBack);
/**
* Sets that the voter have voted
* Passes true to callBack.HandleResult if the actions succeeded else false
*
* @param voterId id tag string
* @param callBack when the adding voter done callBack.HandleResult will be called
* @return true if the set voted succeed else false
*/
void SetVoted(RegistryMessages.VoterID voterId, RegistryCallBack callBack);
/**
* Requests all the groups that the given id voter is in
* Passes wanted groups to callback.HandleResult if the actions succeeded else null
*
* @param groupID id tag string
* @param callBack when the adding voter done callBack.HandleResult will be called
*/
void GetGroups(RegistryMessages.GroupID groupID, RegistryCallBack callBack);
/**
* Retrieves list of strings that represents voter
* Passes wanted data to callback.HandleResult if the actions succeeded else null
*
* @param voterID id tag string
* @param callBack when the adding voter done callBack.HandleResult will be called
* @return list of strings (empty list if the lookup failed)
*/
void GetPersonIDDetails(RegistryMessages.VoterID voterID, RegistryCallBack callBack);
}

View File

@ -16,9 +16,10 @@ public class VoterRegistryMessage {
public BulletinBoardAPI.UnsignedBulletinBoardMessage base;
public VoterRegistryMessage(BulletinBoardAPI.UnsignedBulletinBoardMessage message) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder unsignedBase = BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().addAllTag(message.getTagList());
unsignedBase.setData(message.getData());
public VoterRegistryMessage(BulletinBoardAPI.BulletinBoardMessage message) {
BulletinBoardAPI.UnsignedBulletinBoardMessage.Builder unsignedBase =
BulletinBoardAPI.UnsignedBulletinBoardMessage.newBuilder().addAllTag(message.getMsg().getTagList());
unsignedBase.setData(message.getMsg().getData());
base = unsignedBase.build();
}

View File

@ -23,7 +23,7 @@ public abstract class CollectionMessagesUtils {
* @return List<VoterRegistryMessage>
*/
public static List<VoterRegistryMessage> ConvertToVoterRegistryMessages(
List<BulletinBoardAPI.UnsignedBulletinBoardMessage> messages){
List<BulletinBoardAPI.BulletinBoardMessage> messages){
return messages.stream().map(VoterRegistryMessage::new).collect(Collectors.toList());
}

View File

@ -1,60 +0,0 @@
package util;
import meerkat.RegistryCallBack;
import java.util.List;
/**
* Handles the return values from called methods or registry
*/
public class SimpleRegistryCallBackHandler implements RegistryCallBack {
/**
* Presents the state of the called registy method
*/
public boolean ActionSucceed;
/**
* Presents the wanted information from registry
*/
public List<String> WantedInformation;
public int counter = 0;
@Override
public void HandleVoterAdded(boolean succeeded) {
ActionSucceed = succeeded;
counter++;
}
@Override
public void HandleVoterAddedToGroup(boolean succeeded) {
ActionSucceed = succeeded;
counter++;
}
@Override
public void HandleVoterRemovedFromGroup(boolean succeeded) {
ActionSucceed = succeeded;
counter++;
}
@Override
public void HandleSetVoted(boolean succeeded) {
ActionSucceed = succeeded;
counter++;
}
@Override
public void HandleVoterInfo(List<String> voterInformation) {
ActionSucceed = (voterInformation != null);
WantedInformation = voterInformation;
counter++;
}
@Override
public void HandleVoterGroups(List<String> groupsVoterIn) {
ActionSucceed = (groupsVoterIn != null);
WantedInformation = groupsVoterIn;
counter++;
}
}

View File

@ -0,0 +1,18 @@
package util;
import meerkat.VoterRegistry;
/**
* Handles the return value of the AddVoter method of
*/
public class VoterAdded implements VoterRegistry.RegistryCallBack<Boolean> {
public boolean Data;
@Override
public void HandleResult(Boolean result) {
Data = result;
}
}

View File

@ -1,6 +1,5 @@
import com.google.protobuf.InvalidProtocolBufferException;
import junit.framework.TestCase;
import meerkat.RegistryCallBack;
import meerkat.SimpleRegistry;
import meerkat.VoterRegistryMessage;
import meerkat.bulletinboard.SimpleBulletinBoardClient;
@ -12,7 +11,6 @@ import meerkat.protobuf.Voting;
import org.factcenter.qilin.primitives.concrete.ECElGamal;
import org.factcenter.qilin.primitives.concrete.ECGroup;
import util.RegistryTags;
import util.SimpleRegistryCallBackHandler;
import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;
@ -66,8 +64,8 @@ public class SimpleRegistryTest extends TestCase {
}
private SimpleRegistryCallBackHandler RegistryAnswersHandlerSetup() {
return new SimpleRegistryCallBackHandler();
private SimpleRegistryCallBackHandlers RegistryAnswersHandlerSetup() {
return new SimpleRegistryCallBackHandlers();
}
/**
@ -123,7 +121,7 @@ public class SimpleRegistryTest extends TestCase {
private int countActualAddedMessages(List<List<String>> votersData, List<String> tags, SimpleRegistry registry) throws InvalidProtocolBufferException {
System.out.println("- Check that the server have the new voters data:");
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessages(tags);
BulletinBoardAPI.MessageFilterList filters = registry.GetRelevantMessagesFilters(tags);
List<VoterRegistryMessage> messages =
ConvertToVoterRegistryMessages(GetUnsignedBulletinBoardMessages(communicator.readMessages(filters)));
@ -155,7 +153,7 @@ public class SimpleRegistryTest extends TestCase {
* @param voterData List of string which is the params to pass to the wantedMethod
*/
private void CallWantedMethod(String methodToCheck, int numberOfParamsWithoutHandler,
SimpleRegistryCallBackHandler handler, SimpleRegistry registry,
SimpleRegistryCallBackHandlers handler, SimpleRegistry registry,
List<String> voterData) {
try {
if (numberOfParamsWithoutHandler == 1)
@ -186,7 +184,7 @@ public class SimpleRegistryTest extends TestCase {
* @throws InvalidProtocolBufferException
*/
private List<List<String>> checkMessagesPostedSuccessfully(String methodToCheck, int numberOfParamsToPass, int numberOfChecks, List<String> tags) throws InvalidProtocolBufferException {
SimpleRegistryCallBackHandler handler = RegistryAnswersHandlerSetup();
SimpleRegistryCallBackHandlers handler = RegistryAnswersHandlerSetup();
System.out.println("Starting testing the " + methodToCheck + " capability.");
setActionsData(numberOfParamsToPass, numberOfChecks);
@ -244,7 +242,7 @@ public class SimpleRegistryTest extends TestCase {
List<List<String>> votersData = checkMessagesPostedSuccessfully("AddToGroup", 2, 3, null);
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
SimpleRegistryCallBackHandler handler = RegistryAnswersHandlerSetup();
SimpleRegistryCallBackHandlers handler = RegistryAnswersHandlerSetup();
System.out.println("- Checks that every group id we added to every voter really exists:");
for (List<String> voterData : votersData){
@ -268,7 +266,7 @@ public class SimpleRegistryTest extends TestCase {
new ArrayList<String>(){{add(RegistryTags.VOTER_ENTRY_TAG.toString());}});
SimpleRegistry registry = new SimpleRegistry(signatory, communicator);
SimpleRegistryCallBackHandler handler = RegistryAnswersHandlerSetup();
SimpleRegistryCallBackHandlers handler = RegistryAnswersHandlerSetup();
System.out.println("- Check that every added voter can be retrieved by SimpleRegistry:");