github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_batch_tracker/tests.py (about) 1 # Copyright 2018 Intel Corporation 2 # 3 # Licensed under the Apache License, Version 2.0 (the "License"); 4 # you may not use this file except in compliance with the License. 5 # You may obtain a copy of the License at 6 # 7 # http://www.apache.org/licenses/LICENSE-2.0 8 # 9 # Unless required by applicable law or agreed to in writing, software 10 # distributed under the License is distributed on an "AS IS" BASIS, 11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 # See the License for the specific language governing permissions and 13 # limitations under the License. 14 # ------------------------------------------------------------------------------ 15 import unittest 16 from unittest.mock import Mock 17 18 from sawtooth_validator.protobuf import batch_pb2 19 from sawtooth_validator.protobuf import transaction_pb2 20 from sawtooth_validator.state.batch_tracker import BatchTracker 21 22 23 class BatchTrackerTest(unittest.TestCase): 24 def test_invalid_txn_infos(self): 25 """Test that the invalid batch information is return correctly. 26 27 - Add valid batch info 28 - Add invalid batch info 29 - Ensure that the invalid batch info is returned 30 - Ensure that modifying the returned info does not affect future calls 31 """ 32 block_store = Mock() 33 batch_tracker = BatchTracker(block_store) 34 35 batch_tracker.notify_batch_pending( 36 make_batch("good_batch", "good_txn")) 37 batch_tracker.notify_batch_pending( 38 make_batch("bad_batch", "bad_txn")) 39 40 batch_tracker.notify_txn_invalid("bad_txn") 41 42 invalid_info = batch_tracker.get_invalid_txn_info("bad_batch") 43 self.assertEqual(1, len(invalid_info)) 44 self.assertEqual("bad_txn", invalid_info[0]["id"]) 45 46 invalid_info[0]["header_signature"] = invalid_info[0].pop("id") 47 48 more_invalid_info = batch_tracker.get_invalid_txn_info("bad_batch") 49 self.assertEqual(1, len(more_invalid_info)) 50 self.assertEqual("bad_txn", more_invalid_info[0]["id"]) 51 52 53 def make_batch(batch_id, txn_id): 54 transaction = transaction_pb2.Transaction(header_signature=txn_id) 55 batch = batch_pb2.Batch( 56 header_signature=batch_id, transactions=[transaction]) 57 58 return batch