Exemplo n.º 1
0
    def _generate_batch(self, payload):
        payload_encoded = payload.encode('utf-8')
        hasher = hashlib.sha512()
        hasher.update(payload_encoded)

        header = TransactionHeader()
        header.batcher_pubkey = self.public_key
        # txn.dependencies not yet
        header.family_name = 'test'
        header.family_version = '1'
        header.nonce = _generate_id(16)
        header.payload_encoding = "text"
        header.payload_sha512 = hasher.hexdigest().encode()
        header.signer_pubkey = self.public_key

        txn = Transaction()
        header_bytes = header.SerializeToString()
        txn.header = header_bytes
        txn.header_signature = signing.sign(header_bytes, self.signing_key)
        txn.payload = payload_encoded

        batch_header = BatchHeader()
        batch_header.signer_pubkey = self.public_key
        batch_header.transaction_ids.extend([txn.header_signature])

        batch = Batch()
        header_bytes = batch_header.SerializeToString()
        batch.header = header_bytes
        batch.header_signature = signing.sign(header_bytes, self.signing_key)
        batch.transactions.extend([txn])
        return batch
Exemplo n.º 2
0
def create_intkey_transaction(verb, name, value, deps, signer):
    payload = IntKeyPayload(verb=verb, name=name, value=value)

    # The prefix should eventually be looked up from the
    # validator's namespace registry.
    addr = make_intkey_address(name)

    header = TransactionHeader(
        signer_public_key=signer.get_public_key().as_hex(),
        family_name='intkey',
        family_version='1.0',
        inputs=[addr],
        outputs=[addr],
        dependencies=deps,
        payload_sha512=payload.sha512(),
        batcher_public_key=signer.get_public_key().as_hex())

    header_bytes = header.SerializeToString()

    signature = signer.sign(header_bytes)

    transaction = Transaction(header=header_bytes,
                              payload=payload.to_cbor(),
                              header_signature=signature)

    return transaction
Exemplo n.º 3
0
def create_transaction(signer, verb, name, value):
    payload = cbor.dumps({
        'Verb': verb,
        'Name': name,
        'Value': value
    },
                         sort_keys=True)

    addresses = [make_intkey_address(name)]

    nonce = hex(random.randint(0, 2**64))

    txn_pub_key = signer.get_public_key().as_hex()
    header = TransactionHeader(
        signer_public_key=txn_pub_key,
        family_name="intkey",
        family_version="1.0",
        inputs=addresses,
        outputs=addresses,
        dependencies=[],
        payload_sha512=hashlib.sha512(payload).hexdigest(),
        batcher_public_key=signer.get_public_key().as_hex(),
        nonce=nonce)

    signature = signer.sign(header.SerializeToString())

    return Transaction(header=header.SerializeToString(),
                       payload=payload,
                       header_signature=signature)
Exemplo n.º 4
0
    def _create_transactions(self, count):
        txn_list = []
        for i in range(count):
            payload = {'Verb': 'set', 'Name': 'name', 'Value': 1}
            intkey_prefix = \
                hashlib.sha512('intkey'.encode('utf-8')).hexdigest()[0:6]

            addr = intkey_prefix + \
                hashlib.sha512(payload["Name"].encode('utf-8')).hexdigest()

            payload_encode = hashlib.sha512(cbor.dumps(payload)).hexdigest()

            header = TransactionHeader(signer_public_key=self.public_key,
                                       family_name='intkey',
                                       family_version='1.0',
                                       inputs=[addr],
                                       outputs=[addr],
                                       dependencies=[],
                                       payload_sha512=payload_encode)

            header.batcher_public_key = self.public_key

            header_bytes = header.SerializeToString()

            signature = signing.sign(header_bytes, self.private_key)

            transaction = Transaction(header=header_bytes,
                                      payload=cbor.dumps(payload),
                                      header_signature=signature)

            txn_list.append(transaction)

        return txn_list
Exemplo n.º 5
0
    def create_batch(self, block_info):
        payload = BlockInfoTxn(block=block_info).SerializeToString()
        public_key = self._signer.get_public_key().as_hex()
        header = TransactionHeader(
            signer_public_key=public_key,
            family_name=FAMILY_NAME,
            family_version=FAMILY_VERSION,
            inputs=[CONFIG_ADDRESS, BLOCK_INFO_NAMESPACE],
            outputs=[CONFIG_ADDRESS, BLOCK_INFO_NAMESPACE],
            dependencies=[],
            payload_sha512=hashlib.sha512(payload).hexdigest(),
            batcher_public_key=public_key,
        ).SerializeToString()

        transaction_signature = self._signer.sign(header)

        transaction = Transaction(
            header=header,
            payload=payload,
            header_signature=transaction_signature,
        )

        header = BatchHeader(
            signer_public_key=public_key,
            transaction_ids=[transaction_signature],
        ).SerializeToString()

        batch_signature = self._signer.sign(header)

        return Batch(
            header=header,
            transactions=[transaction],
            header_signature=batch_signature,
        )
Exemplo n.º 6
0
def _create_role_txn(signer, role_name, policy_name):
    role = Role(name=role_name, policy_name=policy_name)
    payload = IdentityPayload(type=IdentityPayload.ROLE,
                              data=role.SerializeToString())

    policy_address = _policy_to_address(policy_name)
    role_address = _role_to_address(role_name)

    header = TransactionHeader(
        signer_public_key=signer.get_public_key().as_hex(),
        family_name='sawtooth_identity',
        family_version='1.0',
        inputs=[_REQUIRED_INPUT, policy_address, role_address],
        outputs=[role_address],
        dependencies=[],
        payload_sha512=hashlib.sha512(payload.SerializeToString()).hexdigest(),
        batcher_public_key=signer.get_public_key().as_hex(),
        nonce=hex(random.randint(0, 2**64)))

    header_bytes = header.SerializeToString()

    transaction = Transaction(header=header_bytes,
                              payload=payload.SerializeToString(),
                              header_signature=signer.sign(header_bytes))

    return transaction
Exemplo n.º 7
0
def _create_policy_txn(signer, policy_name, rules):
    entries = []
    for rule in rules:
        rule = rule.split(" ")
        if rule[0] == "PERMIT_KEY":
            entry = Policy.Entry(type=Policy.PERMIT_KEY, key=rule[1])
            entries.append(entry)
        elif rule[0] == "DENY_KEY":
            entry = Policy.Entry(type=Policy.DENY_KEY, key=rule[1])
            entries.append(entry)
    policy = Policy(name=policy_name, entries=entries)
    payload = IdentityPayload(type=IdentityPayload.POLICY,
                              data=policy.SerializeToString())

    policy_address = _policy_to_address(policy_name)

    header = TransactionHeader(
        signer_public_key=signer.get_public_key().as_hex(),
        family_name='sawtooth_identity',
        family_version='1.0',
        inputs=[_REQUIRED_INPUT, policy_address],
        outputs=[policy_address],
        dependencies=[],
        payload_sha512=hashlib.sha512(payload.SerializeToString()).hexdigest(),
        batcher_public_key=signer.get_public_key().as_hex(),
        nonce=hex(random.randint(0, 2**64)))

    header_bytes = header.SerializeToString()

    transaction = Transaction(header=header_bytes,
                              payload=payload.SerializeToString(),
                              header_signature=signer.sign(header_bytes))

    return transaction
Exemplo n.º 8
0
def _make_mock_transaction(self, txn_id='txn_id', payload='payload'):
    header = TransactionHeader(batcher_pubkey='pubkey',
                               family_name='family',
                               family_version='0.0',
                               nonce=txn_id,
                               signer_pubkey='pubkey')

    return Transaction(header=header.SerializeToString(),
                       header_signature=txn_id,
                       payload=payload.encode())
Exemplo n.º 9
0
    def get_transaction(self, transaction_id):
        """Returns a Transaction object from the block store by its id.

        Params:
            transaction_id (str): The header_signature of the desired txn

        Returns:
            Transaction: The specified transaction

        Raises:
            ValueError: The transaction is not in the block store
        """
        payload = self._get_data_by_id(transaction_id,
                                       'commit_store_get_transaction')

        txn = Transaction()
        txn.ParseFromString(payload)

        return txn
Exemplo n.º 10
0
def _make_mock_transaction(base_id='id', payload='payload'):
    txn_id = 'c' * (128 - len(base_id)) + base_id
    header = TransactionHeader(batcher_public_key='public_key-' + base_id,
                               family_name='family',
                               family_version='0.0',
                               nonce=txn_id,
                               signer_public_key='public_key-' + base_id)
    return Transaction(header=header.SerializeToString(),
                       header_signature=txn_id,
                       payload=payload.encode())
Exemplo n.º 11
0
    def _create_transactions(self,
                             count,
                             matched_payload=True,
                             valid_signature=True,
                             valid_batcher=True):
        txn_list = []

        for i in range(count):
            payload = {'Verb': 'set',
                       'Name': 'name' + str(random.randint(0, 100)),
                       'Value': random.randint(0, 100)}
            intkey_prefix = \
                hashlib.sha512('intkey'.encode('utf-8')).hexdigest()[0:6]

            addr = intkey_prefix + \
                hashlib.sha512(payload["Name"].encode('utf-8')).hexdigest()

            payload_encode = hashlib.sha512(cbor.dumps(payload)).hexdigest()

            header = TransactionHeader(
                signer_pubkey=self.public_key,
                family_name='intkey',
                family_version='1.0',
                inputs=[addr],
                outputs=[addr],
                dependencies=[],
                payload_encoding="application/cbor",
                payload_sha512=payload_encode)

            if valid_batcher:
                header.batcher_pubkey = self.public_key
            else:
                header.batcher_pubkey = "bad_batcher"

            header_bytes = header.SerializeToString()

            if valid_signature:
                signature = signing.sign(
                    header_bytes,
                    self.private_key)
            else:
                signature = "bad_signature"

            if not matched_payload:
                payload['Name'] = 'unmatched_payload'

            transaction = Transaction(
                header=header_bytes,
                payload=cbor.dumps(payload),
                header_signature=signature)

            txn_list.append(transaction)

        return txn_list
Exemplo n.º 12
0
    def _make_block(self, txns_family, signer_pubkey, same_pubkey=True):
        transactions = []
        for family in txns_family:
            txn_header = TransactionHeader(
                family_name=family,
                signer_pubkey=signer_pubkey)
            txn = Transaction(header=txn_header.SerializeToString())
            transactions.append(txn)

        batch = Batch(transactions=transactions)
        if same_pubkey:
            block_header = BlockHeader(signer_pubkey=signer_pubkey)
        else:
            block_header = BlockHeader(signer_pubkey="other")
        block = Block(header=block_header.SerializeToString(), batches=[batch])
        return BlockWrapper(block)
Exemplo n.º 13
0
def _make_txn(signer, setting_key, payload):
    """Creates and signs a bgx_settings transaction with with a payload.
    """
    serialized_payload = payload.SerializeToString()
    header = TransactionHeader(
        signer_public_key=signer.get_public_key().as_hex(),
        family_name='bgx_settings',  # 'sawtooth_settings'
        family_version='1.0',
        inputs=_config_inputs(setting_key),
        outputs=_config_outputs(setting_key),
        dependencies=[],
        payload_sha512=hashlib.sha512(serialized_payload).hexdigest(),
        batcher_public_key=signer.get_public_key().as_hex()).SerializeToString(
        )

    return Transaction(header=header,
                       header_signature=signer.sign(header),
                       payload=serialized_payload)
Exemplo n.º 14
0
    def test_batch_with_missing_dep(self):
        """
        Add batch to completer that has a missing dependency. The missing
        transaction's batch should be requested add the missing batch is then
        added to the completer. The incomplete batch should be rechecked
        and passed to on_batch_received.
        """
        batch = self._create_batches(1, 1, missing_dep=True)[0]
        self.completer.add_batch(batch)
        self.assertIn("Missing", self.gossip.requested_batches_by_txn_id)

        missing = Transaction(header_signature="Missing")
        missing_batch = Batch(header_signature="Missing_batch",
                              transactions=[missing])
        self.completer.add_batch(missing_batch)
        self.assertIn(missing_batch.header_signature, self.batches)
        self.assertIn(batch.header_signature, self.batches)
        self.assertEqual(missing_batch,
                         self.completer.get_batch_by_transaction("Missing"))
Exemplo n.º 15
0
    def generate_transaction(self, payload='txn', deps=None):
        payload_encoded = payload.encode('utf-8')
        hasher = hashlib.sha512()
        hasher.update(payload_encoded)

        txn_header = TransactionHeader(
            dependencies=([] if deps is None else deps),
            batcher_public_key=self.public_key,
            family_name='test',
            family_version='1',
            nonce=_generate_id(16),
            payload_sha512=hasher.hexdigest().encode(),
            signer_public_key=self.public_key).SerializeToString()

        txn = Transaction(header=txn_header,
                          header_signature=self._signed_header(txn_header),
                          payload=payload_encoded)

        return txn
Exemplo n.º 16
0
    def _create_transactions(self, count, missing_dep=False):
        txn_list = []

        for _ in range(count):
            payload = {
                'Verb': 'set',
                'Name': 'name' + str(random.randint(0, 100)),
                'Value': random.randint(0, 100)
            }
            intkey_prefix = \
                hashlib.sha512('intkey'.encode('utf-8')).hexdigest()[0:6]

            addr = intkey_prefix + \
                hashlib.sha512(payload["Name"].encode('utf-8')).hexdigest()

            payload_encode = hashlib.sha512(cbor.dumps(payload)).hexdigest()

            header = TransactionHeader(
                signer_public_key=self.signer.get_public_key().as_hex(),
                family_name='intkey',
                family_version='1.0',
                inputs=[addr],
                outputs=[addr],
                dependencies=[],
                batcher_public_key=self.signer.get_public_key().as_hex(),
                payload_sha512=payload_encode)

            if missing_dep:
                header.dependencies.extend(["Missing"])

            header_bytes = header.SerializeToString()

            signature = self.signer.sign(header_bytes)

            transaction = Transaction(
                header=header_bytes,
                payload=cbor.dumps(payload),
                header_signature=signature)

            txn_list.append(transaction)

        return txn_list
 def gen_txn(self, txn_id, deps=None):
     return Transaction(
         header_signature=txn_id,
         header=TransactionHeader(dependencies=deps).SerializeToString())