Skip to content

Commit

Permalink
fix: make near-primitives compile (#12608)
Browse files Browse the repository at this point in the history
`cargo build -pnear-primitives` is [failing and
blocks](https://github.com/near/nearcore/actions/runs/12265615118/job/34222072993)
2.4.0 crate release.

`itertools` and `rand` were marked as optional in
#11597.
But recent changes made these libraries required, causing compilation
errors.

This PR:
* Uses a [patch](#11981) that was
included in previous release but not in master
* Marks `itertools` as non-optional, as we now use it not only in tests
/ optional features.
* Gates some test-only code that uses `rand` by `test_utils` feature.
* Increase dependency limit for `near-client-primitives` to `152`.
Though I am not really adding new dependencies, just making it compiles,
so that I can release crates for 2.4.0.
* make `single_shard()` does not use rand, as it is used in production
code elsewhere.
  • Loading branch information
staffik authored Dec 12, 2024
1 parent 544fa1f commit 4ee8111
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 12 deletions.
2 changes: 1 addition & 1 deletion core/chain-configs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ nightly = [
"nightly_protocol",
"protocol_feature_nonrefundable_transfer_nep491",
]
test_genesis = ["near-primitives/rand"]
test_genesis = ["near-primitives/rand", "near-primitives/test_utils"]
test_utils = ["near-primitives/rand"]
default = []
metrics = ["near-o11y", "near-time/clock"]
9 changes: 5 additions & 4 deletions core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ chrono.workspace = true
derive_more = { workspace = true, features = ["as_ref", "from", "into", "deref" ] }
easy-ext.workspace = true
hex.workspace = true
itertools = { workspace = true, optional = true }
itertools.workspace = true
num-rational.workspace = true
ordered-float.workspace = true
primitive-types.workspace = true
Expand Down Expand Up @@ -52,8 +52,9 @@ near-schema-checker-lib.workspace = true
[features]
sandbox = []
test_features = []
solomon = ["reed-solomon-erasure", "itertools"]
rand = ["dep:rand", "rand_chacha", "near-crypto/rand", "itertools"]
test_utils = ["rand"]
solomon = ["reed-solomon-erasure"]
rand = ["dep:rand", "rand_chacha", "near-crypto/rand"]
clock = ["near-time/clock", "near-time/serde"]
protocol_feature_fix_contract_loading_cost = [
"near-primitives-core/protocol_feature_fix_contract_loading_cost",
Expand Down Expand Up @@ -91,7 +92,7 @@ protocol_schema = [

[dev-dependencies]
chrono = { workspace = true, features = ["clock"] }
near-primitives = { workspace = true, features = ["clock", "solomon", "rand"] }
near-primitives = { path = ".", features = ["clock", "solomon", "rand", "test_utils"] }
assert_matches.workspace = true
bencher.workspace = true
bolero.workspace = true
Expand Down
1 change: 1 addition & 0 deletions core/primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ pub mod state_record;
pub mod state_sync;
pub mod stateless_validation;
pub mod telemetry;
#[cfg(feature = "test_utils")]
pub mod test_utils;
pub mod transaction;
pub mod trie_key;
Expand Down
21 changes: 17 additions & 4 deletions core/primitives/src/shard_layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@ use borsh::{BorshDeserialize, BorshSerialize};
use itertools::Itertools;
use near_primitives_core::types::{ShardId, ShardIndex};
use near_schema_checker_lib::ProtocolSchema;
use rand::rngs::StdRng;
use rand::seq::SliceRandom;
use rand::SeedableRng;
#[cfg(feature = "test_utils")]
use rand::{rngs::StdRng, seq::SliceRandom, SeedableRng};
use std::collections::{BTreeMap, BTreeSet};
use std::{fmt, str};

Expand Down Expand Up @@ -344,13 +343,23 @@ impl std::error::Error for ShardLayoutError {}
impl ShardLayout {
/// Handy constructor for a single-shard layout, mostly for test purposes
pub fn single_shard() -> Self {
Self::multi_shard(1, 0)
let shard_id = ShardId::new(0);
Self::V2(ShardLayoutV2 {
boundary_accounts: vec![],
shard_ids: vec![shard_id],
id_to_index_map: [(shard_id, 0)].into(),
index_to_id_map: [(0, shard_id)].into(),
shards_split_map: None,
shards_parent_map: None,
version: 0,
})
}

/// Creates a multi-shard ShardLayout using the most recent ShardLayout
/// version and default boundary accounts. It should be used for tests only.
/// The shard ids are deterministic but arbitrary in order to test the
/// non-contiguous ShardIds.
#[cfg(feature = "test_utils")]
pub fn multi_shard(num_shards: NumShards, version: ShardVersion) -> Self {
assert!(num_shards > 0, "at least 1 shard is required");

Expand All @@ -365,6 +374,7 @@ impl ShardLayout {
/// version and provided boundary accounts. It should be used for tests
/// only. The shard ids are deterministic but arbitrary in order to test the
/// non-contiguous ShardIds.
#[cfg(feature = "test_utils")]
pub fn multi_shard_custom(boundary_accounts: Vec<AccountId>, version: ShardVersion) -> Self {
let num_shards = (boundary_accounts.len() + 1) as u64;

Expand Down Expand Up @@ -393,6 +403,7 @@ impl ShardLayout {

/// Test-only helper to create a simple multi-shard ShardLayout with the provided boundaries.
/// The shard ids are deterministic but arbitrary in order to test the non-contiguous ShardIds.
#[cfg(feature = "test_utils")]
pub fn simple_v1(boundary_accounts: &[&str]) -> ShardLayout {
// TODO these test methods should go into a different namespace
let boundary_accounts = boundary_accounts.iter().map(|a| a.parse().unwrap()).collect();
Expand Down Expand Up @@ -536,6 +547,7 @@ impl ShardLayout {
/// TODO(resharding) Determine the shard layout for v4.
/// This layout is provisional, the actual shard layout should be determined
/// based on the fresh data before the resharding.
#[cfg(test)]
pub fn get_simple_nightshade_layout_v4() -> ShardLayout {
let v3 = Self::get_simple_nightshade_layout_v3();
ShardLayout::derive_shard_layout(&v3, "game.hot.tg-0".parse().unwrap())
Expand Down Expand Up @@ -881,6 +893,7 @@ impl ShardUId {

/// Returns the only shard uid in the ShardLayout::single_shard layout.
/// It is not suitable for use with any other shard layout.
#[cfg(feature = "test_utils")]
pub fn single_shard() -> Self {
ShardLayout::single_shard().shard_uids().next().unwrap()
}
Expand Down
4 changes: 2 additions & 2 deletions integration-tests/src/tests/dependencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const LIBS_THRESHOLDS: [(&str, usize); 9] = [
("near-jsonrpc-primitives", 130),
("near-chain-configs", 130),
("near-chain-primitives", 130),
("near-client-primitives", 150),
("near-client-primitives", 152),
("near-parameters", 65),
("near-crypto", 75),
("near-primitives-core", 60),
Expand Down Expand Up @@ -71,7 +71,7 @@ fn get_and_assert_crate_dependencies(name: &str, threshold: usize) -> usize {

assert!(
crate_count < threshold,
"Dependencies number is too high for {name}: {} > {}",
"Dependencies number is too high for {name}: {} >= {}",
crate_count,
threshold
);
Expand Down
2 changes: 1 addition & 1 deletion runtime/runtime-params-estimator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ near-store.workspace = true
near-test-contracts.workspace = true
near-vm-runner = { workspace = true, features = [ "near_vm", "prepare" ] }
nearcore.workspace = true
node-runtime.workspace = true
node-runtime = { workspace = true, features = ["estimator"] }

[dev-dependencies]
insta.workspace = true
Expand Down
1 change: 1 addition & 0 deletions runtime/runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ near-wallet-contract.workspace = true

[features]
default = []
estimator = ["near-primitives/test_utils"]
nightly = [
"near-chain-configs/nightly",
"near-o11y/nightly",
Expand Down
1 change: 1 addition & 0 deletions runtime/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2682,6 +2682,7 @@ fn schedule_contract_preparation<'b, R: MaybeRefReceipt>(
Some(scheduled_receipt_offset.saturating_add(1))
}

#[cfg(feature = "estimator")]
/// Interface provided for gas cost estimations.
pub mod estimator {
use super::{ReceiptSink, Runtime};
Expand Down

0 comments on commit 4ee8111

Please sign in to comment.