I’m trying to understand the way Bitcoin stores block data – among other things I want to run some statistics on the block chain / transaction history and check just how anonymous Bitcoin really is. So I went to the source to see how Bitcoin reads/writes block data to file.
[…]
Now – and I apologize if I’m reading this wrong, this is a little more advanced C/C++ code than I’m used to – as I understand it, the FLATDATA call interprets the raw bytes of a CBlock object as an array (stream??) of characters. The CBlock::WriteToDisk method writes the constant 4-byte message header (0xf9, 0xbe, 0xb4, 0xd9), the size of the CBlock object in bytes, and then the FLATDATA of the CBlock it’s writing to disk – which is just the raw bytes of the CBlock object. So after the header, the data written to file is byte-for-byte the same as the CBlock object represented in memory. Also, if I’m reading it correctly, CBlock::ReadFromFile copies those bytes directly into the space allocated for a CBlock object in memory to re-create the block. Is this correct?
Related question – I am under the impression that the exact way an instance of a C++ class is represented internally not guaranteed under standards; compiling a program with different compilers or different optimization flags can change the order in which member variables are stored in memory, and some debug mode compilers even add a few bytes between member variables to make memory inspection easier. I’m not positive about this, it’s just something I’ve picked up and never seriously questioned.
FLATDATA was a workaround to serialize a fixed field length array. There was a cleaner way to make it understand how to serialize arrays directly, but MSVC6 couldn’t do it and I wanted to keep compatibility with MSVC6 at that time. We don’t support MSVC6 anymore because we use something in Boost that doesn’t. We lost support for it after 0.2.0. Maybe someday I’ll swap in the clean way that just knows how to serialize fixed length arrays without wrapping them in FLATDATA.
63,657 total views, 19 views today
https://bitcointalk.org/index.php?topic=555.msg5450#msg5450