Exemple #1
0
    def test_mempool_observer(self):
        connection = Mock()
        from pycoin.tx.Tx import Tx
        tx = Tx.from_hex(
            '01000000000101112a649fd72656cf572259cb7cb61bd31ccdbdf0944070e73401565affbe629d0100000000ffffffff02608'
            'de2110000000017a914d52b516c1a094462959ed6facebb94429d2cebf487d3135b0b00000000220020701a8d401c84fb13e6'
            'baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d0400473044022006b149e0cf031f57fd443bd1210b381e9b1b15094'
            '57ba1f49e48b803696f56e802203d66bd974ad3ac5b7591cc84e706b78d139c61e2bf1995a89c4dc0758984a2b70148304502'
            '2100fe7275d601080e1870517774a3ad6accaa7f8ad144addec3251e98685d4fefad02207792c2b0ed6ab42ed2ba6d12e6bd3'
            '4db8c6f4ac6f15e604f70ea85a735c450b1016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd'
            '92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e'
            '046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000')
        self.loop.run_until_complete(
            self.sut.on_transaction(connection, {'tx': tx}))
        self.assertEqual(tx.w_id(),
                         [x for x in self.mempool_repository.get_txids()][0])
        self.repository.blockchain.get_transactions_by_block_hash.return_value = [], None

        block = Block(1,
                      b'0' * 32,
                      merkle_root=merkle([tx.hash()]),
                      timestamp=123456789,
                      difficulty=3000000,
                      nonce=1 * 137)
        block.txs.append(tx)
        as_bin = block.as_bin()
        Block.from_bin(as_bin)
        block_header = {'block_hash': block.id()}
        self.batcher_factory.add_peer.return_value = async_coro(True)
        self.batcher_factory.inv_item_to_future.return_value = block.as_bin()
        mempool_response = self.mempool_repository.get_raw_mempool(True)

        self.assertEqual(
            {
                '41867301a6cff5c47951aa1a4eef0be910db0cb5f154eaeb469732e1f9b54548':
                {
                    'size': 381,
                    'fee': 0,
                    'modifiedfee': 0,
                    'time': ANY,
                    'height': 0,
                    'descendantcount': 0,
                    'descendantsize': 0,
                    'descendantfees': 0,
                    'ancestorcount': 0,
                    'ancestorsize': 0,
                    'ancestorfees': 0,
                    'depends': []
                }
            }, mempool_response)
        self.repository.blockchain.save_block.side_effect = lambda a: {
            'block_object': Block.from_bin(a['block_bytes'])
        }
        self.repository.blockchain.get_transactions_by_block_hash.return_value = [], None
        self.loop.run_until_complete(self.sut.on_block_header(block_header))
        self.assertEqual(self.mempool_repository.get_raw_mempool(True), {})
        self.assertEqual([x for x in self.mempool_repository.get_txids()], [])
Exemple #2
0
def make_block(index):
    s = index * 30000
    txs = [make_tx(i) for i in range(s, s + 8)]
    block = Block(version=1,
                  previous_block_hash=b'\0' * 32,
                  merkle_root=b'\0' * 32,
                  timestamp=GENESIS_TIME + index,
                  difficulty=s,
                  nonce=s,
                  txs=txs)
    return block
Exemple #3
0
def make_block(i):
    s = i * 30000
    txs = [make_tx(i) for i in range(s, s + 8)]
    block = Block(version=1,
                  previous_block_hash=b'\0' * 32,
                  merkle_root=b'\0' * 32,
                  timestamp=1390000000 + i,
                  difficulty=s,
                  nonce=s,
                  txs=txs)
    return block
Exemple #4
0
def make_headers(count, header=None):
    if header is None:
        last_hash = HASH_INITIAL_BLOCK
    else:
        last_hash = header.hash()
    tweak = last_hash
    headers = []
    for i in range(count):
        headers.append(
            Block(version=1,
                  previous_block_hash=last_hash,
                  merkle_root=make_hash(i, tweak),
                  timestamp=GENESIS_TIME + i * 600,
                  difficulty=DEFAULT_DIFFICULTY,
                  nonce=i * 137))
        last_hash = headers[-1].hash()
    return headers
Exemple #5
0
 def get_blockheader_with_transaction_hashes(self, block_hash):
     URL = "%s%sblock/%s" % (self.base_url, self.base_path, b2h_rev(block_hash))
     r = json.loads(urlopen(URL).read().decode("utf8"))
     version = r.get("version")
     previous_block_hash = h2b_rev(r.get("previousblockhash"))
     merkle_root = h2b_rev(r.get("merkleroot"))
     timestamp = r.get("time")
     difficulty = int(r.get("bits"), 16)
     nonce = int(r.get("nonce"))
     tx_hashes = [h2b_rev(tx_hash) for tx_hash in r.get("tx")]
     blockheader = Block(version, previous_block_hash, merkle_root, timestamp, difficulty, nonce)
     if blockheader.hash() != block_hash:
         return None, None
     calculated_hash = merkle(tx_hashes, double_sha256)
     if calculated_hash != merkle_root:
         return None, None
     blockheader.height = r.get("height")
     return blockheader, tx_hashes
Exemple #6
0
 def _get_block_with_tx(self):
     from pycoin.tx.Tx import Tx
     tx = Tx.from_hex(
         '01000000000101112a649fd72656cf572259cb7cb61bd31ccdbdf0944070e73401565affbe629d0100000000ffffffff02608'
         'de2110000000017a914d52b516c1a094462959ed6facebb94429d2cebf487d3135b0b00000000220020701a8d401c84fb13e6'
         'baf169d59684e17abd9fa216c8cc5b9fc63d622ff8c58d0400473044022006b149e0cf031f57fd443bd1210b381e9b1b15094'
         '57ba1f49e48b803696f56e802203d66bd974ad3ac5b7591cc84e706b78d139c61e2bf1995a89c4dc0758984a2b70148304502'
         '2100fe7275d601080e1870517774a3ad6accaa7f8ad144addec3251e98685d4fefad02207792c2b0ed6ab42ed2ba6d12e6bd3'
         '4db8c6f4ac6f15e604f70ea85a735c450b1016952210375e00eb72e29da82b89367947f29ef34afb75e8654f6ea368e0acdfd'
         '92976b7c2103a1b26313f430c4b15bb1fdce663207659d8cac749a0e53d70eff01874496feff2103c96d495bfdd5ba4145e3e'
         '046fee45e84a8a48ad05bd8dbb395c011a32cf9f88053ae00000000')
     block = Block(1,
                   b'0' * 32,
                   merkle_root=merkle([tx.hash()]),
                   timestamp=123456789,
                   difficulty=3000000,
                   nonce=1 * 137)
     block.txs.append(tx)
     return block
Exemple #7
0
def make_blocks(count, nonce_base=30000, previous_block_hash=b'\0' * 32):
    blocks = []
    for i in range(count):
        s = i * nonce_base
        txs = [COINBASE_TX]  # + [make_tx(i) for i in range(s, s+8)]
        nonce = s
        while True:
            block = Block(version=1,
                          previous_block_hash=previous_block_hash,
                          merkle_root=b'\0' * 32,
                          timestamp=1390000000 + i * 600,
                          difficulty=i,
                          nonce=nonce,
                          txs=txs)
            if block.hash()[-1] == i & 0xff:
                break
            nonce += 1
        blocks.append(block)
        previous_block_hash = block.hash()
    return blocks
Exemple #8
0
def make_blocks(count,
                nonce_base=30000,
                previous_block_hash=HASH_INITIAL_BLOCK):
    blocks = []
    for i in range(count):
        s = i * nonce_base
        txs = [coinbase_tx(i + 1)] + [make_tx(i) for i in range(s, s + 8)]
        nonce = s
        while True:
            merkle_root = merkle([tx.hash() for tx in txs])
            block = Block(version=1,
                          previous_block_hash=previous_block_hash,
                          merkle_root=merkle_root,
                          timestamp=GENESIS_TIME + i * 600,
                          difficulty=i,
                          nonce=nonce)
            block.set_txs(txs)
            if block.hash()[-1] == i & 0xff:
                break
            nonce += 1
        blocks.append(block)
        previous_block_hash = block.hash()
    return blocks