Skip to content

Commit 1c65067

Browse files
committed
Improve doc for the System contract
1 parent b429625 commit 1c65067

File tree

4 files changed

+103
-34
lines changed

4 files changed

+103
-34
lines changed

crates/pink/pink-extension/README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,17 @@ fn http_get_example(&self) {
3434

3535
The Pink! crate is designed to empower you, enabling you to leverage the unique features of the Phat Contract, such as making HTTP requests as demonstrated in our examples. This crate supplies the crucial types and functions needed to seamlessly interact with the Phat Contract runtime.
3636

37-
There two kind of APIs to communication with the runtime:
37+
There are three kind of APIs to communication with the runtime:
3838

3939
- Emitting Events:
4040
These APIs are primarily used in situations where the operation could lead to side effects that need to be deterministically recorded and may be rolled back during the execution of the contract call. For additional information on Emitting Events APIs, please refer to the [PinkEvent documentation](crate::PinkEvent).
4141

4242
- Chain Extension:
4343
These APIs are predominantly used for read-only operations or operations that aren't expected to create deterministic side effects. For an in-depth understanding of Chain Extension APIs, please refer to the [PinkExt documentation](crate::chain_extension::PinkExtBackend).
4444

45+
- System contract:
46+
There is a special contract called the System contract in each cluster. The system contract is instantiated when the cluster is created. Either ink contracts or external accounts can call the system contract to perform certain operations. For more information on the System contract, please refer to the [System documentation](crate::system::SystemForDoc).
47+
4548
For practical implementation examples, explore our [Phat Contract examples](https://github.com/Phala-Network/phat-contract-examples) repository.
4649

4750
## Using JavaScript with Phat Contract

crates/pink/pink-extension/macro/src/driver_system.rs

+38-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
22
use quote::quote;
3-
use syn::{spanned::Spanned, FnArg, Result};
3+
use syn::{parse_quote, spanned::Spanned, FnArg, Result};
44

55
pub(crate) enum InterfaceType {
66
System,
@@ -25,6 +25,8 @@ fn patch_or_err(
2525
) -> Result<TokenStream2> {
2626
use heck::{ToLowerCamelCase, ToSnakeCase};
2727
let the_trait: syn::ItemTrait = syn::parse2(input)?;
28+
let trait_for_doc = generate_trait_for_doc(the_trait.clone());
29+
let the_trait = patch_origin_system_doc(the_trait);
2830
let trait_ident = &the_trait.ident;
2931
let trait_name = the_trait.ident.to_string();
3032
let trait_impl_mod = Ident::new(
@@ -133,6 +135,9 @@ fn patch_or_err(
133135
#config
134136
#the_trait
135137

138+
#[cfg(doc)]
139+
#trait_for_doc
140+
136141
pub use #trait_impl_mod::#impl_type;
137142
mod #trait_impl_mod {
138143
use super::*;
@@ -223,6 +228,38 @@ fn patch_or_err(
223228
})
224229
}
225230

231+
fn patch_origin_system_doc(mut trait_item: syn::ItemTrait) -> syn::ItemTrait {
232+
let additonal_doc = format!(
233+
"**The doc is messed up by the ink macro. See [`{}ForDoc`] for a clean version**\n\n",
234+
trait_item.ident
235+
);
236+
trait_item
237+
.attrs
238+
.insert(0, parse_quote!(#[doc = #additonal_doc]));
239+
trait_item
240+
}
241+
242+
fn generate_trait_for_doc(mut trait_item: syn::ItemTrait) -> syn::ItemTrait {
243+
let additonal_doc = format!(
244+
"**This is the clean version doc of [`{}`]**\n\n",
245+
trait_item.ident
246+
);
247+
trait_item.attrs.retain(|attr| attr.path().is_ident("doc"));
248+
trait_item
249+
.attrs
250+
.insert(0, parse_quote!(#[doc = #additonal_doc]));
251+
trait_item.ident = syn::Ident::new(
252+
&format!("{}ForDoc", trait_item.ident),
253+
trait_item.ident.span(),
254+
);
255+
for item in trait_item.items.iter_mut() {
256+
if let syn::TraitItem::Fn(method) = item {
257+
method.attrs.retain(|attr| !attr.path().is_ident("ink"));
258+
}
259+
}
260+
trait_item
261+
}
262+
226263
#[cfg(test)]
227264
mod tests {
228265
use super::*;

crates/pink/pink-extension/src/chain_extension/http_request.rs

+17
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@ pub struct HttpRequest {
1212
pub body: Vec<u8>,
1313
}
1414

15+
impl HttpRequest {
16+
/// Create a new http request.
17+
pub fn new(
18+
url: impl Into<String>,
19+
method: impl Into<String>,
20+
headers: Vec<(String, String)>,
21+
body: Vec<u8>,
22+
) -> Self {
23+
Self {
24+
url: url.into(),
25+
method: method.into(),
26+
headers,
27+
body,
28+
}
29+
}
30+
}
31+
1532
#[derive(scale::Encode, scale::Decode)]
1633
#[cfg_attr(feature = "std", derive(scale_info::TypeInfo))]
1734
pub struct HttpResponse {

crates/pink/pink-extension/src/system.rs

+44-32
Original file line numberDiff line numberDiff line change
@@ -41,52 +41,65 @@ pub use this_crate::VersionTuple;
4141

4242
/// The pink system contract interface.
4343
///
44-
/// A system contract would be instantiated whenever a cluster is created.
44+
/// The system contract, instantiated with each cluster creation, manages access permissions to
45+
/// the privileged chain extension functions and pink events. Some of these functions or events
46+
/// are exclusive to the system contract. User contracts wishing to call these functions or
47+
/// emit these events must first request the system contract, which then checks the permissions
48+
/// to either execute or reject the request.
4549
#[pink::system]
4650
#[ink::trait_definition(namespace = "pink_system")]
4751
pub trait System {
48-
/// The version of the system. Can be used to determine the api ability.
52+
/// Returns the system contract version, indicating its API capabilities.
53+
///
54+
/// # Example
55+
/// ```no_run
56+
/// use pink_extension::system::SystemRef;
57+
/// let (major, minor, patch) = SystemRef::instance().version();
58+
/// ```
4959
#[ink(message, selector = 0x87c98a8d)]
5060
fn version(&self) -> VersionTuple;
51-
/// Grant an address the administrator role.
61+
62+
/// Grants the administrator role to an address. Administrator contracts can set drivers,
63+
/// deploy sidevm, etc.
5264
///
53-
/// The caller must be the owner of the cluster.
65+
/// Must be called by the cluster owner.
5466
#[ink(message)]
5567
fn grant_admin(&mut self, contract_id: AccountId) -> Result<()>;
5668

57-
/// Check if an address is an administrator
69+
/// Checks if an address is an administrator.
5870
#[ink(message)]
5971
fn is_admin(&self, contract_id: AccountId) -> bool;
6072

61-
/// Set a contract as a driver for `name`.
73+
/// Marks a contract as a driver for a given name, retrievable via `get_driver` or `get_driver2`.
74+
/// The caller must be the cluster owner or an administrator. Any valid string can be a name.
75+
/// There are predefined names used by the Phat Contract system.
6276
///
63-
/// The caller must be the owner of the cluster or an administrator.
77+
/// There are some predefined names that are used by the Phat Contract system:
78+
/// - `PinkLogger`: The contract that with a sidevm instance that collect the logs and events
79+
/// emitted by the ink! contracts in current cluster.
80+
/// - `ContractDeposit`: The contract that implements the `trait ContractDeposit` which talks
81+
/// to the pallet PhatKokenomic on Phala chain.
6482
#[ink(message)]
6583
fn set_driver(&mut self, name: String, contract_id: AccountId) -> Result<()>;
6684

67-
/// Get driver contract id for `name`.
85+
/// Retrieves the driver contract id for a given name.
6886
#[ink(message)]
6987
fn get_driver(&self, name: String) -> Option<AccountId>;
7088

71-
/// Get driver contract id for `name` and the set block number.
89+
/// Retrieves the driver contract id and the set block number for a given name.
7290
#[ink(message)]
7391
fn get_driver2(&self, name: String) -> Option<(crate::BlockNumber, AccountId)>;
7492

75-
/// Deploy a sidevm instance attached to a given contract.
76-
///
77-
/// The caller must be an administrator.
93+
/// Deploys a sidevm instance attached to a contract. Must be called by an administrator.
7894
#[ink(message)]
7995
fn deploy_sidevm_to(&self, contract_id: AccountId, code_hash: Hash) -> Result<()>;
8096

81-
/// Stop a sidevm instance attached to a given contract.
82-
///
83-
/// The caller must be an administrator.
97+
/// Stops a sidevm instance attached to a contract. Must be called by an administrator.
8498
#[ink(message)]
8599
fn stop_sidevm_at(&self, contract_id: AccountId) -> Result<()>;
86100

87-
/// Set block hook, such as OnBlockEnd, for given contract
88-
///
89-
/// The caller must be an administrator.
101+
/// Sets a block hook for a contract. Must be called by an administrator.
102+
/// Note: This feature is deprecated and will be removed in the future.
90103
#[ink(message)]
91104
fn set_hook(
92105
&mut self,
@@ -96,44 +109,43 @@ pub trait System {
96109
gas_limit: u64,
97110
) -> Result<()>;
98111

99-
/// Set weight of the contract for query requests and sidevm scheduling.
100-
///
101-
/// Higher weight would let the contract to get more resource.
112+
/// Sets the contract weight for query requests and sidevm scheduling.
113+
/// A higher weight allows the contract to access more resources.
102114
#[ink(message)]
103115
fn set_contract_weight(&self, contract_id: AccountId, weight: u32) -> Result<()>;
104116

105-
/// Return the total balance of given account
117+
/// Returns the total balance of a given account.
106118
#[ink(message)]
107119
fn total_balance_of(&self, account: AccountId) -> Balance;
108120

109-
/// Return the free balance of given account
121+
/// Returns the free balance of a given account.
110122
#[ink(message)]
111123
fn free_balance_of(&self, account: AccountId) -> Balance;
112124

113-
/// Upgrade the system contract to the latest version.
125+
/// Upgrades the system contract to the latest version.
114126
#[ink(message)]
115127
fn upgrade_system_contract(&mut self) -> Result<()>;
116128

117-
/// Do the upgrade condition checks and state migration if necessary.
118-
///
119-
/// This function is called by the system contract itself on the new version
120-
/// of code in the upgrading process.
129+
/// Performs upgrade condition checks and state migration if necessary.
130+
/// Called by the system contract on the new code version during an upgrade process.
121131
#[ink(message)]
122132
fn do_upgrade(&self, from_version: VersionTuple) -> Result<()>;
123133

124-
/// Upgrade the contract runtime
134+
/// Upgrades the contract runtime.
125135
#[ink(message)]
126136
fn upgrade_runtime(&mut self, version: (u32, u32)) -> Result<()>;
127137

128-
/// Check if the code is already uploaded to the cluster with given code hash.
138+
/// Checks if the code with a given hash is already uploaded to the cluster.
129139
#[ink(message)]
130140
fn code_exists(&self, code_hash: Hash, code_type: CodeType) -> bool;
131141

132-
/// Get the current code hash of given contract.
142+
/// Retrieves the current code hash of a given contract.
133143
#[ink(message)]
134144
fn code_hash(&self, account: AccountId) -> Option<ink::primitives::Hash>;
135145

136-
/// Get the history of given driver.
146+
/// Retrieves the history of a given driver, returning a vector of
147+
/// (block_number, contract_id) tuples where the block number is the
148+
/// block number when the driver is set.
137149
#[ink(message)]
138150
fn driver_history(&self, name: String) -> Option<Vec<(crate::BlockNumber, AccountId)>>;
139151
}

0 commit comments

Comments
 (0)