Simple Hive App on a $7 SBC 1: Setup and Reading a Block

in #programming4 years ago

When I first saw the Orange Pi i96, a $7 single-board computer, I thought to myself, "Maybe I could get a simple Hive app working on that thing.." After all, it's pretty easy to run things on Hive, and I already have most of the code I'd need. So let's see what this thing can do!

Setup

This was not simple or easy, and it's the main reason not to buy one of these things (the more mainstream Orange Pi boards have better support). This isn't meant to be a tutorial, so I won't go into much detail here. Suffice to say it took a lot of patience, trial and error, and digging around the internet.
I did eventually get Python 3.8 working with all the necessary libraries.

First Code: Reading a Hive Block

One of the most basic things a Hive app needs to be able to do is read transactions from the chain and respond to them accordingly. Fortunately, this is very simple to test with just a few lines of code:

import requests

def get_block(block):
    data = {'jsonrpc':'2.0', 'method':'condenser_api.get_block', 'params':[block], 'id':1}
    with session.post('https://api.deathwing.me', json=data) as r:
        return r.json().get('result')

def main():
    block = 52000000
    global session
    with requests.Session() as session:
        b = get_block(block)
        print(f'block: {block}')
        print(f"timestamp: {b['timestamp']}")
        print(f"witness: {b['witness']}")
        print(f"transaction count: {len(b['transactions'])}")

if __name__ == '__main__':
    main()

Obviously there are a few things in there you wouldn't want in a real app, but I think it's a good enough start.
Let's see if it works..

Success!

Next Steps

The next challenge is to sign a transaction, which will involve a bit more code. Then we can start designing and building the app.

Let me know what you think the app should actually do when it's further along.