forked from bisq-network/bisq
/
ProposalListPresentation.java
211 lines (174 loc) · 9.08 KB
/
ProposalListPresentation.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.core.dao.governance.proposal;
import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.dao.DaoSetupService;
import bisq.core.dao.governance.proposal.storage.appendonly.ProposalPayload;
import bisq.core.dao.governance.proposal.storage.temp.TempProposalPayload;
import bisq.core.dao.state.DaoStateListener;
import bisq.core.dao.state.DaoStateService;
import bisq.core.dao.state.model.blockchain.Block;
import bisq.core.dao.state.model.governance.Proposal;
import bisq.network.p2p.storage.HashMapChangedListener;
import bisq.network.p2p.storage.P2PDataStorage;
import bisq.network.p2p.storage.payload.ProtectedStorageEntry;
import bisq.common.UserThread;
import org.bitcoinj.core.TransactionConfidence;
import com.google.inject.Inject;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.collections.transformation.FilteredList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
/**
* Provides filtered observableLists of the Proposals from proposalService and myProposalListService.
* We want to show the own proposals in unconfirmed state (validation of phase and cycle cannot be done but as it is
* our own proposal that is not critical). Foreign proposals are only shown if confirmed and fully validated.
*/
@Slf4j
public class ProposalListPresentation implements DaoStateListener, HashMapChangedListener,
MyProposalListService.Listener, DaoSetupService {
private final ProposalService proposalService;
private final DaoStateService daoStateService;
private final MyProposalListService myProposalListService;
private final BsqWalletService bsqWalletService;
private final ProposalValidatorProvider validatorProvider;
private final ObservableList<Proposal> allProposals = FXCollections.observableArrayList();
@Getter
private final FilteredList<Proposal> activeOrMyUnconfirmedProposals = new FilteredList<>(allProposals);
private final ListChangeListener<Proposal> proposalListChangeListener;
private boolean tempProposalsChanged;
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public ProposalListPresentation(ProposalService proposalService,
DaoStateService daoStateService,
P2PDataStorage p2PDataStorage,
MyProposalListService myProposalListService,
BsqWalletService bsqWalletService,
ProposalValidatorProvider validatorProvider) {
this.proposalService = proposalService;
this.daoStateService = daoStateService;
this.myProposalListService = myProposalListService;
this.bsqWalletService = bsqWalletService;
this.validatorProvider = validatorProvider;
daoStateService.addDaoStateListener(this);
p2PDataStorage.addHashMapChangedListener(this);
myProposalListService.addListener(this);
proposalListChangeListener = c -> updateLists();
}
///////////////////////////////////////////////////////////////////////////////////////////
// DaoSetupService
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void addListeners() {
}
@Override
public void start() {
// We must set the listeners initially and not on onParseBlockChainComplete as activeOrMyUnconfirmedProposals
// is used in voteResults which can be called earlier during sync.
// To avoid unneeded upDateLists calls we delay one render frame so that once the proposalService is complete we
// register out listeners.
UserThread.execute(() -> {
proposalService.getTempProposals().addListener(proposalListChangeListener);
proposalService.getProposalPayloads().addListener((ListChangeListener<ProposalPayload>) c -> updateLists());
});
}
///////////////////////////////////////////////////////////////////////////////////////////
// DaoStateListener
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onParseBlockCompleteAfterBatchProcessing(Block block) {
updateLists();
}
///////////////////////////////////////////////////////////////////////////////////////////
// HashMapChangedListener
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onAdded(ProtectedStorageEntry entry) {
if (entry.getProtectedStoragePayload() instanceof TempProposalPayload) {
tempProposalsChanged = true;
}
}
@Override
public void onRemoved(ProtectedStorageEntry entry) {
if (entry.getProtectedStoragePayload() instanceof TempProposalPayload) {
tempProposalsChanged = true;
}
}
@Override
public void onBatchRemoveExpiredDataStarted() {
// We temporary remove the listener when batch processing starts to avoid that we rebuild our lists at each
// remove call. After batch processing at onBatchRemoveExpiredDataCompleted we add again our listener and call
// the updateLists method.
proposalService.getTempProposals().removeListener(proposalListChangeListener);
}
@Override
public void onBatchRemoveExpiredDataCompleted() {
proposalService.getTempProposals().addListener(proposalListChangeListener);
// We only call updateLists if tempProposals have changed. updateLists() is an expensive call and takes 200 ms.
if (tempProposalsChanged) {
updateLists();
tempProposalsChanged = false;
}
}
///////////////////////////////////////////////////////////////////////////////////////////
// MyProposalListService.Listener
///////////////////////////////////////////////////////////////////////////////////////////
@Override
public void onListChanged(List<Proposal> list) {
updateLists();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Private
///////////////////////////////////////////////////////////////////////////////////////////
private void updateLists() {
List<Proposal> tempProposals = proposalService.getTempProposals();
Set<Proposal> verifiedProposals = proposalService.getProposalPayloads().stream()
.map(ProposalPayload::getProposal)
.filter(proposal -> !daoStateService.isParseBlockChainComplete() ||
validatorProvider.getValidator(proposal).isValidAndConfirmed(proposal))
.collect(Collectors.toSet());
Set<Proposal> set = new HashSet<>(tempProposals);
set.addAll(verifiedProposals);
// We want to show our own unconfirmed proposals. Unconfirmed proposals from other users are not included
// in the list.
// If a tx is not found in the daoStateService it can be that it is either unconfirmed or invalid.
// To avoid inclusion of invalid txs we add a check for the confidence type PENDING from the bsqWalletService.
// So we only add proposals if they are unconfirmed and therefor not yet parsed. Once confirmed they have to be
// found in the daoStateService.
List<Proposal> myUnconfirmedProposals = myProposalListService.getList().stream()
.filter(p -> !daoStateService.getTx(p.getTxId()).isPresent()) // Tx is still not in our bsq blocks
.filter(p -> {
TransactionConfidence confidenceForTxId = bsqWalletService.getConfidenceForTxId(p.getTxId());
return confidenceForTxId != null &&
confidenceForTxId.getConfidenceType() == TransactionConfidence.ConfidenceType.PENDING;
})
.collect(Collectors.toList());
set.addAll(myUnconfirmedProposals);
allProposals.clear();
allProposals.addAll(set);
activeOrMyUnconfirmedProposals.setPredicate(proposal -> validatorProvider.getValidator(proposal).isValidAndConfirmed(proposal) ||
myUnconfirmedProposals.contains(proposal));
}
}