Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suspend MantaPay when InternalLedgerError #977

Merged
merged 12 commits into from
Jan 17, 2023
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion pallets/manta-pay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,11 @@ manta-util = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.
[dev-dependencies]
lazy_static = "1.4.0"
manta-crypto = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.5.9", features = ["getrandom"] }
manta-pay = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.5.9", default-features = false, features = ["groth16", "parameters", "scale", "download", "test"] }
manta-pay = { git = "https://github.com/manta-network/manta-rs.git", tag = "v0.5.9", features = ["groth16", "parameters", "scale", "download", "test"] }
pallet-asset-manager = { path = "../asset-manager" }
pallet-assets = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.26" }
pallet-balances = { git = 'https://github.com/paritytech/substrate.git', branch = "polkadot-v0.9.26" }
pallet-tx-pause = { path = '../tx-pause' }
sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.26" }
sp-io = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.26" }
tempfile = "3.3.0"
Expand Down
102 changes: 86 additions & 16 deletions pallets/manta-pay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,17 @@ pub mod pallet {

const STORAGE_VERSION: StorageVersion = StorageVersion::new(1);

/// Suspend execution of MantaPay extrinsics (via tx-pause) due to unexpected internal ledger error.
/// Currently the following errors, which are only controlled by our own code, not user input:
/// ChecksumError, MerkleTreeCapacityError, VerifyingContextDecodeError
pub trait SuspendMantaPay {
fn suspend_manta_pay_execution();
}
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved

impl SuspendMantaPay for () {
fn suspend_manta_pay_execution() {}
}

/// Pallet
#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
Expand All @@ -146,6 +157,9 @@ pub mod pallet {

/// Pallet ID
type PalletId: Get<PalletId>;

/// Suspends execution of all extrinsics via TxPause
type Suspender: SuspendMantaPay;
}

/// Fungible Ledger Implementation for [`Config`]
Expand Down Expand Up @@ -474,7 +488,10 @@ pub mod pallet {
}
}

impl<T> From<SenderPostError<SenderLedgerError>> for Error<T> {
impl<T> From<SenderPostError<SenderLedgerError>> for Error<T>
where
T: Config,
{
#[inline]
fn from(err: SenderPostError<SenderLedgerError>) -> Self {
match err {
Expand All @@ -485,12 +502,24 @@ pub mod pallet {
}
}

impl<T> From<ReceiverPostError<ReceiverLedgerError>> for Error<T> {
impl<T> From<ReceiverPostError<ReceiverLedgerError<T>>> for Error<T>
where
T: Config,
{
#[inline]
fn from(err: ReceiverPostError<ReceiverLedgerError>) -> Self {
fn from(err: ReceiverPostError<ReceiverLedgerError<T>>) -> Self {
match err {
ReceiverPostError::AssetRegistered => Self::AssetRegistered,
ReceiverPostError::UnexpectedError(_) => Self::InternalLedgerError,
ReceiverPostError::UnexpectedError(e) => {
match e {
ReceiverLedgerError::ChecksumError
| ReceiverLedgerError::MerkleTreeCapacityError => {
T::Suspender::suspend_manta_pay_execution();
}
_ => {}
};
Self::InternalLedgerError
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand All @@ -505,12 +534,19 @@ pub mod pallet {
}
}

impl<T> From<ReceiverLedgerError> for TransferLedgerError<T>
impl<T> From<ReceiverLedgerError<T>> for TransferLedgerError<T>
where
T: Config,
{
#[inline]
fn from(err: ReceiverLedgerError) -> Self {
fn from(err: ReceiverLedgerError<T>) -> Self {
match err {
ReceiverLedgerError::ChecksumError
| ReceiverLedgerError::MerkleTreeCapacityError => {
T::Suspender::suspend_manta_pay_execution();
}
_ => {}
};
TransferLedgerError::ReceiverLedgerError(err)
}
}
Expand Down Expand Up @@ -541,7 +577,7 @@ pub mod pallet {
config::Config,
<T as frame_system::Config>::AccountId,
SenderLedgerError,
ReceiverLedgerError,
ReceiverLedgerError<T>,
TransferLedgerError<T>,
>;

Expand All @@ -561,7 +597,16 @@ pub mod pallet {
TransferPostError::<T>::DuplicateMint => Self::DuplicateRegister,
TransferPostError::<T>::DuplicateSpend => Self::DuplicateSpend,
TransferPostError::<T>::InvalidProof => Self::InvalidProof,
TransferPostError::<T>::UnexpectedError(_) => Self::InternalLedgerError,
TransferPostError::<T>::UnexpectedError(e) => {
match e {
TransferLedgerError::ChecksumError
| TransferLedgerError::VerifyingContextDecodeError(_) => {
T::Suspender::suspend_manta_pay_execution();
}
_ => {}
};
Self::InternalLedgerError
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
}
Expand Down Expand Up @@ -921,7 +966,10 @@ pub type UtxoItemHashError = codec::DecodeError<
>;

/// Receiver Ledger Error
pub enum ReceiverLedgerError {
pub enum ReceiverLedgerError<T>
where
T: Config,
{
/// Utxo Decoding Error
UtxoDecodeError(scale_codec::Error),

Expand Down Expand Up @@ -953,14 +1001,27 @@ pub enum ReceiverLedgerError {
///
/// The asset has already been registered with the ledger.
AssetRegistered,

/// Type Marker Parameter
Marker(PhantomData<T>),
Garandor marked this conversation as resolved.
Show resolved Hide resolved
}

impl From<ReceiverLedgerError> for ReceiverPostError<ReceiverLedgerError> {
impl<T> From<ReceiverLedgerError<T>> for ReceiverPostError<ReceiverLedgerError<T>>
where
T: Config,
{
#[inline]
fn from(value: ReceiverLedgerError) -> Self {
fn from(value: ReceiverLedgerError<T>) -> Self {
if let ReceiverLedgerError::AssetRegistered = value {
Self::AssetRegistered
} else {
match value {
ReceiverLedgerError::ChecksumError
| ReceiverLedgerError::MerkleTreeCapacityError => {
T::Suspender::suspend_manta_pay_execution();
}
_ => {}
};
Self::UnexpectedError(value)
}
}
Expand All @@ -972,7 +1033,7 @@ where
{
type SuperPostingKey = (Wrap<()>, ());
type ValidUtxo = Wrap<config::Utxo>;
type Error = ReceiverLedgerError;
type Error = ReceiverLedgerError<T>;

#[inline]
fn is_not_registered(&self, utxo: config::Utxo) -> Result<Self::ValidUtxo, Self::Error> {
Expand Down Expand Up @@ -1093,7 +1154,7 @@ where
ChecksumError,

/// Verifying Context Decoding Error
VerifiyingContextDecodeError(VerifyingContextError),
VerifyingContextDecodeError(VerifyingContextError),

/// Field Element Encoding Error
FpEncodeError(scale_codec::Error),
Expand All @@ -1108,7 +1169,7 @@ where
SenderLedgerError(SenderLedgerError),

/// Receiver Ledger Error
ReceiverLedgerError(ReceiverLedgerError),
ReceiverLedgerError(ReceiverLedgerError<T>),

/// Invalid Transfer Shape
InvalidTransferShape,
Expand Down Expand Up @@ -1137,7 +1198,16 @@ where
TransferLedgerError::SenderLedgerError(err) => Self::Sender(err.into()),
TransferLedgerError::ReceiverLedgerError(err) => Self::Receiver(err.into()),
ghzlatarev marked this conversation as resolved.
Show resolved Hide resolved
TransferLedgerError::UnknownAsset => Self::InvalidProof,
err => Self::UnexpectedError(err),
err => {
match err {
TransferLedgerError::ChecksumError
| TransferLedgerError::VerifyingContextDecodeError(_) => {
T::Suspender::suspend_manta_pay_execution();
}
_ => {}
};
Self::UnexpectedError(err)
}
}
}
}
Expand Down Expand Up @@ -1285,7 +1355,7 @@ where
let verification = posting_key
.has_valid_proof(
&config::VerifyingContext::decode(&mut verifying_context)
.map_err(TransferLedgerError::VerifiyingContextDecodeError)?,
.map_err(TransferLedgerError::VerifyingContextDecodeError)?,
)
.map_err(TransferLedgerError::ProofSystemError)?;
if verification {
Expand Down
48 changes: 41 additions & 7 deletions pallets/manta-pay/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
use frame_support::{
pallet_prelude::DispatchResult,
parameter_types,
traits::{ConstU32, Everything},
traits::{ConstU32, IsInVec},
PalletId,
};
use frame_system::EnsureRoot;
use frame_system::{EnsureRoot, RawOrigin};
use manta_primitives::{
assets::{
AssetConfig, AssetIdType, AssetLocation, AssetRegistry, AssetRegistryMetadata,
Expand Down Expand Up @@ -52,10 +52,11 @@ frame_support::construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
MantaPayPallet: crate::{Pallet, Call, Storage, Event<T>},
MantaPay: crate::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
Assets: pallet_assets::{Pallet, Storage, Event<T>},
AssetManager: pallet_asset_manager::{Pallet, Call, Storage, Event<T>},
TransactionPause: pallet_tx_pause::{Pallet, Storage, Call, Event<T>},
}
);

Expand All @@ -65,7 +66,7 @@ parameter_types! {
}

impl frame_system::Config for Test {
type BaseCallFilter = Everything;
type BaseCallFilter = frame_support::traits::Everything;
type BlockWeights = ();
type BlockLength = ();
type DbWeight = ();
Expand Down Expand Up @@ -251,6 +252,23 @@ impl pallet_asset_manager::Config for Test {
type WeightInfo = ();
}

pub struct MantaPaySuspensionManager;
impl crate::SuspendMantaPay for MantaPaySuspensionManager {
fn suspend_manta_pay_execution() {
let _ = TransactionPause::pause_transactions(
RawOrigin::Root.into(),
vec![(
b"MantaPay".to_vec(),
vec![
b"to_private".to_vec(),
b"private_transfer".to_vec(),
b"to_public".to_vec(),
],
)],
);
}
}

parameter_types! {
pub const MantaPayPalletId: PalletId = MANTA_PAY_PALLET_ID;
}
Expand All @@ -260,11 +278,27 @@ impl crate::Config for Test {
type WeightInfo = crate::weights::SubstrateWeight<Self>;
type PalletId = MantaPayPalletId;
type AssetConfig = MantaAssetConfig;
type Suspender = MantaPaySuspensionManager;
}

parameter_types! {
pub NonPausablePallets: Vec<Vec<u8>> = vec![b"Democracy".to_vec(), b"Balances".to_vec(), b"Council".to_vec(), b"CouncilCollective".to_vec(), b"TechnicalCommittee".to_vec(), b"TechnicalCollective".to_vec()];
}

impl pallet_tx_pause::Config for Test {
type Event = Event;
type Call = Call;
type MaxCallNames = ConstU32<25>;
type PauseOrigin = EnsureRoot<AccountId32>;
type UnpauseOrigin = EnsureRoot<AccountId32>;
type NonPausablePallets = IsInVec<NonPausablePallets>;
type WeightInfo = ();
}

pub fn new_test_ext() -> sp_io::TestExternalities {
frame_system::GenesisConfig::default()
let t = frame_system::GenesisConfig::default()
.build_storage::<Test>()
.unwrap()
.into()
.unwrap();

t.into()
}
Loading