Hive Stream: (Update) Support for writing custom contracts on the Hive blockchain

in HiveDevs4 years ago (edited)

Whilst building dApps on the Hive blockchain, you'll inevitably want to react to specific actions users perform. If you are familiar with Hive Engine (or Steem Engine) then you might be familiar with how it watches for custom JSON operations on the blockchain.

Screen Shot 20200402 at 11.31.20 pm.png

After trying to set up and run my own node, I realised that that the Hive Engine node software is limited in that you can't do anything with balances or create anything complicated. I already had my Hive Stream library, so I set out to create a rudimentary contracts system within Hive Stream.

One of my favourite things about the way Hive Engine's smart node software works is the ability to write "smart contracts" using Javascript. Basically, the ability to define "actions" which are matched to an action value inside of a custom JSON payload. I wanted to replicate that same approach, but make it even easier (no Base64 encoding required).

At its core, it's basically just watching for custom JSON operations with a specific property inside of the JSON object on the chain called hiveContract. An example contract can be seen here for an imaginary dice game use Hive Stream. And in this runnable test file here you can see how contracts are registered. Consult the README for details on how to use the library.

I guess you could describe it as more of a reactive blockchain library, looking for specific properties and matching those to actions. It takes a lot of the work out of needing to write this stuff manually yourself. Why reinvent the wheel? Furthermore, the library now has failover support, if an API node goes down, it'll try one of the other specific API nodes until it works or completely fails. It supports custom JSON operations as well as transfers with custom JSON in the memo which allows you to react to transfers (dice games, deposits, etc).

In the coming weeks, a more standardised approach to using this library to build decentralised applications on the Hive blockchain is coming, but for now, it should do most of the things dApp authors would want to do. I have yet to completely test all of this extensively (however, it is well tested from a unit test perspective), so exercise caution when using this library and feel free to provide feedback and contributions on the repository. I will have a tutorial coming shortly showing you how to create a dice game using this library.

Sort:  

This is really neat. I can see how there are many ways to apply this contracts concept.

I was wondering about how STEEM-Engine works. Is there ever an issue with missed transactions? Such as an exception or server outage. Is this a non-issue or is it handled by rewinding/replaying the missed blocks?

Steem Engine node keeps track of where it was last up too. So if there is an error, the client would fail and then try using a different API node. If it completely fails, the block number remains and it can continue off later on if it needs to be restarted. Because it also uses MongoDB backing it, it keeps track of everything there too, so it can be replayed. Hive Stream keeps track of where you're up too by incrementing the block number in a file instead.

Thanks for the detailed explanation of how these apps maintain state.

Are the contracts happening on a side chain or on the Hive chain?

They're happening on the Hive chain. Unlike say Ethereum which supports publishing contracts that live on the blockchain, this is more of a code contract that lives on your server when you have the streamer running. So, it's not anything running on the chain itself, it's more akin to running a side chain that watches for custom JSON. Basically, it just watches the Hive blockchain and then matches JSON property names and actions to your contract actions. I have a tutorial coming for how to create a dice game using it hopefully today.

so why would anyone use this library instead of steemjs or similar?How is it any better?