Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: Stabilize Compute Costs #8892

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,12 @@ protocol_feature_fix_staking_threshold = []
protocol_feature_fix_contract_loading_cost = []
protocol_feature_reject_blocks_with_outdated_protocol_version = []
protocol_feature_flat_state = []
protocol_feature_compute_costs = []
nightly = [
"nightly_protocol",
"protocol_feature_fix_staking_threshold",
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"protocol_feature_flat_state",
"protocol_feature_compute_costs",
]

nightly_protocol = []
Expand Down
4 changes: 0 additions & 4 deletions core/primitives/src/runtime/parameter_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,6 @@ impl TryFrom<&ParameterValue> for ParameterCost {
fn try_from(value: &ParameterValue) -> Result<Self, Self::Error> {
match value {
ParameterValue::ParameterCost { gas, compute } => {
if !cfg!(feature = "protocol_feature_compute_costs") {
assert_eq!(compute, gas, "Compute cost must match gas cost");
}

Ok(ParameterCost { gas: *gas, compute: *compute })
}
// If not specified, compute costs default to gas costs.
Expand Down
11 changes: 7 additions & 4 deletions core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,12 @@ pub enum ProtocolFeature {
/// Meta Transaction NEP-366: https://github.com/near/NEPs/blob/master/neps/nep-0366.md
DelegateAction,

/// Decouple compute and gas costs of operations to safely limit the compute time it takes to
/// process the chunk.
///
/// Compute Costs NET-455: https://github.com/near/NEPs/blob/master/neps/nep-0455.md
ComputeCosts,

/// In case not all validator seats are occupied our algorithm provide incorrect minimal seat
/// price - it reports as alpha * sum_stake instead of alpha * sum_stake / (1 - alpha), where
/// alpha is min stake ratio
Expand All @@ -152,8 +158,6 @@ pub enum ProtocolFeature {
RejectBlocksWithOutdatedProtocolVersions,
#[cfg(feature = "protocol_feature_flat_state")]
FlatStorageReads,
#[cfg(feature = "protocol_feature_compute_costs")]
ComputeCosts,
}

/// Both, outgoing and incoming tcp connections to peers, will be rejected if `peer's`
Expand Down Expand Up @@ -238,6 +242,7 @@ impl ProtocolFeature {
ProtocolFeature::Ed25519Verify
| ProtocolFeature::ZeroBalanceAccount
| ProtocolFeature::DelegateAction => 59,
ProtocolFeature::ComputeCosts => 61,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When (or before) we can put stable protocol features to version 61, we have to bump STABLE_PROTOCOL_VERSION to 61. We usually do it in the same PR as the feature stabilization, so you can do it directly in this PR. That should also make stable tests easier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed this in #8915


// Nightly features
#[cfg(feature = "protocol_feature_fix_staking_threshold")]
Expand All @@ -248,8 +253,6 @@ impl ProtocolFeature {
ProtocolFeature::RejectBlocksWithOutdatedProtocolVersions => 132,
#[cfg(feature = "protocol_feature_flat_state")]
ProtocolFeature::FlatStorageReads => 135,
#[cfg(feature = "protocol_feature_compute_costs")]
ProtocolFeature::ComputeCosts => 136,
}
}
}
Expand Down
1 change: 0 additions & 1 deletion runtime/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ near-vm-runner.workspace = true
default = []
dump_errors_schema = ["near-vm-errors/dump_errors_schema"]
protocol_feature_flat_state = ["near-store/protocol_feature_flat_state", "near-vm-logic/protocol_feature_flat_state"]
protocol_feature_compute_costs = ["near-primitives/protocol_feature_compute_costs"]
nightly_protocol = ["near-primitives/nightly_protocol"]
no_cpu_compatibility_checks = ["near-vm-runner/no_cpu_compatibility_checks"]

Expand Down
28 changes: 17 additions & 11 deletions runtime/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1287,8 +1287,7 @@ impl Runtime {
.expect("`process_transaction` must populate compute usage"),
)?;

// TODO(#8032): Remove when compute costs are stabilized.
if !cfg!(feature = "protocol_feature_compute_costs") {
if !checked_feature!("stable", ComputeCosts, apply_state.current_protocol_version) {
assert_eq!(
total_compute_usage, total_gas_burnt,
"Compute usage must match burnt gas"
Expand Down Expand Up @@ -1337,8 +1336,8 @@ impl Runtime {
.compute_usage
.expect("`process_receipt` must populate compute usage"),
)?;
// TODO(#8032): Remove when compute costs are stabilized.
if !cfg!(feature = "protocol_feature_compute_costs") {

if !checked_feature!("stable", ComputeCosts, apply_state.current_protocol_version) {
assert_eq!(
total_compute_usage, total_gas_burnt,
"Compute usage must match burnt gas"
Expand Down Expand Up @@ -1594,7 +1593,6 @@ impl Runtime {

#[cfg(test)]
mod tests {
#[cfg(feature = "protocol_feature_compute_costs")]
use assert_matches::assert_matches;
use near_crypto::{InMemorySigner, KeyType, Signer};
use near_primitives::account::AccessKey;
Expand All @@ -1611,7 +1609,6 @@ mod tests {
use near_store::set_access_key;
use near_store::test_utils::create_tries;
use near_store::StoreCompiledContractCache;
#[cfg(feature = "protocol_feature_compute_costs")]
use near_vm_logic::{ExtCosts, ParameterCost};
use near_vm_runner::get_contract_cache_key;
use near_vm_runner::internal::VMKind;
Expand Down Expand Up @@ -2553,7 +2550,6 @@ mod tests {
.expect("Compilation result should be non-empty");
}

#[cfg(feature = "protocol_feature_compute_costs")]
#[test]
fn test_compute_usage_limit() {
let initial_balance = to_yocto(1_000_000);
Expand All @@ -2562,8 +2558,18 @@ mod tests {
setup_runtime(initial_balance, initial_locked, 1);

let mut free_config = RuntimeConfig::free();
let sha256_cost =
ParameterCost { gas: Gas::from(1u64), compute: Compute::from(10_000_000_000_000u64) };
let sha256_cost = ParameterCost {
gas: Gas::from(1_000_000u64),
compute: if checked_feature!(
"stable",
ComputeCosts,
apply_state.current_protocol_version
) {
Compute::from(10_000_000_000_000u64)
} else {
Compute::from(1_000_000u64)
},
};
Comment on lines +2561 to +2572
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless you want to add a test that checks a version before the feature was added, I don't think you need this checked_feature!, right? (Assuming we bump the protocol version first)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've addressed this in #8915

free_config.wasm_config.ext_costs.costs[ExtCosts::sha256_base] = sha256_cost.clone();
apply_state.config = Arc::new(free_config);
// This allows us to execute 1 receipt with a function call per apply.
Expand All @@ -2583,7 +2589,7 @@ mod tests {
vec![Action::FunctionCall(FunctionCallAction {
method_name: "ext_sha256".to_string(),
args: b"first".to_vec(),
gas: 1,
gas: sha256_cost.gas,
deposit: 0,
})],
);
Expand All @@ -2594,7 +2600,7 @@ mod tests {
vec![Action::FunctionCall(FunctionCallAction {
method_name: "ext_sha256".to_string(),
args: b"second".to_vec(),
gas: 1,
gas: sha256_cost.gas,
deposit: 0,
})],
);
Expand Down