public void CanAdd()
    {
        var cjIdStore = new CoinJoinIdStore();

        cjIdStore.TryAdd(uint256.One);

        Assert.True(cjIdStore.Contains(uint256.One));

        cjIdStore.TryAdd(uint256.One);
        cjIdStore.TryAdd(uint256.One);

        Assert.Single(cjIdStore.GetCoinJoinIds);
    }
Exemple #2
0
    private async Task <InputRegistrationResponse> RegisterInputCoreAsync(InputRegistrationRequest request, CancellationToken cancellationToken)
    {
        var coin = await OutpointToCoinAsync(request, cancellationToken).ConfigureAwait(false);

        using (await AsyncLock.LockAsync(cancellationToken).ConfigureAwait(false))
        {
            var round = GetRound(request.RoundId);

            var registeredCoins = Rounds.Where(x => !(x.Phase == Phase.Ended && !x.WasTransactionBroadcast))
                                  .SelectMany(r => r.Alices.Select(a => a.Coin));

            if (registeredCoins.Any(x => x.Outpoint == coin.Outpoint))
            {
                throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.AliceAlreadyRegistered);
            }

            if (round.IsInputRegistrationEnded(Config.MaxInputCountByRound))
            {
                throw new WrongPhaseException(round, Phase.InputRegistration);
            }

            if (round is BlameRound blameRound && !blameRound.BlameWhitelist.Contains(coin.Outpoint))
            {
                throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.InputNotWhitelisted);
            }

            // Compute but don't commit updated coinjoin to round state, it will
            // be re-calculated on input confirmation. This is computed in here
            // for validation purposes.
            _ = round.Assert <ConstructionState>().AddInput(coin);

            var coinJoinInputCommitmentData = new CoinJoinInputCommitmentData("CoinJoinCoordinatorIdentifier", round.Id);
            if (!OwnershipProof.VerifyCoinJoinInputProof(request.OwnershipProof, coin.TxOut.ScriptPubKey, coinJoinInputCommitmentData))
            {
                throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.WrongOwnershipProof);
            }

            // Generate a new GUID with the secure random source, to be sure
            // that it is not guessable (Guid.NewGuid() documentation does
            // not say anything about GUID version or randomness source,
            // only that the probability of duplicates is very low).
            var id = new Guid(SecureRandom.Instance.GetBytes(16));

            var isPayingZeroCoordinationFee = CoinJoinIdStore.Contains(coin.Outpoint.Hash);

            if (!isPayingZeroCoordinationFee)
            {
                // If the coin comes from a tx that all of the tx inputs are coming from a CJ (1 hop - no pay).
                Transaction tx = await Rpc.GetRawTransactionAsync(coin.Outpoint.Hash, true, cancellationToken).ConfigureAwait(false);

                if (tx.Inputs.All(input => CoinJoinIdStore.Contains(input.PrevOut.Hash)))
                {
                    isPayingZeroCoordinationFee = true;
                }
            }

            var alice = new Alice(coin, request.OwnershipProof, round, id, isPayingZeroCoordinationFee);

            if (alice.CalculateRemainingAmountCredentials(round.Parameters.MiningFeeRate, round.Parameters.CoordinationFeeRate) <= Money.Zero)
            {
                throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.UneconomicalInput);
            }

            if (alice.TotalInputAmount < round.Parameters.MinAmountCredentialValue)
            {
                throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.NotEnoughFunds);
            }
            if (alice.TotalInputAmount > round.Parameters.MaxAmountCredentialValue)
            {
                throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.TooMuchFunds);
            }

            if (alice.TotalInputVsize > round.Parameters.MaxVsizeAllocationPerAlice)
            {
                throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.TooMuchVsize);
            }

            var amountCredentialTask = round.AmountCredentialIssuer.HandleRequestAsync(request.ZeroAmountCredentialRequests, cancellationToken);
            var vsizeCredentialTask  = round.VsizeCredentialIssuer.HandleRequestAsync(request.ZeroVsizeCredentialRequests, cancellationToken);

            if (round.RemainingInputVsizeAllocation < round.Parameters.MaxVsizeAllocationPerAlice)
            {
                throw new WabiSabiProtocolException(WabiSabiProtocolErrorCode.VsizeQuotaExceeded);
            }

            var commitAmountCredentialResponse = await amountCredentialTask.ConfigureAwait(false);

            var commitVsizeCredentialResponse = await vsizeCredentialTask.ConfigureAwait(false);

            alice.SetDeadlineRelativeTo(round.ConnectionConfirmationTimeFrame.Duration);
            round.Alices.Add(alice);

            return(new(alice.Id,
                       commitAmountCredentialResponse,
                       commitVsizeCredentialResponse,
                       alice.IsPayingZeroCoordinationFee));
        }
    }