Skip to content

Electrum Hemis: porting Electrum

tafkat edited this page May 28, 2024 · 1 revision

Porting Electrum for Hemis is far from straightforward. This article is based on a blog post by ahmedbodi, who was instrumental in getting Electrum to work for Hemis. The original blog post can be found here: https://it4f.co.uk/2024/05/05/porting-electrum-to-hemis/

Porting Electrum to Hemis

Hemis has a few problems which made this task, much harder than originally thought. Namely, proof-of-stake. The way POS is calculated here, makes validating the pos block hash impossible which is why some coins e.g. Qtum have opted to store more data in the block headers to allow for this. Hemis is not one of those.

For that reason, any POS blocks cant have their targets validated so this does mean a bit more trust in the electrumx servers.

Second of all, the algorithm. As is common in the world. People come, people go. This is more common than usual in the crypto world and it means many of the open source maintainers dont update their libraries to work with the changes that happen over time, in this case Python 2 to Python 3.

This resulted in a headache of its own. My solution for this was to do the same and give my AlgoLib library some love and make it compatible with builds on Python 3 and dropping support for Python 2.

AlgoLib is a Cython based project which incorporates over 10 common hashing algorithms for usage in Python based projects. I needed to fix a number of compile issues that had cropped up due to the switch over, along with rebuild this to work with Python 3, and the new packaging changes.

Along came these awesome tools to help get this done:

  • cibuildwheel

This is an awesome build tool that uses docker containers to provide a generic environment and tooling to allow for wheels built to be compatible with many os’s including: Windows, OSX and Linux for various architectures. Important for us since electrum is cross platform

  • Github Actions

Who doesn’t love fast builds on many architectures?

And finally, the headache of the issue. Hemis has varying block structures in the early blocks which raises an issue of its own. Electrum’s standard protocol sends a chunk of blocks as a single combined hex string so how are we to determine what the header size is?

Getting around the problem, ElectrumX

This commit was the changes i needed to make to allow ElectrumX to gather a list of blocks and send them individually to electrum for processing. This way it can process the blocks one by one without caring about the size of them. It can handle that per block itself based on the version.

Once we got this done, the only thing we needed to tackle was, how do we save the blocks because the old structure of static headers in a file was now incompatible with what we’ve done?

This link is the changes we had to make to complete the port, showing how we adopted the sqlite3 database that was open sourced by the awesome Qtum team for their electrum.

Once we had that imported into the codebase, it was just a case of fixing builds to add in the new algorithm library, doing the renames and then came the profit!