Search in sources :

Example 1 with TransactionReconstruction

use of org.thoughtcrime.securesms.payments.history.TransactionReconstruction in project Signal-Android by WhisperSystems.

the class LedgerReconcile method reconstructAllTransactions.

@NonNull
private static List<DetailedTransaction> reconstructAllTransactions(@NonNull Set<MobileCoinLedgerWrapper.OwnedTxo> unknownReceived, @NonNull Set<MobileCoinLedgerWrapper.OwnedTxo> unknownSpent) {
    Set<Long> allBlocksWithActivity = Stream.of(unknownReceived).map(MobileCoinLedgerWrapper.OwnedTxo::getReceivedInBlock).collect(Collectors.toSet());
    allBlocksWithActivity.addAll(Stream.of(unknownSpent).map(MobileCoinLedgerWrapper.OwnedTxo::getSpentInBlock).collect(Collectors.toSet()));
    Map<Long, List<MobileCoinLedgerWrapper.OwnedTxo>> receivedInBlock = Stream.of(unknownReceived).collect(Collectors.groupingBy(MobileCoinLedgerWrapper.OwnedTxo::getReceivedInBlock));
    Map<Long, List<MobileCoinLedgerWrapper.OwnedTxo>> spentInBlock = Stream.of(unknownSpent).filter(MobileCoinLedgerWrapper.OwnedTxo::isSpent).collect(Collectors.groupingBy(MobileCoinLedgerWrapper.OwnedTxo::getSpentInBlock));
    return Stream.of(allBlocksWithActivity).sorted((a, b) -> b.compareTo(a)).flatMap(blockIndex -> {
        List<MobileCoinLedgerWrapper.OwnedTxo> unspent = MapUtil.getOrDefault(receivedInBlock, blockIndex, Collections.emptyList());
        List<MobileCoinLedgerWrapper.OwnedTxo> spent = MapUtil.getOrDefault(spentInBlock, blockIndex, Collections.emptyList());
        if (spent.size() + unspent.size() == 0) {
            throw new AssertionError();
        }
        Long timeStamp = null;
        if (spent.size() > 0) {
            timeStamp = spent.get(0).getSpentInBlockTimestamp();
        }
        if (timeStamp == null && unspent.size() > 0) {
            timeStamp = unspent.get(0).getReceivedInBlockTimestamp();
        }
        TransactionReconstruction transactionReconstruction = TransactionReconstruction.estimateBlockLevelActivity(toMobileCoinList(spent), toMobileCoinList(unspent));
        BlockDetail blockDetail = new BlockDetail(blockIndex, timeStamp);
        return Stream.of(transactionReconstruction.getAllTransactions()).map(t -> new DetailedTransaction(blockDetail, t));
    }).sorted(DetailedTransaction.DESCENDING).toList();
}
Also used : PaymentMetaData(org.thoughtcrime.securesms.payments.proto.PaymentMetaData) PaymentDecorator(org.thoughtcrime.securesms.payments.PaymentDecorator) MapUtil(org.signal.core.util.MapUtil) Stream(com.annimon.stream.Stream) NonNull(androidx.annotation.NonNull) MobileCoinLedgerWrapper(org.thoughtcrime.securesms.payments.MobileCoinLedgerWrapper) WorkerThread(androidx.annotation.WorkerThread) HashMap(java.util.HashMap) ComparatorCompat(com.annimon.stream.ComparatorCompat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Locale(java.util.Locale) Map(java.util.Map) TransactionReconstruction(org.thoughtcrime.securesms.payments.history.TransactionReconstruction) Nullable(javax.annotation.Nullable) State(org.thoughtcrime.securesms.payments.State) Collectors(com.annimon.stream.Collectors) Collection(java.util.Collection) Set(java.util.Set) ByteString(com.google.protobuf.ByteString) Log(org.signal.core.util.logging.Log) List(java.util.List) ReconstructedPayment(org.thoughtcrime.securesms.payments.ReconstructedPayment) Payment(org.thoughtcrime.securesms.payments.Payment) Comparator(java.util.Comparator) Collections(java.util.Collections) Money(org.whispersystems.signalservice.api.payments.Money) TransactionReconstruction(org.thoughtcrime.securesms.payments.history.TransactionReconstruction) ArrayList(java.util.ArrayList) List(java.util.List) MobileCoinLedgerWrapper(org.thoughtcrime.securesms.payments.MobileCoinLedgerWrapper) NonNull(androidx.annotation.NonNull)

Example 2 with TransactionReconstruction

use of org.thoughtcrime.securesms.payments.history.TransactionReconstruction in project Signal-Android by signalapp.

the class LedgerReconcile method reconstructAllTransactions.

@NonNull
private static List<DetailedTransaction> reconstructAllTransactions(@NonNull Set<MobileCoinLedgerWrapper.OwnedTxo> unknownReceived, @NonNull Set<MobileCoinLedgerWrapper.OwnedTxo> unknownSpent) {
    Set<Long> allBlocksWithActivity = Stream.of(unknownReceived).map(MobileCoinLedgerWrapper.OwnedTxo::getReceivedInBlock).collect(Collectors.toSet());
    allBlocksWithActivity.addAll(Stream.of(unknownSpent).map(MobileCoinLedgerWrapper.OwnedTxo::getSpentInBlock).collect(Collectors.toSet()));
    Map<Long, List<MobileCoinLedgerWrapper.OwnedTxo>> receivedInBlock = Stream.of(unknownReceived).collect(Collectors.groupingBy(MobileCoinLedgerWrapper.OwnedTxo::getReceivedInBlock));
    Map<Long, List<MobileCoinLedgerWrapper.OwnedTxo>> spentInBlock = Stream.of(unknownSpent).filter(MobileCoinLedgerWrapper.OwnedTxo::isSpent).collect(Collectors.groupingBy(MobileCoinLedgerWrapper.OwnedTxo::getSpentInBlock));
    return Stream.of(allBlocksWithActivity).sorted((a, b) -> b.compareTo(a)).flatMap(blockIndex -> {
        List<MobileCoinLedgerWrapper.OwnedTxo> unspent = MapUtil.getOrDefault(receivedInBlock, blockIndex, Collections.emptyList());
        List<MobileCoinLedgerWrapper.OwnedTxo> spent = MapUtil.getOrDefault(spentInBlock, blockIndex, Collections.emptyList());
        if (spent.size() + unspent.size() == 0) {
            throw new AssertionError();
        }
        Long timeStamp = null;
        if (spent.size() > 0) {
            timeStamp = spent.get(0).getSpentInBlockTimestamp();
        }
        if (timeStamp == null && unspent.size() > 0) {
            timeStamp = unspent.get(0).getReceivedInBlockTimestamp();
        }
        TransactionReconstruction transactionReconstruction = TransactionReconstruction.estimateBlockLevelActivity(toMobileCoinList(spent), toMobileCoinList(unspent));
        BlockDetail blockDetail = new BlockDetail(blockIndex, timeStamp);
        return Stream.of(transactionReconstruction.getAllTransactions()).map(t -> new DetailedTransaction(blockDetail, t));
    }).sorted(DetailedTransaction.DESCENDING).toList();
}
Also used : PaymentMetaData(org.thoughtcrime.securesms.payments.proto.PaymentMetaData) PaymentDecorator(org.thoughtcrime.securesms.payments.PaymentDecorator) MapUtil(org.signal.core.util.MapUtil) Stream(com.annimon.stream.Stream) NonNull(androidx.annotation.NonNull) MobileCoinLedgerWrapper(org.thoughtcrime.securesms.payments.MobileCoinLedgerWrapper) WorkerThread(androidx.annotation.WorkerThread) HashMap(java.util.HashMap) ComparatorCompat(com.annimon.stream.ComparatorCompat) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Locale(java.util.Locale) Map(java.util.Map) TransactionReconstruction(org.thoughtcrime.securesms.payments.history.TransactionReconstruction) Nullable(javax.annotation.Nullable) State(org.thoughtcrime.securesms.payments.State) Collectors(com.annimon.stream.Collectors) Collection(java.util.Collection) Set(java.util.Set) ByteString(com.google.protobuf.ByteString) Log(org.signal.core.util.logging.Log) List(java.util.List) ReconstructedPayment(org.thoughtcrime.securesms.payments.ReconstructedPayment) Payment(org.thoughtcrime.securesms.payments.Payment) Comparator(java.util.Comparator) Collections(java.util.Collections) Money(org.whispersystems.signalservice.api.payments.Money) TransactionReconstruction(org.thoughtcrime.securesms.payments.history.TransactionReconstruction) ArrayList(java.util.ArrayList) List(java.util.List) MobileCoinLedgerWrapper(org.thoughtcrime.securesms.payments.MobileCoinLedgerWrapper) NonNull(androidx.annotation.NonNull)

Aggregations

NonNull (androidx.annotation.NonNull)2 WorkerThread (androidx.annotation.WorkerThread)2 Collectors (com.annimon.stream.Collectors)2 ComparatorCompat (com.annimon.stream.ComparatorCompat)2 Stream (com.annimon.stream.Stream)2 ByteString (com.google.protobuf.ByteString)2 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Collections (java.util.Collections)2 Comparator (java.util.Comparator)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 List (java.util.List)2 Locale (java.util.Locale)2 Map (java.util.Map)2 Set (java.util.Set)2 Nullable (javax.annotation.Nullable)2 MapUtil (org.signal.core.util.MapUtil)2 Log (org.signal.core.util.logging.Log)2 MobileCoinLedgerWrapper (org.thoughtcrime.securesms.payments.MobileCoinLedgerWrapper)2