Skip to content

Commit bffc8b6

Browse files
authored
Merge pull request #1153 from opentensor/feat/evm-rao-staking
Feat/evm rao staking
2 parents a6bd117 + a60b1da commit bffc8b6

File tree

5 files changed

+60
-22
lines changed

5 files changed

+60
-22
lines changed

pallets/subtensor/src/macros/genesis.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ mod genesis {
4949
let hotkey = DefaultAccount::<T>::get();
5050
SubnetMechanism::<T>::insert(netuid, 1); // Make dynamic.
5151
Owner::<T>::insert(hotkey.clone(), hotkey.clone());
52-
SubnetAlphaIn::<T>::insert(netuid, 1);
52+
SubnetAlphaIn::<T>::insert(netuid, 10_000_000_000);
5353
SubnetTAO::<T>::insert(netuid, 10_000_000_000);
5454
NetworksAdded::<T>::insert(netuid, true);
5555
TotalNetworks::<T>::mutate(|n| *n = n.saturating_add(1));

runtime/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
220220
// `spec_version`, and `authoring_version` are the same between Wasm and native.
221221
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
222222
// the compatible custom types.
223-
spec_version: 221,
223+
spec_version: 222,
224224
impl_version: 1,
225225
apis: RUNTIME_API_VERSIONS,
226226
transaction_version: 1,

runtime/src/precompiles/solidity/staking.abi

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,40 @@
77
"type": "bytes32"
88
},
99
{
10-
"internalType": "uint16",
10+
"internalType": "uint256",
1111
"name": "netuid",
12-
"type": "uint16"
12+
"type": "uint256"
1313
}
1414
],
1515
"name": "addStake",
1616
"outputs": [],
1717
"stateMutability": "payable",
1818
"type": "function"
1919
},
20+
{
21+
inputs: [
22+
{
23+
internalType: "bytes32",
24+
name: "hotkey",
25+
type: "bytes32",
26+
},
27+
{
28+
internalType: "bytes32",
29+
name: "coldkey",
30+
type: "bytes32",
31+
},
32+
],
33+
name: "getStake",
34+
outputs: [
35+
{
36+
internalType: "uint64",
37+
name: "",
38+
type: "uint64",
39+
},
40+
],
41+
stateMutability: "view",
42+
type: "function",
43+
},
2044
{
2145
"inputs": [
2246
{
@@ -30,14 +54,14 @@
3054
"type": "uint256"
3155
},
3256
{
33-
"internalType": "uint16",
57+
"internalType": "uint256",
3458
"name": "netuid",
35-
"type": "uint16"
59+
"type": "uint256"
3660
}
3761
],
3862
"name": "removeStake",
3963
"outputs": [],
40-
"stateMutability": "payable",
64+
"stateMutability": "nonpayable",
4165
"type": "function"
4266
}
4367
]

runtime/src/precompiles/solidity/staking.sol

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ interface IStaking {
1414
* https://github.com/polkadot-evm/frontier/blob/2e219e17a526125da003e64ef22ec037917083fa/frame/evm/src/lib.rs#L739
1515
*
1616
* @param hotkey The hotkey public key (32 bytes).
17-
* @param netuid The subnet to stake to (uint16). Currently a noop, functionality will be enabled with RAO.
17+
* @param netuid The subnet to stake to (uint256). Currently a noop, functionality will be enabled with RAO.
1818
*
1919
* Requirements:
2020
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
2121
* correctly attributed.
2222
*/
23-
function addStake(bytes32 hotkey, uint16 netuid) external payable;
23+
function addStake(bytes32 hotkey, uint256 netuid) external payable;
2424

2525
/**
2626
* @dev Removes a subtensor stake `amount` from the specified `hotkey`.
@@ -33,13 +33,13 @@ interface IStaking {
3333
*
3434
* @param hotkey The hotkey public key (32 bytes).
3535
* @param amount The amount to unstake in rao.
36-
* @param netuid The subnet to stake to (uint16). Currently a noop, functionality will be enabled with RAO.
36+
* @param netuid The subnet to stake to (uint256). Currently a noop, functionality will be enabled with RAO.
3737
3838
*
3939
* Requirements:
4040
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
4141
* correctly attributed.
4242
* - The existing stake amount must be not lower than specified amount
4343
*/
44-
function removeStake(bytes32 hotkey, uint256 amount, uint16 netuid) external;
44+
function removeStake(bytes32 hotkey, uint256 amount, uint256 netuid) external;
4545
}

runtime/src/precompiles/staking.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ impl StakingPrecompile {
5353
.map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts
5454

5555
match method_id {
56-
id if id == get_method_id("addStake(bytes32,uint16)") => {
56+
id if id == get_method_id("addStake(bytes32,uint256)") => {
5757
Self::add_stake(handle, &method_input)
5858
}
59-
id if id == get_method_id("removeStake(bytes32,uint256,uint16)") => {
59+
id if id == get_method_id("removeStake(bytes32,uint256,uint256)") => {
6060
Self::remove_stake(handle, &method_input)
6161
}
6262
_ => Err(PrecompileFailure::Error {
@@ -68,9 +68,7 @@ impl StakingPrecompile {
6868
fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
6969
let hotkey = Self::parse_hotkey(data)?.into();
7070
let amount: U256 = handle.context().apparent_value;
71-
72-
// TODO: Use netuid method parameter here
73-
let netuid: u16 = 0;
71+
let netuid = Self::parse_netuid(data, 0x3E)?;
7472

7573
let amount_sub =
7674
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
@@ -85,11 +83,10 @@ impl StakingPrecompile {
8583
// Dispatch the add_stake call
8684
Self::dispatch(handle, call)
8785
}
86+
8887
fn remove_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
8988
let hotkey = Self::parse_hotkey(data)?.into();
90-
91-
// TODO: Use netuid method parameter here
92-
let netuid: u16 = 0;
89+
let netuid = Self::parse_netuid(data, 0x5E)?;
9390

9491
// We have to treat this as uint256 (because of Solidity ABI encoding rules, it pads uint64),
9592
// but this will never exceed 8 bytes, se we will ignore higher bytes and will only use lower
@@ -121,6 +118,20 @@ impl StakingPrecompile {
121118
Ok(hotkey)
122119
}
123120

121+
fn parse_netuid(data: &[u8], offset: usize) -> Result<u16, PrecompileFailure> {
122+
if data.len() < offset + 2 {
123+
return Err(PrecompileFailure::Error {
124+
exit_status: ExitError::InvalidRange,
125+
});
126+
}
127+
128+
let mut netuid_bytes = [0u8; 2];
129+
netuid_bytes.copy_from_slice(get_slice(data, offset, offset + 2)?);
130+
let netuid: u16 = netuid_bytes[1] as u16 | ((netuid_bytes[0] as u16) << 8u16);
131+
132+
Ok(netuid)
133+
}
134+
124135
fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult {
125136
let account_id =
126137
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
@@ -145,9 +156,12 @@ impl StakingPrecompile {
145156
exit_status: ExitSucceed::Returned,
146157
output: vec![],
147158
}),
148-
Err(_) => Err(PrecompileFailure::Error {
149-
exit_status: ExitError::Other("Subtensor call failed".into()),
150-
}),
159+
Err(_) => {
160+
log::warn!("Returning error PrecompileFailure::Error");
161+
Err(PrecompileFailure::Error {
162+
exit_status: ExitError::Other("Subtensor call failed".into()),
163+
})
164+
}
151165
}
152166
}
153167

0 commit comments

Comments
 (0)