mirror of
https://github.com/serai-dex/serai.git
synced 2025-12-09 04:39:24 +00:00
Update sc_tendermint per previous commit
This commit is contained in:
@@ -1,11 +1,9 @@
|
|||||||
use std::{boxed::Box, sync::Arc, error::Error};
|
use std::{boxed::Box, sync::Arc, error::Error};
|
||||||
|
|
||||||
use sp_keystore::SyncCryptoStore;
|
|
||||||
use sp_runtime::traits::{Block as BlockTrait};
|
use sp_runtime::traits::{Block as BlockTrait};
|
||||||
use sp_inherents::CreateInherentDataProviders;
|
use sp_inherents::CreateInherentDataProviders;
|
||||||
use sp_consensus::DisableProofRecording;
|
use sp_consensus::DisableProofRecording;
|
||||||
use sp_api::{BlockId, ProvideRuntimeApi};
|
use sp_api::ProvideRuntimeApi;
|
||||||
use sp_tendermint::TendermintApi;
|
|
||||||
|
|
||||||
use sc_executor::{NativeVersion, NativeExecutionDispatch, NativeElseWasmExecutor};
|
use sc_executor::{NativeVersion, NativeExecutionDispatch, NativeElseWasmExecutor};
|
||||||
use sc_transaction_pool::FullPool;
|
use sc_transaction_pool::FullPool;
|
||||||
@@ -231,44 +229,23 @@ pub async fn new_full(mut config: Configuration) -> Result<TaskManager, ServiceE
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
if is_authority {
|
if is_authority {
|
||||||
let keys = keystore_container.sync_keystore();
|
task_manager.spawn_essential_handle().spawn(
|
||||||
let key = SyncCryptoStore::sr25519_public_keys(&*keys, sc_tendermint::KEY_TYPE_ID)
|
"tendermint",
|
||||||
.get(0)
|
None,
|
||||||
.cloned()
|
TendermintAuthority::new(authority).authority(
|
||||||
.unwrap_or_else(|| {
|
keystore_container.keystore(),
|
||||||
SyncCryptoStore::sr25519_generate_new(&*keys, sc_tendermint::KEY_TYPE_ID, None).unwrap()
|
Cidp,
|
||||||
});
|
sc_basic_authorship::ProposerFactory::new(
|
||||||
|
task_manager.spawn_handle(),
|
||||||
let mut spawned = false;
|
client,
|
||||||
let mut validators =
|
transaction_pool,
|
||||||
client.runtime_api().validators(&BlockId::Hash(client.chain_info().finalized_hash)).unwrap();
|
registry.as_ref(),
|
||||||
for (i, validator) in validators.drain(..).enumerate() {
|
telemetry.map(|telemtry| telemtry.handle()),
|
||||||
if validator == key {
|
),
|
||||||
task_manager.spawn_essential_handle().spawn(
|
network,
|
||||||
"tendermint",
|
None,
|
||||||
None,
|
),
|
||||||
TendermintAuthority::new(authority).authority(
|
);
|
||||||
(u16::try_from(i).unwrap(), keystore_container.keystore()),
|
|
||||||
Cidp,
|
|
||||||
sc_basic_authorship::ProposerFactory::new(
|
|
||||||
task_manager.spawn_handle(),
|
|
||||||
client,
|
|
||||||
transaction_pool,
|
|
||||||
registry.as_ref(),
|
|
||||||
telemetry.map(|telemtry| telemtry.handle()),
|
|
||||||
),
|
|
||||||
network,
|
|
||||||
None,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
spawned = true;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if !spawned {
|
|
||||||
log::warn!("authority role yet not a validator");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
network_starter.start_network();
|
network_starter.start_network();
|
||||||
|
|||||||
@@ -13,14 +13,11 @@ use crate::{TendermintValidator, validators::TendermintValidators};
|
|||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
pub(crate) struct TendermintGossip<T: TendermintValidator> {
|
pub(crate) struct TendermintGossip<T: TendermintValidator> {
|
||||||
number: Arc<RwLock<u64>>,
|
number: Arc<RwLock<u64>>,
|
||||||
signature_scheme: Arc<TendermintValidators<T>>,
|
signature_scheme: TendermintValidators<T>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T: TendermintValidator> TendermintGossip<T> {
|
impl<T: TendermintValidator> TendermintGossip<T> {
|
||||||
pub(crate) fn new(
|
pub(crate) fn new(number: Arc<RwLock<u64>>, signature_scheme: TendermintValidators<T>) -> Self {
|
||||||
number: Arc<RwLock<u64>>,
|
|
||||||
signature_scheme: Arc<TendermintValidators<T>>,
|
|
||||||
) -> Self {
|
|
||||||
TendermintGossip { number, signature_scheme }
|
TendermintGossip { number, signature_scheme }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -34,7 +34,8 @@ use tendermint_machine::{
|
|||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
CONSENSUS_ID, PROTOCOL_NAME, TendermintValidator, validators::TendermintValidators,
|
CONSENSUS_ID, PROTOCOL_NAME, TendermintValidator,
|
||||||
|
validators::{TendermintSigner, TendermintValidators},
|
||||||
tendermint::TendermintImport,
|
tendermint::TendermintImport,
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -49,6 +50,8 @@ use import_future::ImportFuture;
|
|||||||
// as it's only Authority which implements tendermint_machine::ext::Network. Network has
|
// as it's only Authority which implements tendermint_machine::ext::Network. Network has
|
||||||
// verify_commit provided, and even non-authorities have to verify commits
|
// verify_commit provided, and even non-authorities have to verify commits
|
||||||
struct ActiveAuthority<T: TendermintValidator> {
|
struct ActiveAuthority<T: TendermintValidator> {
|
||||||
|
signer: TendermintSigner<T>,
|
||||||
|
|
||||||
// Block whose gossip is being tracked
|
// Block whose gossip is being tracked
|
||||||
number: Arc<RwLock<u64>>,
|
number: Arc<RwLock<u64>>,
|
||||||
// Outgoing message queue, placed here as the GossipEngine itself can't be
|
// Outgoing message queue, placed here as the GossipEngine itself can't be
|
||||||
@@ -138,7 +141,7 @@ impl<T: TendermintValidator> TendermintAuthority<T> {
|
|||||||
/// as it will not return until the P2P stack shuts down.
|
/// as it will not return until the P2P stack shuts down.
|
||||||
pub async fn authority(
|
pub async fn authority(
|
||||||
mut self,
|
mut self,
|
||||||
validator: (u16, Arc<dyn CryptoStore>),
|
keys: Arc<dyn CryptoStore>,
|
||||||
providers: T::CIDP,
|
providers: T::CIDP,
|
||||||
env: T::Environment,
|
env: T::Environment,
|
||||||
network: T::Network,
|
network: T::Network,
|
||||||
@@ -165,21 +168,21 @@ impl<T: TendermintValidator> TendermintAuthority<T> {
|
|||||||
// Set this struct as active
|
// Set this struct as active
|
||||||
*self.import.providers.write().await = Some(providers);
|
*self.import.providers.write().await = Some(providers);
|
||||||
self.active = Some(ActiveAuthority {
|
self.active = Some(ActiveAuthority {
|
||||||
|
signer: TendermintSigner(keys, self.import.validators.clone()),
|
||||||
|
|
||||||
number: number.clone(),
|
number: number.clone(),
|
||||||
gossip_queue: gossip_queue.clone(),
|
gossip_queue: gossip_queue.clone(),
|
||||||
|
|
||||||
env,
|
env,
|
||||||
announce: network,
|
announce: network,
|
||||||
});
|
});
|
||||||
let (validator, keys) = validator;
|
|
||||||
self.import.validators.set_keys(keys).await;
|
|
||||||
|
|
||||||
let proposal = self
|
let proposal = self
|
||||||
.get_proposal(&self.import.client.header(BlockId::Hash(best_hash)).unwrap().unwrap())
|
.get_proposal(&self.import.client.header(BlockId::Hash(best_hash)).unwrap().unwrap())
|
||||||
.await;
|
.await;
|
||||||
|
|
||||||
// We no longer need self, so let TendermintMachine become its owner
|
// We no longer need self, so let TendermintMachine become its owner
|
||||||
TendermintMachine::new(self, validator, last, proposal)
|
TendermintMachine::new(self, last, proposal)
|
||||||
};
|
};
|
||||||
|
|
||||||
// Start receiving messages about the Tendermint process for this block
|
// Start receiving messages about the Tendermint process for this block
|
||||||
@@ -248,11 +251,15 @@ impl<T: TendermintValidator> Network for TendermintAuthority<T> {
|
|||||||
|
|
||||||
const BLOCK_TIME: u32 = T::BLOCK_TIME_IN_SECONDS;
|
const BLOCK_TIME: u32 = T::BLOCK_TIME_IN_SECONDS;
|
||||||
|
|
||||||
fn signature_scheme(&self) -> Arc<TendermintValidators<T>> {
|
fn signer(&self) -> TendermintSigner<T> {
|
||||||
|
self.active.as_ref().unwrap().signer.clone()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn signature_scheme(&self) -> TendermintValidators<T> {
|
||||||
self.import.validators.clone()
|
self.import.validators.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn weights(&self) -> Arc<TendermintValidators<T>> {
|
fn weights(&self) -> TendermintValidators<T> {
|
||||||
self.import.validators.clone()
|
self.import.validators.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -27,7 +27,7 @@ use crate::{
|
|||||||
|
|
||||||
/// Tendermint import handler.
|
/// Tendermint import handler.
|
||||||
pub struct TendermintImport<T: TendermintValidator> {
|
pub struct TendermintImport<T: TendermintValidator> {
|
||||||
pub(crate) validators: Arc<TendermintValidators<T>>,
|
pub(crate) validators: TendermintValidators<T>,
|
||||||
|
|
||||||
pub(crate) providers: Arc<AsyncRwLock<Option<T::CIDP>>>,
|
pub(crate) providers: Arc<AsyncRwLock<Option<T::CIDP>>>,
|
||||||
pub(crate) importing_block: Arc<RwLock<Option<<T::Block as Block>::Hash>>>,
|
pub(crate) importing_block: Arc<RwLock<Option<<T::Block as Block>::Hash>>>,
|
||||||
@@ -54,7 +54,7 @@ impl<T: TendermintValidator> Clone for TendermintImport<T> {
|
|||||||
impl<T: TendermintValidator> TendermintImport<T> {
|
impl<T: TendermintValidator> TendermintImport<T> {
|
||||||
pub(crate) fn new(client: Arc<T::Client>) -> TendermintImport<T> {
|
pub(crate) fn new(client: Arc<T::Client>) -> TendermintImport<T> {
|
||||||
TendermintImport {
|
TendermintImport {
|
||||||
validators: Arc::new(TendermintValidators::new(client.clone())),
|
validators: TendermintValidators::new(client.clone()),
|
||||||
|
|
||||||
providers: Arc::new(AsyncRwLock::new(None)),
|
providers: Arc::new(AsyncRwLock::new(None)),
|
||||||
importing_block: Arc::new(RwLock::new(None)),
|
importing_block: Arc::new(RwLock::new(None)),
|
||||||
|
|||||||
@@ -3,8 +3,6 @@ use std::sync::{Arc, RwLock};
|
|||||||
|
|
||||||
use async_trait::async_trait;
|
use async_trait::async_trait;
|
||||||
|
|
||||||
use tokio::sync::RwLock as AsyncRwLock;
|
|
||||||
|
|
||||||
use sp_core::Decode;
|
use sp_core::Decode;
|
||||||
use sp_application_crypto::{
|
use sp_application_crypto::{
|
||||||
RuntimePublic as PublicTrait,
|
RuntimePublic as PublicTrait,
|
||||||
@@ -17,7 +15,7 @@ use sp_api::{BlockId, ProvideRuntimeApi};
|
|||||||
|
|
||||||
use sc_client_api::HeaderBackend;
|
use sc_client_api::HeaderBackend;
|
||||||
|
|
||||||
use tendermint_machine::ext::{BlockNumber, Round, Weights, SignatureScheme};
|
use tendermint_machine::ext::{BlockNumber, Round, Weights, Signer, SignatureScheme};
|
||||||
|
|
||||||
use sp_tendermint::TendermintApi;
|
use sp_tendermint::TendermintApi;
|
||||||
|
|
||||||
@@ -82,49 +80,81 @@ impl<T: TendermintClient> Deref for Refresh<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Tendermint validators observer, providing data on the active validators.
|
/// Tendermint validators observer, providing data on the active validators.
|
||||||
pub struct TendermintValidators<T: TendermintClient>(
|
pub struct TendermintValidators<T: TendermintClient>(Refresh<T>);
|
||||||
Refresh<T>,
|
impl<T: TendermintClient> Clone for TendermintValidators<T> {
|
||||||
Arc<AsyncRwLock<Option<Arc<dyn CryptoStore>>>>,
|
fn clone(&self) -> Self {
|
||||||
);
|
Self(Refresh { _refresh: self.0._refresh.clone(), client: self.0.client.clone() })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: TendermintClient> TendermintValidators<T> {
|
impl<T: TendermintClient> TendermintValidators<T> {
|
||||||
pub(crate) fn new(client: Arc<T::Client>) -> TendermintValidators<T> {
|
pub(crate) fn new(client: Arc<T::Client>) -> TendermintValidators<T> {
|
||||||
TendermintValidators(
|
TendermintValidators(Refresh {
|
||||||
Refresh {
|
_refresh: Arc::new(RwLock::new(TendermintValidatorsStruct::from_module::<T>(&client))),
|
||||||
_refresh: Arc::new(RwLock::new(TendermintValidatorsStruct::from_module::<T>(&client))),
|
client,
|
||||||
client,
|
})
|
||||||
},
|
|
||||||
Arc::new(AsyncRwLock::new(None)),
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) async fn set_keys(&self, keys: Arc<dyn CryptoStore>) {
|
pub struct TendermintSigner<T: TendermintClient>(
|
||||||
*self.1.write().await = Some(keys);
|
pub(crate) Arc<dyn CryptoStore>,
|
||||||
|
pub(crate) TendermintValidators<T>,
|
||||||
|
);
|
||||||
|
|
||||||
|
impl<T: TendermintClient> Clone for TendermintSigner<T> {
|
||||||
|
fn clone(&self) -> Self {
|
||||||
|
Self(self.0.clone(), self.1.clone())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: TendermintClient> TendermintSigner<T> {
|
||||||
|
async fn get_public_key(&self) -> Public {
|
||||||
|
let pubs = self.0.sr25519_public_keys(KEY_TYPE_ID).await;
|
||||||
|
if pubs.is_empty() {
|
||||||
|
self.0.sr25519_generate_new(KEY_TYPE_ID, None).await.unwrap()
|
||||||
|
} else {
|
||||||
|
pubs[0]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[async_trait]
|
#[async_trait]
|
||||||
|
impl<T: TendermintClient> Signer for TendermintSigner<T> {
|
||||||
|
type ValidatorId = u16;
|
||||||
|
type Signature = Signature;
|
||||||
|
|
||||||
|
async fn validator_id(&self) -> u16 {
|
||||||
|
let key = self.get_public_key().await;
|
||||||
|
for (i, k) in (*self.1 .0).read().unwrap().lookup.iter().enumerate() {
|
||||||
|
if k == &key {
|
||||||
|
return u16::try_from(i).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// TODO: Enable switching between being a validator and not being one, likely be returning
|
||||||
|
// Option<u16> here. Non-validators should be able to simply not broadcast when they think
|
||||||
|
// they have messages.
|
||||||
|
panic!("not a validator");
|
||||||
|
}
|
||||||
|
|
||||||
|
async fn sign(&self, msg: &[u8]) -> Signature {
|
||||||
|
Signature::decode(
|
||||||
|
&mut self
|
||||||
|
.0
|
||||||
|
.sign_with(KEY_TYPE_ID, &self.get_public_key().await.into(), msg)
|
||||||
|
.await
|
||||||
|
.unwrap()
|
||||||
|
.unwrap()
|
||||||
|
.as_ref(),
|
||||||
|
)
|
||||||
|
.unwrap()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<T: TendermintClient> SignatureScheme for TendermintValidators<T> {
|
impl<T: TendermintClient> SignatureScheme for TendermintValidators<T> {
|
||||||
type ValidatorId = u16;
|
type ValidatorId = u16;
|
||||||
type Signature = Signature;
|
type Signature = Signature;
|
||||||
type AggregateSignature = Vec<Signature>;
|
type AggregateSignature = Vec<Signature>;
|
||||||
|
type Signer = TendermintSigner<T>;
|
||||||
async fn sign(&self, msg: &[u8]) -> Signature {
|
|
||||||
let read = self.1.read().await;
|
|
||||||
let keys = read.as_ref().unwrap();
|
|
||||||
let key = {
|
|
||||||
let pubs = keys.sr25519_public_keys(KEY_TYPE_ID).await;
|
|
||||||
if pubs.is_empty() {
|
|
||||||
keys.sr25519_generate_new(KEY_TYPE_ID, None).await.unwrap()
|
|
||||||
} else {
|
|
||||||
pubs[0]
|
|
||||||
}
|
|
||||||
};
|
|
||||||
Signature::decode(
|
|
||||||
&mut keys.sign_with(KEY_TYPE_ID, &key.into(), msg).await.unwrap().unwrap().as_ref(),
|
|
||||||
)
|
|
||||||
.unwrap()
|
|
||||||
}
|
|
||||||
|
|
||||||
fn verify(&self, validator: u16, msg: &[u8], sig: &Signature) -> bool {
|
fn verify(&self, validator: u16, msg: &[u8], sig: &Signature) -> bool {
|
||||||
self.0.read().unwrap().lookup[usize::try_from(validator).unwrap()].verify(&msg, sig)
|
self.0.read().unwrap().lookup[usize::try_from(validator).unwrap()].verify(&msg, sig)
|
||||||
|
|||||||
Reference in New Issue
Block a user