Skip to content

Commit c15399c

Browse files
committed
Merge branch 'devnet-ready' into feat/solidity-get-stake
2 parents 9d3218b + bffc8b6 commit c15399c

File tree

5 files changed

+72
-35
lines changed

5 files changed

+72
-35
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
{
@@ -54,14 +78,14 @@
5478
"type": "uint256"
5579
},
5680
{
57-
"internalType": "uint16",
81+
"internalType": "uint256",
5882
"name": "netuid",
59-
"type": "uint16"
83+
"type": "uint256"
6084
}
6185
],
6286
"name": "removeStake",
6387
"outputs": [],
64-
"stateMutability": "payable",
88+
"stateMutability": "nonpayable",
6589
"type": "function"
6690
}
6791
]

runtime/src/precompiles/solidity/staking.sol

Lines changed: 16 additions & 17 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).
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,26 +33,25 @@ 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.
37-
36+
* @param netuid The subnet to stake to (uint256).
3837
*
3938
* Requirements:
4039
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
4140
* correctly attributed.
4241
* - The existing stake amount must be not lower than specified amount
4342
*/
44-
function removeStake(bytes32 hotkey, uint256 amount, uint16 netuid) external;
43+
function removeStake(bytes32 hotkey, uint256 amount, uint256 netuid) external;
4544

46-
/**
47-
* @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`.
48-
*
49-
* This function retrieves the current stake amount linked to a specific hotkey and coldkey pair.
50-
* It is a view function, meaning it does not modify the state of the contract and is free to call.
51-
*
52-
* @param hotkey The hotkey public key (32 bytes).
53-
* @param coldkey The coldkey public key (32 bytes).
54-
* @param netuid The subnet the stake is on (uint16).
55-
* @return The current stake amount in uint64 format.
56-
*/
57-
function getStake(bytes32 hotkey, bytes32 coldkey, uint16 netuid) external view returns (uint64);
45+
/**
46+
* @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`.
47+
*
48+
* This function retrieves the current stake amount linked to a specific hotkey and coldkey pair.
49+
* It is a view function, meaning it does not modify the state of the contract and is free to call.
50+
*
51+
* @param hotkey The hotkey public key (32 bytes).
52+
* @param coldkey The coldkey public key (32 bytes).
53+
* @param netuid The subnet the stake is on (uint256).
54+
* @return The current stake amount in uint64 format.
55+
*/
56+
function getStake(bytes32 hotkey, bytes32 coldkey, uint256 netuid) external view returns (uint64);
5857
}

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
id if id == get_method_id("getStake(bytes32,bytes32,uint16)") => {
@@ -71,9 +71,7 @@ impl StakingPrecompile {
7171
fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
7272
let hotkey = Self::parse_hotkey(data)?.into();
7373
let amount: U256 = handle.context().apparent_value;
74-
75-
// TODO: Use netuid method parameter here
76-
let netuid: u16 = 0;
74+
let netuid = Self::parse_netuid(data, 0x3E)?;
7775

7876
let amount_sub =
7977
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
@@ -88,11 +86,10 @@ impl StakingPrecompile {
8886
// Dispatch the add_stake call
8987
Self::dispatch(handle, call)
9088
}
89+
9190
fn remove_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
9291
let hotkey = Self::parse_hotkey(data)?.into();
93-
94-
// TODO: Use netuid method parameter here
95-
let netuid: u16 = 0;
92+
let netuid = Self::parse_netuid(data, 0x5E)?;
9693

9794
// We have to treat this as uint256 (because of Solidity ABI encoding rules, it pads uint64),
9895
// but this will never exceed 8 bytes, se we will ignore higher bytes and will only use lower
@@ -157,6 +154,20 @@ impl StakingPrecompile {
157154
Ok(hotkey)
158155
}
159156

157+
fn parse_netuid(data: &[u8], offset: usize) -> Result<u16, PrecompileFailure> {
158+
if data.len() < offset + 2 {
159+
return Err(PrecompileFailure::Error {
160+
exit_status: ExitError::InvalidRange,
161+
});
162+
}
163+
164+
let mut netuid_bytes = [0u8; 2];
165+
netuid_bytes.copy_from_slice(get_slice(data, offset, offset + 2)?);
166+
let netuid: u16 = netuid_bytes[1] as u16 | ((netuid_bytes[0] as u16) << 8u16);
167+
168+
Ok(netuid)
169+
}
170+
160171
fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult {
161172
let account_id =
162173
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
@@ -181,9 +192,12 @@ impl StakingPrecompile {
181192
exit_status: ExitSucceed::Returned,
182193
output: vec![],
183194
}),
184-
Err(_) => Err(PrecompileFailure::Error {
185-
exit_status: ExitError::Other("Subtensor call failed".into()),
186-
}),
195+
Err(_) => {
196+
log::warn!("Returning error PrecompileFailure::Error");
197+
Err(PrecompileFailure::Error {
198+
exit_status: ExitError::Other("Subtensor call failed".into()),
199+
})
200+
}
187201
}
188202
}
189203

0 commit comments

Comments
 (0)