Author

Topic: Is Blockchain an array of Blocks? (Read 350 times)

member
Activity: 247
Merit: 40
January 11, 2019, 04:27:22 AM
#7
Blockchain looks more like a "Linked Lists" with hash checksum to tie the link and integrity checks.
sr. member
Activity: 279
Merit: 435
January 09, 2019, 08:51:43 AM
#6
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
Hi,

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 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 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
 * genesis block at the root, with each block potentially having multiple
 * candidates to be the next block. A blockindex may have multiple pprev pointing
 * to it, but at most one of them can be part of the currently active branch.
 */
class CBlockIndex
link : https://github.com/bitcoin/bitcoin/blob/c6806ee8694e9c864a29ef260c87b9fed77de84f/src/chain.h#L170

Code:
/** An in-memory indexed chain of blocks. */
class CChain {
link : https://github.com/bitcoin/bitcoin/blob/c6806ee8694e9c864a29ef260c87b9fed77de84f/src/chain.h#L444

And to store/access them in/from the data dir :
Code:
class CBlockFileInfo
{
public:
    unsigned int nBlocks;      //!< number of blocks stored in file
    unsigned int nSize;        //!< number of used bytes of block file
    unsigned int nUndoSize;    //!< number of used bytes in the undo file
    unsigned int nHeightFirst; //!< lowest height of block in file
    unsigned int nHeightLast;  //!< highest height of block in file
    uint64_t nTimeFirst;       //!< earliest time of block in file
    uint64_t nTimeLast;        //!< latest time of block in file
link : https://github.com/bitcoin/bitcoin/blob/c6806ee8694e9c864a29ef260c87b9fed77de84f/src/chain.h#L31

Code:
struct CDiskBlockPos
{
    int nFile;
    unsigned int nPos;
link : https://github.com/bitcoin/bitcoin/blob/c6806ee8694e9c864a29ef260c87b9fed77de84f/src/chain.h#L85

Code:
/** Used to marshal pointers into hashes for db storage. */
class CDiskBlockIndex : public CBlockIndex
{
public:
    uint256 hashPrev;

    CDiskBlockIndex() {
        hashPrev = uint256();
    }

    explicit CDiskBlockIndex(const CBlockIndex* pindex) : CBlockIndex(*pindex) {
        hashPrev = (pprev ? pprev->GetBlockHash() : uint256());
}
link : https://github.com/bitcoin/bitcoin/blob/c6806ee8694e9c864a29ef260c87b9fed77de84f/src/chain.h#L379

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 file. Since this is just a header file you can try to read it is not too difficult.
sr. member
Activity: 938
Merit: 452
Check your coin privilege
January 08, 2019, 05:05:03 PM
#5

You seem to be an overqualified Google researcher


No joke Cheesy 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.
member
Activity: 421
Merit: 97
January 08, 2019, 04:48:37 PM
#4


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.
sr. member
Activity: 938
Merit: 452
Check your coin privilege
January 08, 2019, 04:38:26 PM
#3
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.
legendary
Activity: 3038
Merit: 2162
January 08, 2019, 04:26:20 PM
#2

Code:
class Blockchain {
  constructor() {
    this.chain = [GenesisBlock()]; [b]/// array containing all Blocks[/b]
}
}

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.

member
Activity: 421
Merit: 97
January 08, 2019, 03:41:20 PM
#1
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 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.
Alternatively I will learn more about C++ however this would take longer than actually asking here and hoping
to get an answer  Smiley

My understanding of how Blockchains are implemented is the following:

You create a class Block such as:
Code:
class Block {
  constructor(index, timestamp, nonce, prevBlockHash, hash, transactions) {
    this.index = index;

    this.timestamp = timestamp;

    this.prevBlockHash = prevBlockHash;
    this.hash = hash;
    this.transactions = transactions;
    this.nonce = nonce;
}
}

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 {
  constructor() {
    this.chain = [GenesisBlock()]; [b]/// array containing all Blocks[/b]
}
}

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.
Jump to: