Esempio n. 1
0
    def setUp(self):
        self.mock_blockchain = Mock(Blockchain)
        self.mock_mempool = Mock(Mempool)
        self.HOST = '123.456.789.012'
        self.REWARD_ADDRESS = 'RewardAddress'
        self.MAX_TRANSACTIONS_PER_BLOCK = 10

        self.subject = Miner(self.mock_blockchain, self.mock_mempool)
        self.subject.HOST = self.HOST
        self.subject.MAX_TRANSACTIONS_PER_BLOCK = self.MAX_TRANSACTIONS_PER_BLOCK
        self.subject.REWARD_ADDRESS = self.REWARD_ADDRESS
Esempio n. 2
0
class TestMiner(unittest.TestCase):

    def setUp(self):
        self.mock_blockchain = Mock(Blockchain)
        self.mock_mempool = Mock(Mempool)
        self.HOST = '123.456.789.012'
        self.REWARD_ADDRESS = 'RewardAddress'
        self.MAX_TRANSACTIONS_PER_BLOCK = 10

        self.subject = Miner(self.mock_blockchain, self.mock_mempool)
        self.subject.HOST = self.HOST
        self.subject.MAX_TRANSACTIONS_PER_BLOCK = self.MAX_TRANSACTIONS_PER_BLOCK
        self.subject.REWARD_ADDRESS = self.REWARD_ADDRESS

    def test_mine_block_When_mempool_empty_Returns_block_with_coinbase(self):
        mock_block_header = Mock(BlockHeader)
        mock_block = Mock(Block)
        mock_block.block_header = mock_block_header
        mock_hash_difficulty = PropertyMock(side_effect=[0, 0, 10])

        type(mock_block_header).hash_difficulty = mock_hash_difficulty
        mock_block_header.hash = '0000000000111111111'

        self.mock_blockchain.get_tallest_block_header.return_value = mock_block_header, 0, 125
        self.mock_blockchain.get_coinbase_hash_by_block_hash.return_value = "prevcoinbasehash"
        self.mock_blockchain.get_reward.return_value = 50
        self.mock_blockchain.calculate_hash_difficulty.return_value = 1
        self.mock_mempool.get_unconfirmed_transactions_chunk.return_value = []

        with patch('crankycoin.miner.Block', return_value=mock_block) as patched_block, \
                patch('time.time', return_value=1521946404):
            result = self.subject.mine_block()

            self.assertEqual(self.mock_blockchain.calculate_hash_difficulty.call_count, 3)
            self.assertEqual(result, mock_block)
Esempio n. 3
0
def full():
    helptext = '''
        Available commands:
        ===================
        balance <public key (optional)>
        history <public key (optional)>
        getnodes
        getblock <index (optional)>
        getblocks <start index (optional)> <stop index (optional)>
        mempoolcount
        getmempool
        getunconfirmedtx <tx hash>
        mine <start | stop>
        quit or exit
    '''
    peers = Peers()
    api_client = ApiClient(peers)
    blockchain = Blockchain()
    mempool = Mempool()
    validator = Validator()
    ip = config['user']['ip']
    public_key = config['user']['public_key']
    if ip is None or public_key is None:
        print("\n\npublic key and IP must be provided.\n\n")
        sys.exit(1)
    else:
        print("\n\nfull node starting...\n\n")
        full_node = FullNode(peers, api_client, blockchain, mempool, validator)
        full_node.start()
        miner = Miner(blockchain, mempool)
        mining = False

    while True:
        cmd = input("{} ({}) full node > ".format(
            config['network']['name'], config['network']['ticker_symbol']))
        cmd_split = cmd.split()
        try:
            if cmd_split[0] == "balance":
                if len(cmd_split) == 2:
                    url = full_node.BALANCE_URL.format(
                        "localhost", full_node.FULL_NODE_PORT, cmd_split[1])
                else:
                    url = full_node.BALANCE_URL.format(
                        "localhost", full_node.FULL_NODE_PORT, public_key)
                response = requests.get(url)
                print(response.json())
            elif cmd_split[0] == "history":
                if len(cmd_split) == 2:
                    url = full_node.TRANSACTION_HISTORY_URL.format(
                        "localhost", full_node.FULL_NODE_PORT, cmd_split[1])
                    response = requests.get(url)
                else:
                    url = full_node.TRANSACTION_HISTORY_URL.format(
                        "localhost", full_node.FULL_NODE_PORT, public_key)
                    response = requests.get(url)
                print(response.json())
            elif cmd_split[0] == "getnodes":
                url = full_node.NODES_URL.format("localhost",
                                                 full_node.FULL_NODE_PORT)
                response = requests.get(url)
                print(response.json())
            elif cmd_split[0] == "getblock":
                if len(cmd_split) == 2:
                    url = full_node.BLOCKS_URL.format("localhost",
                                                      full_node.FULL_NODE_PORT,
                                                      cmd_split[1])
                else:
                    url = full_node.BLOCKS_URL.format("localhost",
                                                      full_node.FULL_NODE_PORT,
                                                      "latest")
                response = requests.get(url)
                print(response.json())
            elif cmd_split[0] == "getblocks":
                if len(cmd_split) == 3:
                    url = full_node.BLOCKS_INV_URL.format(
                        "localhost", full_node.FULL_NODE_PORT, cmd_split[1],
                        cmd_split[2])
                else:
                    url = full_node.BLOCKS_URL.format("localhost",
                                                      full_node.FULL_NODE_PORT,
                                                      "")
                response = requests.get(url)
                print(response.json())
            elif cmd_split[0] == "mempoolcount":
                url = full_node.UNCONFIRMED_TRANSACTIONS_URL.format(
                    "localhost", full_node.FULL_NODE_PORT, "count")
                response = requests.get(url)
                print(response.json())
            elif cmd_split[0] == "getmempool":
                url = full_node.UNCONFIRMED_TRANSACTIONS_URL.format(
                    "localhost", full_node.FULL_NODE_PORT, "")
                response = requests.get(url)
                print(response.json())
            elif cmd_split[0] == "getunconfirmedtx":
                if len(cmd_split) == 2:
                    url = full_node.UNCONFIRMED_TRANSACTIONS_URL.format(
                        "localhost", full_node.FULL_NODE_PORT, cmd_split[1])
                    response = requests.get(url)
                    print(response.json())
                else:
                    print("\nRequires tx hash\n")
            elif cmd_split[0] == "mine":
                if len(cmd_split) == 2:
                    if cmd_split[1] == "start":
                        if mining is False:
                            print("\n\nminer starting...\n\n")
                            mining = True
                            miner.start()
                    elif cmd_split[1] == "stop":
                        if mining is True:
                            print("\n\nminer shutting down...\n\n")
                            mining = False
                            miner.shutdown()
                    else:
                        print("\nRequires: start | stop")
                else:
                    print("\nRequires: start | stop")
            elif cmd_split[0] in ("quit", "exit"):
                if mining is True:
                    print("\n\nminer shutting down...\n\n")
                    miner.shutdown()
                    time.sleep(2)
                full_node.shutdown()
                time.sleep(2)
                sys.exit(0)
            else:  # help
                print(helptext)
        except IndexError:
            pass