From 86e355c32eff34fd6db325ba6fc1000af88255ed Mon Sep 17 00:00:00 2001 From: Tom Jack Date: Sun, 6 Jan 2019 01:23:25 +0000 Subject: [PATCH] Client: fix fee calculation --- src/proto_alpha/lib_client/injection.ml | 74 ++++++++++++++----------- 1 file changed, 42 insertions(+), 32 deletions(-) diff --git a/src/proto_alpha/lib_client/injection.ml b/src/proto_alpha/lib_client/injection.ml index 6a30a648d..f9cd4ef0a 100644 --- a/src/proto_alpha/lib_client/injection.ml +++ b/src/proto_alpha/lib_client/injection.ml @@ -384,9 +384,45 @@ let may_patch_limits | Some c, Some rest -> Some (Cons (c, rest)) end in + let rec patch_fee : + type kind. bool -> kind contents -> kind contents = fun first -> function + | Manager_operation c as op -> + let gas_limit = c.gas_limit in + let size = + if first then + Data_encoding.Binary.fixed_length_exn + Tezos_base.Operation.shell_header_encoding + + Data_encoding.Binary.length + Operation.contents_encoding + (Contents op) + + Signature.size + else + Data_encoding.Binary.length + Operation.contents_encoding + (Contents op) + in + let minimal_fees_in_nanotez = + Z.mul (Z.of_int64 (Tez.to_mutez fee_parameter.minimal_fees)) (Z.of_int 1000) in + let minimal_fees_for_gas_in_nanotez = + Z.mul fee_parameter.minimal_nanotez_per_gas_unit gas_limit in + let minimal_fees_for_size_in_nanotez = + Z.mul fee_parameter.minimal_nanotez_per_byte (Z.of_int size) in + let fees_in_nanotez = + Z.add minimal_fees_in_nanotez @@ + Z.add minimal_fees_for_gas_in_nanotez minimal_fees_for_size_in_nanotez in + begin match Tez.of_mutez (Z.to_int64 (Z.div (Z.add (Z.of_int 999) fees_in_nanotez) (Z.of_int 1000))) with + | None -> assert false + | Some fee -> + if fee <= c.fee then + op + else + patch_fee first (Manager_operation { c with fee }) + end + | c -> c in + let patch : type kind. bool -> kind contents * kind contents_result -> kind contents tzresult Lwt.t = fun first -> function - | Manager_operation c as op, (Manager_operation_result _ as result) -> + | Manager_operation c, (Manager_operation_result _ as result) -> begin if c.gas_limit < Z.zero || gas_limit <= c.gas_limit then Lwt.return (estimated_gas_single result) >>=? fun gas -> @@ -417,37 +453,11 @@ let may_patch_limits end else return c.storage_limit end >>=? fun storage_limit -> - begin - if compute_fee then - let size = - if first then - Data_encoding.Binary.fixed_length_exn - Tezos_base.Operation.shell_header_encoding + - Data_encoding.Binary.length - Operation.contents_encoding - (Contents op) + - Signature.size - else - Data_encoding.Binary.length - Operation.contents_encoding - (Contents op) - in - let minimal_fees_in_nanotez = - Z.mul (Z.of_int64 (Tez.to_mutez fee_parameter.minimal_fees)) (Z.of_int 1000) in - let minimal_fees_for_gas_in_nanotez = - Z.mul fee_parameter.minimal_nanotez_per_gas_unit gas_limit in - let minimal_fees_for_size_in_nanotez = - Z.mul fee_parameter.minimal_nanotez_per_byte (Z.of_int size) in - let fees_in_nanotez = - Z.add minimal_fees_in_nanotez @@ - Z.add minimal_fees_for_gas_in_nanotez minimal_fees_for_size_in_nanotez in - match Tez.of_mutez (Z.to_int64 (Z.div (Z.add (Z.of_int 999) fees_in_nanotez) (Z.of_int 1000))) with - | None -> assert false - | Some fee -> return fee - else - return c.fee - end >>=? fun fee -> - return (Manager_operation { c with gas_limit ; storage_limit ; fee }) + let c = Manager_operation { c with gas_limit ; storage_limit } in + if compute_fee then + return (patch_fee first c) + else + return c | (c, _) -> return c in let rec patch_list : type kind. bool -> kind contents_and_result_list -> kind contents_list tzresult Lwt.t =