Exemplo n.º 1
0
        public bool TryAdd(SmartCoin coin)
        {
            var added = false;

            lock (Lock)
            {
                if (!SpentCoins.Contains(coin))
                {
                    added = Coins.Add(coin);
                    if (added)
                    {
                        if (ClustersByScriptPubKey.TryGetValue(coin.ScriptPubKey, out var cluster))
                        {
                            coin.Clusters = cluster;
                        }
                        else
                        {
                            ClustersByScriptPubKey.Add(coin.ScriptPubKey, coin.Clusters);
                        }
                        InvalidateSnapshot = true;
                    }
                }
            }
            return(added);
        }
Exemplo n.º 2
0
        public bool TryAdd(SmartCoin coin)
        {
            var added = false;

            lock (Lock)
            {
                if (!SpentCoins.Contains(coin))
                {
                    added = Coins.Add(coin);
                    if (added)
                    {
                        if (ClustersByScriptPubKey.TryGetValue(coin.ScriptPubKey, out var cluster))
                        {
                            coin.Cluster = cluster;
                        }
                        else
                        {
                            ClustersByScriptPubKey.Add(coin.ScriptPubKey, coin.Cluster);
                        }

                        foreach (var spentOutPoint in coin.SpentOutputs)
                        {
                            var outPoint   = spentOutPoint;
                            var newCoinSet = new HashSet <SmartCoin> {
                                coin
                            };

                            // If we don't succeed to add a new entry to the dictionary.
                            if (!CoinsByOutPoint.TryAdd(outPoint, newCoinSet))
                            {
                                var previousCoinTxId = CoinsByOutPoint[outPoint].First().TransactionId;

                                // Then check if we're in the same transaction as the previous coins in the dictionary are.
                                if (coin.TransactionId == previousCoinTxId)
                                {
                                    // If we are in the same transaction, then just add it to value set.
                                    CoinsByOutPoint[outPoint].Add(coin);
                                }
                                else
                                {
                                    // If we aren't in the same transaction, then it's a conflict, so replace the old set with the new one.
                                    CoinsByOutPoint[outPoint] = newCoinSet;
                                }
                            }
                        }
                        InvalidateSnapshot = true;
                    }
                }
            }
            return(added);
        }
Exemplo n.º 3
0
 public void Spend(SmartCoin spentCoin)
 {
     lock (Lock)
     {
         if (Coins.Remove(spentCoin))
         {
             InvalidateSnapshot = true;
             SpentCoins.Add(spentCoin);
             var createdCoins = CreatedByNoLock(spentCoin.SpenderTransactionId);
             foreach (var newCoin in createdCoins)
             {
                 if (newCoin.AnonymitySet < PrivacyLevelThreshold)
                 {
                     spentCoin.Clusters.Merge(newCoin.Clusters);
                     newCoin.Clusters = spentCoin.Clusters;
                     ClustersByScriptPubKey.AddOrReplace(newCoin.ScriptPubKey, newCoin.Clusters);
                 }
             }
         }
     }
 }