Skip to content

Commit

Permalink
fix tests and cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: ashcherbakov <alexander.sherbakov@dsr-corporation.com>
  • Loading branch information
ashcherbakov committed Feb 27, 2020
1 parent a2b72d7 commit 37db2f5
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 18 deletions.
10 changes: 10 additions & 0 deletions plenum/common/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,16 @@ def friendlyEx(ex: Exception) -> str:
return friendly


def reasonForClientFromException(ex: Exception):
friendly = friendlyEx(ex)
reason = "client request invalid: {}".format(friendly)
return reason


def reasonForClientFromExReason(reason: str):
return "client request invalid: {}".format(reason)


def updateFieldsWithSeqNo(fields):
r = OrderedDict()
r[F.seqNo.name] = (str, int)
Expand Down
2 changes: 1 addition & 1 deletion plenum/server/client_error_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Rejects:
TAA_RETIRED = ClientError(203, "Txn Author Agreement is retired: version {}, seq_no {}, txn_time {}")
TAA_TOO_PRECISE = ClientError(204, "Txn Author Agreement acceptance time {} is too precise and is a privacy risk")
TAA_WRONG_ACCEPTANCE_TIME = ClientError(205,
"Txn Author Agreement acceptance time is inappropriate: provided {}, expected in [{}, {}]")
"Txn Author Agreement acceptance time is inappropriate: provided {}, expected in [{}, {}]")
TAA_AML_INVALID = ClientError(206,
"Txn Author Agreement acceptance mechanism is inappropriate: provided {}, expected one of {}")

Expand Down
8 changes: 5 additions & 3 deletions plenum/server/consensus/ordering_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
from plenum.common.txn_util import get_payload_digest, get_payload_data, get_seq_no, get_txn_time, get_digest
from plenum.common.types import f
from plenum.common.util import compare_3PC_keys, updateNamedTuple, SortedDict, getMaxFailures, mostCommonElement, \
get_utc_epoch, max_3PC_key
get_utc_epoch, max_3PC_key, reasonForClientFromExReason
from plenum.server.batch_handlers.three_pc_batch import ThreePcBatch
from plenum.server.consensus.consensus_shared_data import ConsensusSharedData
from plenum.server.consensus.batch_id import BatchID
Expand Down Expand Up @@ -1154,7 +1154,8 @@ def _apply_pre_prepare(self, pre_prepare: PrePrepare):
except InvalidClientMessageException as ex:
logger.warning('{} encountered exception {} while processing {}, '
'will reject'.format(self, ex, req))
rejects.append((req.key, Reject(req.identifier, req.reqId, ex.reason, ex.code)))
rejects.append((req.key, Reject(req.identifier, req.reqId,
reasonForClientFromExReason(ex.reason), ex.code)))
invalid_indices.append(idx)
except SuspiciousPrePrepare:
suspicious = True
Expand Down Expand Up @@ -2140,7 +2141,8 @@ def _consume_req_queue_for_pre_prepare(self, ledger_id, tm,
) as ex:
logger.warning('{} encountered exception {} while processing {}, '
'will reject'.format(self, ex, fin_req))
rejects.append((fin_req.key, Reject(fin_req.identifier, fin_req.reqId, ex.reason, ex.code)))
rejects.append((fin_req.key, Reject(fin_req.identifier, fin_req.reqId,
reasonForClientFromExReason(ex.reason), ex.code)))
invalid_indices.append(idx)
except SuspiciousPrePrepare:
malicious_req = True
Expand Down
17 changes: 3 additions & 14 deletions plenum/server/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@
get_txn_time, get_digest, TxnUtilConfig, get_payload_digest
from plenum.common.types import OPERATION, f
from plenum.common.util import friendlyEx, getMaxFailures, pop_keys, \
compare_3PC_keys, get_utc_epoch
compare_3PC_keys, get_utc_epoch, reasonForClientFromException
from plenum.common.verifier import DidVerifier
from plenum.common.config_helper import PNodeConfigHelper

Expand Down Expand Up @@ -1331,14 +1331,9 @@ def service_replicas_outbox(self, limit: int = None) -> int:
elif isinstance(message, tuple) and isinstance(message[1], Reject):
with self.metrics.measure_time(MetricsName.NODE_SEND_REJECT_TIME):
digest, reject = message
result_reject = Reject(
reject.identifier,
reject.reqId,
self.reasonForClientFromException(reject.reason),
)
# TODO: What the case when reqKey will be not in requestSender dict
if digest in self.requestSender:
self.transmitToClient(result_reject, self.requestSender[digest])
self.transmitToClient(reject, self.requestSender[digest])
self.doneProcessingReq(digest)
elif isinstance(message, Exception):
self.processEscalatedException(message)
Expand Down Expand Up @@ -1583,7 +1578,7 @@ def handleInvalidClientMsg(self, ex, wrappedMsg):
msg, frm = wrappedMsg
exc = ex.__cause__ if ex.__cause__ else ex
friendly = friendlyEx(ex)
reason = self.reasonForClientFromException(ex)
reason = reasonForClientFromException(ex)
if isinstance(msg, Request):
msg = msg.as_dict
identifier = idr_from_req_data(msg)
Expand Down Expand Up @@ -2856,12 +2851,6 @@ def ensureKeysAreSetup(self):
if not areKeysSetup(self.name, self.keys_dir):
raise REx(REx.reason.format(self.name) + self.keygenScript)

@staticmethod
def reasonForClientFromException(ex: Exception):
friendly = friendlyEx(ex)
reason = "client request invalid: {}".format(friendly)
return reason

def reportSuspiciousNodeEx(self, ex: SuspiciousNode):
"""
Report suspicion on a node on the basis of an exception
Expand Down

0 comments on commit 37db2f5

Please sign in to comment.