-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add consolidated updates to nc-consensus-pow
- Loading branch information
Showing
8 changed files
with
473 additions
and
269 deletions.
There are no files selected for viewing
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,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 } |
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,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)])) | ||
} |
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,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())]) | ||
} | ||
} |
Oops, something went wrong.