Skip to content

Commit

Permalink
Merge branch 'chore/add-tests-for-v1-gas-service' into fix/test-for-D…
Browse files Browse the repository at this point in the history
…A-L2-sync-race-condition
  • Loading branch information
MitchTurner committed Jan 7, 2025
2 parents d19cef0 + a7eaa32 commit 4669ce8
Show file tree
Hide file tree
Showing 20 changed files with 204 additions and 163 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [2327](https://github.com/FuelLabs/fuel-core/pull/2327): Add more services tests and more checks of the pool. Also add an high level documentation for users of the pool and contributors.
- [2416](https://github.com/FuelLabs/fuel-core/issues/2416): Define the `GasPriceServiceV1` task.
- [2033](https://github.com/FuelLabs/fuel-core/pull/2033): Remove `Option<BlockHeight>` in favor of `BlockHeightQuery` where applicable.
- [2439](https://github.com/FuelLabs/fuel-core/pull/2439): Add gas costs for the two new zk opcodes `ecop` and `eadd` and the benches that allow to calibrate them.
- [2472](https://github.com/FuelLabs/fuel-core/pull/2472): Added the `amountU128` field to the `Balance` GraphQL schema, providing the total balance as a `U128`. The existing `amount` field clamps any balance exceeding `U64` to `u64::MAX`.
- [2526](https://github.com/FuelLabs/fuel-core/pull/2526): Add possibility to not have any cache set for RocksDB. Add an option to either load the RocksDB columns families on creation of the database or when the column is used.
- [2532](https://github.com/FuelLabs/fuel-core/pull/2532): Getters for inner rocksdb database handles.
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions benches/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ fuel-core-sync = { path = "./../crates/services/sync", features = [
] }
fuel-core-types = { path = "./../crates/types", features = ["test-helpers"] }
futures = { workspace = true }
hex = "0.4.3"
itertools = { workspace = true }
num_enum = { workspace = true }
p256 = { version = "0.13", default-features = false, features = [
Expand Down
2 changes: 1 addition & 1 deletion benches/benches-outputs/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fuel_core_types::fuel_tx::{
consensus_parameters::gas::GasCostsValuesV4,
consensus_parameters::gas::*,
DependentCost,
GasCostsValues,
};
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ pub fn get_state_size() -> u64 {
}

/// Allocates a byte array from heap and initializes it. Then points `reg` to it.
fn aloc_bytearray<const S: usize>(reg: u8, v: [u8; S]) -> Vec<Instruction> {
pub fn aloc_bytearray<const S: usize>(reg: u8, v: [u8; S]) -> Vec<Instruction> {
let mut ops = vec![op::movi(reg, S as u32), op::aloc(reg)];
for (i, b) in v.iter().enumerate() {
if *b != 0 {
Expand Down
51 changes: 44 additions & 7 deletions benches/benches/vm_set/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,11 @@ use fuel_core_types::{
},
fuel_tx::{
ContractIdExt,
Finalizable,
Input,
Output,
Transaction,
TransactionBuilder,
Word,
},
fuel_types::*,
Expand All @@ -71,6 +74,7 @@ pub struct BenchDb {
db: GenesisDatabase,
/// Used for RAII cleanup. Contents of this directory are deleted on drop.
_tmp_dir: utils::ShallowTempDir,
latest_block: u32,
}

impl BenchDb {
Expand Down Expand Up @@ -131,18 +135,40 @@ impl BenchDb {
&block.compress(&chain_config.consensus_parameters.chain_id()),
)
.unwrap();

Ok(Self {
_tmp_dir: tmp_dir,
db: database,
latest_block: 0,
})
}

fn add_blocks(&mut self, nb_blocks: u32) {
for i in 1..=nb_blocks {
let mut block =
fuel_core::service::genesis::create_genesis_block(&Config::local_node());
let transactions = block.transactions_mut();
// Add a dummy transaction to the block to make the block bigger.
transactions.push(Transaction::Script(
TransactionBuilder::script(vec![], vec![1; 200_000]).finalize(),
));
let config = Config::local_node();
let chain_config = config.snapshot_reader.chain_config();
self.db
.storage::<FuelBlocks>()
.insert(
&i.into(),
&block.compress(&chain_config.consensus_parameters.chain_id()),
)
.unwrap();
}
self.latest_block = nb_blocks;
}

/// Creates a `VmDatabase` instance.
fn to_vm_database(&self) -> VmStorage<StorageTransaction<GenesisDatabase>> {
let consensus = ConsensusHeader {
prev_root: Default::default(),
height: 1.into(),
height: self.latest_block.into(),
time: Tai64::UNIX_EPOCH,
generated: (),
};
Expand Down Expand Up @@ -526,14 +552,25 @@ pub fn run(c: &mut Criterion) {
VmBench::new(op::bhei(0x10)),
);

let mut db =
BenchDb::new(&VmBench::CONTRACT).expect("Unable to fill contract storage");
db.add_blocks(10000);

run_group_ref(
&mut c.benchmark_group("bhsh"),
"bhsh",
VmBench::new(op::bhsh(0x10, RegId::ZERO)).with_prepare_script(vec![
op::movi(0x10, Bytes32::LEN.try_into().unwrap()),
op::aloc(0x10),
op::move_(0x10, RegId::HP),
]),
VmBench::new(op::bhsh(RegId::HP, 0x11))
.prepend_prepare_script(vec![
// Store number of bytes we want to alloc for the future result
op::movi(0x10, Bytes32::LEN.try_into().unwrap()),
// Allocate space for the future result
op::aloc(0x10),
// Add block height to 0x11
op::movi(0x11, 0xc9),
])
// Random height
.with_height(0xca.into())
.with_db(db.to_vm_database()),
);

run_group_ref(
Expand Down
65 changes: 62 additions & 3 deletions benches/benches/vm_set/crypto.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use crate::utils::aloc_bytearray;

use super::run_group_ref;

use criterion::{
Expand Down Expand Up @@ -28,7 +30,7 @@ pub fn run(c: &mut Criterion) {
"eck1",
VmBench::new(op::eck1(RegId::HP, 0x20, 0x21))
.with_prepare_script(vec![
op::gtf_args(0x20, 0x00, GTFArgs::ScriptData),
op::gtf_args(0x20, RegId::ZERO, GTFArgs::ScriptData),
op::addi(
0x21,
0x20,
Expand All @@ -55,7 +57,7 @@ pub fn run(c: &mut Criterion) {
"ecr1",
VmBench::new(op::ecr1(RegId::HP, 0x20, 0x21))
.with_prepare_script(vec![
op::gtf_args(0x20, 0x00, GTFArgs::ScriptData),
op::gtf_args(0x20, RegId::ZERO, GTFArgs::ScriptData),
op::addi(
0x21,
0x20,
Expand Down Expand Up @@ -122,7 +124,7 @@ pub fn run(c: &mut Criterion) {
format!("{i}"),
VmBench::new(op::ed19(0x20, 0x21, RegId::ZERO, 0x10))
.with_prepare_script(vec![
op::gtf_args(0x20, 0x00, GTFArgs::ScriptData),
op::gtf_args(0x20, RegId::ZERO, GTFArgs::ScriptData),
op::addi(
0x21,
0x20,
Expand Down Expand Up @@ -154,4 +156,61 @@ pub fn run(c: &mut Criterion) {
);
}
bench_ed19.finish();

// ecop testing mul as it's the most expensive operation
let mut points_bytearray = Vec::new();
// X
points_bytearray.extend(
hex::decode("2bd3e6d0f3b142924f5ca7b49ce5b9d54c4703d7ae5648e61d02268b1a0a9fb7")
.unwrap(),
);
// Y
points_bytearray.extend(
hex::decode("21611ce0a6af85915e2f1d70300909ce2e49dfad4a4619c8390cae66cefdb204")
.unwrap(),
);
// Scalar
points_bytearray.extend(
hex::decode("ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff")
.unwrap(),
);
// 96 bytes = 1 point and 1 scalar
let prepare_points = aloc_bytearray::<96>(0x10, points_bytearray.try_into().unwrap());
run_group_ref(
&mut c.benchmark_group("ecop"),
"ecop",
VmBench::new(op::ecop(0x10, RegId::ZERO, 0x01, 0x10))
.with_prepare_script(prepare_points),
);

// ec pairing
let mut bench_epar = c.benchmark_group("epar");
for i in [1, 2, 4, 8, 16, 32, 64, 128, 256, 512] {
let mut points_bytearray = Vec::new();
for _ in 0u32..i {
points_bytearray.extend(
hex::decode(
"0000000000000000000000000000000000000000000000000000000000000001\
0000000000000000000000000000000000000000000000000000000000000002\
198e9393920d483a7260bfb731fb5d25f1aa493335a9e71297e485b7aef312c2\
1800deef121f1e76426a00665e5c4479674322d4f75edadd46debd5cd992f6ed\
090689d0585ff075ec9e99ad690c3395bc4b313370b38ef355acdadcd122975b\
12c85ea5db8c6deb4aab71808dcb408fe3d1e7690c43d37b4ce6cc0166fa7daa",
)
.unwrap(),
);
}
bench_epar.throughput(Throughput::Bytes(i as u64));
run_group_ref(
&mut bench_epar,
format!("{i}"),
VmBench::new(op::epar(0x12, RegId::ZERO, 0x11, 0x10))
.with_data(points_bytearray)
.with_prepare_script(vec![
op::movi(0x11, i),
op::gtf_args(0x10, 0x00, GTFArgs::ScriptData),
]),
);
}
bench_epar.finish();
}
6 changes: 3 additions & 3 deletions benches/src/bin/collect.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use clap::Parser;
use fuel_core_types::fuel_tx::{
consensus_parameters::gas::GasCostsValuesV4,
consensus_parameters::gas::GasCostsValuesV5,
ConsensusParameters,
GasCosts,
};
Expand Down Expand Up @@ -371,7 +371,7 @@ pub const GIT: &str = ""#,
r#"";"#,
r##"
pub fn default_gas_costs() -> GasCostsValues {
GasCostsValuesV4 {"##,
GasCostsValuesV5 {"##,
r##" }.into()
}
"##,
Expand Down Expand Up @@ -495,7 +495,7 @@ impl State {
)
}

fn to_gas_costs(&self) -> GasCostsValuesV4 {
fn to_gas_costs(&self) -> GasCostsValuesV5 {
serde_yaml::from_value(self.to_yaml()).unwrap()
}

Expand Down
10 changes: 4 additions & 6 deletions benches/src/default_gas_costs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ pub fn default_gas_costs() -> GasCostsValues {
andi: 2,
bal: 274,
bhei: 2,
bhsh: 2,
bhsh: 32,
burn: 7566,
cb: 2,
cfsi: 2,
Expand Down Expand Up @@ -90,12 +90,10 @@ pub fn default_gas_costs() -> GasCostsValues {
wqmm: 6,
xor: 2,
xori: 2,
// TODO: Change to correct values
ecop: 2,
// TODO: Change to correct values
ecop: 3500,
epar: DependentCost::HeavyOperation {
base: 2,
gas_per_unit: 2,
base: 69000,
gas_per_unit: 52000,
},
aloc: DependentCost::LightOperation {
base: 2,
Expand Down
11 changes: 10 additions & 1 deletion benches/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,17 +141,26 @@ pub struct VmBenchPrepared {
pub diff: diff::Diff<diff::InitialVmState>,
}

const TX_SIZE: u64 = 64 * 1024 * 1024;

impl VmBench {
pub const SALT: Salt = Salt::zeroed();
pub const CONTRACT: ContractId = ContractId::zeroed();

pub fn new(instruction: Instruction) -> Self {
let mut consensus_params = ConsensusParameters::default();
consensus_params.set_tx_params(
TxParameters::default().with_max_gas_per_tx(LARGE_GAS_LIMIT + 1),
TxParameters::default()
.with_max_gas_per_tx(LARGE_GAS_LIMIT + 1)
.with_max_size(TX_SIZE),
);
consensus_params.set_fee_params(FeeParameters::default().with_gas_per_byte(0));
consensus_params.set_gas_costs(GasCosts::free());
consensus_params.set_script_params(
ScriptParameters::default()
.with_max_script_length(TX_SIZE)
.with_max_script_data_length(TX_SIZE),
);

Self {
params: consensus_params,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"andi": 2,
"bal": 366,
"bhei": 2,
"bhsh": 2,
"bhsh": 32,
"burn": 33949,
"cb": 2,
"cfei": 2,
Expand Down Expand Up @@ -129,7 +129,7 @@
"wqmm": 11,
"xor": 2,
"xori": 2,
"ecop": 2,
"ecop": 3500,
"call": {
"LightOperation": {
"base": 21687,
Expand Down Expand Up @@ -240,8 +240,8 @@
},
"epar": {
"HeavyOperation": {
"base": 2,
"gas_per_unit": 2
"base": 69000,
"gas_per_unit": 52000
}
},
"contract_root": {
Expand Down
8 changes: 4 additions & 4 deletions bin/fuel-core/chainspec/local-testnet/chain_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"andi": 2,
"bal": 274,
"bhei": 2,
"bhsh": 2,
"bhsh": 32,
"burn": 7566,
"cb": 2,
"cfsi": 2,
Expand Down Expand Up @@ -126,7 +126,7 @@
"wqmm": 6,
"xor": 2,
"xori": 2,
"ecop": 2,
"ecop": 3500,
"aloc": {
"LightOperation": {
"base": 2,
Expand Down Expand Up @@ -273,8 +273,8 @@
},
"epar": {
"HeavyOperation": {
"base": 2,
"gas_per_unit": 2
"base": 69000,
"gas_per_unit": 52000
}
},
"contract_root": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,27 +30,20 @@ mod tests;
#[derive(Debug)]
/// Receives the next gas price algorithm via a shared `BlockGasPriceAlgo` instance
pub struct FuelGasPriceProvider<A> {
// // Scale the gas price down by this factor, for e.g. Wei to Gwei
// scaling_factor: u64,
algorithm: SharedGasPriceAlgo<A>,
}

impl<A> Clone for FuelGasPriceProvider<A> {
fn clone(&self) -> Self {
Self {
// scaling_factor: self.scaling_factor,
algorithm: self.algorithm.clone(),
}
}
}

impl<A> FuelGasPriceProvider<A> {
pub fn new(algorithm: SharedGasPriceAlgo<A>) -> Self {
Self {
// // Default scaling factor is 1_000_000_000, to convert Wei to Gwei
// scaling_factor: 1_000_000_000,
algorithm,
}
Self { algorithm }
}
}

Expand Down
Loading

0 comments on commit 4669ce8

Please sign in to comment.