Exemple #1
0
 def insert_transactions(self, transactions):
     """ insert given transactions into database """
     for txn in transactions:
         try:
             transaction_db.insert_transaction(txn)
         except Exception as ex:
             template = "An exception of type {0} occurred. Arguments:\n{1!r}"
             message = template.format(type(ex).__name__, ex.args)
             logger().error(message)
             continue
    def post(self):
        txn = self.request.body
        log = self.application.log
        log.debug("Parsing JSON")

        try:
            txn = tornado.escape.json_decode(txn)
            txn["header"]["transaction_id"] = str(uuid.uuid4())
            txn["header"]["transaction_ts"] = int(time.time())
        except:
            log.error("Failed to parse JSON.  Details: " +
                      str(sys.exc_info()[1]))
            self.clear()
            self.set_txn_status(log, 400)
            self.write(
                format_error("invalid input",
                             "ERROR:  Failed to parse JSON\n"))
            return

        try:
            log.info("Validating the transaction")
            if not valid_vestal_transaction(txn):
                return False

            if valid_transaction_sig(txn):
                log.info("Signing the transaction")
                txn = sign_transaction("transaction-service",
                                       self.application.private_key,
                                       self.application.public_key, txn)

                tx_dao.insert_transaction(txn)
                self.set_header("Content-Type", "application/json")
                self.set_txn_status(log, 201)
                self.write(
                    json.dumps(
                        {"transaction_id": txn["header"]["transaction_id"]}))
                return
            else:  # TODO: add status function accepts status code number and status string
                self.set_txn_status(log, 400)
                return
        except:
            log.error(str(sys.exc_info()))
            self.clear()
            self.set_txn_status(log, 500)
            self.write(format_error("validation", str(sys.exc_info()[1])))
    def post(self):
        txn = self.request.body
        log = self.application.log
        log.debug("Parsing JSON")

        try:
            txn = tornado.escape.json_decode(txn)
            txn["header"]["transaction_id"] = str(uuid.uuid4())
            txn["header"]["transaction_ts"] = int(time.time())
        except:
            log.error("Failed to parse JSON.  Details: " + str(sys.exc_info()[1]))
            self.clear()
            self.set_txn_status(log, 400)
            self.write(format_error("invalid input", "ERROR: malformed json\nDocumentation for transaction format can be found at \nhttps://github.com/dragonchain/dragonchain/blob/master/docs/transaction.md"))
            return

        try:
            log.info("Validating the transaction")
            if not valid_vestal_transaction(txn):
                return False

            if valid_transaction_sig(txn):
                log.info("Signing the transaction")
                txn = sign_transaction("transaction-service",
                                       self.application.private_key,
                                       self.application.public_key, txn)

                tx_dao.insert_transaction(txn)
                self.set_header("Content-Type", "application/json")
                self.set_txn_status(log, 201)
                self.write(json.dumps({
                    "transaction_id": txn["header"]["transaction_id"]
                }))
                return
            else:  # TODO: add status function accepts status code number and status string
                self.set_txn_status(log, 400)
                return
        except:
            log.error(str(sys.exc_info()))
            self.clear()
            self.set_txn_status(log, 500)
            self.write(format_error("validation", str(sys.exc_info()[1])))