<commit_before>from copy import deepcopy

from plenum.common.txn import POOL_TXN_TYPES, TXN_TYPE, DATA, ALIAS, \
    TARGET_NYM
from plenum.server.pool_manager import HasPoolManager as PHasPoolManager, \
    TxnPoolManager as PTxnPoolManager
from sovrin_common.auth import Authoriser


class HasPoolManager(PHasPoolManager):
    # noinspection PyUnresolvedReferences, PyTypeChecker
    def __init__(self, nodeRegistry=None, ha=None, cliname=None, cliha=None):
        if not nodeRegistry:
            self.poolManager = TxnPoolManager(self, ha=ha, cliname=cliname,
                                              cliha=cliha)
            for types in POOL_TXN_TYPES:
                self.requestExecuter[types] = \
                    self.poolManager.executePoolTxnRequest
        else:
            super().__init__(nodeRegistry=nodeRegistry, ha=ha, cliname=cliname,
                             cliha=cliha)


class TxnPoolManager(PTxnPoolManager):
    def authErrorWhileUpdatingNode(self, request):
        origin = request.identifier
        isTrustee = self.node.secondaryStorage.isTrustee(origin)
        if not isTrustee:
            error = super().authErrorWhileUpdatingNode(request)
            if error:
                return error
        origin = request.identifier
        operation = request.operation
        nodeNym = operation.get(TARGET_NYM)
        isSteward = self.node.secondaryStorage.isSteward(origin)
        actorRole = self.node.graphStore.getRole(origin)
        _, nodeInfo = self.getNodeInfoFromLedger(nodeNym, excludeLast=False)
        typ = operation.get(TXN_TYPE)
        data = deepcopy(operation.get(DATA))
        data.pop(ALIAS, None)
        vals = []
        msgs = []
        for k in data:
            oldVal = nodeInfo[DATA][k]
            newVal = data[k]
            if oldVal != newVal:
                r, msg = Authoriser.authorised(typ, k, actorRole,
                                           oldVal=oldVal,
                                           newVal=newVal,
                                           isActorOwnerOfSubject=isSteward)
                vals.append(r)
                msgs.append(msg)
        msg = None if all(vals) else '\n'.join(msgs)
        return msg<commit_msg>Fixed Bug: sovrin-node.service randomly crashes or does not start at all after reboot<commit_after>from copy import deepcopy

from plenum.common.txn import POOL_TXN_TYPES, TXN_TYPE, DATA, ALIAS, \
    TARGET_NYM
from plenum.server.pool_manager import HasPoolManager as PHasPoolManager, \
    TxnPoolManager as PTxnPoolManager
from sovrin_common.auth import Authoriser


class HasPoolManager(PHasPoolManager):
    # noinspection PyUnresolvedReferences, PyTypeChecker
    def __init__(self, nodeRegistry=None, ha=None, cliname=None, cliha=None):
        if not nodeRegistry:
            self.poolManager = TxnPoolManager(self, ha=ha, cliname=cliname,
                                              cliha=cliha)
            for types in POOL_TXN_TYPES:
                self.requestExecuter[types] = \
                    self.poolManager.executePoolTxnRequest
        else:
            super().__init__(nodeRegistry=nodeRegistry, ha=ha, cliname=cliname,
                             cliha=cliha)


class TxnPoolManager(PTxnPoolManager):
    def authErrorWhileUpdatingNode(self, request):
        origin = request.identifier
        isTrustee = self.node.secondaryStorage.isTrustee(origin)
        if not isTrustee:
            error = super().authErrorWhileUpdatingNode(request)
            if error:
                return error
        origin = request.identifier
        operation = request.operation
        nodeNym = operation.get(TARGET_NYM)
        isSteward = self.node.secondaryStorage.isSteward(origin)
        actorRole = self.node.graphStore.getRole(origin)
        _, nodeInfo = self.getNodeInfoFromLedger(nodeNym, excludeLast=False)
        typ = operation.get(TXN_TYPE)
        data = deepcopy(operation.get(DATA))
        data.pop(ALIAS, None)
        vals = []
        msgs = []
        for k in data:
            oldVal = (nodeInfo.get(DATA, {})).get(k, None) if nodeInfo else None
            newVal = data[k]
            if oldVal != newVal:
                r, msg = Authoriser.authorised(typ, k, actorRole,
                                           oldVal=oldVal,
                                           newVal=newVal,
                                           isActorOwnerOfSubject=isSteward)
                vals.append(r)
                msgs.append(msg)
        msg = None if all(vals) else '\n'.join(msgs)
        return msg
{'fraction_non_alphanumeric': 0.07781168265039233, 'fraction_numerical': 0.0, 'mean_word_length': 1.9663865546218486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3499325', 'n_tokens_mistral': 1294, 'n_tokens_neox': 1262, 'n_words': 263}