// Explicit calculation which is used to test the wallet constant
// We get the same virtual size due to rounding(weight/4) for both use_max_sig values
static size_t CalculateNestedKeyhashInputSize(bool use_max_sig)
{
    // Generate ephemeral valid pubkey
    CKey key;
    key.MakeNewKey(true);
    CPubKey pubkey = key.GetPubKey();

    // Generate pubkey hash
    uint160 key_hash(Hash160(pubkey.begin(), pubkey.end()));

    // Create inner-script to enter into keystore. Key hash can't be 0...
    CScript inner_script = CScript() << OP_0 << std::vector<unsigned char>(key_hash.begin(), key_hash.end());

    // Create outer P2SH script for the output
    uint160 script_id(Hash160(inner_script.begin(), inner_script.end()));
    CScript script_pubkey = CScript() << OP_HASH160 << std::vector<unsigned char>(script_id.begin(), script_id.end()) << OP_EQUAL;

    // Add inner-script to key store and key to watchonly
    CBasicKeyStore keystore;
    keystore.AddCScript(inner_script);
    keystore.AddKeyPubKey(key, pubkey);

    // Fill in dummy signatures for fee calculation.
    SignatureData sig_data;

    if (!ProduceSignature(keystore, use_max_sig ? DUMMY_MAXIMUM_SIGNATURE_CREATOR : DUMMY_SIGNATURE_CREATOR, script_pubkey, sig_data)) {
        // We're hand-feeding it correct arguments; shouldn't happen
        assert(false);
    }

    CTxIn tx_in;
    UpdateInput(tx_in, sig_data);
    return (size_t)GetVirtualTransactionInputSize(tx_in);
}
bool TestChainForComputingMediansSetup::GenerateRandomTransaction(CTransaction& txNew)
{
    CAmount amountToSend = 5000;
    std::vector<CTransaction> res;

    CKey key;
    key.MakeNewKey(true);
    CScript scriptPubKey = CScript() << ToByteVector(key.GetPubKey())
            << OP_CHECKSIG;

    CBasicKeyStore keystore;
    keystore.AddKeyPubKey(coinbaseKey, coinbaseKey.GetPubKey());

    CTransaction utxo = coinbaseTxns[0];
    coinbaseTxns.erase(coinbaseTxns.begin());

    txNew.nLockTime = chainActive.Height();
    txNew.vin.clear();
    txNew.vout.clear();

    for (int j = 0; j < nOutputs; ++j) {
        CTxOut txout(amountToSend, scriptPubKey);
        txNew.vout.push_back(txout);
    }

    //vin
    CTxIn vin = CTxIn(utxo.GetHash(), 0, CScript(),
            std::numeric_limits<unsigned int>::max() - 1);
    txNew.vin.push_back(vin);

    //previous tx's script pub key that we need to sign
    CScript& scriptSigRes = txNew.vin[0].scriptSig;
    CTransaction txNewConst(txNew);
    ProduceSignature(TransactionSignatureCreator(&keystore, &txNewConst, 0),
            utxo.vout[0].scriptPubKey, scriptSigRes);
    res.push_back(txNew);

    return true;
}