2

So I'm trying to read the block files with Python.

From this link, What are the keys used in the blockchain levelDB (ie what are the key:value pairs)?, it tells you how to access the files with leveldb. So I wrote a class to read in the meta data.

class ReadBytes:
    def __init__(self, bytes):
        self.posistion = 0
        self.bytes = bytes

    def get_int(self, byte_count):
        self.posistion += byte_count
        return decode(self.bytes[self.posistion - byte_count:self.posistion][::-1], 256)

    def get_var_int(self):
        self.posistion += 1
        value = byte_to_int(self.bytes[self.posistion - 1])
        if value < 253:
            return value
        return self.get_int(pow(2, value - 252))

But from this link, https://github.com/bitcoin/bitcoin/blob/master/src/chain.h#L364, It turns out there is this thing call VarIntMode::NONNEGATIVE_SIGNED that is tripping me up. I can read regular var int with my Python class, but how do I read the non-negative signed ones in Python?

0

A non-negative signed varint is just a normal varint but should be put into a signed integer rather than an unsigned integer as with normal varints. You just need to also check that it is a non-negative integer.

For example, instead of decoding a 32 bit varint as a uint32_t, you decode it as int32_t and check that it's value is >= 0.


Your code looks like it is decoding things as signed integers already. Instead of using Python's conversions built into the various classes, I recommend that you use the struct module so that you can specify endianness and signedness of the bytes that you are decoding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.