def test_recorder_get_next_incoming_only(recorder): incoming_count = 100 incoming = [(randomString(100), randomString(6)) for _ in range(incoming_count)] while incoming: recorder.add_incoming(*incoming.pop()) time.sleep(random.choice([0, 1]) + random.random()) recorded_incomings = OrderedDict() keys = [] for k, v in recorder.store.iterator(include_value=True): v = Recorder.get_parsed(v) keys.append(int(k)) recorded_incomings[int(k)] = v assert len(recorded_incomings) == incoming_count assert sorted(keys) == keys max_time_to_run = incoming_count * 2 + 10 recorder.start_playing() start = time.perf_counter() while recorder.is_playing and (time.perf_counter() < start + max_time_to_run): vals = recorder.get_next() if vals: check = recorded_incomings.popitem(last=False)[1] assert check == vals else: time.sleep(0.01) assert len(recorded_incomings) == 0 assert not recorder.is_playing
def add_revoc_def_by_default(create_node_and_not_start, looper, sdk_wallet_steward): node = create_node_and_not_start data = { ID: randomString(50), TXN_TYPE: REVOC_REG_DEF, REVOC_TYPE: "CL_ACCUM", TAG: randomString(5), CRED_DEF_ID: ":".join(4*[randomString(10)]), VALUE:{ ISSUANCE_TYPE: ISSUANCE_BY_DEFAULT, MAX_CRED_NUM: 1000000, TAILS_HASH: randomString(50), TAILS_LOCATION: 'http://tails.location.com', PUBLIC_KEYS: {}, } } req = sdk_sign_request_from_dict(looper, sdk_wallet_steward, data) req_handler = node.getDomainReqHandler() txn = append_txn_metadata(reqToTxn(Request(**req)), txn_time=int(time.time()), seq_no=node.domainLedger.seqNo + 1) req_handler._addRevocDef(txn) return req
def test_split_messages_on_batches(): str_len = 10 max_depth = 2 ** 9 msg_limit = len(json.dumps([{1: randomString(str_len)}])) msgs = [{1: randomString(str_len)} for i in range(max_depth)] res = split_messages_on_batches(msgs, json.dumps, lambda l: l <= msg_limit) assert res is not None
def sdk_change_bls_key(looper, txnPoolNodeSet, node, sdk_pool_handle, sdk_wallet_steward, add_wrong=False, new_bls=None): new_blspk = init_bls_keys(node.keys_dir, node.name) key_in_txn = new_bls or new_blspk \ if not add_wrong \ else base58.b58encode(randomString(128).encode()) node_dest = hexToFriendly(node.nodestack.verhex) sdk_send_update_node(looper, sdk_wallet_steward, sdk_pool_handle, node_dest, node.name, None, None, None, None, bls_key=key_in_txn, services=None) poolSetExceptOne = list(txnPoolNodeSet) poolSetExceptOne.remove(node) waitNodeDataEquality(looper, node, *poolSetExceptOne) sdk_pool_refresh(looper, sdk_pool_handle) sdk_add_new_nym(looper, sdk_pool_handle, sdk_wallet_steward, alias=randomString(5)) ensure_all_nodes_have_same_data(looper, txnPoolNodeSet) return new_blspk
def test_state_regenerated_from_ledger(looper, nodeSet, tconf, tdir, sdk_pool_handle, sdk_wallet_trustee, allPluginsPath): """ Node loses its state database but recreates it from ledger after start. Checking ATTRIB txns too since they store some data off ledger too """ trust_anchors = [] for i in range(5): trust_anchors.append(sdk_add_new_nym(looper, sdk_pool_handle, sdk_wallet_trustee, 'TA' + str(i), TRUST_ANCHOR_STRING)) sdk_add_raw_attribute(looper, sdk_pool_handle, trust_anchors[-1], randomString(6), randomString(10)) for wh in trust_anchors: for i in range(3): sdk_add_new_nym(looper, sdk_pool_handle, wh, 'NP1' + str(i)) ensure_all_nodes_have_same_data(looper, nodeSet) node_to_stop = nodeSet[-1] node_state = node_to_stop.states[DOMAIN_LEDGER_ID] assert not node_state.isEmpty state_db_path = node_state._kv.db_path node_to_stop.cleanupOnStopping = False node_to_stop.stop() looper.removeProdable(node_to_stop) ensure_node_disconnected(looper, node_to_stop, nodeSet[:-1]) shutil.rmtree(state_db_path) config_helper = NodeConfigHelper(node_to_stop.name, tconf, chroot=tdir) restarted_node = TestNode( node_to_stop.name, config_helper=config_helper, config=tconf, pluginPaths=allPluginsPath, ha=node_to_stop.nodestack.ha, cliha=node_to_stop.clientstack.ha) looper.add(restarted_node) nodeSet[-1] = restarted_node looper.run(checkNodesConnected(nodeSet)) # Need some time as `last_ordered_3PC` is compared too and that is # communicated through catchup waitNodeDataEquality(looper, restarted_node, *nodeSet[:-1]) # Pool is still functional for wh in trust_anchors: sdk_add_new_nym(looper, sdk_pool_handle, wh, 'NP--' + randomString(5)) ensure_all_nodes_have_same_data(looper, nodeSet)
def test_plugin_client_req_fields(txn_pool_node_set_post_creation, looper, sdk_wallet_steward, sdk_pool_handle): """ Test that plugin's addition of request fields and their validation is successful """ op = { TXN_TYPE: GET_BAL, DATA: {'id': '123'} } # Valid field value results in successful processing req_obj = sdk_gen_request(op, identifier=sdk_wallet_steward[1], fix_length_dummy=randomString(dummy_field_length)) req = sdk_sign_and_submit_req_obj(looper, sdk_pool_handle, sdk_wallet_steward, req_obj) sdk_get_reply(looper, req) # Invalid field value results in proper failure _, did = sdk_wallet_steward req = sdk_gen_request(op, identifier=did, fix_length_dummy=randomString(dummy_field_length + 1)) reqs = sdk_sign_request_objects(looper, sdk_wallet_steward, [req]) reqs = sdk_send_signed_requests(sdk_pool_handle, reqs) with pytest.raises(RequestNackedException) as e: sdk_get_and_check_replies(looper, reqs) assert 'should have length' in e._excinfo[1].args[0]
def test_state_regenerated_from_ledger(looper, tdirWithPoolTxns, tdirWithDomainTxnsUpdated, nodeSet, tconf, trustee, trusteeWallet, allPluginsPath): """ Node loses its state database but recreates it from ledger after start. Checking ATTRIB txns too since they store some data off ledger too """ trust_anchors = [] for i in range(5): trust_anchors.append(getClientAddedWithRole(nodeSet, tdirWithPoolTxns, looper, trustee, trusteeWallet, 'TA' + str(i), role=TRUST_ANCHOR)) addRawAttribute(looper, *trust_anchors[-1], randomString(6), randomString(10), dest=trust_anchors[-1][1].defaultId) for tc, tw in trust_anchors: for i in range(3): getClientAddedWithRole(nodeSet, tdirWithPoolTxns, looper, tc, tw, 'NP1' + str(i)) ensure_all_nodes_have_same_data(looper, nodeSet) node_to_stop = nodeSet[-1] node_state = node_to_stop.states[DOMAIN_LEDGER_ID] assert not node_state.isEmpty state_db_path = node_state._kv._dbPath node_to_stop.cleanupOnStopping = False node_to_stop.stop() looper.removeProdable(node_to_stop) ensure_node_disconnected(looper, node_to_stop.name, nodeSet[:-1]) shutil.rmtree(state_db_path) restarted_node = TestNode(node_to_stop.name, basedirpath=tdirWithPoolTxns, config=tconf, pluginPaths=allPluginsPath, ha=node_to_stop.nodestack.ha, cliha=node_to_stop.clientstack.ha) looper.add(restarted_node) nodeSet[-1] = restarted_node looper.run(checkNodesConnected(nodeSet)) # Need some time as `last_ordered_3PC` is compared too and that is # communicated through catchup waitNodeDataEquality(looper, restarted_node, *nodeSet[:-1]) # Pool is still functional for tc, tw in trust_anchors: getClientAddedWithRole(nodeSet, tdirWithPoolTxns, looper, tc, tw, 'NP--{}'.format(tc.name)) ensure_all_nodes_have_same_data(looper, nodeSet)
def test_split_messages_by_size(): str_len = 10 msg_count = 100 msg_limit = len(json.dumps([{1: randomString(str_len)}])) msgs = [{1: randomString(str_len)} for i in range(msg_count)] res = split_messages_on_batches(msgs, json.dumps, lambda l: l <= msg_limit) assert len(res) == msg_count
def test_randomString_invalid_args(): for size in (None, '5', [4]): with pytest.raises(PlenumTypeError): randomString(size) for size in (-1, 0): with pytest.raises(PlenumValueError): randomString(size)
def testChangeHaPersistsPostNodesRestart(looper, txnPoolNodeSet, tdir, tconf, sdk_pool_handle, sdk_wallet_client, sdk_wallet_steward): new_steward_wallet, new_node = \ sdk_add_new_steward_and_node(looper, sdk_pool_handle, sdk_wallet_steward, 'AnotherSteward' + randomString(4), 'AnotherNode' + randomString(4), tdir, tconf) txnPoolNodeSet.append(new_node) looper.run(checkNodesConnected(txnPoolNodeSet)) sdk_pool_refresh(looper, sdk_pool_handle) node_new_ha, client_new_ha = genHa(2) logger.debug("{} changing HAs to {} {}".format(new_node, node_new_ha, client_new_ha)) # Making the change HA txn an confirming its succeeded node_dest = hexToFriendly(new_node.nodestack.verhex) sdk_send_update_node(looper, new_steward_wallet, sdk_pool_handle, node_dest, new_node.name, node_new_ha.host, node_new_ha.port, client_new_ha.host, client_new_ha.port) # Stopping existing nodes for node in txnPoolNodeSet: node.stop() looper.removeProdable(node) # Starting nodes again by creating `Node` objects since that simulates # what happens when starting the node with script restartedNodes = [] for node in txnPoolNodeSet[:-1]: config_helper = PNodeConfigHelper(node.name, tconf, chroot=tdir) restartedNode = TestNode(node.name, config_helper=config_helper, config=tconf, ha=node.nodestack.ha, cliha=node.clientstack.ha) looper.add(restartedNode) restartedNodes.append(restartedNode) # Starting the node whose HA was changed config_helper = PNodeConfigHelper(new_node.name, tconf, chroot=tdir) node = TestNode(new_node.name, config_helper=config_helper, config=tconf, ha=node_new_ha, cliha=client_new_ha) looper.add(node) restartedNodes.append(node) looper.run(checkNodesConnected(restartedNodes)) waitNodeDataEquality(looper, node, *restartedNodes[:-1]) sdk_pool_refresh(looper, sdk_pool_handle) sdk_ensure_pool_functional(looper, restartedNodes, sdk_wallet_client, sdk_pool_handle)
def populate_log_with_upgrade_events( pool_txn_node_names, tdir, tconf, version: Tuple[str, str, str]): for nm in pool_txn_node_names: config_helper = NodeConfigHelper(nm, tconf, chroot=tdir) ledger_dir = config_helper.ledger_dir os.makedirs(ledger_dir) log = UpgradeLog(os.path.join(ledger_dir, tconf.upgradeLogFile)) when = datetime.utcnow().replace(tzinfo=dateutil.tz.tzutc()) log.appendScheduled(when, version, randomString(10)) log.appendStarted(when, version, randomString(10))
def test_random_string1(): assert (len(randomString(3)) == 3), \ "Function randomString(3) did not return string of len 3 characters" assert (len(randomString(20)) == 20), \ "Function randomString() did not return string of default len 20 characters" assert (len(randomString(32)) == 32), \ "Function call randomString(32) did not return string of len 32 characters" assert (len(randomString(128)) == 128), \ "Function randomString(128) did not return string of len 128 characters" assert (len(randomString(12800)) == 12800), \ "Function randomString(12800) did not return string of len 12800 characters"
def replica(replica): replica.node.requests = Requests() replica.isMaster = True replica.node.replica = replica replica.node.doDynamicValidation = functools.partial(randomDynamicValidation, replica.node) replica.node.applyReq = lambda self, *args, **kwargs: True replica.stateRootHash = lambda self, *args, **kwargs: base58.b58encode(randomString(32)).decode() replica.txnRootHash = lambda self, *args, **kwargs: base58.b58encode(randomString(32)).decode() replica.node.onBatchCreated = lambda self, *args, **kwargs: True replica.requestQueues[DOMAIN_LEDGER_ID] = OrderedSet() return replica
def nodeThetaAdded(looper, nodeSet, tdirWithPoolTxns, tconf, steward, stewardWallet, allPluginsPath, testNodeClass, testClientClass, tdir): newStewardName = "testClientSteward" + randomString(3) newNodeName = "Theta" newSteward, newStewardWallet = getClientAddedWithRole(nodeSet, tdir, looper, steward, stewardWallet, newStewardName, role=STEWARD) sigseed = randomString(32).encode() nodeSigner = SimpleSigner(seed=sigseed) (nodeIp, nodePort), (clientIp, clientPort) = genHa(2) data = { NODE_IP: nodeIp, NODE_PORT: nodePort, CLIENT_IP: clientIp, CLIENT_PORT: clientPort, ALIAS: newNodeName, SERVICES: [VALIDATOR, ] } node = Node(nodeSigner.identifier, data, newStewardWallet.defaultId) newStewardWallet.addNode(node) reqs = newStewardWallet.preparePending() req, = newSteward.submitReqs(*reqs) waitForSufficientRepliesForRequests(looper, newSteward, requests=[req]) def chk(): assert newStewardWallet.getNode(node.id).seqNo is not None timeout = plenumWaits.expectedTransactionExecutionTime(len(nodeSet)) looper.run(eventually(chk, retryWait=1, timeout=timeout)) initNodeKeysForBothStacks(newNodeName, tdirWithPoolTxns, sigseed, override=True) newNode = testNodeClass(newNodeName, basedirpath=tdir, config=tconf, ha=(nodeIp, nodePort), cliha=(clientIp, clientPort), pluginPaths=allPluginsPath) nodeSet.append(newNode) looper.add(newNode) looper.run(checkNodesConnected(nodeSet)) ensureClientConnectedToNodesAndPoolLedgerSame(looper, steward, *nodeSet) ensureClientConnectedToNodesAndPoolLedgerSame(looper, newSteward, *nodeSet) return newSteward, newStewardWallet, newNode
def testStewardsCanBeAddedOnlyTillAThresholdIsReached(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_steward, tconf): sdk_add_new_nym(looper, sdk_pool_handle, sdk_wallet_steward, alias='testSteward' + randomString(3), role=STEWARD_STRING) with pytest.raises(RequestRejectedException) as e: sdk_add_new_nym(looper, sdk_pool_handle, sdk_wallet_steward, alias='testSteward' + randomString(3), role=STEWARD_STRING) error_message = 'New stewards cannot be added by other stewards as there ' \ 'are already {} stewards in the system'.format(tconf.stewardThreshold) assert error_message in e._excinfo[1].args[0]
def test_proof_multiple_prefix_nodes(): node_trie = Trie(PersistentDB(KeyValueStorageInMemory())) prefix_1 = 'abcdefgh' prefix_2 = 'abcdefxy' # Prefix overlaps with previous prefix_3 = 'pqrstuvw' prefix_4 = 'mnoptuvw' # Suffix overlaps all_prefixes = (prefix_1, prefix_2, prefix_3, prefix_4) other_nodes_count = 1000 prefix_nodes_count = 100 # Some nodes before prefix nodes for _ in range(other_nodes_count): k, v = randomString(randint(8, 19)).encode(), rlp_encode([randomString(15)]) node_trie.update(k, v) keys_suffices = set() while len(keys_suffices) != prefix_nodes_count: keys_suffices.add(randint(25, 250000)) key_vals = {'{}{}'.format(prefix, k): str(randint(3000, 5000)) for prefix in all_prefixes for k in keys_suffices} for k, v in key_vals.items(): node_trie.update(k.encode(), rlp_encode([v])) # Some nodes after prefix nodes for _ in range(other_nodes_count): node_trie.update(randomString(randint(8, 19)).encode(), rlp_encode([randomString(15)])) for prefix in all_prefixes: client_trie = Trie(PersistentDB(KeyValueStorageInMemory())) proof_nodes, val = node_trie.generate_state_proof_for_keys_with_prefix( prefix.encode(), get_value=True) encoded = {k.encode(): rlp_encode([v]) for k, v in key_vals.items() if k.startswith(prefix)} # Check returned values match the actual values assert encoded == val assert client_trie.verify_spv_proof_multi(node_trie.root_hash, encoded, proof_nodes) # Check without value proof_nodes = node_trie.generate_state_proof_for_keys_with_prefix( prefix.encode(), get_value=False) assert client_trie.verify_spv_proof_multi(node_trie.root_hash, encoded, proof_nodes) # Verify keys with a different prefix encoded = {k.encode(): rlp_encode([v]) for k, v in key_vals.items() if not k.startswith(prefix)} assert not client_trie.verify_spv_proof_multi(node_trie.root_hash, encoded, proof_nodes)
def test_add_node_to_pool_with_large_ppseqno_diff_views(do_view_change, looper, txnPoolNodeSet, tconf, sdk_pool_handle, sdk_wallet_steward, tdir, allPluginsPath): """ Adding a node to the pool while ppSeqNo is big caused a node to stash all the requests because of incorrect watermarks limits set. The case of view_no == 0 is special. The test emulates big ppSeqNo number, adds a node and checks all the pool nodes are functional. The test is run with several starting view_no, including 0 """ ensure_several_view_change(looper, txnPoolNodeSet, do_view_change, custom_timeout=tconf.VIEW_CHANGE_TIMEOUT) big_ppseqno = tconf.LOG_SIZE * 2 + 2345 cur_ppseqno = _get_ppseqno(txnPoolNodeSet) assert (big_ppseqno > cur_ppseqno) # ensure pool is working properly sdk_send_random_and_check(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_steward, 3) assert (cur_ppseqno < _get_ppseqno(txnPoolNodeSet)) _set_ppseqno(txnPoolNodeSet, big_ppseqno) cur_ppseqno = _get_ppseqno(txnPoolNodeSet) assert (big_ppseqno == cur_ppseqno) sdk_send_random_and_check(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_steward, 3) assert (cur_ppseqno < _get_ppseqno(txnPoolNodeSet)) new_steward_name = "testClientSteward" + randomString(4) new_node_name = "TestTheta" + randomString(4) new_steward_wallet_handle, new_node = sdk_add_new_steward_and_node( looper, sdk_pool_handle, sdk_wallet_steward, new_steward_name, new_node_name, tdir, tconf, allPluginsPath=allPluginsPath) txnPoolNodeSet.append(new_node) looper.run(checkNodesConnected(txnPoolNodeSet)) sdk_ensure_pool_functional(looper, txnPoolNodeSet, sdk_wallet_steward, sdk_pool_handle) sdk_ensure_pool_functional(looper, txnPoolNodeSet, new_steward_wallet_handle, sdk_pool_handle) waitNodeDataEquality(looper, new_node, *txnPoolNodeSet[:-1]) sdk_send_random_and_check(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_steward, 3) waitNodeDataEquality(looper, new_node, *txnPoolNodeSet[:-1])
def test_random_string2(): test_iterations = 1000 rss = [] for i in range(test_iterations): rs = randomString(20) assert rs not in rss, "random string # %d exists in list, we have a collision" % i rss.append(rs)
def testAddInactiveNodeThenActivate(looper, txnPoolNodeSet, sdk_wallet_steward, sdk_pool_handle, tdir, tconf, allPluginsPath): new_steward_name = "testClientSteward" + randomString(3) new_node_name = "Kappa" # adding a new node without SERVICES field # it means the node is in the inactive state new_steward_wallet, new_node = \ sdk_add_new_steward_and_node(looper, sdk_pool_handle, sdk_wallet_steward, new_steward_name, new_node_name, tdir, tconf, allPluginsPath, services=None) looper.run(checkNodesConnected(txnPoolNodeSet)) sdk_pool_refresh(looper, sdk_pool_handle) new_node = update_node_data_and_reconnect(looper, txnPoolNodeSet + [new_node], new_steward_wallet, sdk_pool_handle, new_node, None, None, None, None, tdir, tconf) txnPoolNodeSet.append(new_node) sdk_ensure_pool_functional(looper, txnPoolNodeSet, new_steward_wallet, sdk_pool_handle)
def addNewNode(looper, stewardClient, stewardWallet, newNodeName, tdir, tconf, allPluginsPath=None, autoStart=True): sigseed = randomString(32).encode() nodeSigner = SimpleSigner(seed=sigseed) (nodeIp, nodePort), (clientIp, clientPort) = genHa(2) op = { TXN_TYPE: NEW_NODE, TARGET_NYM: nodeSigner.identifier, DATA: { NODE_IP: nodeIp, NODE_PORT: nodePort, CLIENT_IP: clientIp, CLIENT_PORT: clientPort, ALIAS: newNodeName } } req = stewardWallet.signOp(op) stewardClient.submitReqs(req) nodeCount = len(stewardClient.nodeReg) looper.run(eventually(checkSufficientRepliesRecvd, stewardClient.inBox, req.reqId, 1, retryWait=1, timeout=3 * nodeCount)) initLocalKeep(newNodeName, tdir, sigseed, override=True) node = TestNode(newNodeName, basedirpath=tdir, config=tconf, ha=(nodeIp, nodePort), cliha=(clientIp, clientPort), pluginPaths=allPluginsPath) if autoStart: looper.add(node) return node
def testRescheduleUpgradeToLowerVersionThanPreviouslyScheduled( looper, tconf, nodeSet, validUpgrade, trustee, trusteeWallet): """ A node starts at version 1.2 running has scheduled upgrade for version 1.5 but get a txn for upgrade 1.4, it will schedule it and cancel upgrade to 1.5. """ upgr1 = deepcopy(validUpgrade) upgr2 = deepcopy(upgr1) upgr2[VERSION] = bumpVersion(upgr1[VERSION]) upgr2[NAME] += randomString(3) # upgr2[SHA256] = get_valid_code_hash() upgr2[SHA256] = 'ef9c3984e7a31994d4f692139116120bd0dd1ff7e270b6a2d773f8f2f9214d4c' # An upgrade for higher version scheduled, it should pass ensureUpgradeSent(looper, trustee, trusteeWallet, upgr2) looper.run( eventually( checkUpgradeScheduled, nodeSet, upgr2[VERSION], retryWait=1, timeout=waits.expectedUpgradeScheduled())) # An upgrade for lower version scheduled, the transaction should pass and # the upgrade should be scheduled ensureUpgradeSent(looper, trustee, trusteeWallet, upgr1) looper.run( eventually( checkUpgradeScheduled, nodeSet, upgr1[VERSION], retryWait=1, timeout=waits.expectedUpgradeScheduled()))
def testPoolUpgradeHasInvalidSyntaxIfJustificationIsVeryLong(be, do, validUpgrade, trusteeCli): validUpgrade[JUSTIFICATION] = randomString(JUSTIFICATION_MAX_SIZE + 1) be(trusteeCli) do('send POOL_UPGRADE name={name} version={version} sha256={sha256} ' 'action={action} schedule={schedule} timeout={timeout} justification={justification}', mapper=validUpgrade, expect=INVALID_SYNTAX, within=10)
def testLoggingTxnStateForInvalidRequest( looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_client, logsearch): logsPropagate, _ = logsearch(files=['propagator.py'], funcs=['propagate'], msgs=['propagating.*request.*from client']) logsReject, _ = logsearch(files=['replica.py'], funcs=['consume_req_queue_for_pre_prepare'], msgs=['encountered exception.*while processing.*will reject']) seed = randomString(32) wh, _ = sdk_wallet_client nym_request, _ = looper.loop.run_until_complete( prepare_nym_request(sdk_wallet_client, seed, "name", STEWARD_STRING)) request_couple = sdk_sign_and_send_prepared_request(looper, sdk_wallet_client, sdk_pool_handle, nym_request) with pytest.raises(RequestRejectedException) as e: sdk_get_and_check_replies(looper, [request_couple]) assert 'Only Steward is allowed to do these transactions' in e._excinfo[1].args[0] request = json.loads(nym_request) req_id = str(request[f.REQ_ID.nm]) digest = get_key_from_req(request) assert any(digest in record.getMessage() for record in logsPropagate) assert any(req_id in record.getMessage() for record in logsReject)
def testStewardSuspensionByTrustee(looper, anotherTrustee, anotherSteward): trClient, trWallet = anotherTrustee _, stWallet = anotherSteward suspendRole(looper, trClient, trWallet, stWallet.defaultId) with pytest.raises(AssertionError): addRole(looper, *anotherSteward, name=randomString(), role=TRUST_ANCHOR)
def create_specific_node_request(looper, steward_wallet_handle, tconf, tdir, new_node_name=randomString(5), new_node_ip=None, new_node_port=None, new_client_ip=None, new_client_port=None): sigseed, verkey, bls_key, node_ip, node_port, client_ip, client_port, key_proof = \ prepare_new_node_data(tconf, tdir, new_node_name) node_ip = new_node_ip if new_node_ip else node_ip node_port = new_node_port if new_node_port else node_port client_ip = new_client_ip if new_client_ip else client_ip client_port = new_client_port if new_client_port else client_port _, steward_did = steward_wallet_handle node_request = looper.loop.run_until_complete( prepare_node_request(steward_did, new_node_name=new_node_name, clientIp=client_ip, clientPort=client_port, nodeIp=node_ip, nodePort=node_port, bls_key=bls_key, sigseed=sigseed, services=[VALIDATOR], key_proof=key_proof)) return node_request
def testTrustAnchorSuspensionByTrustee( looper, anotherTrustee, anotherTrustAnchor): trClient, trWallet = anotherTrustee _, spWallet = anotherTrustAnchor suspendRole(looper, trClient, trWallet, spWallet.defaultId) with pytest.raises(AssertionError): addRole(looper, *anotherTrustAnchor, name=randomString())
def testTrustAnchorSuspensionByTrustee( looper, sdk_pool_handle, another_trustee, another_trust_anchor): _, did_ta = another_trust_anchor sdk_suspend_role(looper, sdk_pool_handle, another_trustee, did_ta) with pytest.raises(RequestRejectedException): sdk_add_new_nym(looper, sdk_pool_handle, another_trust_anchor, alias=randomString())
def prepare_new_node_data(tconf, tdir, newNodeName, configClass=PNodeConfigHelper): sigseed = randomString(32).encode() (nodeIp, nodePort), (clientIp, clientPort) = genHa(2) config_helper = configClass(newNodeName, tconf, chroot=tdir) pubkey, verkey, bls_key, key_proof = initNodeKeysForBothStacks(newNodeName, config_helper.keys_dir, sigseed, override=True) return sigseed, verkey, bls_key, nodeIp, nodePort, clientIp, clientPort, key_proof
def sdk_node_theta_added(looper, txnPoolNodeSet, tdir, tconf, sdk_pool_handle, sdk_wallet_steward, allPluginsPath, testNodeClass=TestNode, name=None): new_steward_name = "testClientSteward" + randomString(3) new_node_name = name or "Theta" new_steward_wallet, new_node = \ sdk_add_new_steward_and_node(looper, sdk_pool_handle, sdk_wallet_steward, new_steward_name, new_node_name, tdir, tconf, allPluginsPath, nodeClass=testNodeClass) txnPoolNodeSet.append(new_node) looper.run(checkNodesConnected(txnPoolNodeSet)) sdk_pool_refresh(looper, sdk_pool_handle) return new_steward_wallet, new_node
def test_get_txn_response_as_expected(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_steward): seed = randomString(32) wh, _ = sdk_wallet_steward # filling nym request and getting steward did # if role == None, we are adding client nym_request, new_did = looper.loop.run_until_complete( prepare_nym_request(sdk_wallet_steward, seed, None, None)) # sending request using 'sdk_' functions request_couple = sdk_sign_and_send_prepared_request( looper, sdk_wallet_steward, sdk_pool_handle, nym_request) result1 = sdk_get_and_check_replies(looper, [request_couple])[0][1]['result'] seqNo = get_seq_no(result1) _, steward_did = sdk_wallet_steward request = sdk_build_get_txn_request(looper, steward_did, seqNo) request_couple = \ sdk_sign_and_send_prepared_request(looper, sdk_wallet_steward, sdk_pool_handle, request) result2 = sdk_get_and_check_replies(looper, [request_couple])[0][1]['result'] assert result1 == result2['data']
def test_nym_dynamic_validation_for_existing_nym_fails_with_no_changes( nym_handler: NymHandler, creator): nym_request = Request(identifier=creator, reqId=5, operation={ 'type': NYM, 'dest': randomString() }) add_to_idr(nym_handler.database_manager.idr_cache, nym_request.operation['dest'], None) add_to_idr(nym_handler.database_manager.idr_cache, creator, STEWARD) nym_handler.write_req_validator.validate = get_exception(True) with pytest.raises(InvalidClientRequest): nym_handler.dynamic_validation(nym_request, 0)
def add_another_reg_id(looper, sdk_wallet_steward, create_node_and_not_start): node = create_node_and_not_start data = { ID: randomString(50), TXN_TYPE: REVOC_REG_DEF, REVOC_TYPE: "CL_ACCUM", TAG: randomString(5), CRED_DEF_ID: randomString(50), VALUE: { ISSUANCE_TYPE: ISSUANCE_BY_DEFAULT, MAX_CRED_NUM: 1000000, TAILS_HASH: randomString(50), TAILS_LOCATION: 'http://tails.location.com', PUBLIC_KEYS: {}, } } req = sdk_sign_request_from_dict(looper, sdk_wallet_steward, data) looper.runFor(2) req_handler = node.get_req_handler(DOMAIN_LEDGER_ID) txn = append_txn_metadata(reqToTxn(Request(**req)), txn_time=FIRST_ID_TS, seq_no=node.domainLedger.seqNo + 1) req_handler._addRevocDef(txn) return req
def test_update_bls_by_trustee(be, do, trusteeCli, newNodeVals, newNodeAdded, nodeValsEmptyData): ''' Checks that it's not possible to update BLS keys by Trustee (just alias and new key are required) ''' be(trusteeCli) node_vals = nodeValsEmptyData node_vals['newNodeData'][ALIAS] = newNodeVals['newNodeData'][ALIAS] node_vals['newNodeData'][BLS_KEY] = randomString(32) doSendNodeCmd(do, node_vals, expMsgs=["TRUSTEE not in allowed roles ['STEWARD']"]) exitFromCli(do)
def test_get_taa_aml_static_validation_fails(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_client): req = { OPERATION: { TXN_TYPE: GET_TXN_AUTHOR_AGREEMENT_AML, 'timestamp': randint(1, 2147483647), 'version': randomString() }, f.IDENTIFIER.nm: sdk_wallet_client[1], f.REQ_ID.nm: randint(1, 2147483647), f.PROTOCOL_VERSION.nm: CURRENT_PROTOCOL_VERSION } rep = sdk_sign_and_send_prepared_request(looper, sdk_wallet_client, sdk_pool_handle, json.dumps(req)) with pytest.raises(RequestNackedException) as e: sdk_get_and_check_replies(looper, [rep]) e.match('cannot be used in GET_TXN_AUTHOR_AGREEMENT_AML request together')
def schema_request(): return Request(identifier=randomString(), reqId=5, signature="sig", operation={ 'type': SCHEMA, 'data': { 'version': '1.0', 'name': 'Degree', 'attr_names': [ 'last_name', 'first_name', ] } })
def test_validation_with_issued_no_revoked_before_by_default( build_txn_for_revoc_def_entry_by_default, create_node_and_not_start): node = create_node_and_not_start req_entry = build_txn_for_revoc_def_entry_by_default req_entry['operation'][VALUE][REVOKED] = [1, 2] req_handler = node.getDomainReqHandler() req_handler.apply(Request(**req_entry), int(time.time())) req_entry['operation'][VALUE][ISSUED] = [3, 4] req_entry['operation'][VALUE][REVOKED] = [] req_entry['operation'][VALUE][PREV_ACCUM] = req_entry['operation'][VALUE][ ACCUM] req_entry['operation'][VALUE][ACCUM] = randomString(10) with pytest.raises(InvalidClientRequest, match="are not present in the current revoked list"): req_handler.validate(Request(**req_entry))
def test_send_with_wrong_rev_reg_id_default(looper, txnPoolNodeSet, sdk_pool_handle, send_revoc_reg_entry_by_default, build_get_revoc_reg_delta): get_revoc_reg_delta = copy.deepcopy(build_get_revoc_reg_delta) del get_revoc_reg_delta['operation'][FROM] get_revoc_reg_delta['operation'][REVOC_REG_DEF_ID] = randomString(30) get_revoc_reg_delta['operation'][TO] = get_utc_epoch() + 1000 sdk_reply = sdk_send_and_check([json.dumps(get_revoc_reg_delta)], looper, txnPoolNodeSet, sdk_pool_handle) reply = sdk_reply[0][1] assert reply['result'][DATA][REVOC_REG_DEF_ID] == get_revoc_reg_delta[ 'operation'][REVOC_REG_DEF_ID] assert VALUE not in reply['result'][DATA] assert REVOC_TYPE not in reply['result'][DATA]
def test_future_primaries_replicas_increase(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_stewards, tdir, tconf, allPluginsPath): # Don't delete NodeStates, so we could check them. global old_commit old_commit = txnPoolNodeSet[ 0].write_manager.future_primary_handler.commit_batch for node in txnPoolNodeSet: node.write_manager.future_primary_handler.commit_batch = lambda three_pc_batch, prev_handler_result=None: 0 initial_primaries = copy.copy(txnPoolNodeSet[0].primaries) last_ordered = txnPoolNodeSet[0].master_replica.last_ordered_3pc starting_view_number = checkViewNoForNodes(txnPoolNodeSet) # Increase replicas count add_new_node(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_stewards[0], tdir, tconf, allPluginsPath) new_view_no = checkViewNoForNodes(txnPoolNodeSet) assert new_view_no == starting_view_number + 1 # "seq_no + 2" because 1 domain and 1 pool txn. node = txnPoolNodeSet[0] with delay_rules(node.nodeIbStasher, cDelay()): req = sdk_send_random_and_check(looper, txnPoolNodeSet, sdk_pool_handle, sdk_wallet_stewards[0], 1)[0][0] req = Request(**req) three_pc_batch = ThreePcBatch(DOMAIN_LEDGER_ID, 0, 0, 1, time.time(), randomString(), randomString(), ['a', 'b', 'c'], [req.digest]) primaries = node.write_manager.future_primary_handler.post_batch_applied( three_pc_batch) assert len(primaries) == len(initial_primaries) + 1 assert len(primaries) == len(node.primaries)
def add_new_node(looper, nodes, sdk_pool_handle, sdk_wallet_steward, tdir, client_tdir, tconf, all_plugins_path, name=None): node_name = name or "Psi" new_steward_name = "testClientSteward" + randomString(3) _, new_node = sdk_add_new_steward_and_node( looper, sdk_pool_handle, sdk_wallet_steward, new_steward_name, node_name, tdir, tconf, allPluginsPath=all_plugins_path) nodes.append(new_node) looper.run(checkNodesConnected(nodes)) timeout = waits.expectedPoolCatchupTime(nodeCount=len(nodes)) waitNodeDataEquality(looper, new_node, *nodes[:-1], customTimeout=timeout) sdk_pool_refresh(looper, sdk_pool_handle) return new_node
def add_revoc_def_by_default(create_node_and_not_start, looper, sdk_wallet_steward): node = create_node_and_not_start data = { "id": randomString(50), "type": REVOC_REG_DEF, "tag": randomString(5), "credDefId": randomString(50), "value": { "issuanceType": ISSUANCE_BY_DEFAULT, "maxCredNum": 1000000, "tailsHash": randomString(50), "tailsLocation": 'http://tails.location.com', "publicKeys": {}, } } req = sdk_sign_request_from_dict(looper, sdk_wallet_steward, data) req_handler = node.getDomainReqHandler() txn = reqToTxn(Request(**req)) txn[f.SEQ_NO.nm] = node.domainLedger.seqNo + 1 txn[TXN_TIME] = int(time.time()) req_handler._addRevocDef(txn) return req
def test_forced_request_validation(looper, txnPoolNodeSet, sdk_wallet_client, sdk_pool_handle, sdk_wallet_steward): nym_request, new_did = looper.loop.run_until_complete( prepare_nym_request(sdk_wallet_client, randomString(32), None, None)) request_json = json.loads(nym_request) request_json['operation'][FORCE] = True node_request = json.dumps(request_json) request_couple = sdk_sign_and_send_prepared_request( looper, sdk_wallet_client, sdk_pool_handle, node_request) with pytest.raises(RequestNackedException): sdk_get_and_check_replies(looper, [request_couple]) sdk_add_new_nym(looper, sdk_pool_handle, sdk_wallet_steward)
def sdk_send_restart(looper, trusty_wallet, sdk_pool_handle, action=None, datetime=None, seed=None): # filling restart request and getting trusty did seed = seed or randomString(32) restart_request, did = looper.loop.run_until_complete( prepare_restart_request(trusty_wallet, seed, action, datetime)) # sending request using 'sdk_' functions request_couple = sdk_sign_and_send_prepared_request( looper, trusty_wallet, sdk_pool_handle, restart_request) # waiting for replies return sdk_get_and_check_multiply_replies(looper, request_couple)
def mapping_req(rich_schema_handler, encoding_handler, rich_schema_req): make_rich_schema_object_exist(rich_schema_handler, rich_schema_req) make_rich_schema_object_exist(encoding_handler, TEST_ENCODING_1) make_rich_schema_object_exist(encoding_handler, TEST_ENCODING_2) make_rich_schema_object_exist(encoding_handler, TEST_ENCODING_3) id = randomString() content = copy.deepcopy(TEST_MAPPING) content['@id'] = id req = rs_req(RICH_SCHEMA_MAPPING, RS_MAPPING_TYPE_VALUE, content=content, id=id) add_to_idr(rich_schema_handler.database_manager.idr_cache, req.identifier, TRUSTEE) add_to_idr(rich_schema_handler.database_manager.idr_cache, req.endorser, ENDORSER) return req
def __init__(self, port: int, msgHandler: Callable, name: str=None, basedirpath: str=None, seed=None, onlyListener=False): stackParams = { "name": name or randomString(8), "ha": HA("0.0.0.0", port), "auto": AutoMode.always } if basedirpath: stackParams["basedirpath"] = basedirpath seed = seed or randomSeed() SimpleZStack.__init__(self, stackParams, self.baseMsgHandler, seed=seed, onlyListener=onlyListener) self.msgHandler = msgHandler
def build_attrib(self, nym, raw=None, enc=None, hsh=None): assert int(bool(raw)) + int(bool(enc)) + int(bool(hsh)) == 1 if raw: l = LedgerStore.RAW data = raw elif enc: l = LedgerStore.ENC data = enc elif hsh: l = LedgerStore.HASH data = hsh else: raise RuntimeError('One of raw, enc, or hash are required.') return Attribute(randomString(5), data, self.defaultId, dest=nym, ledgerStore=LedgerStore.RAW)
def test_not_owner_steward_cant_edit_revoc_reg_entry( looper, txnPoolNodeSet, sdk_wallet_steward, sdk_wallet_trust_anchor, sdk_pool_handle, build_revoc_def_by_steward, claim_def, tconf): revoc_entry_req_steward = create_revoc_reg_entry( looper, txnPoolNodeSet, sdk_pool_handle, build_revoc_def_by_steward, claim_def, sdk_wallet_trust_anchor) revoc_entry_req_steward['operation'][VALUE][REVOKED] = [6, 7, 8] revoc_entry_req_steward['operation'][VALUE][ PREV_ACCUM] = revoc_entry_req_steward['operation'][VALUE][ACCUM] revoc_entry_req_steward['operation'][VALUE][ACCUM] = randomString(10) revoc_entry_req_steward = sdk_sign_request_from_dict( looper, sdk_wallet_steward, revoc_entry_req_steward['operation']) with pytest.raises(RequestRejectedException): sdk_send_and_check([json.dumps(revoc_entry_req_steward)], looper, txnPoolNodeSet, sdk_pool_handle)
def build_attrib(self, nym, raw=None, enc=None, hsh=None): assert int(bool(raw)) + int(bool(enc)) + int(bool(hsh)) == 1 if raw: # l = LedgerStore.RAW data = raw elif enc: # l = LedgerStore.ENC data = enc elif hsh: # l = LedgerStore.HASH data = hsh else: raise RuntimeError('One of raw, enc, or hash are required.') # TODO looks like a possible error why we do not use `l` (see above)? return Attribute(randomString(5), data, self.defaultId, dest=nym, ledgerStore=LedgerStore.RAW)
def add_new_node(looper, nodes, sdk_pool_handle, sdk_wallet_steward, tdir, tconf, all_plugins_path, name=None, wait_till_added=True): node_name = name or "Psi" new_steward_name = "testClientSteward" + randomString(3) _, new_node = sdk_add_new_steward_and_node( looper, sdk_pool_handle, sdk_wallet_steward, new_steward_name, node_name, tdir, tconf, allPluginsPath=all_plugins_path, wait_till_added=wait_till_added) if wait_till_added: nodes.append(new_node) looper.run(checkNodesConnected(nodes)) timeout = waits.expectedPoolCatchupTime(nodeCount=len(nodes)) waitNodeDataEquality(looper, new_node, *nodes[:-1], customTimeout=timeout, exclude_from_check=['check_last_ordered_3pc_backup']) sdk_pool_refresh(looper, sdk_pool_handle) return new_node
def test_state_proof_returned_for_missing_get_rich_schema_obj_by_id( looper, nodeSetWithOneNodeResponding, sdk_wallet_endorser, sdk_pool_handle, sdk_wallet_client): """ Tests that state proof is returned in the reply for GET_RICH_SCHEMA_OBJECT_BY_ID. Use different submitter and reader! """ rs_id = randomString() get_rich_schema_by_id_operation = { TXN_TYPE: GET_RICH_SCHEMA_OBJECT_BY_ID, RS_ID: rs_id, } result = sdk_submit_operation_and_get_result( looper, sdk_pool_handle, sdk_wallet_client, get_rich_schema_by_id_operation) check_no_data_and_valid_proof(result)