Skip to content

Commit

Permalink
Merge pull request #2195 from ManfredKarrer/dao-fix-vote-reveal-tx-pu…
Browse files Browse the repository at this point in the history
…blishing

Dao: fix vote reveal tx publishing
  • Loading branch information
ManfredKarrer authored Jan 5, 2019
2 parents f12ff5c + 4d8a348 commit a3287ab
Show file tree
Hide file tree
Showing 10 changed files with 147 additions and 101 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ private void sendRepublishRequest(NodeAddress nodeAddress) {
TIMEOUT);
}

log.warn("We send to peer {} a republishGovernanceDataRequest.", nodeAddress);
log.info("We send to peer {} a republishGovernanceDataRequest.", nodeAddress);
SettableFuture<Connection> future = networkNode.sendMessage(nodeAddress, republishGovernanceDataRequest);
Futures.addCallback(future, new FutureCallback<>() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,33 @@ public static TxOutput getConnectedBlindVoteStakeOutput(Tx voteRevealTx, DaoStat
}
}

public static Tx getBlindVoteTx(TxOutput blindVoteStakeOutput, DaoStateService daoStateService,
PeriodService periodService, int chainHeight)
public static void validateBlindVoteTx(String blindVoteTxId, DaoStateService daoStateService,
PeriodService periodService, int chainHeight)
throws VoteResultException.ValidationException {
try {
String blindVoteTxId = blindVoteStakeOutput.getTxId();
Optional<Tx> optionalBlindVoteTx = daoStateService.getTx(blindVoteTxId);

checkArgument(optionalBlindVoteTx.isPresent(), "blindVoteTx with txId " +
blindVoteTxId + " not found.");

Tx blindVoteTx = optionalBlindVoteTx.get();
Optional<TxType> optionalTxType = daoStateService.getOptionalTxType(blindVoteTx.getId());

checkArgument(optionalTxType.isPresent(), "optionalTxType must be present" +
". blindVoteTxId=" + blindVoteTx.getId());

checkArgument(optionalTxType.get() == TxType.BLIND_VOTE,
"blindVoteTx must have type BLIND_VOTE but is " + optionalTxType.get() +
". blindVoteTxId=" + blindVoteTx.getId());

checkArgument(periodService.isTxInCorrectCycle(blindVoteTx.getBlockHeight(), chainHeight),
"blindVoteTx is not in correct cycle. blindVoteTx.getBlockHeight()="
+ blindVoteTx.getBlockHeight() + ". chainHeight=" + chainHeight +
". blindVoteTxId=" + blindVoteTx.getId());

checkArgument(periodService.isInPhase(blindVoteTx.getBlockHeight(), DaoPhase.Phase.BLIND_VOTE),
"blindVoteTx is not in BLIND_VOTE phase. blindVoteTx.getBlockHeight()="
+ blindVoteTx.getBlockHeight() + ". blindVoteTxId=" + blindVoteTx.getId());
return blindVoteTx;
} catch (Throwable t) {
throw new VoteResultException.ValidationException(t);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public void onParseBlockChainComplete() {

private void maybeCalculateVoteResult(int chainHeight) {
if (isInVoteResultPhase(chainHeight)) {
long startTs = System.currentTimeMillis();
Set<DecryptedBallotsWithMerits> decryptedBallotsWithMeritsSet = getDecryptedBallotsWithMeritsSet(chainHeight);
decryptedBallotsWithMeritsSet.stream()
.filter(e -> !daoStateService.getDecryptedBallotsWithMeritsList().contains(e))
Expand Down Expand Up @@ -232,6 +233,8 @@ private void maybeCalculateVoteResult(int chainHeight) {
daoStateService.getIssuanceCandidateTxOutputs().stream()
.filter(txOutput -> !daoStateService.isIssuanceTx(txOutput.getTxId()))
.forEach(daoStateService::addNonBsqTxOutput);

log.info("Evaluating vote result took {} ms", System.currentTimeMillis() - startTs);
}
}

Expand All @@ -251,14 +254,25 @@ private Set<DecryptedBallotsWithMerits> getDecryptedBallotsWithMeritsSet(int cha
}

Tx voteRevealTx = optionalVoteRevealTx.get();
// If we get a voteReveal tx which was published too late we ignore it.
if (!periodService.isTxInPhaseAndCycle(voteRevealTx.getId(), DaoPhase.Phase.VOTE_REVEAL, chainHeight)) {
log.warn("We got a vote reveal tx with was not in the correct phase and/or cycle. voteRevealTxId={}", voteRevealTx.getId());
return null;
}

try {
// TODO maybe verify version in opReturn
byte[] hashOfBlindVoteList = VoteResultConsensus.getHashOfBlindVoteList(opReturnData);
SecretKey secretKey = VoteResultConsensus.getSecretKey(opReturnData);

TxOutput blindVoteStakeOutput = VoteResultConsensus.getConnectedBlindVoteStakeOutput(voteRevealTx, daoStateService);
long blindVoteStake = blindVoteStakeOutput.getValue();
Tx blindVoteTx = VoteResultConsensus.getBlindVoteTx(blindVoteStakeOutput, daoStateService, periodService, chainHeight);
String blindVoteTxId = blindVoteTx.getId();
String blindVoteTxId = blindVoteStakeOutput.getTxId();
boolean isBlindVoteInCorrectPhaseAndCycle = periodService.isTxInPhaseAndCycle(blindVoteTxId, DaoPhase.Phase.BLIND_VOTE, chainHeight);
// If we get a voteReveal tx which was published too late we ignore it.
if (!isBlindVoteInCorrectPhaseAndCycle) {
log.warn("We got a blind vote tx with was not in the correct phase and/or cycle. blindVoteTxId={}", blindVoteTxId);
return null;
}

VoteResultConsensus.validateBlindVoteTx(blindVoteStakeOutput.getTxId(), daoStateService, periodService, chainHeight);

List<BlindVote> blindVoteList = BlindVoteConsensus.getSortedBlindVoteListOfCycle(blindVoteListService);
Optional<BlindVote> optionalBlindVote = blindVoteList.stream()
Expand All @@ -267,12 +281,15 @@ private Set<DecryptedBallotsWithMerits> getDecryptedBallotsWithMeritsSet(int cha
if (optionalBlindVote.isPresent()) {
BlindVote blindVote = optionalBlindVote.get();
try {
SecretKey secretKey = VoteResultConsensus.getSecretKey(opReturnData);
VoteWithProposalTxIdList voteWithProposalTxIdList = VoteResultConsensus.decryptVotes(blindVote.getEncryptedVotes(), secretKey);
MeritList meritList = MeritConsensus.decryptMeritList(blindVote.getEncryptedMeritList(), secretKey);
// We lookup for the proposals we have in our local list which match the txId from the
// voteWithProposalTxIdList and create a ballot list with the proposal and the vote from
// the voteWithProposalTxIdList
BallotList ballotList = createBallotList(voteWithProposalTxIdList);
byte[] hashOfBlindVoteList = VoteResultConsensus.getHashOfBlindVoteList(opReturnData);
long blindVoteStake = blindVoteStakeOutput.getValue();
return new DecryptedBallotsWithMerits(hashOfBlindVoteList, blindVoteTxId, voteRevealTxId, blindVoteStake, ballotList, meritList);
} catch (VoteResultException.MissingBallotException missingBallotException) {
log.warn("We are missing proposals to create the vote result: " + missingBallotException.toString());
Expand Down Expand Up @@ -338,6 +355,7 @@ private BallotList createBallotList(VoteWithProposalTxIdList voteWithProposalTxI
// We got a vote but we don't have the ballot (which includes the proposal)
// We add it to the missing list to handle it as exception later. We want all missing data so we
// do not throw here.
log.warn("missingBallot for proposal with txId={}. Optional tx={}", txId, daoStateService.getTx(txId));
missingBallots.add(txId);
return null;
}
Expand Down Expand Up @@ -467,13 +485,13 @@ private Set<EvaluatedProposal> getEvaluatedProposals(Set<DecryptedBallotsWithMer
} else {
evaluatedProposals.add(new EvaluatedProposal(false, proposalVoteResult,
requiredQuorum, requiredVoteThreshold));
log.warn("Proposal did not reach the requiredVoteThreshold. reachedThreshold={} %, " +
log.info("Proposal did not reach the requiredVoteThreshold. reachedThreshold={} %, " +
"requiredVoteThreshold={} %", reachedThreshold / 100D, requiredVoteThreshold / 100D);
}
} else {
evaluatedProposals.add(new EvaluatedProposal(false, proposalVoteResult,
requiredQuorum, requiredVoteThreshold));
log.warn("Proposal did not reach the requiredQuorum. reachedQuorum={}, requiredQuorum={}",
log.info("Proposal did not reach the requiredQuorum. reachedQuorum={}, requiredQuorum={}",
reachedQuorum, requiredQuorum);
}
});
Expand Down
Loading

0 comments on commit a3287ab

Please sign in to comment.