Skip to content

Commit

Permalink
Merge remote-tracking branch 'manfred/voting' into periodservice-rework
Browse files Browse the repository at this point in the history
  • Loading branch information
sqrrm committed Apr 8, 2018
2 parents c5b01a0 + 8022172 commit e515138
Show file tree
Hide file tree
Showing 11 changed files with 170 additions and 128 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
import bisq.desktop.components.AutoTooltipTableColumn;
import bisq.desktop.components.HyperlinkWithIcon;
import bisq.desktop.components.TableGroupHeadline;
import bisq.desktop.util.BSFormatter;
import bisq.desktop.util.BsqFormatter;

import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.dao.blockchain.BsqBlockChain;
import bisq.core.dao.blockchain.ReadableBsqBlockChain;
import bisq.core.dao.blockchain.vo.BsqBlock;
import bisq.core.dao.param.DaoParamService;
import bisq.core.dao.vote.PeriodService;
import bisq.core.dao.vote.proposal.Proposal;
import bisq.core.dao.vote.proposal.ProposalPayload;
Expand Down Expand Up @@ -73,8 +75,10 @@ public abstract class BaseProposalView extends ActivatableView<GridPane, Void> i

protected final ProposalService proposalService;
protected final ReadableBsqBlockChain readableBsqBlockChain;
protected final DaoParamService daoParamService;
protected final BsqWalletService bsqWalletService;
protected final BsqFormatter bsqFormatter;
protected final BSFormatter btcFormatter;

protected final ObservableList<ProposalListItem> proposalListItems = FXCollections.observableArrayList();
protected final SortedList<ProposalListItem> sortedList = new SortedList<>(proposalListItems);
Expand All @@ -101,13 +105,17 @@ public abstract class BaseProposalView extends ActivatableView<GridPane, Void> i
protected BaseProposalView(ProposalService proposalService,
BsqWalletService bsqWalletService,
ReadableBsqBlockChain readableBsqBlockChain,
DaoParamService daoParamService,
PeriodService periodService,
BsqFormatter bsqFormatter) {
BsqFormatter bsqFormatter,
BSFormatter btcFormatter) {
this.proposalService = proposalService;
this.bsqWalletService = bsqWalletService;
this.readableBsqBlockChain = readableBsqBlockChain;
this.daoParamService = daoParamService;
this.periodService = periodService;
this.bsqFormatter = bsqFormatter;
this.btcFormatter = btcFormatter;
}

@Override
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/bisq/desktop/main/dao/proposal/ProposalListItem.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import bisq.core.dao.blockchain.ReadableBsqBlockChain;
import bisq.core.dao.blockchain.vo.BsqBlock;
import bisq.core.dao.blockchain.vo.Tx;
import bisq.core.dao.vote.BooleanVote;
import bisq.core.dao.vote.PeriodService;
import bisq.core.dao.vote.Vote;
import bisq.core.dao.vote.proposal.Proposal;
import bisq.core.dao.vote.proposal.ProposalService;
import bisq.core.dao.vote.result.BooleanVoteResult;
import bisq.core.dao.vote.result.VoteResult;
import bisq.core.locale.Res;

import org.bitcoinj.core.Transaction;
Expand Down Expand Up @@ -63,7 +63,7 @@ public class ProposalListItem implements BsqBlockChain.Listener {
private final ReadableBsqBlockChain readableBsqBlockChain;
private final BsqFormatter bsqFormatter;
private final ChangeListener<Number> chainHeightListener;
private final ChangeListener<VoteResult> voteResultChangeListener;
private final ChangeListener<Vote> voteResultChangeListener;
@Getter
private TxConfidenceIndicator txConfidenceIndicator;
@Getter
Expand Down Expand Up @@ -111,7 +111,7 @@ public class ProposalListItem implements BsqBlockChain.Listener {
readableBsqBlockChain.addListener(this);

phaseChangeListener = (observable, oldValue, newValue) -> {
applyState(newValue, proposal.getVoteResult());
applyState(newValue, proposal.getVote());
};

voteResultChangeListener = (observable, oldValue, newValue) -> {
Expand All @@ -122,16 +122,17 @@ public class ProposalListItem implements BsqBlockChain.Listener {
proposal.getVoteResultProperty().addListener(voteResultChangeListener);
}

public void applyState(PeriodService.Phase newValue, VoteResult voteResult) {
public void applyState(PeriodService.Phase newValue, Vote vote) {
actionButton.setText("");
actionButton.setVisible(false);
actionButton.setOnAction(null);
final boolean isTxInPastCycle = periodService.isTxInPastCycle(proposal.getTxId());
final boolean isTxInPastCycle = periodService.isTxInPastCycle(proposal.getTxId(),
readableBsqBlockChain.getChainHeadHeight());
switch (newValue) {
case UNDEFINED:
break;
case PROPOSAL:
if (proposalService.isMine(proposal)) {
if (proposalService.isMine(proposal.getProposalPayload())) {
actionButton.setVisible(!isTxInPastCycle);
actionButtonIconView.setVisible(actionButton.isVisible());
actionButton.setText(Res.get("shared.remove"));
Expand All @@ -150,10 +151,10 @@ public void applyState(PeriodService.Phase newValue, VoteResult voteResult) {
if (!isTxInPastCycle) {
actionNode = actionButtonIconView;
actionButton.setVisible(false);
if (proposal.getVoteResult() != null) {
if (proposal.getVote() != null) {
actionButtonIconView.setVisible(true);
if (voteResult instanceof BooleanVoteResult) {
if (((BooleanVoteResult) voteResult).isAccepted()) {
if (vote instanceof BooleanVote) {
if (((BooleanVote) vote).isAccepted()) {
actionButtonIconView.setId("accepted");
} else {
actionButtonIconView.setId("rejected");
Expand All @@ -162,7 +163,6 @@ public void applyState(PeriodService.Phase newValue, VoteResult voteResult) {
//TODO
}
} else {
log.error("actionButtonIconView.setVisible(false);");
actionButtonIconView.setVisible(false);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,41 +18,40 @@
package bisq.desktop.main.dao.proposal.active;

import bisq.desktop.common.view.FxmlView;
import bisq.desktop.components.BusyAnimation;
import bisq.desktop.components.InputTextField;
import bisq.desktop.components.TitledGroupBg;
import bisq.desktop.main.dao.proposal.BaseProposalView;
import bisq.desktop.main.dao.proposal.ProposalListItem;
import bisq.desktop.main.overlays.popups.Popup;
import bisq.desktop.util.BSFormatter;
import bisq.desktop.util.BsqFormatter;
import bisq.desktop.util.GUIUtil;
import bisq.desktop.util.Layout;

import bisq.core.btc.exceptions.TransactionVerificationException;
import bisq.core.btc.exceptions.WalletException;
import bisq.core.btc.wallet.BsqBalanceListener;
import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.btc.wallet.InsufficientBsqException;
import bisq.core.dao.blockchain.ReadableBsqBlockChain;
import bisq.core.dao.param.DaoParamService;
import bisq.core.dao.vote.BooleanVote;
import bisq.core.dao.vote.PeriodService;
import bisq.core.dao.vote.blindvote.BlindVoteConsensus;
import bisq.core.dao.vote.blindvote.BlindVoteService;
import bisq.core.dao.vote.proposal.Proposal;
import bisq.core.dao.vote.proposal.ProposalService;
import bisq.core.dao.vote.result.BooleanVoteResult;
import bisq.core.locale.Res;

import bisq.common.crypto.CryptoException;
import bisq.common.util.Tuple2;
import bisq.common.util.Tuple3;

import com.google.protobuf.InvalidProtocolBufferException;

import org.bitcoinj.core.Coin;
import org.bitcoinj.core.InsufficientMoneyException;
import org.bitcoinj.core.Transaction;

import javax.inject.Inject;

import com.google.common.util.concurrent.FutureCallback;

import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
Expand All @@ -64,20 +63,11 @@

import javafx.util.Callback;

import java.io.IOException;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

import org.jetbrains.annotations.NotNull;

import javax.annotation.Nullable;

import static bisq.desktop.util.FormBuilder.add3ButtonsAfterGroup;
import static bisq.desktop.util.FormBuilder.addButtonAfterGroup;
import static bisq.desktop.util.FormBuilder.addLabelInputTextField;
import static bisq.desktop.util.FormBuilder.addTitledGroupBg;
import static bisq.desktop.util.FormBuilder.*;

@FxmlView
public class ActiveProposalsView extends BaseProposalView implements BsqBalanceListener {
Expand All @@ -87,6 +77,8 @@ public class ActiveProposalsView extends BaseProposalView implements BsqBalanceL
private Button removeButton, acceptButton, rejectButton, cancelVoteButton, voteButton;
private InputTextField stakeInputTextField;
private List<Node> voteViewItems = new ArrayList<>();
private BusyAnimation voteButtonBusyAnimation;
private Label voteButtonInfoLabel;


///////////////////////////////////////////////////////////////////////////////////////////
Expand All @@ -99,9 +91,11 @@ private ActiveProposalsView(ProposalService voteRequestManger,
BlindVoteService blindVoteService,
BsqWalletService bsqWalletService,
ReadableBsqBlockChain readableBsqBlockChain,
BsqFormatter bsqFormatter) {
super(voteRequestManger, bsqWalletService, readableBsqBlockChain, periodService,
bsqFormatter);
DaoParamService daoParamService,
BsqFormatter bsqFormatter,
BSFormatter btcFormatter) {
super(voteRequestManger, bsqWalletService, readableBsqBlockChain, daoParamService, periodService, bsqFormatter,
btcFormatter);
this.blindVoteService = blindVoteService;
}

Expand All @@ -128,41 +122,43 @@ protected void activate() {

if (voteButton != null) {
voteButton.setOnAction(e -> {
Coin stake = bsqFormatter.parseToCoin(stakeInputTextField.getText());
// TODO verify stake
//TODO show popup
Coin stake = bsqFormatter.parseToCoin(stakeInputTextField.getText());
final Coin fee = BlindVoteConsensus.getFee(daoParamService, readableBsqBlockChain.getChainHeadHeight());
Transaction dummyTx = null;
try {
blindVoteService.publishBlindVote(stake, new FutureCallback<Transaction>() {
@Override
public void onSuccess(@Nullable Transaction result) {
//TODO
}
// We create a tx with dummy opreturn data to get the mining fee for confirmation popup
dummyTx = blindVoteService.getBlindVoteTx(stake, fee, new byte[22]);
} catch (InsufficientMoneyException | WalletException | TransactionVerificationException exception) {
new Popup<>().warning(exception.toString()).show();
}

@Override
public void onFailure(@NotNull Throwable t) {
//TODO
}
});
} catch (CryptoException e1) {
//TODO show error popup
e1.printStackTrace();
} catch (InsufficientBsqException e1) {
e1.printStackTrace();
} catch (WalletException e1) {
e1.printStackTrace();
} catch (TransactionVerificationException e1) {
e1.printStackTrace();
} catch (InsufficientMoneyException e1) {
e1.printStackTrace();
} catch (InvalidProtocolBufferException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
if (dummyTx != null) {
Coin miningFee = dummyTx.getFee();
int txSize = dummyTx.bitcoinSerialize().length;
GUIUtil.showBsqFeeInfoPopup(fee, miningFee, txSize, bsqFormatter, btcFormatter,
Res.get("dao.blindVote"), () -> publishBlindVote(stake));
}
});
}
}

private void publishBlindVote(Coin stake) {
voteButtonBusyAnimation.play();
voteButtonInfoLabel.setText(Res.get("dao.blindVote.startPublishing"));
blindVoteService.publishBlindVote(stake,
() -> {
voteButtonBusyAnimation.stop();
voteButtonInfoLabel.setText("");
new Popup().feedback(Res.get("dao.blindVote.success"))
.show();
}, exception -> {
voteButtonBusyAnimation.stop();
voteButtonInfoLabel.setText("");
new Popup<>().warning(exception.toString()).show();
});
}

@Override
protected void deactivate() {
super.deactivate();
Expand All @@ -183,7 +179,10 @@ private void createVoteView() {
Res.getWithCol("dao.proposal.myVote.stake"), Layout
.FIRST_ROW_AND_GROUP_DISTANCE - 20);
stakeInputTextField = tuple2.second;
voteButton = addButtonAfterGroup(root, ++gridRow, Res.get("dao.proposal.myVote.button"));
Tuple3<Button, BusyAnimation, Label> tuple = addButtonBusyAnimationLabelAfterGroup(root, ++gridRow, Res.get("dao.proposal.myVote.button"));
voteButton = tuple.first;
voteButtonBusyAnimation = tuple.second;
voteButtonInfoLabel = tuple.third;

voteViewItems.add(titledGroupBg);
voteViewItems.add(tuple2.first);
Expand Down Expand Up @@ -236,17 +235,17 @@ protected void onSelectProposal(ProposalListItem item) {
}

private void onAccept() {
selectedProposalListItem.getProposal().setVoteResult(new BooleanVoteResult(true));
selectedProposalListItem.getProposal().setVote(new BooleanVote(true));
updateStateAfterVote();
}

private void onReject() {
selectedProposalListItem.getProposal().setVoteResult(new BooleanVoteResult(false));
selectedProposalListItem.getProposal().setVote(new BooleanVote(false));
updateStateAfterVote();
}

private void onCancelVote() {
selectedProposalListItem.getProposal().setVoteResult(null);
selectedProposalListItem.getProposal().setVote(null);
updateStateAfterVote();
}

Expand All @@ -263,11 +262,12 @@ protected void onPhaseChanged(PeriodService.Phase phase) {
}
if (selectedProposalListItem != null &&
proposalDisplay != null &&
!periodService.isTxInPastCycle(selectedProposalListItem.getProposal().getTxId())) {
!periodService.isTxInPastCycle(selectedProposalListItem.getProposal().getTxId(),
readableBsqBlockChain.getChainHeadHeight())) {
final Proposal proposal = selectedProposalListItem.getProposal();
switch (phase) {
case PROPOSAL:
if (proposalService.isMine(proposal)) {
if (proposalService.isMine(proposal.getProposalPayload())) {
if (removeButton == null) {
removeButton = addButtonAfterGroup(detailsGridPane, proposalDisplay.incrementAndGetGridRow(), Res.get("dao.proposal.active.remove"));
removeButton.setOnAction(event -> onRemove());
Expand Down Expand Up @@ -326,7 +326,7 @@ protected void onPhaseChanged(PeriodService.Phase phase) {

@Override
protected void updateProposalList() {
doUpdateProposalList(proposalService.getActiveProposals());
doUpdateProposalList(proposalService.getActiveOrMyUnconfirmedProposals());
}

private void updateStateAfterVote() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@

import bisq.desktop.common.view.FxmlView;
import bisq.desktop.main.dao.proposal.BaseProposalView;
import bisq.desktop.util.BSFormatter;
import bisq.desktop.util.BsqFormatter;

import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.dao.blockchain.ReadableBsqBlockChain;
import bisq.core.dao.param.DaoParamService;
import bisq.core.dao.vote.PeriodService;
import bisq.core.dao.vote.proposal.ProposalService;

Expand All @@ -40,8 +42,11 @@ private ClosedProposalsView(ProposalService proposalService,
PeriodService periodService,
BsqWalletService bsqWalletService,
ReadableBsqBlockChain readableBsqBlockChain,
BsqFormatter bsqFormatter) {
super(proposalService, bsqWalletService, readableBsqBlockChain, periodService, bsqFormatter);
DaoParamService daoParamService,
BsqFormatter bsqFormatter,
BSFormatter bsFormatter) {
super(proposalService, bsqWalletService, readableBsqBlockChain, daoParamService, periodService,
bsqFormatter, bsFormatter);
}

@Override
Expand Down
Loading

0 comments on commit e515138

Please sign in to comment.