Author

Topic: Get specific blockheight's hash with client RPC? (Read 98 times)

HCP
legendary
Activity: 2086
Merit: 4361
November 05, 2021, 12:29:00 AM
#2
Have you considered submitting a pull request to the Electrum github so that others can benefit from your Superstar self's awesomeness? Wink
newbie
Activity: 5
Merit: 43
I need an end point that could be used to get the block hash of a specific blockheight using the RPC calls from the the Electrum client.  Currently "getinfo" only lists the block height and NOT the block hash and it also doesnt get info on a specific block height.  Is modifying getinfo with block height as an argument as well as adding the block hash to it's results the only option or is there another endpoint that will work?  With the client,  none of the "protocol specification" endpoints work such as "blockchain.block.get_header" as listed here: https://electrum.readthedocs.io/en/latest/protocol.html (I believe this is docs for a server and not a client,  client doesnt seem to have much for docs regarding RPC endpoints other than --help commands)   I tried them all,  none of them work.  But any of the commands in "--help" work great with RPC.  Even if I need to copy/paste some sort of modification to the "gitinfo" command,  that would be cool.  I just dont know a single thing about python let alone electrum's implementation of it.  I am however a PHP dev,  so I could probably stumble through it and get it working after an exorbitant amount for such a simple item that someone more knowledgeable could crank out in 30 seconds in a post here Smiley

EDIT:   Been doing some poking around and I think I found the function I need in the "Blockchain" class but I dont have a clue in how to use it in the "Command" class.  As a test I tried to modify the "getinfo()" method but my lack of Python experience is not going to allow this to be an easy fix.  I could really use some python help to add a "get_hash" function in the Command class.  This is what I have so far:

Code:
//At the top of command.py I added this which I stole from the "network" file that also seems to use the get_hash() function  I have no idea if this is the right way to import and initiate a class so it's methods can be used
from . import blockchain
from .blockchain import Blockchain, HEADER_SIZE

//Then in the at the top of the command Class definition right below self.network = network I added this.  I assume it extends the command class with the Blockchain class?  Thats how it works in PHP anyway
self.blockchain = Blockchain

//Then in the get_info() function below net_params = self.network.get_parameters() I added this,  but I dont know why "blockchain" is a needed argument since the function itself only has "height" as the argument
height = 707983
header_hash = self.blockchain.get_hash(blockchain, height)

//And finally I added this to the results portion right below 'blockchain_height': self.network.get_local_height(),
'header_hash_for_707983' : header_hash,

The result it displays is:
Work-VirtualBox /test $ killall -9 python3 ; electrum daemon -d ; electrum getinfo
starting daemon (PID 6784)
module 'electrum.blockchain' has no attribute 'read_header'

From the above, I am guestimating that I'm still not initiating the "Blochchain" class correctly since it's looking for methods that dont seem to exist.

I opted to add it to the getinfo() function first because it's a working command with working output.  Once I get it working,  I'll attempt to make a NEW command called "get_block_hash(blockheight)"

Where are all the python people?  Tongue  I can see why people hate python,  it looks like an utterly HORRID language.


EDIT 2:

Well I ended up figuring this out myself.  I had suspiciouns that it was the actual Class initiation that wasnt correct so I did more digging and I found the parameters it needed in order to be initiated.  This is what finally got it working,  I had to replace the mod I put in:   self.blockchain = Blockchain    was missing the class parameters that I added below.  Once I did that,  the function started returning the right block hashes.  The forkpoint_hash is the BTC Genesis block,  it's defined as a constant somewhere,  once I find out how to use that,  I'll replace the string with the constant.  Now begins the command creatiion.  But for others looking for this same feature,  this post should be enough to get you going if you're proficient in at least 1 language somewhere.  Python is utter garbage.

Code:
self.blockchain = Blockchain(config=config,
                            forkpoint=0,
                            parent=None,
                            forkpoint_hash='000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f',
                            prev_hash=None)

EDIT 3
This is the new command and it works great now.   Thanks self.  You are a superstar Tongue
Code:
@command('n')
    async def get_block_hash(self, height):
        """Get block hash"""
        header_hash = self.blockchain.get_hash(height)
        response = { 'block_hash': header_hash, }
        return response
Jump to: