Skip to content

Commit d5755b5

Browse files
TarekkMAlibreloisAgusrodriRomarQ
authored
Refund pov gas based on measured proof size usage (#1613)
* refund pov gas based on measured proof size usage (#230) Co-authored-by: Agusrodri <agusrodriguez2456@gmail.com> * fix * fix * Enable ProofSizeExt when estimating gas and emit ReportRefund when PoV gas is the effective gas * fix style * enable proof recording for template * fix style * fix * add to host functions * style * revert meter.rs * update cargo.lock * use cargo.lock from master then update --------- Co-authored-by: Éloïs <c@elo.tf> Co-authored-by: Agusrodri <agusrodriguez2456@gmail.com> Co-authored-by: Rodrigo Quelhas <22591718+RomarQ@users.noreply.github.com>
1 parent 0e7c6cd commit d5755b5

File tree

12 files changed

+725
-447
lines changed

12 files changed

+725
-447
lines changed

Cargo.lock

Lines changed: 456 additions & 328 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,7 @@ sp-std = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2
129129
sp-storage = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
130130
sp-timestamp = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
131131
sp-transaction-pool = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
132+
sp-trie = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
132133
sp-version = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
133134
sp-weights = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
134135
# Substrate FRAME
@@ -155,6 +156,10 @@ substrate-frame-rpc-system = { git = "https://github.com/paritytech/polkadot-sdk
155156
substrate-test-runtime-client = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412" }
156157
substrate-wasm-builder = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412" }
157158

159+
# Cumulus primitives
160+
cumulus-primitives-proof-size-hostfunction = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
161+
cumulus-primitives-storage-weight-reclaim = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
162+
158163
# XCM
159164
xcm = { package = "staging-xcm", git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
160165

client/rpc/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ sp-runtime = { workspace = true, features = ["default"] }
5252
sp-state-machine = { workspace = true, features = ["default"] }
5353
sp-storage = { workspace = true, features = ["default"] }
5454
sp-timestamp = { workspace = true, features = ["default"], optional = true }
55+
sp-trie = { workspace = true, features = ["default"] }
5556
# Frontier
5657
fc-api = { workspace = true }
5758
fc-mapping-sync = { workspace = true }

client/rpc/src/eth/execute.rs

Lines changed: 105 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ where
105105
)
106106
};
107107

108-
let (substrate_hash, api) = match frontier_backend_client::native_block_id::<B, C>(
108+
let (substrate_hash, mut api) = match frontier_backend_client::native_block_id::<B, C>(
109109
self.client.as_ref(),
110110
self.backend.as_ref(),
111111
number_or_hash,
@@ -127,6 +127,12 @@ where
127127
}
128128
};
129129

130+
// Enable proof size recording
131+
api.record_proof();
132+
let recorder: sp_trie::recorder::Recorder<HashingFor<B>> = Default::default();
133+
let ext = sp_trie::proof_size_extension::ProofSizeExt::new(recorder.clone());
134+
api.register_extension(ext);
135+
130136
let api_version = if let Ok(Some(api_version)) =
131137
api.api_version::<dyn EthereumRuntimeRPCApi<B>>(substrate_hash)
132138
{
@@ -238,14 +244,21 @@ where
238244
api_version,
239245
state_overrides,
240246
)?;
247+
248+
// Enable proof size recording
249+
let recorder: sp_trie::recorder::Recorder<HashingFor<B>> = Default::default();
250+
let ext = sp_trie::proof_size_extension::ProofSizeExt::new(recorder.clone());
251+
let mut exts = Extensions::new();
252+
exts.register(ext);
253+
241254
let params = CallApiAtParams {
242255
at: substrate_hash,
243256
function: "EthereumRuntimeRPCApi_call",
244257
arguments: encoded_params,
245258
overlayed_changes: &RefCell::new(overlayed_changes),
246259
call_context: CallContext::Offchain,
247-
recorder: &None,
248-
extensions: &RefCell::new(Extensions::new()),
260+
recorder: &Some(recorder),
261+
extensions: &RefCell::new(exts),
249262
};
250263

251264
let value = if api_version == 4 {
@@ -636,27 +649,56 @@ where
636649
(info.exit_reason, info.value, info.used_gas)
637650
} else {
638651
// Post-london + access list support
639-
let access_list = access_list.unwrap_or_default();
640-
let info = api.call(
641-
substrate_hash,
642-
from.unwrap_or_default(),
643-
to,
644-
data,
645-
value.unwrap_or_default(),
646-
gas_limit,
647-
max_fee_per_gas,
648-
max_priority_fee_per_gas,
649-
None,
650-
estimate_mode,
651-
Some(
652+
let encoded_params = Encode::encode(&(
653+
&from.unwrap_or_default(),
654+
&to,
655+
&data,
656+
&value.unwrap_or_default(),
657+
&gas_limit,
658+
&max_fee_per_gas,
659+
&max_priority_fee_per_gas,
660+
&None::<Option<U256>>,
661+
&estimate_mode,
662+
&Some(
652663
access_list
664+
.unwrap_or_default()
653665
.into_iter()
654666
.map(|item| (item.address, item.storage_keys))
655-
.collect(),
667+
.collect::<Vec<(sp_core::H160, Vec<H256>)>>(),
656668
),
657-
)
658-
.map_err(|err| internal_err(format!("runtime error: {err}")))?
659-
.map_err(|err| internal_err(format!("execution fatal: {err:?}")))?;
669+
));
670+
671+
// Proof size recording
672+
let recorder: sp_trie::recorder::Recorder<HashingFor<B>> = Default::default();
673+
let ext = sp_trie::proof_size_extension::ProofSizeExt::new(recorder.clone());
674+
let mut exts = Extensions::new();
675+
exts.register(ext);
676+
677+
let params = CallApiAtParams {
678+
at: substrate_hash,
679+
function: "EthereumRuntimeRPCApi_call",
680+
arguments: encoded_params,
681+
overlayed_changes: &RefCell::new(Default::default()),
682+
call_context: CallContext::Offchain,
683+
recorder: &Some(recorder),
684+
extensions: &RefCell::new(exts),
685+
};
686+
687+
let info = self
688+
.client
689+
.call_api_at(params)
690+
.and_then(|r| {
691+
Result::map_err(
692+
<Result<ExecutionInfoV2::<Vec<u8>>, DispatchError> as Decode>::decode(&mut &r[..]),
693+
|error| sp_api::ApiError::FailedToDecodeReturnValue {
694+
function: "EthereumRuntimeRPCApi_call",
695+
error,
696+
raw: r
697+
},
698+
)
699+
})
700+
.map_err(|err| internal_err(format!("runtime error: {err}")))?
701+
.map_err(|err| internal_err(format!("execution fatal: {err:?}")))?;
660702

661703
(info.exit_reason, info.value, info.used_gas.effective)
662704
}
@@ -724,24 +766,53 @@ where
724766
(info.exit_reason, Vec::new(), info.used_gas)
725767
} else {
726768
// Post-london + access list support
727-
let access_list = access_list.unwrap_or_default();
728-
let info = api.create(
729-
substrate_hash,
730-
from.unwrap_or_default(),
731-
data,
732-
value.unwrap_or_default(),
733-
gas_limit,
734-
max_fee_per_gas,
735-
max_priority_fee_per_gas,
736-
None,
737-
estimate_mode,
738-
Some(
769+
let encoded_params = Encode::encode(&(
770+
&from.unwrap_or_default(),
771+
&data,
772+
&value.unwrap_or_default(),
773+
&gas_limit,
774+
&max_fee_per_gas,
775+
&max_priority_fee_per_gas,
776+
&None::<Option<U256>>,
777+
&estimate_mode,
778+
&Some(
739779
access_list
780+
.unwrap_or_default()
740781
.into_iter()
741782
.map(|item| (item.address, item.storage_keys))
742-
.collect(),
783+
.collect::<Vec<(sp_core::H160, Vec<H256>)>>(),
743784
),
744-
)
785+
));
786+
787+
// Enable proof size recording
788+
let recorder: sp_trie::recorder::Recorder<HashingFor<B>> = Default::default();
789+
let ext = sp_trie::proof_size_extension::ProofSizeExt::new(recorder.clone());
790+
let mut exts = Extensions::new();
791+
exts.register(ext);
792+
793+
let params = CallApiAtParams {
794+
at: substrate_hash,
795+
function: "EthereumRuntimeRPCApi_create",
796+
arguments: encoded_params,
797+
overlayed_changes: &RefCell::new(Default::default()),
798+
call_context: CallContext::Offchain,
799+
recorder: &Some(recorder),
800+
extensions: &RefCell::new(exts),
801+
};
802+
803+
let info = self
804+
.client
805+
.call_api_at(params)
806+
.and_then(|r| {
807+
Result::map_err(
808+
<Result<ExecutionInfoV2::<H160>, DispatchError> as Decode>::decode(&mut &r[..]),
809+
|error| sp_api::ApiError::FailedToDecodeReturnValue {
810+
function: "EthereumRuntimeRPCApi_create",
811+
error,
812+
raw: r
813+
},
814+
)
815+
})
745816
.map_err(|err| internal_err(format!("runtime error: {err}")))?
746817
.map_err(|err| internal_err(format!("execution fatal: {err:?}")))?;
747818

frame/evm/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ impl-trait-for-tuples = "0.2.3"
2020
log = { workspace = true }
2121
scale-codec = { workspace = true }
2222
scale-info = { workspace = true }
23+
24+
# Cumulus
25+
cumulus-primitives-storage-weight-reclaim = { workspace = true, default-features = false }
26+
2327
# Substrate
2428
frame-benchmarking = { workspace = true, optional = true }
2529
frame-support = { workspace = true }
@@ -48,6 +52,8 @@ std = [
4852
"log/std",
4953
"scale-codec/std",
5054
"scale-info/std",
55+
# Cumulus
56+
"cumulus-primitives-storage-weight-reclaim/std",
5157
# Substrate
5258
"frame-benchmarking?/std",
5359
"frame-support/std",

0 commit comments

Comments
 (0)