Esempio n. 1
0
    def _execute_phase_3(self, config, phase_2_info):
        """
        * At this point, any participating processing nodes will have appended signed Phase 2 verification proof to the block.
        * Processing nodes participating in Phase 3 Verification may be a different set of nodes than the set of nodes participating in Phase 1 and Phase 2 Verification processes.
        * Processing nodes may be defined for the sole purpose of Phase 3 verification (e.g. for independent blockchain verification auditing purposes).
        * A participating node will verify that no invalid transaction has been included in the set of approved transaction.
        * A participating node will verify that all "approved" transactions are signed by their respective owner.
        * A node may perform extra validation steps on all transactions and verification units.
        * All signed "Phase 3 Signature Structures" will be grouped, concatenated, and cryptographically signed by the node.
        """
        phase = 3
        phase_2_record = phase_2_info['record']
        p2_verification_info = phase_2_info['verification_info']
        phase_2_record['verification_info'] = p2_verification_info
        prior_block_hash = self.get_prior_hash(phase_2_record[ORIGIN_ID], phase)

        # validate phase_2's verification record
        if validate_verification_record(phase_2_record, p2_verification_info):
            # storing valid verification record
            verfication_db.insert_verification(phase_2_record)

            phase_2_records = self.get_sig_records(phase_2_record)

            signatories, businesses, locations = self.get_verification_diversity(phase_2_records)

            # checking if passed requirements to move on to next phase
            if len(signatories) >= P2_COUNT_REQ and len(businesses) >= P2_BUS_COUNT_REQ and len(locations) >= P2_LOC_COUNT_REQ:
                # updating record phase
                phase_2_record[PHASE] = phase
                lower_hashes = [record[SIGNATURE]['signatory'] + ":" + record[SIGNATURE][HASH]
                                      for record in phase_2_records]

                # TODO: add a structure such as a tuple to pair signatory with it's appropriate hash (signatory, hash)
                # TODO: and store that instead of lower_phase_hashes also add said structure to phase_3_msg in thrift
                verification_info = {
                    'lower_hashes': lower_hashes,
                    'p2_count': len(signatories),
                    'businesses': list(businesses),
                    'deploy_locations': list(locations)
                }

                lower_hash = str(deep_hash(lower_hashes))

                # sign verification and rewrite record
                block_info = sign_verification_record(self.network.this_node.node_id,
                                                      prior_block_hash,
                                                      lower_hash,
                                                      self.service_config['public_key'],
                                                      self.service_config['private_key'],
                                                      phase_2_record[BLOCK_ID],
                                                      phase_2_record[PHASE],
                                                      phase_2_record[ORIGIN_ID],
                                                      int(time.time()),
                                                      verification_info
                                                      )

                # inserting verification info after signing
                verfication_db.insert_verification(block_info['verification_record'])
                self.network.send_block(self.network.phase_3_broadcast, block_info, phase)
                print "phase 3 executed"
Esempio n. 2
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)

        if 'approve_block' in config:
            return config['approve_block'](config, 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)

        # Use the custom approval code if configured, otherwise approve all valid transaction
        rejected_transactions = []
        if 'approve_transaction' in config:
            approved_transactions, rejected_transactions = \
                self.split_items(config['approve_transaction'], valid_transactions)
        else:
            approved_transactions = valid_transactions

        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_phase_hash = str(deep_hash(0))

            # sign approved transactions
            block_info = sign_verification_record(
                signatory, prior_block_hash, lower_phase_hash,
                self.service_config['public_key'],
                self.service_config['private_key'], current_block_id, phase,
                origin_id, int(time.time()), verification_info)

            # store signed phase specific data
            verfication_db.insert_verification(
                block_info['verification_record'])

            # pass block info to network to send it to appropriate phase
            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)