Search in sources :

Example 1 with Proposal

use of bisq.core.dao.governance.proposal.Proposal in project bisq-core by bisq-network.

the class VoteResultService method createBallotList.

private BallotList createBallotList(VoteWithProposalTxIdList voteWithProposalTxIdList) throws MissingBallotException {
    // We convert the list to a map with proposalTxId as key and the vote as value
    Map<String, Vote> voteByTxIdMap = voteWithProposalTxIdList.stream().filter(voteWithProposalTxId -> voteWithProposalTxId.getVote() != null).collect(Collectors.toMap(VoteWithProposalTxId::getProposalTxId, VoteWithProposalTxId::getVote));
    // We make a map with proposalTxId as key and the ballot as value out of our stored ballot list
    Map<String, Ballot> ballotByTxIdMap = ballotListService.getBallotList().stream().collect(Collectors.toMap(Ballot::getTxId, ballot -> ballot));
    List<String> missingBallots = new ArrayList<>();
    List<Ballot> ballots = voteByTxIdMap.entrySet().stream().map(entry -> {
        String txId = entry.getKey();
        if (ballotByTxIdMap.containsKey(txId)) {
            // why not use proposalList?
            Ballot ballot = ballotByTxIdMap.get(txId);
            // We create a new Ballot with the proposal from the ballot list and the vote from our decrypted votes
            Vote vote = entry.getValue();
            // received from the network?
            return new Ballot(ballot.getProposal(), vote);
        } else {
            // 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.
            missingBallots.add(txId);
            return null;
        }
    }).filter(Objects::nonNull).collect(Collectors.toList());
    if (!missingBallots.isEmpty())
        throw new MissingBallotException(ballots, missingBallots);
    // Let's keep the data more deterministic by sorting it by txId. Though we are not using the sorting.
    ballots.sort(Comparator.comparing(Ballot::getTxId));
    return new BallotList(ballots);
}
Also used : VoteWithProposalTxId(bisq.core.dao.governance.blindvote.VoteWithProposalTxId) Arrays(java.util.Arrays) Utilities(bisq.common.util.Utilities) Vote(bisq.core.dao.governance.ballot.vote.Vote) P2PDataStorage(bisq.network.p2p.storage.P2PDataStorage) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BlindVoteListService(bisq.core.dao.governance.blindvote.BlindVoteListService) Map(java.util.Map) VoteRevealConsensus(bisq.core.dao.governance.votereveal.VoteRevealConsensus) ChangeParamProposal(bisq.core.dao.governance.proposal.param.ChangeParamProposal) BlindVote(bisq.core.dao.governance.blindvote.BlindVote) CompensationProposal(bisq.core.dao.governance.proposal.compensation.CompensationProposal) VoteRevealService(bisq.core.dao.governance.votereveal.VoteRevealService) BondedRolesService(bisq.core.dao.governance.role.BondedRolesService) BallotListService(bisq.core.dao.governance.ballot.BallotListService) Proposal(bisq.core.dao.governance.proposal.Proposal) PeriodService(bisq.core.dao.state.period.PeriodService) Set(java.util.Set) ConfiscateBond(bisq.core.dao.state.governance.ConfiscateBond) Collectors(java.util.stream.Collectors) ProposalListPresentation(bisq.core.dao.governance.proposal.ProposalListPresentation) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) IssuanceService(bisq.core.dao.governance.voteresult.issuance.IssuanceService) Optional(java.util.Optional) BallotList(bisq.core.dao.governance.ballot.BallotList) ObservableList(javafx.collections.ObservableList) SecretKey(javax.crypto.SecretKey) Getter(lombok.Getter) BondedRoleProposal(bisq.core.dao.governance.proposal.role.BondedRoleProposal) TxOutput(bisq.core.dao.state.blockchain.TxOutput) MeritConsensus(bisq.core.dao.governance.merit.MeritConsensus) FXCollections(javafx.collections.FXCollections) HashMap(java.util.HashMap) Block(bisq.core.dao.state.blockchain.Block) ArrayList(java.util.ArrayList) Value(lombok.Value) Inject(javax.inject.Inject) HashSet(java.util.HashSet) ParamChange(bisq.core.dao.state.governance.ParamChange) BondedRole(bisq.core.dao.governance.role.BondedRole) Nullable(javax.annotation.Nullable) MeritList(bisq.core.dao.governance.merit.MeritList) ConfiscateBondProposal(bisq.core.dao.governance.proposal.confiscatebond.ConfiscateBondProposal) Ballot(bisq.core.dao.governance.ballot.Ballot) Tx(bisq.core.dao.state.blockchain.Tx) BsqStateService(bisq.core.dao.state.BsqStateService) DaoSetupService(bisq.core.dao.DaoSetupService) BlindVoteConsensus(bisq.core.dao.governance.blindvote.BlindVoteConsensus) VoteWithProposalTxIdList(bisq.core.dao.governance.blindvote.VoteWithProposalTxIdList) BsqStateListener(bisq.core.dao.state.BsqStateListener) Comparator(java.util.Comparator) DaoPhase(bisq.core.dao.state.period.DaoPhase) Vote(bisq.core.dao.governance.ballot.vote.Vote) BlindVote(bisq.core.dao.governance.blindvote.BlindVote) ArrayList(java.util.ArrayList) Ballot(bisq.core.dao.governance.ballot.Ballot) BallotList(bisq.core.dao.governance.ballot.BallotList)

Example 2 with Proposal

use of bisq.core.dao.governance.proposal.Proposal in project bisq-core by bisq-network.

the class VoteResultService method getDecryptedBallotsWithMeritsSet.

private Set<DecryptedBallotsWithMerits> getDecryptedBallotsWithMeritsSet(int chainHeight) {
    // We want all voteRevealTxOutputs which are in current cycle we are processing.
    return bsqStateService.getVoteRevealOpReturnTxOutputs().stream().filter(txOutput -> periodService.isTxInCorrectCycle(txOutput.getTxId(), chainHeight)).map(txOutput -> {
        // TODO make method
        byte[] opReturnData = txOutput.getOpReturnData();
        String voteRevealTxId = txOutput.getTxId();
        Optional<Tx> optionalVoteRevealTx = bsqStateService.getTx(voteRevealTxId);
        if (!optionalVoteRevealTx.isPresent()) {
            log.error("optionalVoteRevealTx is not present. voteRevealTxId={}", voteRevealTxId);
            // TODO throw exception
            return null;
        }
        Tx voteRevealTx = optionalVoteRevealTx.get();
        try {
            // TODO maybe verify version in opReturn
            byte[] hashOfBlindVoteList = VoteResultConsensus.getHashOfBlindVoteList(opReturnData);
            SecretKey secretKey = VoteResultConsensus.getSecretKey(opReturnData);
            TxOutput blindVoteStakeOutput = VoteResultConsensus.getConnectedBlindVoteStakeOutput(voteRevealTx, bsqStateService);
            long blindVoteStake = blindVoteStakeOutput.getValue();
            Tx blindVoteTx = VoteResultConsensus.getBlindVoteTx(blindVoteStakeOutput, bsqStateService, periodService, chainHeight);
            String blindVoteTxId = blindVoteTx.getId();
            // Here we deal with eventual consistency of the p2p network data!
            // TODO make more clear we are in p2p domain now
            List<BlindVote> blindVoteList = BlindVoteConsensus.getSortedBlindVoteListOfCycle(blindVoteListService);
            Optional<BlindVote> optionalBlindVote = blindVoteList.stream().filter(blindVote -> blindVote.getTxId().equals(blindVoteTxId)).findAny();
            if (optionalBlindVote.isPresent()) {
                BlindVote blindVote = optionalBlindVote.get();
                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);
                return new DecryptedBallotsWithMerits(hashOfBlindVoteList, voteRevealTxId, blindVoteTxId, blindVoteStake, ballotList, meritList);
            } else {
                // TODO handle recovering
                log.warn("We have a blindVoteTx but we do not have the corresponding blindVote in our local list.\n" + "That can happen if the blindVote item was not properly broadcast. We will go on " + "and see if that blindVote was part of the majority data view. If so we should " + "recover the missing blind vote by a request to our peers. blindVoteTxId={}", blindVoteTxId);
                return null;
            }
        } catch (MissingBallotException e) {
            // TODO handle case that we are missing proposals
            log.error("We are missing proposals to create the vote result: " + e.toString());
            return null;
        } catch (Throwable e) {
            log.error("Could not create DecryptedBallotsWithMerits: " + e.toString());
            return null;
        }
    }).filter(Objects::nonNull).collect(Collectors.toSet());
}
Also used : VoteWithProposalTxId(bisq.core.dao.governance.blindvote.VoteWithProposalTxId) Arrays(java.util.Arrays) Utilities(bisq.common.util.Utilities) Vote(bisq.core.dao.governance.ballot.vote.Vote) P2PDataStorage(bisq.network.p2p.storage.P2PDataStorage) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) BlindVoteListService(bisq.core.dao.governance.blindvote.BlindVoteListService) Map(java.util.Map) VoteRevealConsensus(bisq.core.dao.governance.votereveal.VoteRevealConsensus) ChangeParamProposal(bisq.core.dao.governance.proposal.param.ChangeParamProposal) BlindVote(bisq.core.dao.governance.blindvote.BlindVote) CompensationProposal(bisq.core.dao.governance.proposal.compensation.CompensationProposal) VoteRevealService(bisq.core.dao.governance.votereveal.VoteRevealService) BondedRolesService(bisq.core.dao.governance.role.BondedRolesService) BallotListService(bisq.core.dao.governance.ballot.BallotListService) Proposal(bisq.core.dao.governance.proposal.Proposal) PeriodService(bisq.core.dao.state.period.PeriodService) Set(java.util.Set) ConfiscateBond(bisq.core.dao.state.governance.ConfiscateBond) Collectors(java.util.stream.Collectors) ProposalListPresentation(bisq.core.dao.governance.proposal.ProposalListPresentation) Objects(java.util.Objects) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) IssuanceService(bisq.core.dao.governance.voteresult.issuance.IssuanceService) Optional(java.util.Optional) BallotList(bisq.core.dao.governance.ballot.BallotList) ObservableList(javafx.collections.ObservableList) SecretKey(javax.crypto.SecretKey) Getter(lombok.Getter) BondedRoleProposal(bisq.core.dao.governance.proposal.role.BondedRoleProposal) TxOutput(bisq.core.dao.state.blockchain.TxOutput) MeritConsensus(bisq.core.dao.governance.merit.MeritConsensus) FXCollections(javafx.collections.FXCollections) HashMap(java.util.HashMap) Block(bisq.core.dao.state.blockchain.Block) ArrayList(java.util.ArrayList) Value(lombok.Value) Inject(javax.inject.Inject) HashSet(java.util.HashSet) ParamChange(bisq.core.dao.state.governance.ParamChange) BondedRole(bisq.core.dao.governance.role.BondedRole) Nullable(javax.annotation.Nullable) MeritList(bisq.core.dao.governance.merit.MeritList) ConfiscateBondProposal(bisq.core.dao.governance.proposal.confiscatebond.ConfiscateBondProposal) Ballot(bisq.core.dao.governance.ballot.Ballot) Tx(bisq.core.dao.state.blockchain.Tx) BsqStateService(bisq.core.dao.state.BsqStateService) DaoSetupService(bisq.core.dao.DaoSetupService) BlindVoteConsensus(bisq.core.dao.governance.blindvote.BlindVoteConsensus) VoteWithProposalTxIdList(bisq.core.dao.governance.blindvote.VoteWithProposalTxIdList) BsqStateListener(bisq.core.dao.state.BsqStateListener) Comparator(java.util.Comparator) DaoPhase(bisq.core.dao.state.period.DaoPhase) TxOutput(bisq.core.dao.state.blockchain.TxOutput) Tx(bisq.core.dao.state.blockchain.Tx) VoteWithProposalTxIdList(bisq.core.dao.governance.blindvote.VoteWithProposalTxIdList) MeritList(bisq.core.dao.governance.merit.MeritList) BlindVote(bisq.core.dao.governance.blindvote.BlindVote) BallotList(bisq.core.dao.governance.ballot.BallotList) SecretKey(javax.crypto.SecretKey)

Example 3 with Proposal

use of bisq.core.dao.governance.proposal.Proposal in project bisq-core by bisq-network.

the class VoteResultService method getVoteWithStakeListByProposalMap.

private Map<Proposal, List<VoteWithStake>> getVoteWithStakeListByProposalMap(Set<DecryptedBallotsWithMerits> decryptedBallotsWithMeritsSet) {
    Map<Proposal, List<VoteWithStake>> voteWithStakeByProposalMap = new HashMap<>();
    decryptedBallotsWithMeritsSet.forEach(decryptedBallotsWithMerits -> {
        decryptedBallotsWithMerits.getBallotList().forEach(ballot -> {
            Proposal proposal = ballot.getProposal();
            voteWithStakeByProposalMap.putIfAbsent(proposal, new ArrayList<>());
            List<VoteWithStake> voteWithStakeList = voteWithStakeByProposalMap.get(proposal);
            long sumOfAllMerits = MeritConsensus.getMeritStake(decryptedBallotsWithMerits.getBlindVoteTxId(), decryptedBallotsWithMerits.getMeritList(), bsqStateService);
            VoteWithStake voteWithStake = new VoteWithStake(ballot.getVote(), decryptedBallotsWithMerits.getStake(), sumOfAllMerits);
            voteWithStakeList.add(voteWithStake);
        });
    });
    return voteWithStakeByProposalMap;
}
Also used : HashMap(java.util.HashMap) List(java.util.List) BallotList(bisq.core.dao.governance.ballot.BallotList) ObservableList(javafx.collections.ObservableList) ArrayList(java.util.ArrayList) MeritList(bisq.core.dao.governance.merit.MeritList) VoteWithProposalTxIdList(bisq.core.dao.governance.blindvote.VoteWithProposalTxIdList) ChangeParamProposal(bisq.core.dao.governance.proposal.param.ChangeParamProposal) CompensationProposal(bisq.core.dao.governance.proposal.compensation.CompensationProposal) Proposal(bisq.core.dao.governance.proposal.Proposal) BondedRoleProposal(bisq.core.dao.governance.proposal.role.BondedRoleProposal) ConfiscateBondProposal(bisq.core.dao.governance.proposal.confiscatebond.ConfiscateBondProposal)

Aggregations

BallotList (bisq.core.dao.governance.ballot.BallotList)3 VoteWithProposalTxIdList (bisq.core.dao.governance.blindvote.VoteWithProposalTxIdList)3 MeritList (bisq.core.dao.governance.merit.MeritList)3 Proposal (bisq.core.dao.governance.proposal.Proposal)3 CompensationProposal (bisq.core.dao.governance.proposal.compensation.CompensationProposal)3 ConfiscateBondProposal (bisq.core.dao.governance.proposal.confiscatebond.ConfiscateBondProposal)3 ChangeParamProposal (bisq.core.dao.governance.proposal.param.ChangeParamProposal)3 BondedRoleProposal (bisq.core.dao.governance.proposal.role.BondedRoleProposal)3 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 Utilities (bisq.common.util.Utilities)2 DaoSetupService (bisq.core.dao.DaoSetupService)2 Ballot (bisq.core.dao.governance.ballot.Ballot)2 BallotListService (bisq.core.dao.governance.ballot.BallotListService)2 Vote (bisq.core.dao.governance.ballot.vote.Vote)2 BlindVote (bisq.core.dao.governance.blindvote.BlindVote)2 BlindVoteConsensus (bisq.core.dao.governance.blindvote.BlindVoteConsensus)2 BlindVoteListService (bisq.core.dao.governance.blindvote.BlindVoteListService)2 VoteWithProposalTxId (bisq.core.dao.governance.blindvote.VoteWithProposalTxId)2 MeritConsensus (bisq.core.dao.governance.merit.MeritConsensus)2