Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Szegoo committed Jan 5, 2024
1 parent 05d3573 commit 3cf6efd
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 67 deletions.
41 changes: 21 additions & 20 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ registry = "registry.json"
[payment_info]
rpc_url = "wss://rpc.ibp.network/polkadot"
receiver = "126X27SbhrV19mBFawys3ovkyBS87SGfYwtwa8J2FjHrtbmA"
cost = 100
cost = "100"
6 changes: 0 additions & 6 deletions registry.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,5 @@
"rpc_url": "wss://acala-rpc.dwellir.com",
"para_id": 2000,
"relay_chain": "Polkadot"
},
{
"name": "Acala",
"rpc_url": "wss://acala-rpc.dwellir.com",
"para_id": 2005,
"relay_chain": "Polkadot"
}
]
3 changes: 3 additions & 0 deletions routes/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub enum Error {
PaymentRequired,
/// Failed to validate they payment.
PaymentValidationFailed,
/// The payment was not found in the specified block.
PaymentNotFound,
}

impl<'r> Responder<'r, 'static> for Error {
Expand All @@ -61,6 +63,7 @@ impl From<String> for Error {
"ConsumptionDataNotFound" => Self::ConsumptionDataNotFound,
"InvalidData" => Self::InvalidData,
"PaymentValidationFailed" => Self::PaymentValidationFailed,
"PaymentNotFound" => Self::PaymentNotFound,
_ => panic!("UnknownError"),
}
}
Expand Down
87 changes: 50 additions & 37 deletions routes/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ pub struct RegistrationData {
pub async fn register_para(registration_data: Json<RegistrationData>) -> Result<(), Error> {
let para = registration_data.para.clone();

log::info!(
target: LOG_TARGET,
"Attempting to register para: {}:{}",
para.relay_chain, para.para_id
);

let mut paras = registered_paras();

if registered_para(para.relay_chain.clone(), para.para_id).is_some() {
Expand Down Expand Up @@ -108,53 +114,60 @@ async fn validate_registration_payment(
payment_info: PaymentInfo,
receipt: Receipt,
) -> Result<(), Error> {
if let Ok(rpc_client) = RpcClient::from_url(&payment_info.rpc_url.clone()).await {
let params = rpc_params![Some(receipt.block_number)];
// TODO: ensure that the specified block is finalized.
let block_hash: H256 = rpc_client.request("chain_getBlockHash", params).await.unwrap();

let api = OnlineClient::<PolkadotConfig>::from_url(payment_info.rpc_url.clone())
.await
.unwrap();
let block = api.blocks().at(block_hash).await.unwrap();

let payment = opaque_payment_extrinsic(para, payment_info).await?;

let extrinsics = block.extrinsics().await.unwrap();
let extrinsics: Vec<Vec<u8>> = extrinsics
.iter()
.filter_map(|ext| {
ext.as_ref().ok().and_then(|e| e.as_root_extrinsic::<polkadot::Call>().ok())
})
.map(|ext| ext.encode())
.collect();

if extrinsics.contains(&payment.encode()) {
// Green light
} else {
// Red light
}

let rpc_client = RpcClient::from_url(&payment_info.rpc_url.clone())
.await
.map_err(|_| Error::PaymentValidationFailed)?;

let params = rpc_params![Some(receipt.block_number)];
// TODO: ensure that the specified block is finalized.
let block_hash: H256 = rpc_client.request("chain_getBlockHash", params).await.unwrap();

let api = OnlineClient::<PolkadotConfig>::from_url(payment_info.rpc_url.clone())
.await
.map_err(|_| Error::PaymentValidationFailed)?;

let block = api.blocks().at(block_hash).await.map_err(|_| Error::PaymentValidationFailed)?;
let payment = opaque_payment_extrinsic(para, payment_info).await?;

let extrinsics = block.extrinsics().await.unwrap();
let extrinsics: Vec<Vec<u8>> = extrinsics
.iter()
.filter_map(|ext| {
ext.as_ref().ok().and_then(|e| e.as_root_extrinsic::<polkadot::Call>().ok())
})
.map(|ext| ext.encode())
.collect();

if extrinsics.contains(&payment.encode()) {
Ok(())
} else {
Err(Error::PaymentValidationFailed)
Err(Error::PaymentNotFound)
}
}

async fn opaque_payment_extrinsic(
para: Parachain,
payment_info: PaymentInfo,
) -> Result<polkadot::Call, Error> {
let transfer_call = polkadot::Call::Balances(BalancesCall::transfer_keep_alive {
dest: payment_info.receiver.into(),
value: payment_info.cost as u128,
});
if let Ok(cost) = payment_info.cost.parse::<u128>() {
let transfer_call = polkadot::Call::Balances(BalancesCall::transfer_keep_alive {
dest: payment_info.receiver.into(),
value: cost,
});

let remark = format!("{}:{}", para.relay_chain, para.para_id).as_bytes().to_vec();
let remark_call = polkadot::Call::System(SystemCall::remark { remark });
let remark = format!("{}:{}", para.relay_chain, para.para_id).as_bytes().to_vec();
let remark_call = polkadot::Call::System(SystemCall::remark { remark });

let batch_call =
polkadot::Call::Utility(UtilityCall::batch_all { calls: vec![transfer_call, remark_call] });
let batch_call = polkadot::Call::Utility(UtilityCall::batch_all {
calls: vec![transfer_call, remark_call],
});

Ok(batch_call)
Ok(batch_call)
} else {
log::error!(
target: LOG_TARGET,
"Failed to parse cost",
);
Err(Error::PaymentValidationFailed)
}
}
2 changes: 1 addition & 1 deletion shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ license.workspace = true
[dependencies]
csv = "1.3.0"
log = "0.4"
toml = "0.5.8"
toml = "0.8.8"
serde = "1.0.193"
serde_json = "1.0.108"
subxt = "0.32.1"
Expand Down
5 changes: 3 additions & 2 deletions shared/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
// along with RegionX. If not, see <https://www.gnu.org/licenses/>.

use subxt::utils::AccountId32;
use types::Balance;

const CONFIG_FILE: &str = "config.toml";

Expand All @@ -25,7 +24,9 @@ pub struct PaymentInfo {
/// The account that the payment should be sent to.
pub receiver: AccountId32,
/// The cost of the payment.
pub cost: u64,
//
// Defined as a `String` since the `toml` crate has issues parsing `u128`.
pub cost: String,
}

#[derive(serde::Deserialize)]
Expand Down

0 comments on commit 3cf6efd

Please sign in to comment.