This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
forked from paritytech/substrate
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into restart-fixes
- Loading branch information
Showing
19 changed files
with
483 additions
and
74 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
))); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.