Call function for block n on blockchain

0

I've been going through main.cpp in order to roughly find out which function calls the best block and I found out depending on situation there are several similar-sounding function names and code extracts that I suspect of calling the best block. Here's my list:

  • CBlockIndex *pindexBestKnownBlock
  • uint256 CCoinsView::GetBestBlock () const
  • CBlockIndex* pindexBestHeader
  • CBlockIndex* pindexBestForkTip
  • pindexBestInvalid
  • // move best block pointer to prevout block
    view.SetBestBlock(pindex->pprev->GetBlockHash());
  • chainActive.Tip()->GetBlockHash().ToString()

Now my question: Which one of the list items above can I use and modify in a way that calls a block that is n blocks deeper than the best block on the same blockchain?

Aliakbar Ahmadi

Posted 2015-05-19T17:15:01.367

Reputation: 1 335

Answers

2

The chainActive variable stores the best known block chain. But this is essentially just the chain of block headers, not the actual blocks themselves. To load the actual block from disk, use the ReadBlockFromDisk() function. But, be aware that this is an expensive function to use, it does not return immediately.

chainActive[0] or chainActive.Genesis() [1]

Gives the genesis block header.

chainActive[chainActive.Height()] or chainActive.Tip() [2]

Gives the header for the most recent block in the active chain.

chainActive[chainActive.Height()-10]

Gives the block header for the block with 10 blocks built on top of it.

Use any of these CBlockIndex pointers as below to get the block.

CBlock block;
if (ReadBlockFromDisk(block, pindex)) {
    // do something
}

morsecoder

Posted 2015-05-19T17:15:01.367

Reputation: 12 624

Thanks! Does ReadBlockFromDisc() access blk?????.dat? Is there any software or app for opening and reading blk?????.dat?Aliakbar Ahmadi 2015-05-20T16:00:44.933

1I believe it does access the blk___.dat files. Those files just contain raw concatenated block bytes, so you would probably need a blockchain parser to access those. What exactly are you trying to do? I might be able to point you at a good tool for the job.morsecoder 2015-05-20T16:15:29.367

I'm going to simulate and analyse outcome of a "block mining race". I want to make a rebuild of bitcoin-0.10.0 with modified code that prescribes selfish mining algorithm. For this part I got the hint that bitcoin code must work with a pointer (speaking on processor level) that is shifted back and forth according to the state of a node. And here I want to implement the selfish behaviour: modified code shall "point to blocks" according to the algorithm of the attack. With the idea of a pointer I hope to be able to "hide" blocks - this would mean a backshifted pointer to a publicly known block.Aliakbar Ahmadi 2015-05-20T16:34:25.800

For the simulation, for example I'm planning to run 1 selfish node (modified node) alongside with 4 other honest nodes (original code), fork the blockchain and start mining on both branches.Aliakbar Ahmadi 2015-05-20T16:36:26.273

In that case, what you probably need to do is make a new set of parameters in one of the chainparams files, and program your node to selfish mine when it is started with some flag. This doesn't require digging into the blk___.dat files at all, mainly some modification of what/when things are shared in main.cpp. Then start up the various nodes, connect them to each other, and have them all mine. If the selfish one gains more coins in the long run, then the strategy is working.morsecoder 2015-05-20T16:47:13.233

Thanks a lot! Can you maybe give me a good source where I can read about what each chainparams file does or do you recommend to go through the files and just try to understand? Unfortunately, I'm very new to C++ but I'm eager to learn new stuff. Is there a good approach for getting a rough idea what those chainparams files do? (I hope I'm speaking of same files as you: chainparams.cpp, chainparamsbase.cpp, chainparamsseeds.h etc.)Aliakbar Ahmadi 2015-05-20T17:08:25.507

1I think the code is pretty much the best documentation at this point. Just read main.cpp, chainparams.cpp, and chainparamsbase.cpp. Look up code elements that you're not sure about to see their documentation (e.g. std::vector or std::map, or anything related to boost). Best of luck!morsecoder 2015-05-20T18:39:36.970

Thanks for the review! Could you please explain why chainActive[] works with genesis block but not with any other block? Is it because a block can have multiple valid "twins"?Aliakbar Ahmadi 2015-06-05T20:02:27.317

@AliakbarAhmadi The operator chainActive[] may be confusing to people without intimate C++ knowledege, so I left that part out of the suggested edits. But in general, 'twins', as you say, are not an issue because the chainActive only has the headers of the main chain in it.morsecoder 2015-06-05T22:10:23.930