Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests to test_static_fee_req_handler.py #178

Merged
merged 5 commits into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion sovtoken/sovtoken/test/test_public_xfer_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,10 @@ def test_seller_xfer_invalid_inputs(
seq_no = get_seq_no(initial_mint)
[seller_address, user1_address] = addresses

inputs = [{"address": seller_address, "seqNo": seq_no}, {"address": seller_address, "seqNo": seq_no}]
inputs = [
{"address": seller_address, "seqNo": seq_no},
{"address": seller_address, "seqNo": seq_no}
]
outputs = [
{"address": user1_address, "amount": 10},
{"address": seller_address, "amount": 90}
Expand Down
44 changes: 19 additions & 25 deletions sovtokenfees/sovtokenfees/static_fee_req_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,15 @@ def can_pay_fees(self, request):

if request.operation[TXN_TYPE] == XFER_PUBLIC:
# Fees in XFER_PUBLIC is part of operation[INPUTS]
self._get_deducted_fees_xfer(request, required_fees)
inputs = request.operation[INPUTS]
outputs = request.operation[OUTPUTS]
self._validate_fees_can_pay(request, inputs, outputs, required_fees)
self.deducted_fees_xfer[request.key] = required_fees
elif required_fees:
if StaticFeesReqHandler.has_fees(request):
self._validate_fees_can_pay(request, required_fees)
inputs = request.fees[0]
outputs = self.get_change_for_fees(request)
self._validate_fees_can_pay(request, inputs, outputs, required_fees)
else:
raise InvalidClientMessageException(getattr(request, f.IDENTIFIER.nm, None),
getattr(request, f.REQ_ID.nm, None),
Expand Down Expand Up @@ -202,25 +206,8 @@ def post_batch_committed(self, ledger_id, pp_time, committed_txns,
txn[FEES] = r[i]
i += 1

def _get_deducted_fees_xfer(self, request, required_fees):
try:
sum_inputs = TokenReqHandler.sum_inputs(self.utxo_cache,
request,
is_committed=False)

sum_outputs = TokenReqHandler.sum_outputs(request)
except InvalidClientMessageException as ex:
raise ex
except Exception as ex:
error = 'Exception {} while processing inputs/outputs'.format(ex)
raise UnauthorizedClientRequest(request.identifier, request.reqId, error)
else:
expected_amount = sum_outputs + required_fees
TokenReqHandler.validate_given_inputs_outputs(sum_inputs, sum_outputs,
expected_amount, request,
'fees: {}'.format(required_fees))

def _validate_fees_can_pay(self, request, required_fees):
def _validate_fees_can_pay(self, request, inputs, outputs, required_fees):
"""
Calculate and verify that inputs and outputs for fees can both be paid and change is properly specified

Expand All @@ -232,15 +219,22 @@ def _validate_fees_can_pay(self, request, required_fees):
"""

try:
sum_inputs = self.utxo_cache.sum_inputs(request.fees[0], is_committed=False)
sum_inputs = self.utxo_cache.sum_inputs(inputs, is_committed=False)
except UTXOError as ex:
raise InvalidFundsError(request.identifier, request.reqId, "{}".format(ex))
except Exception as ex:
error = 'Exception {} while processing inputs/outputs'.format(ex)
raise UnauthorizedClientRequest(request.identifier, request.reqId, error)
else:
change_amount = sum([a[AMOUNT] for a in self.get_change_for_fees(request)])
change_amount = sum([a[AMOUNT] for a in outputs])
expected_amount = change_amount + required_fees
TokenReqHandler.validate_given_inputs_outputs(sum_inputs, change_amount,
expected_amount, request,
'fees: {}'.format(required_fees))
TokenReqHandler.validate_given_inputs_outputs(
sum_inputs,
change_amount,
expected_amount,
request,
'fees: {}'.format(required_fees)
)


def _get_fees(self, is_committed=False, with_proof=False):
Expand Down
Loading