Esempio n. 1
0
def test_get_taa_data(
        config_req_handler: ConfigReqHandler,
        taa_input_data, taa_expected_data, taa_expected_digests
):
    """ `get_taa_data` returns expected value """
    written = []
    for data in taa_input_data:
        config_req_handler.update_txn_author_agreement(*data)
        written.append(data.version)

        assert (
            config_req_handler.get_taa_data(isCommitted=False) ==
            (taa_expected_data[data.version], taa_expected_digests[data.version])
        )

        for version in taa_expected_data:
            expected = (
                (taa_expected_data[version], taa_expected_digests[version])
                if version in written else None
            )
            assert (
                expected ==
                config_req_handler.get_taa_data(version=version, isCommitted=False)
            )
            assert (
                expected ==
                config_req_handler.get_taa_data(
                    digest=taa_expected_digests[version],
                    version='any-version-since-ignored',
                    isCommitted=False
                )
            )
Esempio n. 2
0
def test_send_valid_txn_athr_agrmt_succeeds(looper, txnPoolNodeSet,
                                            sdk_pool_handle,
                                            sdk_wallet_trustee):
    req = looper.loop.run_until_complete(
        prepare_txn_author_agreement(sdk_wallet_trustee[1]))
    rep = sdk_sign_and_send_prepared_request(looper, sdk_wallet_trustee,
                                             sdk_pool_handle, req)
    sdk_get_and_check_replies(looper, [rep])

    req = json.loads(req)
    version = req[OPERATION][TXN_AUTHOR_AGREEMENT_VERSION]
    text = req[OPERATION][TXN_AUTHOR_AGREEMENT_TEXT]
    digest = ConfigReqHandler._taa_digest(version, text)

    # TODO: Replace this with get transaction
    ensure_all_nodes_have_same_data(looper, txnPoolNodeSet)
    for node in txnPoolNodeSet:
        config_req_handler = get_config_req_handler(node)
        assert config_req_handler.get_taa_digest() == digest.encode()
        assert config_req_handler.get_taa_digest(version) == digest.encode()

        taa = config_req_handler.state.get(
            ConfigReqHandler._state_path_taa_digest(digest))
        assert taa is not None

        taa = json.loads(taa.decode())
        assert taa[TXN_AUTHOR_AGREEMENT_VERSION] == version
        assert taa[TXN_AUTHOR_AGREEMENT_TEXT] == text
Esempio n. 3
0
def test_multiple_update_txn_author_agreement(config_req_handler: ConfigReqHandler):
    text_v1 = 'Some agreement'
    agreement_v1 = {TXN_AUTHOR_AGREEMENT_VERSION: 'v1', TXN_AUTHOR_AGREEMENT_TEXT: text_v1}
    digest_v1 = ConfigReqHandler._taa_digest('v1', text_v1).encode()
    config_req_handler.update_txn_author_agreement(agreement_v1)

    assert config_req_handler.get_taa_digest(isCommitted=False) == digest_v1
    assert config_req_handler.get_taa_digest(version='v1', isCommitted=False) == digest_v1
    assert config_req_handler.get_taa_digest(version='v2', isCommitted=False) is None

    text_v2 = 'New agreement'
    agreement_v2 = {TXN_AUTHOR_AGREEMENT_VERSION: 'v2', TXN_AUTHOR_AGREEMENT_TEXT: text_v2}
    digest_v2 = ConfigReqHandler._taa_digest('v2', text_v2).encode()
    config_req_handler.update_txn_author_agreement(agreement_v2)

    assert config_req_handler.get_taa_digest(isCommitted=False) == digest_v2
    assert config_req_handler.get_taa_digest(version='v1', isCommitted=False) == digest_v1
    assert config_req_handler.get_taa_digest(version='v2', isCommitted=False) == digest_v2
Esempio n. 4
0
def test_update_txn_author_agreement(
        config_req_handler: ConfigReqHandler, taa_input_data,
        taa_expected_state_data, taa_expected_digests
):
    """ `update_txn_author_agreement` updates state properly """
    state = config_req_handler.state
    written = []

    def _check_state(version):
        digest = taa_expected_digests[version]

        _digest = state.get(
            ConfigReqHandler._state_path_taa_version(version),
            isCommitted=False
        )
        _data = state.get(
            ConfigReqHandler._state_path_taa_digest(digest),
            isCommitted=False
        )

        if version in written:
            assert _digest == digest.encode()
            assert (
                config_state_serializer.deserialize(_data) ==
                taa_expected_state_data[version]
            )
        else:
            assert _digest is None
            assert _data is None

    for data in taa_input_data:
        config_req_handler.update_txn_author_agreement(
            data.text, data.version, data.seq_no, data.txn_time)
        written.append(data.version)

        digest = taa_expected_digests[data.version]
        assert state.get(
            ConfigReqHandler._state_path_taa_latest(),
            isCommitted=False
        ) == digest.encode()

        for version in taa_expected_state_data:
            _check_state(version)
Esempio n. 5
0
    def _check_state(version):
        digest = taa_expected_digests[version]

        _digest = state.get(
            ConfigReqHandler._state_path_taa_version(version),
            isCommitted=False
        )
        _data = state.get(
            ConfigReqHandler._state_path_taa_digest(digest),
            isCommitted=False
        )

        if version in written:
            assert _digest == digest.encode()
            assert (
                config_state_serializer.deserialize(_data) ==
                taa_expected_state_data[version]
            )
        else:
            assert _digest is None
            assert _data is None
Esempio n. 6
0
def test_get_taa_digest(
        config_req_handler: ConfigReqHandler, taa_input_data,
        taa_expected_data, taa_expected_digests
):
    """ `get_taa_digest` returns expected value """
    written = []
    for data in taa_input_data:
        config_req_handler.update_txn_author_agreement(*data)
        written.append(data.version)

        assert (
            config_req_handler.get_taa_digest(isCommitted=False) ==
            taa_expected_digests[data.version]
        )

        for version in taa_expected_data:
            digest = config_req_handler.get_taa_digest(
                version=version, isCommitted=False)
            assert (
                digest ==
                (taa_expected_digests[version] if version in written else None)
            )
Esempio n. 7
0
    def _check_state(version):
        expected_data = taa_aml_expected_data[version]

        _data = state.get(
            ConfigReqHandler._state_path_taa_aml_version(version),
            isCommitted=False
        )

        if version in written:
            assert (config_state_serializer.deserialize(_data)[VALUE] ==
                    config_state_serializer.deserialize(expected_data))
        else:
            assert _data is None
Esempio n. 8
0
def test_update_taa_aml(
        config_req_handler: ConfigReqHandler, taa_aml_input_data,
        taa_aml_expected_state_data, taa_aml_expected_data):
    """ `update_txn_author_agreement` updates state properly """
    state = config_req_handler.state
    written = []

    def _check_state(version):
        expected_data = taa_aml_expected_data[version]

        _data = state.get(
            ConfigReqHandler._state_path_taa_aml_version(version),
            isCommitted=False
        )

        if version in written:
            assert (config_state_serializer.deserialize(_data)[VALUE] ==
                    config_state_serializer.deserialize(expected_data))
        else:
            assert _data is None

    for data in taa_aml_input_data:
        config_req_handler.update_txn_author_agreement_acceptance_mechanisms(
            {AML_VERSION: data.version, AML: data.aml, AML_CONTEXT: data.amlContext}, data.seq_no, data.txn_time)
        written.append(data.version)

        data_d = data._asdict()
        assert state.get(
            ConfigReqHandler._state_path_taa_aml_latest(),
            isCommitted=False) == encode_state_value({AML_CONTEXT: data_d[AML_CONTEXT],
                                                      AML: data_d[AML],
                                                      AML_VERSION: data_d[AML_VERSION]
                                                      },
                                                     data_d['seq_no'],
                                                     data_d['txn_time'])

        for version in taa_aml_expected_state_data:
            _check_state(version)
Esempio n. 9
0
def test_get_taa_digest(config_req_handler: ConfigReqHandler):
    agreements = [{TXN_AUTHOR_AGREEMENT_VERSION: randomString(8),
                   TXN_AUTHOR_AGREEMENT_TEXT: randomString(32)} for _ in range(10)]
    agreements = [(payload,
                   ConfigReqHandler._taa_digest(payload[TXN_AUTHOR_AGREEMENT_VERSION],
                                                payload[TXN_AUTHOR_AGREEMENT_TEXT]))
                  for payload in agreements]
    versions = [payload.get(TXN_AUTHOR_AGREEMENT_VERSION) for payload, _ in agreements]
    state = config_req_handler.state

    for payload, digest in agreements:
        config_req_handler.update_txn_author_agreement(payload)

        assert config_req_handler.get_taa_digest(isCommitted=False) == \
               state.get(ConfigReqHandler._state_path_taa_latest(), isCommitted=False)

        for version in versions:
            assert config_req_handler.get_taa_digest(version=version, isCommitted=False) == \
                   state.get(ConfigReqHandler._state_path_taa_version(version), isCommitted=False)
Esempio n. 10
0
def test_update_txn_author_agreement(config_req_handler: ConfigReqHandler):
    version = 'Some version'
    text = 'Some agreement'
    agreement = {TXN_AUTHOR_AGREEMENT_VERSION: version, TXN_AUTHOR_AGREEMENT_TEXT: text}
    digest = ConfigReqHandler._taa_digest(version, text)
    config_req_handler.update_txn_author_agreement(agreement)
    state = config_req_handler.state

    assert state.get(ConfigReqHandler._state_path_taa_latest(), isCommitted=False) == digest.encode()
    assert state.get(ConfigReqHandler._state_path_taa_version(version), isCommitted=False) == digest.encode()

    taa = state.get(ConfigReqHandler._state_path_taa_digest(digest), isCommitted=False)
    assert taa is not None

    taa = json.loads(taa.decode())
    assert taa.get(TXN_AUTHOR_AGREEMENT_VERSION) == version
    assert taa.get(TXN_AUTHOR_AGREEMENT_TEXT) == text
Esempio n. 11
0
def test_state_path_taa_latest():
    assert ConfigReqHandler._state_path_taa_latest() == b'taa:v:latest'
Esempio n. 12
0
def test_taa_digest():
    assert ConfigReqHandler._taa_digest('some_version', 'some_text') == \
           "fb2ea9d28380a021ec747c442d62a68952b4b5813b45671098ad2b684b2f4646"
Esempio n. 13
0
def test_state_path_taa_version():
    assert ConfigReqHandler._state_path_taa_version('some_version') == b'taa:v:some_version'
Esempio n. 14
0
def test_state_path_taa_aml_latest():
    assert ConfigReqHandler._state_path_taa_aml_latest() == b'3:latest'
Esempio n. 15
0
def calc_taa_digest(text, version):
    return ConfigReqHandler._taa_digest(text, version)
Esempio n. 16
0
def config_req_handler(config_state, config_ledger):
    return ConfigReqHandler(config_ledger,
                            config_state,
                            domain_state=FakeSomething(),
                            bls_store=FakeSomething())
Esempio n. 17
0
def test_state_path_taa_digest():
    assert ConfigReqHandler._state_path_taa_digest('some_digest') == b'taa:d:some_digest'