Skip to content

Commit 11a8222

Browse files
committed
feat(api): add transfer_from benchmark
1 parent ad9d125 commit 11a8222

File tree

3 files changed

+113
-42
lines changed

3 files changed

+113
-42
lines changed

pallets/api-vnext/src/fungibles/benchmarking.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use frame_support::{
1212
traits::{
1313
fungible::{Inspect, Mutate},
1414
fungibles::{
15-
approvals::{self, Inspect as _},
15+
approvals::{self, Inspect as _, Mutate as _},
1616
metadata::{self, Inspect as _},
1717
Create, Inspect as _, Mutate as _,
1818
},
@@ -92,6 +92,42 @@ mod benchmarks {
9292
assert_eq!(<Assets<T, I>>::balance(token, &to), value);
9393
}
9494

95+
#[benchmark]
96+
fn transfer_from() {
97+
let owner = <AddressMapper<T>>::to_account_id(&ALICE_ADDR);
98+
let token = super::create::<T, I>(owner.clone());
99+
let spender = <AddressMapper<T>>::to_account_id(&BOB_ADDR);
100+
let to = <AddressMapper<T>>::to_account_id(&CHARLIE_ADDR);
101+
let value: AssetsBalance<T, I> = u32::MAX.into();
102+
103+
let mut call_setup = set_up_call();
104+
call_setup.set_origin(Origin::Signed(spender.clone()));
105+
let mut ext = call_setup.ext().0;
106+
let input = IFungiblesCalls::transferFrom(IFungibles::transferFromCall {
107+
token: token.clone().into(),
108+
from: <AddressMapper<T>>::to_address(&owner).0.into(),
109+
to: <AddressMapper<T>>::to_address(&to).0.into(),
110+
value: alloy::U256::from(value),
111+
});
112+
113+
<Assets<T, I>>::set_balance(token.clone(), &owner, value);
114+
assert_eq!(<Assets<T, I>>::balance(token.clone(), &owner), value);
115+
assert_eq!(<Assets<T, I>>::balance(token.clone(), &spender), 0u8.into());
116+
assert_eq!(<Assets<T, I>>::balance(token.clone(), &to), 0u8.into());
117+
assert_ok!(<Assets<T, I>>::approve(token.clone(), &owner, &spender, value));
118+
assert_eq!(<Assets<T, I>>::allowance(token.clone(), &owner, &spender), value);
119+
120+
#[block]
121+
{
122+
assert_ok!(call_precompile::<Fungibles<T, I>, _, ()>(&mut ext, &ADDRESS, &input));
123+
}
124+
125+
assert_eq!(<Assets<T, I>>::balance(token.clone(), &owner), 0u8.into());
126+
assert_eq!(<Assets<T, I>>::balance(token.clone(), &spender), 0u8.into());
127+
assert_eq!(<Assets<T, I>>::balance(token.clone(), &to), value);
128+
assert_eq!(<Assets<T, I>>::allowance(token, &owner, &spender), 0u8.into());
129+
}
130+
95131
// Parameter:
96132
// - 'a': whether `approve_transfer` is required.
97133
// - 'c': whether `cancel_approval` is required.

pallets/api-vnext/src/fungibles/precompiles/mod.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ where
6767
Ok(transferCall::abi_encode_returns(&transferReturn {}))
6868
},
6969
IFungiblesCalls::transferFrom(transferFromCall { token, from, to, value }) => {
70-
// TODO: charge based on benchmarked weight
70+
env.charge(<T as Config<I>>::WeightInfo::transfer_from())?;
7171

7272
transfer_from::<T, I>(
7373
to_runtime_origin(env.caller()),
@@ -77,12 +77,8 @@ where
7777
value.saturating_to(),
7878
)?;
7979

80-
let value = *value;
81-
deposit_event(
82-
env,
83-
address,
84-
Transfer { token: *token, from: *from, to: *to, value },
85-
);
80+
let event = Transfer { token: *token, from: *from, to: *to, value: *value };
81+
deposit_event(env, address, event);
8682
Ok(transferFromCall::abi_encode_returns(&transferFromReturn {}))
8783
},
8884
IFungiblesCalls::approve(approveCall { token, spender, value }) => {

pallets/api-vnext/src/fungibles/weights.rs

Lines changed: 73 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use core::marker::PhantomData;
3232
/// Weight functions needed for `pallet_api_vnext::fungibles`.
3333
pub trait WeightInfo {
3434
fn transfer() -> Weight;
35+
fn transfer_from() -> Weight;
3536
fn approve(a: u32, c: u32, ) -> Weight;
3637
fn create() -> Weight;
3738
fn start_destroy() -> Weight;
@@ -64,10 +65,29 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
6465
// Measured: `781`
6566
// Estimated: `6208`
6667
// Minimum execution time: 47_000_000 picoseconds.
67-
Weight::from_parts(48_000_000, 6208)
68+
Weight::from_parts(49_000_000, 6208)
6869
.saturating_add(T::DbWeight::get().reads(5_u64))
6970
.saturating_add(T::DbWeight::get().writes(4_u64))
7071
}
72+
/// Storage: `Revive::OriginalAccount` (r:2 w:0)
73+
/// Proof: `Revive::OriginalAccount` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
74+
/// Storage: `Assets::Asset` (r:1 w:1)
75+
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
76+
/// Storage: `Assets::Approvals` (r:1 w:1)
77+
/// Proof: `Assets::Approvals` (`max_values`: None, `max_size`: Some(148), added: 2623, mode: `MaxEncodedLen`)
78+
/// Storage: `Assets::Account` (r:2 w:2)
79+
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
80+
/// Storage: `System::Account` (r:1 w:1)
81+
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
82+
fn transfer_from() -> Weight {
83+
// Proof Size summary in bytes:
84+
// Measured: `951`
85+
// Estimated: `6208`
86+
// Minimum execution time: 68_000_000 picoseconds.
87+
Weight::from_parts(69_000_000, 6208)
88+
.saturating_add(T::DbWeight::get().reads(7_u64))
89+
.saturating_add(T::DbWeight::get().writes(5_u64))
90+
}
7191
/// Storage: `Revive::OriginalAccount` (r:1 w:0)
7292
/// Proof: `Revive::OriginalAccount` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
7393
/// Storage: `Assets::Approvals` (r:1 w:1)
@@ -81,11 +101,11 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
81101
// Measured: `699 + c * (140 ±0)`
82102
// Estimated: `3675`
83103
// Minimum execution time: 27_000_000 picoseconds.
84-
Weight::from_parts(6_576_530, 3675)
85-
// Standard Error: 127_016
86-
.saturating_add(Weight::from_parts(22_054_081, 0).saturating_mul(a.into()))
87-
// Standard Error: 127_016
88-
.saturating_add(Weight::from_parts(36_419_387, 0).saturating_mul(c.into()))
104+
Weight::from_parts(7_862_244, 3675)
105+
// Standard Error: 43_634
106+
.saturating_add(Weight::from_parts(20_137_755, 0).saturating_mul(a.into()))
107+
// Standard Error: 43_634
108+
.saturating_add(Weight::from_parts(35_150_000, 0).saturating_mul(c.into()))
89109
.saturating_add(T::DbWeight::get().reads(3_u64))
90110
.saturating_add(T::DbWeight::get().writes(2_u64))
91111
}
@@ -99,8 +119,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
99119
// Proof Size summary in bytes:
100120
// Measured: `471`
101121
// Estimated: `3675`
102-
// Minimum execution time: 33_000_000 picoseconds.
103-
Weight::from_parts(34_000_000, 3675)
122+
// Minimum execution time: 32_000_000 picoseconds.
123+
Weight::from_parts(33_000_000, 3675)
104124
.saturating_add(T::DbWeight::get().reads(3_u64))
105125
.saturating_add(T::DbWeight::get().writes(2_u64))
106126
}
@@ -123,8 +143,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
123143
// Proof Size summary in bytes:
124144
// Measured: `505`
125145
// Estimated: `3675`
126-
// Minimum execution time: 14_000_000 picoseconds.
127-
Weight::from_parts(15_000_000, 3675)
146+
// Minimum execution time: 15_000_000 picoseconds.
147+
Weight::from_parts(16_000_000, 3675)
128148
.saturating_add(T::DbWeight::get().reads(2_u64))
129149
.saturating_add(T::DbWeight::get().writes(1_u64))
130150
}
@@ -137,7 +157,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
137157
// Measured: `645`
138158
// Estimated: `3675`
139159
// Minimum execution time: 29_000_000 picoseconds.
140-
Weight::from_parts(30_000_000, 3675)
160+
Weight::from_parts(29_000_000, 3675)
141161
.saturating_add(T::DbWeight::get().reads(2_u64))
142162
.saturating_add(T::DbWeight::get().writes(1_u64))
143163
}
@@ -153,8 +173,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
153173
// Proof Size summary in bytes:
154174
// Measured: `584`
155175
// Estimated: `3675`
156-
// Minimum execution time: 28_000_000 picoseconds.
157-
Weight::from_parts(30_000_000, 3675)
176+
// Minimum execution time: 27_000_000 picoseconds.
177+
Weight::from_parts(28_000_000, 3675)
158178
.saturating_add(T::DbWeight::get().reads(4_u64))
159179
.saturating_add(T::DbWeight::get().writes(3_u64))
160180
}
@@ -170,8 +190,8 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
170190
// Proof Size summary in bytes:
171191
// Measured: `744`
172192
// Estimated: `3675`
173-
// Minimum execution time: 38_000_000 picoseconds.
174-
Weight::from_parts(39_000_000, 3675)
193+
// Minimum execution time: 40_000_000 picoseconds.
194+
Weight::from_parts(41_000_000, 3675)
175195
.saturating_add(T::DbWeight::get().reads(4_u64))
176196
.saturating_add(T::DbWeight::get().writes(3_u64))
177197
}
@@ -182,7 +202,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
182202
// Measured: `300`
183203
// Estimated: `3675`
184204
// Minimum execution time: 5_000_000 picoseconds.
185-
Weight::from_parts(6_000_000, 3675)
205+
Weight::from_parts(5_000_000, 3675)
186206
.saturating_add(T::DbWeight::get().reads(1_u64))
187207
}
188208
/// Storage: `Revive::OriginalAccount` (r:1 w:0)
@@ -193,7 +213,7 @@ impl<T: frame_system::Config> WeightInfo for SubstrateWeight<T> {
193213
// Proof Size summary in bytes:
194214
// Measured: `346`
195215
// Estimated: `3599`
196-
// Minimum execution time: 8_000_000 picoseconds.
216+
// Minimum execution time: 7_000_000 picoseconds.
197217
Weight::from_parts(8_000_000, 3599)
198218
.saturating_add(T::DbWeight::get().reads(2_u64))
199219
}
@@ -266,10 +286,29 @@ impl WeightInfo for () {
266286
// Measured: `781`
267287
// Estimated: `6208`
268288
// Minimum execution time: 47_000_000 picoseconds.
269-
Weight::from_parts(48_000_000, 6208)
289+
Weight::from_parts(49_000_000, 6208)
270290
.saturating_add(RocksDbWeight::get().reads(5_u64))
271291
.saturating_add(RocksDbWeight::get().writes(4_u64))
272292
}
293+
/// Storage: `Revive::OriginalAccount` (r:2 w:0)
294+
/// Proof: `Revive::OriginalAccount` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
295+
/// Storage: `Assets::Asset` (r:1 w:1)
296+
/// Proof: `Assets::Asset` (`max_values`: None, `max_size`: Some(210), added: 2685, mode: `MaxEncodedLen`)
297+
/// Storage: `Assets::Approvals` (r:1 w:1)
298+
/// Proof: `Assets::Approvals` (`max_values`: None, `max_size`: Some(148), added: 2623, mode: `MaxEncodedLen`)
299+
/// Storage: `Assets::Account` (r:2 w:2)
300+
/// Proof: `Assets::Account` (`max_values`: None, `max_size`: Some(134), added: 2609, mode: `MaxEncodedLen`)
301+
/// Storage: `System::Account` (r:1 w:1)
302+
/// Proof: `System::Account` (`max_values`: None, `max_size`: Some(128), added: 2603, mode: `MaxEncodedLen`)
303+
fn transfer_from() -> Weight {
304+
// Proof Size summary in bytes:
305+
// Measured: `951`
306+
// Estimated: `6208`
307+
// Minimum execution time: 68_000_000 picoseconds.
308+
Weight::from_parts(69_000_000, 6208)
309+
.saturating_add(RocksDbWeight::get().reads(7_u64))
310+
.saturating_add(RocksDbWeight::get().writes(5_u64))
311+
}
273312
/// Storage: `Revive::OriginalAccount` (r:1 w:0)
274313
/// Proof: `Revive::OriginalAccount` (`max_values`: None, `max_size`: Some(52), added: 2527, mode: `MaxEncodedLen`)
275314
/// Storage: `Assets::Approvals` (r:1 w:1)
@@ -283,11 +322,11 @@ impl WeightInfo for () {
283322
// Measured: `699 + c * (140 ±0)`
284323
// Estimated: `3675`
285324
// Minimum execution time: 27_000_000 picoseconds.
286-
Weight::from_parts(6_576_530, 3675)
287-
// Standard Error: 127_016
288-
.saturating_add(Weight::from_parts(22_054_081, 0).saturating_mul(a.into()))
289-
// Standard Error: 127_016
290-
.saturating_add(Weight::from_parts(36_419_387, 0).saturating_mul(c.into()))
325+
Weight::from_parts(7_862_244, 3675)
326+
// Standard Error: 43_634
327+
.saturating_add(Weight::from_parts(20_137_755, 0).saturating_mul(a.into()))
328+
// Standard Error: 43_634
329+
.saturating_add(Weight::from_parts(35_150_000, 0).saturating_mul(c.into()))
291330
.saturating_add(RocksDbWeight::get().reads(3_u64))
292331
.saturating_add(RocksDbWeight::get().writes(2_u64))
293332
}
@@ -301,8 +340,8 @@ impl WeightInfo for () {
301340
// Proof Size summary in bytes:
302341
// Measured: `471`
303342
// Estimated: `3675`
304-
// Minimum execution time: 33_000_000 picoseconds.
305-
Weight::from_parts(34_000_000, 3675)
343+
// Minimum execution time: 32_000_000 picoseconds.
344+
Weight::from_parts(33_000_000, 3675)
306345
.saturating_add(RocksDbWeight::get().reads(3_u64))
307346
.saturating_add(RocksDbWeight::get().writes(2_u64))
308347
}
@@ -325,8 +364,8 @@ impl WeightInfo for () {
325364
// Proof Size summary in bytes:
326365
// Measured: `505`
327366
// Estimated: `3675`
328-
// Minimum execution time: 14_000_000 picoseconds.
329-
Weight::from_parts(15_000_000, 3675)
367+
// Minimum execution time: 15_000_000 picoseconds.
368+
Weight::from_parts(16_000_000, 3675)
330369
.saturating_add(RocksDbWeight::get().reads(2_u64))
331370
.saturating_add(RocksDbWeight::get().writes(1_u64))
332371
}
@@ -339,7 +378,7 @@ impl WeightInfo for () {
339378
// Measured: `645`
340379
// Estimated: `3675`
341380
// Minimum execution time: 29_000_000 picoseconds.
342-
Weight::from_parts(30_000_000, 3675)
381+
Weight::from_parts(29_000_000, 3675)
343382
.saturating_add(RocksDbWeight::get().reads(2_u64))
344383
.saturating_add(RocksDbWeight::get().writes(1_u64))
345384
}
@@ -355,8 +394,8 @@ impl WeightInfo for () {
355394
// Proof Size summary in bytes:
356395
// Measured: `584`
357396
// Estimated: `3675`
358-
// Minimum execution time: 28_000_000 picoseconds.
359-
Weight::from_parts(30_000_000, 3675)
397+
// Minimum execution time: 27_000_000 picoseconds.
398+
Weight::from_parts(28_000_000, 3675)
360399
.saturating_add(RocksDbWeight::get().reads(4_u64))
361400
.saturating_add(RocksDbWeight::get().writes(3_u64))
362401
}
@@ -372,8 +411,8 @@ impl WeightInfo for () {
372411
// Proof Size summary in bytes:
373412
// Measured: `744`
374413
// Estimated: `3675`
375-
// Minimum execution time: 38_000_000 picoseconds.
376-
Weight::from_parts(39_000_000, 3675)
414+
// Minimum execution time: 40_000_000 picoseconds.
415+
Weight::from_parts(41_000_000, 3675)
377416
.saturating_add(RocksDbWeight::get().reads(4_u64))
378417
.saturating_add(RocksDbWeight::get().writes(3_u64))
379418
}
@@ -384,7 +423,7 @@ impl WeightInfo for () {
384423
// Measured: `300`
385424
// Estimated: `3675`
386425
// Minimum execution time: 5_000_000 picoseconds.
387-
Weight::from_parts(6_000_000, 3675)
426+
Weight::from_parts(5_000_000, 3675)
388427
.saturating_add(RocksDbWeight::get().reads(1_u64))
389428
}
390429
/// Storage: `Revive::OriginalAccount` (r:1 w:0)
@@ -395,7 +434,7 @@ impl WeightInfo for () {
395434
// Proof Size summary in bytes:
396435
// Measured: `346`
397436
// Estimated: `3599`
398-
// Minimum execution time: 8_000_000 picoseconds.
437+
// Minimum execution time: 7_000_000 picoseconds.
399438
Weight::from_parts(8_000_000, 3599)
400439
.saturating_add(RocksDbWeight::get().reads(2_u64))
401440
}

0 commit comments

Comments
 (0)