Skip to content

Commit

Permalink
move calculate_gas_info into fn
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Sep 17, 2023
1 parent 628b434 commit 6dd74de
Showing 1 changed file with 115 additions and 134 deletions.
249 changes: 115 additions & 134 deletions pallets/gear/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,45 @@ where
))
})
}

fn calculate_gas_info(
&self,
at_hash: <Block as BlockT>::Hash,
source: H256,
kind: HandleKind,
payload: Vec<u8>,
value: u128,
allow_other_panics: bool,
min_limit: Option<u64>,
) -> RpcResult<GasInfo> {
let api_version = self.get_api_version(at_hash)?;

self.run_with_api_copy(|api| {
if api_version < 2 {
#[allow(deprecated)]
api.calculate_gas_info_before_version_2(
at_hash,
source,
kind,
payload,
value,
allow_other_panics,
min_limit,
)
} else {
api.calculate_gas_info(
at_hash,
source,
kind,
payload,
value,
allow_other_panics,
min_limit,
Some(self.allowance_multiplier),
)
}
})
}
}

/// Error type of this RPC api.
Expand Down Expand Up @@ -228,38 +267,23 @@ where
) -> RpcResult<GasInfo> {
let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);

let api_version = self.get_api_version(at_hash)?;

let calculate_gas_info = |min_limit: Option<u64>| {
self.run_with_api_copy(|api| {
if api_version < 2 {
#[allow(deprecated)]
api.calculate_gas_info_before_version_2(
at_hash,
source,
HandleKind::InitByHash(CodeId::from_origin(code_id)),
payload.to_vec(),
value,
allow_other_panics,
min_limit,
)
} else {
api.calculate_gas_info(
at_hash,
source,
HandleKind::InitByHash(CodeId::from_origin(code_id)),
payload.to_vec(),
value,
allow_other_panics,
min_limit,
Some(self.allowance_multiplier),
)
}
})
};

let GasInfo { min_limit, .. } = calculate_gas_info(None)?;
calculate_gas_info(Some(min_limit))
let GasInfo { min_limit, .. } = self.calculate_gas_info(
at_hash,
source,
HandleKind::InitByHash(CodeId::from_origin(code_id)),
payload.to_vec(),
value,
allow_other_panics,
None)?;

self.calculate_gas_info(
at_hash,
source,
HandleKind::InitByHash(CodeId::from_origin(code_id)),
payload.to_vec(),
value,
allow_other_panics,
Some(min_limit))
}

fn get_init_upload_gas_spent(
Expand All @@ -273,38 +297,23 @@ where
) -> RpcResult<GasInfo> {
let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);

let api_version = self.get_api_version(at_hash)?;

let calculate_gas_info = |min_limit: Option<u64>| {
self.run_with_api_copy(|api| {
if api_version < 2 {
#[allow(deprecated)]
api.calculate_gas_info_before_version_2(
at_hash,
source,
HandleKind::Init(code.to_vec()),
payload.to_vec(),
value,
allow_other_panics,
min_limit,
)
} else {
api.calculate_gas_info(
at_hash,
source,
HandleKind::Init(code.to_vec()),
payload.to_vec(),
value,
allow_other_panics,
min_limit,
Some(self.allowance_multiplier),
)
}
})
};

let GasInfo { min_limit, .. } = calculate_gas_info(None)?;
calculate_gas_info(Some(min_limit))
let GasInfo { min_limit, .. } = self.calculate_gas_info(
at_hash,
source,
HandleKind::Init(code.to_vec()),
payload.to_vec(),
value,
allow_other_panics,
None)?;

self.calculate_gas_info(
at_hash,
source,
HandleKind::Init(code.to_vec()),
payload.to_vec(),
value,
allow_other_panics,
Some(min_limit))
}

fn get_handle_gas_spent(
Expand All @@ -318,38 +327,25 @@ where
) -> RpcResult<GasInfo> {
let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);

let api_version = self.get_api_version(at_hash)?;

let calculate_gas_info = |min_limit: Option<u64>| {
self.run_with_api_copy(|api| {
if api_version < 2 {
#[allow(deprecated)]
api.calculate_gas_info_before_version_2(
at_hash,
source,
HandleKind::Handle(ProgramId::from_origin(dest)),
payload.to_vec(),
value,
allow_other_panics,
min_limit,
)
} else {
api.calculate_gas_info(
at_hash,
source,
HandleKind::Handle(ProgramId::from_origin(dest)),
payload.to_vec(),
value,
allow_other_panics,
min_limit,
Some(self.allowance_multiplier),
)
}
})
};

let GasInfo { min_limit, .. } = calculate_gas_info(None)?;
calculate_gas_info(Some(min_limit))
let GasInfo { min_limit, .. } = self.calculate_gas_info(
at_hash,
source,
HandleKind::Handle(ProgramId::from_origin(dest)),
payload.to_vec(),
value,
allow_other_panics,
None,
)?;

self.calculate_gas_info(
at_hash,
source,
HandleKind::Handle(ProgramId::from_origin(dest)),
payload.to_vec(),
value,
allow_other_panics,
Some(min_limit),
)
}

fn get_reply_gas_spent(
Expand All @@ -363,44 +359,29 @@ where
) -> RpcResult<GasInfo> {
let at_hash = at.unwrap_or_else(|| self.client.info().best_hash);

let api_version = self.get_api_version(at_hash)?;

let calculate_gas_info = |min_limit: Option<u64>| {
self.run_with_api_copy(|api| {
if api_version < 2 {
#[allow(deprecated)]
api.calculate_gas_info_before_version_2(
at_hash,
source,
HandleKind::Reply(
MessageId::from_origin(message_id),
ReplyCode::Success(SuccessReplyReason::Manual),
),
payload.to_vec(),
value,
allow_other_panics,
min_limit,
)
} else {
api.calculate_gas_info(
at_hash,
source,
HandleKind::Reply(
MessageId::from_origin(message_id),
ReplyCode::Success(SuccessReplyReason::Manual),
),
payload.to_vec(),
value,
allow_other_panics,
min_limit,
Some(self.allowance_multiplier),
)
}
})
};

let GasInfo { min_limit, .. } = calculate_gas_info(None)?;
calculate_gas_info(Some(min_limit))
let GasInfo { min_limit, .. } = self.calculate_gas_info(
at_hash,
source,
HandleKind::Reply(
MessageId::from_origin(message_id),
ReplyCode::Success(SuccessReplyReason::Manual),
),
payload.to_vec(),
value,
allow_other_panics,
None)?;

self.calculate_gas_info(
at_hash,
source,
HandleKind::Reply(
MessageId::from_origin(message_id),
ReplyCode::Success(SuccessReplyReason::Manual),
),
payload.to_vec(),
value,
allow_other_panics,
Some(min_limit))
}

fn read_state(
Expand Down

0 comments on commit 6dd74de

Please sign in to comment.