Title: Is Blockchain an array of Blocks? Post by: CristianOff on January 08, 2019, 08:41:20 PM For few years I had the impression that the Blockchain is nothing but
an array of a defined data structure, the so called "Blocks". This Stack Overflow (https://stackoverflow.com/questions/54084583/are-all-blockchain-array-implementations-incorrect/54085008#54085008) that I found today has made me ask many questions. I am not proficient in reading C++ so I would like to ask here people who can read and understand Bitcoin's official implementation on Github (https://github.com/bitcoin/bitcoin). Alternatively I will learn more about C++ however this would take longer than actually asking here and hoping to get an answer :) My understanding of how Blockchains are implemented is the following: You create a class Block such as: Code: class Block { Then we have the Blockchain which apparently, Quote is a toy implementation to initiate a chain as an array . Therefore this implementation is wrong:Code: class Blockchain { Obviously, when creating a new Block I would expect to push it into our chain array but since there is no array, then how Bitcoin does it? Can someone clarify it? I am extremely confused in this moment and I tried to understand it the entire last week. Title: Re: Is Blockchain an array of Blocks? Post by: hatshepsut93 on January 08, 2019, 09:26:20 PM Code: class Blockchain { Obviously, when creating a new Block I would expect to push it into our chain array but since there is no array, then how Bitcoin does it? Can someone clarify it? I am extremely confused in this moment and I tried to understand it the entire last week. In this example class Blockchain has a property "chain" which is indeed an array of Blocks. It is initialized as an Array literal with one element equal to the return value of GenesisBlock() function (a hard-coded Block). Keep in mind that the links in that stackoverflow question point to articles and repositories that show oversimplified code for educational purposes. They try to teach people to understand how blockchain works, not how to build your own serious protocol. The actual Bitcoin code is much more complicated and uses different data structures for maximum performance. Title: Re: Is Blockchain an array of Blocks? Post by: KingZee on January 08, 2019, 09:38:26 PM I used my googling skills again. Each block is stored in a single file. If you want more information on that this is a great extensive explanation on what/how that data is stored :
https://en.bitcoin.it/wiki/Bitcoin_Core_0.11_(ch_2):_Data_Storage I skimmed through this and it explains a ton, but it still misses just a slight part about how this is linked to the codebase, what's the data type of the blockchain and if it follows an OOP structure like you're expecting. I understand how they'd never be arrays though, the blockchain is never loaded in full in memory. That's probably the number one reason why C++ was used, for its extensive memory management capabilities. You should scroll down that link and check the last 2 chapters for more information about the implementation, I'm not sure if it'll be enough for what you want to understand, you can always wait or ask in the bitcoin.stackexchange.com. Title: Re: Is Blockchain an array of Blocks? Post by: CristianOff on January 08, 2019, 09:48:37 PM In this example class Blockchain has a property "chain" which is indeed an array of Blocks. It is initialized as an Array literal with one element equal to the return value of GenesisBlock() function (a hard-coded Block). Keep in mind that the links in that stackoverflow question point to articles and repositories that show oversimplified code for educational purposes. They try to teach people to understand how blockchain works, not how to build your own serious protocol. The actual Bitcoin code is much more complicated and uses different data structures for maximum performance. Thanks! Because of the oversimplified code for educational purposes, I assumed that blockchains would work this way. Then this educational purposes are not truly educational, but more like a poor copy paste. Thanks a lot for clarifying! I used my googling skills again. Each block is stored in a single file. If you want more information on that this is a great extensive explanation on what/how that data is stored : https://en.bitcoin.it/wiki/Bitcoin_Core_0.11_(ch_2):_Data_Storage I skimmed through this and it explains a ton, but it still misses just a slight part about how this is linked to the codebase, what's the data type of the blockchain and if it follows an OOP structure like you're expecting. I understand how they'd never be arrays though, the blockchain is never loaded in full in memory. That's probably the number one reason why C++ was used, for its extensive memory management capabilities. You should scroll down that link and check the last 2 chapters for more information about the implementation, I'm not sure if it'll be enough for what you want to understand, you can always wait or ask in the bitcoin.stackexchange.com. You seem to be an overqualified Google researcher because I researched for so much time and I couldn't find this. Maybe I am not typing the correct words in Google. Thanks for sharing it. It makes more sense now. Therefore, the new target is to store these blocks in a local database on each node. Title: Re: Is Blockchain an array of Blocks? Post by: KingZee on January 08, 2019, 10:05:03 PM You seem to be an overqualified Google researcher No joke :D I get told that so many times I'm starting to believe I might actually have something figured out that most people don't. Glad you found what you needed, it honestly looks like a great read if you have the time. Title: Re: Is Blockchain an array of Blocks? Post by: darosior on January 09, 2019, 01:51:43 PM I used my googling skills again. Each block is stored in a single file. If you want more information on that this is a great extensive explanation on what/how that data is stored : Hi,https://en.bitcoin.it/wiki/Bitcoin_Core_0.11_(ch_2):_Data_Storage This is wrong, blocks are stored in blk****.dat and there is not a file per block. Each blk file contains many blocks and is limited in size to 128MB. Here (http://learnmeabitcoin.com/glossary/blkdat) is a good explanation about them if you want to know more. Quote Block files are about 128 MB, allocated in 16 MB chunks to prevent excessive fragmentation. As of October 2015, the block chain is stored in about 365 block files, for a total of about 45 GB. This is stated in the link you provided, you obviously did not read it before posting.@OP here (https://en.bitcoin.it/wiki/Data_directory) is another link about the data dir. To answer your question here are class used in the code to manipulate blocks : Code: /** The block chain is a tree shaped structure starting with the Code: /** An in-memory indexed chain of blocks. */ And to store/access them in/from the data dir : Code: class CBlockFileInfo Code: struct CDiskBlockPos Code: /** Used to marshal pointers into hashes for db storage. */ All this code (I selected relevant information so you can understand without having to read it as a whole) is taken from the chain.h (https://github.com/bitcoin/bitcoin/blob/c6806ee8694e9c864a29ef260c87b9fed77de84f/src/chain.h#L379) file. Since this is just a header file you can try to read it is not too difficult. Title: Re: Is Blockchain an array of Blocks? Post by: coingecko on January 11, 2019, 09:27:22 AM Blockchain looks more like a "Linked Lists" with hash checksum to tie the link and integrity checks.
|