def is_genesis(self) -> bool:
        recipient = wallet.get_public_key(0).dumpb()
        if self.recipient != recipient:
            return False
        amount = 100 * util.get_nodes()
        if self.amount != amount:
            return False
        if self.inputs != [TransactionInput(b"0", 0)]:
            return False
        if self.sender != b"0":
            return False
        if len(self.outputs) != 2:
            return False
        out0 = self.outputs[0]
        if not (out0.index == 0 and \
                out0.recipient == recipient and \
                out0.amount == amount):
            return False
        out1 = self.outputs[1]
        if not (out1.index == 1 and \
                out1.recipient == b"0" and \
                out1.amount == 0):
            return False
        if self.id != self.hash():
            return False
        if self.signature != b"0":
            return False

        return True
def test_util():
    r = util.get_db()
    r.flushdb()

    util.set_nodes(NODES)
    assert util.get_nodes() == NODES
    util.set_node_id(NODE_ID)
    assert util.get_node_id() == NODE_ID
    peer_ids = util.get_peer_ids()
    assert len(peer_ids) == NODES - 1
    assert NODE_ID not in peer_ids
 def genesis() -> 'Transaction':
     recipient = wallet.get_public_key(0).dumpb()
     amount = 100 * util.get_nodes()
     gt = Transaction(recipient=recipient,
                      amount=amount,
                      inputs=[TransactionInput(b"0", 0)],
                      input_amount=amount)
     gt.sender = b"0"
     gt.outputs = [
         TransactionOutput(0, recipient, amount),
         TransactionOutput(1, b"0", 0)
     ]
     gt.id = gt.hash()
     gt.signature = b"0"
     return gt
Beispiel #4
0
def getTotalNodes():
    return util.get_nodes()
Beispiel #5
0
        if _check_difficulty(digest, DIFFICULTY):
            logging.debug(
                "Miner %d finished (nonce %d, current %s, mining time %f)",
                os.getpid(), nonce, util.bintos(digest),
                time.time() - tstart)
            b.nonce = nonce
            b.current_hash = digest
            if ECHO:
                print(b.dumps())
            else:
                logging.debug("Miner %d broadcasting", os.getpid())
                # NOTE: Yes, we send the block back to our own node through the
                # network stack. This is the dumbest, slowest, but also the most
                # robust way for now
                # NOTE: We do a blocking broadcast, because otherwise the
                # process will just terminate before actually making the
                # requests (the requests are still sent concurrently). Since
                # the listener will respond without blocking, this shouldn't be
                # too big of a problem
                chatter.broadcast_block(b,
                                        list(range(util.get_nodes())),
                                        blocking=True)
            r = util.get_db()
            with r.lock("blockchain:miner_pid:lock"):
                r.delete("blockchain:miner_pid")
            logging.debug("Miner %d cleared", os.getpid())

            break
        else:
            nonce += 1