Beispiel #1
0
    def test_n_of_x(self):
        """
        Test that if NofX Rule is set, the validation rule is checked
        correctly. Test:
            1. Valid Block, has one or less intkey transactions.
            2. Invalid Block, to many intkey transactions.
            3. Valid Block, ignore rule because it is formatted incorrectly.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "NofX:1,intkey")

        self.assertTrue(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "NofX:0,intkey")

        self.assertFalse(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "NofX:0")

        self.assertTrue(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))
Beispiel #2
0
    def test_local(self):
        """
        Test that if local Rule is set, the validation rule is checked
        correctly. Test:
            1. Valid Block, first transaction is signed by the same signer as
               the block.
            2. Invalid Block, first transaction is not signed by the same
               signer as the block.
            3. Valid Block, ignore rule because it is formatted incorrectly.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "local:0")

        self.assertTrue(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))

        blkw = self._make_block(["intkey"], "pub_key", False)
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "local:0")

        self.assertFalse(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "local:test")

        self.assertTrue(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))
Beispiel #3
0
    def test_x_at_y(self):
        """
        Test that if XatY Rule is set, the validation rule is checked
        correctly. Test:
            1. Valid Block, has intkey at the 0th position.
            2. Invalid Block, does not have an blockinfo txn at the 0th postion
            3. Valid Block, ignore rule because it is formatted incorrectly.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "XatY:intkey,0")

        self.assertTrue(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "XatY:blockinfo,0")

        self.assertFalse(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "XatY:0")

        self.assertTrue(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))
Beispiel #4
0
    def do_n_of_x():
        blkw = validationRuleEnforcerTest._make_block(["intkey"], "pub_key")

        validationRuleEnforcerTest._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules", "NofX:0")

        assert enforce_validation_rules(
            validationRuleEnforcerTest._settings_view(),
            blkw.header.signer_public_key, blkw.batches) == True
Beispiel #5
0
 def test_no_setting(self):
     """
     Test that if no validation rules are set, the block is valid.
     """
     blkw = self._make_block(["intkey"], "pub_key")
     self.assertTrue(
         enforce_validation_rules(self._settings_view(),
                                  blkw.header.signer_public_key,
                                  blkw.batches))
Beispiel #6
0
 def test_no_setting(self):
     """
     Test that if no validation rules are set, the block is valid.
     """
     blkw = self._make_block(["intkey"], "pub_key")
     self.assertTrue(
         enforce_validation_rules(
             self._settings_view(),
             blkw.header.signer_public_key,
             blkw.batches))
Beispiel #7
0
    def do_all_at_once_signer_key():
        blkw = validationRuleEnforcerTest._make_block(["intkey"], "pub_key",
                                                      False)
        validationRuleEnforcerTest._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:intkey,0;XatY:intkey,0;local:0")

        assert enforce_validation_rules(
            validationRuleEnforcerTest._settings_view(),
            blkw.header.signer_public_key, blkw.batches) == False
Beispiel #8
0
    def test_local(self):
        """
        Test that if local Rule is set, the validation rule is checked
        correctly. Test:
            1. Valid Block, first transaction is signed by the same signer as
               the block.
            2. Invalid Block, first transaction is not signed by the same
               signer as the block.
            3. Valid Block, ignore rule because it is formatted incorrectly.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "local:0")

        self.assertTrue(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))

        blkw = self._make_block(["intkey"], "pub_key", False)
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "local:0")

        self.assertFalse(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "local:test")

        self.assertTrue(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))
Beispiel #9
0
 def _validate_on_chain_rules(self, blkw, prev_state_root):
     """
     Validate that the block conforms to all validation rules stored in
     state. If the block breaks any of the stored rules, the block is
     invalid.
     """
     if blkw.block_num != 0:
         return enforce_validation_rules(
             self._settings_view_factory.create_settings_view(
                 prev_state_root), blkw.header.signer_public_key,
             blkw.batches)
     return True
 def _validate_on_chain_rules(self, blkw, prev_state_root):
     """
     Validate that the block conforms to all validation rules stored in
     state. If the block breaks any of the stored rules, the block is
     invalid.
     """
     if blkw.block_num != 0:
         return enforce_validation_rules(
             self._settings_view_factory.create_settings_view(
                 prev_state_root),
             blkw.header.signer_public_key,
             blkw.batches)
     return True
Beispiel #11
0
    def test_n_of_x(self):
        """
        Test that if NofX Rule is set, the validation rule is checked
        correctly. Test:
            1. Valid Block, has one or less intkey transactions.
            2. Invalid Block, to many intkey transactions.
            3. Valid Block, ignore rule because it is formatted incorrectly.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "NofX:1,intkey")

        self.assertTrue(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "NofX:0,intkey")

        self.assertFalse(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "NofX:0")

        self.assertTrue(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))
Beispiel #12
0
    def test_x_at_y(self):
        """
        Test that if XatY Rule is set, the validation rule is checked
        correctly. Test:
            1. Valid Block, has intkey at the 0th position.
            2. Invalid Block, does not have an blockinfo txn at the 0th postion
            3. Valid Block, ignore rule because it is formatted incorrectly.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:intkey,0")

        self.assertTrue(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:blockinfo,0")

        self.assertFalse(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))

        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:0")

        self.assertTrue(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))
Beispiel #13
0
    def test_all_at_once_bad_number_of_intkey(self):
        """
        Test that if multiple rules are set, they are all checked correctly.
        Block is invalid, because there are too many intkey transactions
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "NofX:0,intkey;XatY:intkey,0;local:0")

        self.assertFalse(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))
Beispiel #14
0
    def test_all_at_once(self):
        """
        Test that if multiple rules are set, they are all checked correctly.
        Block should be valid.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:intkey,0;XatY:intkey,0;local:0")

        self.assertTrue(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))
Beispiel #15
0
    def test_all_at_once_bad_number_of_intkey(self):
        """
        Test that if multiple rules are set, they are all checked correctly.
        Block is invalid, because there are too many intkey transactions
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "NofX:0,intkey;XatY:intkey,0;local:0")

        self.assertFalse(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))
Beispiel #16
0
    def test_all_at_once(self):
        """
        Test that if multiple rules are set, they are all checked correctly.
        Block should be valid.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:intkey,0;XatY:intkey,0;local:0")

        self.assertTrue(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))
Beispiel #17
0
    def test_all_at_once_bad_family_at_index(self):
        """
        Test that if multiple rules are set, they are all checked correctly.
        Block is invalid, there is not a blockinfo transactions at the 0th
        position.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:intkey,0;XatY:blockinfo,0;local:0")

        self.assertFalse(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))
Beispiel #18
0
    def test_all_at_once_signer_key(self):
        """
        Test that if multiple rules are set, they are all checked correctly.
        Block is invalid, transaction at the 0th postion is not signed by the
        same signer as the block.
        """
        blkw = self._make_block(["intkey"], "pub_key", False)
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:intkey,0;XatY:intkey,0;local:0")

        self.assertFalse(
            enforce_validation_rules(self._settings_view(),
                                     blkw.header.signer_public_key,
                                     blkw.batches))
Beispiel #19
0
    def test_all_at_once_bad_family_at_index(self):
        """
        Test that if multiple rules are set, they are all checked correctly.
        Block is invalid, there is not a blockinfo transactions at the 0th
        position.
        """
        blkw = self._make_block(["intkey"], "pub_key")
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:intkey,0;XatY:blockinfo,0;local:0")

        self.assertFalse(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))
Beispiel #20
0
    def test_all_at_once_signer_key(self):
        """
        Test that if multiple rules are set, they are all checked correctly.
        Block is invalid, transaction at the 0th postion is not signed by the
        same signer as the block.
        """
        blkw = self._make_block(["intkey"], "pub_key", False)
        self._settings_view_factory.add_setting(
            "sawtooth.validator.block_validation_rules",
            "XatY:intkey,0;XatY:intkey,0;local:0")

        self.assertFalse(
            enforce_validation_rules(
                self._settings_view(),
                blkw.header.signer_public_key,
                blkw.batches))
Beispiel #21
0
    def add_batch(self, batch):
        """Add a batch to the _CandidateBlock
        :param batch: the batch to add to the block
        """
        if batch.trace:
            LOGGER.debug("TRACE %s: %s", batch.header_signature,
                         self.__class__.__name__)

        # first we check if the transaction dependencies are satisfied
        # The completer should have taken care of making sure all
        # Batches containing dependent transactions were sent to the
        # BlockPublisher prior to this Batch. So if there is a missing
        # dependency this is an error condition and the batch will be
        # dropped.
        if self._is_batch_already_committed(batch):
            # batch is already committed.
            LOGGER.debug("Dropping previously committed batch: %s",
                         batch.header_signature)
            return
        elif self._check_batch_dependencies(batch, self._committed_txn_cache):
            batches_to_add = []

            # Inject batches at the beginning of the block
            if not self._pending_batches:
                self._poll_injectors(
                    lambda injector: injector.block_start(
                        self._block_builder.previous_block_id), batches_to_add)

            batches_to_add.append(batch)

            if not enforce_validation_rules(
                    self._settings_view,
                    self._identity_signer.get_public_key().as_hex(),
                    self._pending_batches + batches_to_add):
                return

            for b in batches_to_add:
                self._pending_batches.append(b)
                self._pending_batch_ids.add(b.header_signature)
                try:
                    injected = b.header_signature in self._injected_batch_ids
                    self._scheduler.add_batch(b, required=injected)
                except SchedulerError as err:
                    LOGGER.debug("Scheduler error processing batch: %s", err)
        else:
            LOGGER.debug("Dropping batch due to missing dependencies: %s",
                         batch.header_signature)
Beispiel #22
0
    def add_batch(self, batch):
        """Add a batch to the _CandidateBlock
        :param batch: the batch to add to the block
        """
        if batch.trace:
            LOGGER.debug("TRACE %s: %s", batch.header_signature,
                         self.__class__.__name__)

        # first we check if the transaction dependencies are satisfied
        # The completer should have taken care of making sure all
        # Batches containing dependent transactions were sent to the
        # BlockPublisher prior to this Batch. So if there is a missing
        # dependency this is an error condition and the batch will be
        # dropped.
        if self._is_batch_already_committed(batch):
            # batch is already committed.
            LOGGER.debug("Dropping previously committed batch: %s",
                         batch.header_signature)
            return
        elif self._check_batch_dependencies(batch, self._committed_txn_cache):
            batches_to_add = []

            # Inject batches at the beginning of the block
            if not self._pending_batches:
                self._poll_injectors(lambda injector: injector.block_start(
                    self._block_builder.previous_block_id), batches_to_add)

            batches_to_add.append(batch)

            if not enforce_validation_rules(
                    self._settings_view,
                    self._signer_public_key,
                    self._pending_batches + batches_to_add):
                return

            for b in batches_to_add:
                self._pending_batches.append(b)
                self._pending_batch_ids.add(b.header_signature)
                try:
                    injected = b.header_signature in self._injected_batch_ids
                    self._scheduler.add_batch(b, required=injected)
                except SchedulerError as err:
                    LOGGER.debug("Scheduler error processing batch: %s", err)
        else:
            LOGGER.debug("Dropping batch due to missing dependencies: %s",
                         batch.header_signature)