Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
1xstj committed Sep 12, 2024
1 parent 39eb4ac commit f4fcbdd
Show file tree
Hide file tree
Showing 5 changed files with 182 additions and 130 deletions.
55 changes: 55 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ members = [
"precompiles/verify-schnorr-signatures",
"precompiles/verify-bls381-signature",
"precompiles/multi-asset-delegation",
"precompiles/services",
"tangle-subxt",
"evm-tracer",
]
Expand Down
5 changes: 3 additions & 2 deletions precompiles/services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ sp-std = { workspace = true }
# Frontier
fp-evm = { workspace = true }
pallet-evm = { workspace = true, features = ["forbid-evm-reentrancy"] }

tangle-primitives = { workspace = true }
pallet-services = { workspace = true }

[dev-dependencies]
derive_more = { workspace = true }
Expand Down Expand Up @@ -57,5 +57,6 @@ std = [
"sp-runtime/std",
"sp-std/std",
"tangle-primitives/std",
"pallet-assets/std"
"pallet-assets/std",
"pallet-services/std",
]
249 changes: 122 additions & 127 deletions precompiles/services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,142 +3,137 @@
use fp_evm::PrecompileHandle;
use frame_support::dispatch::{DispatchResult, GetDispatchInfo, PostDispatchInfo};
use pallet_evm::AddressMapping;
use pallet_services::{Field, OperatorPreferences};
use parity_scale_codec::Decode;
use precompile_utils::prelude::*;
use sp_core::{H160, U256};
use sp_runtime::traits::Dispatchable;
use sp_std::{marker::PhantomData, vec::Vec};
use pallet_service_blueprint::{OperatorPreferences, Field};
use tangle_primitives::services::ServiceBlueprint;

/// Precompile for the `ServiceBlueprint` pallet.
pub struct ServiceBlueprintPrecompile<Runtime>(PhantomData<Runtime>);

#[precompile_utils::precompile]
impl<Runtime> ServiceBlueprintPrecompile<Runtime>
where
Runtime: pallet_service_blueprint::Config + pallet_evm::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
Runtime::RuntimeCall: From<pallet_service_blueprint::Call<Runtime>>,
Runtime: pallet_services::Config + pallet_evm::Config,
Runtime::RuntimeCall: Dispatchable<PostInfo = PostDispatchInfo> + GetDispatchInfo,
<Runtime::RuntimeCall as Dispatchable>::RuntimeOrigin: From<Option<Runtime::AccountId>>,
Runtime::RuntimeCall: From<pallet_services::Call<Runtime>>,
{
/// Create a new blueprint.
#[precompile::public("createBlueprint(bytes32)")]
fn create_blueprint(handle: &mut impl PrecompileHandle, blueprint_data: Vec<u8>) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let blueprint: ServiceBlueprint<Runtime::Constraints> =
codec::Decode::decode(&mut &blueprint_data[..])
.map_err(|_| revert("Invalid blueprint data"))?;

let call = pallet_service_blueprint::Call::<Runtime>::create_blueprint {
blueprint
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}

/// Register as an operator for a specific blueprint.
#[precompile::public("registerOperator(uint256,bytes)")]
fn register_operator(
handle: &mut impl PrecompileHandle,
blueprint_id: U256,
preferences: Vec<u8>,
registration_args: Vec<u8>,
) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let blueprint_id: u64 = blueprint_id.as_u64();
let preferences: OperatorPreferences =
codec::Decode::decode(&mut &preferences[..])
.map_err(|_| revert("Invalid preferences data"))?;

let registration_args: Vec<Field<Runtime::Constraints, Runtime::AccountId>> =
codec::Decode::decode(&mut &registration_args[..])
.map_err(|_| revert("Invalid registration arguments"))?;

let call = pallet_service_blueprint::Call::<Runtime>::register {
blueprint_id,
preferences,
registration_args,
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}

/// Unregister as an operator from a blueprint.
#[precompile::public("unregisterOperator(uint256)")]
fn unregister_operator(handle: &mut impl PrecompileHandle, blueprint_id: U256) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let blueprint_id: u64 = blueprint_id.as_u64();

let call = pallet_service_blueprint::Call::<Runtime>::unregister {
blueprint_id,
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}

/// Request a new service.
#[precompile::public("requestService(uint256,bytes,bytes,bytes)")]
fn request_service(
handle: &mut impl PrecompileHandle,
blueprint_id: U256,
permitted_callers_data: Vec<u8>,
service_providers_data: Vec<u8>,
request_args_data: Vec<u8>,
) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let blueprint_id: u64 = blueprint_id.as_u64();
let permitted_callers: Vec<Runtime::AccountId> =
codec::Decode::decode(&mut &permitted_callers_data[..])
.map_err(|_| revert("Invalid permitted callers data"))?;

let service_providers: Vec<Runtime::AccountId> =
codec::Decode::decode(&mut &service_providers_data[..])
.map_err(|_| revert("Invalid service providers data"))?;

let request_args: Vec<Field<Runtime::Constraints, Runtime::AccountId>> =
codec::Decode::decode(&mut &request_args_data[..])
.map_err(|_| revert("Invalid request arguments data"))?;

let call = pallet_service_blueprint::Call::<Runtime>::request {
blueprint_id,
permitted_callers,
service_providers,
ttl: 10000,
request_args,
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}

/// Terminate a service.
#[precompile::public("terminateService(uint256)")]
fn terminate_service(handle: &mut impl PrecompileHandle, service_id: U256) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let service_id: u64 = service_id.as_u64();

let call = pallet_service_blueprint::Call::<Runtime>::terminate {
service_id,
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}
}
/// Create a new blueprint.
#[precompile::public("createBlueprint(bytes32)")]
fn create_blueprint(handle: &mut impl PrecompileHandle, blueprint_data: Vec<u8>) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let blueprint: ServiceBlueprint<Runtime::Constraints> =
Decode::decode(&mut &blueprint_data[..])
.map_err(|_| revert("Invalid blueprint data"))?;

let call = pallet_services::Call::<Runtime>::create_blueprint { blueprint };

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}

/// Register as an operator for a specific blueprint.
#[precompile::public("registerOperator(uint256,bytes)")]
fn register_operator(
handle: &mut impl PrecompileHandle,
blueprint_id: U256,
preferences: Vec<u8>,
registration_args: Vec<u8>,
) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let blueprint_id: u64 = blueprint_id.as_u64();
let preferences: OperatorPreferences = Decode::decode(&mut &preferences[..])
.map_err(|_| revert("Invalid preferences data"))?;

let registration_args: Vec<Field<Runtime::Constraints, Runtime::AccountId>> =
Decode::decode(&mut &registration_args[..])
.map_err(|_| revert("Invalid registration arguments"))?;

let call = pallet_services::Call::<Runtime>::register {
blueprint_id,
preferences,
registration_args,
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}

/// Unregister as an operator from a blueprint.
#[precompile::public("unregisterOperator(uint256)")]
fn unregister_operator(handle: &mut impl PrecompileHandle, blueprint_id: U256) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let blueprint_id: u64 = blueprint_id.as_u64();

let call = pallet_services::Call::<Runtime>::unregister { blueprint_id };

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}

/// Request a new service.
#[precompile::public("requestService(uint256,bytes,bytes,bytes)")]
fn request_service(
handle: &mut impl PrecompileHandle,
blueprint_id: U256,
permitted_callers_data: Vec<u8>,
service_providers_data: Vec<u8>,
request_args_data: Vec<u8>,
) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let blueprint_id: u64 = blueprint_id.as_u64();
let permitted_callers: Vec<Runtime::AccountId> =
Decode::decode(&mut &permitted_callers_data[..])
.map_err(|_| revert("Invalid permitted callers data"))?;

let service_providers: Vec<Runtime::AccountId> =
Decode::decode(&mut &service_providers_data[..])
.map_err(|_| revert("Invalid service providers data"))?;

let request_args: Vec<Field<Runtime::Constraints, Runtime::AccountId>> =
Decode::decode(&mut &request_args_data[..])
.map_err(|_| revert("Invalid request arguments data"))?;

let call = pallet_services::Call::<Runtime>::request {
blueprint_id,
permitted_callers,
service_providers,
ttl: 10000_u32.into(),
request_args,
};

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}

/// Terminate a service.
#[precompile::public("terminateService(uint256)")]
fn terminate_service(handle: &mut impl PrecompileHandle, service_id: U256) -> EvmResult {
handle.record_cost(RuntimeHelper::<Runtime>::db_read_gas_cost())?;
let origin = Runtime::AddressMapping::into_account_id(handle.context().caller);

let service_id: u64 = service_id.as_u64();

let call = pallet_services::Call::<Runtime>::terminate { service_id };

RuntimeHelper::<Runtime>::try_dispatch(handle, Some(origin).into(), call)?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion precompiles/services/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::mock::*;
use crate::U256;
use frame_support::assert_ok;
use pallet_service_blueprint::{Blueprints, Operators, UserServices};
use pallet_services::{Blueprints, Operators, UserServices};
use precompile_utils::testing::*;
use sp_core::H160;

Expand Down

0 comments on commit f4fcbdd

Please sign in to comment.