Exemplo n.º 1
0
    def get(self, transaction_id=None):
        try:
            log = self.application.log
            rows = int(self.get_query_argument('rows', default='-1'))
            offset = int(self.get_query_argument('offset', default='-1'))
            if rows < 1:
                rows = None
            if offset < 1:
                offset = None

            results = []
            if transaction_id:
                results = tx_dao.get(transaction_id)
            else:
                query = {}
                for field, default in self.query_fields.iteritems():
                    value = self.get_query_argument(field, default)
                    if value:
                        query[field] = value

                for tx in tx_dao.get_all(limit=rows, offset=offset, **query):
                    results += [tx]
            self.write(json.dumps(results))
        except:
            log.error(str(sys.exc_info()))
            self.clear()
            self.set_status(500)
            self.write(format_error("server error", str(sys.exc_info()[1])))
Exemplo n.º 2
0
    def get(self, transaction_id=None):
        try:
            log = self.application.log
            rows = int(self.get_query_argument('rows', default='-1'))
            offset = int(self.get_query_argument('offset', default='-1'))
            if rows < 1:
                rows = None
            if offset < 1:
                offset = None

            results = []
            if transaction_id:
                results = tx_dao.get(transaction_id)
            else:
                query = {}
                for field, default in self.query_fields.iteritems():
                    value = self.get_query_argument(field, default)
                    if value:
                        query[field] = value

                for tx in tx_dao.get_all(limit=rows, offset=offset, **query):
                    results += [tx]
            self.set_header("Content-Type", "application/json")
            self.write(json.dumps(results))
        except:
            log.error(str(sys.exc_info()))
            self.clear()
            self.set_status(500)
            self.write(format_error("server error", str(sys.exc_info()[1])))
Exemplo n.º 3
0
    def execute_ssc(self, min_block_id, vr_limit, txn_limit):
        """ execute subscription smart contract """
        for sc_key in self.ssc.keys():
            (origin_id, txn_type, phase) = sc_key.split(":")
            vrs = verification_db.get_all(limit=vr_limit,
                                          origin_id=origin_id,
                                          phase=phase,
                                          min_block_id=min_block_id)
            #dedupe block ids
            block_ids = {v['block_id'] for v in vrs}
            #group vr's by block id
            block_vrs = defaultdict(list)
            for v in vrs:
                block_vrs[v['block_id']].append(v)

            # fetch all txns for each block
            for block_id in block_ids:
                #todo: filter by p1 signatory
                txns = transaction_db.get_all(transaction_type=txn_type,
                                              block_id=block_id,
                                              limit=txn_limit)
                # execute ssc for each vr in block
                for v in block_vrs[block_id]:
                    self.ssc[sc_key](txns, v)
Exemplo n.º 4
0
    def _execute_phase_1(self, config, current_block_id):
        """
        TODO update all EXEC comments/docs
        * Each node gathers all transactions that may be included in the prospective block and groups them by transaction owner.
        * All transactions owned (or sourced from) a respective node's business unit or system (owned) are grouped for approval.
        * All transactions not owned by the node's business unit or system (others) are grouped for validation.
        * All owned transactions are verified per business rules, configurable, (e.g, existence or non-existence of particular fields, with field value validation logic).
        * All owned and verified transactions are "approved" by executing the Transaction Verification Signing Process defined below.
        * Any transactions deemed "unapproved" will be taken out of the prospective block from a node's perspective by non-inclusion in the signing process, and sent to a system "pool" or "queue" for analysis and alternate processing
        * All other (non-owned) transactions are validated to system wide rules agreed upon for all nodes through business and system processes.
        * All other (non-owned) transactions are declared "valid" by the node by executing the Transaction Verification Signing Process defined below.
        * Any transactions deemed "invalid" will be taken out of the prospective block from a node's perspective by non-inclusion in the signing process, and sent to a system "pool" or "queue" for analysis and alternate processing.
        """
        print("Phase 1 Verify Start.")
        # Group transactions for last 5 seconds into current block id
        block_bound_lower_ts = get_block_time(current_block_id - BLOCK_FIXATE_OFFSET)
        print ("""Time bounds: %i - %i""" % (block_bound_lower_ts, block_bound_lower_ts + BLOCK_INTERVAL))
        transaction_db.fixate_block(block_bound_lower_ts, block_bound_lower_ts + BLOCK_INTERVAL, current_block_id)

        transactions = transaction_db.get_all(block_id=current_block_id)

        # Validate the schema and structure of the transactions
        valid_transactions, invalid_transactions = self.split_items(valid_transaction_sig, transactions)

        rejected_transactions = []
        approved_transactions = []

        for txn in valid_transactions:
            if self.handle_transaction(txn):
                approved_transactions.append(txn)
            else:
                rejected_transactions.append(txn)

        if len(approved_transactions) > 0:
            # update status of approved transactions
            for tx in approved_transactions:
                tx["header"]["status"] = "approved"
                transaction_db.update_transaction(tx)

            # stripping payload from all transactions before signing
            self.strip_payload(approved_transactions)

            phase = 1
            # signatory equals origin_id in phase 1
            signatory = origin_id = self.network.this_node.node_id
            prior_block_hash = self.get_prior_hash(origin_id, phase)
            verification_info = approved_transactions

            lower_hash = str(final_hash([0]))

            # sign approved transactions
            block_info = sign_verification_record(signatory,
                                                  prior_block_hash,
                                                  lower_hash,
                                                  self.service_config['public_key'],
                                                  self.service_config['private_key'],
                                                  current_block_id,
                                                  phase,
                                                  origin_id,
                                                  int(time.time()),
                                                  self.public_transmission,
                                                  verification_info)
            # store signed phase specific data
            verification_id = str(uuid.uuid4())
            verification_db.insert_verification(block_info['verification_record'], verification_id)

            # send block info off for public transmission if configured to do so
            if block_info['verification_record']['public_transmission']['p1_pub_trans']:
                self.network.public_broadcast(block_info, phase)

            # send block info for phase 2 validation
            self.network.send_block(self.network.phase_1_broadcast, block_info, phase)
            print("Phase 1 signed " + str(len(approved_transactions)) + " transactions")

        # update status transactions that were rejected
        if len(rejected_transactions) > 0:
            for tx in rejected_transactions:
                tx["header"]["status"] = "rejected"
                transaction_db.update_transaction(tx)