Search in sources :

Example 1 with WalletUtxoMixesChangedEvent

use of com.sparrowwallet.sparrow.event.WalletUtxoMixesChangedEvent 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)

Aggregations

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