Esempio n. 1
0
    /// <summary>
    /// Calculates how many inputs are desirable to be registered
    /// based on roughly the total number of coins in a wallet.
    /// Note: random biasing is applied.
    /// </summary>
    /// <returns>Desired input count.</returns>
    private static int GetInputTarget(int nonPrivateCount, int privateCount, WasabiRandom rnd)
    {
        var utxoCount                 = nonPrivateCount + privateCount;
        var utxoCountTarget           = 21;
        var minPrivateUtxoCountTarget = 10;
        var maxUtxoCountTarget        = 100;

        int targetInputCount;

        if (utxoCount < utxoCountTarget)
        {
            targetInputCount = 1;
        }
        else if (utxoCount > maxUtxoCountTarget ||
                 privateCount > utxoCountTarget ||
                 (privateCount > nonPrivateCount && privateCount >= minPrivateUtxoCountTarget))
        {
            targetInputCount = MaxInputsRegistrableByWallet;
        }
        else
        {
            var min = 2;
            var max = MaxInputsRegistrableByWallet - 1;

            var percent = (double)(utxoCount - utxoCountTarget) / (maxUtxoCountTarget - utxoCountTarget);
            targetInputCount = (int)Math.Round((max - min) * percent + min);
        }

        var distance = new Dictionary <int, int>();

        for (int i = 1; i <= MaxInputsRegistrableByWallet; i++)
        {
            distance.TryAdd(i, Math.Abs(i - targetInputCount));
        }

        foreach (var best in distance.OrderBy(x => x.Value))
        {
            if (rnd.GetInt(0, 10) < 5)
            {
                return(best.Key);
            }
        }

        return(targetInputCount);
    }