github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-core-master/validator/tests/test_message_validation/tests.py (about) 1 # Copyright 2016 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 import hashlib 17 import random 18 import string 19 20 import cbor 21 22 from sawtooth_signing import create_context 23 from sawtooth_signing import CryptoFactory 24 from sawtooth_validator.protobuf.transaction_pb2 import TransactionHeader, \ 25 Transaction 26 from sawtooth_validator.protobuf.batch_pb2 import BatchHeader, Batch 27 from sawtooth_validator.protobuf.block_pb2 import BlockHeader, Block 28 from sawtooth_validator.gossip import signature_verifier as verifier 29 from sawtooth_validator.gossip import structure_verifier 30 31 32 class TestMessageValidation(unittest.TestCase): 33 def setUp(self): 34 context = create_context('secp256k1') 35 private_key = context.new_random_private_key() 36 crypto_factory = CryptoFactory(context) 37 self.signer = crypto_factory.new_signer(private_key) 38 39 @property 40 def public_key(self): 41 return self.signer.get_public_key().as_hex() 42 43 def broadcast(self, msg): 44 pass 45 46 def _create_transactions(self, 47 count, 48 matched_payload=True, 49 valid_signature=True, 50 valid_batcher=True): 51 txn_list = [] 52 53 for _ in range(count): 54 payload = { 55 'Verb': 'set', 56 'Name': 'name' + str(random.randint(0, 100)), 57 'Value': random.randint(0, 100) 58 } 59 intkey_prefix = \ 60 hashlib.sha512('intkey'.encode('utf-8')).hexdigest()[0:6] 61 62 addr = intkey_prefix + \ 63 hashlib.sha512(payload["Name"].encode('utf-8')).hexdigest() 64 65 payload_encode = hashlib.sha512(cbor.dumps(payload)).hexdigest() 66 67 header = TransactionHeader( 68 signer_public_key=self.public_key, 69 family_name='intkey', 70 family_version='1.0', 71 inputs=[addr], 72 outputs=[addr], 73 dependencies=[], 74 payload_sha512=payload_encode) 75 76 if valid_batcher: 77 header.batcher_public_key = self.public_key 78 else: 79 header.batcher_public_key = "bad_batcher" 80 81 header_bytes = header.SerializeToString() 82 83 if valid_signature: 84 signature = self.signer.sign(header_bytes) 85 else: 86 signature = "bad_signature" 87 88 if not matched_payload: 89 payload['Name'] = 'unmatched_payload' 90 91 transaction = Transaction( 92 header=header_bytes, 93 payload=cbor.dumps(payload), 94 header_signature=signature) 95 96 txn_list.append(transaction) 97 98 return txn_list 99 100 def _generate_id(self): 101 return hashlib.sha512(''.join( 102 [random.choice(string.ascii_letters) 103 for _ in range(0, 1024)]).encode()).hexdigest() 104 105 def _create_batches(self, batch_count, txn_count, 106 valid_batch=True, valid_txn=True, 107 valid_structure=True, valid_batcher=True): 108 109 batch_list = [] 110 111 for _ in range(batch_count): 112 txn_list = self._create_transactions(txn_count, valid_txn, 113 valid_batcher) 114 txn_sig_list = [txn.header_signature for txn in txn_list] 115 if not valid_structure: 116 txn_sig_list.pop() 117 118 batch_header = BatchHeader(signer_public_key=self.public_key) 119 batch_header.transaction_ids.extend(txn_sig_list) 120 121 header_bytes = batch_header.SerializeToString() 122 123 if valid_batch: 124 signature = self.signer.sign(header_bytes) 125 else: 126 signature = "bad_signature" 127 128 batch = Batch( 129 header=header_bytes, 130 transactions=txn_list, 131 header_signature=signature) 132 133 batch_list.append(batch) 134 135 return batch_list 136 137 def _create_blocks(self, block_count, batch_count, 138 valid_block=True, valid_batch=True): 139 block_list = [] 140 141 for _ in range(block_count): 142 batch_list = self._create_batches( 143 batch_count, 2, valid_batch=valid_batch) 144 batch_ids = [batch.header_signature for batch in batch_list] 145 146 block_header = BlockHeader(signer_public_key=self.public_key, 147 batch_ids=batch_ids) 148 149 header_bytes = block_header.SerializeToString() 150 151 if valid_block: 152 signature = self.signer.sign(header_bytes) 153 else: 154 signature = "bad_signature" 155 156 block = Block(header=header_bytes, 157 batches=batch_list, 158 header_signature=signature) 159 160 block_list.append(block) 161 162 return block_list 163 164 def test_valid_transaction(self): 165 txn_list = self._create_transactions(1) 166 txn = txn_list[0] 167 valid = verifier.is_valid_transaction(txn) 168 self.assertTrue(valid) 169 170 def test_invalid_transaction(self): 171 # add invalid flag to _create transaction 172 txn_list = self._create_transactions(1, valid_signature=False) 173 txn = txn_list[0] 174 valid = verifier.is_valid_transaction(txn) 175 self.assertFalse(valid) 176 177 def test_unmatched_payload_transaction(self): 178 # add invalid flag to _create transaction 179 txn_list = self._create_transactions(1, matched_payload=False) 180 txn = txn_list[0] 181 valid = verifier.is_valid_transaction(txn) 182 self.assertFalse(valid) 183 184 def test_valid_batch(self): 185 batch_list = self._create_batches(1, 10) 186 batch = batch_list[0] 187 valid = verifier.is_valid_batch(batch) 188 self.assertTrue(valid) 189 190 def test_invalid_batch(self): 191 # add invalid flag to create_batches 192 batch_list = self._create_batches(1, 1, valid_batch=False) 193 batch = batch_list[0] 194 valid = verifier.is_valid_batch(batch) 195 self.assertFalse(valid) 196 197 # create an invalid txn in the batch 198 batch_list = self._create_batches(1, 1, valid_txn=False) 199 batch = batch_list[0] 200 valid = verifier.is_valid_batch(batch) 201 self.assertFalse(valid) 202 203 # create an invalid txn with bad batcher 204 batch_list = self._create_batches(1, 1, valid_batcher=False) 205 batch = batch_list[0] 206 valid = verifier.is_valid_batch(batch) 207 self.assertFalse(valid) 208 209 def test_invalid_batch_structure(self): 210 batch_list = self._create_batches(1, 2, valid_structure=False) 211 batch = batch_list[0] 212 valid = structure_verifier.is_valid_batch(batch) 213 self.assertFalse(valid) 214 215 def test_valid_block(self): 216 block_list = self._create_blocks(1, 1) 217 block = block_list[0] 218 valid = verifier.is_valid_block(block) 219 self.assertTrue(valid) 220 221 def test_invalid_block(self): 222 block_list = self._create_blocks(1, 1, valid_batch=False) 223 block = block_list[0] 224 valid = verifier.is_valid_block(block) 225 self.assertFalse(valid) 226 227 block_list = self._create_blocks(1, 1, valid_block=False) 228 block = block_list[0] 229 valid = verifier.is_valid_block(block) 230 self.assertFalse(valid)