如何使用前一个块的散列链接2个块? 我是BooStand的新手,想在C++中实现一个基本的Buffic链。我在做一个链接列表的类比,我想知道,我到底如何使用散列而不是指针将块链链接在一起

如何使用前一个块的散列链接2个块? 我是BooStand的新手,想在C++中实现一个基本的Buffic链。我在做一个链接列表的类比,我想知道,我到底如何使用散列而不是指针将块链链接在一起,c++,pointers,hash,blockchain,C++,Pointers,Hash,Blockchain,考虑C++中链表实现的这段代码: struct node { node *prev; string data; } main() { node *first=new node; node *second=new node; second->prev=first; } 现在考虑块链的BaseBand块结构: class block { string hash; string p

考虑C++中链表实现的这段代码:

struct node
    {
        node *prev;
        string data;
    }

main()
{
    node *first=new node;
    node *second=new node;
    second->prev=first;
}

现在考虑块链的BaseBand块结构:

class block
    {
        string hash;
        string prev_hash;
        string data;

        public:
        string calc_hash();
    }

main()                       
{
    block genesis;
    genesis.data="name,gender,age";
    genesis.hash=calc_hash(data);
    genesis.prev_hash=0000;
    block second;
    second.data="name,gender,age";
    second.hash=calc_hash(data);
    second.prev_hash=genesis.hash;
}

现在,我如何使用散列而不是指针将这些块链接在一起?或者它应该像一个带有指针的链表一样实现,但有一些功能用于验证块的完整性?

块包含一个标题和一些数据(通常是事务)。用于计算散列的唯一部分是块头

块标题包含以下内容:

块头 {version 4B}{previous block hash 32B}{merkle root hash 32B}{time 4B}{bits 4B}{nonce 4B}

version (4 Bytes) - Block format version.
previous block hash (32 Bytes) - The hash of the preceding block. This is important to include in the header because the hash of the block is calculated from the header, and thus depends on the value of the previous block, linking each new block to the last. This is the link in the chain of the blockchain.
merkle root hash (32 Bytes) - The hash of the merkle tree root of all transactions in the block. If any transaction is changed, removed, or reordered, it will change the merkle root hash. This is what locks all of the transactions in the block.
time (4 Bytes) - Timestamp in Unix Time (seconds). Since the clocks of each node around the world is not guaranteed to be synchronized, this is just required to be within of the rest of the network.
bits (4 Bytes) - Target hash value in Compact Format. The block hash must be equal to or less than this value in order to be considered valid.
nonce (4 Bytes) - Can be any 4 Byte value, and is continuously changed while mining until a valid block hash is found.

不,这不是我要问的。请查看我的更新描述,它可能有助于更好地理解我的问题。
second.prev_hash=genesis.hash这就是链接它们的方式,您已经这样做了。如果不想像链表一样将所有块加载到内存中,则内存不足。例如,您可以将它们持久化到数据库中,并将它们的哈希作为索引。然后,在将任何新块添加到数据库之前,通过检查它们的数据、头和散列的有效性来验证它们,包括前一个块散列是否存在以及数据是否有效(例如,没有双重花费)好的,现在就知道了。非常感谢:)