<commit_before>package com.msgilligan.bitcoinj.test

import com.msgilligan.bitcoinj.rpc.BitcoinClientDelegate
import com.msgilligan.bitcoinj.rpc.Loggable
import com.msgilligan.bitcoinj.rpc.Outpoint
import com.msgilligan.bitcoinj.rpc.UnspentOutput
import com.msgilligan.bitcoinj.rpc.conversion.BitcoinMath
import org.bitcoinj.core.Address
import org.bitcoinj.core.Coin
import org.bitcoinj.core.ECKey
import org.bitcoinj.core.NetworkParameters
import org.bitcoinj.core.Sha256Hash
import org.bitcoinj.core.Transaction
import org.bitcoinj.core.TransactionOutPoint
import org.bitcoinj.core.TransactionOutput
import org.bitcoinj.params.RegTestParams

/**
 * Test support functions intended to be mixed-in to Spock test specs
 */
trait BTCTestSupport implements BitcoinClientDelegate, Loggable {
    // TODO: set, or get and verify default values of the client
    private final NetworkParameters netParams = RegTestParams.get()
    private static final BigDecimal satoshisPerBTCDecimal = new BigDecimal(Coin.COIN.value);
    private static final BigDecimal bdSatoshiPerCoin = new BigDecimal(Coin.COIN.longValue());
    final BigDecimal stdTxFee = 0.00010000
    final BigDecimal stdRelayTxFee = 0.00001000
    final Integer defaultMaxConf = 9999999
    final long stdTxFeeSatoshis = btcToSatoshi(stdTxFee)

    @Deprecated
    Sha256Hash requestBitcoin(Address toAddress, BigDecimal requestedBTC) {
        return requestBitcoin(toAddress,btcToCoin(requestedBTC))
    }

    /**
     * Generate blocks and fund an address with requested amount of BTC
     *
     * TODO: Improve performance. Can we mine multiple blocks with a single RPC?
     *
     * @param toAddress Address to fund with BTC
     * @param requestedBTC Amount of BTC to "mine" and send
     * @return
     */
    Sha256Hash requestBitcoin(Address toAddress, Coin requestedAmount) {
        long requestedSatoshi = requestedAmount.longValue()
        log.info "requestBitcoin requesting {} satoshi ({} BTC)", requestedSatoshi, satoshiToBtc(requestedSatoshi)
        long amountGatheredSoFar = 0
        def inputs = new ArrayList<Outpoint>()

        // Newly mined coins need to mature to be spendable
        def minCoinAge = 100

        if (blockCount < minCoinAge) {
            generateBlocks(minCoinAge - blockCount)
        }

        while (amountGatheredSoFar < requestedSatoshi) {
            generateBlock()
            def blockIndex = blockCount - minCoinAge
            def block = client.getBlock(blockIndex)
            def blockTxs = block.tx as List<String>
            def coinbaseTx = new Sha256Hash(blockTxs.get(0))
            def txout = client.getTxOut(coinbaseTx, 0)

            // txout is empty, if output was already spent
            if (txout && txout.containsKey("value")) {
                log.debug "txout = {}", txout
                def amountBTCbd = BigDecimal.valueOf(txout.value)
                long amountSatoshi = btcToSatoshi(amountBTCbd)
                amountGatheredSoFar += amountSatoshi
                inputs << new Outpoint(coinbaseTx, 0)
            }
            log.debug "amountGatheredSoFar = {}", satoshiToBtc(amountGatheredSoFar)
        }

        // Don't care about change, we mine it anyway
        def outputs = new HashMap<Address, BigDecimal>()<commit_msg>log.info -> log.debug in one place<commit_after>package com.msgilligan.bitcoinj.test

import com.msgilligan.bitcoinj.rpc.BitcoinClientDelegate
import com.msgilligan.bitcoinj.rpc.Loggable
import com.msgilligan.bitcoinj.rpc.Outpoint
import com.msgilligan.bitcoinj.rpc.UnspentOutput
import com.msgilligan.bitcoinj.rpc.conversion.BitcoinMath
import org.bitcoinj.core.Address
import org.bitcoinj.core.Coin
import org.bitcoinj.core.ECKey
import org.bitcoinj.core.NetworkParameters
import org.bitcoinj.core.Sha256Hash
import org.bitcoinj.core.Transaction
import org.bitcoinj.core.TransactionOutPoint
import org.bitcoinj.core.TransactionOutput
import org.bitcoinj.params.RegTestParams

/**
 * Test support functions intended to be mixed-in to Spock test specs
 * TODO: Factor out some methods to Java reusable class.
 */
trait BTCTestSupport implements BitcoinClientDelegate, Loggable {
    // TODO: set, or get and verify default values of the client
    private final NetworkParameters netParams = RegTestParams.get()
    private static final BigDecimal satoshisPerBTCDecimal = new BigDecimal(Coin.COIN.value);
    private static final BigDecimal bdSatoshiPerCoin = new BigDecimal(Coin.COIN.longValue());
    final BigDecimal stdTxFee = 0.00010000
    final BigDecimal stdRelayTxFee = 0.00001000
    final Integer defaultMaxConf = 9999999
    final long stdTxFeeSatoshis = btcToSatoshi(stdTxFee)

    @Deprecated
    Sha256Hash requestBitcoin(Address toAddress, BigDecimal requestedBTC) {
        return requestBitcoin(toAddress,btcToCoin(requestedBTC))
    }

    /**
     * Generate blocks and fund an address with requested amount of BTC
     *
     * TODO: Improve performance. Can we mine multiple blocks with a single RPC?
     *
     * @param toAddress Address to fund with BTC
     * @param requestedBTC Amount of BTC to "mine" and send
     * @return
     */
    Sha256Hash requestBitcoin(Address toAddress, Coin requestedAmount) {
        long requestedSatoshi = requestedAmount.longValue()
        log.debug "requestBitcoin requesting {} satoshi ({} BTC)", requestedSatoshi, satoshiToBtc(requestedSatoshi)
        long amountGatheredSoFar = 0
        def inputs = new ArrayList<Outpoint>()

        // Newly mined coins need to mature to be spendable
        def minCoinAge = 100

        if (blockCount < minCoinAge) {
            generateBlocks(minCoinAge - blockCount)
        }

        while (amountGatheredSoFar < requestedSatoshi) {
            generateBlock()
            def blockIndex = blockCount - minCoinAge
            def block = client.getBlock(blockIndex)
            def blockTxs = block.tx as List<String>
            def coinbaseTx = new Sha256Hash(blockTxs.get(0))
            def txout = client.getTxOut(coinbaseTx, 0)

            // txout is empty, if output was already spent
            if (txout && txout.containsKey("value")) {
                log.debug "txout = {}", txout
                def amountBTCbd = BigDecimal.valueOf(txout.value)
                long amountSatoshi = btcToSatoshi(amountBTCbd)
                amountGatheredSoFar += amountSatoshi
                inputs << new Outpoint(coinbaseTx, 0)
            }
            log.debug "amountGatheredSoFar = {}", satoshiToBtc(amountGatheredSoFar)
        }

        // Don't care about change, we mine it anyway
        def outputs = new HashMap<Address, BigDecimal>()
{'fraction_non_alphanumeric': 0.07479307750188112, 'fraction_numerical': 0.013243039879608728, 'mean_word_length': 3.0698101653398653, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3637620', 'n_tokens_mistral': 2044, 'n_tokens_neox': 1891, 'n_words': 502}