Skip to content

Commit 998867e

Browse files
committed
fix wrong origin
1 parent f25cabc commit 998867e

File tree

2 files changed

+73
-28
lines changed

2 files changed

+73
-28
lines changed

runtime/src/precompiles/staking.rs

Lines changed: 60 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@
2626
//
2727

2828
use crate::precompiles::{
29-
contract_to_origin, get_method_id, get_pubkey, get_slice, parse_netuid,
30-
try_dispatch_runtime_call,
29+
get_method_id, get_pubkey, get_slice, parse_netuid, try_dispatch_runtime_call,
3130
};
3231
use crate::{ProxyType, Runtime, RuntimeCall};
32+
use frame_system::RawOrigin;
3333
use pallet_evm::{
34-
BalanceConverter, ExitError, ExitSucceed, PrecompileFailure, PrecompileHandle,
35-
PrecompileOutput, PrecompileResult,
34+
AddressMapping, BalanceConverter, ExitError, ExitSucceed, HashedAddressMapping,
35+
PrecompileFailure, PrecompileHandle, PrecompileOutput, PrecompileResult,
3636
};
3737
use sp_core::U256;
38-
use sp_runtime::traits::{StaticLookup, UniqueSaturatedInto};
38+
use sp_runtime::traits::{BlakeTwo256, Dispatchable, StaticLookup, UniqueSaturatedInto};
39+
use sp_runtime::AccountId32;
3940
use sp_std::vec;
4041

4142
pub const STAKING_PRECOMPILE_INDEX: u64 = 2049;
@@ -75,27 +76,37 @@ impl StakingPrecompile {
7576
}
7677

7778
fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
79+
let account_id =
80+
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
81+
handle.context().caller,
82+
);
83+
7884
let (hotkey, _) = get_pubkey(data)?;
7985
let amount: U256 = handle.context().apparent_value;
8086
let netuid = Self::parse_netuid(data, 0x3E)?;
8187

88+
if !amount.is_zero() {
89+
Self::transfer_back_to_caller(&account_id, amount)?;
90+
}
91+
8292
let amount_sub =
8393
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
8494
.ok_or(ExitError::OutOfFund)?;
8595

86-
// let (account_id_src, _) = get_pubkey(&CONTRACT_ADDRESS_SS58)?;
87-
// Create the add_stake call
8896
let call = RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::add_stake {
8997
hotkey,
9098
netuid,
9199
amount_staked: amount_sub.unique_saturated_into(),
92100
});
93-
// let origin = RawOrigin::Signed(account_id_src);
94-
// Dispatch the add_stake call
95-
try_dispatch_runtime_call(handle, call, contract_to_origin(&CONTRACT_ADDRESS_SS58)?)
101+
102+
try_dispatch_runtime_call(handle, call, RawOrigin::Signed(account_id))
96103
}
97104

98105
fn remove_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
106+
let account_id =
107+
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
108+
handle.context().caller,
109+
);
99110
let (hotkey, _) = get_pubkey(data)?;
100111
let netuid = Self::parse_netuid(data, 0x5E)?;
101112

@@ -115,10 +126,14 @@ impl StakingPrecompile {
115126
netuid,
116127
amount_unstaked: amount_sub.unique_saturated_into(),
117128
});
118-
try_dispatch_runtime_call(handle, call, contract_to_origin(&CONTRACT_ADDRESS_SS58)?)
129+
try_dispatch_runtime_call(handle, call, RawOrigin::Signed(account_id))
119130
}
120131

121132
fn add_proxy(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
133+
let account_id =
134+
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
135+
handle.context().caller,
136+
);
122137
let (delegate, _) = get_pubkey(data)?;
123138
let delegate = <Runtime as frame_system::Config>::Lookup::unlookup(delegate);
124139
let call = RuntimeCall::Proxy(pallet_proxy::Call::<Runtime>::add_proxy {
@@ -127,10 +142,14 @@ impl StakingPrecompile {
127142
delay: 0,
128143
});
129144

130-
try_dispatch_runtime_call(handle, call, contract_to_origin(&CONTRACT_ADDRESS_SS58)?)
145+
try_dispatch_runtime_call(handle, call, RawOrigin::Signed(account_id))
131146
}
132147

133148
fn remove_proxy(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
149+
let account_id =
150+
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
151+
handle.context().caller,
152+
);
134153
let (delegate, _) = get_pubkey(data)?;
135154
let delegate = <Runtime as frame_system::Config>::Lookup::unlookup(delegate);
136155
let call = RuntimeCall::Proxy(pallet_proxy::Call::<Runtime>::remove_proxy {
@@ -139,7 +158,7 @@ impl StakingPrecompile {
139158
delay: 0,
140159
});
141160

142-
try_dispatch_runtime_call(handle, call, contract_to_origin(&CONTRACT_ADDRESS_SS58)?)
161+
try_dispatch_runtime_call(handle, call, RawOrigin::Signed(account_id))
143162
}
144163

145164
fn get_stake(data: &[u8]) -> PrecompileResult {
@@ -167,17 +186,37 @@ impl StakingPrecompile {
167186
})
168187
}
169188

170-
fn parse_netuid(data: &[u8], offset: usize) -> Result<u16, PrecompileFailure> {
171-
if data.len() < offset + 2 {
189+
fn transfer_back_to_caller(
190+
account_id: &AccountId32,
191+
amount: U256,
192+
) -> Result<(), PrecompileFailure> {
193+
let smart_contract_account_id: AccountId32 = CONTRACT_ADDRESS_SS58.into();
194+
195+
let amount_sub =
196+
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
197+
.ok_or(ExitError::OutOfFund)?;
198+
199+
// Create a transfer call from the smart contract to the caller
200+
let transfer_call =
201+
RuntimeCall::Balances(pallet_balances::Call::<Runtime>::transfer_allow_death {
202+
dest: account_id.clone().into(),
203+
value: amount_sub.unique_saturated_into(),
204+
});
205+
206+
// Execute the transfer
207+
let transfer_result =
208+
transfer_call.dispatch(RawOrigin::Signed(smart_contract_account_id).into());
209+
210+
if let Err(dispatch_error) = transfer_result {
211+
log::error!(
212+
"Transfer back to caller failed. Error: {:?}",
213+
dispatch_error
214+
);
172215
return Err(PrecompileFailure::Error {
173-
exit_status: ExitError::InvalidRange,
216+
exit_status: ExitError::Other("Transfer back to caller failed".into()),
174217
});
175218
}
176219

177-
let mut netuid_bytes = [0u8; 2];
178-
netuid_bytes.copy_from_slice(get_slice(data, offset, offset + 2)?);
179-
let netuid: u16 = netuid_bytes[1] as u16 | ((netuid_bytes[0] as u16) << 8u16);
180-
181-
Ok(netuid)
220+
Ok(())
182221
}
183222
}

runtime/src/precompiles/subnet.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1-
use crate::precompiles::{
2-
contract_to_origin, get_method_id, get_pubkey, get_slice, try_dispatch_runtime_call,
3-
};
1+
use crate::precompiles::{get_method_id, get_pubkey, get_slice, try_dispatch_runtime_call};
42
use crate::{Runtime, RuntimeCall};
5-
use pallet_evm::{ExitError, PrecompileFailure, PrecompileHandle, PrecompileResult};
3+
use frame_system::RawOrigin;
4+
use pallet_evm::{
5+
AddressMapping, ExitError, HashedAddressMapping, PrecompileFailure, PrecompileHandle,
6+
PrecompileResult,
7+
};
8+
use sp_runtime::traits::BlakeTwo256;
69
use sp_runtime::AccountId32;
710
use sp_std::vec;
8-
911
pub const SUBNET_PRECOMPILE_INDEX: u64 = 2051;
1012
// bytes with max lenght 1K
1113
pub const MAX_SINGLE_PARAMETER_SIZE: usize = 1024;
@@ -51,7 +53,6 @@ impl SubnetPrecompile {
5153
let call = match data.len() {
5254
32 => {
5355
let (hotkey, _) = get_pubkey(data)?;
54-
5556
RuntimeCall::SubtensorModule(
5657
pallet_subtensor::Call::<Runtime>::register_network_with_identity {
5758
hotkey,
@@ -85,8 +86,13 @@ impl SubnetPrecompile {
8586
}
8687
};
8788

89+
let account_id =
90+
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
91+
handle.context().caller,
92+
);
93+
8894
// Dispatch the register_network call
89-
try_dispatch_runtime_call(handle, call, contract_to_origin(&CONTRACT_ADDRESS_SS58)?)
95+
try_dispatch_runtime_call(handle, call, RawOrigin::Signed(account_id))
9096
}
9197

9298
fn parse_register_network_parameters(

0 commit comments

Comments
 (0)