Skip to content

Commit

Permalink
feat: Add consolidated updates to nc-consensus-pow
Browse files Browse the repository at this point in the history
  • Loading branch information
conr2d committed Feb 1, 2025
1 parent ce96d4e commit afd77e5
Show file tree
Hide file tree
Showing 8 changed files with 473 additions and 269 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ repository = "https://github.com/noirhq/noir.git"
[workspace]
resolver = "2"
members = [
"client/consensus",
"client/consensus/pow",
"core-primitives",
"frame/babel",
Expand Down Expand Up @@ -193,6 +194,7 @@ precompile-utils = { git = "https://github.com/noirhq/frontier", branch = "crate
cosmos-rpc = { path = "frame/cosmos/rpc", default-features = false }
cosmos-runtime-api = { path = "frame/cosmos/runtime-api", default-features = false }
frame-babel = { path = "frame/babel", default-features = false }
nc-consensus = { path = "client/consensus" }
nc-consensus-pow = { path = "client/consensus/pow" }
noir-core-primitives = { path = "core-primitives", default-features = false }
noir-runtime-common = { path = "runtime/common", default-features = false }
Expand Down
14 changes: 14 additions & 0 deletions client/consensus/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "nc-consensus"
description = "Noir common types for consensus"
license = "Apache-2.0"
authors = { workspace = true }
version = { workspace = true }
edition = { workspace = true }
repository = { workspace = true }
publish = false

[dependencies]
async-trait = { workspace = true }
sp-blockchain = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }
1 change: 1 addition & 0 deletions client/consensus/pow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ parity-scale-codec = { workspace = true, default-features = true, features = ["d
parking_lot = { workspace = true, default-features = true }
thiserror = { workspace = true, default-features = true }

nc-consensus = { workspace = true }
np-consensus-pow = { workspace = true, default-features = true }
sc-client-api = { workspace = true }
sc-consensus = { workspace = true }
Expand Down
58 changes: 58 additions & 0 deletions client/consensus/pow/src/aux_schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// This file is part of Noir.

// Copyright (C) Haderech Pte. Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use np_consensus_pow::BlockWeight;
use parity_scale_codec::{Decode, Encode};
use sc_client_api::AuxStore;
use sp_blockchain::{Error, Result};

pub fn block_weight_key<H: Encode>(block_hash: H) -> Vec<u8> {
(b"block_weight", block_hash).encode()
}

fn load_decode<B: AuxStore, T: Decode>(backend: &B, key: &[u8]) -> Result<Option<T>> {
match backend.get_aux(key)? {
None => Ok(None),
Some(t) => T::decode(&mut &t[..])
.map_err(|e| Error::Backend(format!("PoW DB is corrupted: {}", e)))
.map(Some),
}
}

pub fn load_block_weight<B, H, W>(backend: &B, block_hash: &H) -> Result<BlockWeight<W>>
where
B: AuxStore,
H: Encode,
W: Decode + Default,
{
load_decode(backend, &block_weight_key(block_hash)[..]).map(|v| v.unwrap_or_default())
}

pub(crate) fn write_block_weight<H, W, F, R>(
block_hash: H,
block_weight: BlockWeight<W>,
write_aux: F,
) -> R
where
H: Encode,
W: Encode,
F: FnOnce(&[(Vec<u8>, &[u8])]) -> R,
{
let key = block_weight_key(block_hash);
block_weight.using_encoded(|s| write_aux(&[(key, s)]))
}
63 changes: 63 additions & 0 deletions client/consensus/pow/src/digests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// This file is part of Noir.

// Copyright (C) Haderech Pte. Ltd.
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use nc_consensus::PreDigestProvider;
use np_consensus_pow::POW_ENGINE_ID;
use parity_scale_codec::{Decode, Encode};
use sp_blockchain::Result;
use sp_runtime::DigestItem;
use std::ops::Deref;

/// Generic pre-digest for PoW consensus engine.
#[derive(Clone, Debug, Decode, Encode)]
pub struct PreDigest<AccountId, Inner = ()> {
author: AccountId,
inner: Inner,
}

impl<AccountId, Inner> PreDigest<AccountId, Inner> {
pub fn new(author: AccountId, inner: Inner) -> Self {
Self { author, inner }
}

pub fn author(&self) -> &AccountId {
&self.author
}

pub fn into_inner(self) -> Inner {
self.inner
}
}

impl<AccountId, Inner> Deref for PreDigest<AccountId, Inner> {
type Target = Inner;

fn deref(&self) -> &Self::Target {
&self.inner
}
}

#[async_trait::async_trait]
impl<AccountId> PreDigestProvider for PreDigest<AccountId, ()>
where
AccountId: Encode + Send + Sync,
{
async fn pre_digest(&self, _best_hash: &[u8]) -> Result<Vec<DigestItem>> {
Ok(vec![DigestItem::PreRuntime(POW_ENGINE_ID, self.author.encode())])
}
}
Loading

0 comments on commit afd77e5

Please sign in to comment.