Beispiel #1
0
def test_internal_order():
	# test value for conversion correctness
	assert internal_order(3) == b'\x03\x00\x00\x00'
	assert int.from_bytes(b'\x03\x00\x00\x00', 'little') == 3

	# test byte size
	assert len(internal_order(3)) == 4
Beispiel #2
0
    def mine(self):
        """
        Since NamedTuples are immutable, we need to return a new block as _replace really returns a new version of 
        the object
        """

        # clears the mine_interrupt Event, sets it
        from chainmanager import ChainManager
        ChainManager.mine_interrupt.clear()

        start = time.time()
        nonce = 0
        target = self.target

        # if we've explored all possible uint32, we can change either timestamp or transactions (merkle hash)
        template = self._base_hash
        while int(sha256d_hexdigest(template + internal_order(nonce)),
                  16) >= target:
            nonce += 1
            if nonce % 1000000 == 0 and ChainManager.mine_interrupt.is_set():
                logger.info(f'sanity check: {nonce}')

        new_block = self._replace(nonce=nonce)

        # In case we find the nonce right away
        duration = int(time.time() - start) or 0.001
        khs = (new_block.nonce // duration) // 1000
        logger.info(
            f'[mining] block found! {duration} s - {khs} KH/s - {new_block.id}'
        )

        return new_block
Beispiel #3
0
    def _base_hash(self) -> bytes:
        field_bytes = []
        fields = list(self._fields)
        fields.remove('nonce')

        for field in fields:
            raw_attr = getattr(self, field)
            cleaned_attr = internal_order(
                raw_attr) if raw_attr != None else b'\00' * 32
            field_bytes.append(cleaned_attr)
        return b''.join(field_bytes)
Beispiel #4
0
    def _base_hash(self) -> bytes:
        field_bytes = []
        fields = list(self._fields)

        # we do not need to byte order nonce or txns
        fields.remove('nonce')
        fields.remove('txns')

        for field in fields:
            raw_attr = getattr(self, field)
            cleaned_attr = (isinstance(raw_attr, bytes) and raw_attr
                            or internal_order(raw_attr)
                            ) if raw_attr != None else b'\00' * 32
            field_bytes.append(cleaned_attr)
        return b''.join(field_bytes)
Beispiel #5
0
 def header_hash(self) -> bytes:
     return self._base_hash + internal_order(self.nonce)