Wow! There are a lot more missed blocks on the blockchain than I thought there would be. Just from now to last night there have been 10 missed blocks!
Why does this matter? Well, due to Steemit Inc's incompetence I'm creating a hack that lets you retrieve a block using a date.
var BLOCKS_PER_SECOND = 3 var ANCHOR_BLOCK = 26471727 var ANCHOR_DATE = new Date("2018-10-03T02:24:54"+"Z")
function getBlockNum(dateTime){ dateTime += 'Z' // UTC conversion. var date = new Date(dateTime) var deltaBlocks = (date - ANCHOR_DATE) / 1000 / BLOCKS_PER_SECOND var block_num = ANCHOR_BLOCK + deltaBlocks return block_num }
You see, I'm using an anchor block (one that we know the values for) to extrapolate how many blocks we need to go from the anchor block to get to the block we are looking for. However, those pesky missed blocks are quite annoying! This code would work if there were no missed blocks. It would also work if, when we missed a block, we skipped that block number as well. Alas, this is not the case.
Fun fact: JavaScript Dates are stored as milliseconds before/after January 1, 1970. This means that today's date is 1,538,585,316,458
. Which means it's only been a trillion and a half milliseconds from 1970. This number, of course, changes on a millisecond to millisecond basis.
When I first started writing this code it was 50 lines long. I was trying to do all the time conversions and math myself. Then I hit a wall when I was supposed to figure out the month logic [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
It was at this point that I realized there was a 99% chance I was wasting my time and that JavaScript already had built in functions for Date
objects. I was right 😜. Not only that, Steemit Inc's database API stores the dates in the correct format; I only had to add a 'Z' at the end to let JavaScript know the times where in UTC format and not my local time zone PST.
var ANCHOR_DATE = new Date("2018-10-03T02:24:54"+"Z")
In any case, I'll probably have to figure out JavaScript promises now. I probably also should figure out cookies. I can use the cookies to store where the missed blocks are, and I can use the promises to get the block information I need from Steemit Inc. It's basically a guess and check scenario, where I guess where the block should be and then confirm if I got it or not. If I didn't get it I guess again. The second guess is much more accurate so I doubt it will require more than that 99% of the time.
Again, this is ridiculous. Steemit Inc should be embarrassed that they haven't provided basic functionality in their API. It's seriously like it was programmed by children. On that note, I'm getting old. 'Kids' in their early twenties now look like they are 14 to me. Everything is relative I guess.
Return from Missed Blocks to edicted's Web3 Blog