Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
refactor: Refactor self contained call in runtime (#23)
Browse files Browse the repository at this point in the history
Co-authored-by: Jungyong Um <[email protected]>
  • Loading branch information
code0xff and Jungyong Um authored Jun 17, 2024
1 parent 03b7fa2 commit e37b180
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 67 deletions.
4 changes: 2 additions & 2 deletions client/rpc/src/cosm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ where
.runtime_api()
.convert_tx(block_hash, tx_bytes.to_vec(), chain_id.as_bytes().to_vec())
.map_err(|_| internal_err("cannot access runtime api"))?;
let tx_hash = sha2_256(&tx_bytes);
let tx_hash = H256(sha2_256(&tx_bytes));
self.pool
.submit_one(block_hash, TransactionSource::Local, extrinsic)
.map_ok(move |_| H256(tx_hash))
.map_ok(move |_| tx_hash)
.map_err(|e| internal_err(e.to_string()))
.await
}
Expand Down
2 changes: 1 addition & 1 deletion template/runtime/src/compat/cosm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use sp_core::{ecdsa, Hasher, H160, H256};
use sp_std::marker::PhantomData;

/// Hashed address mapping.
pub struct HashedAddressMapping<T, H>(PhantomData<T>, PhantomData<H>);
pub struct HashedAddressMapping<T, H>(PhantomData<(T, H)>);

impl<T, H> AddressMapping<T::AccountId> for HashedAddressMapping<T, H>
where
Expand Down
86 changes: 22 additions & 64 deletions template/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use frame_support::{
IdentityFee, Weight,
},
};
use hp_cosmos::{PublicKey, SignerPublicKey};
use hp_crypto::EcdsaExt;
use pallet_cosmos::handler::cosm::MsgHandler;
use pallet_grandpa::{
Expand All @@ -58,7 +59,6 @@ use sp_runtime::{
},
transaction_validity::{
InvalidTransaction, TransactionSource, TransactionValidity, TransactionValidityError,
UnknownTransaction,
},
ApplyExtrinsicResult, ExtrinsicInclusionMode, Perbill,
};
Expand Down Expand Up @@ -393,38 +393,22 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {

fn check_self_contained(&self) -> Option<Result<Self::SignedInfo, TransactionValidityError>> {
match self {
RuntimeCall::Cosmos(call) => {
if let pallet_cosmos::Call::transact { tx_bytes, chain_id } = call {
let tx = hp_io::decode_tx::decode(tx_bytes, chain_id).unwrap();

let check = || {
if let Some(hp_cosmos::SignerPublicKey::Single(
hp_cosmos::PublicKey::Secp256k1(pk),
)) = tx.auth_info.signer_infos[0].public_key
{
if hp_io::crypto::secp256k1_ecdsa_verify(
&tx.signatures[0],
tx.hash.as_bytes(),
&pk,
) {
Ok(Self::SignedInfo::from(pk))
} else {
Err(InvalidTransaction::Custom(
hp_cosmos::error::TransactionValidationError::InvalidSignature
as u8,
))?
}
RuntimeCall::Cosmos(call) => match call.check_self_contained()? {
Ok(_) =>
if let pallet_cosmos::Call::transact { tx_bytes, chain_id } = call {
let tx = hp_io::decode_tx::decode(tx_bytes, chain_id)?;
let public_key = tx.auth_info.signer_infos.first()?.clone().public_key?;
if let SignerPublicKey::Single(PublicKey::Secp256k1(pk)) = public_key {
Some(Ok(Self::SignedInfo::from(pk)))
} else {
Err(InvalidTransaction::Custom(
hp_cosmos::error::TransactionValidationError::UnsupportedSignerType
as u8,
))?
Some(Err(TransactionValidityError::Invalid(
InvalidTransaction::BadProof,
)))
}
};
Some(check())
} else {
None
}
} else {
None
},
Err(e) => Some(Err(e)),
},
_ => None,
}
Expand All @@ -437,21 +421,8 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
len: usize,
) -> Option<TransactionValidity> {
match self {
RuntimeCall::Cosmos(call) => {
if let pallet_cosmos::Call::transact { tx_bytes, chain_id } = &call {
let tx = hp_io::decode_tx::decode(tx_bytes, chain_id).unwrap();

if tx.auth_info.signer_infos[0].sequence == 0 &&
Runtime::migrate_cosm_account(&info.to_cosm_address().unwrap(), info)
.is_err()
{
return Some(Err(TransactionValidityError::Unknown(
UnknownTransaction::CannotLookup,
)));
}
}
call.validate_self_contained(&info.to_cosm_address().unwrap(), dispatch_info, len)
},
RuntimeCall::Cosmos(call) =>
call.validate_self_contained(&info.to_cosm_address().unwrap(), dispatch_info, len),
_ => None,
}
}
Expand All @@ -463,24 +434,11 @@ impl fp_self_contained::SelfContainedCall for RuntimeCall {
len: usize,
) -> Option<Result<(), TransactionValidityError>> {
match self {
RuntimeCall::Cosmos(call) => {
if let pallet_cosmos::Call::transact { tx_bytes, chain_id } = &call {
let tx = hp_io::decode_tx::decode(tx_bytes, chain_id).unwrap();
if tx.auth_info.signer_infos[0].sequence == 0 &&
Runtime::migrate_cosm_account(&info.to_cosm_address().unwrap(), info)
.is_err()
{
return Some(Err(TransactionValidityError::Unknown(
UnknownTransaction::CannotLookup,
)));
}
}
call.pre_dispatch_self_contained(
&info.to_cosm_address().unwrap(),
dispatch_info,
len,
)
},
RuntimeCall::Cosmos(call) => call.pre_dispatch_self_contained(
&info.to_cosm_address().unwrap(),
dispatch_info,
len,
),
_ => None,
}
}
Expand Down

0 comments on commit e37b180

Please sign in to comment.