Skip to content
This repository has been archived by the owner on Feb 21, 2024. It is now read-only.

Commit

Permalink
Merge branch 'main' into restart-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
nazar-pc authored Nov 17, 2021
2 parents 9566a2d + 4c598db commit 212f854
Show file tree
Hide file tree
Showing 19 changed files with 483 additions and 74 deletions.
19 changes: 19 additions & 0 deletions Cargo.lock

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

4 changes: 1 addition & 3 deletions crates/pallet-feeds/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

use core::mem;
pub use pallet::*;
use sp_std::vec::Vec;
use subspace_core_primitives::{crypto, Sha256Hash};

#[cfg(all(feature = "std", test))]
Expand All @@ -31,11 +30,10 @@ mod tests;

#[frame_support::pallet]
mod pallet {
use super::*;
use frame_support::dispatch::DispatchResult;
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use scale_info::TypeInfo;
use sp_std::prelude::*;

#[pallet::config]
pub trait Config: frame_system::Config {
Expand Down
3 changes: 2 additions & 1 deletion crates/pallet-feeds/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{mock::*, Error, FeedId, ObjectMetadata, PutDataObject, TotalObjectsAndSize};
use crate::mock::{new_test_ext, Event, Feeds, Origin, System, Test};
use crate::{Error, FeedId, ObjectMetadata, PutDataObject, TotalObjectsAndSize};
use frame_support::{assert_noop, assert_ok};

const FEED_ID: FeedId = 0;
Expand Down
43 changes: 43 additions & 0 deletions crates/pallet-object-store/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
[package]
name = "pallet-object-store"
version = "0.1.0"
authors = ["Nazar Mokrynskyi <[email protected]>"]
edition = "2021"
license = "Apache-2.0"
homepage = "https://subspace.network"
repository = "https://github.com/subspace/subspace"
description = "Subspace node pallet for simple objects storage"
readme = "README.md"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.3.0", default-features = false, features = ["derive"] }
frame-support = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
frame-system = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
log = { version = "0.4.14", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
sp-std = { version = "4.0.0-dev", default-features = false, git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
subspace-core-primitives = { version = "0.1.0", default-features = false, path = "../subspace-core-primitives" }

[dev-dependencies]
serde = "1.0.127"
sp-core = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
sp-io = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }
sp-runtime = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate", rev = "26d69bcbe26f6b463e9374e1b1c54c3067fb6131" }

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"hex/std",
"log/std",
"scale-info/std",
"sp-std/std",
"subspace-core-primitives/std",
]
try-runtime = ["frame-support/try-runtime"]
5 changes: 5 additions & 0 deletions crates/pallet-object-store/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Pallet Object Store

Subspace node pallet for simple objects storage

License: Apache-2.0
107 changes: 107 additions & 0 deletions crates/pallet-object-store/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright (C) 2021 Subspace Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! Pallet object store, used for simple object storage on the network.

#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![warn(rust_2018_idioms, missing_debug_implementations)]

#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
mod tests;

pub use pallet::*;
use subspace_core_primitives::{crypto, Sha256Hash};

#[frame_support::pallet]
mod pallet {
use frame_support::pallet_prelude::*;
use frame_system::pallet_prelude::*;
use log::debug;
use sp_std::prelude::*;
use subspace_core_primitives::{crypto, Sha256Hash};

#[pallet::config]
pub trait Config: frame_system::Config {
/// `pallet-object-store` events
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
}

/// Pallet object-store, used for storing arbitrary user-provided data combined into object-store.
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

/// `pallet-object-store` events
#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// New object is added \[who, object_id, object_size\]
DataSubmitted(T::AccountId, Sha256Hash, u32),
}

#[pallet::call]
impl<T: Config> Pallet<T> {
// TODO: add proper weights
// TODO: For now we don't have fees, but we will have them in the future
/// Put a new object into a feed
#[pallet::weight((10_000, Pays::No))]
pub fn put(origin: OriginFor<T>, data: Vec<u8>) -> DispatchResult {
let who = ensure_signed(origin)?;

let object_size = data.len() as u32;

let object_id = crypto::sha256_hash(&data);

debug!(
target: "runtime:object-store",
"New object {}, size {} bytes",
hex::encode(&object_id),
object_size
);

Self::deposit_event(Event::DataSubmitted(who, object_id, object_size));

Ok(())
}
}
}

/// Mapping to the object offset and size within an extrinsic
#[derive(Debug)]
pub struct CallObjectLocation {
/// Object hash
pub hash: Sha256Hash,
/// Offset
pub offset: u32,
}

impl<T: Config> Call<T> {
/// Extract object location if an extrinsic corresponds to `put` call
pub fn extract_object_location(&self) -> Option<CallObjectLocation> {
match self {
Self::put { data } => {
// `1` corresponds to `Call::put {}` enum variant encoding.
Some(CallObjectLocation {
hash: crypto::sha256_hash(data),
offset: 1,
})
}
_ => None,
}
}
}
71 changes: 71 additions & 0 deletions crates/pallet-object-store/src/mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
use frame_support::parameter_types;
use sp_core::H256;
use sp_runtime::{
testing::Header,
traits::{BlakeTwo256, IdentityLookup},
};

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

frame_support::construct_runtime!(
pub enum Test where
Block = Block,
NodeBlock = Block,
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
ObjectStore: crate::{Pallet, Call, Event<T>}
}
);

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const SS58Prefix: u8 = 42;
}

impl frame_system::Config for Test {
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
type Origin = Origin;
type Call = Call;
type Index = u64;
type BlockNumber = u64;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type Event = Event;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
type OnSetCode = ();
}

parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}

impl crate::Config for Test {
type Event = Event;
}

pub fn new_test_ext() -> sp_io::TestExternalities {
let t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap();

let mut t: sp_io::TestExternalities = t.into();

t.execute_with(|| System::set_block_number(1));

t
}
22 changes: 22 additions & 0 deletions crates/pallet-object-store/src/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::mock::{new_test_ext, Event, ObjectStore, Origin, System, Test};
use frame_support::assert_ok;
use subspace_core_primitives::crypto;

const ACCOUNT_ID: u64 = 100;

#[test]
fn can_do_put() {
new_test_ext().execute_with(|| {
let object = vec![1, 2, 3, 4, 5];
let object_id = crypto::sha256_hash(&object);
let object_size = object.len() as u32;

assert_ok!(ObjectStore::put(Origin::signed(ACCOUNT_ID), object));

System::assert_last_event(Event::ObjectStore(crate::Event::<Test>::DataSubmitted(
ACCOUNT_ID,
object_id,
object_size,
)));
});
}
12 changes: 7 additions & 5 deletions crates/pallet-offences-subspace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,27 @@ mod mock;
mod tests;

use codec::{Decode, Encode};
pub use pallet::*;
use sp_consensus_subspace::{
offence::{Kind, Offence, OffenceDetails, OffenceError, OnOffenceHandler, ReportOffence},
offence::{Offence, OffenceDetails, OffenceError, OnOffenceHandler, ReportOffence},
FarmerPublicKey,
};
use sp_runtime::traits::Hash;
use sp_std::prelude::*;

pub use pallet::*;

/// A binary blob which represents a SCALE codec-encoded `O::TimeSlot`.
type OpaqueTimeSlot = Vec<u8>;

/// A type alias for a report identifier.
type ReportIdOf<T> = <T as frame_system::Config>::Hash;

#[frame_support::pallet]
pub mod pallet {
use super::*;
mod pallet {
use super::{OpaqueTimeSlot, ReportIdOf};
use frame_support::pallet_prelude::*;
use sp_consensus_subspace::offence::{Kind, OffenceDetails, OnOffenceHandler};
use sp_consensus_subspace::FarmerPublicKey;
use sp_std::prelude::*;

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
Expand Down
4 changes: 3 additions & 1 deletion crates/pallet-offences-subspace/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@

#![cfg(test)]

use super::*;
use crate::mock::{
new_test_ext, offence_reports, report_id, with_on_offence_fractions, Event, Offence,
OffencesSubspace, System, KIND,
};
use codec::{Decode, Encode};
use frame_system::{EventRecord, Phase};
use schnorrkel::Keypair;
use sp_consensus_subspace::offence::{OffenceDetails, OffenceError, ReportOffence};
use sp_consensus_subspace::FarmerPublicKey;
use sp_core::Public;
use sp_runtime::Perbill;

Expand Down
9 changes: 5 additions & 4 deletions crates/pallet-rewards/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@ pub use pallet::*;
use sp_consensus_subspace::digests::PreDigest;
use sp_consensus_subspace::SUBSPACE_ENGINE_ID;

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

pub trait WeightInfo {
fn on_initialize() -> Weight;
}

#[frame_support::pallet]
mod pallet {
use super::*;
use super::WeightInfo;
use frame_support::pallet_prelude::*;
use frame_support::traits::Currency;
use frame_system::pallet_prelude::*;

type BalanceOf<T> =
<<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

#[pallet::config]
pub trait Config: frame_system::Config {
/// `pallet-rewards` events
Expand Down
Loading

0 comments on commit 212f854

Please sign in to comment.