package org.mastercoin.consensus

import com.msgilligan.bitcoin.rpc.BitcoinClient
import groovy.json.JsonSlurper

/**
 * Utility class that uses a reference (currently Blockchain.info API) and waits for an BitcoinClient
 * to reach the current blockheight.
 */
class WaitForBlockchainSync {

    static def waitForSync(BitcoinClient client) {
        //
        // Get in sync with the Blockchain
        //
        def curHeight = 0
        def newHeight = getReferenceBlockHeight()
        println "Blockchain.info current height: ${newHeight}"
        while ( newHeight > curHeight ) {
            curHeight = newHeight
            Boolean upToDate = client.waitForSync(curHeight, 60*60)
            newHeight = getReferenceBlockHeight()
            println "Current reference block height: ${newHeight}"
        }

    }

    /**
     * Use an external service to get the current block height
     * Currently uses blockchain.info
     */
    static def getReferenceBlockHeight() {
        // Use Blockchain.info to get the current block height
        def height = new JsonSlurper().parse(new URL("http://blockchain.info/latestblock")).height
        return height
    }

}

package org.mastercoin.consensus

import com.msgilligan.bitcoin.rpc.BitcoinClient
import groovy.json.JsonSlurper

/**
 * Utility class that uses a reference (currently Blockchain.info API) and waits for an BitcoinClient
 * to reach the current blockheight.
 */
class WaitForBlockchainSync {

    static def waitForSync(BitcoinClient client) {
        //
        // Get in sync with the Blockchain
        //
        def curHeight = 0
        def newHeight = getReferenceBlockHeight()
        println "Blockchain.info current height: ${newHeight}"
        while ( newHeight > curHeight ) {
            curHeight = newHeight
            Boolean upToDate = client.waitForSync(curHeight, 60*60)
            newHeight = getReferenceBlockHeight()
            println "Current reference block height: ${newHeight}"
        }

    }

    /**
     * Use an external service to get the current block height
     * Currently uses blockchain.info
     */
    static def getReferenceBlockHeight() {
        // Use Blockchain.info to get the current block height
        def height = new JsonSlurper().parse(new URL("https://blockchain.info/latestblock")).height
        return height
    }

}

Use https to talk to blockchain.info
Use https to talk to blockchain.info

They started redirecting and that broke the tests.

