Ejemplo n.º 1
0
 def generate_tx(self,
                 sender_monitor_id,
                 change_subaddress,
                 input_list,
                 outlay_dict,
                 fee=0,
                 tombstone=0):
     """ Prepares a transaction. If the fee is zero, we use the default minimum fee. Mix-ins and other
     complexities of the MobileCoin protocol are handled automatically.
     """
     outlay_list = [
         api.Outlay(value=r['value'], receiver=r['receiver'])
         for r in outlay_dict
     ]
     request = api.GenerateTxRequest(sender_monitor_id=sender_monitor_id,
                                     change_subaddress=change_subaddress,
                                     input_list=input_list,
                                     outlay_list=outlay_list,
                                     fee=fee)
     return self.stub.GenerateTx(request).tx_proposal
Ejemplo n.º 2
0
def run_test(stub, amount, monitor_id, dest, max_seconds):
    tx_stats = {}
    sync_start = time.time()
    wait_for_accounts_sync(stub, [monitor_id, dest.monitor_id], 3)
    logging.info("Time to sync: %s", time.time() - sync_start)

    resp = stub.GetBalance(
        mobilecoind_api_pb2.GetBalanceRequest(monitor_id=monitor_id))
    starting_balance = resp.balance
    logging.info("Starting balance prior to transfer: %s", starting_balance)

    tx_resp = stub.SendPayment(
        mobilecoind_api_pb2.SendPaymentRequest(
            sender_monitor_id=monitor_id,
            sender_subaddress=0,
            outlay_list=[
                mobilecoind_api_pb2.Outlay(
                    value=amount,
                    receiver=dest.public_address,
                )
            ],
            fee=0,
            tombstone=0,
        ))

    tx_stats[0] = {
        'start': time.time(),
        'time_delta': None,
        'tombstone': tx_resp.sender_tx_receipt.tombstone,
        'block_delta': None,
        'status': TransferStatus.pending,
        'receipt': tx_resp,
    }
    stats = poll(monitor_id, tx_stats, stub)
    # FIXME: Move max seconds check inside polling
    assert tx_stats[0]['time_delta'] < max_seconds, "Did not clear in time"
    assert tx_stats[0][
        'status'] == TransferStatus.success, "Transfer did not succeed"
    return stats
Ejemplo n.º 3
0
def run_test(stub, amount, monitor_id, dest, max_seconds, token_id):
    tx_stats = {}
    sync_start = time.time()
    wait_for_accounts_sync(stub, [monitor_id], 3)
    logging.info("Time to sync: %s", time.time() - sync_start)

    resp = stub.GetBalance(
        mobilecoind_api_pb2.GetBalanceRequest(monitor_id=monitor_id, token_id=token_id))
    starting_balance = resp.balance
    logging.info("Starting balance prior to transfer: %s", starting_balance)

    # Try building a payment that sends all of our balance
    # We may get a defragmentation error -- if we do, then submit optimization Tx outs
    # until we don't get that error anymore
    while True:
        try:
            tx_resp = stub.SendPayment(
                mobilecoind_api_pb2.SendPaymentRequest(
                    sender_monitor_id=monitor_id,
                    sender_subaddress=0,
                    outlay_list=[
                        mobilecoind_api_pb2.Outlay(
                            value=amount,
                            receiver=dest,
                        )
                    ],
                    fee=0,
                    tombstone=0,
                    token_id=token_id,
                ))
            break
        except grpc.RpcError as e:
            if "insufficient funds due to utxo fragmentation" in e.details.lower():
                logging.info("Got defragmentation error, building an optimization transaction")
                opt_tx = stub.GenerateOptimizationTx(
                    mobilecoind_api_pb2.GenerateOptimizationTxRequest(
                        monitor_id=monitor_id,
                        subaddress=0,
                        fee=0,
                        token_id=token_id,
                    ))
                tx_resp = stub.SubmitTx(
                    mobilecoind_api_pb2.SubmitTxRequest(
                        tx_proposal=opt_tx.tx_proposal
                    ))
                tx_stats[0] = {
                    'start': time.time(),
                    'time_delta': None,
                    'tombstone': tx_resp.sender_tx_receipt.tombstone,
                    'block_delta': None,
                    'status': TransferStatus.pending,
                    'receipt': tx_resp,
                }
                stats = poll(monitor_id, tx_stats, stub)
                # Subtract fee from amount, so that we don't get an insufficient funds error
                # in the next cycle (since it costs us a fee to defragment)
                amount = amount - opt_tx.tx_proposal.fee
            else:
                logging.error("RPC Error encountered")
                raise

    tx_stats[0] = {
        'start': time.time(),
        'time_delta': None,
        'tombstone': tx_resp.sender_tx_receipt.tombstone,
        'block_delta': None,
        'status': TransferStatus.pending,
        'receipt': tx_resp,
    }
    stats = poll(monitor_id, tx_stats, stub)
    # FIXME: Move max seconds check inside polling
    assert tx_stats[0]['time_delta'] < max_seconds, "Did not clear in time"
    assert tx_stats[0]['status'] == TransferStatus.success, "Transfer did not succeed"
    return stats