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

EC consensus preparation #931

Merged
merged 7 commits into from
Nov 18, 2022
Merged
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
7 changes: 4 additions & 3 deletions Cargo.lock

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

5 changes: 5 additions & 0 deletions crates/pallet-subspace/src/default_weights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,9 @@ impl crate::WeightInfo for () {
// TODO: Proper value
Weight::from_ref_time(10_000 * (root_blocks_count as u64 + 1))
}

fn vote() -> Weight {
// TODO: Proper value
Weight::from_ref_time(10_000)
}
}
3 changes: 2 additions & 1 deletion crates/pallet-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ use subspace_verification::{
pub trait WeightInfo {
fn report_equivocation() -> Weight;
fn store_root_blocks(root_blocks_count: usize) -> Weight;
fn vote() -> Weight;
}

/// Trigger global randomness every interval.
Expand Down Expand Up @@ -492,7 +493,7 @@ mod pallet {

/// Farmer vote, currently only used for extra rewards to farmers.
// TODO: Proper weight
#[pallet::weight((100_000, DispatchClass::Operational, Pays::No))]
#[pallet::weight((<T as Config>::WeightInfo::vote(), DispatchClass::Operational, Pays::No))]
// Suppression because the custom syntax will also generate an enum and we need enum to have
// boxed value.
#[allow(clippy::boxed_local)]
Expand Down
1 change: 1 addition & 0 deletions crates/sc-consensus-subspace-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ sp-core = { version = "6.0.0", git = "https://github.com/subspace/substrate", re
sp-runtime = { version = "6.0.0", git = "https://github.com/subspace/substrate", rev = "66d3f73c5ef66c975fa5f54b6736ccd6821e395f" }
subspace-archiving = { version = "0.1.0", path = "../subspace-archiving" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-farmer-components = { version = "0.1.0", path = "../subspace-farmer-components" }
subspace-rpc-primitives = { version = "0.1.0", path = "../subspace-rpc-primitives" }
3 changes: 2 additions & 1 deletion crates/sc-consensus-subspace-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ use subspace_core_primitives::{
Piece, PieceIndex, RecordsRoot, SegmentIndex, Solution, RECORDED_HISTORY_SEGMENT_SIZE,
RECORD_SIZE,
};
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_rpc_primitives::{
FarmerProtocolInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
MAX_SEGMENT_INDEXES_PER_REQUEST,
};

Expand Down
29 changes: 29 additions & 0 deletions crates/subspace-core-primitives/src/sector_codec/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,32 @@ fn basic() {

assert_eq!(sector, decoded);
}

// TODO: Unlock this test once https://github.com/arkworks-rs/algebra/issues/516 is resolved
#[test]
#[ignore]
fn two_way_transformation_works() {
let rows_columns_count = 4_usize;
let sector_size = rows_columns_count.pow(2) * Scalar::SAFE_BYTES;

let mut scalars = {
let mut sector = Vec::with_capacity(sector_size / Scalar::SAFE_BYTES);

for _ in 0..sector.capacity() {
sector.push(Scalar::try_from(rand::random::<[u8; 31]>().as_slice()).unwrap());
}

sector
};

let codec = SectorCodec::new(sector_size).unwrap();

codec.encode(&mut scalars).unwrap();

let new_scalars = scalars
.iter()
.map(|scalar| Scalar::from(&scalar.to_bytes()))
.collect::<Vec<_>>();

assert_eq!(scalars, new_scalars);
}
3 changes: 2 additions & 1 deletion crates/subspace-farmer-components/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ async-trait = "0.1.57"
bitvec = "1.0.1"
blake2-rfc = "0.2.18"
fs2 = "0.4.3"
hex = { version = "0.4.3", features = ["serde"] }
libc = "0.2.131"
parity-scale-codec = "3.1.5"
rand = "0.8.5"
schnorrkel = "0.9.1"
serde = { version = "1.0.143", features = ["derive"] }
static_assertions = "1.1.0"
subspace-solving = { version = "0.1.0", path = "../subspace-solving" }
subspace-core-primitives = { version = "0.1.0", path = "../subspace-core-primitives" }
subspace-rpc-primitives = { version = "0.1.0", path = "../subspace-rpc-primitives" }
subspace-verification = { version = "0.1.0", path = "../subspace-verification" }
thiserror = "1.0.32"
tracing = "0.1.36"
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-farmer-components/benches/auditing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use subspace_core_primitives::{
use subspace_farmer_components::farming::audit_sector;
use subspace_farmer_components::file_ext::FileExt;
use subspace_farmer_components::plotting::plot_sector;
use subspace_rpc_primitives::FarmerProtocolInfo;
use subspace_farmer_components::FarmerProtocolInfo;
use utils::BenchPieceReceiver;

mod utils;
Expand Down
2 changes: 1 addition & 1 deletion crates/subspace-farmer-components/benches/plotting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use subspace_core_primitives::{
Piece, PublicKey, PIECES_IN_SEGMENT, PLOT_SECTOR_SIZE, RECORD_SIZE,
};
use subspace_farmer_components::plotting::plot_sector;
use subspace_rpc_primitives::FarmerProtocolInfo;
use subspace_farmer_components::FarmerProtocolInfo;
use utils::BenchPieceReceiver;

mod utils;
Expand Down
3 changes: 1 addition & 2 deletions crates/subspace-farmer-components/benches/proving.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ use subspace_core_primitives::{
use subspace_farmer_components::farming::audit_sector;
use subspace_farmer_components::file_ext::FileExt;
use subspace_farmer_components::plotting::plot_sector;
use subspace_farmer_components::SectorMetadata;
use subspace_rpc_primitives::FarmerProtocolInfo;
use subspace_farmer_components::{FarmerProtocolInfo, SectorMetadata};
use utils::BenchPieceReceiver;

mod utils;
Expand Down
3 changes: 1 addition & 2 deletions crates/subspace-farmer-components/src/farming.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::SectorMetadata;
use crate::{FarmerProtocolInfo, SectorMetadata};
use bitvec::prelude::*;
use parity_scale_codec::{Decode, IoReader};
use schnorrkel::Keypair;
Expand All @@ -10,7 +10,6 @@ use subspace_core_primitives::{
Blake2b256Hash, Chunk, Piece, PublicKey, SectorId, SectorIndex, Solution, SolutionRange,
PIECE_SIZE,
};
use subspace_rpc_primitives::FarmerProtocolInfo;
use subspace_solving::{create_chunk_signature, derive_chunk_otp};
use subspace_verification::is_within_solution_range;
use thiserror::Error;
Expand Down
22 changes: 21 additions & 1 deletion crates/subspace-farmer-components/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,34 @@ pub mod file_ext;
pub mod plotting;

use parity_scale_codec::{Decode, Encode};
use serde::{Deserialize, Serialize};
use static_assertions::const_assert;
use std::num::NonZeroU64;
use std::num::{NonZeroU16, NonZeroU32, NonZeroU64};
use subspace_core_primitives::SegmentIndex;

// Refuse to compile on non-64-bit platforms, offsets may fail on those when converting from u64 to
// usize depending on chain parameters
const_assert!(std::mem::size_of::<usize>() >= std::mem::size_of::<u64>());

/// Information about the protocol necessary for farmer operation
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FarmerProtocolInfo {
/// Genesis hash of the chain
#[serde(with = "hex::serde")]
pub genesis_hash: [u8; 32],
/// The size of data in one piece (in bytes).
pub record_size: NonZeroU32,
/// Recorded history is encoded and plotted in segments of this size (in bytes).
pub recorded_history_segment_size: u32,
/// Total number of pieces stored on the network
pub total_pieces: NonZeroU64,
/// Space parameter for proof-of-replication in bits
pub space_l: NonZeroU16,
/// Number of segments after which sector expires
pub sector_expiration: SegmentIndex,
}

/// Metadata of the plotted sector
#[doc(hidden)]
#[derive(Debug, Encode, Decode, Clone)]
Expand Down
3 changes: 1 addition & 2 deletions crates/subspace-farmer-components/src/plotting.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::SectorMetadata;
use crate::{FarmerProtocolInfo, SectorMetadata};
use async_trait::async_trait;
use bitvec::order::Lsb0;
use bitvec::prelude::*;
Expand All @@ -9,7 +9,6 @@ use std::sync::atomic::{AtomicBool, Ordering};
use subspace_core_primitives::{
Piece, PieceIndex, PublicKey, SectorId, SectorIndex, PIECE_SIZE, PLOT_SECTOR_SIZE,
};
use subspace_rpc_primitives::FarmerProtocolInfo;
use subspace_solving::derive_chunk_otp;
use thiserror::Error;
use tracing::debug;
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-farmer/src/rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use futures::Stream;
use std::pin::Pin;
use subspace_archiving::archiver::ArchivedSegment;
use subspace_core_primitives::{Piece, PieceIndex, RecordsRoot, SegmentIndex};
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_rpc_primitives::{
FarmerProtocolInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
};

/// To become error type agnostic
Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-farmer/src/rpc_client/bench_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use std::pin::Pin;
use std::sync::Arc;
use subspace_archiving::archiver::ArchivedSegment;
use subspace_core_primitives::{Piece, PieceIndex, RecordsRoot, SegmentIndex};
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_rpc_primitives::{
FarmerProtocolInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
};
use tokio::sync::Mutex;

Expand Down
3 changes: 2 additions & 1 deletion crates/subspace-farmer/src/rpc_client/node_rpc_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use std::pin::Pin;
use std::sync::Arc;
use subspace_archiving::archiver::ArchivedSegment;
use subspace_core_primitives::{Piece, PieceIndex, RecordsRoot, SegmentIndex};
use subspace_farmer_components::FarmerProtocolInfo;
use subspace_rpc_primitives::{
FarmerProtocolInfo, RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
RewardSignatureResponse, RewardSigningInfo, SlotInfo, SolutionResponse,
};

// Defines max_concurrent_requests constant in the node rpc client.
Expand Down
7 changes: 5 additions & 2 deletions crates/subspace-node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ clap = { version = "4.0.22", features = ["derive"] }
core-payments-domain-runtime = { version = "0.1.0", path = "../../domains/runtime/core-payments" }
dirs = "4.0.0"
domain-service = { version = "0.1.0", path = "../../domains/service" }
frame-benchmarking-cli = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "66d3f73c5ef66c975fa5f54b6736ccd6821e395f", default-features = false, features = ["runtime-benchmarks"] }
frame-benchmarking = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "66d3f73c5ef66c975fa5f54b6736ccd6821e395f", default-features = false }
frame-benchmarking-cli = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "66d3f73c5ef66c975fa5f54b6736ccd6821e395f", default-features = false }
frame-support = { version = "4.0.0-dev", git = "https://github.com/subspace/substrate", rev = "66d3f73c5ef66c975fa5f54b6736ccd6821e395f" }
futures = "0.3.21"
log = "0.4.17"
Expand Down Expand Up @@ -54,7 +55,7 @@ subspace-runtime-primitives = { version = "0.1.0", path = "../subspace-runtime-p
subspace-service = { version = "0.1.0", path = "../subspace-service" }
system-domain-runtime = { version = "0.1.0", path = "../../domains/runtime/system" }
thiserror = "1.0.32"
tokio = { version = "1.20.1" }
tokio = "1.20.1"

[build-dependencies]
substrate-build-script-utils = { version = "3.0.0", git = "https://github.com/subspace/substrate", rev = "66d3f73c5ef66c975fa5f54b6736ccd6821e395f" }
Expand All @@ -65,6 +66,8 @@ do-not-enforce-cost-of-storage = [
"subspace-runtime/do-not-enforce-cost-of-storage"
]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-benchmarking-cli/runtime-benchmarks",
"domain-service/runtime-benchmarks",
"subspace-runtime/runtime-benchmarks",
"system-domain-runtime/runtime-benchmarks",
Expand Down
22 changes: 1 addition & 21 deletions crates/subspace-rpc-primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,13 @@
//! Primitives for Subspace RPC.

use serde::{Deserialize, Serialize};
use std::num::{NonZeroU16, NonZeroU32, NonZeroU64};
use subspace_core_primitives::{
Blake2b256Hash, PublicKey, RewardSignature, SegmentIndex, SlotNumber, Solution, SolutionRange,
Blake2b256Hash, PublicKey, RewardSignature, SlotNumber, Solution, SolutionRange,
};

/// Defines a limit for segment indexes array. It affects storage access on the runtime side.
pub const MAX_SEGMENT_INDEXES_PER_REQUEST: usize = 300;

/// Information about the protocol necessary for farmer operation
#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FarmerProtocolInfo {
/// Genesis hash of the chain
#[serde(with = "hex::serde")]
pub genesis_hash: [u8; 32],
/// The size of data in one piece (in bytes).
pub record_size: NonZeroU32,
/// Recorded history is encoded and plotted in segments of this size (in bytes).
pub recorded_history_segment_size: u32,
/// Total number of pieces stored on the network
pub total_pieces: NonZeroU64,
/// Space parameter for proof-of-replication in bits
pub space_l: NonZeroU16,
/// Number of segments after which sector expires
pub sector_expiration: SegmentIndex,
}

/// Information about new slot that just arrived
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
Expand Down
Loading