Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
Preallocate attributes.
Args:
path: Path to folder containing .dats eg. "Blocks/"
fn: File name of .dat eg. "blk0000.dat"
verb: Control verbosity of printing. Level 2 (default)
prints Dat level updates (ie. not detailed block
or trans info.)
defer_printing: Don't print anything until block
n then print at level specified by verb.
**kwargs: Args to pass on to Block and Trans classes when used.
"""
# Increment Dat counter and remember which one this is
Dat._index += 1
self.index = Dat._index
self.f = f
self.path = path
self.mmap = None
self.length = 0
self.prepare_mem()
self.cursor = 0
self.blocks = {}
self.nBlock = -1
self.verb = verb
self.defer_printing = defer_printing
self.block_kwargs = kwargs
self.validateBlocks = kwargs.get('validateBlocks', True)
# -*- coding: utf-8 -*-
"""
Examples for using dat class
"""
# %% Imports
from pybit.py3.chain import Dat
# %% Read .dat
path = 'Blocks/'
f = 'blk00003.dat'
dat = Dat(path, f,
verb=1)
# Read the block
dat.read_next_block()
# Verify it's correct (this may already have been done on import)
dat.blocks[0].api_verify()
# Output block data as dict
dat.blocks[0].to_dict()
# %% Read another 10 blocks and export
# Read block
dat.verb = 1
def readDat(self, datn: int) -> Dat:
"""
Read block specified by file number.
Args:
datn: Integer identifier for file. Used to generate name.
eg. 1 -> "blk0001.dat".
"""
fn = "blk{0:05d}.dat".format(datn)
if self.verb >= 1:
print(fn)
d = Dat(path=self.datPath,
f=fn,
verb=self.verb,
**self.dat_kwargs)
self.datni += 1
return d
# For now:
# Save dat contents to Chain (dats ordered, blocks not)
print(d)
self.dats[d.index] = d
if __name__ == "__main__":
"""
Examples and tests.
"""
# %% Load .dat
path = 'Blocks/'
f = 'blk00001.dat'
dat = Dat(path, f,
verb=6)
# Read first block
dat.read_next_block()
# Verify it's correct (this may already have been done on import)
dat.blocks[0].api_verify()
# Output block data as dict
dat.blocks[0].to_dict()
# %% Read another 10 blocks and export
# Read block
dat.read_next_block(10)
"""
# %% Imports
import pandas as pd
import matplotlib.pyplot as plt
import pickle
from pybit.py3.chain import Dat
# %% Read a few blocks
f = 'Blocks/blk00000.dat'
dat = Dat(f,
verb=4)
dat.read_next_block(500)
# %% Get one block
block = dat.blocks[0]
block._print()
# %% Convert block to dict
def to_dict(block,
keys=['hash', 'start', 'end', 'blockSize', 'version', 'prevHash',
class ChainMap(Chain):
def readDat(self, datn: int) -> Dat:
fn = "blk{0:05d}.dat".format(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,
n: int=1,
tqdm_on=True) -> None:
"""
Read and return the next block.
Track cursor position.
"""
# If verb is low,
# tqdm is not specifically turned off,
# and available.
if tqdm_on:
# Note if tqdm isn't available, it'll use the placeholder
# function which does nothing
tqdm_runner = tqdm
else:
# 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