-
Notifications
You must be signed in to change notification settings - Fork 197
add subnets precompile #1110
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
Merged
Merged
add subnets precompile #1110
Changes from 18 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
bc8c514
add subnet precompile contract
open-junius 157032b
remove commented code
open-junius 5435ccd
refactor code
open-junius 7169908
fix clippy
open-junius 7155329
merge with target branch
open-junius cdda2f6
fix compile error
open-junius e565825
add memory storage
open-junius 6a0f6f9
add subnets precompile
open-junius 2141e00
Merge branch 'devnet-ready' into feat/subnet-precompile
open-junius 2284e31
merge with subnet precompile
open-junius be5bc35
merge with target branch
open-junius 1643238
fix compilation
open-junius 5040e52
update sol
open-junius 1341e7a
rename contract
open-junius 23f9b87
fix e2e test
open-junius f3c6059
Merge branch 'devnet-ready' into neuron-precompile
open-junius 9de1927
commit Cargo.lock
open-junius 3d05f7b
cargo clippy
open-junius 1a34d33
refactor helper
open-junius 541518e
fix compilation error
open-junius d148c2b
refactor get pubkey method
open-junius 96a1d08
cargo clippy
open-junius 241d620
cargo fmt
open-junius a0819b4
remove byte_to_account_id
open-junius 4a73af6
merge with devnet ready
open-junius 682ac1a
Merge branch 'devnet-ready' into neuron-precompile
open-junius ae60405
merge with target branch
open-junius f644f26
refactor code
open-junius f25cabc
add len check
open-junius 998867e
fix wrong origin
open-junius cdfffaf
fix clippy
open-junius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use pallet_evm::{ExitError, PrecompileFailure, PrecompileHandle, PrecompileResult}; | ||
|
||
use crate::precompiles::{dispatch, get_method_id, get_slice}; | ||
use sp_std::vec; | ||
|
||
use crate::{Runtime, RuntimeCall}; | ||
pub const NEURON_PRECOMPILE_INDEX: u64 = 2052; | ||
|
||
// this is neuron smart contract's(0x0000000000000000000000000000000000000804) sr25519 address | ||
pub const NEURON_CONTRACT_ADDRESS: &str = "5GKZiUUgTnWSz3BgiVBMehEKkLszsG4ZXnvgWpWFUFKqrqyn"; | ||
|
||
pub struct NeuronPrecompile; | ||
|
||
impl NeuronPrecompile { | ||
pub fn execute(handle: &mut impl PrecompileHandle) -> PrecompileResult { | ||
let txdata = handle.input(); | ||
let method_id = get_slice(txdata, 0, 4)?; | ||
let method_input = txdata | ||
.get(4..) | ||
.map_or_else(vec::Vec::new, |slice| slice.to_vec()); // Avoiding borrowing conflicts | ||
|
||
match method_id { | ||
id if id == get_method_id("burnedRegister(uint16,bytes32)") => { | ||
Self::burned_register(handle, &method_input) | ||
} | ||
|
||
_ => Err(PrecompileFailure::Error { | ||
exit_status: ExitError::InvalidRange, | ||
}), | ||
} | ||
} | ||
|
||
pub fn burned_register(handle: &mut impl PrecompileHandle, data: &[u8]) -> PrecompileResult { | ||
let (netuid, hotkey) = Self::parse_netuid_hotkey_parameter(data)?; | ||
let call = | ||
RuntimeCall::SubtensorModule(pallet_subtensor::Call::<Runtime>::burned_register { | ||
netuid, | ||
hotkey: hotkey.into(), | ||
}); | ||
dispatch(handle, call, NEURON_CONTRACT_ADDRESS) | ||
} | ||
|
||
fn parse_netuid_hotkey_parameter(data: &[u8]) -> Result<(u16, [u8; 32]), PrecompileFailure> { | ||
if data.len() < 64 { | ||
return Err(PrecompileFailure::Error { | ||
exit_status: ExitError::InvalidRange, | ||
}); | ||
} | ||
let mut netuid_vec = [0u8; 2]; | ||
netuid_vec.copy_from_slice(get_slice(data, 30, 32)?); | ||
let netuid = u16::from_be_bytes(netuid_vec); | ||
|
||
let mut parameter = [0u8; 32]; | ||
parameter.copy_from_slice(get_slice(data, 32, 64)?); | ||
|
||
Ok((netuid, parameter)) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
[ | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint16", | ||
"name": "netuid", | ||
"type": "uint16" | ||
}, | ||
{ | ||
"internalType": "bytes32", | ||
"name": "hotkey", | ||
"type": "bytes32" | ||
} | ||
], | ||
"name": "burnedRegister", | ||
"outputs": [], | ||
"stateMutability": "payable", | ||
"type": "function" | ||
} | ||
] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
pragma solidity ^0.8.0; | ||
|
||
address constant INeuron_ADDRESS = 0x0000000000000000000000000000000000000804; | ||
|
||
interface INeuron { | ||
/** | ||
* @dev Registers a neuron by calling `do_burned_registration` internally with the origin set to the ss58 mirror of the H160 address. | ||
* This allows the H160 to further call neuron-related methods and receive emissions. | ||
* | ||
* @param netuid The subnet to register the neuron to (uint16). | ||
* @param hotkey The hotkey public key (32 bytes). | ||
*/ | ||
function burnedRegister(uint16 netuid, bytes32 hotkey) external payable; | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please move
parse_pub_key
andparse_netuid
to the common space like you did withdispatch
, for example, and use them here?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
moved both functions to mod