def test_sweep(self): cmds = MocCommands() cmds.wallet.add_address_transaction(120000) cmds.wallet.add_address_transaction(120000) unspent = [] for tx in cmds.wallet.transactions.values(): for output in tx._outputs: unspent.append((tx.hash(), output)) cmds.network = MocNetwork({ 'blockchain.address.listunspent': lambda _: [{ 'tx_hash': tx_hash, 'tx_pos': 1, 'address': address, 'coinbase': False, 'height': 2, 'is_claim': False, 'is_support': False, 'is_update': False, 'prevout_hash': 'df303881e9014cce89c7acf55b124372e22979284baa99bb9fa178a9d35c97cb', 'prevout_n': 0, 'value': amount } for (tx_hash, (type, address, amount)) in unspent] }) destination = cmds.wallet.create_new_address() result = cmds.sweep(self.private_key, destination) self.assertEqual(True, result['success']) sent = Transaction(result['tx']) sent.deserialize() self.assertEqual(len(sent._outputs), 1) self.assertEqual(sent._outputs[0][2], 230000)
def tx_response(self, response): params, result = self.parse_response(response) if not params: return tx_hash, tx_height = params assert tx_hash == hash_encode(Hash(result.decode('hex'))) tx = Transaction(result) try: tx.deserialize() except Exception: log.info("cannot deserialize transaction, skipping: %s", tx_hash) return self.wallet.receive_tx_callback(tx_hash, tx, tx_height) self.requested_tx.remove((tx_hash, tx_height)) log.info("received tx %s height: %d bytes: %d", tx_hash, tx_height, len(tx.raw)) # callbacks self.network.trigger_callback('new_transaction', tx) if not self.requested_tx: self.network.trigger_callback('updated')
def tx_response(response): params, result = self.parse_response(response) if not params: log.warning("failed to get %s", txid) self.requested_tx.remove((txid, height)) return tx_hash, tx_height = params assert tx_hash == hash_encode(Hash(result.decode('hex'))) tx = Transaction(result) try: tx.deserialize() except Exception: log.info("cannot deserialize transaction, skipping: %s", tx_hash) return self.wallet.receive_tx_callback(tx_hash, tx, tx_height) self.requested_tx.remove((tx_hash, tx_height)) log.info("received tx %s height: %d bytes: %d", tx_hash, tx_height, len(tx.raw)) # callbacks self.network.trigger_callback('new_transaction', tx) if not self.requested_tx: self.network.trigger_callback('updated')