Search in sources :

Example 1 with UtxoConfigPersisted

use of com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisted in project sparrow by sparrowwallet.

the class SparrowUtxoConfigPersister method doWrite.

@Override
protected void doWrite(UtxoConfigData data) throws Exception {
    Wallet wallet = getWallet();
    if (wallet == null) {
        // Wallet is already closed
        return;
    }
    Map<String, UtxoConfigPersisted> currentData = new HashMap<>(data.getUtxoConfigs());
    Map<Sha256Hash, UtxoMixData> changedUtxoMixes = currentData.entrySet().stream().collect(Collectors.toMap(entry -> Sha256Hash.wrap(entry.getKey()), entry -> new UtxoMixData(entry.getValue().getMixsDone(), entry.getValue().getExpired()), (u, v) -> {
        throw new IllegalStateException("Duplicate utxo config hashes");
    }, HashMap::new));
    MapDifference<Sha256Hash, UtxoMixData> mapDifference = Maps.difference(changedUtxoMixes, wallet.getUtxoMixes());
    Map<Sha256Hash, UtxoMixData> removedUtxoMixes = mapDifference.entriesOnlyOnRight();
    wallet.getUtxoMixes().putAll(changedUtxoMixes);
    wallet.getUtxoMixes().keySet().removeAll(removedUtxoMixes.keySet());
    if (!changedUtxoMixes.isEmpty() || !removedUtxoMixes.isEmpty()) {
        EventManager.get().post(new WalletUtxoMixesChangedEvent(wallet, changedUtxoMixes, removedUtxoMixes));
    }
}
Also used : Logger(org.slf4j.Logger) UtxoMixData(com.sparrowwallet.drongo.wallet.UtxoMixData) Wallet(com.sparrowwallet.drongo.wallet.Wallet) LoggerFactory(org.slf4j.LoggerFactory) UtxoConfigPersisted(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisted) HashMap(java.util.HashMap) Maps(com.google.common.collect.Maps) WalletUtxoMixesChangedEvent(com.sparrowwallet.sparrow.event.WalletUtxoMixesChangedEvent) Collectors(java.util.stream.Collectors) UtxoConfigData(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigData) MapDifference(com.google.common.collect.MapDifference) AppServices(com.sparrowwallet.sparrow.AppServices) Map(java.util.Map) EventManager(com.sparrowwallet.sparrow.EventManager) UtxoConfigPersister(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersister) Sha256Hash(com.sparrowwallet.drongo.protocol.Sha256Hash) WalletUtxoMixesChangedEvent(com.sparrowwallet.sparrow.event.WalletUtxoMixesChangedEvent) HashMap(java.util.HashMap) UtxoMixData(com.sparrowwallet.drongo.wallet.UtxoMixData) Wallet(com.sparrowwallet.drongo.wallet.Wallet) UtxoConfigPersisted(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisted) Sha256Hash(com.sparrowwallet.drongo.protocol.Sha256Hash)

Example 2 with UtxoConfigPersisted

use of com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisted in project sparrow by sparrowwallet.

the class SparrowUtxoConfigPersister method read.

@Override
public synchronized UtxoConfigData read() throws Exception {
    Wallet wallet = getWallet();
    if (wallet == null) {
        throw new IllegalStateException("Can't find wallet with walletId " + walletId);
    }
    Map<String, UtxoConfigPersisted> utxoConfigs = wallet.getUtxoMixes().entrySet().stream().collect(Collectors.toMap(entry -> entry.getKey().toString(), entry -> new UtxoConfigPersisted(entry.getValue().getMixesDone(), entry.getValue().getExpired()), (u, v) -> {
        throw new IllegalStateException("Duplicate utxo config hashes");
    }, HashMap::new));
    return new UtxoConfigData(utxoConfigs);
}
Also used : Logger(org.slf4j.Logger) UtxoMixData(com.sparrowwallet.drongo.wallet.UtxoMixData) Wallet(com.sparrowwallet.drongo.wallet.Wallet) LoggerFactory(org.slf4j.LoggerFactory) UtxoConfigPersisted(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisted) HashMap(java.util.HashMap) Maps(com.google.common.collect.Maps) WalletUtxoMixesChangedEvent(com.sparrowwallet.sparrow.event.WalletUtxoMixesChangedEvent) Collectors(java.util.stream.Collectors) UtxoConfigData(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigData) MapDifference(com.google.common.collect.MapDifference) AppServices(com.sparrowwallet.sparrow.AppServices) Map(java.util.Map) EventManager(com.sparrowwallet.sparrow.EventManager) UtxoConfigPersister(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersister) Sha256Hash(com.sparrowwallet.drongo.protocol.Sha256Hash) Wallet(com.sparrowwallet.drongo.wallet.Wallet) UtxoConfigPersisted(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisted) UtxoConfigData(com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigData)

Aggregations

MapDifference (com.google.common.collect.MapDifference)2 Maps (com.google.common.collect.Maps)2 UtxoConfigData (com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigData)2 UtxoConfigPersisted (com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersisted)2 UtxoConfigPersister (com.samourai.whirlpool.client.wallet.data.utxoConfig.UtxoConfigPersister)2 Sha256Hash (com.sparrowwallet.drongo.protocol.Sha256Hash)2 UtxoMixData (com.sparrowwallet.drongo.wallet.UtxoMixData)2 Wallet (com.sparrowwallet.drongo.wallet.Wallet)2 AppServices (com.sparrowwallet.sparrow.AppServices)2 EventManager (com.sparrowwallet.sparrow.EventManager)2 WalletUtxoMixesChangedEvent (com.sparrowwallet.sparrow.event.WalletUtxoMixesChangedEvent)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2