Partial work on correcting pallet calls

This commit is contained in:
Luke Parker 2022-10-27 06:29:56 -04:00
parent eb418448eb
commit 4c2dd9b306
No known key found for this signature in database
GPG key ID: F9F1386DB1E119B6
7 changed files with 89 additions and 27 deletions

1
Cargo.lock generated
View file

@ -7419,6 +7419,7 @@ name = "serai-consensus"
version = "0.1.0"
dependencies = [
"async-trait",
"frame-support",
"futures",
"log",
"pallet-session",

View file

@ -38,6 +38,7 @@ sc-service = { git = "https://github.com/serai-dex/substrate" }
sc-client-api = { git = "https://github.com/serai-dex/substrate" }
sc-consensus = { git = "https://github.com/serai-dex/substrate" }
frame-support = { git = "https://github.com/serai-dex/substrate" }
pallet-session = { git = "https://github.com/serai-dex/substrate" }
substrate-prometheus-endpoint = { git = "https://github.com/serai-dex/substrate" }

View file

@ -2,6 +2,7 @@ use std::{sync::Arc, collections::HashMap};
use async_trait::async_trait;
use sp_core::sr25519::Public;
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::Block;
use sp_api::TransactionFor;
@ -11,6 +12,8 @@ use sc_consensus::{BlockCheckParams, BlockImportParams, ImportResult, BlockImpor
use sc_client_api::Backend;
use frame_support::traits::ValidatorSet;
use crate::{
tendermint::{TendermintClient, TendermintImport},
Announce,
@ -29,6 +32,7 @@ where
TransactionFor<C, B>: Send + Sync + 'static,
Arc<C>: BlockImport<B, Transaction = TransactionFor<C, B>>,
<Arc<C> as BlockImport<B>>::Error: Into<Error>,
C::Api: ValidatorSet<Public>,
{
type Error = Error;
type Transaction = TransactionFor<C, B>;

View file

@ -6,7 +6,7 @@ use std::{
time::{UNIX_EPOCH, SystemTime},
};
use sp_core::Decode;
use sp_core::{Decode, sr25519::Public};
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::{Header, Block};
use sp_api::{BlockId, TransactionFor};
@ -19,6 +19,8 @@ use sc_client_api::Backend;
use substrate_prometheus_endpoint::Registry;
use frame_support::traits::ValidatorSet;
use tendermint_machine::{
ext::{BlockNumber, Commit},
TendermintMachine,
@ -98,6 +100,7 @@ where
TransactionFor<C, B>: Send + Sync + 'static,
Arc<C>: BlockImport<B, Transaction = TransactionFor<C, B>>,
<Arc<C> as BlockImport<B>>::Error: Into<Error>,
C::Api: ValidatorSet<Public>,
{
let import = TendermintImport::new(client, announce, providers, env);
@ -116,7 +119,7 @@ where
Ok(best) => BlockNumber(best),
Err(_) => panic!("BlockNumber exceeded u64"),
},
Commit::<TendermintValidators>::decode(
Commit::<TendermintValidators<B, C>>::decode(
&mut import_clone
.client
.justifications(&BlockId::Number(best))

View file

@ -10,8 +10,10 @@ use log::warn;
use tokio::sync::RwLock as AsyncRwLock;
use sp_core::{Encode, Decode};
use sp_application_crypto::sr25519::Signature;
use sp_core::{
Encode, Decode,
sr25519::{Public, Signature},
};
use sp_inherents::{InherentData, InherentDataProvider, CreateInherentDataProviders};
use sp_runtime::{
traits::{Header, Block},
@ -26,6 +28,8 @@ use sc_consensus::{ForkChoiceStrategy, BlockImportParams, BlockImport, import_qu
use sc_service::ImportQueue;
use sc_client_api::{BlockBackend, Backend, Finalizer};
use frame_support::traits::ValidatorSet;
use tendermint_machine::{
ext::{BlockError, Commit, Network},
SignedMessage, TendermintHandle,
@ -47,6 +51,9 @@ pub trait TendermintClient<B: Block, Be: Backend<B> + 'static>:
+ Finalizer<B, Be>
+ ProvideRuntimeApi<B>
+ 'static
where
TransactionFor<Self, B>: Send + Sync + 'static,
Self::Api: ValidatorSet<Public>,
{
}
impl<
@ -61,6 +68,9 @@ impl<
+ ProvideRuntimeApi<B>
+ 'static,
> TendermintClient<B, Be> for C
where
TransactionFor<C, B>: Send + Sync + 'static,
C::Api: ValidatorSet<Public>,
{
}
@ -73,10 +83,13 @@ pub(crate) struct TendermintImport<
A: Announce<B>,
> where
TransactionFor<C, B>: Send + Sync + 'static,
C::Api: ValidatorSet<Public>,
{
_block: PhantomData<B>,
_backend: PhantomData<Be>,
validators: Arc<TendermintValidators<B, C>>,
importing_block: Arc<RwLock<Option<B::Hash>>>,
pub(crate) machine: Arc<RwLock<Option<TendermintHandle<Self>>>>,
@ -98,12 +111,15 @@ impl<
> Clone for TendermintImport<B, Be, C, CIDP, E, A>
where
TransactionFor<C, B>: Send + Sync + 'static,
C::Api: ValidatorSet<Public>,
{
fn clone(&self) -> Self {
TendermintImport {
_block: PhantomData,
_backend: PhantomData,
validators: self.validators.clone(),
importing_block: self.importing_block.clone(),
machine: self.machine.clone(),
@ -127,6 +143,7 @@ impl<
> TendermintImport<B, Be, C, CIDP, E, A>
where
TransactionFor<C, B>: Send + Sync + 'static,
C::Api: ValidatorSet<Public>,
{
pub(crate) fn new(
client: Arc<C>,
@ -138,6 +155,8 @@ where
_block: PhantomData,
_backend: PhantomData,
validators: TendermintValidators::new(client),
importing_block: Arc::new(RwLock::new(None)),
machine: Arc::new(RwLock::new(None)),
@ -196,7 +215,7 @@ where
Err(Error::InvalidJustification)?;
}
let commit: Commit<TendermintValidators> =
let commit: Commit<TendermintValidators<B, C>> =
Commit::decode(&mut justification.1.as_ref()).map_err(|_| Error::InvalidJustification)?;
if !self.verify_commit(hash, &commit) {
Err(Error::InvalidJustification)?;
@ -309,20 +328,21 @@ impl<
> Network for TendermintImport<B, Be, C, CIDP, E, A>
where
TransactionFor<C, B>: Send + Sync + 'static,
C::Api: ValidatorSet<Public>,
{
type ValidatorId = u16;
type SignatureScheme = TendermintValidators;
type Weights = TendermintValidators;
type SignatureScheme = TendermintValidators<B, C>;
type Weights = TendermintValidators<B, C>;
type Block = B;
const BLOCK_TIME: u32 = { (serai_runtime::MILLISECS_PER_BLOCK / 1000) as u32 };
fn signature_scheme(&self) -> Arc<TendermintValidators> {
Arc::new(TendermintValidators::new())
fn signature_scheme(&self) -> Arc<TendermintValidators<B, C>> {
self.validators.clone()
}
fn weights(&self) -> Arc<TendermintValidators> {
Arc::new(TendermintValidators::new())
fn weights(&self) -> Arc<TendermintValidators<B, C>> {
self.validators.clone()
}
async fn broadcast(&mut self, msg: SignedMessage<u16, Self::Block, Signature>) {
@ -390,7 +410,7 @@ where
Ok(())
}
async fn add_block(&mut self, block: B, commit: Commit<TendermintValidators>) -> B {
async fn add_block(&mut self, block: B, commit: Commit<TendermintValidators<B, C>>) -> B {
let hash = block.hash();
let justification = (CONSENSUS_ID, commit.encode());
debug_assert!(self.verify_justification(hash, &justification).is_ok());

View file

@ -1,4 +1,4 @@
use core::ops::Deref;
use core::{marker::PhantomData, ops::Deref};
use std::sync::{Arc, RwLock};
use sp_application_crypto::{
@ -6,8 +6,11 @@ use sp_application_crypto::{
sr25519::{Public, Pair, Signature},
};
use sp_runtime::traits::Block;
use sp_staking::SessionIndex;
use pallet_session::Pallet as Session;
use sp_api::ProvideRuntimeApi;
use frame_support::traits::ValidatorSet;
use tendermint_machine::ext::{BlockNumber, Round, Weights, SignatureScheme};
@ -22,12 +25,17 @@ struct TendermintValidatorsStruct {
}
impl TendermintValidatorsStruct {
fn from_module() -> TendermintValidatorsStruct {
let validators = Session::<serai_runtime::Runtime>::validators();
fn from_module<B: Block, C: Send + Sync + ProvideRuntimeApi<B>>(
client: C,
) -> TendermintValidatorsStruct
where
C::Api: ValidatorSet<Public>,
{
let validators = client.runtime_api().validators();
assert_eq!(validators.len(), 1);
let keys = Pair::from_string("//Alice", None).unwrap();
TendermintValidatorsStruct {
session: Session::<serai_runtime::Runtime>::current_index(),
session: client.runtime_api().session_index(),
// TODO
total_weight: validators.len().try_into().unwrap(),
@ -40,20 +48,28 @@ impl TendermintValidatorsStruct {
}
// Wrap every access of the validators struct in something which forces calling refresh
struct Refresh {
struct Refresh<B: Block, C: Send + Sync + ProvideRuntimeApi<B>> {
_block: PhantomData<B>,
client: C,
_refresh: Arc<RwLock<TendermintValidatorsStruct>>,
}
impl Refresh {
impl<B: Block, C: Send + Sync + ProvideRuntimeApi<B>> Refresh<B, C>
where
C::Api: ValidatorSet<Public>,
{
// If the session has changed, re-create the struct with the data on it
fn refresh(&self) {
let session = self._refresh.read().unwrap().session;
if session != Session::<serai_runtime::Runtime>::current_index() {
*self._refresh.write().unwrap() = TendermintValidatorsStruct::from_module();
if session != self.client.runtime_api().session_index() {
*self._refresh.write().unwrap() = TendermintValidatorsStruct::from_module(self.client);
}
}
}
impl Deref for Refresh {
impl<B: Block, C: Send + Sync + ProvideRuntimeApi<B>> Deref for Refresh<B, C>
where
C::Api: ValidatorSet<Public>,
{
type Target = RwLock<TendermintValidatorsStruct>;
fn deref(&self) -> &RwLock<TendermintValidatorsStruct> {
self.refresh();
@ -61,16 +77,26 @@ impl Deref for Refresh {
}
}
pub(crate) struct TendermintValidators(Refresh);
impl TendermintValidators {
pub(crate) fn new() -> TendermintValidators {
pub(crate) struct TendermintValidators<B: Block, C: Send + Sync + ProvideRuntimeApi<B>>(
Refresh<B, C>,
);
impl<B: Block, C: Send + Sync + ProvideRuntimeApi<B>> TendermintValidators<B, C>
where
C::Api: ValidatorSet<Public>,
{
pub(crate) fn new(client: C) -> TendermintValidators<B, C> {
TendermintValidators(Refresh {
_block: PhantomData,
client,
_refresh: Arc::new(RwLock::new(TendermintValidatorsStruct::from_module())),
})
}
}
impl SignatureScheme for TendermintValidators {
impl<B: Block, C: Send + Sync + ProvideRuntimeApi<B>> SignatureScheme for TendermintValidators<B, C>
where
C::Api: ValidatorSet<Public>,
{
type ValidatorId = u16;
type Signature = Signature;
type AggregateSignature = Vec<Signature>;
@ -100,7 +126,10 @@ impl SignatureScheme for TendermintValidators {
}
}
impl Weights for TendermintValidators {
impl<B: Block, C: Send + Sync + ProvideRuntimeApi<B>> Weights for TendermintValidators<B, C>
where
C::Api: ValidatorSet<Public>,
{
type ValidatorId = u16;
fn total_weight(&self) -> u64 {

View file

@ -2,6 +2,7 @@ use std::sync::Arc;
use async_trait::async_trait;
use sp_core::sr25519::Public;
use sp_inherents::CreateInherentDataProviders;
use sp_runtime::traits::Block;
use sp_api::TransactionFor;
@ -11,6 +12,8 @@ use sc_consensus::{BlockImportParams, BlockImport, Verifier};
use sc_client_api::Backend;
use frame_support::traits::ValidatorSet;
use crate::{
tendermint::{TendermintClient, TendermintImport},
Announce,
@ -29,6 +32,7 @@ where
TransactionFor<C, B>: Send + Sync + 'static,
Arc<C>: BlockImport<B, Transaction = TransactionFor<C, B>>,
<Arc<C> as BlockImport<B>>::Error: Into<Error>,
C::Api: ValidatorSet<Public>,
{
async fn verify(
&mut self,