From d3073e0cbf878e949ed0576f76f1fc8e74720477 Mon Sep 17 00:00:00 2001 From: Emma Turner Date: Fri, 12 Jan 2024 13:59:16 +0000 Subject: [PATCH] Tezt/EVM: fix overflow in expected gas fees Expected gas fees easily overflow the bounds of Int32.t Therefore, we must always convert to the appropriate units prior to multiplying. --- tezt/lib_ethereum/wei.mli | 5 +++-- tezt/tests/evm_rollup.ml | 14 +++++++++----- 2 files changed, 12 insertions(+), 7 deletions(-) diff --git a/tezt/lib_ethereum/wei.mli b/tezt/lib_ethereum/wei.mli index 72542a6f1398..29024a290629 100644 --- a/tezt/lib_ethereum/wei.mli +++ b/tezt/lib_ethereum/wei.mli @@ -2,6 +2,7 @@ (* *) (* Open Source License *) (* Copyright (c) 2023 Nomadic Labs *) +(* Copyright (c) 2024 Trilitech *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) @@ -67,10 +68,10 @@ val ( + ) : t -> t -> t val ( - ) : t -> t -> t (** Multiplication. This doesn't perform any bound checks. *) -val ( * ) : t -> t -> t +val ( * ) : t -> Z.t -> t (** Division. This doesn't perform any bound checks. *) -val ( / ) : t -> t -> t +val ( / ) : t -> Z.t -> t (** The wei {!Check.typ}. *) val typ : t Check.typ diff --git a/tezt/tests/evm_rollup.ml b/tezt/tests/evm_rollup.ml index 0184be80179d..a77b77a9f709 100644 --- a/tezt/tests/evm_rollup.ml +++ b/tezt/tests/evm_rollup.ml @@ -2,7 +2,7 @@ (* *) (* SPDX-License-Identifier: MIT *) (* Copyright (c) 2023 Nomadic Labs *) -(* Copyright (c) 2023 TriliTech *) +(* Copyright (c) 2023-2024 TriliTech *) (* Copyright (c) 2023 Marigold *) (* Copyright (c) 2023 Functori *) (* *) @@ -53,6 +53,12 @@ let hex_256_of_address acc = (* prepend 24 leading zeros *) String.("0x" ^ make 24 '0' ^ s) +let expected_gas_fees ~gas_price ~gas_used = + let open Wei in + let gas_price = gas_price |> Z.of_int32 |> Wei.to_wei_z in + let gas_used = gas_used |> Z.of_int32 in + gas_price * gas_used + let evm_node_version evm_node = let endpoint = Evm_node.endpoint evm_node in let get_version_url = endpoint ^ "/version" in @@ -1391,7 +1397,7 @@ let transfer ?data ~evm_setup () = let fees = match receipt with | Some Transaction.{status = true; gasUsed; effectiveGasPrice; _} -> - Int32.(to_string (mul gasUsed effectiveGasPrice)) |> Wei.of_string + expected_gas_fees ~gas_price:effectiveGasPrice ~gas_used:gasUsed | _ -> Test.fail "Transaction didn't succeed" in Check.( @@ -3875,9 +3881,7 @@ let test_l2_revert_returns_unused_gas = let* balance_after = Eth_cli.balance ~account:sender.address ~endpoint in let gas_fee_paid = Wei.(balance_before - balance_after) in let gas_price = transaction_receipt.effectiveGasPrice in - let expected_gas_fee_paid = - Wei.to_wei_z @@ Z.of_int32 @@ Int32.mul gas_price gas_used - in + let expected_gas_fee_paid = expected_gas_fees ~gas_price ~gas_used in Check.((expected_gas_fee_paid = gas_fee_paid) Wei.typ) ~error_msg:"Expected gas fee paid to be %L, got %R" ; unit -- GitLab