generated from dopt-python/py311-cython
prepare hashing
This commit is contained in:
parent
60794ddf2f
commit
61753f03d8
File diff suppressed because it is too large
Load Diff
@ -17,6 +17,7 @@ from polluck_blockchain.block cimport Block
|
||||
from libcpp.unordered_map cimport unordered_map
|
||||
from libcpp.string cimport string
|
||||
from libc.stdint cimport uint64_t
|
||||
from cython.operator import postincrement, dereference
|
||||
|
||||
ctypedef unsigned long ULong
|
||||
ctypedef unordered_map[uint64_t, Block*] BcHashmap
|
||||
@ -92,6 +93,12 @@ cdef class PyBlock:
|
||||
|
||||
return py_block
|
||||
|
||||
def __repr__(self):
|
||||
return f"PyBlock({self.index, self.timestamp, self.nonce, self.data})"
|
||||
|
||||
def __str__(self):
|
||||
return self.__repr__()
|
||||
|
||||
# cpdef _perform_hash(self):
|
||||
# parts = bytearray()
|
||||
# parts.extend(self._index.to_bytes(8, "big"))
|
||||
@ -164,15 +171,29 @@ cdef class Blockchain:
|
||||
def __init__(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
# TODO add entries of map
|
||||
# ownership is typically not transferred from the Blockchain extension class
|
||||
def __dealloc__(self):
|
||||
# ownership is typically not transferred from the Blockchain extension class
|
||||
cdef BcHashmap.iterator it = self._chain.begin()
|
||||
if self._chain is not NULL:
|
||||
while it != self._chain.end():
|
||||
del dereference(it).second
|
||||
postincrement(it)
|
||||
|
||||
del self._chain
|
||||
self._chain = NULL
|
||||
|
||||
def __len__(self):
|
||||
return self._index + 1
|
||||
|
||||
def print_key_value_pair(self):
|
||||
cdef BcHashmap.iterator it = self._chain.begin()
|
||||
cdef Block *block
|
||||
while it != self._chain.end():
|
||||
print(dereference(it).first)
|
||||
block = dereference(it).second
|
||||
py_block = PyBlock.from_ptr(block)
|
||||
print(py_block)
|
||||
postincrement(it)
|
||||
|
||||
@property
|
||||
def genesis_done(self):
|
||||
@ -207,9 +228,9 @@ cdef class Blockchain:
|
||||
"0".encode("UTF-8"),
|
||||
"".encode("UTF-8"),
|
||||
)
|
||||
block.hash = <string>"dummy hash".encode("UTF-8")
|
||||
block.hash = "dummy hash".encode("UTF-8")
|
||||
self.add_block(block)
|
||||
self._genesis_done = <bint>1
|
||||
self._genesis_done = True
|
||||
|
||||
def new_block(self, data):
|
||||
if not self._genesis_done:
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user