Looks like peer discovery doesn't work. I would for now use a plain
DnsDiscovery. Let's see if it discovers peers.

Also perhaps try on the bitcoinj release-0.15 branch. It will get tagged
for release soon anyway.


On 20/02/2019 22.21, Ben Moore wrote:
> I am currently trying to build an Android wallet using the bitcoinJ
> library but I am having issue with the library downloading the
> blockchain in order to synchronise correctly. I have put the blockchain
> download into a seperate service in the android application in order to
> make sure it doesn't block the execution of the application. Whiich I
> saw here
> https://github.com/bitcoin-wallet/bitcoin-wallet/blob/c306db6cf39127ccaa6cfd38ef28a2a7bf7aaf1d/wallet/src/de/schildbach/wallet/service/BlockchainService.java
> <https://github.com/bitcoin-wallet/bitcoin-wallet/blob/c306db6cf39127ccaa6cfd38ef28a2a7bf7aaf1d/wallet/src/de/schildbach/wallet/service/BlockchainService.java>
> 
> My service starts but it repeatedly just gives me the following log entry
> 
> |
> 2019-02-2021:15:57.12815495-15656/com.bitcoinwallet
> W/System.err:[PeerGroupThread]INFO
> org.bitcoinj.core.PeerGroup-Peerdiscovery took 9.667ms andreturned 0items
> 2019-02-2021:15:57.13015495-15656/com.bitcoinwallet
> W/System.err:[PeerGroupThread]INFO
> org.bitcoinj.core.PeerGroup-Peerdiscovery didn't provide us any more
> peers, will try again in 10017ms.
> |
> 
> And the onChanDownloadStarted function never gets called, I've tried
> everything I know so I really cannot understand why it would not be
> working. The service definitely starts because I see the "Blockchain
> Download service started" in the logs but I don't see any of the other
> logs. I'm really stumped by this problem so any help or suggestions
> would be really appreciated, thanks!
> 
> 
> Below you will find the code for my BlockchainDownload service
> 
> 
> |
> packagecom.bitcoinwallet.services
> 
> importandroid.arch.lifecycle.LifecycleService
> importandroid.content.SharedPreferences
> importandroid.os.Handler
> importandroid.os.PowerManager
> importandroid.text.format.DateUtils
> importandroid.util.Log
> importcom.bitcoinwallet.utilities.Globals
> importorg.bitcoinj.core.Block
> importorg.bitcoinj.core.FilteredBlock
> importorg.bitcoinj.core.Peer
> importorg.bitcoinj.core.listeners.AbstractPeerDataEventListener
> importorg.bitcoinj.net.discovery.MultiplexingDiscovery
> importorg.bitcoinj.net.discovery.PeerDiscovery
> importjava.net.InetSocketAddress
> importjava.util.*
> importjava.util.concurrent.TimeUnit
> importjava.util.concurrent.atomic.AtomicLong
> 
> classBlockchainDownloadService:LifecycleService(){
>     privatelateinit varwakeLock:PowerManager.WakeLock
>     privatevardelayHandler =Handler()
>     privateval PREFS_KEY_TRUSTED_PEER ="trusted_peer"
>     privatelateinit varprefs:SharedPreferences
>     privateval maxConnectedPeers =4
> 
>     privatevarblockchainDownloadListener
> =object:AbstractPeerDataEventListener(){
>         privateval lastMessageTime =AtomicLong(0)
> 
>         privateval runnable =object:Runnable{
>             overridefun run(){
>                 lastMessageTime.set(System.currentTimeMillis())
>             }
>         }
> 
>         overridefun onBlocksDownloaded(
>             peer:Peer?,
>             block:Block?,
>             filteredBlock:FilteredBlock?,
>             blocksLeft:Int
>         ){
>             super.onBlocksDownloaded(peer,block,filteredBlock,blocksLeft)
> 
>             delayHandler.removeCallbacksAndMessages(null)
> 
>             varnow =System.currentTimeMillis()
> 
>             Log.d(Globals.LOG_TAG,"Block downloaded, only
> "+blocksLeft.toString()+" left!")
> 
>             if(now -lastMessageTime.get()>DateUtils.SECOND_IN_MILLIS){
>                 delayHandler.post(runnable)
>             }else{
>                
> delayHandler.postDelayed(runnable,DateUtils.SECOND_IN_MILLIS)
>             }
>         }
> 
>         overridefun onChainDownloadStarted(peer:Peer?,blocksLeft:Int){
>             super.onChainDownloadStarted(peer,blocksLeft)
>             Log.d(Globals.LOG_TAG,"Blockchain Downloadt started")
>         }
>     }
> 
>     overridefun onCreate(){
>         super.onCreate()
>         val powerManager
> =getSystemService(PowerManager::class.java)asPowerManager
>         // TODO: Change this string to a constant also
>         wakeLock
> =powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK,"BLOCKCHAIN DL
> SERVICE:")
>         wakeLock.acquire()
>         Log.d(Globals.LOG_TAG,"Blockchain Download service started")
> 
>         Globals.peerGroup?.addWallet(Globals.wallet)
>         Globals.peerGroup?.setDownloadTxDependencies(0)
>         Globals.peerGroup?.setConnectTimeoutMillis(15*1000)// 15 seconds
>        
> Globals.peerGroup?.setPeerDiscoveryTimeoutMillis(10*DateUtils.SECOND_IN_MILLIS)//
> 10 seconds
>         // TODO: Change these to constants
>         Globals.peerGroup?.setUserAgent("BTC Wallet","0.1")
>         Globals.peerGroup?.maxConnections =maxConnectedPeers
> 
>         Globals.peerGroup?.addPeerDiscovery(object:PeerDiscovery{
>             privateval normalPeerDiscovery
> =MultiplexingDiscovery.forServices(Globals.networkParams,0)
>             overridefun getPeers(
>                 services:Long,
>                 timeoutValue:Long,
>                 timeoutUnit:TimeUnit?
>             ):Array<InetSocketAddress>{
>                 val peers =LinkedList<InetSocketAddress>()
> 
>                 val addr
> =InetSocketAddress(PREFS_KEY_TRUSTED_PEER,Globals.networkParams.port)
> 
>                 if(addr.address !=null){
>                     peers.add(addr)
>                 }
> 
>                 while(peers.size >=maxConnectedPeers){
>                     peers.removeAt(peers.size -1)
>                 }
> 
>                 returnpeers.toTypedArray()
>             }
> 
>             overridefun shutdown(){
>                 TODO("not implemented")//To change body of created
> functions use File | Settings | File Templates.
>                 normalPeerDiscovery.shutdown()
>             }
>         })
> 
> 
>         Globals.peerGroup?.startAsync()
>        
> Globals.peerGroup?.startBlockChainDownload(blockchainDownloadListener)
>     }
> 
>     overridefun onDestroy(){
>         super.onDestroy()
>         delayHandler.removeCallbacksAndMessages(null)
>         if(wakeLock.isHeld){
>             wakeLock.release()
>         }
> 
>         if(Globals.peerGroup?.isRunning!!){
>             Globals.peerGroup?.stopAsync()
>         }
>     }
> }
> |
> 
> 
> Here is my Globals class which is used a lot
> 
> |
> classGlobals{
>     companion object{
>         val LOG_TAG ="BTC WALLET:"
>         val WALLET_NAME ="wallet_file.dat"
>         val BLOCK_STORE_NAME ="block_store_file.dat"
>         val IS_PRODUCTION =false
>         //var networkParams = if (IS_PRODUCTION)
> NetworkParameters.fromID(NetworkParameters.ID_MAINNET)
>         //else NetworkParameters.fromID(NetworkParameters.ID_TESTNET)
>         val networkParams:TestNet3Params
>             get()=TestNet3Params.get()
> 
>         varwallet :Wallet?=null
>         varwalletFile:File?=null
>         varblockStore:SPVBlockStore?=null
>         varblockChain:BlockChain?=null
>         varpeerGroup:PeerGroup?=null
>     }
> }
> |
> 
> Also here is where everything in Globals is initialised
> 
> |
> 
> 
> classBitcoinUtilities{
>     companion object{
>         fun initialiseWallet(walletFile:File,blockStoreFile:File){
>             Globals.walletFile =walletFile
>             Globals.wallet =Wallet(Globals.networkParams)
>             Globals.blockStore
> =SPVBlockStore(Globals.networkParams,blockStoreFile)
>             Globals.blockChain =BlockChain(
>                 Globals.networkParams,
>                 Globals.wallet,Globals.blockStore
>             )
>             Globals.peerGroup
> =PeerGroup(Globals.networkParams,Globals.blockChain)
>             Globals.wallet!!.saveToFile(walletFile)
>         }
> }
> |
> 
> 
> -- 
> You received this message because you are subscribed to the Google
> Groups "bitcoinj" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to bitcoinj+unsubscr...@googlegroups.com
> <mailto:bitcoinj+unsubscr...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


-- 
You received this message because you are subscribed to the Google Groups 
"bitcoinj" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to bitcoinj+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to