create account

Exploring Hive Blocks by geekgirl

View this thread on: hive.blogpeakd.comecency.com
· @geekgirl ·
$29.63
Exploring Hive Blocks
<center>![hivechain.png](https://images.hive.blog/DQmXWaj7thFepdn9UptfAXCzQiMBFBTE1gPD4cV4skexQYa/hivechain.png) Image by @doze</center>

This is not an expert information or educational material on Hive blockchain. This is just an observation by an ordinary participant of the Hive economy and an amateur coder trying to understand Hive blocks. I will be using python and Beem library by @holger80 for this exploration.

I start with the following python code that gets the last block number, gets the latest block, converts into a dictionary format and prints the contents of the block.

```
from beem import Hive
from beem.nodelist import NodeList
from beem.instance import set_shared_blockchain_instance
from beem.blockchain import Blockchain
from beem.block import Block 
from pprint import pprint 

nodelist = NodeList()
nodelist.update_nodes()
nodes = nodelist.get_hive_nodes()
hive = Hive(node=nodes)
set_shared_blockchain_instance(hive)

bc = Blockchain()
cbn = bc.get_current_block_num()

block = Block(cbn)
block = dict(block)
pprint(block)

```

The code returns key/value pair information about the block itself and transactions stored within the block. By running a for loop we can get what kind of items or information is stored in each block. Here is the list of 11 items that each block stores:

1. `id`
2. `block_id`
3. `previous`
4. `timestamp`
5. `extensions`
6. `witness`
7. `witness_signature`
8. `signing_key`
9. `transaction_merkle_root`
10. `transaction_ids`
11. `transactions`

The most interesting item in the above list is `transactions`, that is where all the transactions like asset transfers, posts, comments, votes, etc are stored. We will explore transactions in more details later. Rest of the items are information about the block itself. If we remove `transactions` and `transaction_ids` from the block, the result would look like this:

<center>![block.png](https://images.hive.blog/DQmWhN4WUVQcFvhkVHvYu7cFyTdCSHDv2rgtoPA3f6Dgbj8/block.png)</center>

`id` represents the block number. `block_id` looks like a unique alphanumeric id for the block. `previous` is a block_id of the previous block. Is this how chaining blocks happen, by including a reference to the previous block? Not sure, but that would be my guess. Then we have witness related information.

`witness` provides the name of the witness who produced the block. `witness_signature` how the block producing witness signs the block. And the `signing_key` is a public witness key. Interesting to note that average users who are not witnesses don't have this key. There are normally owner, active, posting, memo keys. But for witnesses looks like there is an additional key for signing blocks.

Two I items I have no clue about are the `extensions` and `transactions_merkle_root`. `extensions` appear in each block and looks like suppose to have an array/list of data in it. But it always appears to be empty.  This is what google says about merkle root:

>A merkle root is created by hashing together pairs of TXID s, which gives you a short yet unique fingerprint for all the transactions in a block. This merkle root is then used as a field in a block header, which means that every block header will have a short representation of every transaction inside the block.

That sort of explains the purpose of `transactions_merkle_root`.

My favorite one is `timestamp`, provides the date and time the block was produced. HIve blocks `timestamp` use UTC timezone. Each block is produced 3 seconds apart. However, I noticed sometimes there is 6 seconds difference between block timestamps. My guess for this is that sometimes witnesses miss blocks and when that happens the time difference between blocks is higher than 3 seconds.

The reason `timestamp` is my favorite because the blockchain uses its internal clock to stamp each block and prevents unexpected actions like double spending, provide transparency and security, and keep the integrity of the chain intact.

`transaction_ids` stores the list of unique transactoin ids for each transaction stored in the `transactions`. The following image shows an example of how a list of `transaction_ids` look like.

<center>![transactionids.png](https://images.hive.blog/DQmV7kL3u9f6vZHZwVt5w723nKJxXS4UHZ6wdreuqiwSFFi/transactionids.png)</center>

`transactions` is the most interesting compartment of each block. This is where all transactions are stored, and takes up the most space in a block. It contains a list of transactions and each transactions has the following data within them.

1. `ref_block_num`
2. `ref_block_prefix`
3. `expiration`
4. `extensions`
5. `signatures`
6. `operations`

<center>![transaction.png](https://images.hive.blog/DQmTufDKN6uSdPTnHhHohmctJqPgsqwDPy1PuU4xxZtrLyY/transaction.png)</center>

The main content of the transaction is stored in `operations`, and rest of the items seems to store information about the transaction itself. I really don't understand how `ref_block_num` and `ref_block_prefix` numbers are generated. One thing I noticed though is that `ref_block_num` never goes higher than 65536 and when it gets that high it starts again from 1. We have `extensions` again that appear to be an empty array in each transaction. I would guess `signature` is signed by an account executing the transaction. The `expiration` stores date and time info like `timestamp` but the time is in the future. So, my guess is that transaction will expire if not put in a block and produced within the provided time.
Lastly, `operations` is where all the fun stuff is happening.

`operations` contains a list of operations. In theory, it seems we can store multiple operations here. However, it looks like there is only one operation stored here most of the time. I explored multiple blocks, and yet to see where there were multiple operations were bundled together within `operations`.

Each operation has two items: *type* and *value*. The way the *value* data is stored depends on the type of the operations. I wrote a script to go through last 100000 blocks to get the list of unique operation types. Following is the list of operation types I came up with:

1. `custom_json_operation`
2. `vote_operation`
3. `transfer_operation`
4. `comment_operation`
5. `claim_reward_balance_operation`
6. `feed_publish_operation`
7. `limit_order_create_operation`
8. `limit_order_cancel_operation`
9. `witness_set_properties_operation`
10. `claim_account_operation`
11. `create_claimed_account_operation`
12. `account_update_operation`
13. `account_update2_operation`
14. `account_witness_vote_operation`
15. `transfer_to_vesting_operation`
16. `delegate_vesting_shares_operation` 
17. `convert_operation`
18. `transfer_to_savings_operation`
19. `account_witness_proxy_operation`
20. `delete_comment_operation`
21. `withdraw_vesting_operation`
22. `update_proposal_votes_operation`

There probably are more operations that can be done Hive. They all are self-explanatory. Most common ones we use on daily basis are the vote, comment, transfer, claim_reward_balance operations. The most used operation it seems is the `custom_json_operation`.  If you play games like Spliterlands, you can see those operations stored in a special `custom_json_operation`.  This is probably one of the most powerful features of Hive blockchain to provide for developers of Apps and Games with ability to store and retrieve their data easily on the blockchain.

Feel free to correct me if I made mistakes or wrong assumption about certain parts of the blocks and feel free to share about things I have no clue about yet. Thanks!

Posted Using [LeoFinance](https://leofinance.io/@geekgirl/exploring-hive-blocks)
👍  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 242 others
properties (23)
authorgeekgirl
permlinkexploring-hive-blocks
categoryhive-167922
json_metadata{"tags":["hive","blockchain","python","coding","stemgeeks","stemsocial","neoxian","ctptalk","posh","leofinance"],"users":["doze","holger80"],"image":["https://images.hive.blog/DQmXWaj7thFepdn9UptfAXCzQiMBFBTE1gPD4cV4skexQYa/hivechain.png","https://images.hive.blog/DQmWhN4WUVQcFvhkVHvYu7cFyTdCSHDv2rgtoPA3f6Dgbj8/block.png","https://images.hive.blog/DQmV7kL3u9f6vZHZwVt5w723nKJxXS4UHZ6wdreuqiwSFFi/transactionids.png","https://images.hive.blog/DQmTufDKN6uSdPTnHhHohmctJqPgsqwDPy1PuU4xxZtrLyY/transaction.png"],"app":"leofinance/0.1","format":"markdown","canonical_url":"https://leofinance.io/@geekgirl/exploring-hive-blocks"}
created2020-09-20 23:24:03
last_update2020-09-20 23:24:03
depth0
children15
last_payout2020-09-27 23:24:03
cashout_time1969-12-31 23:59:59
total_payout_value16.002 HBD
curator_payout_value13.627 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length7,656
author_reputation1,588,017,852,468,897
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,733,955
net_rshares111,577,657,281,145
author_curate_reward""
vote details (306)
@edicted ·
$0.26
>I really don't understand how ref_block_num and ref_block_prefix numbers are generated.

#### Someone who knows what they are talking about:
https://leofinance.io/steem/@xeroc/steem-transaction-signing-in-a-nutshell

>Let's discuss the ref_block_* parameters a little: The ref_block_num indicates a particular block in the past by referring to the block number which has this number as the last two bytes. The ref_block_prefix on the other hand is obtain from the block id of that particular reference block. It is one unsigned integer (4 bytes) of the block id, but not starting at the first position but with an offset of 4 bytes.

>The purpose of these two parameters is to prevent replay attacks in the case of a fork. Once two chains have forked, the two parameters identify two different blocks. Applying a signed transaction of one chain at another chain will invalidate the signature.

-----

> One thing I noticed though is that ref_block_num never goes higher than 65536

This is to stop someone from using a very old block number.  They don't want people to sign a transaction using an old block... because if their was a fork a hacker would be able to rebroadcast the public transaction on the other chain. 

# Me:
https://peakd.com/hextech/@edicted/the-day-of-milestones

>So not only does ref_block_num & ref_block_reference stop users from repeating a transaction on the same chain, it also prevents users from repeating a transaction on a forked chain. This was obviously very intriguing for me, because Hive just forked away from Steem and I was totally confused about how this would be accomplished.

>Essentially, as long as ref_block_num references a block AFTER a hardfork, it would be impossible to broadcast that same transaction onto another fork. This is because the signatures of the 2 blocks on the separate forks would be completely different. Pretty cool. They really thought of everything, eh?

>Because ref_block_num is only the last two bytes of the block number in question, it is impossible to reference a block that is very old. 2 bytes is 16 bits, so the maximum length of an unsigned integer stored in this structure is 2^16 or 65,536. Therefore, every 65,536 blocks the ref_block_num overflows back to zero and the process starts all over again. With 3 second blocks, this only ends up being about 54.6 hours per cycle.

Posted Using [LeoFinance](https://leofinance.io/@edicted/qgzgb5)
👍  , ,
properties (23)
authoredicted
permlinkqgzgb5
categoryhive-167922
json_metadata{"tags":["leofinance"],"links":["https://leofinance.io/steem/@xeroc/steem-transaction-signing-in-a-nutshell","https://peakd.com/hextech/@edicted/the-day-of-milestones"],"app":"leofinance/0.1","canonical_url":"https://leofinance.io/@edicted/qgzgb5"}
created2020-09-21 00:35:30
last_update2020-09-21 00:35:30
depth1
children1
last_payout2020-09-28 00:35:30
cashout_time1969-12-31 23:59:59
total_payout_value0.128 HBD
curator_payout_value0.129 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length2,424
author_reputation3,497,786,108,755,621
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,734,758
net_rshares1,506,880,802,731
author_curate_reward""
vote details (3)
@geekgirl ·
$0.02
Awesome! Thank you for sharing the posts on this and explaining why and how ref_block_num and ref_block_reference work. Very helpful. I will study the posts more.

Posted Using [LeoFinance](https://leofinance.io/@geekgirl/qgzhc1)
👍  , ,
properties (23)
authorgeekgirl
permlinkqgzhc1
categoryhive-167922
json_metadata{"tags":["leofinance"],"app":"leofinance/0.1","canonical_url":"https://leofinance.io/@geekgirl/qgzhc1"}
created2020-09-21 00:57:39
last_update2020-09-21 00:57:39
depth2
children0
last_payout2020-09-28 00:57:39
cashout_time1969-12-31 23:59:59
total_payout_value0.016 HBD
curator_payout_value0.007 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length229
author_reputation1,588,017,852,468,897
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,735,028
net_rshares214,155,623,934
author_curate_reward""
vote details (3)
@frankbacon ·
HIVE.D!


![Screenshot 2020-09-18 at 8.50.37 AM.png](https://images.hive.blog/DQmcdXBi7f7Wxif1u3oRiKnUFD1y5jD3ntaqCJbYbUQJYPb/Screenshot%202020-09-18%20at%208.50.37%20AM.png)
properties (22)
authorfrankbacon
permlinkqh0oxe
categoryhive-167922
json_metadata{"image":["https://images.hive.blog/DQmcdXBi7f7Wxif1u3oRiKnUFD1y5jD3ntaqCJbYbUQJYPb/Screenshot%202020-09-18%20at%208.50.37%20AM.png"],"app":"hiveblog/0.1"}
created2020-09-21 16:39:12
last_update2020-09-21 16:39:12
depth1
children0
last_payout2020-09-28 16:39:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length174
author_reputation38,509,879,409,111
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,746,188
net_rshares0
@hivetrending · (edited)
$0.19
Anyone who’s interested in exploring hive blocks may enjoy this visualization. It streams Hive blocks and visualizes their contents, including identifying the apps in custom JSON operations. 

https://hiveuprss.github.io/hiveisbeautiful/
👍  
properties (23)
authorhivetrending
permlinkre-geekgirl-qgzf19
categoryhive-167922
json_metadata{"tags":["hive-167922"],"app":"peakd/2020.09.4"}
created2020-09-21 00:07:57
last_update2020-09-21 00:08:18
depth1
children3
last_payout2020-09-28 00:07:57
cashout_time1969-12-31 23:59:59
total_payout_value0.118 HBD
curator_payout_value0.073 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length237
author_reputation73,034,118,030,806
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,734,427
net_rshares1,392,331,872,735
author_curate_reward""
vote details (1)
@geekgirl ·
This is pretty cool tool! Where can I see the code?

Posted Using [LeoFinance](https://leofinance.io/@geekgirl/qgzh6i)
properties (22)
authorgeekgirl
permlinkqgzh6i
categoryhive-167922
json_metadata{"tags":["leofinance"],"app":"leofinance/0.1","canonical_url":"https://leofinance.io/@geekgirl/qgzh6i"}
created2020-09-21 00:54:21
last_update2020-09-21 00:54:21
depth2
children2
last_payout2020-09-28 00:54:21
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length118
author_reputation1,588,017,852,468,897
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,734,976
net_rshares0
@hivetrending ·
$0.10
Hey! The code is here: https://github.com/hiveuprss/hiveuprss.github.io/tree/master/hiveisbeautiful
👍  
properties (23)
authorhivetrending
permlinkre-geekgirl-qgzhdc
categoryhive-167922
json_metadata{"tags":["hive-167922"],"app":"peakd/2020.09.4"}
created2020-09-21 00:58:24
last_update2020-09-21 00:58:24
depth3
children1
last_payout2020-09-28 00:58:24
cashout_time1969-12-31 23:59:59
total_payout_value0.052 HBD
curator_payout_value0.052 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length99
author_reputation73,034,118,030,806
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,735,034
net_rshares661,586,647,722
author_curate_reward""
vote details (1)
@nathanmars ·
$0.23
My python skillzz might become useful soon

Posted Using [LeoFinance](https://leofinance.io/@nathanmars/qgzf88)
👍  
properties (23)
authornathanmars
permlinkqgzf88
categoryhive-167922
json_metadata{"tags":["leofinance"],"app":"leofinance/0.1","canonical_url":"https://leofinance.io/@nathanmars/qgzf88"}
created2020-09-21 00:12:09
last_update2020-09-21 00:12:09
depth1
children1
last_payout2020-09-28 00:12:09
cashout_time1969-12-31 23:59:59
total_payout_value0.115 HBD
curator_payout_value0.115 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length111
author_reputation336,354,946,115,368
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,734,477
net_rshares1,366,479,701,312
author_curate_reward""
vote details (1)
@geekgirl ·
Your skillz are impressive!

Posted Using [LeoFinance](https://leofinance.io/@geekgirl/qgzh76)
👍  
properties (23)
authorgeekgirl
permlinkqgzh76
categoryhive-167922
json_metadata{"tags":["leofinance"],"app":"leofinance/0.1","canonical_url":"https://leofinance.io/@geekgirl/qgzh76"}
created2020-09-21 00:54:48
last_update2020-09-21 00:54:48
depth2
children0
last_payout2020-09-28 00:54:48
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length94
author_reputation1,588,017,852,468,897
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,734,982
net_rshares75,756,685,252
author_curate_reward""
vote details (1)
@poshbot ·
https://twitter.com/geekjen/status/1307823303504011264
properties (22)
authorposhbot
permlinkre-exploring-hive-blocks-20200920t232607z
categoryhive-167922
json_metadata"{"app": "beem/0.24.8"}"
created2020-09-20 23:26:06
last_update2020-09-20 23:26:06
depth1
children0
last_payout2020-09-27 23:26:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length54
author_reputation5,554,335,374,496
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,733,965
net_rshares0
@poshbot ·
https://twitter.com/free_steinbart/status/1333604029818089477
properties (22)
authorposhbot
permlinkre-exploring-hive-blocks-20201201t024945z
categoryhive-167922
json_metadata"{"app": "beem/0.24.8"}"
created2020-12-01 02:49:45
last_update2020-12-01 02:49:45
depth1
children0
last_payout2020-12-08 02:49:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length61
author_reputation5,554,335,374,496
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id100,757,951
net_rshares0
@techcoderx ·
$0.22
The `extensions` used to store the running version of `steemd` that signed the block, as well as the upcoming hardforks that a witness has accepted (if any). But since HF23 this information is no longer present.

This is the [first block](https://hiveblocks.com/b/38707240) that I signed on the chain which recorded v0.22.1, and the [last block](https://hiveblocks.com/b/41815781) that I signed on Steem, which shows that I accepted HF23 at a particular timestamp.

>There probably are more operations that can be done Hive.

There are more than 50 different operations on Hive, which can be found [here](https://gitlab.syncad.com/hive/hive/-/blob/master/libraries/protocol/include/hive/protocol/operations.hpp). Some of them were depreciated in previous hardforks, such as creating an account with delegation.
👍  
properties (23)
authortechcoderx
permlinkre-geekgirl-2020921t94931160z
categoryhive-167922
json_metadata{"tags":["hive","blockchain","python","coding","stemgeeks","stemsocial","neoxian","ctptalk","posh","leofinance"],"app":"esteem/2.2.7-surfer","format":"markdown+html","community":"esteem.app"}
created2020-09-21 01:49:33
last_update2020-09-21 01:49:33
depth1
children2
last_payout2020-09-28 01:49:33
cashout_time1969-12-31 23:59:59
total_payout_value0.108 HBD
curator_payout_value0.112 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length810
author_reputation47,782,567,826,605
root_title"Exploring Hive Blocks"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,735,649
net_rshares1,330,032,456,298
author_curate_reward""
vote details (1)
@geekgirl ·
Thank you for sharing. These are very useful. Do you know what `extensions` inside of transactions for?
That operations list is great. Thanks. It even has virtual operations. Something I was going to look at next.
👍  
properties (23)
authorgeekgirl
permlinkqgzmn5
categoryhive-167922
json_metadata{"app":"hiveblog/0.1"}
created2020-09-21 02:52:18
last_update2020-09-21 02:52:18
depth2
children1
last_payout2020-09-28 02:52:18
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length213
author_reputation1,588,017,852,468,897
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,736,260
net_rshares1,483,385,268
author_curate_reward""
vote details (1)
@techcoderx ·
For some reason they are meant to be empty

https://gitlab.syncad.com/hive/hive/-/blob/master/libraries/plugins/apis/condenser_api/include/hive/plugins/condenser_api/condenser_api_legacy_objects.hpp#L39
properties (22)
authortechcoderx
permlinkre-geekgirl-2020922t144431975z
categoryhive-167922
json_metadata{"tags":["esteem"],"app":"esteem/2.2.7-surfer","format":"markdown+html","community":"esteem.app"}
created2020-09-22 06:44:36
last_update2020-09-22 06:44:36
depth3
children0
last_payout2020-09-29 06:44:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length202
author_reputation47,782,567,826,605
root_title"Exploring Hive Blocks"
beneficiaries
0.
accountesteemapp
weight300
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,756,393
net_rshares0
@utopiaeducators ·
Great Post Geek Girl
👍  
👎  ,
properties (23)
authorutopiaeducators
permlinkqh25mw
categoryhive-167922
json_metadata{"app":"hiveblog/0.1"}
created2020-09-22 11:37:45
last_update2020-09-22 11:37:45
depth1
children0
last_payout2020-09-29 11:37:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length20
author_reputation-4,088,797,786,952
root_title"Exploring Hive Blocks"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id99,759,354
net_rshares-10,456,875,944,310
author_curate_reward""
vote details (3)