Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
2opremio committed Aug 6, 2023
1 parent bb18498 commit 2734472
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions cmd/soroban-rpc/lib/preflight/src/fees.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use ledger_storage;
use soroban_env_host::budget::Budget;
use soroban_env_host::e2e_invoke::get_ledger_changes;
use soroban_env_host::fees::{
compute_transaction_resource_fee, compute_write_fee_per_1kb, FeeConfiguration,
TransactionResources, WriteFeeConfiguration,
Expand Down Expand Up @@ -42,12 +43,12 @@ pub(crate) fn compute_host_function_transaction_data_and_min_fee(
write_entries: read_write_entries,
read_bytes: soroban_resources.read_bytes,
write_bytes: soroban_resources.write_bytes,
metadata_size_bytes: soroban_resources.extended_meta_data_size_bytes,
// Note: we could get a better transaction size if the full transaction was passed down to libpreflight
transaction_size_bytes: estimate_max_transaction_size_for_operation(
&OperationBody::InvokeHostFunction(op.clone()),
&soroban_resources.footprint,
)?,
contract_events_size_bytes: soroban_resources.contract_events_size_bytes,
};
let (min_fee, ref_fee) =
compute_transaction_resource_fee_wrapper(&transaction_resources, &fee_configuration);
Expand Down Expand Up @@ -95,7 +96,7 @@ fn estimate_max_transaction_size_for_operation(
instructions: 0,
read_bytes: 0,
write_bytes: 0,
extended_meta_data_size_bytes: 0,
contract_events_size_bytes: 0,
},
refundable_fee: 0,
ext: ExtensionPoint::V0,
Expand All @@ -118,33 +119,29 @@ fn calculate_host_function_soroban_resources(
budget: &Budget,
events: &Vec<DiagnosticEvent>,
) -> Result<SorobanResources, Box<dyn error::Error>> {
let fp = storage_footprint_to_ledger_footprint(&storage.footprint)?;
/*
readBytes = size(footprint.readOnly) + size(footprint.readWrite)
writeBytes = size(storage.map[rw entries])
metadataSize = readBytes(footprint.readWrite) + writeBytes + eventsSize
*/
let original_write_ledger_entry_bytes =
calculate_unmodified_ledger_entry_bytes(fp.read_write.as_vec(), snapshot_source, false)?;
let read_bytes =
calculate_unmodified_ledger_entry_bytes(fp.read_only.as_vec(), snapshot_source, false)?
+ original_write_ledger_entry_bytes;
let write_bytes =
calculate_modified_read_write_ledger_entry_bytes(&storage.footprint, &storage.map, budget)?;
let meta_data_size_bytes =
original_write_ledger_entry_bytes + write_bytes + calculate_event_size_bytes(events)?;
let ledger_changes = get_ledger_changes(budget, storage, snapshot_source)?;
let read_bytes = ledger_changes
.iter()
.map(|c| c.encoded_key.len() + c.old_entry_size_bytes)
.sum();
let write_bytes = ledger_changes
.iter()
.map(|c| c.encoded_key.len() + c.encoded_new_value.len())
.sum();
let footprint = storage_footprint_to_ledger_footprint(&storage.footprint)?;
let contract_events_size_bytes = calculate_event_size_bytes(events)?;

// Add a 15% leeway with a minimum of 50k instructions
let instructions = max(
budget.get_cpu_insns_consumed()? + 50000,
budget.get_cpu_insns_consumed()? * 115 / 100,
);
Ok(SorobanResources {
footprint: fp,
footprint,
instructions: u32::try_from(instructions)?,
read_bytes,
write_bytes,
extended_meta_data_size_bytes: meta_data_size_bytes,
contract_events_size_bytes,
})
}

Expand Down Expand Up @@ -183,8 +180,8 @@ fn get_fee_configuration(
);
};

let ConfigSettingEntry::ContractMetaDataV0(metadata) =
ledger_storage.get_configuration_setting(ConfigSettingId::ContractMetaDataV0)?
let ConfigSettingEntry::ContractEventsV0(events) =
ledger_storage.get_configuration_setting(ConfigSettingId::ContractEventsV0)?
else {
return Err(
"get_fee_configuration(): unexpected config setting entry for MetaDataV0 key".into(),
Expand Down Expand Up @@ -217,8 +214,8 @@ fn get_fee_configuration(
&write_fee_configuration,
),
fee_per_historical_1kb: historical_data.fee_historical1_kb,
fee_per_metadata_1kb: metadata.fee_extended_meta_data1_kb,
fee_per_propagate_1kb: bandwidth.fee_propagate_data1_kb,
fee_per_contract_event_1kb: events.fee_contract_events1_kb,
fee_per_transaction_size_1kb: bandwidth.fee_tx_size1_kb,
};
Ok(fee_configuration)
}
Expand Down Expand Up @@ -314,22 +311,22 @@ pub(crate) fn compute_bump_footprint_exp_transaction_data_and_min_fee(
instructions: 0,
read_bytes,
write_bytes: 0,
extended_meta_data_size_bytes: 2 * read_bytes,
contract_events_size_bytes: 0,
});
let transaction_resources = TransactionResources {
instructions: 0,
read_entries: u32::try_from(soroban_resources.footprint.read_only.as_vec().len())?,
write_entries: 0,
read_bytes: soroban_resources.read_bytes,
write_bytes: 0,
metadata_size_bytes: soroban_resources.extended_meta_data_size_bytes,
transaction_size_bytes: estimate_max_transaction_size_for_operation(
&OperationBody::BumpFootprintExpiration(BumpFootprintExpirationOp {
ext: ExtensionPoint::V0,
ledgers_to_expire: ledgers_to_expire,
}),
&soroban_resources.footprint,
)?,
contract_events_size_bytes: 0,
};
let fee_configuration = get_fee_configuration(snapshot_source, bucket_list_size)?;
let (min_fee, ref_fee) =
Expand Down Expand Up @@ -359,7 +356,7 @@ pub(crate) fn compute_restore_footprint_transaction_data_and_min_fee(
// we should review it in preview 11.
read_bytes: write_bytes,
write_bytes,
extended_meta_data_size_bytes: 2 * write_bytes,
contract_events_size_bytes: 0,
});
let entry_count = u32::try_from(soroban_resources.footprint.read_write.as_vec().len())?;
let transaction_resources = TransactionResources {
Expand All @@ -368,13 +365,13 @@ pub(crate) fn compute_restore_footprint_transaction_data_and_min_fee(
write_entries: entry_count,
read_bytes: soroban_resources.read_bytes,
write_bytes: soroban_resources.write_bytes,
metadata_size_bytes: soroban_resources.extended_meta_data_size_bytes,
transaction_size_bytes: estimate_max_transaction_size_for_operation(
&OperationBody::RestoreFootprint(RestoreFootprintOp {
ext: ExtensionPoint::V0,
}),
&soroban_resources.footprint,
)?,
contract_events_size_bytes: 0,
};
let fee_configuration = get_fee_configuration(snapshot_source, bucket_list_size)?;
let (min_fee, ref_fee) =
Expand All @@ -397,10 +394,7 @@ fn hack_soroban_resources(resources: SorobanResources) -> SorobanResources {
resources.read_bytes * 120 / 100,
),
write_bytes: resources.write_bytes,
extended_meta_data_size_bytes: max(
resources.extended_meta_data_size_bytes + 1000,
resources.extended_meta_data_size_bytes * 120 / 100,
),
contract_events_size_bytes: resources.contract_events_size_bytes,
};
}

Expand Down

0 comments on commit 2734472

Please sign in to comment.