Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6,939 changes: 3,669 additions & 3,270 deletions Cargo.lock

Large diffs are not rendered by default.

400 changes: 213 additions & 187 deletions Cargo.toml

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@ cifrost:
handover:
cargo build -p handover --release
ceseal:
make -C standalone/teeworker/ceseal
$(MAKE) -C standalone/teeworker/ceseal BUILD=${BUILD} OA=${OA} VC=${VC}
test:
cargo test --workspace --exclude node-executor --exclude cess-node

clippy:
cargo clippy --tests
make clippy -C standalone/teeworker/ceseal
$(MAKE) clippy -C standalone/teeworker/ceseal

lint:
cargo dylint --all --workspace

clean:
cargo clean
make -C standalone/teeworker/ceseal clean
$(MAKE) -C standalone/teeworker/ceseal clean
4 changes: 2 additions & 2 deletions crates/ces-pdp/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -423,15 +423,15 @@ impl Keys {
Ok(sigma.to_string())
}

pub fn aggr_append_proof(&self, mut aggr_sigma: String, mut sub_sigma: String) -> Result<String, PDPError> {
pub fn aggr_append_proof(&self, mut aggr_sigma: String, sub_sigma: String) -> Result<String, PDPError> {
let n = num_bigint::BigUint::from_bytes_be(&self.pkey.n().to_bytes_be());
if aggr_sigma == "".to_string() {
aggr_sigma = "1".to_string()
}
let mut sigma = num_bigint::BigUint::from_str(&aggr_sigma)
.map_err(|e| PDPError { error_code: FailCode::InternalError(e.to_string()) })?;

let mut sub_sigma = num_bigint::BigUint::from_str(&sub_sigma)
let sub_sigma = num_bigint::BigUint::from_str(&sub_sigma)
.map_err(|e| PDPError { error_code: FailCode::InternalError(e.to_string()) })?;
sigma = sigma * sub_sigma;
sigma = sigma.mod_floor(&n);
Expand Down
1 change: 1 addition & 0 deletions crates/cestory/src/light_validation/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use derive_more::{Display, From};

/// Substrate Client error
#[derive(Debug, Display, From)]
#[allow(dead_code)]
pub enum JustificationError {
/// Error decoding header justification.
#[display("error decoding justification for header")]
Expand Down
14 changes: 4 additions & 10 deletions crates/cestory/src/light_validation/justification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use sp_runtime::traits::{Block as BlockT, Header as HeaderT, NumberFor};
/// This is meant to be stored in the db and passed around the network to other
/// nodes, and are used by syncing nodes to prove authority set handoffs.
#[derive(Clone, Encode, Decode, PartialEq, Eq, Debug)]
#[allow(dead_code)]
pub struct GrandpaJustification<Block: BlockT> {
/// The GRANDPA justification for block finality.
pub justification: sp_consensus_grandpa::GrandpaJustification<Block::Header>,
Expand All @@ -60,6 +61,7 @@ impl<Block: BlockT> Into<sp_consensus_grandpa::GrandpaJustification<Block::Heade
impl<Block: BlockT> GrandpaJustification<Block> {
/// Decode a GRANDPA justification and validate the commit and the votes'
/// ancestry proofs finalize the given block.
#[allow(dead_code)]
pub fn decode_and_verify_finalizes(
encoded: &[u8],
finalized_target: (Block::Hash, NumberFor<Block>),
Expand All @@ -70,16 +72,7 @@ impl<Block: BlockT> GrandpaJustification<Block> {
where
NumberFor<Block>: finality_grandpa::BlockNumberOps,
{
let mut justification = GrandpaJustification::<Block>::decode(&mut &*encoded).map_err(|e| {
// log::error!(
// "decode justification error:{:?}, block:{:?}, input(len:{}):{}",
// e,
// finalized_target.1,
// encoded.len(),
// hex::encode(encoded)
// );
ClientError::JustificationDecode
})?;
let mut justification = GrandpaJustification::<Block>::decode(&mut &*encoded).map_err(|_| ClientError::JustificationDecode)?;
justification.grandpa_note_stalled = grandpa_note_stalled;

if (justification.justification.commit.target_hash, justification.justification.commit.target_number)
Expand Down Expand Up @@ -197,6 +190,7 @@ impl<Block: BlockT> GrandpaJustification<Block> {
/// A utility trait implementing `finality_grandpa::Chain` using a given set of headers.
/// This is useful when validating commits, using the given set of headers to
/// verify a valid ancestry route to the target commit block.
#[allow(dead_code)]
struct AncestryChain<Block: BlockT> {
ancestry: HashMap<Block::Hash, Block::Header>,
}
Expand Down
28 changes: 1 addition & 27 deletions crates/cestory/src/light_validation/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
// You should have received a copy of the GNU General Public License
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

// Ensure we're `no_std` when compiling for Wasm.
// #![cfg_attr(not(feature = "std"), no_std)]

mod error;
mod justification;
pub mod storage_proof;
Expand All @@ -27,7 +24,6 @@ use ces_serde_more as more;
use error::JustificationError;
#[cfg(not(test))]
use im::OrdMap as BTreeMap;
use justification::GrandpaJustification;
use log::{error, trace};
use serde::{Deserialize, Serialize};
#[cfg(test)]
Expand All @@ -36,7 +32,6 @@ use std::collections::BTreeMap;
use std::{fmt, marker::PhantomData};
use storage_proof::{StorageProof, StorageProofChecker};

use finality_grandpa::voter_set::VoterSet;
use num_traits::AsPrimitive;
use parity_scale_codec::{Decode, Encode};
use sp_consensus_grandpa::{AuthorityId, AuthorityWeight, SetId};
Expand Down Expand Up @@ -135,7 +130,7 @@ where
bridge_id: BridgeId,
header: BlockHeader,
ancestry_proof: Vec<BlockHeader>,
grandpa_proof: EncodedJustification,
_grandpa_proof: EncodedJustification,
) -> Result<()> {
let bridge = self
.tracked_bridges
Expand All @@ -146,31 +141,10 @@ where
let last_header = &bridge.last_finalized_block_header;
verify_ancestry(ancestry_proof, last_header.hash(), &header)?;

let block_hash = header.hash();
let block_num = *header.number();

// Check that the header has been finalized
let voters = &bridge.current_set;
let voter_set = VoterSet::new(voters.list.clone()).unwrap();
let voter_set_id = voters.id;

// We don't really care about the justification, as long as it's valid
match GrandpaJustification::<<T as frame_system::Config>::Block>::decode_and_verify_finalizes(
&grandpa_proof,
(block_hash, block_num.into()),
voter_set_id,
&voter_set,
self.grandpa_note_stalled,
) {
Ok(_) => {},
Err(JustificationError::JustificationDecode) if self.grandpa_note_stalled => {
log::debug!("grandpa_note_stalled is true, ignore JustificationDecode error");
},
Err(e) => {
return Err(anyhow::Error::msg(e));
},
}

match self.tracked_bridges.get_mut(&bridge_id) {
Some(bridge_info) => {
if let Some(scheduled_change) = find_scheduled_change(&header) {
Expand Down
7 changes: 4 additions & 3 deletions crates/cesxt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ scale-encode = { workspace = true }
anyhow = { workspace = true }
tokio = { workspace = true }

subxt = { workspace = true, features = ["jsonrpsee", "native", "substrate-compat"] }
jsonrpsee = { version = "0.20" } # use the same version of jsonrpsee in subxt
subxt = { workspace = true, features = ["jsonrpsee", "native"] }
jsonrpsee = { workspace = true }
primitive-types = { workspace = true, default-features = true }
sp-core = { workspace = true, default-features = true }
sp-runtime = { workspace = true, default-features = true }

ces-types = { workspace = true, default-features = true }
ces-node-rpc-ext-types = { workspace = true, default-features = true }
# ces-pallets = { workspace = true, default-features = true }
11 changes: 5 additions & 6 deletions crates/cesxt/src/chain_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,15 @@ use subxt::{
rpc::RpcClient as SubxtRpcClient,
},
dynamic::Value,
ext::scale_encode::EncodeAsType,
storage::Storage,
storage::{Storage, StorageKey},
};

pub async fn connect(uri: &str) -> Result<ChainApi> {
let wsc = ws_client(uri).await?;
let rpc_client = SubxtRpcClient::new(wsc);
let rpc_methods = LegacyRpcMethods::<Config>::new(rpc_client.clone());
let extra_rpc_methods = ExtraRpcMethods::<Config>::new(rpc_client.clone());
let backend = LegacyBackend::new(rpc_client);
let backend = LegacyBackend::builder().build(rpc_client);
let client = SubxtOnlineClient::from_backend(Arc::new(backend))
.await
.context("Failed to connect to substrate")?;
Expand Down Expand Up @@ -88,14 +87,14 @@ impl ChainApi {
&self,
pallet_name: &str,
entry_name: &str,
key: &impl EncodeAsType,
keys: impl StorageKey,
) -> Result<Vec<u8>> {
let address = subxt::dynamic::storage(pallet_name, entry_name, vec![key]);
let address = subxt::dynamic::storage(pallet_name, entry_name, keys);
Ok(self.storage().address_bytes(&address)?)
}

pub async fn current_set_id(&self, block_hash: Option<Hash>) -> Result<u64> {
let address = subxt::dynamic::storage("Grandpa", "CurrentSetId", Vec::<()>::new());
let address = subxt::dynamic::storage("Grandpa", "CurrentSetId", ());
let set_id = self
.storage_at(block_hash)
.await?
Expand Down
4 changes: 3 additions & 1 deletion crates/cesxt/src/dynamic.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
pub mod tx;

use subxt::dynamic::{self, Value};

pub fn storage_key(pallet: &str, entry: &str) -> Vec<u8> {
::subxt::dynamic::storage(pallet, entry, Vec::<()>::new()).to_root_bytes()
dynamic::storage(pallet, entry, Vec::<Value>::new()).to_root_bytes()
}
6 changes: 3 additions & 3 deletions crates/cesxt/src/dynamic/tx.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use ces_types::messaging::SignedMessage;
use parity_scale_codec::Encode;
use subxt::{tx::TxPayload, utils::Encoded};
use subxt::{ext::subxt_core::Error as SubxtCoreError, tx::Payload, utils::Encoded};

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct EncodedPayload {
Expand All @@ -19,12 +19,12 @@ impl EncodedPayload {
}
}

impl TxPayload for EncodedPayload {
impl Payload for EncodedPayload {
fn encode_call_data_to(
&self,
metadata: &subxt::Metadata,
out: &mut Vec<u8>,
) -> Result<(), subxt::Error> {
) -> Result<(), SubxtCoreError> {
let pallet = metadata.pallet_by_name_err(self.pallet_name)?;
let call = pallet.call_variant_by_name(self.call_name).ok_or_else(|| {
subxt::error::MetadataError::CallNameNotFound((*self.call_name).to_owned())
Expand Down
105 changes: 83 additions & 22 deletions crates/cesxt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ mod chain_api;
pub mod dynamic;
pub mod rpc;

pub use subxt::ext::sp_core;

#[derive(Encode, Decode, Clone, PartialEq, Eq, TypeInfo, PartialOrd, Ord, Debug, EncodeAsType)]
pub struct ParaId(pub u32);

Expand All @@ -29,28 +27,91 @@ pub type BlockNumber = u32;
pub type Hash = primitive_types::H256;
pub type AccountId = <Config as subxt::Config>::AccountId;

/// A wrapper for subxt::tx::PairSigner to make it compatible with older API.
pub struct PairSigner {
pub signer: subxt::tx::PairSigner<Config, sp_core::sr25519::Pair>,
nonce: u64,
pub mod sp_core {
pub use sp_core::*;
}
impl PairSigner {
pub fn new(pair: sp_core::sr25519::Pair) -> Self {
Self {
signer: subxt::tx::PairSigner::new(pair),
nonce: 0,
}
}
pub fn increment_nonce(&mut self) {
self.nonce += 1;
}
pub fn nonce(&self) -> u64 {
self.nonce

pub use pair_signer::PairSigner;

/// A concrete PairSigner implementation which relies on `sr25519::Pair` for signing
/// and that PolkadotConfig is the runtime configuration.
mod pair_signer {
use sp_core::{sr25519, Pair as _};
use subxt::{Config, PolkadotConfig};

use sp_runtime::{
traits::{IdentifyAccount, Verify},
MultiSignature as SpMultiSignature,
};
use subxt::{
config::substrate::{AccountId32, MultiSignature},
tx::Signer,
};

/// A [`Signer`] implementation for [`polkadot_sdk::sp_core::sr25519::Pair`].
#[derive(Clone)]
pub struct PairSigner {
account_id: <PolkadotConfig as Config>::AccountId,
signer: sr25519::Pair,
nonce: u64,
}
pub fn set_nonce(&mut self, nonce: u64) {
self.nonce = nonce;

impl PairSigner {
/// Creates a new [`Signer`] from an [`sp_core::sr25519::Pair`].
pub fn new(signer: sr25519::Pair) -> Self {
let account_id =
<SpMultiSignature as Verify>::Signer::from(signer.public()).into_account();
Self {
// Convert `sp_core::AccountId32` to `subxt::config::substrate::AccountId32`.
//
// This is necessary because we use `subxt::config::substrate::AccountId32` and no
// From/Into impls are provided between `sp_core::AccountId32` because `polkadot-sdk` isn't a direct
// dependency in subxt.
//
// This can also be done by provided a wrapper type around `subxt::config::substrate::AccountId32` to implement
// such conversions but that also most likely requires a custom `Config` with a separate `AccountId` type to work
// properly without additional hacks.
account_id: AccountId32(account_id.into()),
signer,
nonce: 0,
}
}

/// Returns the [`sp_core::sr25519::Pair`] implementation used to construct this.
pub fn signer(&self) -> &sr25519::Pair {
&self.signer
}

/// Return the account ID.
pub fn account_id(&self) -> &AccountId32 {
&self.account_id
}

pub fn increment_nonce(&mut self) {
self.nonce += 1;
}

pub fn nonce(&self) -> u64 {
self.nonce
}

pub fn set_nonce(&mut self, nonce: u64) {
self.nonce = nonce;
}
}
pub fn account_id(&self) -> &AccountId {
self.signer.account_id()

impl Signer<PolkadotConfig> for PairSigner {
fn account_id(&self) -> <PolkadotConfig as Config>::AccountId {
self.account_id.clone()
}

fn address(&self) -> <PolkadotConfig as Config>::Address {
self.account_id.clone().into()
}

fn sign(&self, signer_payload: &[u8]) -> <PolkadotConfig as Config>::Signature {
let signature = self.signer.sign(signer_payload);
MultiSignature::Sr25519(signature.0)
}
}
}
2 changes: 1 addition & 1 deletion crates/cesxt/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use subxt::{
};

pub use subxt::backend::legacy::rpc_methods::{Bytes, NumberOrHex as BlockNumberOrHex};
pub use subxt::ext::sp_core::storage::{StorageData, StorageKey};
pub use sp_core::storage::{StorageData, StorageKey};

pub struct ExtraRpcMethods<T> {
client: RpcClient,
Expand Down
5 changes: 3 additions & 2 deletions crates/rrsc/pallet/src/equivocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<T, R, P, L>
OffenceReportSystem<Option<T::AccountId>, (EquivocationProof<HeaderFor<T>>, T::KeyOwnerProof)>
for EquivocationReportSystem<T, R, P, L>
where
T: Config + pallet_authorship::Config + frame_system::offchain::SendTransactionTypes<Call<T>>,
T: Config + pallet_authorship::Config + frame_system::offchain::CreateInherent<Call<T>>,
R: ReportOffence<
T::AccountId,
P::IdentificationTuple,
Expand All @@ -132,7 +132,8 @@ where
equivocation_proof: Box::new(equivocation_proof),
key_owner_proof,
};
let res = SubmitTransaction::<T, Call<T>>::submit_unsigned_transaction(call.into());
let xt = T::create_inherent(call.into());
let res = SubmitTransaction::<T, Call<T>>::submit_transaction(xt);
match res {
Ok(_) => info!(target: LOG_TARGET, "Submitted equivocation report"),
Err(e) => error!(target: LOG_TARGET, "Error submitting equivocation report: {:?}", e),
Expand Down
4 changes: 0 additions & 4 deletions crates/rrsc/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,6 @@ mod vrf_solver;

#[cfg(any(feature = "runtime-benchmarks", test))]
mod benchmarking;
#[cfg(all(feature = "std", test))]
mod mock;
#[cfg(all(feature = "std", test))]
mod tests;

pub use equivocation::{EquivocationOffence, EquivocationReportSystem};
#[allow(deprecated)]
Expand Down
Loading
Loading