XCM Revamp Continued (#2865)

* Introduce plurality XCM locations

* Add RelayedFrom

* DMP dispatch weight handling.

* Add pallet for XCM sending, add routing logic.

* Update error types & doc

* Fix warnings.

* Fixes

* Fixes

* Fixes

* Bump Substrate

* Fixes

* Docs

* Docs

* Docs

* Fixes

* Fixes

* Fixes

* Update xcm/pallet-xcm/src/lib.rs

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>

* Docs

* Fixes

* Update lib.rs

* Fixes

Co-authored-by: Shawn Tabrizi <shawntabrizi@gmail.com>
This commit is contained in:
Gavin Wood
2021-04-09 20:34:28 +02:00
committed by GitHub
parent 8961628e7c
commit c9102c11a4
18 changed files with 632 additions and 227 deletions
@@ -14,10 +14,11 @@
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
use sp_std::marker::PhantomData;
use frame_support::traits::{Get, OriginTrait};
use xcm::v0::{MultiLocation, OriginKind, NetworkId, Junction};
use sp_std::{marker::PhantomData, convert::TryInto};
use xcm::v0::{MultiLocation, OriginKind, NetworkId, Junction, BodyId, BodyPart};
use xcm_executor::traits::{Convert, ConvertOrigin};
use frame_support::traits::{EnsureOrigin, Get, OriginTrait, GetBacking};
use frame_system::RawOrigin as SystemRawOrigin;
use polkadot_parachain::primitives::IsSystem;
/// Sovereign accounts use the system's `Signed` origin with an account ID derived from the
@@ -169,3 +170,87 @@ impl<
}
}
}
/// EnsureOrigin barrier to convert from dispatch origin to XCM origin, if one exists.
pub struct EnsureXcmOrigin<Origin, Conversion>(PhantomData<(Origin, Conversion)>);
impl<
Origin: OriginTrait + Clone,
Conversion: Convert<Origin, MultiLocation>,
> EnsureOrigin<Origin> for EnsureXcmOrigin<Origin, Conversion> where
Origin::PalletsOrigin: PartialEq,
{
type Success = MultiLocation;
fn try_origin(o: Origin) -> Result<Self::Success, Origin> {
let o = match Conversion::convert(o) {
Ok(location) => return Ok(location),
Err(o) => o,
};
// We institute a root fallback so root can always represent the context. This
// guarantees that `successful_origin` will work.
if o.caller() == Origin::root().caller() {
Ok(MultiLocation::Null)
} else {
Err(o)
}
}
#[cfg(feature = "runtime-benchmarks")]
fn successful_origin() -> Origin {
Origin::root()
}
}
/// `Convert` implementation to convert from some a `Signed` (system) `Origin` into an `AccountId32`.
///
/// Typically used when configuring `pallet-xcm` for allowing normal accounts to dispatch an XCM from an `AccountId32`
/// origin.
pub struct SignedToAccountId32<Origin, AccountId, Network>(
PhantomData<(Origin, AccountId, Network)>
);
impl<
Origin: OriginTrait + Clone,
AccountId: Into<[u8; 32]>,
Network: Get<NetworkId>,
> Convert<Origin, MultiLocation> for SignedToAccountId32<Origin, AccountId, Network> where
Origin::PalletsOrigin: From<SystemRawOrigin<AccountId>> +
TryInto<SystemRawOrigin<AccountId>, Error=Origin::PalletsOrigin>
{
fn convert(o: Origin) -> Result<MultiLocation, Origin> {
o.try_with_caller(|caller| match caller.try_into() {
Ok(SystemRawOrigin::Signed(who)) =>
Ok(Junction::AccountId32 { network: Network::get(), id: who.into() }.into()),
Ok(other) => Err(other.into()),
Err(other) => Err(other),
})
}
}
/// `Convert` implementation to convert from some an origin which implements `Backing` into a corresponding `Plurality`
/// MultiLocation.
///
/// Typically used when configuring `pallet-xcm` for allowing a collective's Origin to dispatch an XCM from a
/// `Plurality` origin.
pub struct BackingToPlurality<Origin, COrigin, Body>(
PhantomData<(Origin, COrigin, Body)>
);
impl<
Origin: OriginTrait + Clone,
COrigin: GetBacking,
Body: Get<BodyId>,
> Convert<Origin, MultiLocation> for BackingToPlurality<Origin, COrigin, Body> where
Origin::PalletsOrigin: From<COrigin> +
TryInto<COrigin, Error=Origin::PalletsOrigin>
{
fn convert(o: Origin) -> Result<MultiLocation, Origin> {
o.try_with_caller(|caller| match caller.try_into() {
Ok(co) => match co.get_backing() {
Some(backing) => Ok(Junction::Plurality {
id: Body::get(),
part: BodyPart::Fraction { nom: backing.approvals, denom: backing.eligible },
}.into()),
None => Err(co.into()),
}
Err(other) => Err(other),
})
}
}