Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
"""
# 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)
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
# 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
# %% 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:
# 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 \
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