Skip to content
This repository was archived by the owner on Dec 1, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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,434 changes: 1,100 additions & 1,334 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion crates/phala-mq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ hex = { version = "0.4.3", default-features = false, features = ['alloc'] }
derive_more = { version = "0.99", default-features = false, features = ["display"] }
parity-scale-codec = { version = "3.0", default-features = false, features = ["derive"] }
scale-info = { version = "2.0", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false }
serde = { version = "1.0", default-features = false, features = ["derive"] }

spin = { version = "0.9", default-features = false, features = ["mutex", "use_ticket_mutex"], optional = true }
Expand Down
17 changes: 7 additions & 10 deletions crates/phala-node-rpc-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,29 +11,26 @@ repository = "https://github.com/Phala-Network/phala-blockchain"
# third-party dependencies
serde = { version = "1.0.102", features = ["derive"] }
thiserror = "1.0"
jsonrpc-derive = "18.0"
jsonrpc-core = "18.0"
jsonrpc-core-client = "18.0"
jsonrpsee = { version = "0.13", features = ["server"] }
impl-serde = "0.3"
log = { version = "0.4.14", default-features = false }
hex = { version = "0.4.3", default-features = false }
codec = { package = "parity-scale-codec", version = "3.0" }
scale-info = { version = "2.0", default-features = false }

# primitives
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }

# client dependencies
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }
sc-transaction-pool-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }
sc-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20" }
sc-client-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
sc-transaction-pool-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }
sc-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22" }

phala-mq = { path = "../phala-mq" }
phala-pallets = { path = "../../pallets/phala" }
pallet-mq-runtime-api = { path = "../../pallets/phala/mq-runtime-api" }
ext-types = { path = "./types", package = "phala-node-rpc-ext-types" }

rayon = "1"

81 changes: 46 additions & 35 deletions crates/phala-node-rpc-ext/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ use std::marker::PhantomData;
use std::sync::Arc;

use codec::Encode;
use jsonrpc_derive::rpc;
use mq_seq::Error as MqSeqError;
use jsonrpsee::{
core::{async_trait, Error as JsonRpseeError, RpcResult},
proc_macros::rpc,
types::error::{CallError, ErrorObject},
RpcModule
};
use pallet_mq_runtime_api::MqApi;
use sc_client_api::blockchain::{HeaderBackend, HeaderMetadata};
use sc_client_api::{backend, Backend, BlockBackend, StorageProvider};
Expand All @@ -20,28 +24,28 @@ mod mq_seq;
mod storage_changes;

/// Base code for all errors.
const CUSTOM_RPC_ERROR: i64 = 10000;
const CUSTOM_RPC_ERROR: i32 = 10000;

#[rpc]
#[rpc(server)]
pub trait NodeRpcExtApi<BlockHash> {
/// Return the storage changes made by each block one by one from `from` to `to`(both inclusive).
/// To get better performance, the client should limit the amount of requested block properly.
/// 100 blocks for each call should be OK. REQUESTS FOR TOO LARGE NUMBER OF BLOCKS WILL BE REJECTED.
#[rpc(name = "pha_getStorageChanges")]
#[method(name = "pha_getStorageChanges")]
fn get_storage_changes(
&self,
from: BlockHash,
to: BlockHash,
) -> Result<GetStorageChangesResponse, StorageChangesError>;
) -> RpcResult<GetStorageChangesResponse>;

/// Get storage changes made by given block.
/// Returns `hex_encode(scale_encode(StorageChanges))`
#[rpc(name = "pha_getStorageChangesAt")]
fn get_storage_changes_at(&self, block: BlockHash) -> Result<String, StorageChangesError>;
#[method(name = "pha_getStorageChangesAt")]
fn get_storage_changes_at(&self, block: BlockHash) -> RpcResult<String>;

/// Return the next mq sequence number for given sender which take the ready transactions in count.
#[rpc(name = "pha_getMqNextSequence")]
fn get_mq_seq(&self, sender_hex: String) -> Result<u64, MqSeqError>;
#[method(name = "pha_getMqNextSequence")]
fn get_mq_seq(&self, sender_hex: String) -> RpcResult<u64>;
}

/// Stuffs for custom RPC
Expand All @@ -65,55 +69,60 @@ impl<BE, Block: BlockT, Client, P> NodeRpcExt<BE, Block, Client, P> {
}
}

impl<BE: 'static, Block: BlockT, Client: 'static, P> NodeRpcExtApi<Block::Hash>
for NodeRpcExt<BE, Block, Client, P>
where
BE: Backend<Block>,
Client: StorageProvider<Block, BE>
#[async_trait]
impl<BE: 'static, Block: BlockT, Client: 'static, P> NodeRpcExtApiServer<Block::Hash>
for NodeRpcExt<BE, Block, Client, P>
where
BE: Backend<Block>,
Client: StorageProvider<Block, BE>
+ HeaderBackend<Block>
+ BlockBackend<Block>
+ HeaderMetadata<Block, Error = sp_blockchain::Error>
+ ProvideRuntimeApi<Block>,
Client::Api:
Client::Api:
sp_api::Metadata<Block> + ApiExt<Block, StateBackend = backend::StateBackendFor<BE, Block>>,
Client::Api: MqApi<Block>,
Block: BlockT + 'static,
<<Block as BlockT>::Header as Header>::Number: Into<u64>,
P: TransactionPool + 'static,
Client::Api: MqApi<Block>,
Block: BlockT + 'static,
<<Block as BlockT>::Header as Header>::Number: Into<u64>,
P: TransactionPool + 'static,
{
fn get_storage_changes(
&self,
from: Block::Hash,
to: Block::Hash,
) -> Result<GetStorageChangesResponse, StorageChangesError> {
) -> RpcResult<GetStorageChangesResponse> {
if !self.is_archive_mode {
Err(StorageChangesError::Unavailable(
Err(JsonRpseeError::from(StorageChangesError::Unavailable(
r#"Add "--pruning=archive" to the command line to enable this RPC"#.into(),
))
)))
} else {
storage_changes::get_storage_changes(
let result = storage_changes::get_storage_changes(
self.client.as_ref(),
self.backend.as_ref(),
from,
to,
)
);

Ok(result?)
}
}

fn get_storage_changes_at(&self, block: Block::Hash) -> Result<String, StorageChangesError> {
fn get_storage_changes_at(&self, block: Block::Hash) -> RpcResult<String> {
let changes = self.get_storage_changes(block, block)?;
// get_storage_changes never returns empty vec without error.
let encoded = changes[0].encode();
Ok(impl_serde::serialize::to_hex(&encoded, false))
}

fn get_mq_seq(&self, sender_hex: String) -> Result<u64, MqSeqError> {
mq_seq::get_mq_seq(&*self.client, &self.pool, sender_hex)
fn get_mq_seq(&self, sender_hex: String) -> RpcResult<u64> {
let result = mq_seq::get_mq_seq(&*self.client, &self.pool, sender_hex);

Ok(result?)
}
}

pub fn extend_rpc<Client, BE, Block, P>(
io: &mut jsonrpc_core::IoHandler<sc_rpc::Metadata>,
io: &mut RpcModule<()>,
client: Arc<Client>,
backend: Arc<BE>,
is_archive_mode: bool,
Expand All @@ -133,10 +142,12 @@ pub fn extend_rpc<Client, BE, Block, P>(
<<Block as BlockT>::Header as Header>::Number: Into<u64>,
P: TransactionPool + 'static,
{
io.extend_with(NodeRpcExtApi::to_delegate(NodeRpcExt::new(
client,
backend,
is_archive_mode,
pool,
)));
io.merge(
NodeRpcExt::new(
client,
backend,
is_archive_mode,
pool,
).into_rpc(),
).expect("Initialize Phala node RPC ext failed.");
}
16 changes: 10 additions & 6 deletions crates/phala-node-rpc-ext/src/mq_seq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@ pub enum Error {
ApiError(#[from] sp_api::ApiError),
}

impl From<Error> for jsonrpc_core::Error {
impl From<Error> for JsonRpseeError {
fn from(e: Error) -> Self {
jsonrpc_core::Error {
code: jsonrpc_core::ErrorCode::ServerError(CUSTOM_RPC_ERROR),
message: e.to_string(),
data: None,
}
JsonRpseeError::Call(
CallError::Custom(
ErrorObject::owned(
CUSTOM_RPC_ERROR,
e.to_string(),
Option::<()>::None
)
)
)
}
}

Expand Down
16 changes: 10 additions & 6 deletions crates/phala-node-rpc-ext/src/storage_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,17 @@ impl Error {
}
}

impl From<Error> for jsonrpc_core::Error {
impl From<Error> for JsonRpseeError {
fn from(e: Error) -> Self {
jsonrpc_core::Error {
code: jsonrpc_core::ErrorCode::ServerError(CUSTOM_RPC_ERROR),
message: e.to_string(),
data: None,
}
JsonRpseeError::Call(
CallError::Custom(
ErrorObject::owned(
CUSTOM_RPC_ERROR,
e.to_string(),
Option::<()>::None
)
)
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/phala-serde-more/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ edition = "2021"

[dependencies]
serde = { version = "1.0.130", default-features = false, features = ["derive", "alloc"] }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false }
scale = { package = "parity-scale-codec", version = "3.0", default-features = false }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }

Expand Down
12 changes: 6 additions & 6 deletions crates/phala-trie-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ repository = "https://github.com/Phala-Network/phala-blockchain"
[dependencies]
parity-scale-codec = { version = "3.0", default-features = false }
scale-info = { version = "2.0", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false, features = ["full_crypto"] }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false, features = ["full_crypto"] }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false, features = ["disable_panic_handler", "disable_oom", "disable_allocator"] }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false }

serde = { version = "1.0", default-features = false, features = ["derive", "alloc"], optional = true }

[dev-dependencies]
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false, features = ["full_crypto"] }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false, features = ["full_crypto"] }
hash256-std-hasher = { version = "0.15", default-features = false }
hex = "0.4"
serde_json = "1.0"
Expand Down
4 changes: 2 additions & 2 deletions crates/phala-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ hex = { version = "0.4", default-features = false, features = ["alloc"] }
serde = { version = "1.0.101", default-features = false, optional = true }
codec = { package = "parity-scale-codec", version = "3.0", default-features = false, features = ["full"] }
scale-info = { version = "2.0", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.20", default-features = false, optional = true }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.22", default-features = false, optional = true }

phala-trie-storage = { path = "../../crates/phala-trie-storage", default-features = false, optional = true }
phala-mq = { path = "../../crates/phala-mq", default-features = false }
Expand Down
Loading