Skip to content

Made pchMessageStart variable so it works with altcoins #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

priestc
Copy link

@priestc priestc commented Apr 22, 2017

The variable name pchMessageStart comes from the variable name in the bitcoin C++ code:

https://github.com/dashpay/dash/blob/master/src/chainparams.cpp#L115
https://github.com/dogecoin/dogecoin/blob/master/src/chainparams.cpp#L82

This allows this library to work on altcoins.

To use with litecoin:

>>> ltc=Blockchain('/media/chris/3033-6537/litecoin/blocks', b"\xfb\xc0\xb6\xdb")
>>> next(ltc.get_unordered_blocks())
Block(12a765e31ffd4059bada1e25190f6e98c99d9714d334efa41a195a7e7e04bfe2)

To use with bitcoin:

>>> btc=Blockchain('/home/chris/.bitcoin/blocks/')
>>> next(btc.get_unordered_blocks())
Block(000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f)

and dash:

>>> dash=Blockchain('/media/chris/3033-6537/dash/blocks', b"\xbf\x0c\x6b\xbd")
>>> next(dash.get_unordered_blocks())
Block(089fc444b06edd0f70d9fda85f9a3b2e22e549b354a1bdb210ce7804c69eb0a4)

@alecalve
Copy link
Owner

Thanks for your contribution!
The code looks good to me, I'll merge it once you revert the changes to the README

@alecalve alecalve self-assigned this Sep 30, 2017
Copy link

@sembrestels sembrestels left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave the BITCOIN_CONSTANT constant.

@@ -16,10 +16,6 @@
from .block import Block


# Constant separating blocks in the .blk files
BITCOIN_CONSTANT = b"\xf9\xbe\xb4\xd9"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would leave this constat here.

@@ -31,7 +27,7 @@ def get_files(path):
return sorted(files)


def get_blocks(blockfile):
def get_blocks(blockfile, pchMessageStart=b"\xf9\xbe\xb4\xd9"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would write pchMessageStart=BITCOIN_CONSTANT here.

@@ -63,13 +59,14 @@ class Blockchain(object):
maintained by bitcoind.
"""

def __init__(self, path):
def __init__(self, path, pchMessageStart=b"\xf9\xbe\xb4\xd9"):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would write pchMessageStart=BITCOIN_CONSTANT here.

@brannondorsey
Copy link
Contributor

brannondorsey commented Feb 26, 2018

I agree with @sembrestels that it would be nice to have a constant variable that equals the Bitcoin header. It would also be nice if there were also a few altcoin headers that we included for convenience as well. Perhaps:

blockchain.BITCOIN_CONSTANT
blockchain.LITECOIN_CONSTANT
blockchain.DASH_CONSTANT

Or perhaps there is a better name to expose to the user. What does the "pch" in pchMessageStart stand for?

@alecalve
Copy link
Owner

I think pch is some bitcoind C++ jargon. I agree with providing some defaults one (maybe including testnet and BCH)

@alecalve
Copy link
Owner

Note that supporting altcoins would also include changing the version byte for addresses.

@brannondorsey
Copy link
Contributor

It might also make sense to add this functionality in the Blockchain() constructor. Something like:

# not sure if this is the real litecoin directory name, but you get the idea
# not sure what the best name is for "block_separator". blockchain? type?
blockchain = Blockchain('~/.litecoin/blocks', block_seperator=blockchain.LITECOIN)

@alecalve
Copy link
Owner

We could also do it à la bitcoinj where a params object is used which encapsulates all the variable parameters for a given chain (genesis block, separator, version bytes for addresses, etc..).

@brannondorsey
Copy link
Contributor

Yeah, that is nice. Makes it clean and a bit more extensible for blockchains that potentially don't exist yet. We could have a few pre-filled objects for common coins and networks that a user could use by default by passing them along in the constructor via a named parameter e.g. blockchain=blockchain.LITECOIN. But then if a user wanted to parse a new blockchain it would be as simple creating an object with all of the right properties.

@brannondorsey
Copy link
Contributor

Magic number seems to be the appropriate name for the constant block separator. For what it's worth ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants