Bitcoin Forum
February 24, 2019, 12:14:37 AM *
News: Latest Bitcoin Core release: 0.17.1 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: Is Blockchain an array of Blocks?  (Read 201 times)
CristianOff
Member
**
Offline Offline

Activity: 154
Merit: 30


View Profile
January 08, 2019, 08:41:20 PM
Merited by ETFbitcoin (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.

Blockchain is the most human thing that we achieved. Our DNA is a blockchain and it took us thousands of years to realize it.
1550967277
Hero Member
*
Offline Offline

Posts: 1550967277

View Profile Personal Message (Offline)

Ignore
1550967277
Reply with quote  #2

1550967277
Report to moderator
1550967277
Hero Member
*
Offline Offline

Posts: 1550967277

View Profile Personal Message (Offline)

Ignore
1550967277
Reply with quote  #2

1550967277
Report to moderator
Your Bitcoin transactions
The Ultimate Bitcoin mixer
made truly anonymous.
with an advanced technology.
Mix coins
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1550967277
Hero Member
*
Offline Offline

Posts: 1550967277

View Profile Personal Message (Offline)

Ignore
1550967277
Reply with quote  #2

1550967277
Report to moderator
1550967277
Hero Member
*
Offline Offline

Posts: 1550967277

View Profile Personal Message (Offline)

Ignore
1550967277
Reply with quote  #2

1550967277
Report to moderator
1550967277
Hero Member
*
Offline Offline

Posts: 1550967277

View Profile Personal Message (Offline)

Ignore
1550967277
Reply with quote  #2

1550967277
Report to moderator
hatshepsut93
Hero Member
*****
Offline Offline

Activity: 1064
Merit: 678


Bitcoin maximalist


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


KingZee
Sr. Member
****
Offline Offline

Activity: 518
Merit: 397


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.

███████████
██
██
██
██
██
██
██
██
██
██
██
███████████
#1
███████████
██
██
██
██
██
██
██
██
██
██
██
███████████
BTC 
  ●
   BTC
  BTC  
.
    ▄▄▄▀▀▀▀
 ▄██▀
███        ▄▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄▄▄
▀███▄▄▄▄▀▀▀                 ▀▀▄▄
  ▀▀▀██████████████████████████▀
   ▄█▄     ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
    ▀▀██▄▄█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▀▀
      ▄  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
      ▀██▄  ▄▀▀▀▀▀▀▀▀▀▀▀▀▄
        ▀█▀██████████████▀▀
         ▀█▄▄ ▄▄▄▄▄▄▄▄▄▄
            █▀▄▄▄▄▄▄▄▄▄▄▀
             ▀▀▄▄▄▄▄▄▄
.
     BTC
  BTC   
  ●
  BTC  
███████████
██
██
██
██
██
██
██
██
██
██
██
███████████
███████████
██
██
██
██
██
██
██
██
██
██
██
███████████
CristianOff
Member
**
Offline Offline

Activity: 154
Merit: 30


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.

Blockchain is the most human thing that we achieved. Our DNA is a blockchain and it took us thousands of years to realize it.
KingZee
Sr. Member
****
Offline Offline

Activity: 518
Merit: 397


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.

███████████
██
██
██
██
██
██
██
██
██
██
██
███████████
#1
███████████
██
██
██
██
██
██
██
██
██
██
██
███████████
BTC 
  ●
   BTC
  BTC  
.
    ▄▄▄▀▀▀▀
 ▄██▀
███        ▄▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄▄▄
▀███▄▄▄▄▀▀▀                 ▀▀▄▄
  ▀▀▀██████████████████████████▀
   ▄█▄     ▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄
    ▀▀██▄▄█▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▄▀▀
      ▄  ▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀
      ▀██▄  ▄▀▀▀▀▀▀▀▀▀▀▀▀▄
        ▀█▀██████████████▀▀
         ▀█▄▄ ▄▄▄▄▄▄▄▄▄▄
            █▀▄▄▄▄▄▄▄▄▄▄▀
             ▀▀▄▄▄▄▄▄▄
.
     BTC
  BTC   
  ●
  BTC  
███████████
██
██
██
██
██
██
██
██
██
██
██
███████████
███████████
██
██
██
██
██
██
██
██
██
██
██
███████████
ETFbitcoin
Legendary
*
Offline Offline

Activity: 1596
Merit: 1555

Use SegWit and enjoy lower fees.


View Profile WWW
January 09, 2019, 05:11:47 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

Don't forget that Core 0.11 is outdated by now and there's change with chainstate/txindex database

Also, i found some links that might be relevant with your question :
https://bitcoin.stackexchange.com/q/28168
https://bitcoin.stackexchange.com/q/19019

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'd need 200GB+ of RAM if all blocks stored as array on RAM and you need twice amount of RAM if you need to resize the array Grin

darosior
Member
**
Offline Offline

Activity: 112
Merit: 121


View Profile WWW
January 09, 2019, 01:51:43 PM
Last edit: January 09, 2019, 02:01:55 PM by darosior
Merited by DarkStar_ (5), ETFbitcoin (4)
 #7

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
Jr. Member
*
Offline Offline

Activity: 70
Merit: 7


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

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

Cryptocurrency market and fundamental data (https://coingecko.com)
Pages: [1]
  Print  
 
Jump to:  

Bitcointalk.org is not available or authorized for sale. Do not believe any fake listings.
Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!