Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/evm rao staking #1153

Merged
merged 6 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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 All @@ -30,14 +54,14 @@
"type": "uint256"
},
{
"internalType": "uint16",
"internalType": "uint256",
"name": "netuid",
"type": "uint16"
"type": "uint256"
}
],
"name": "removeStake",
"outputs": [],
"stateMutability": "payable",
"stateMutability": "nonpayable",
"type": "function"
}
]
8 changes: 4 additions & 4 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). Currently a noop, functionality will be enabled with RAO.
*
* 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,13 +33,13 @@ 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). Currently a noop, functionality will be enabled with RAO.

*
* 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;
}
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)
}
_ => Err(PrecompileFailure::Error {
Expand All @@ -68,9 +68,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 @@ -85,11 +83,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 @@ -121,6 +118,20 @@ impl StakingPrecompile {
Ok(hotkey)
}

fn parse_netuid(data: &[u8], offset: usize) -> Result<u16, PrecompileFailure> {
if data.len() < (offset + 2) as usize {
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 @@ -145,9 +156,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
Loading