Greenshift 011: Finding the Most Recent Vote

in greenshift •  6 years ago 

Last time we were able to separate the votes of the author from the rest of the operations in the program that I wrote. However, the relationship we are trying to build requires a single piece of content rather than a list of the most recent votes. Also we need to consider cases where such a vote may not be in the most recent operations and in some cases may not be in any of the operations.

So, the following code attempts to address these issues and finish our relationship. However, there are a few key things to note when going over this initial solution. The first is that I'm by no means a perfect or even good programmer. I certainly do not know all of the tricks in the book. There is a good chance that a lot of the code I write may not have the best performance or be the most efficient. I'm still learning as is everyone else in the space, so if you think you have a better way, feel free to share and enlighten me.


Greenshift E1.png

So in the first half of the code, we create the API object like we did last time, but now we initialize two variables: result and cycles. Result will hold the final permlink and cycles will count the number of times we cycle through the while below.

The purpose of the while loop is simple. We continue to loop through the code as long as the result remains nil or we exceed a large number of cycles. In our case, we'll select 10 cycles. In the loop, we increment the number of cycles by one each time we loop and we'll grab the most recent operations from the blockchain. If after going through these operations, we can't find a vote, we will stay in the loop because result will still be nil.

In each loop, we expand the number of results we pull from the blockchain until we either hit the limit cycle or find a vote and grab a permlink associated with that vote. This method works, but is somewhat inefficient since we poll the blockchain multiple times and grab the same stuff multiple times. We can certainly improve on this design, but for now, this code works.


Greenshift E2.png

Once we grab results from the blockchain, we proceed with the filter we designed in the previous blog that only allows operations that are votes and results that are by different authors. If an operation passes this test, we store the permlink found in the operation in the result variable. This makes the variable non-nil and allows us to escape the while loop.

We then print the final result and will in the future simply return the result as part of a function. This concludes the initial development process of this relationship and we have four more to go. After developing our relationships, we then need to tie everything together within an application and design an interface that allows users to interact with the content and serves up the new content with the finished relationships.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  
  ·  6 years ago (edited)

You might consider streaming the blockchain instead.

https://www.rubydoc.info/gems/radiator/Radiator/Stream#operations-instance_method

Or are you trying to just peek at history to get the minimum amount back to find the latest vote, without asking for 10,000 results?

I'm just peeking at the history to get the most recent vote. Streaming gets live information from the blockchain, right? I don't think that will work since there's a chance that the author may not vote while the stream is live. I think though I will remove the loop and simply take a larger sample as that's probably more efficient than pinging the blockchain multiple times.

Yeah, that's basically what this tutoral does, except it's for comments instead of votes. But either op type would work:

https://developers.steem.io/tutorials-ruby/get_account_comments

If you look at the JavaScript version, they do get_state which is discouraged. While it's discouraged, it might be a more efficient API call at the moment. When hivemind goes live, these kinds of queries will probably be a lot more efficient.

At the moment, the best, non-descouraged-yet-still-efficient way is to use SteemSQL.

Cool. I like the way that's formatted. It's nicer than using indices just by passing that array as a block into another closure.

This post has been just added as new item to timeline of Q-Filter on Steem Projects.

If you want to be notified about new updates from this project, register on Steem Projects and add Q-Filter to your favorite projects.