create account

Part 23: Retrieve And Process Full Blocks From The Steem Blockchain by steempytutorials

View this thread on: hive.blogpeakd.comecency.com
· @steempytutorials · (edited)
$50.40
Part 23: Retrieve And Process Full Blocks From The Steem Blockchain
<center>![steem-python.png](https://res.cloudinary.com/hpiynhbhq/image/upload/v1515886103/kmzfcpvtzuwhvqhgpyjp.png)</center>

This tutorial is part of a series where different aspects of programming with `steem-python` are explained. Links to the other tutorials can be found in the curriculum section below. Every action performed on the `Steem Blockchain` is registered in `blocks`. These `blocks` are the building stones for the `Steem Blockchain` and this tutorial will explain how to retrieve and process individual `blocks`.

---

#### Repository
https://github.com/steemit/steem-python

#### What will I learn

- Stream full blocks from the blockchain
- What do blocks look like?
- What in a transaction?
- What is an operation?
- Filter for different kind of operations

#### Requirements

- Python3.6
- `steem-python`

#### Difficulty

- basic

---

### Tutorial

#### Setup
Download the file from [Github](https://github.com/amosbastian/steempy-tutorials/tree/master/part_23). There is 1 file `get_block.py` which contains the code. The file takes 3 arguments from the command line which sets the `starting_block`, `block_count` and what `operation` to filter for.

Run scripts as following:
`> python get_block.py 22224731 10 transfer`

#### Stream full blocks from the blockchain
Tutorials until now used  the function `stream` to stream `operations` as they appeared on the `Steem blockchain` from the current `head block`. This is pretty close to a live stream as long as there is no backlog. This works perfect however it is susceptible to crashes and it is also not possible to backtrack previous `blocks`. 

By taking full control over which blocks to retrieve from the blockchain the user will have more control and the software becomes more resistant to crashes. The `Blockchain` class holds the function `stream_from` which can be used to achieve this. The `start_block` must be set to the preferred block to starting streaming from, `block_count` is used to calculate the `end_block` and `full_blocks` must be set to `True` in order to retrieve the entire block.

```
from steem.blockchain import Blockchain

stream = self.b.stream_from(start_block=self.block,
                            end_block=self.end_block,
                            full_blocks=True)
            for block in stream:
              //do something with the block
```
<br>


**Note: For this tutorial end_block is set for testing purposes. Leaving this blank will keep the stream alive and wait for new blocks**

#### What do blocks look like?
`Blocks` are the building stones for the `blockchain` and in essence contain all the information that is stored on `Steem`. `Blocks` are linked in a chain hence the name `blockchain`. Each `block` itself is not that large and new `blocks` are created every 3 seconds. Only information that is added or changed gets put into new `blocks`. The basic structure of a `block` looks as follows:

```
{
	'previous': '01531f5aded177b75bbe144f44958c01e8061890',
	'timestamp': '2018-05-07T15:20:03',
	'witness': 'jesta',
	'transaction_merkle_root': '01fbed04e90d4b19569788f2f34467d2de2edb40',
	'extensions': [],
	'witness_signature': '204cb7d24d1b3b440768873d3bd5fd29dd35925e38508346e998a486a83164b8aa7be6d748fdc4433231275b2caad3fe9400b850ac8656dc4565409115563d9a8b',
	'transactions': []
	'block_id': '01531f5ba3f99e0e10a6015491fa6f09bb3501ac',
	'signing_key': 'STM8MFSHJA2DsjmEUBsf8JYdhm25YghdEUeDEMqNVMrWMCgHErDaL',
	'transaction_ids': []
}
```
<br>

The `timestamp` is useful to validate the age of the data, `transactions` and `transaction_ids` are left blank in this example as they contain most of the data. The `transaction` and the `transaction_id` are associated with each other based on their `index`. So the first `transaction` correlates with the first `transaction_id`. Every action on the `Steem` network registered into a `transaction`.

#### What is a transaction?
Every `transaction` has the following structure:

```
{
	'ref_block_num': 8004,
	'ref_block_prefix': 1744181707,
	'expiration': '2018-05-07T15:29:54',
	'operations': [
		['vote', {
			'voter': 'acehjaya',
			'author': 'zunaofficial79',
			'permlink': 'kabut-pagi',
			'weight': 10000
		}]
	],
	'extensions': [],
	'signatures': ['1f205086670c27365738697f0bc7cfde8b6e976608dc12b0d391b2b85ad7870a002313be4b09a358c30010be2a09bebacead866ba180afc348ce40c70566f4ed88']
}
```

Found under `operations` are alterations made to the `Steem Blockchain` by its users. Here common `operations` like voting, transfers and comments are found. 

#### What is an operation

Every action that is registered on `Steem` is a `operation` which fits in one of the following categories:

```
comment, delete_comment, vote
account_create, account_update, request_account_recovery, recover_account
limit_order_create, limit_order_cancel
transfer, transfer_to_vesting, withdraw_vesting, convert
pow, feed_publish, witness_update, account_witness_vote
custom, custom_json
```
[Source](https://steemit.com/steem/@furion/developers-guide-to-steem-s-blockchain)
<br>
Each `operation` has its own structure and requires unique handling for processing. A transfer looks as follows:
```
{
	'from': 'yusril-steem',
	'to': 'postpromoter',
	'amount': '5.000 SBD',
	'memo': 'https://steemit.com/life/@yusril-steem/our-challenges-and-trials-in-life'
}
```
<br>

#### Filter for different kind of operations
Each `block` is filled with `transactions` in which an `operation` is embedded. Filtering for `operations` is done by checking its type which is located under `['operations'][0][1]`. The `transaction_index` is tracked to know the specific `transaction` in which the `operation` took place.

```
for transaction in block['transactions']:
    if transaction['operations'][0][0] == self.operation:
        print(transaction_index,
              transaction['operations'][0][1])
    transaction_index += 1
```
<br>

#### Running the script
Running the script will print `operations`, for which the filter is set,  to the terminal ,with their corresponding `transaction_index`, starting from the `starting_block` for `block_count` amount of `blocks`. Feel free to test out the different `operation` types to get a feeling how they are stored in the `blocks`.

```
python get_block.py 22224731 10 transfer

Booted
Connected to: https://rpc.buildteam.io

Block:  22224731
44 {'from': 'hottopic', 'to': 'jgullinese', 'amount': '0.001 SBD', 'memo': 'Hello Friend, Your post will be more popular and you will find new friends.We provide "Resteem upvote and promo" service.Resteem to 18.000+ Follower,Min 45+ Upvote.Send 1 SBD or 1 STEEM to @hottopic (URL as memo) Service Active'}
...
...
...
```



#### Curriculum
##### Set up:
- [Part 0: How To Install Steem-python, The Official Steem Library For Python](https://utopian.io/utopian-io/@amosbastian/how-to-install-steem-python-the-official-steem-library-for-python)
- [Part 1: How To Configure The Steempy CLI Wallet And Upvote An Article With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-1-how-to-configure-the-steempy-cli-wallet-and-upvote-an-article-with-steem-python)
##### Filtering
- [Part 2: How To Stream And Filter The Blockchain Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-2-how-to-stream-and-filter-the-blockchain-using-steem-python)
- [Part 6: How To Automatically Reply To Mentions Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-6-how-to-automatically-reply-to-mentions-using-steem-python)
##### Voting
- [Part 3: Creating A Dynamic Autovoter That Runs 24/7](https://utopian.io/utopian-io/@steempytutorials/part-3-creating-a-dynamic-upvote-bot-that-runs-24-7-first-weekly-challenge-3-steem-prize-pool)
- [Part 4: How To Follow A Voting Trail Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-4-how-to-follow-a-voting-trail-using-steem-python)
- [Part 8: How To Create Your Own Upvote Bot Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-8-how-to-create-your-own-upvote-bot-using-steem-python)
##### Posting
- [Part 5: Post An Article Directly To The Steem Blockchain And Automatically Buy Upvotes From Upvote Bots](https://utopian.io/utopian-io/@steempytutorials/part-5-post-an-article-directly-to-the-steem-blockchain-and-automatically-buy-upvotes-from-upvote-bots)
- [Part 7: How To Schedule Posts And Manually Upvote Posts For A Variable Voting Weight With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-7-how-to-schedule-posts-and-manually-upvote-posts-for-a-variable-voting-weight-with-steem-python)
###### Constructing
- [Part 10: Use Urls To Retrieve Post Data And Construct A Dynamic Post With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-10-use-urls-to-retrieve-post-data-and-construct-a-dynamic-post-with-steem-python)
##### Rewards
- [Part 9: How To Calculate A Post's Total Rewards Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/how-to-calculate-a-post-s-total-rewards-using-steem-python)
- [Part 12: How To Estimate Curation Rewards Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-12-how-to-estimate-curation-rewards)
- [Part 14: How To Estimate All Rewards In Last N Days Using Steem-Python](https://utopian.io/utopian-io/@steempytutorials/how-to-estimate-all-rewards-in-last-n-days-using-steem-python)
##### Transfers
- [Part 11: How To Build A List Of Transfers And Broadcast These In One Transaction With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-11-how-to-build-a-list-of-transfers-and-broadcast-these-in-one-transaction-with-steem-python)
- [Part 13: Upvote Posts In Batches Based On Current Voting Power With Steem-Python](https://utopian.io/utopian-io/@steempytutorials/part-13-upvote-posts-in-batches-based-on-current-voting-power-with-steem-python)
##### Account Analysis
- [Part 15: How To Check If An Account Is Following Back And Retrieve Mutual Followers/Following Between Two Accounts](https://utopian.io/utopian-io/@steempytutorials/part-15-how-to-check-if-an-account-is-following-back-and-retrieve-mutual-followers-following-between-two-accounts)
- [Part 16: How To Analyse A User's Vote History In A Specific Time Period Using Steem-Python](https://steemit.com/utopian-io/@steempytutorials/part-16-how-to-analyse-a-user-s-vote-history-in-a-specific-time-period-using-steem-python)
- [Part 18: How To Analyse An Account's Resteemers Using Steem-Python](https://steemit.com/utopian-io/@steempytutorials/part-18-how-to-analyse-an-account-s-resteemers)
##### Wallet
- [Part 22: Calculate Account Value Based On Current Market Prices Via API](https://steemit.com/utopian-io/@steempytutorials/part-22-calculate-account-value-based-on-current-market-prices-via-api)
---

The code for this tutorial can be found on [GitHub](https://github.com/amosbastian/steempy-tutorials/tree/master/part_23)!

This tutorial was written by @juliank.
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , ,
properties (23)
authorsteempytutorials
permlinkpart-23-retrieve-and-process-full-blocks-from-the-steem-blockchain
categoryutopian-io
json_metadata{"tags":["utopian-io","tutorials","steem-python","python","programming"],"users":["juliank"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1515886103/kmzfcpvtzuwhvqhgpyjp.png"],"links":["https://github.com/steemit/steem-python","https://github.com/amosbastian/steempy-tutorials/tree/master/part_23","https://steemit.com/steem/@furion/developers-guide-to-steem-s-blockchain","https://utopian.io/utopian-io/@amosbastian/how-to-install-steem-python-the-official-steem-library-for-python","https://utopian.io/utopian-io/@steempytutorials/part-1-how-to-configure-the-steempy-cli-wallet-and-upvote-an-article-with-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-2-how-to-stream-and-filter-the-blockchain-using-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-6-how-to-automatically-reply-to-mentions-using-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-3-creating-a-dynamic-upvote-bot-that-runs-24-7-first-weekly-challenge-3-steem-prize-pool","https://utopian.io/utopian-io/@steempytutorials/part-4-how-to-follow-a-voting-trail-using-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-8-how-to-create-your-own-upvote-bot-using-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-5-post-an-article-directly-to-the-steem-blockchain-and-automatically-buy-upvotes-from-upvote-bots","https://utopian.io/utopian-io/@steempytutorials/part-7-how-to-schedule-posts-and-manually-upvote-posts-for-a-variable-voting-weight-with-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-10-use-urls-to-retrieve-post-data-and-construct-a-dynamic-post-with-steem-python","https://utopian.io/utopian-io/@steempytutorials/how-to-calculate-a-post-s-total-rewards-using-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-12-how-to-estimate-curation-rewards","https://utopian.io/utopian-io/@steempytutorials/how-to-estimate-all-rewards-in-last-n-days-using-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-11-how-to-build-a-list-of-transfers-and-broadcast-these-in-one-transaction-with-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-13-upvote-posts-in-batches-based-on-current-voting-power-with-steem-python","https://utopian.io/utopian-io/@steempytutorials/part-15-how-to-check-if-an-account-is-following-back-and-retrieve-mutual-followers-following-between-two-accounts","https://steemit.com/utopian-io/@steempytutorials/part-16-how-to-analyse-a-user-s-vote-history-in-a-specific-time-period-using-steem-python","https://steemit.com/utopian-io/@steempytutorials/part-18-how-to-analyse-an-account-s-resteemers","https://steemit.com/utopian-io/@steempytutorials/part-22-calculate-account-value-based-on-current-market-prices-via-api"],"app":"steemit/0.1","format":"markdown"}
created2018-06-17 22:10:30
last_update2018-06-17 23:00:48
depth0
children7
last_payout2018-06-24 22:10:30
cashout_time1969-12-31 23:59:59
total_payout_value38.973 HBD
curator_payout_value11.422 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length10,928
author_reputation31,094,047,689,691
root_title"Part 23: Retrieve And Process Full Blocks From The Steem Blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,146,978
net_rshares23,511,823,062,820
author_curate_reward""
vote details (28)
@grammarnazi ·
Minor Correction
You have a minor grammatical mistake in the following sentence: <blockquote>Each `operation` has it's own structure and requires unique handling for processing.</blockquote> It should be <i>its own</i> instead of <i>it's own</i>.
πŸ‘  
properties (23)
authorgrammarnazi
permlinkre-steempytutorials-part-23-retrieve-and-process-full-blocks-from-the-steem-blockchain-20180617t221029959z
categoryutopian-io
json_metadata{"app":"steemit"}
created2018-06-17 22:10:33
last_update2018-06-17 22:10:33
depth1
children1
last_payout2018-06-24 22:10:33
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_length229
author_reputation-144,064,903,190
root_title"Part 23: Retrieve And Process Full Blocks From The Steem Blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,146,985
net_rshares4,578,875,214
author_curate_reward""
vote details (1)
@steempytutorials ·
Thanks bot!
properties (22)
authorsteempytutorials
permlinkre-grammarnazi-re-steempytutorials-part-23-retrieve-and-process-full-blocks-from-the-steem-blockchain-20180617t221141651z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-06-17 22:11:42
last_update2018-06-17 22:11:42
depth2
children0
last_payout2018-06-24 22:11:42
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_length11
author_reputation31,094,047,689,691
root_title"Part 23: Retrieve And Process Full Blocks From The Steem Blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,147,088
net_rshares0
@portugalcoin ·
$0.22
Thank you for your contribution.

- It would be interesting to see print screens about your work during the tutorial.

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/8/11114123).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  
properties (23)
authorportugalcoin
permlinkre-steempytutorials-part-23-retrieve-and-process-full-blocks-from-the-steem-blockchain-20180618t210202834z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://join.utopian.io/guidelines","https://review.utopian.io/result/8/11114123","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-06-18 21:02:03
last_update2018-06-18 21:02:03
depth1
children1
last_payout2018-06-25 21:02:03
cashout_time1969-12-31 23:59:59
total_payout_value0.178 HBD
curator_payout_value0.045 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length609
author_reputation599,460,462,895,094
root_title"Part 23: Retrieve And Process Full Blocks From The Steem Blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,274,503
net_rshares106,353,745,549
author_curate_reward""
vote details (1)
@juliank ·
Thanks for the work and feedback, what would you like to see in these screenshots that are not already in the commandline outputs I shared above?
properties (22)
authorjuliank
permlinkre-portugalcoin-re-steempytutorials-part-23-retrieve-and-process-full-blocks-from-the-steem-blockchain-20180618t212713287z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-06-18 21:27:15
last_update2018-06-18 21:27:15
depth2
children0
last_payout2018-06-25 21:27:15
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_length145
author_reputation117,823,071,447,502
root_title"Part 23: Retrieve And Process Full Blocks From The Steem Blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,276,851
net_rshares0
@utopian-io ·
Hey @steempytutorials
**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Contributing on Utopian**
Learn how to contribute on <a href='https://join.utopian.io'>our website</a> or by watching <a href='https://www.youtube.com/watch?v=8S1AtrzYY1Q'>this tutorial</a> on Youtube.

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-part-23-retrieve-and-process-full-blocks-from-the-steem-blockchain-20180619t005008z
categoryutopian-io
json_metadata"{"app": "beem/0.19.29"}"
created2018-06-19 00:50:09
last_update2018-06-19 00:50:09
depth1
children0
last_payout2018-06-26 00:50:09
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_length513
author_reputation152,955,367,999,756
root_title"Part 23: Retrieve And Process Full Blocks From The Steem Blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,292,966
net_rshares0
@vividessor · (edited)
$0.06
Thank you.  That was very useful!

Your links to tutorials before Part 16 show me a blank page.

Tutorial 15 is at:
https://steemit.com/utopian-io/@steempytutorials/part-15-how-to-check-if-an-account-is-following-back-and-retrieve-mutual-followers-following-between-two-accounts
Your link points at:
https://utopian.io/utopian-io/@steempytutorials/part-15-how-to-check-if-an-account-is-following-back-and-retrieve-mutual-followers-following-between-two-accounts
πŸ‘  , , , ,
properties (23)
authorvividessor
permlinkre-steempytutorials-part-23-retrieve-and-process-full-blocks-from-the-steem-blockchain-20180618t023048523z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1","links":["https://steemit.com/utopian-io/@steempytutorials/part-15-how-to-check-if-an-account-is-following-back-and-retrieve-mutual-followers-following-between-two-accounts","https://utopian.io/utopian-io/@steempytutorials/part-15-how-to-check-if-an-account-is-following-back-and-retrieve-mutual-followers-following-between-two-accounts"]}
created2018-06-18 02:30:48
last_update2018-06-18 02:35:45
depth1
children1
last_payout2018-06-25 02:30:48
cashout_time1969-12-31 23:59:59
total_payout_value0.047 HBD
curator_payout_value0.010 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length461
author_reputation337,542,545,478
root_title"Part 23: Retrieve And Process Full Blocks From The Steem Blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,167,035
net_rshares27,655,235,598
author_curate_reward""
vote details (5)
@juliank ·
Thanks for mentioning, will update that later
properties (22)
authorjuliank
permlinkre-vividessor-re-steempytutorials-part-23-retrieve-and-process-full-blocks-from-the-steem-blockchain-20180618t092447037z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-06-18 09:24:48
last_update2018-06-18 09:24:48
depth2
children0
last_payout2018-06-25 09:24: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_length45
author_reputation117,823,071,447,502
root_title"Part 23: Retrieve And Process Full Blocks From The Steem Blockchain"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id61,202,718
net_rshares0