Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Reworked for the new inherents API
Browse files Browse the repository at this point in the history
  • Loading branch information
arkpar committed May 7, 2021
1 parent 8480ffd commit f8d89f2
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 36 deletions.
1 change: 0 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 bin/node-template/node/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ pub fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceError>
select_chain,
block_import,
proposer_factory,
create_inherent_data_providers: move |_, _| async move {
create_inherent_data_providers: move |_, ()| async move {
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();

let slot =
Expand Down
1 change: 0 additions & 1 deletion bin/node/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ sp-keystore = { version = "0.9.0", path = "../../../primitives/keystore" }
sp-io = { version = "3.0.0", path = "../../../primitives/io" }
sp-consensus = { version = "0.9.0", path = "../../../primitives/consensus/common" }
sp-transaction-pool = { version = "3.0.0", path = "../../../primitives/transaction-pool" }
sp-transaction-storage-proof = { version = "3.0.0", path = "../../../primitives/transaction-storage-proof" }

# client dependencies
sc-client-api = { version = "3.0.0", path = "../../../client/api" }
Expand Down
12 changes: 3 additions & 9 deletions bin/node/cli/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ pub fn new_full_base(
env: proposer,
block_import,
sync_oracle: network.clone(),
create_inherent_data_providers: move |parent, number, ()| {
create_inherent_data_providers: move |parent, ()| {
let client_clone = client_clone.clone();
async move {
let uncles = sc_consensus_uncles::create_uncles_inherent_data_provider(
Expand All @@ -326,13 +326,7 @@ pub fn new_full_base(
slot_duration,
);

let transaction_proof = sp_transaction_storage_proof::registration::new_data_provider(
&*client_clone,
parent,
number
);

Ok((timestamp, slot, uncles, transaction_proof))
Ok((timestamp, slot, uncles))
}
},
force_authoring,
Expand Down Expand Up @@ -505,7 +499,7 @@ pub fn new_light_base(
Some(Box::new(justification_import)),
client.clone(),
select_chain.clone(),
move |_, _| async move {
move |_, ()| async move {
let timestamp = sp_timestamp::InherentDataProvider::from_system_time();

let slot =
Expand Down
12 changes: 10 additions & 2 deletions client/service/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2053,8 +2053,16 @@ where
fn block_indexed_body(
&self,
number: NumberFor<B>,
) ->Result<Option<Vec<Vec<u8>>>, sp_inherents::Error> {
) ->Result<Option<Vec<Vec<u8>>>, sp_transaction_storage_proof::Error> {
self.backend.blockchain().block_indexed_body(BlockId::number(number))
.map_err(|e| sp_inherents::Error::Application(Box::new(e)))
.map_err(|e| sp_transaction_storage_proof::Error::Application(Box::new(e)))
}

fn number(
&self,
hash: B::Hash,
) -> Result<Option<NumberFor<B>>, sp_transaction_storage_proof::Error> {
self.backend.blockchain().number(hash)
.map_err(|e| sp_transaction_storage_proof::Error::Application(Box::new(e)))
}
}
3 changes: 3 additions & 0 deletions frame/transaction-storage/README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# Transaction Storage Pallet

Indexes transactions and manages storage proofs.

License: Apache-2.0
2 changes: 1 addition & 1 deletion frame/transaction-storage/src/benchmarking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Benchmarks for transction-storage Pallet
//! Benchmarks for transaction-storage Pallet
#![cfg(feature = "runtime-benchmarks")]

Expand Down
4 changes: 2 additions & 2 deletions frame/transaction-storage/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub mod pallet {

#[pallet::call]
impl<T: Config> Pallet<T> {
/// Index and store data on chain. Minimum data size is 1 bytes, maxmum is `MAX_DATA_SIZE`.
/// Index and store data on chain. Minimum data size is 1 bytes, maximum is `MAX_DATA_SIZE`.
/// Data will be removed after `STORAGE_PERIOD` blocks, unless `renew` is called.
/// # <weight>
/// - n*log(n) of data size, as all data is pushed to an in-memory trie.
Expand Down Expand Up @@ -173,7 +173,7 @@ pub mod pallet {
/// Transaction index is emitted in the `Stored` or `Renewed` event.
/// Applies same fees as `store`.
/// # <weight>
/// - Constant with a single DB read.
/// - Constant.
/// # </weight>
#[pallet::weight(T::WeightInfo::renew())]
pub(super) fn renew(
Expand Down
2 changes: 1 addition & 1 deletion frame/transaction-storage/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

//! Tests for transction-stroage pallet.
//! Tests for transction-storage pallet.
use super::*;
use crate::mock::*;
Expand Down
2 changes: 1 addition & 1 deletion primitives/inherents/src/client_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub trait CreateInherentDataProviders<Block: BlockT, ExtraArgs>: Send + Sync {
/// Create the inherent data providers at the given `parent` block using the given `extra_args`.
async fn create_inherent_data_providers(
&self,
parent: Block::Header,
parent: Block::Hash,
extra_args: ExtraArgs,
) -> Result<Self::InherentDataProviders, Box<dyn std::error::Error + Send + Sync>>;
}
Expand Down
38 changes: 21 additions & 17 deletions primitives/transaction-storage-proof/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
use sp_std::{result::Result, prelude::*};

use codec::{Encode, Decode};
use sp_inherents::{Error, InherentIdentifier, InherentData, IsFatalError};
use sp_inherents::{InherentIdentifier, InherentData, IsFatalError};
use sp_runtime::{traits::{Block as BlockT, NumberFor}};

pub use sp_inherents::Error;

/// The identifier for the proof inherent.
pub const INHERENT_IDENTIFIER: InherentIdentifier = *b"tx_proof";
/// Storage period for data.
Expand Down Expand Up @@ -124,13 +126,17 @@ pub trait IndexedBody<B: BlockT> {
fn block_indexed_body(
&self,
number: NumberFor<B>,
) -> Result<Option<Vec<Vec<u8>>>, sp_inherents::Error>;
) -> Result<Option<Vec<Vec<u8>>>, Error>;

fn number(
&self,
hash: B::Hash,
) -> Result<Option<NumberFor<B>>, Error>;
}

#[cfg(feature = "std")]
pub mod registration {
use log::warn;
use sp_runtime::{traits::{Block as BlockT, Saturating, Zero, NumberFor}};
use sp_runtime::{traits::{Block as BlockT, Saturating, Zero, One}};
use sp_trie::TrieMut;
use super::*;

Expand All @@ -140,38 +146,36 @@ pub mod registration {
/// Register uncles inherent data provider, if not registered already.
pub fn new_data_provider<B, C>(
client: &C,
head_number: NumberFor<B>,
parent: &B::Hash,
) -> Result<InherentDataProvider, sp_inherents::Error>
) -> Result<InherentDataProvider, Error>
where
B: BlockT,
C: IndexedBody<B>,
{
let number = head_number.saturating_sub(DEFAULT_STORAGE_PERIOD.into());
let parent_number = client.number(parent.clone())?.unwrap_or(Zero::zero());
let number = parent_number
.saturating_add(One::one())
.saturating_sub(DEFAULT_STORAGE_PERIOD.into());
if number.is_zero() {
// Too early to collect proofs.
return Ok(InherentDataProvider::new(None));
}

let proof = match client.block_indexed_body(number) {
Ok(Some(transactions)) => {
let proof = match client.block_indexed_body(number)? {
Some(transactions) => {
Some(build_proof(parent.as_ref(), transactions)?)
},
Ok(None) => {
None => {
// Nothing was indexed in that block.
None
}
Err(e) => {
warn!(target: "storage-proof", "Unable to get transactions: {:?}", e);
None
}
};
Ok(InherentDataProvider::new(proof))
}

/// Build a proof for a given source of randomness and indexed transactions.
pub fn build_proof(random_hash: &[u8], transactions: Vec<Vec<u8>>)
-> Result<TransactionStorageProof, sp_inherents::Error>
-> Result<TransactionStorageProof, Error>
{
let mut db = sp_trie::MemoryDB::<Hasher>::default();

Expand All @@ -195,7 +199,7 @@ pub mod registration {
for (index, chunk) in chunks.enumerate() {
let index = encode_index(index as u32);
trie.insert(&index, &chunk)
.map_err(|e| sp_inherents::Error::Application(Box::new(e)))?;
.map_err(|e| Error::Application(Box::new(e)))?;
if chunk_index == target_chunk_index {
target_chunk = Some(chunk);
target_chunk_key = index;
Expand All @@ -210,7 +214,7 @@ pub mod registration {
&db,
transaction_root.clone(),
&[target_chunk_key.clone()]
).map_err(|e| sp_inherents::Error::Application(Box::new(e)))?;
).map_err(|e| Error::Application(Box::new(e)))?;
}
};

Expand Down

0 comments on commit f8d89f2

Please sign in to comment.