Skip to content

Commit

Permalink
attempt number 3
Browse files Browse the repository at this point in the history
  • Loading branch information
Longarithm committed Jul 9, 2024
1 parent ccd6f57 commit 8bb952f
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 153 deletions.
11 changes: 10 additions & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion core/chain-configs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ nightly = [
test_genesis = ["near-primitives/rand"]
test_utils = ["near-primitives/rand"]
default = []
metrics = ["near-o11y"]
metrics = ["near-o11y", "near-time/clock"]
12 changes: 6 additions & 6 deletions core/primitives-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ borsh.workspace = true
bs58.workspace = true
derive_more.workspace = true
enum-map.workspace = true
inventory = { workspace = true, optional = true }
inventory = { workspace = true, optional = false }
num-rational.workspace = true
serde.workspace = true
serde_repr.workspace = true
Expand All @@ -43,11 +43,11 @@ protocol_feature_nonrefundable_transfer_nep491 = []
#check_protocol_structs = ["inventory", "near-structs-checker/check_protocol_structs"]

nightly = [
"nightly_protocol",
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_fix_staking_threshold",
"protocol_feature_nonrefundable_transfer_nep491",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
"nightly_protocol",
"protocol_feature_fix_contract_loading_cost",
"protocol_feature_fix_staking_threshold",
"protocol_feature_nonrefundable_transfer_nep491",
"protocol_feature_reject_blocks_with_outdated_protocol_version",
]

nightly_protocol = [
Expand Down
5 changes: 4 additions & 1 deletion core/primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ derive_more.workspace = true
easy-ext.workspace = true
hex.workspace = true
itertools = { workspace = true, optional = true }
inventory = { workspace = true, optional = false }
num-rational.workspace = true
once_cell.workspace = true
primitive-types.workspace = true
Expand All @@ -47,6 +48,8 @@ near-fmt.workspace = true
near-primitives-core.workspace = true
near-rpc-error-macro.workspace = true
near-parameters.workspace = true
near-structs-checker.workspace = true
near-structs-checker-core.workspace = true

[features]
sandbox = []
Expand Down Expand Up @@ -90,7 +93,7 @@ nightly_protocol = [
statelessnet_protocol = ["near-primitives-core/statelessnet_protocol"]

new_epoch_sync = []

default = ["near-primitives/rand"]

calimero_zero_storage = []

Expand Down
45 changes: 25 additions & 20 deletions core/primitives/src/epoch_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -617,6 +617,25 @@ impl BlockInfoV1 {
)]
pub struct ValidatorWeight(ValidatorId, u64);

use near_structs_checker::ProtocolStruct;

/// Information per epoch.
#[derive(
BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq, Eq, serde::Serialize, ProtocolStruct,
)]
pub enum EpochInfo {
V1(EpochInfoV1),
V2(epoch_info::EpochInfoV2),
V3(epoch_info::EpochInfoV3),
V4(epoch_info::EpochInfoV4),
}

impl Default for EpochInfo {
fn default() -> Self {
Self::V2(epoch_info::EpochInfoV2::default())
}
}

pub mod epoch_info {
use crate::epoch_manager::ValidatorWeight;
use crate::types::validator_stake::{ValidatorStake, ValidatorStakeIter};
Expand All @@ -632,30 +651,15 @@ pub mod epoch_info {
use smart_default::SmartDefault;
use std::collections::{BTreeMap, HashMap};

pub use super::EpochInfoV1;
use crate::types::validator_stake::ValidatorStakeV1;
use crate::{epoch_manager::RngSeed, rand::WeightedIndex};
use near_primitives_core::{
checked_feature,
hash::hash,
types::{BlockHeight, ShardId},
};

pub use super::EpochInfoV1;

/// Information per epoch.
#[derive(BorshSerialize, BorshDeserialize, Clone, Debug, PartialEq, Eq, serde::Serialize)]
pub enum EpochInfo {
V1(EpochInfoV1),
V2(EpochInfoV2),
V3(EpochInfoV3),
V4(EpochInfoV4),
}

impl Default for EpochInfo {
fn default() -> Self {
Self::V2(EpochInfoV2::default())
}
}
use near_structs_checker::ProtocolStruct;

// V1 -> V2: Use versioned ValidatorStake structure in validators and fishermen
#[derive(
Expand Down Expand Up @@ -745,6 +749,7 @@ pub mod epoch_info {
PartialEq,
Eq,
serde::Serialize,
ProtocolStruct,
)]
pub struct EpochInfoV4 {
pub epoch_height: EpochHeight,
Expand Down Expand Up @@ -773,7 +778,7 @@ pub mod epoch_info {
validator_mandates: ValidatorMandates,
}

impl EpochInfo {
impl super::EpochInfo {
pub fn new(
epoch_height: EpochHeight,
validators: Vec<ValidatorStake>,
Expand Down Expand Up @@ -1268,7 +1273,7 @@ pub mod epoch_info {
}

#[cfg(feature = "rand")]
impl EpochInfo {
impl super::EpochInfo {
/// Returns a new RNG obtained from combining the provided `seed` and `height`.
///
/// The returned RNG can be used to shuffle slices via [`rand::seq::SliceRandom`].
Expand Down Expand Up @@ -1366,7 +1371,7 @@ pub enum SlashState {
pub mod epoch_sync {
use crate::block_header::BlockHeader;
use crate::epoch_manager::block_info::BlockInfo;
use crate::epoch_manager::epoch_info::EpochInfo;
use crate::epoch_manager::EpochInfo;
use crate::errors::epoch_sync::{EpochSyncHashType, EpochSyncInfoError};
use crate::types::EpochId;
use borsh::{BorshDeserialize, BorshSerialize};
Expand Down
2 changes: 1 addition & 1 deletion core/primitives/src/epoch_sync.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::block_header::BlockHeader;
use crate::epoch_manager::block_info::BlockInfo;
use crate::epoch_manager::epoch_info::EpochInfo;
use crate::epoch_manager::EpochInfo;
use crate::merkle::PartialMerkleTree;
use crate::views::LightClientBlockView;
use borsh::{BorshDeserialize, BorshSerialize};
Expand Down
2 changes: 2 additions & 0 deletions core/primitives/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::sync::Arc;
mod chunk_validator_stats;

pub use chunk_validator_stats::ChunkStats;
use near_structs_checker::ProtocolStruct;

/// Hash used by to store state root.
pub type StateRoot = CryptoHash;
Expand Down Expand Up @@ -1058,6 +1059,7 @@ pub enum ValidatorInfoIdentifier {
Debug,
PartialEq,
Eq,
ProtocolStruct,
)]
pub enum ValidatorKickoutReason {
/// Slashed validators are kicked out.
Expand Down
2 changes: 2 additions & 0 deletions core/structs-checker-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ license.workspace = true

[dependencies]
serde = { workspace = true, optional = false }
serde_json = { workspace = true, features = ["preserve_order"], optional = false }
once_cell = { workspace = true, optional = false }
inventory = { workspace = true, optional = false }
82 changes: 21 additions & 61 deletions core/structs-checker-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,68 +1,28 @@
use serde::{Deserialize, Serialize};
use std::any::TypeId;
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

use once_cell::sync::Lazy;
use std::sync::Mutex;

#[derive(Clone, Serialize, Deserialize)]
pub struct FieldInfo {
pub name: String,
pub type_name: String,
pub hash: u64,
}

#[derive(Clone, Serialize, Deserialize)]
pub struct ProtocolStructInfo {
pub name: String,
pub hash: u64,
pub fields: Vec<FieldInfo>,
}

pub trait ProtocolStruct {
fn type_name() -> String {
std::any::type_name::<Self>().to_string()
}

fn calculate_hash() -> u64 {
let mut hasher = DefaultHasher::new();
Self::type_name().hash(&mut hasher);
hasher.finish()
}

fn get_info() -> ProtocolStructInfo {
ProtocolStructInfo { name: Self::type_name(), hash: Self::calculate_hash(), fields: vec![] }
}
use serde::{Deserialize, Serialize};
use std::sync::Arc;

#[derive(Copy, Clone, Serialize)]
pub enum ProtocolStructInfo {
Struct {
name: &'static str,
fields: &'static [(&'static str, &'static str)],
},
Enum {
name: &'static str,
variants: &'static [(&'static str, Option<&'static [(&'static str, &'static str)]>)],
},
}

// Implement ProtocolStruct for primitive types
macro_rules! impl_protocol_struct_for_primitive {
($($t:ty),*) => {
$(
impl ProtocolStruct for $t {}
)*
impl ProtocolStructInfo {
pub fn name(&self) -> &'static str {
match self {
ProtocolStructInfo::Struct { name, .. } => name,
ProtocolStructInfo::Enum { name, .. } => name,
}
}
}

impl_protocol_struct_for_primitive!(u8, u16, u32, u64, u128, i8, i16, i32, i64, i128, bool, String);

// Implement for arrays
impl<T: ProtocolStruct, const N: usize> ProtocolStruct for [T; N] {}

pub fn calculate_struct_hash<T: 'static>() -> u64 {
let mut hasher = DefaultHasher::new();
TypeId::of::<T>().hash(&mut hasher);
hasher.finish()
}

static PROTOCOL_STRUCTS: Lazy<Mutex<Vec<ProtocolStructInfo>>> =
Lazy::new(|| Mutex::new(Vec::new()));
inventory::collect!(ProtocolStructInfo);

pub fn register_protocol_struct(info: ProtocolStructInfo) {
PROTOCOL_STRUCTS.lock().unwrap().push(info);
}

pub fn collect_protocol_structs() -> Vec<ProtocolStructInfo> {
PROTOCOL_STRUCTS.lock().unwrap().clone()
}
pub trait ProtocolStruct {}
2 changes: 2 additions & 0 deletions core/structs-checker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ quote = { workspace = true, optional = false }
syn = { workspace = true, optional = false }
serde_json = { workspace = true, features = ["preserve_order"], optional = false }
serde = { workspace = true, optional = false }
inventory = { workspace = true, optional = false }
proc-macro2 = "1.0.86"

[features]
#check_protocol_structs = ["inventory", "quote"]
Loading

0 comments on commit 8bb952f

Please sign in to comment.