Bitcoin Forum
May 05, 2024, 03:20:50 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Is Blockchain an array of Blocks?  (Read 330 times)
CristianOff (OP)
Member
**
Offline Offline

Activity: 421
Merit: 97


View Profile
January 08, 2019, 08:41:20 PM
Merited by ABCbits (1), bones261 (1)
 #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.
1714922450
Hero Member
*
Offline Offline

Posts: 1714922450

View Profile Personal Message (Offline)

Ignore
1714922450
Reply with quote  #2

1714922450
Report to moderator
1714922450
Hero Member
*
Offline Offline

Posts: 1714922450

View Profile Personal Message (Offline)

Ignore
1714922450
Reply with quote  #2

1714922450
Report to moderator
"With e-currency based on cryptographic proof, without the need to trust a third party middleman, money can be secure and transactions effortless." -- Satoshi
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction.
1714922450
Hero Member
*
Offline Offline

Posts: 1714922450

View Profile Personal Message (Offline)

Ignore
1714922450
Reply with quote  #2

1714922450
Report to moderator
hatshepsut93
Legendary
*
Offline Offline

Activity: 2968
Merit: 2145



View Profile
January 08, 2019, 09:26:20 PM
Merited by ABCbits (1), joniboini (1), bones261 (1)
 #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.


.BEST.CHANGE..███████████████
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
██
███████████████
..BUY/ SELL CRYPTO..
KingZee
Sr. Member
****
Offline Offline

Activity: 910
Merit: 452


Check your coin privilege


View Profile
January 08, 2019, 09: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.

Beep boop beep boop
CristianOff (OP)
Member
**
Offline Offline

Activity: 421
Merit: 97


View Profile
January 08, 2019, 09: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.
KingZee
Sr. Member
****
Offline Offline

Activity: 910
Merit: 452


Check your coin privilege


View Profile
January 08, 2019, 10: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.

Beep boop beep boop
darosior
Sr. Member
****
Offline Offline

Activity: 279
Merit: 435


View Profile
January 09, 2019, 01:51:43 PM
Last edit: January 09, 2019, 02:01:55 PM by darosior
Merited by DarkStar_ (5), ABCbits (4)
 #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.
coingecko
Member
**
Offline Offline

Activity: 247
Merit: 40


View Profile WWW
January 11, 2019, 09:27:22 AM
 #7

Blockchain looks more like a "Linked Lists" with hash checksum to tie the link and integrity checks.

Track bitcoin holdings by public companies and bitcoin price on CoinGecko
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!