Add DaoService, phases (WIP)

This commit is contained in:
Manfred Karrer 2016-12-25 16:35:47 +01:00
parent 4cbd0f8966
commit cf6f305533
9 changed files with 126 additions and 19 deletions

View File

@ -717,7 +717,6 @@ public class BtcWalletService extends WalletService {
return sendRequest;
}
///////////////////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////////////////

View File

@ -124,6 +124,9 @@ public abstract class WalletService {
wallet.removeEventListener(walletEventListener);
}
public int getBestChainHeight() {
return walletsSetup.getChain().getBestChainHeight();
}
///////////////////////////////////////////////////////////////////////////////////////////
// Listener

View File

@ -37,6 +37,7 @@ public class DaoModule extends AppModule {
protected void configure() {
bind(CompensationRequestManager.class).in(Singleton.class);
bind(VoteManager.class).in(Singleton.class);
bind(DaoService.class).in(Singleton.class);
bind(VotingParameters.class).in(Singleton.class);
}
}

View File

@ -0,0 +1,62 @@
/*
* This file is part of Bitsquare.
*
* Bitsquare 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.
*
* Bitsquare 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 Bitsquare. If not, see <http://www.gnu.org/licenses/>.
*/
package io.bitsquare.dao;
import com.google.inject.Inject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class DaoService {
private static final Logger log = LoggerFactory.getLogger(DaoService.class);
///////////////////////////////////////////////////////////////////////////////////////////
// Constructor
///////////////////////////////////////////////////////////////////////////////////////////
@Inject
public DaoService() {
}
///////////////////////////////////////////////////////////////////////////////////////////
// Public methods
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// Interface implementation: Initializable
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// Getters
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// Setters
///////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////
// Private methods
///////////////////////////////////////////////////////////////////////////////////////////
}

View File

@ -23,6 +23,7 @@ import org.bitcoinj.core.Coin;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// Represents the state of the CompensationRequest data
public final class CompensationRequest implements Persistable {
// That object is saved to disc. We need to take care of changes to not break deserialization.
private static final long serialVersionUID = Version.LOCAL_DB_VERSION;

View File

@ -21,6 +21,7 @@ import com.google.common.util.concurrent.FutureCallback;
import com.google.inject.Inject;
import io.bitsquare.btc.wallet.BtcWalletService;
import io.bitsquare.btc.wallet.SquWalletService;
import io.bitsquare.dao.vote.VotingParameters;
import io.bitsquare.p2p.P2PService;
import io.bitsquare.p2p.storage.HashMapChangedListener;
import io.bitsquare.p2p.storage.payload.StoragePayload;
@ -35,15 +36,21 @@ import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import static com.google.common.base.Preconditions.checkArgument;
public class CompensationRequestManager {
private static final Logger log = LoggerFactory.getLogger(CompensationRequestManager.class);
private static final int GENESIS_BLOCK_HEIGHT = 391; // TODO dev version regtest
private P2PService p2PService;
private Storage<ArrayList<CompensationRequest>> compensationRequestsStorage;
private BtcWalletService btcWalletService;
private SquWalletService squWalletService;
private VotingParameters votingParameters;
private ObservableList<CompensationRequest> observableCompensationRequestsList = FXCollections.observableArrayList();
private CompensationRequest selectedCompensationRequest;
private int bestChainHeight = -1;
///////////////////////////////////////////////////////////////////////////////////////////
@ -52,11 +59,12 @@ public class CompensationRequestManager {
@Inject
public CompensationRequestManager(P2PService p2PService, Storage<ArrayList<CompensationRequest>> compensationRequestsStorage,
BtcWalletService btcWalletService, SquWalletService squWalletService) {
BtcWalletService btcWalletService, SquWalletService squWalletService, VotingParameters votingParameters) {
this.p2PService = p2PService;
this.compensationRequestsStorage = compensationRequestsStorage;
this.btcWalletService = btcWalletService;
this.squWalletService = squWalletService;
this.votingParameters = votingParameters;
init(compensationRequestsStorage);
}
@ -76,7 +84,7 @@ public class CompensationRequestManager {
@Override
public void onRemoved(ProtectedStorageEntry data) {
// We don't remove items
// TODO
}
});
@ -88,12 +96,31 @@ public class CompensationRequestManager {
});
}
public void onAllServicesInitialized() {
}
//TODO WIP
public void setPhase() {
bestChainHeight = btcWalletService.getBestChainHeight();
checkArgument(bestChainHeight >= GENESIS_BLOCK_HEIGHT, "GENESIS_BLOCK_HEIGHT must be in the past");
int pastBlocks = bestChainHeight - GENESIS_BLOCK_HEIGHT;
int periodIndex = pastBlocks / votingParameters.getTotalPeriodInBlocks();
int startPhase1 = GENESIS_BLOCK_HEIGHT + periodIndex * votingParameters.getTotalPeriodInBlocks();
int endPhase1 = startPhase1 + votingParameters.getCompensationRequestPeriodInBlocks();
}
public void addToP2PNetwork(CompensationRequestPayload compensationRequestPayload) {
p2PService.addData(compensationRequestPayload, true);
}
public void addToList(CompensationRequestPayload compensationRequestPayload, boolean storeLocally) {
if (!observableCompensationRequestsList.stream().filter(e -> e.getCompensationRequestPayload().equals(compensationRequestPayload)).findAny().isPresent()) {
if (!contains(compensationRequestPayload)) {
observableCompensationRequestsList.add(new CompensationRequest(compensationRequestPayload));
if (storeLocally)
compensationRequestsStorage.queueUpForSave(new ArrayList<>(observableCompensationRequestsList), 500);
@ -102,6 +129,10 @@ public class CompensationRequestManager {
}
}
private boolean contains(CompensationRequestPayload compensationRequestPayload) {
return observableCompensationRequestsList.stream().filter(e -> e.getCompensationRequestPayload().equals(compensationRequestPayload)).findAny().isPresent();
}
public ObservableList<CompensationRequest> getObservableCompensationRequestsList() {
return observableCompensationRequestsList;
}
@ -118,5 +149,4 @@ public class CompensationRequestManager {
return selectedCompensationRequest;
}
}

View File

@ -31,6 +31,7 @@ import java.security.PublicKey;
import java.util.Date;
import java.util.concurrent.TimeUnit;
// The data of the CompensationRequest
public final class CompensationRequestPayload implements LazyProcessedStoragePayload, PersistedStoragePayload {
// That object is sent over the wire, so we need to take care of version compatibility.
private static final long serialVersionUID = Version.P2P_NETWORK_VERSION;
@ -40,6 +41,8 @@ public final class CompensationRequestPayload implements LazyProcessedStoragePay
public static final long TTL = TimeUnit.DAYS.toMillis(30);
public final byte version;
private final long creationDate;
public final String uid;
public final String name;
public final String title;
@ -51,7 +54,7 @@ public final class CompensationRequestPayload implements LazyProcessedStoragePay
private final long requestedBtc;
public final String btcAddress;
private final String nodeAddress;
private final long creationDate;
@JsonExclude
private PublicKey p2pStorageSignaturePubKey;
// used for json

View File

@ -73,10 +73,10 @@ public class VotingParameters {
private double conversionRate = 0.000001; // how many btc you get for 1 squ (e.g. 1 000 000 squ = 1 btc)
// 144 blocks is 1 day
private long compensationRequestPeriodInBlocks = 2880; // 20 days
private long votingPeriodInBlocks = 432; // 3 days
private long fundingPeriodInBlocks = 1008; // 7 days
private long breakBetweenPeriodsInBlocks = 10;
private int compensationRequestPeriodInBlocks = 2880; // 20 days
private int votingPeriodInBlocks = 432; // 3 days
private int fundingPeriodInBlocks = 1008; // 7 days
private int breakBetweenPeriodsInBlocks = 10;
private double quorumForCompensationRequestVoting = 5; // 5%
private double quorumForParameterVoting = 5; // 5%
@ -157,35 +157,39 @@ public class VotingParameters {
this.conversionRate = conversionRate;
}
public long getCompensationRequestPeriodInBlocks() {
public int getCompensationRequestPeriodInBlocks() {
return compensationRequestPeriodInBlocks;
}
public void setCompensationRequestPeriodInBlocks(long compensationRequestPeriodInBlocks) {
public int getTotalPeriodInBlocks() {
return compensationRequestPeriodInBlocks + votingPeriodInBlocks + fundingPeriodInBlocks + 3 * breakBetweenPeriodsInBlocks;
}
public void setCompensationRequestPeriodInBlocks(int compensationRequestPeriodInBlocks) {
this.compensationRequestPeriodInBlocks = compensationRequestPeriodInBlocks;
}
public long getVotingPeriodInBlocks() {
public int getVotingPeriodInBlocks() {
return votingPeriodInBlocks;
}
public void setVotingPeriodInBlocks(long votingPeriodInBlocks) {
public void setVotingPeriodInBlocks(int votingPeriodInBlocks) {
this.votingPeriodInBlocks = votingPeriodInBlocks;
}
public long getFundingPeriodInBlocks() {
public int getFundingPeriodInBlocks() {
return fundingPeriodInBlocks;
}
public void setFundingPeriodInBlocks(long fundingPeriodInBlocks) {
public void setFundingPeriodInBlocks(int fundingPeriodInBlocks) {
this.fundingPeriodInBlocks = fundingPeriodInBlocks;
}
public long getBreakBetweenPeriodsInBlocks() {
public int getBreakBetweenPeriodsInBlocks() {
return breakBetweenPeriodsInBlocks;
}
public void setBreakBetweenPeriodsInBlocks(long breakBetweenPeriodsInBlocks) {
public void setBreakBetweenPeriodsInBlocks(int breakBetweenPeriodsInBlocks) {
this.breakBetweenPeriodsInBlocks = breakBetweenPeriodsInBlocks;
}

View File

@ -42,6 +42,7 @@ import io.bitsquare.common.Clock;
import io.bitsquare.common.Timer;
import io.bitsquare.common.UserThread;
import io.bitsquare.common.crypto.*;
import io.bitsquare.dao.compensation.CompensationRequestManager;
import io.bitsquare.filter.FilterManager;
import io.bitsquare.gui.Navigation;
import io.bitsquare.gui.common.model.ViewModel;
@ -117,6 +118,7 @@ public class MainViewModel implements ViewModel {
private final Clock clock;
private final FeeService feeService;
private final SquUtxoFeedService squUtxoFeedService;
private CompensationRequestManager compensationRequestManager;
private final KeyRing keyRing;
private final Navigation navigation;
private final BSFormatter formatter;
@ -185,7 +187,7 @@ public class MainViewModel implements ViewModel {
User user, AlertManager alertManager, PrivateNotificationManager privateNotificationManager,
FilterManager filterManager, WalletPasswordWindow walletPasswordWindow, AddBitcoinNodesWindow addBitcoinNodesWindow,
NotificationCenter notificationCenter, TacWindow tacWindow, Clock clock, FeeService feeService, SquUtxoFeedService squUtxoFeedService,
KeyRing keyRing, Navigation navigation, BSFormatter formatter) {
CompensationRequestManager compensationRequestManager, KeyRing keyRing, Navigation navigation, BSFormatter formatter) {
this.walletsManager = walletsManager;
this.walletsSetup = walletsSetup;
this.btcWalletService = btcWalletService;
@ -207,6 +209,7 @@ public class MainViewModel implements ViewModel {
this.clock = clock;
this.feeService = feeService;
this.squUtxoFeedService = squUtxoFeedService;
this.compensationRequestManager = compensationRequestManager;
this.keyRing = keyRing;
this.navigation = navigation;
this.formatter = formatter;
@ -550,6 +553,7 @@ public class MainViewModel implements ViewModel {
feeService.onAllServicesInitialized();
squUtxoFeedService.onAllServicesInitialized();
compensationRequestManager.onAllServicesInitialized();
setupBtcNumPeersWatcher();
setupP2PNumPeersWatcher();