Skip to content

Commit

Permalink
Adopt to new PersistenceManager
Browse files Browse the repository at this point in the history
  • Loading branch information
chimp1984 committed Oct 22, 2020
1 parent 57064a0 commit 9a018cc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@Slf4j
public class AccountAgeWitnessStore extends PersistableNetworkPayloadStore<AccountAgeWitness> {

AccountAgeWitnessStore() {
public AccountAgeWitnessStore() {
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
@Slf4j
public class TradeStatistics3Store extends PersistableNetworkPayloadStore<TradeStatistics3> {

TradeStatistics3Store() {
public TradeStatistics3Store() {
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import bisq.core.account.witness.AccountAgeWitnessStore;
import bisq.core.proto.network.CoreNetworkProtoResolver;
import bisq.core.proto.persistable.CorePersistenceProtoResolver;
import bisq.core.trade.statistics.TradeStatistics2Store;
import bisq.core.trade.statistics.TradeStatistics3Store;

import bisq.network.p2p.CloseConnectionMessage;
import bisq.network.p2p.NodeAddress;
Expand All @@ -38,9 +38,8 @@

import bisq.common.app.Version;
import bisq.common.config.BaseCurrencyNetwork;
import bisq.common.persistence.PersistenceManager;
import bisq.common.proto.network.NetworkEnvelope;
import bisq.common.proto.persistable.PersistableEnvelope;
import bisq.common.storage.Storage;

import com.google.common.util.concurrent.FutureCallback;
import com.google.common.util.concurrent.Futures;
Expand Down Expand Up @@ -90,7 +89,7 @@ public abstract class P2PSeedNodeSnapshotBase extends Metric implements MessageL
*
* @param <T> the value type of the statistics implementation
*/
protected abstract class Statistics<T> {
protected abstract static class Statistics<T> {
protected final Map<String, T> buckets = new HashMap<>();

abstract void log(Object message);
Expand All @@ -116,12 +115,29 @@ public void configure(Properties properties) {
File dir = new File(configuration.getProperty(DATABASE_DIR));
String networkPostfix = "_" + BaseCurrencyNetwork.values()[Version.getBaseCurrencyNetwork()].toString();
try {
Storage<PersistableEnvelope> storage = new Storage<>(dir, new CorePersistenceProtoResolver(null, null, null, null), null);
TradeStatistics2Store tradeStatistics2Store = (TradeStatistics2Store) storage.initAndGetPersistedWithFileName(TradeStatistics2Store.class.getSimpleName() + networkPostfix, 0);
hashes.addAll(tradeStatistics2Store.getMap().keySet().stream().map(byteArray -> byteArray.bytes).collect(Collectors.toList()));

AccountAgeWitnessStore accountAgeWitnessStore = (AccountAgeWitnessStore) storage.initAndGetPersistedWithFileName(AccountAgeWitnessStore.class.getSimpleName() + networkPostfix, 0);
hashes.addAll(accountAgeWitnessStore.getMap().keySet().stream().map(byteArray -> byteArray.bytes).collect(Collectors.toList()));
CorePersistenceProtoResolver persistenceProtoResolver = new CorePersistenceProtoResolver(null, null);

TradeStatistics3Store tradeStatistics3Store = new TradeStatistics3Store();
PersistenceManager<TradeStatistics3Store> tradeStatistics3PersistenceManager = new PersistenceManager<>(dir,
persistenceProtoResolver, null);
tradeStatistics3PersistenceManager.initialize(tradeStatistics3Store, PersistenceManager.Source.NETWORK);
TradeStatistics3Store persistedTradeStatistics3Store = tradeStatistics3PersistenceManager.getPersisted();
if (persistedTradeStatistics3Store != null) {
tradeStatistics3Store.getMap().putAll(persistedTradeStatistics3Store.getMap());
}
hashes.addAll(tradeStatistics3Store.getMap().keySet().stream()
.map(byteArray -> byteArray.bytes).collect(Collectors.toSet()));

AccountAgeWitnessStore accountAgeWitnessStore = new AccountAgeWitnessStore();
PersistenceManager<AccountAgeWitnessStore> accountAgeWitnessPersistenceManager = new PersistenceManager<>(dir,
persistenceProtoResolver, null);
accountAgeWitnessPersistenceManager.initialize(accountAgeWitnessStore, PersistenceManager.Source.NETWORK);
AccountAgeWitnessStore persistedAccountAgeWitnessStore = accountAgeWitnessPersistenceManager.getPersisted();
if (persistedAccountAgeWitnessStore != null) {
accountAgeWitnessStore.getMap().putAll(persistedAccountAgeWitnessStore.getMap());
}
hashes.addAll(accountAgeWitnessStore.getMap().keySet().stream()
.map(byteArray -> byteArray.bytes).collect(Collectors.toSet()));
} catch (NullPointerException e) {
// in case there is no store file
log.error("There is no storage file where there should be one: {}", dir.getAbsolutePath());
Expand Down Expand Up @@ -156,32 +172,32 @@ protected void send(NetworkNode networkNode, NetworkEnvelope message) {
for (String current : configuration.getProperty(HOSTS, "").split(",")) {
threadList.add(new Thread(() -> {

try {
// parse Url
NodeAddress target = OnionParser.getNodeAddress(current);

// do the data request
aboutToSend(message);
SettableFuture<Connection> future = networkNode.sendMessage(target, message);

Futures.addCallback(future, new FutureCallback<>() {
@Override
public void onSuccess(Connection connection) {
connection.addMessageListener(P2PSeedNodeSnapshotBase.this);
}

@Override
public void onFailure(@NotNull Throwable throwable) {
gate.proceed();
log.error(
"Sending {} failed. That is expected if the peer is offline.\n\tException={}", message.getClass().getSimpleName(), throwable.getMessage());
}
}, MoreExecutors.directExecutor());

} catch (Exception e) {
gate.proceed(); // release the gate on error
e.printStackTrace();
}
try {
// parse Url
NodeAddress target = OnionParser.getNodeAddress(current);

// do the data request
aboutToSend(message);
SettableFuture<Connection> future = networkNode.sendMessage(target, message);

Futures.addCallback(future, new FutureCallback<>() {
@Override
public void onSuccess(Connection connection) {
connection.addMessageListener(P2PSeedNodeSnapshotBase.this);
}

@Override
public void onFailure(@NotNull Throwable throwable) {
gate.proceed();
log.error(
"Sending {} failed. That is expected if the peer is offline.\n\tException={}", message.getClass().getSimpleName(), throwable.getMessage());
}
}, MoreExecutors.directExecutor());

} catch (Exception e) {
gate.proceed(); // release the gate on error
e.printStackTrace();
}
}, current));
}

Expand All @@ -195,7 +211,8 @@ public void onFailure(@NotNull Throwable throwable) {
gate.await();
}

protected void aboutToSend(NetworkEnvelope message) { };
protected void aboutToSend(NetworkEnvelope message) {
}

/**
* Report all the stuff. Uses the configured reporter directly.
Expand Down

0 comments on commit 9a018cc

Please sign in to comment.