1. 程式人生 > >C++從零開始區塊鏈:區塊鏈業務模組之區塊結構定義

C++從零開始區塊鏈:區塊鏈業務模組之區塊結構定義

區塊鏈的對外展示主要是以json的形式,我們先來定義一下json,主要是說明格式,資料什麼的我瞎寫的。實際應用中應該加入一個隨機值用作校驗,這裡就不加了

block = {
'index': 1,                                           //索引
'timestamp': 1506057125,                              //時間戳
'transactions': [                                     //交易列表,可以有多個交易
{ 
'sender': "8527147fe1f5426f9dd545de4b27ee00"
, //付款方 'recipient': "a77f5cdfa2934df3954a5c7c7da5df1f", //收款方 'amount': 5, //金額 } ], 'proof': 324984774000, //工作量證明 'previous_hash': "2cf24dba5fb0a30e26e83b2ac5b" //前一塊的hash }

json對應的C++結構體

typedef struct __transactions
{
    std
::string sender; std::string recipient; float amount; bool operator == (const struct __transactions & value) const { return this->sender == value.sender && this->recipient == value.recipient && this->amount == value.amount; } }Transactions; typedef
struct __block { int index; time_t timestamp; std::list<Transactions> lst_ts; long int proof; std::string previous_hash; bool operator == (const struct __block & value) const { return this->index == value.index && this->timestamp == value.timestamp && this->previous_hash == value.previous_hash && this->lst_ts == value.lst_ts && this->proof == value.proof; } } Block;