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

feat: stabilize limiting functions number in a contract #5361

Merged
merged 14 commits into from
Nov 20, 2021
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [unreleased]

### Protocol Changes
* Limit number of wasm functions in one contract to 10_000. [#4954](https://github.com/near/nearcore/pull/4954)

## `1.22.0` [11-15-2021]

### Protocol Changes
* Upgrade from Wasmer 0 to Wasmer 2, bringing better performance and reliability. [#4934](https://github.com/near/nearcore/pull/4934)
* Lower regular_op_cost (execution of a single WASM instruction) from 3_856_371 to 2_207_874. [#4979](https://github.com/near/nearcore/pull/4979)
Expand Down
3 changes: 1 addition & 2 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,8 @@ protocol_feature_block_header_v3 = []
protocol_feature_alt_bn128 = ["near-primitives-core/protocol_feature_alt_bn128", "near-vm-errors/protocol_feature_alt_bn128"]
protocol_feature_chunk_only_producers = ["protocol_feature_block_header_v3"]
protocol_feature_routing_exchange_algorithm = ["near-primitives-core/protocol_feature_routing_exchange_algorithm"]
protocol_feature_limit_contract_functions_number = []
protocol_feature_lower_regular_op_cost2 = []
nightly_protocol_features = ["nightly_protocol", "protocol_feature_block_header_v3", "protocol_feature_alt_bn128", "protocol_feature_chunk_only_producers", "protocol_feature_routing_exchange_algorithm", "protocol_feature_limit_contract_functions_number", "protocol_feature_lower_regular_op_cost2"]
nightly_protocol_features = ["nightly_protocol", "protocol_feature_block_header_v3", "protocol_feature_alt_bn128", "protocol_feature_chunk_only_producers", "protocol_feature_routing_exchange_algorithm", "protocol_feature_lower_regular_op_cost2"]
nightly_protocol = []

[dev-dependencies]
Expand Down
4 changes: 1 addition & 3 deletions core/primitives/src/runtime/config_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ static CONFIGS: &[(ProtocolVersion, &[u8])] = &[
(0, include_config!("29.json")),
(42, include_config!("42.json")),
(48, include_config!("48.json")),
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
(123, include_config!("123.json")),
(49, include_config!("49.json")),
#[cfg(feature = "protocol_feature_lower_regular_op_cost2")]
(124, include_config!("124.json")),
];
Expand Down Expand Up @@ -119,7 +118,6 @@ mod tests {
"FF2Qg5qSM6iWQjD5ZyzEhdAV2g5MyQKXGYh4kyt8mMcE",
"97UzHtVFBc4235jdur3DgNSUGNfGQfzRDLmAkYdZ19Re",
"C6uw6BoeXr3KoKpVP34hBA7TqoywMbwMtJgqbTpPCiSB",
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
"AFpppR4PmSe4YBWzcyyH8vzLCeDbzfTRjeCGPVrmpPJj",
#[cfg(feature = "protocol_feature_lower_regular_op_cost2")]
"2cuq2HvuHT7Z27LUbgEtMxP2ejqrHK34J2V1GL1joiMn",
Expand Down
12 changes: 5 additions & 7 deletions core/primitives/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,9 @@ pub enum ProtocolFeature {
LowerDataReceiptAndEcrecoverBaseCost,
/// Lowers the cost of wasm instruction due to switch to wasmer2.
LowerRegularOpCost,
/// Limit number of wasm functions in one contract. See
/// <https://github.com/near/nearcore/pull/4954> for more details.
LimitContractFunctionsNumber,

// nightly features
#[cfg(feature = "protocol_feature_block_header_v3")]
Expand All @@ -124,10 +127,6 @@ pub enum ProtocolFeature {
ChunkOnlyProducers,
#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
RoutingExchangeAlgorithm,
/// Limit number of wasm functions in one contract. See
/// <https://github.com/near/nearcore/pull/4954> for more details.
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
LimitContractFunctionsNumber,
/// Lowers the cost of wasm instruction due to switch to faster,
/// compiler-intrinsics based gas counter.
#[cfg(feature = "protocol_feature_lower_regular_op_cost2")]
Expand All @@ -138,7 +137,7 @@ pub enum ProtocolFeature {
/// Some features (e. g. FixStorageUsage) require that there is at least one epoch with exactly
/// the corresponding version
#[cfg(not(feature = "nightly_protocol"))]
pub const PROTOCOL_VERSION: ProtocolVersion = 48;
pub const PROTOCOL_VERSION: ProtocolVersion = 49;

/// Current latest nightly version of the protocol.
#[cfg(feature = "nightly_protocol")]
Expand All @@ -165,6 +164,7 @@ impl ProtocolFeature {
| ProtocolFeature::LowerDataReceiptAndEcrecoverBaseCost
| ProtocolFeature::LowerRegularOpCost
| ProtocolFeature::SimpleNightshade => 48,
ProtocolFeature::LimitContractFunctionsNumber => 49,

// Nightly features
#[cfg(feature = "protocol_feature_alt_bn128")]
Expand All @@ -175,8 +175,6 @@ impl ProtocolFeature {
ProtocolFeature::ChunkOnlyProducers => 115,
#[cfg(feature = "protocol_feature_routing_exchange_algorithm")]
ProtocolFeature::RoutingExchangeAlgorithm => 117,
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
ProtocolFeature::LimitContractFunctionsNumber => 123,
#[cfg(feature = "protocol_feature_lower_regular_op_cost2")]
ProtocolFeature::LowerRegularOpCost2 => 124,
}
Expand Down
5 changes: 2 additions & 3 deletions integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ protocol_feature_alt_bn128 = [
]
protocol_feature_block_header_v3 = ["near-primitives/protocol_feature_block_header_v3", "near-chain/protocol_feature_block_header_v3", "near-store/protocol_feature_block_header_v3"]
protocol_feature_chunk_only_producers = ["near-client/protocol_feature_chunk_only_producers"]
protocol_feature_limit_contract_functions_number = ["near-primitives/protocol_feature_limit_contract_functions_number", "near-vm-runner/protocol_feature_limit_contract_functions_number"]
nightly_protocol_features = ["nearcore/nightly_protocol_features", "protocol_feature_alt_bn128", "protocol_feature_block_header_v3", "protocol_feature_limit_contract_functions_number"]
nightly_protocol_features = ["nearcore/nightly_protocol_features", "protocol_feature_alt_bn128", "protocol_feature_block_header_v3"]
nightly_protocol = ["nearcore/nightly_protocol"]
sandbox = ["near-network/sandbox", "near-chain/sandbox", "node-runtime/sandbox", "near-client/sandbox"]
no_cache = ["nearcore/no_cache"]
no_cache = ["nearcore/no_cache"]
17 changes: 4 additions & 13 deletions integration-tests/tests/client/process_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ use near_primitives::types::validator_stake::ValidatorStake;
use near_primitives::types::{AccountId, BlockHeight, EpochId, NumBlocks, ProtocolVersion};
use near_primitives::utils::to_timestamp;
use near_primitives::validator_signer::{InMemoryValidatorSigner, ValidatorSigner};
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
use near_primitives::version::ProtocolFeature;
use near_primitives::version::PROTOCOL_VERSION;
use near_primitives::views::{
Expand Down Expand Up @@ -3196,11 +3195,7 @@ fn test_validator_stake_host_function() {
fn test_limit_contract_functions_number_upgrade() {
let functions_number_limit: u32 = 10_000;

#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
let old_protocol_version = ProtocolFeature::LimitContractFunctionsNumber.protocol_version() - 1;
#[cfg(not(feature = "protocol_feature_limit_contract_functions_number"))]
let old_protocol_version = PROTOCOL_VERSION - 1;

let new_protocol_version = old_protocol_version + 1;

// Prepare TestEnv with a contract at the old protocol version.
Expand Down Expand Up @@ -3288,14 +3283,10 @@ fn test_limit_contract_functions_number_upgrade() {
};

assert!(matches!(old_outcome.status, FinalExecutionStatus::SuccessValue(_)));
if cfg!(feature = "protocol_feature_limit_contract_functions_number") {
assert!(matches!(
new_outcome.status,
FinalExecutionStatus::Failure(TxExecutionError::ActionError(_))
));
} else {
assert!(matches!(new_outcome.status, FinalExecutionStatus::SuccessValue(_)));
}
assert!(matches!(
new_outcome.status,
FinalExecutionStatus::Failure(TxExecutionError::ActionError(_))
));
}

mod access_key_nonce_range_tests {
Expand Down
3 changes: 1 addition & 2 deletions nearcore/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ protocol_feature_alt_bn128 = ["near-primitives/protocol_feature_alt_bn128", "nod
protocol_feature_block_header_v3 = ["near-epoch-manager/protocol_feature_block_header_v3", "near-store/protocol_feature_block_header_v3", "near-primitives/protocol_feature_block_header_v3", "near-chain/protocol_feature_block_header_v3", "near-client/protocol_feature_block_header_v3"]
protocol_feature_chunk_only_producers = ["protocol_feature_block_header_v3", "near-chain-configs/protocol_feature_chunk_only_producers", "near-epoch-manager/protocol_feature_chunk_only_producers", "near-chain/protocol_feature_chunk_only_producers", "near-client/protocol_feature_chunk_only_producers", "node-runtime/protocol_feature_chunk_only_producers", "near-rosetta-rpc/protocol_feature_chunk_only_producers"]
protocol_feature_routing_exchange_algorithm = ["near-primitives/protocol_feature_routing_exchange_algorithm", "near-chain/protocol_feature_routing_exchange_algorithm", "near-network/protocol_feature_routing_exchange_algorithm", "near-client/protocol_feature_routing_exchange_algorithm", "near-jsonrpc/protocol_feature_routing_exchange_algorithm"]
protocol_feature_limit_contract_functions_number = ["near-primitives/protocol_feature_limit_contract_functions_number", "near-vm-runner/protocol_feature_limit_contract_functions_number"]
protocol_feature_lower_regular_op_cost2 = ["near-primitives/protocol_feature_lower_regular_op_cost2"]
nightly_protocol_features = ["nightly_protocol", "near-primitives/nightly_protocol_features", "near-client/nightly_protocol_features", "near-epoch-manager/nightly_protocol_features", "near-store/nightly_protocol_features", "protocol_feature_block_header_v3", "protocol_feature_alt_bn128", "protocol_feature_chunk_only_producers", "protocol_feature_routing_exchange_algorithm", "protocol_feature_limit_contract_functions_number", "protocol_feature_lower_regular_op_cost2"]
nightly_protocol_features = ["nightly_protocol", "near-primitives/nightly_protocol_features", "near-client/nightly_protocol_features", "near-epoch-manager/nightly_protocol_features", "near-store/nightly_protocol_features", "protocol_feature_block_header_v3", "protocol_feature_alt_bn128", "protocol_feature_chunk_only_producers", "protocol_feature_routing_exchange_algorithm", "protocol_feature_lower_regular_op_cost2"]
nightly_protocol = ["near-primitives/nightly_protocol", "near-jsonrpc/nightly_protocol"]

# Force usage of a specific wasm vm irrespective of protocol version.
Expand Down
1 change: 0 additions & 1 deletion runtime/near-vm-errors/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ near-rpc-error-macro = { path = "../../tools/rpctypegen/macro", version = "0.1.0
[features]
dump_errors_schema = ["near-rpc-error-macro/dump_errors_schema"]
protocol_feature_alt_bn128 = []
protocol_feature_limit_contract_functions_number = []

[package.metadata.workspaces]
independent = true
2 changes: 0 additions & 2 deletions runtime/near-vm-errors/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ pub enum PrepareError {
/// Error creating memory.
Memory,
/// Contract contains too many functions.
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
TooManyFunctions,
}

Expand Down Expand Up @@ -294,7 +293,6 @@ impl fmt::Display for PrepareError {
StackHeightInstrumentation => write!(f, "Stack instrumentation failed."),
Instantiate => write!(f, "Error happened during instantiation."),
Memory => write!(f, "Error creating memory."),
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
TooManyFunctions => write!(f, "Too many functions in contract."),
}
}
Expand Down
1 change: 0 additions & 1 deletion runtime/near-vm-runner/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ protocol_feature_alt_bn128 = [
"near-primitives/protocol_feature_alt_bn128",
"near-vm-errors/protocol_feature_alt_bn128"
]
protocol_feature_limit_contract_functions_number = ["near-primitives/protocol_feature_limit_contract_functions_number", "near-vm-errors/protocol_feature_limit_contract_functions_number"]
nightly_protocol = ["near-primitives/nightly_protocol"]

[package.metadata.cargo-udeps.ignore]
Expand Down
1 change: 0 additions & 1 deletion runtime/near-vm-runner/src/prepare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ impl<'a> ContractModule<'a> {
}

fn validate_functions_number(self) -> Result<Self, PrepareError> {
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
if let Some(max_functions_number) =
self.config.limit_config.max_functions_number_per_contract
{
Expand Down
11 changes: 0 additions & 11 deletions runtime/near-vm-runner/src/tests/compile_errors.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
use near_primitives::version::ProtocolFeature;
#[cfg(not(feature = "protocol_feature_limit_contract_functions_number"))]
use near_primitives::version::PROTOCOL_VERSION;
use near_vm_errors::{CompilationError, FunctionCallError, PrepareError, VMError};

#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
use assert_matches::assert_matches;

use crate::tests::{
Expand Down Expand Up @@ -156,12 +152,8 @@ fn test_evil_function_index() {
#[test]
fn test_limit_contract_functions_number() {
with_vm_variants(|vm_kind: VMKind| {
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
let old_protocol_version =
ProtocolFeature::LimitContractFunctionsNumber.protocol_version() - 1;
#[cfg(not(feature = "protocol_feature_limit_contract_functions_number"))]
let old_protocol_version = PROTOCOL_VERSION - 1;

let new_protocol_version = old_protocol_version + 1;

let functions_number_limit: u32 = 10_000;
Expand Down Expand Up @@ -192,9 +184,6 @@ fn test_limit_contract_functions_number() {
new_protocol_version,
vm_kind,
);
#[cfg(not(feature = "protocol_feature_limit_contract_functions_number"))]
assert_eq!(err, None);
#[cfg(feature = "protocol_feature_limit_contract_functions_number")]
assert_matches!(
err,
Some(VMError::FunctionCallError(FunctionCallError::CompilationError(
Expand Down