steem-python for dummies #11 - Listening new blocks

in #utopian-io7 years ago (edited)

Hello,

This is the 11. post of "steem-python for dummies" series. If you didn't read the older ones take your time and have a look. (Just realizing we have already completed 10 posts!)

  1. steem-python for dummies #1 - Introduction and Basic Operations
  2. steem-python for dummies #2 - Playing with account data
  3. steem-python for dummies #3 - Coding an upvote bot
  4. steem-python for dummies #4 - Private Memos
  5. steem-python for dummies #5 - Bundling Blockchain Operations
  6. steem-python for dummies #6 - Delegating
  7. steem-python for dummies #7 - Creating Accounts
  8. steem-python for dummies #8 - Calculating Post Rewards
  9. steem-python for dummies #9 - Creating orders in the market
  10. steem-python for dummies #10 - Listening accounts

In this post, we will learn how to listen new blocks and parse them into transactions and operations. Basically, what you need for a simple block explorer.

1. Planning

parse_block function

  1. Get a block
  2. Split the block into transactions
  3. Split the transactions into operations
  4. Do something with that (store maybe?)
  5. Sleep with block interval
  6. Go to step1

This is a basic flow of a block parser. To listen all future blocks, let's expand the logic a little bit.

listen_blocks function

  1. Start an infinite loop (while True)
  2. Get a starting block ID
  3. Get the last irreversible block ID
  4. Get the difference and send each block id to parse_block function
  5. Sleep for block interval in seconds. (Currently 3)

That sounds good, right? That's the main flow of block explorers in the crypto-currency world. Let's implement that flow.

2. Implementation

parse_block function

Let's start with implementing parse_block function. It should get an integer which indicates the block height.

  • We will use steem.get_block() to get block details.
  • We will loop block["transactions"] to get related transaction
  • We will iterate transaction["operations"] to get all operations in transaction.

I will skip storing them because it's irrelavant to the subject. However, if you want to store the data somewhere, I suggest using mongodb since it will be easier to get things working in a fast way.

from steem import Steem
from pprint import pprint
s = Steem()
def parse_block(block_height):
    block = s.get_block(block_height)
    for transaction in block["transactions"]:
        for operation in transaction['operations']:
            operation_type, operation_data = operation[0:2]
            print("Operation: %s" % operation_type)
            pprint(operation_data)
parse_block(18260154)

This snippet will print each operation with it's name and it's raw data on block 18260154.


Note that, this is the simplest way of parsing a block. But normally:

  • You need to check get_block returns OK. Sometimes, nodes become unstable and you have timeouts. My practice for that to have a retry mechanism to get the data.

  • Blocks may have zero transactions. Especially if you parse old blocks, you will encounter them. So, checking a block actually has transactions is a must.

  • Every operation type has a different type of data inside it. Read operations.py to see a good list and how they're implemented as objects.

listen_blocks function

We need to have a stream of new blocks produced and pass them into get_block() to see the latest operations in the network.

def get_last_block_height():
    props = s.get_dynamic_global_properties()
    return props['last_irreversible_block_num']
def listen_blocks(starting_point=None):
    if not starting_point:
        starting_point = get_last_block_height()
    while True:
        while(get_last_block_height() - starting_point) > 0:
            starting_point += 1
            parse_block(starting_point)
        print("Sleeping for 3 seconds...")
        time.sleep(3)

This snippet has 3 steps.

  1. Get a starting block height. If it's not set, get the latest one.
  2. Parse the blocks between starting point and the last irreversible block.
  3. Sleep 3 seconds and go to step 2.

Enjoy your matrix themed console with STEEM transactions:

You can see the whole script here.


That's all for this post. Feel free to ask questions or request topics about steem-python for the incoming posts
.



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Qurator
Your Quality Content Curator
This post has been upvoted and given the stamp of authenticity by @qurator. To join the quality content creators and receive daily upvotes click here for more info.

Qurator's exclusive support bot is now live. For more info click HERE or send some SBD and your link to @qustodian to get even more support.

Hey @emrebeyler I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • You are generating more rewards than average for this category. Super!;)
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Birthdaypost !BEER



Hey @emrebeyler, here is a little bit of BEER from @isnochys for you. Enjoy it!