Skip to content

Commit

Permalink
INDY-1858: Implement node handler in indy_node
Browse files Browse the repository at this point in the history
Signed-off-by: ArtObr <[email protected]>
  • Loading branch information
ArtObr committed Jan 14, 2019
1 parent 2b0a8d3 commit 3facae9
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,22 @@
from indy_common.state import domain

from indy_common.constants import CLAIM_DEF, REF, SCHEMA
from indy_node.server.request_handlers.write_request_handler import WriteRequestHandler

from plenum.common.constants import DOMAIN_LEDGER_ID
from plenum.common.exceptions import InvalidClientRequest
from plenum.common.request import Request
from plenum.common.txn_util import get_request_data

from plenum.server.database_manager import DatabaseManager
from plenum.server.request_handlers.handler_interfaces.write_request_handler import WriteRequestHandler


class ClaimDefHandler(WriteRequestHandler):

def __init__(self, database_manager: DatabaseManager,
write_request_validator: WriteRequestValidator):
super().__init__(database_manager, CLAIM_DEF, DOMAIN_LEDGER_ID,
write_request_validator)
super().__init__(database_manager, CLAIM_DEF, DOMAIN_LEDGER_ID)
self.write_request_validator = write_request_validator

def static_validation(self, request: Request):
pass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
from indy_common.state import domain
from indy_common.constants import NYM
from indy_common.auth import Authoriser
from indy_node.server.request_handlers.write_request_handler import WriteRequestHandler
from ledger.util import F

from plenum.common.constants import ROLE, TARGET_NYM, VERKEY, DOMAIN_LEDGER_ID, TXN_TIME
Expand All @@ -16,14 +15,16 @@
from plenum.common.types import f
from plenum.server.database_manager import DatabaseManager
from plenum.server.request_handlers.utils import nym_to_state_key, get_nym_details
from plenum.server.request_handlers.handler_interfaces.write_request_handler import WriteRequestHandler


class NymHandler(WriteRequestHandler):
state_serializer = domain_state_serializer

def __init__(self, database_manager: DatabaseManager,
write_request_validator: WriteRequestValidator):
super().__init__(database_manager, NYM, DOMAIN_LEDGER_ID, write_request_validator)
super().__init__(database_manager, NYM, DOMAIN_LEDGER_ID)
self.write_request_validator = write_request_validator

def static_validation(self, request: Request):
self._validate_request_type(request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@

from indy_common.req_utils import get_write_schema_name, get_write_schema_version
from indy_node.server.request_handlers.read_req_handlers.get_schema_handler import GetSchemaHandler
from indy_node.server.request_handlers.write_request_handler import WriteRequestHandler
from plenum.common.constants import DOMAIN_LEDGER_ID
from plenum.common.exceptions import InvalidClientRequest

from plenum.common.request import Request
from plenum.common.txn_util import get_request_data
from plenum.server.database_manager import DatabaseManager
from plenum.server.request_handlers.handler_interfaces.write_request_handler import WriteRequestHandler


class SchemaHandler(WriteRequestHandler):

def __init__(self, database_manager: DatabaseManager, get_schema_handler: GetSchemaHandler,
write_request_validator: WriteRequestValidator):
super().__init__(database_manager, SCHEMA, DOMAIN_LEDGER_ID, write_request_validator)
super().__init__(database_manager, SCHEMA, DOMAIN_LEDGER_ID)
self.write_request_validator = write_request_validator
self.get_schema_handler = get_schema_handler

def static_validation(self, request: Request):
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from copy import deepcopy

from indy_common.constants import NODE

from indy_common.authorize.auth_actions import AuthActionAdd, AuthActionEdit
from indy_common.authorize.auth_request_validator import WriteRequestValidator
from plenum.common.constants import DATA, SERVICES, VALIDATOR, TARGET_NYM, ALIAS, BLS_KEY_PROOF
from plenum.server.database_manager import DatabaseManager
from plenum.server.request_handlers.node_handler import NodeHandler as PNodeHandler


class NodeHandler(PNodeHandler):

def __init__(self, database_manager: DatabaseManager, bls_crypto_verifier,
write_request_validator: WriteRequestValidator):
super().__init__(database_manager, bls_crypto_verifier)
self.write_request_validator = write_request_validator

def _is_steward(self, nym, is_committed: bool = True):
return self.database_manager.idr_cache.hasSteward(nym, is_committed)

def _auth_error_while_adding_node(self, request):
origin = request.identifier
operation = request.operation
data = operation.get(DATA, {})
error = self._data_error_while_validating(data, skip_keys=False)
if error:
return error

if self._steward_has_node(origin):
return "{} already has a node".format(origin)
error = self._is_node_data_conflicting(data)
if error:
return "existing data has conflicts with " \
"request data {}. Error: {}".format(operation.get(DATA), error)
self.write_request_validator.validate(request,
[AuthActionAdd(txn_type=NODE,
field=SERVICES,
value=data.get(SERVICES, [VALIDATOR]))])

def _auth_error_while_updating_node(self, request):
origin = request.identifier
is_trustee = self.database_manager.idr_cache.hasTrustee(origin, isCommitted=False)
if not is_trustee:
error = super()._auth_error_while_updating_node(request)
if error:
return error
origin = request.identifier
operation = request.operation
node_nym = operation.get(TARGET_NYM)

data = operation.get(DATA, {})
error = self._data_error_while_validating_update(data, node_nym)
if error:
return error

is_steward_of_node = self._is_steward_of_node(
origin, node_nym, is_committed=False)

node_info = self._get_node_data(node_nym, is_committed=False)
data = deepcopy(data)
data.pop(ALIAS, None)
for k in data:
if k == BLS_KEY_PROOF:
continue
old_val = node_info.get(k, None) if node_info else None
new_val = data[k]
if k == SERVICES:
if not old_val:
old_val = []
if not new_val:
new_val = []
if old_val != new_val:
self.write_request_validator.validate(request,
[AuthActionEdit(txn_type=NODE,
field=k,
old_value=old_val,
new_value=new_val,
is_owner=is_steward_of_node)])
12 changes: 0 additions & 12 deletions indy_node/server/request_handlers/write_request_handler.py

This file was deleted.

0 comments on commit 3facae9

Please sign in to comment.