How to use the pybit.py2.chain.Dat function in pybit

To help you get started, we’ve selected a few pybit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github garethjns / PyBC / pybit / Examples / Py2_HashBlock.py View on Github external
"""

    # Collect header hex
    header = block._version \
            + block._prevHash \
            + block._merkleRootHash \
            + block._timestamp \
            + block._nBits \
            + block._nonce

    return header


# Import first block
f = 'Blocks/blk00000.dat'
dat = Dat(f)
dat.read_next_block()

# Prep the header
prepped_header = prep_header(dat.blocks[0])

# This should now match the header as loaded in the example above:
assert prepped_header == gbHeader

# Hash
hash256_twice(prepped_header)
github garethjns / PyBC / pybit / py2 / chain.py View on Github external
def readDat(self, datn):
        f = "{0}blk{1:05d}.dat".format(self.datPath, datn)

        if self.verb >= 1:
            print f

        dat = Dat(f,
                  verb=self.verb)
        self.datni += 1
        return dat
github garethjns / PyBC / pybit / Examples / Py2_BlockchainInfoAPI.py View on Github external
# The the overall result
    result = t1 & t2 & t3 & t4

    # Report
    if pr:
        if result:
            print "Pass"
        else:
            print "Fail"

    return lastTime, result


# Load a block
f = 'Blocks/blk00000.dat'
dat = Dat(f,
          verb=5)

dat.read_next_block()
block = dat.blocks[0]

# Reset timer
lastTime = 0
# Query using this block hash
lastTime, result = block_validate(block)

# Run same query again to test wait timer
print "\n"
lastTime, result = block_validate(block, lastTime)


# %% Transaction validation
github garethjns / PyBC / pybit / py2 / chain_map.py View on Github external
# %% Higher level classes

class ChainMap(Chain):
    def readDat(self, datn):
        f = "{0}blk{1:05d}.dat".format(self.datPath, datn)

        if self.verb >= 1:
            print f

        dat = DatMap(f,
                     verb=self.verb)
        self.datni += 1
        return dat


class DatMap(Dat):
    def read_next_block(self):
        """
        Read and return the next block
        Track cursor position
        """
        b = BlockMap(self.mmap, self.cursor,
                     verb=self.verb,
                     f=self.f)
        self.cursor = b.end

        self.nBlock += 1

        # Save block dat object - unordered at this point
        self.blocks[self.nBlock] = b

        if self.verb == 2:
github garethjns / PyBC / pybit / Examples / Py2_HashTransaction.py View on Github external
# TODO:
    - Prep transaction header
    - Hash
"""

# %% Imports

from pybit.py2.utils import hash_SHA256_twice
from pybit.py2.chain import Dat


# %% Get a transaction

f = 'Blocks/blk00000.dat'
dat = Dat(f,
          verb=5)

# Read a block
dat.read_next_block()

# Get the first transaction from the gensis block
trans = dat.blocks[0].trans[0]
trans._print()


# %% Prepare header

header = trans._version \
        + trans._nInputs \
        + trans.txIn[0]._prevOutput \
        + trans.txIn[0]._prevIndex \
github garethjns / PyBC / pybit / py2 / chain.py View on Github external
nBlock = 0
        while self.cursor < len(self.mmap):
            self.read_next_block()
            nBlock += 1

        if self.verb >= 2:
            print "\nRead {0} blocks".format(nBlock)


if __name__ == "__main__":
    ""

    # %% Load .dat

    f = 'Blocks/blk00000.dat'
    dat = Dat(f,
              verb=5)

    # %% Read next block

    # Read the block
    dat.read_next_block()

    # Verify it's correct (this may already have been done on import)
    dat.blocks[0].api_verify()

    # %% Print example transaction

    dat.blocks[0].trans[0]._print()

    # %% Verify it's correct