Skip to content

Commit

Permalink
Merge branch 'devnet-ready' into feat/solidity-get-stake
Browse files Browse the repository at this point in the history
  • Loading branch information
gztensor committed Jan 14, 2025
2 parents 9d3218b + bffc8b6 commit c15399c
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 35 deletions.
2 changes: 1 addition & 1 deletion pallets/subtensor/src/macros/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ mod genesis {
let hotkey = DefaultAccount::<T>::get();
SubnetMechanism::<T>::insert(netuid, 1); // Make dynamic.
Owner::<T>::insert(hotkey.clone(), hotkey.clone());
SubnetAlphaIn::<T>::insert(netuid, 1);
SubnetAlphaIn::<T>::insert(netuid, 10_000_000_000);
SubnetTAO::<T>::insert(netuid, 10_000_000_000);
NetworksAdded::<T>::insert(netuid, true);
TotalNetworks::<T>::mutate(|n| *n = n.saturating_add(1));
Expand Down
2 changes: 1 addition & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// `spec_version`, and `authoring_version` are the same between Wasm and native.
// This value is set to 100 to notify Polkadot-JS App (https://polkadot.js.org/apps) to use
// the compatible custom types.
spec_version: 221,
spec_version: 222,
impl_version: 1,
apis: RUNTIME_API_VERSIONS,
transaction_version: 1,
Expand Down
34 changes: 29 additions & 5 deletions runtime/src/precompiles/solidity/staking.abi
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,40 @@
"type": "bytes32"
},
{
"internalType": "uint16",
"internalType": "uint256",
"name": "netuid",
"type": "uint16"
"type": "uint256"
}
],
"name": "addStake",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
inputs: [
{
internalType: "bytes32",
name: "hotkey",
type: "bytes32",
},
{
internalType: "bytes32",
name: "coldkey",
type: "bytes32",
},
],
name: "getStake",
outputs: [
{
internalType: "uint64",
name: "",
type: "uint64",
},
],
stateMutability: "view",
type: "function",
},
{
"inputs": [
{
Expand Down Expand Up @@ -54,14 +78,14 @@
"type": "uint256"
},
{
"internalType": "uint16",
"internalType": "uint256",
"name": "netuid",
"type": "uint16"
"type": "uint256"
}
],
"name": "removeStake",
"outputs": [],
"stateMutability": "payable",
"stateMutability": "nonpayable",
"type": "function"
}
]
33 changes: 16 additions & 17 deletions runtime/src/precompiles/solidity/staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ interface IStaking {
* https://github.com/polkadot-evm/frontier/blob/2e219e17a526125da003e64ef22ec037917083fa/frame/evm/src/lib.rs#L739
*
* @param hotkey The hotkey public key (32 bytes).
* @param netuid The subnet to stake to (uint16). Currently a noop, functionality will be enabled with RAO.
* @param netuid The subnet to stake to (uint256).
*
* Requirements:
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
* correctly attributed.
*/
function addStake(bytes32 hotkey, uint16 netuid) external payable;
function addStake(bytes32 hotkey, uint256 netuid) external payable;

/**
* @dev Removes a subtensor stake `amount` from the specified `hotkey`.
Expand All @@ -33,26 +33,25 @@ interface IStaking {
*
* @param hotkey The hotkey public key (32 bytes).
* @param amount The amount to unstake in rao.
* @param netuid The subnet to stake to (uint16). Currently a noop, functionality will be enabled with RAO.
* @param netuid The subnet to stake to (uint256).
*
* Requirements:
* - `hotkey` must be a valid hotkey registered on the network, ensuring that the stake is
* correctly attributed.
* - The existing stake amount must be not lower than specified amount
*/
function removeStake(bytes32 hotkey, uint256 amount, uint16 netuid) external;
function removeStake(bytes32 hotkey, uint256 amount, uint256 netuid) external;

/**
* @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`.
*
* This function retrieves the current stake amount linked to a specific hotkey and coldkey pair.
* It is a view function, meaning it does not modify the state of the contract and is free to call.
*
* @param hotkey The hotkey public key (32 bytes).
* @param coldkey The coldkey public key (32 bytes).
* @param netuid The subnet the stake is on (uint16).
* @return The current stake amount in uint64 format.
*/
function getStake(bytes32 hotkey, bytes32 coldkey, uint16 netuid) external view returns (uint64);
/**
* @dev Returns the stake amount associated with the specified `hotkey` and `coldkey`.
*
* This function retrieves the current stake amount linked to a specific hotkey and coldkey pair.
* It is a view function, meaning it does not modify the state of the contract and is free to call.
*
* @param hotkey The hotkey public key (32 bytes).
* @param coldkey The coldkey public key (32 bytes).
* @param netuid The subnet the stake is on (uint256).
* @return The current stake amount in uint64 format.
*/
function getStake(bytes32 hotkey, bytes32 coldkey, uint256 netuid) external view returns (uint64);
}
36 changes: 25 additions & 11 deletions runtime/src/precompiles/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ impl StakingPrecompile {
.map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts

match method_id {
id if id == get_method_id("addStake(bytes32,uint16)") => {
id if id == get_method_id("addStake(bytes32,uint256)") => {
Self::add_stake(handle, &method_input)
}
id if id == get_method_id("removeStake(bytes32,uint256,uint16)") => {
id if id == get_method_id("removeStake(bytes32,uint256,uint256)") => {
Self::remove_stake(handle, &method_input)
}
id if id == get_method_id("getStake(bytes32,bytes32,uint16)") => {
Expand All @@ -71,9 +71,7 @@ impl StakingPrecompile {
fn add_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
let hotkey = Self::parse_hotkey(data)?.into();
let amount: U256 = handle.context().apparent_value;

// TODO: Use netuid method parameter here
let netuid: u16 = 0;
let netuid = Self::parse_netuid(data, 0x3E)?;

let amount_sub =
<Runtime as pallet_evm::Config>::BalanceConverter::into_substrate_balance(amount)
Expand All @@ -88,11 +86,10 @@ impl StakingPrecompile {
// Dispatch the add_stake call
Self::dispatch(handle, call)
}

fn remove_stake(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult {
let hotkey = Self::parse_hotkey(data)?.into();

// TODO: Use netuid method parameter here
let netuid: u16 = 0;
let netuid = Self::parse_netuid(data, 0x5E)?;

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

fn parse_netuid(data: &[u8], offset: usize) -> Result<u16, PrecompileFailure> {
if data.len() < offset + 2 {
return Err(PrecompileFailure::Error {
exit_status: ExitError::InvalidRange,
});
}

let mut netuid_bytes = [0u8; 2];
netuid_bytes.copy_from_slice(get_slice(data, offset, offset + 2)?);
let netuid: u16 = netuid_bytes[1] as u16 | ((netuid_bytes[0] as u16) << 8u16);

Ok(netuid)
}

fn dispatch(handle: &mut impl PrecompileHandle, call: RuntimeCall) -> PrecompileResult {
let account_id =
<HashedAddressMapping<BlakeTwo256> as AddressMapping<AccountId32>>::into_account_id(
Expand All @@ -181,9 +192,12 @@ impl StakingPrecompile {
exit_status: ExitSucceed::Returned,
output: vec![],
}),
Err(_) => Err(PrecompileFailure::Error {
exit_status: ExitError::Other("Subtensor call failed".into()),
}),
Err(_) => {
log::warn!("Returning error PrecompileFailure::Error");
Err(PrecompileFailure::Error {
exit_status: ExitError::Other("Subtensor call failed".into()),
})
}
}
}

Expand Down

0 comments on commit c15399c

Please sign in to comment.