Skip to content

Commit 5864a4d

Browse files
committed
feat(avm): mutate global gas fees
1 parent d85d8db commit 5864a4d

File tree

7 files changed

+31
-15
lines changed

7 files changed

+31
-15
lines changed

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/fuzz.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,18 @@ SimulatorResult fuzz_against_ts_simulator(FuzzerData& fuzzer_data, FuzzerContext
4040
FF fee_required_l2 = FF(tx.effective_gas_fees.fee_per_l2_gas) * FF(tx.gas_settings.gas_limits.l2_gas);
4141
ws_mgr->write_fee_payer_balance(tx.fee_payer, fee_required_da + fee_required_l2);
4242

43+
auto globals = create_default_globals();
44+
4345
try {
4446
ws_mgr->checkpoint();
45-
cpp_result = cpp_simulator.simulate(*ws_mgr, contract_db, tx, /*public_data_writes=*/{});
47+
cpp_result = cpp_simulator.simulate(*ws_mgr, contract_db, tx, globals, /*public_data_writes=*/{});
4648
ws_mgr->revert();
4749
} catch (const std::exception& e) {
4850
throw std::runtime_error(std::string("CppSimulator threw an exception: ") + e.what());
4951
}
5052

5153
ws_mgr->checkpoint();
52-
auto js_result = js_simulator->simulate(*ws_mgr, contract_db, tx, /*public_data_writes=*/{});
54+
auto js_result = js_simulator->simulate(*ws_mgr, contract_db, tx, globals, /*public_data_writes=*/{});
5355

5456
context.reset();
5557

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/fuzz.test.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ class FuzzTest : public ::testing::Test {
5454
FF fee_required_l2 = FF(tx.effective_gas_fees.fee_per_l2_gas) * FF(tx.gas_settings.gas_limits.l2_gas);
5555
ws_mgr->write_fee_payer_balance(tx.fee_payer, fee_required_da + fee_required_l2);
5656
auto cpp_simulator = CppSimulator();
57+
auto globals = create_default_globals();
5758

58-
auto result = cpp_simulator.simulate(*ws_mgr, contract_db, tx);
59+
auto result = cpp_simulator.simulate(*ws_mgr, contract_db, tx, globals, /*public_data_writes=*/{});
5960

6061
ws_mgr->revert();
6162

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/simulator.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ SimulatorResult CppSimulator::simulate(
7979
fuzzer::FuzzerWorldStateManager& ws_mgr,
8080
fuzzer::FuzzerContractDB& contract_db,
8181
const Tx& tx,
82+
const GlobalVariables& globals,
8283
[[maybe_unused]] const std::vector<bb::crypto::merkle_tree::PublicDataLeafValue>& public_data_writes)
8384
{
8485
// Note: public_data_writes are already applied to C++ world state in setup_fuzzer_state
@@ -94,8 +95,6 @@ SimulatorResult CppSimulator::simulate(
9495

9596
ProtocolContracts protocol_contracts{};
9697

97-
auto globals = create_default_globals();
98-
9998
WorldState& ws = ws_mgr.get_world_state();
10099
WorldStateRevision ws_rev = ws_mgr.get_current_revision();
101100

@@ -153,10 +152,9 @@ SimulatorResult JsSimulator::simulate(
153152
[[maybe_unused]] fuzzer::FuzzerWorldStateManager& ws_mgr,
154153
fuzzer::FuzzerContractDB& contract_db,
155154
const Tx& tx,
155+
const GlobalVariables& globals,
156156
const std::vector<bb::crypto::merkle_tree::PublicDataLeafValue>& public_data_writes)
157157
{
158-
auto globals = create_default_globals();
159-
160158
std::string serialized = serialize_simulation_request(tx, globals, contract_db, public_data_writes);
161159

162160
// Send the request

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzz_lib/simulator.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ class Simulator {
5353
fuzzer::FuzzerWorldStateManager& ws_mgr,
5454
fuzzer::FuzzerContractDB& contract_db,
5555
const Tx& tx,
56+
const GlobalVariables& globals,
5657
const std::vector<bb::crypto::merkle_tree::PublicDataLeafValue>& public_data_writes) = 0;
5758
};
5859

@@ -63,6 +64,7 @@ class CppSimulator : public Simulator {
6364
fuzzer::FuzzerWorldStateManager& ws_mgr,
6465
fuzzer::FuzzerContractDB& contract_db,
6566
const Tx& tx,
67+
const GlobalVariables& globals,
6668
const std::vector<bb::crypto::merkle_tree::PublicDataLeafValue>& public_data_writes) override;
6769
};
6870

@@ -89,6 +91,7 @@ class JsSimulator : public Simulator {
8991
fuzzer::FuzzerWorldStateManager& ws_mgr,
9092
fuzzer::FuzzerContractDB& contract_db,
9193
const Tx& tx,
94+
const GlobalVariables& globals,
9295
const std::vector<bb::crypto::merkle_tree::PublicDataLeafValue>& public_data_writes) override;
9396
};
9497

@@ -102,5 +105,3 @@ Tx create_default_tx(const AztecAddress& contract_address,
102105
const Gas& gas_limit);
103106

104107
bool compare_simulator_results(SimulatorResult& result1, SimulatorResult& result2);
105-
106-
GlobalVariables create_default_globals();

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzzer_lib.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include "barretenberg/avm_fuzzer/fuzz_lib/control_flow.hpp"
1212
#include "barretenberg/avm_fuzzer/fuzz_lib/fuzz.hpp"
1313
#include "barretenberg/avm_fuzzer/fuzzer_comparison_helper.hpp"
14+
#include "barretenberg/avm_fuzzer/mutations/basic_types/uint64_t.hpp"
15+
#include "barretenberg/avm_fuzzer/mutations/configuration.hpp"
1416
#include "barretenberg/avm_fuzzer/mutations/fuzzer_data.hpp"
1517
#include "barretenberg/avm_fuzzer/mutations/tx_data.hpp"
1618
#include "barretenberg/avm_fuzzer/mutations/tx_types/gas.hpp"
@@ -79,7 +81,8 @@ SimulatorResult fuzz_tx(FuzzerWorldStateManager& ws_mgr, FuzzerContractDB& contr
7981

8082
try {
8183
ws_mgr.checkpoint();
82-
cpp_result = cpp_simulator.simulate(ws_mgr, contract_db, tx_data.tx, tx_data.public_data_writes);
84+
cpp_result = cpp_simulator.simulate(
85+
ws_mgr, contract_db, tx_data.tx, tx_data.global_variables, tx_data.public_data_writes);
8386
fuzz_info("CppSimulator completed without exception");
8487
fuzz_info("CppSimulator result: ", cpp_result);
8588
ws_mgr.revert();
@@ -95,7 +98,8 @@ SimulatorResult fuzz_tx(FuzzerWorldStateManager& ws_mgr, FuzzerContractDB& contr
9598
}
9699

97100
ws_mgr.checkpoint();
98-
auto js_result = js_simulator->simulate(ws_mgr, contract_db, tx_data.tx, tx_data.public_data_writes);
101+
auto js_result =
102+
js_simulator->simulate(ws_mgr, contract_db, tx_data.tx, tx_data.global_variables, tx_data.public_data_writes);
99103

100104
// If the results do not match
101105
if (!compare_simulator_results(cpp_result, js_result)) {
@@ -344,8 +348,16 @@ size_t mutate_tx_data(FuzzerContext& context,
344348
case FuzzerTxDataMutationType::ContractInstanceMutation:
345349
mutate_contract_instances(tx_data.contract_instances, tx_data.contract_addresses, rng);
346350
break;
347-
// case TxDataMutationType::GlobalVariablesMutation:
348-
// break;
351+
case FuzzerTxDataMutationType::GlobalVariablesMutation:
352+
// This is just mutating the gas values and timestamp
353+
mutate_uint64_t(tx_data.global_variables.timestamp, rng, BASIC_UINT64_T_MUTATION_CONFIGURATION);
354+
mutate_gas_fees(tx_data.global_variables.gas_fees, rng);
355+
// This must be less than or equal to the tx max fees per gas
356+
tx_data.global_variables.gas_fees.fee_per_da_gas = std::min(
357+
tx_data.global_variables.gas_fees.fee_per_da_gas, tx_data.tx.gas_settings.max_fees_per_gas.fee_per_da_gas);
358+
tx_data.global_variables.gas_fees.fee_per_l2_gas = std::min(
359+
tx_data.global_variables.gas_fees.fee_per_l2_gas, tx_data.tx.gas_settings.max_fees_per_gas.fee_per_l2_gas);
360+
break;
349361
// case TxDataMutationType::ProtocolContractsMutation:
350362
// break;
351363
}

barretenberg/cpp/src/barretenberg/avm_fuzzer/fuzzer_lib.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,18 @@ enum class FuzzerTxDataMutationType : uint8_t {
6363
BytecodeMutation,
6464
ContractClassMutation,
6565
ContractInstanceMutation,
66-
// GlobalVariablesMutation,
66+
GlobalVariablesMutation,
6767
// ProtocolContractsMutation
6868
};
6969

70-
using FuzzerTxDataMutationConfig = WeightedSelectionConfig<FuzzerTxDataMutationType, 4>;
70+
using FuzzerTxDataMutationConfig = WeightedSelectionConfig<FuzzerTxDataMutationType, 5>;
7171

7272
constexpr FuzzerTxDataMutationConfig FUZZER_TX_DATA_MUTATION_CONFIGURATION = FuzzerTxDataMutationConfig({
7373
{ FuzzerTxDataMutationType::TxMutation, 10 },
7474
{ FuzzerTxDataMutationType::BytecodeMutation, 1 },
7575
{ FuzzerTxDataMutationType::ContractClassMutation, 1 },
7676
{ FuzzerTxDataMutationType::ContractInstanceMutation, 1 },
77+
{ FuzzerTxDataMutationType::GlobalVariablesMutation, 4 },
7778
});
7879

7980
// Build bytecode and contract artifacts from fuzzer data

barretenberg/cpp/src/barretenberg/avm_fuzzer/mutations/bytecode.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ void mutate_bytecode(std::vector<ContractClassWithCommitment>& contract_classes,
9898
FF delayed_public_mutable_slot = Poseidon2::hash({ FF(UPDATED_CLASS_IDS_SLOT), address });
9999

100100
// Build preimage
101+
// todo(ilyas): make this somewhat random but also take into account the mutation on global variables.timestamp
101102
FF metadata = 0; // The lower 32 bits are the timestamp_of_change, we set to 0 so it has "taken effect"
102103
FF hash = Poseidon2::hash({ metadata, original_class_id, new_class_id });
103104

0 commit comments

Comments
 (0)