-
Notifications
You must be signed in to change notification settings - Fork 244
Add pallet-contracts integration with Subtensor chain extension #2059
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
Open
silva-fj
wants to merge
54
commits into
opentensor:devnet-ready
Choose a base branch
from
silva-fj:feature/add-pallet-contracts
base: devnet-ready
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 10 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
19f09f1
Add pallet-contracts dependency
silva-fj 7d65d8f
Integrate contracts pallet into runtime
silva-fj d241271
Add contracts genesis config to localnet
silva-fj 27c2510
fix(chain-spec): remove empty contracts from localnet config
silva-fj ac2089a
Configure contracts pallet with call filter and custom schedule
silva-fj 594c930
feat(runtime): add Subtensor chain extension for contracts
silva-fj f13e1a0
docs(runtime): clarify comment for smart contracts support
silva-fj ceca15e
refactor(runtime): rename ContractSchedule to ContractsSchedule
silva-fj 587ee48
docs: add smart contracts documentation
silva-fj 1b6aec5
fix docs
silva-fj c8ca999
feat(runtime): update code hash lockup deposit percent to 30
silva-fj af86928
feat(runtime): customize contract storage deposit calculation
silva-fj c569dac
feat(runtime): expand contract call whitelist for staking operations
silva-fj ef563d3
docs: document expanded contract call whitelist
silva-fj 12788fc
fix(runtime): remove create_pure from proxy call filter
silva-fj 395d6a1
Merge upstream/devnet-ready into feature/add-pallet-contracts
silva-fj 8693c7c
Merge remote-tracking branch 'upstream/devnet-ready' into feature/add…
silva-fj 33b0083
Add DoNotBreakSmartContracts trait for contract API stability
silva-fj 4c0109b
Merge remote-tracking branch 'upstream/devnet-ready' into feature/add…
silva-fj 2610ac1
docs(contracts): remove Proxy::create_pure from allowed calls
silva-fj a490159
refactor(runtime): rename DoNotBreakSmartContracts trait
silva-fj cf70929
refactor(runtime): remove DoNotBreakSmartContractsV1 trait
silva-fj cf30094
build: add chain-extensions workspace member
silva-fj da84eed
feat(chain-extensions): add chain extensions crate
silva-fj 7e7c364
feat(chain-extensions): add FunctionId and Outcome types
silva-fj bab0039
feat(chain-extensions): implement staking chain extensions
silva-fj 3724e33
refactor(chain-ext): rename Outcome to Output
silva-fj c6547fd
build(chain-ext): add test dependencies
silva-fj 2279c43
refactor(chain-ext): introduce testable extension environment trait
silva-fj dab4c46
test(chain-ext): add test infrastructure and test suite
silva-fj 8e55f84
refactor(runtime): replace chain extension with subtensor-chain-exten…
silva-fj 8dd3c5c
refactor(contracts): remove SubtensorModule calls from whitelist
silva-fj 4e7e913
docs(contracts): document all chain extension functions and error codes
silva-fj 20cfeb3
Merge upstream/devnet-ready into feature/add-pallet-contracts
silva-fj 68b89f8
fix(chain-extensions): make types module public
silva-fj 61219e7
fix(chain-extensions): return correct success code
silva-fj 673d8c4
refactor(mock): clean up unused imports
silva-fj e542ed5
chore(chain-extensions): add no_std support
silva-fj 0fd04bf
refactor(chain-extensions): remove unused mock utilities
silva-fj 6a60750
feat(chain-extensions): add AddProxyV1 function ID and error codes
silva-fj 2d975b7
build(chain-extensions): add pallet-subtensor-proxy dependency
silva-fj ea31d42
feat(chain-extensions): implement AddProxyV1 chain extension
silva-fj 7e89630
test(chain-extensions): add proxy pallet to test runtime
silva-fj 0724349
test(chain-extensions): add test for AddProxyV1
silva-fj 00dc850
docs: add AddProxyV1 to chain extension documentation
silva-fj 61b5541
refactor(runtime): remove add_proxy from contract call filter
silva-fj 24e87c6
chore: update Cargo.lock
silva-fj 73e9387
feat(chain-extensions): add RemoveProxyV1 function ID and error code
silva-fj f919287
feat(chain-extensions): implement RemoveProxyV1 chain extension
silva-fj 0a362f2
test(chain-extensions): add test for RemoveProxyV1
silva-fj 49b07bd
docs: add RemoveProxyV1 to chain extension documentation
silva-fj 05550de
fix: add num_enum/std feature propagation
silva-fj fc7eeff
Merge remote-tracking branch 'upstream/devnet-ready' into feature/add…
silva-fj 142842d
fix: remove InitialMaxWeightsLimit from chain-extensions mock
silva-fj 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,66 @@ | ||
# Smart Contracts on Subtensor | ||
|
||
## Overview | ||
|
||
Subtensor now supports smart contract functionality through the integration of `pallet-contracts`, enabling developers to deploy and execute WebAssembly (WASM) smart contracts on the network. Contracts are written in [ink!](https://use.ink/), a Rust-based embedded domain-specific language (eDSL) for writing smart contracts on Substrate-based chains. | ||
|
||
## Getting Started | ||
|
||
For general smart contract development on Subtensor, please refer to the official ink! documentation: | ||
- [ink! Documentation](https://use.ink/docs/v5/) | ||
- [ink! Getting Started Guide](https://use.ink/docs/v5/getting-started/setup) | ||
- [ink! Examples](https://github.com/use-ink/ink-examples/tree/v5.x.x) | ||
|
||
## Subtensor-Specific Features | ||
|
||
### Chain Extension | ||
|
||
Subtensor provides a custom chain extension that allows smart contracts to interact with Subtensor-specific functionality: | ||
|
||
#### Available Functions | ||
|
||
| Function ID | Name | Description | Parameters | Returns | | ||
|------------|------|-------------|------------|---------| | ||
| 1001 | `get_stake_info_for_hotkey_coldkey_netuid` | Query stake information | `(AccountId32, AccountId32, NetUid)` | Stake information | | ||
|
||
Example usage in your ink! contract: | ||
```rust | ||
#[ink::chain_extension(extension = 0)] | ||
pub trait SubtensorExtension { | ||
type ErrorCode = SubtensorError; | ||
|
||
#[ink(function = 1001)] | ||
fn get_stake_info( | ||
hotkey: AccountId, | ||
coldkey: AccountId, | ||
netuid: u16, | ||
) -> Result<Option<StakeInfo>, SubtensorError>; | ||
} | ||
``` | ||
|
||
### Call Filter | ||
|
||
For security, contracts can only dispatch a limited set of runtime calls: | ||
|
||
**Whitelisted Calls:** | ||
- `SubtensorModule::move_stake` - Move stake between hotkeys | ||
- `SubtensorModule::transfer_stake` - Transfer stake between accounts | ||
- `Proxy::proxy` - Execute proxy calls | ||
|
||
All other runtime calls are restricted and cannot be dispatched from contracts. | ||
|
||
### Configuration Parameters | ||
|
||
| Parameter | Value | Description | | ||
|-----------|-------|-------------| | ||
| Maximum code size | 128 KB | Maximum size of contract WASM code | | ||
| Call stack depth | 5 frames | Maximum nested contract call depth | | ||
| Runtime memory | 1 GB | Memory available during contract execution | | ||
| Validator runtime memory | 2 GB | Memory available for validators | | ||
| Transient storage | 1 MB | Maximum transient storage size | | ||
|
||
|
||
## Additional Resources | ||
|
||
- [cargo-contract CLI Tool](https://github.com/paritytech/cargo-contract) | ||
- [Contracts UI](https://contracts-ui.substrate.io/) |
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,52 @@ | ||
use codec::Encode; | ||
use pallet_contracts::chain_extension::{ | ||
ChainExtension, Environment, Ext, InitState, RetVal, SysConfig, | ||
}; | ||
use sp_runtime::{AccountId32, DispatchError}; | ||
use subtensor_runtime_common::NetUid; | ||
|
||
use crate::{Runtime, SubtensorModule}; | ||
|
||
#[derive(Default)] | ||
pub struct SubtensorChainExtension; | ||
|
||
impl ChainExtension<Runtime> for SubtensorChainExtension { | ||
fn call<E: Ext>(&mut self, env: Environment<E, InitState>) -> Result<RetVal, DispatchError> | ||
where | ||
E::T: SysConfig, | ||
{ | ||
let func_id = env.func_id(); | ||
|
||
match func_id { | ||
// Function ID 1001: get_stake_info_for_hotkey_coldkey_netuid | ||
1001 => { | ||
let mut env = env.buf_in_buf_out(); | ||
|
||
let input: (AccountId32, AccountId32, NetUid) = env | ||
.read_as() | ||
.map_err(|_| DispatchError::Other("Failed to decode input parameters"))?; | ||
|
||
let (hotkey, coldkey, netuid) = input; | ||
|
||
let stake_info = SubtensorModule::get_stake_info_for_hotkey_coldkey_netuid( | ||
hotkey, coldkey, netuid, | ||
); | ||
|
||
let encoded_result = stake_info.encode(); | ||
|
||
env.write(&encoded_result, false, None) | ||
.map_err(|_| DispatchError::Other("Failed to write output"))?; | ||
|
||
Ok(RetVal::Converging(0)) | ||
} | ||
_ => { | ||
log::error!("Called an unregistered chain extension function: {func_id}",); | ||
Err(DispatchError::Other("Unimplemented function ID")) | ||
} | ||
} | ||
} | ||
|
||
fn enabled() -> bool { | ||
true | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.