Is there an easy way to iterate forward starting from the genesis block?

42 views
Skip to first unread message

Sean Shubin

unread,
Nov 21, 2021, 4:34:13 AM11/21/21
to bitcoinj

Just started looking at bitcoinj.  I am familiar with the bitcoin data structure, and want to write applications that analyze the blockchain data, so I am looking for a library that lets me interact with the bitcoin protocol directly.  Does bitcoinj have that capability or is it only a higher level library for managing wallets?  For my first prototype I intend to load the first 5000 blocks and compute for myself how many satoshi’s are at each address.  I have already found the getGenesisBlock method on the NetworkParameters class, but am having trouble finding out how to get the next block without creating a WalletAppKit and loading the previous 2,104,841 blocks off the test net from the BlockStore.  That seems like the wrong direction.  I am hoping someone already familiar with bitcoinj  can point me in the right direction regarding which classes I should be looking at first, without all the noise of capabilities I don’t need at the moment.


Andreas Schildbach

unread,
Nov 21, 2021, 4:36:18 AM11/21/21
to bitc...@googlegroups.com
There are a few examples and tools that operate with the protocol
directly, without any wallet.

Classes to look at are Peer and PeerGroup, and the entire Message hierarchy.
> --
> 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+u...@googlegroups.com
> <mailto:bitcoinj+u...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/bitcoinj/8a68259c-df2b-4e6f-956d-76929a61d5a9n%40googlegroups.com
> <https://groups.google.com/d/msgid/bitcoinj/8a68259c-df2b-4e6f-956d-76929a61d5a9n%40googlegroups.com?utm_medium=email&utm_source=footer>.

Sean Shubin

unread,
Nov 21, 2021, 11:38:48 AM11/21/21
to bitcoinj
That helps a lot.  Right now puzzling out how to get a peer added but at least I am looking at stuff that is relevant.  Thanks.

// current prototype in the Kotlin programming language
val networkParameters = TestNet3Params.get()
val fullStoreDepth = 10 // let's just start with the first 10 blocks
val blockStore = MemoryFullPrunedBlockStore(networkParameters, fullStoreDepth)
val chain = FullPrunedBlockChain(networkParameters, blockStore)
val peerGroup = PeerGroup(networkParameters, chain)
val dnsDiscovery = DnsDiscovery(networkParameters)
peerGroup.addPeerDiscovery(dnsDiscovery); // I expected this to add the peers automatically, must be missing something
peerGroup.start()
peerGroup.downloadBlockChain()  // looks like this just waits for a peer to show up
println(blockStore.chainHeadHeight)
println(peerGroup.connectedPeers.size)

Sean Shubin

unread,
Nov 21, 2021, 3:43:21 PM11/21/21
to bitcoinj
Took me a while but I finally figured out how to iterate the blocks from the genesis block.  Here is the prototype code (in Kotlin) in case anyone is interested.  Thanks again for the help.

val networkParameters = TestNet3Params.get()
val blockStore = MemoryBlockStore(networkParameters)
val chain = BlockChain(networkParameters, blockStore)

val peerGroup = PeerGroup(networkParameters, chain)
val blockLimit = 10
var blockOrder = 0
var done =false
fun displayBlock(block:Block){
  println("[$blockOrder] ${block.hash}")
}
peerGroup.addBlocksDownloadedEventListener {
peer, block, filteredBlock, blocksLeft ->
if(!done) {
  blockOrder++
  displayBlock(block)
  if(blockOrder >= blockLimit-1) done = true
  if(blocksLeft == 0) done = true

  }
}
val dnsDiscovery = DnsDiscovery(networkParameters)
peerGroup.addPeerDiscovery(dnsDiscovery);
peerGroup.start()
val peer = peerGroup.waitForPeers(1).get()[0]
displayBlock(networkParameters.genesisBlock)
peer.startBlockChainDownload()
while(!done){
  Thread.sleep(1)
}

On Sunday, November 21, 2021 at 1:36:18 AM UTC-8 Andreas Schildbach wrote:
Reply all
Reply to author
Forward
0 new messages