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

update precompile gas_limit #2007

Merged
merged 3 commits into from
Mar 31, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 13 additions & 11 deletions runtime/common/src/precompile/dex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use super::input::{Input, InputT, Output};
use crate::precompile::input::InputPricer;
use super::{
input::{Input, InputPricer, InputT, Output},
target_gas_limit,
};
use crate::WeightToGas;
use frame_support::{log, traits::Get};
use module_dex::WeightInfo;
Expand Down Expand Up @@ -68,7 +70,7 @@ where
Runtime::AccountId,
Runtime::AddressMapping,
<Runtime as module_dex::Config>::Erc20InfoMapping,
>::new(input, target_gas);
>::new(input, target_gas_limit(target_gas));

let gas_cost = Pricer::<Runtime>::cost(&input)?;

Expand Down Expand Up @@ -119,7 +121,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Dex get_liquidity_token_address failed".into(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down Expand Up @@ -149,7 +151,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Dex get_swap_target_amount failed".into(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down Expand Up @@ -179,7 +181,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Dex get_swap_supply_amount failed".into(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down Expand Up @@ -211,7 +213,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down Expand Up @@ -243,7 +245,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down Expand Up @@ -279,7 +281,7 @@ where
.map_err(|e| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down Expand Up @@ -315,7 +317,7 @@ where
.map_err(|e| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down Expand Up @@ -595,7 +597,7 @@ mod tests {
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "invalid currency id".into(),
cost: 10000,
cost: target_gas_limit(Some(10_000)).unwrap(),
}
);
});
Expand Down
29 changes: 14 additions & 15 deletions runtime/common/src/precompile/evm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,23 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use super::{
input::{Input, InputPricer, InputT, Output},
target_gas_limit,
weights::PrecompileWeights,
};
use crate::WeightToGas;
use module_evm::{
precompiles::Precompile,
runner::state::{PrecompileFailure, PrecompileOutput, PrecompileResult},
Context, ExitError, ExitRevert, ExitSucceed, WeightInfo,
};
use module_support::EVMManager;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use primitives::Balance;
use sp_runtime::{traits::Convert, RuntimeDebug};
use sp_std::{marker::PhantomData, prelude::*};

use module_support::EVMManager;

use super::{
input::{Input, InputT, Output},
weights::PrecompileWeights,
};
use crate::precompile::input::InputPricer;
use primitives::Balance;

/// The `EVM` impl precompile.
///
/// `input` data starts with `action`.
Expand Down Expand Up @@ -71,7 +69,8 @@ where
{
fn execute(input: &[u8], target_gas: Option<u64>, _context: &Context, _is_static: bool) -> PrecompileResult {
let input = Input::<Action, Runtime::AccountId, Runtime::AddressMapping, Runtime::Erc20InfoMapping>::new(
input, target_gas,
input,
target_gas_limit(target_gas),
);

let gas_cost = Pricer::<Runtime>::cost(&input)?;
Expand Down Expand Up @@ -112,7 +111,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
}
})?;

Expand Down Expand Up @@ -160,7 +159,7 @@ where
.map_err(|e| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand All @@ -177,7 +176,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
}
})?;

Expand All @@ -194,7 +193,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
}
})?;

Expand All @@ -211,7 +210,7 @@ where
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
}
})?;

Expand Down
4 changes: 4 additions & 0 deletions runtime/common/src/precompile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ pub const ORACLE: H160 = H160(hex!("0000000000000000000000000000000000000403"));
pub const SCHEDULER: H160 = H160(hex!("0000000000000000000000000000000000000404"));
pub const DEX: H160 = H160(hex!("0000000000000000000000000000000000000405"));

pub fn target_gas_limit(target_gas: Option<u64>) -> Option<u64> {
target_gas.map(|x| x.saturating_div(10).saturating_mul(9)) // 90%
}

pub struct AllPrecompiles<R> {
active: BTreeSet<H160>,
_marker: PhantomData<R>,
Expand Down
23 changes: 13 additions & 10 deletions runtime/common/src/precompile/multicurrency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use super::input::{Input, InputT, Output};
use crate::{precompile::input::InputPricer, WeightToGas};
use super::{
input::{Input, InputPricer, InputT, Output},
target_gas_limit,
};
use crate::WeightToGas;
use frame_support::{
log,
traits::{Currency, Get},
Expand Down Expand Up @@ -70,13 +73,13 @@ where
Runtime::AccountId,
<Runtime as module_evm::Config>::AddressMapping,
Runtime::Erc20InfoMapping,
>::new(input, target_gas);
>::new(input, target_gas_limit(target_gas));

let currency_id =
Runtime::Erc20InfoMapping::decode_evm_address(context.caller).ok_or_else(|| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "invalid currency id".into(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

let gas_cost = Pricer::<Runtime>::cost(&input, currency_id)?;
Expand All @@ -98,7 +101,7 @@ where
let name = Runtime::Erc20InfoMapping::name(currency_id).ok_or_else(|| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Get name failed".into(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;
log::debug!(target: "evm", "multicurrency: name: {:?}", name);

Expand All @@ -114,7 +117,7 @@ where
Runtime::Erc20InfoMapping::symbol(currency_id).ok_or_else(|| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Get symbol failed".into(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;
log::debug!(target: "evm", "multicurrency: symbol: {:?}", symbol);

Expand All @@ -130,7 +133,7 @@ where
Runtime::Erc20InfoMapping::decimals(currency_id).ok_or_else(|| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "Get decimals failed".into(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;
log::debug!(target: "evm", "multicurrency: decimals: {:?}", decimals);

Expand Down Expand Up @@ -185,7 +188,7 @@ where
.map_err(|e| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down Expand Up @@ -306,7 +309,7 @@ mod tests {
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "invalid currency id".into(),
cost: 10_000,
cost: target_gas_limit(Some(10_000)).unwrap(),
}
);
});
Expand Down Expand Up @@ -550,7 +553,7 @@ mod tests {
PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: "BalanceTooLow".into(),
cost: 100_000,
cost: target_gas_limit(Some(100_000)).unwrap(),
}
);
})
Expand Down
18 changes: 10 additions & 8 deletions runtime/common/src/precompile/nft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use super::{
input::{Input, InputT, Output},
target_gas_limit,
};
use frame_support::{
log,
traits::tokens::nonfungibles::{Inspect, Transfer},
Expand All @@ -26,16 +30,13 @@ use module_evm::{
Context, ExitError, ExitRevert, ExitSucceed,
};
use module_support::AddressMapping;
use num_enum::{IntoPrimitive, TryFromPrimitive};
use orml_traits::InspectExtended;
use primitives::nft::NFTBalance;
use sp_core::H160;
use sp_runtime::RuntimeDebug;
use sp_std::{marker::PhantomData, prelude::*};

use orml_traits::InspectExtended;

use super::input::{Input, InputT, Output};
use num_enum::{IntoPrimitive, TryFromPrimitive};
use primitives::nft::NFTBalance;

/// The `NFT` impl precompile.
///
/// `input` data starts with `action`.
Expand Down Expand Up @@ -64,7 +65,8 @@ where
{
fn execute(input: &[u8], target_gas: Option<u64>, _context: &Context, _is_static: bool) -> PrecompileResult {
let input = Input::<Action, Runtime::AccountId, Runtime::AddressMapping, Runtime::Erc20InfoMapping>::new(
input, target_gas,
input,
target_gas_limit(target_gas),
);

let gas_cost = Pricer::<Runtime>::cost(&input)?;
Expand Down Expand Up @@ -127,7 +129,7 @@ where
.map_err(|e| PrecompileFailure::Revert {
exit_status: ExitRevert::Reverted,
output: Into::<&str>::into(e).as_bytes().to_vec(),
cost: target_gas.unwrap_or_default(),
cost: target_gas_limit(target_gas).unwrap_or_default(),
})?;

Ok(PrecompileOutput {
Expand Down
Loading