-
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: Adapt sc-consensus-pow for self-maintenance (#129)
* feat: Introduce pallet-wtema for difficulty adjustment * feat: Introduce np-consensus-pow for PoW consensus primitives * chore: Vendor sc-consensus-pow * chore: Adapt sc-consensus-pow for self-maintenance * feat: Add consolidated updates to nc-consensus-pow * chore: Ignore text block durign doc tests * build: Add nc-consensus to default-members
- Loading branch information
Showing
15 changed files
with
1,553 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[package] | ||
name = "nc-consensus-pow" | ||
description = "Noir PoW consensus algorithm" | ||
license = "GPL-3.0-or-later WITH Classpath-exception-2.0" | ||
authors = { workspace = true } | ||
version = { workspace = true } | ||
edition = { workspace = true } | ||
repository = { workspace = true } | ||
|
||
[dependencies] | ||
async-trait = { workspace = true } | ||
futures = { workspace = true } | ||
futures-timer = { workspace = true } | ||
log = { workspace = true, default-features = true } | ||
parity-scale-codec = { workspace = true, default-features = true, features = ["derive"] } | ||
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 } | ||
sp-api = { workspace = true } | ||
sp-block-builder = { workspace = true, default-features = true } | ||
sp-blockchain = { workspace = true, default-features = true } | ||
sp-consensus = { workspace = true, default-features = true } | ||
sp-core = { workspace = true, default-features = true } | ||
sp-inherents = { workspace = true, default-features = true } | ||
sp-runtime = { workspace = true, default-features = true } | ||
substrate-prometheus-endpoint = { 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
Proof of work consensus for Substrate. | ||
|
||
To use this engine, you can need to have a struct that implements | ||
`PowAlgorithm`. After that, pass an instance of the struct, along | ||
with other necessary client references to `import_queue` to setup | ||
the queue. | ||
|
||
This library also comes with an async mining worker, which can be | ||
started via the `start_mining_worker` function. It returns a worker | ||
handle together with a future. The future must be pulled. Through | ||
the worker handle, you can pull the metadata needed to start the | ||
mining process via `MiningWorker::metadata`, and then do the actual | ||
mining on a standalone thread. Finally, when a seal is found, call | ||
`MiningWorker::submit` to build the block. | ||
|
||
The auxiliary storage for PoW engine only stores the total difficulty. | ||
For other storage requirements for particular PoW algorithm (such as | ||
the actual difficulty for each particular blocks), you can take a client | ||
reference in your `PowAlgorithm` implementation, and use a separate prefix | ||
for the auxiliary storage. It is also possible to just use the runtime | ||
as the storage, but it is not recommended as it won't work well with light | ||
clients. | ||
|
||
License: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
|
||
## Release | ||
|
||
Polkadot SDK stable2409 |
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.