Skip to content

Commit

Permalink
Merge pull request #178 from dastardlychimp/test-static-fee
Browse files Browse the repository at this point in the history
Add tests to `test_static_fee_req_handler.py`
  • Loading branch information
mac-arrap authored Oct 23, 2018
2 parents 4e7ec06 + b2b7058 commit 44c3b7c
Show file tree
Hide file tree
Showing 3 changed files with 369 additions and 102 deletions.
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

0 comments on commit 44c3b7c

Please sign in to comment.