dsteem Tutorial Lesson 6: The Power of BlockStream()

in #utopian-io5 years ago (edited)

Repository

https://github.com/jnordberg/dsteem

You will learn how to:

  • Set up a block/operation stream without the complications of Node.JS
  • Filter content of interest.
  • Take a look at games on the Steem blockchain (custom JSON operations)

Requirements

  • HTML
  • JavaScript

Difficulty

  • Basic

Curriculum

Lesson 0: Steem Tutorials Are Severely Lacking
Lesson 1: Filtering Tabs
Lesson 2: Hello World
Lesson 3: Dissecting Discussions
Lesson 4: Steem Stake Sterilizer
Lesson 5: Inspecting Block Operations

Proof-of-Brain:

https://gist.github.com/edict3d/2ee3f858d42b9d478c1cab6422e9443d

tutorial-keyboard-dsteem-utopian-io.jpg

Here is the easiest way to set up a blockchain data stream from Steemit Inc:

<script src="https://unpkg.com/dsteem@^0.10.0/dist/dsteem.js"></script>

<button id="on"> On </button>

<script>

var client = new dsteem.Client('https://api.steemit.com')
var stream = client.blockchain.getOperationsStream()

function filter(operation){
    console.log(operation);
}

function on(){
    stream.on('data', (operation) => filter(operation))
}

document.getElementById("on").onclick = on

</script>
Use client.blockchain.getBlockStream() to get the full block instead of just operations

steemit_blockstream.png

And there you have it! A real time stream of all the data being logged to the blockchain.

It really is that easy.
Now we can filter this information into something useful.


filter.jpg

Personally I think the most interesting thing to look at is custom JSON operations.
<script src="https://unpkg.com/dsteem@^0.10.0/dist/dsteem.js"></script>

<br><button id="on"> On </button><br>
<br><button id="pause"> Pause </button><br>
<br><button id="resume"> Resume </button><br>
<h1>Output:</h1>
<p id='output'></p>

<script>

var client = new dsteem.Client('https://api.steemit.com')
var stream = client.blockchain.getOperationsStream()

function filter(operation){
    let op = operation.op
    if(op[0] === 'custom_json'){
        let data = op[1]
        document.getElementById("output").innerHTML += data.id + '<br>'
        document.getElementById("output").innerHTML += data.json + '<br>'
    }
}

function on(){
    stream.on('data', (operation) => filter(operation))
}

function pause(){
    stream.pause()
}

function resume(){
    stream.resume()
}

document.getElementById("on").onclick = on
document.getElementById("pause").onclick = pause
document.getElementById("resume").onclick = resume

</script>

blockstream-json-ops.png


It becomes obviously apparent that the vast majority of activity is eclipsed by @drugwars and @steemmonsters.

Let's say we wanted to filter this even further to see only @drugwars accounts attacking other @drugwars accounts. No problem:


function filter(operation){
    let op = operation.op
    let data = op[1]
    if(op[0] === 'custom_json' && data.id === 'drugwars'){
        document.getElementById("output").innerHTML += data.id   + '<br>'
                                                     + data.json + '<br><br>'
    }
}

blockstream-json-ops-drugwars-filter.png

We could then use this information to find weak/undefended accounts.

We could also use this information to capitalize on two users fighting each other resulting in a draw, which would then allow us to swoop in and take the reward for ourselves.

It would be even easier to set up alerts that would play a sound anytime someone attacked your account or any other accounts on your whitelist. Considering the lag and delay of the @drugwars servers, you would get tipped off much faster by simply streaming the block operations manually like this. All you'd have to do is set an alert if the "target": was your account.

It also wouldn't be that difficult to set up a script that detected an attack against your account and in response dumped all your drugs into the heist, purchased units with the rest of your available resources, and then attacked a dummy account with no defense so that you wouldn't lose any units.

However, all of these suggestions are far beyond the scope of this tutorial.


blockstream-json-structure.png

Operation object

Now let's take a look at the actual object given to us by Steemit's API. We can see that they give us an object with several attributes. The most important attribute is op. op is an array with two values in it.

  1. The type of operation (in this case custom_json)
  2. The data structure of the operation

The data structure, in turn, is an object containing the attributes id and json, which is the bulk of the information we are looking for. It also contains required_posting_auths, letting us know who posted this JSON to the blockchain in the first place.

Seems a little convoluted to me personally, but I'm sure Steemit has their reasons. It is what it is.

blockstream-json-structure2.png

The contents of other operations is a little different, but they all seem to use op with an array of length two.

blockstream-json-structure3.png

Conclusion

While the concept of block streaming is simple and the code provided here is sparse, the utility of being able to see everything happening on the blockchain in real time is quite invaluable. Also, the documentation on block streaming is quite poor. I was not able to find the information I needed from the Steemit API docs or from the dsteem repository itself.

I believe we can create dapps where we don't have to run our own servers. Instead, we can rely on witnesses to create the blocks and full-nodes to distribute the information. Hopefully in the future we see a scaling solution using a torrent-like block distribution that takes the load off of full-nodes.

As we continue to grind forward in our journey, it becomes obvious that a scalable way to distribute blocks will be pinnacle to the expansion of the Steem blockchain.

Sort:  

Thank you for your contribution @edicted.
After reviewing your tutorial we suggest the following points listed below:

  • We suggest you put comments in your code. We consider this important because the comments are helpful enough for less experienced readers.

  • Put your code clean without commented code lines. It can confuse less experienced readers.

comments.png

  • This tutorial has some good tips!

  • Simple and well structured tutorial, thank you for your work in developing this contribution.

Looking forward to your upcoming tutorials.

Your contribution has been evaluated according to Utopian policies and 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.


Need help? Chat with us on Discord.

[utopian-moderator]

Thank you for your review, @portugalcoin! Keep up the good work!

This is a great resource. What is the latency with this solution? For example how much time for a comment like this one to appear on the stream?

Posted using Partiko Android

I believe this code is importing immutable blocks only.

https://steemd.com/

looks like it takes 15 blocks deep to become permanent.
i assume they are coming in 45 sec late.

https://jnordberg.github.io/dsteem/classes/blockchain.html#getoperationsstream

https://jnordberg.github.io/dsteem/interfaces/blockchainstreamoptions.html#mode

Streaming mode, if set to Latest may include blocks that are not applied to the final chain. Defaults to Irreversible.

Thanks for the detailed answer!

Great tut!

Looking forward to the drugwars bot.
Have you heard that the drugwars team is planning to ban bots?

Posted using Partiko Android

They can try.
:D

They need to make it a lot harder to extract information from the site.

The bots will fail if they don't know what defenses the enemy has.

Still, I can't image that they would stop using custom JSON to issue commands,
but I'm sure that's within the realm of possibility.

Hi @edicted!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
Feel free to join our @steem-ua Discord server

Hey, @edicted!

Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Get higher incentives and support Utopian.io!
Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via SteemPlus or Steeditor).

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

Vote for Utopian Witness!

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.034
BTC 63688.35
ETH 3125.30
USDT 1.00
SBD 3.97