Search in sources :

Example 1 with BsqStateService

use of bisq.core.dao.state.BsqStateService 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 2 with BsqStateService

use of bisq.core.dao.state.BsqStateService in project bisq-core by bisq-network.

the class JsonBlockChainExporter method maybeExport.

public void maybeExport() {
    if (dumpBlockchainData) {
        ListenableFuture<Void> future = executor.submit(() -> {
            final BsqState bsqStateClone = bsqStateService.getClone();
            Map<String, Tx> txMap = bsqStateService.getBlocksFromState(bsqStateClone).stream().filter(Objects::nonNull).flatMap(block -> block.getTxs().stream()).collect(Collectors.toMap(Tx::getId, tx -> tx));
            for (Tx tx : txMap.values()) {
                String txId = tx.getId();
                final Optional<TxType> optionalTxType = bsqStateService.getOptionalTxType(txId);
                optionalTxType.ifPresent(txType1 -> {
                    JsonTxType txType = txType1 != TxType.UNDEFINED_TX_TYPE ? JsonTxType.valueOf(txType1.name()) : null;
                    List<JsonTxOutput> outputs = new ArrayList<>();
                    tx.getTxOutputs().forEach(txOutput -> {
                        final Optional<SpentInfo> optionalSpentInfo = bsqStateService.getSpentInfo(txOutput);
                        final boolean isBsqOutput = bsqStateService.isBsqTxOutputType(txOutput);
                        final PubKeyScript pubKeyScript = txOutput.getPubKeyScript();
                        final JsonTxOutput outputForJson = new JsonTxOutput(txId, txOutput.getIndex(), isBsqOutput ? txOutput.getValue() : 0, !isBsqOutput ? txOutput.getValue() : 0, txOutput.getBlockHeight(), isBsqOutput, bsqStateService.getBurntFee(tx.getId()), txOutput.getAddress(), pubKeyScript != null ? new JsonScriptPubKey(pubKeyScript) : null, optionalSpentInfo.map(JsonSpentInfo::new).orElse(null), tx.getTime(), txType, txType != null ? txType.getDisplayString() : "", txOutput.getOpReturnData() != null ? Utils.HEX.encode(txOutput.getOpReturnData()) : null);
                        outputs.add(outputForJson);
                        txOutputFileManager.writeToDisc(Utilities.objectToJson(outputForJson), outputForJson.getId());
                    });
                    List<JsonTxInput> inputs = tx.getTxInputs().stream().map(txInput -> {
                        Optional<TxOutput> optionalTxOutput = bsqStateService.getConnectedTxOutput(txInput);
                        if (optionalTxOutput.isPresent()) {
                            final TxOutput connectedTxOutput = optionalTxOutput.get();
                            final boolean isBsqOutput = bsqStateService.isBsqTxOutputType(connectedTxOutput);
                            return new JsonTxInput(txInput.getConnectedTxOutputIndex(), txInput.getConnectedTxOutputTxId(), connectedTxOutput.getValue(), isBsqOutput, connectedTxOutput.getAddress(), tx.getTime());
                        } else {
                            return null;
                        }
                    }).filter(Objects::nonNull).collect(Collectors.toList());
                    final JsonTx jsonTx = new JsonTx(txId, tx.getBlockHeight(), tx.getBlockHash(), tx.getTime(), inputs, outputs, txType, txType != null ? txType.getDisplayString() : "", bsqStateService.getBurntFee(tx.getId()));
                    txFileManager.writeToDisc(Utilities.objectToJson(jsonTx), txId);
                });
            }
            jsonFileManager.writeToDisc(Utilities.objectToJson(bsqStateClone), "BsqStateService");
            return null;
        });
        Futures.addCallback(future, new FutureCallback<Void>() {

            public void onSuccess(Void ignore) {
                log.trace("onSuccess");
            }

            public void onFailure(@NotNull Throwable throwable) {
                log.error(throwable.toString());
                throwable.printStackTrace();
            }
        });
    }
}
Also used : ListenableFuture(com.google.common.util.concurrent.ListenableFuture) Utilities(bisq.common.util.Utilities) TxOutput(bisq.core.dao.state.blockchain.TxOutput) Inject(com.google.inject.Inject) DaoOptionKeys(bisq.core.dao.DaoOptionKeys) TxType(bisq.core.dao.state.blockchain.TxType) ArrayList(java.util.ArrayList) JsonFileManager(bisq.common.storage.JsonFileManager) Map(java.util.Map) Named(javax.inject.Named) SpentInfo(bisq.core.dao.state.blockchain.SpentInfo) BsqState(bisq.core.dao.state.BsqState) Utils(org.bitcoinj.core.Utils) Tx(bisq.core.dao.state.blockchain.Tx) BsqStateService(bisq.core.dao.state.BsqStateService) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) FutureCallback(com.google.common.util.concurrent.FutureCallback) File(java.io.File) PubKeyScript(bisq.core.dao.state.blockchain.PubKeyScript) Objects(java.util.Objects) FileUtil(bisq.common.storage.FileUtil) Futures(com.google.common.util.concurrent.Futures) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Paths(java.nio.file.Paths) Storage(bisq.common.storage.Storage) Optional(java.util.Optional) NotNull(org.jetbrains.annotations.NotNull) ListeningExecutorService(com.google.common.util.concurrent.ListeningExecutorService) TxOutput(bisq.core.dao.state.blockchain.TxOutput) BsqState(bisq.core.dao.state.BsqState) ArrayList(java.util.ArrayList) PubKeyScript(bisq.core.dao.state.blockchain.PubKeyScript) SpentInfo(bisq.core.dao.state.blockchain.SpentInfo) Tx(bisq.core.dao.state.blockchain.Tx) TxType(bisq.core.dao.state.blockchain.TxType) Objects(java.util.Objects)

Aggregations

Utilities (bisq.common.util.Utilities)2 BsqStateService (bisq.core.dao.state.BsqStateService)2 Tx (bisq.core.dao.state.blockchain.Tx)2 TxOutput (bisq.core.dao.state.blockchain.TxOutput)2 ArrayList (java.util.ArrayList)2 FileUtil (bisq.common.storage.FileUtil)1 JsonFileManager (bisq.common.storage.JsonFileManager)1 Storage (bisq.common.storage.Storage)1 DaoOptionKeys (bisq.core.dao.DaoOptionKeys)1 DaoSetupService (bisq.core.dao.DaoSetupService)1 Ballot (bisq.core.dao.governance.ballot.Ballot)1 BallotList (bisq.core.dao.governance.ballot.BallotList)1 BallotListService (bisq.core.dao.governance.ballot.BallotListService)1 Vote (bisq.core.dao.governance.ballot.vote.Vote)1 BlindVote (bisq.core.dao.governance.blindvote.BlindVote)1 BlindVoteConsensus (bisq.core.dao.governance.blindvote.BlindVoteConsensus)1 BlindVoteListService (bisq.core.dao.governance.blindvote.BlindVoteListService)1 VoteWithProposalTxId (bisq.core.dao.governance.blindvote.VoteWithProposalTxId)1 VoteWithProposalTxIdList (bisq.core.dao.governance.blindvote.VoteWithProposalTxIdList)1 MeritConsensus (bisq.core.dao.governance.merit.MeritConsensus)1