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

Upgrade to Polkadot v0.9.40 #263

Merged
merged 18 commits into from
Apr 10, 2023
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
1,379 changes: 751 additions & 628 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion crates/phala-mq/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,20 @@ edition = "2018"

[dependencies]
log = "0.4.14"
tracing = { version = "0.1", default-features = false }
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.3", default-features = false, features = ["derive"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", 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 }
phala-serde-more = { path = "../phala-serde-more", default-features = false }

# for checkpoint
environmental = { version = "1.1.3", optional = true }
im = "15"

[features]
default = ["dispatcher", "queue", "signers", "checkpoint"]
Expand Down
34 changes: 29 additions & 5 deletions crates/phala-mq/src/dispatcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use core::marker::PhantomData;

use alloc::{collections::BTreeMap, vec::Vec};
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};

use crate::simple_mpsc::{channel, ReceiveError, Receiver as RawReceiver, Sender, Seq};
Expand All @@ -15,13 +15,14 @@ impl Seq for (u64, Message) {
}
}

#[derive(Default)]
#[derive(Default, Clone)]
pub struct MessageDispatcher {
subscribers: BTreeMap<Path, Vec<Sender<(u64, Message)>>>,
subscribers: im::OrdMap<Path, Vec<Sender<(u64, Message)>>>,
local_index: u64,
//match_subscribers: Vec<Matcher, Vec<Sender<Message>>>,
}

#[derive(Clone)]
pub struct Receiver<T> {
inner: RawReceiver<(u64, T)>,
topic: Vec<u8>,
Expand Down Expand Up @@ -79,7 +80,11 @@ impl MessageDispatcher {
if let Err(error) = receiver.send((sn, message.clone())) {
use crate::simple_mpsc::SendError::*;
match error {
ReceiverGone => false,
ReceiverGone => {
let dst = String::from_utf8_lossy(message.destination.path());
tracing::warn!(%dst, "ReceiverGone");
false
}
}
} else {
count += 1;
Expand All @@ -97,7 +102,7 @@ impl MessageDispatcher {
/// Drop all unhandled messages.
pub fn clear(&mut self) -> usize {
let mut count = 0;
for subscriber in self.subscribers.values_mut().flatten() {
for subscriber in self.subscribers.values().flatten() {
count += subscriber.clear();
}
count
Expand All @@ -112,6 +117,12 @@ pub enum TypedReceiveError {
CodecError(CodecError),
}

impl TypedReceiveError {
pub fn is_sender_gone(&self) -> bool {
matches!(self, Self::SenderGone)
}
}

impl From<CodecError> for TypedReceiveError {
fn from(e: CodecError) -> Self {
Self::CodecError(e)
Expand All @@ -125,6 +136,15 @@ pub struct TypedReceiver<T> {
_t: PhantomData<T>,
}

impl<T> Clone for TypedReceiver<T> {
fn clone(&self) -> Self {
Self {
queue: self.queue.clone(),
_t: self._t,
}
}
}

impl<T: Decode> TypedReceiver<T> {
pub fn try_next(&mut self) -> Result<Option<(u64, T, MessageOrigin)>, TypedReceiveError> {
let message = self.queue.try_next().map_err(|e| match e {
Expand Down Expand Up @@ -254,6 +274,10 @@ macro_rules! select_ignore_errors {
$block
}
}
Err(err) if err.is_sender_gone() => {
log::warn!("[{}] mq error: {:?}", $crate::function!(), err);
panic!("mq error: {:?}", err);
}
Err(err) => {
log::warn!("[{}] mq ignored error: {:?}", $crate::function!(), err);
}
Expand Down
19 changes: 15 additions & 4 deletions crates/phala-mq/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub mod checkpoint_helper;
#[cfg(feature = "dispatcher")]
pub use dispatcher::{MessageDispatcher, TypedReceiveError, TypedReceiver};
#[cfg(feature = "queue")]
pub use send_queue::{MessageChannel, MessageSendQueue};
pub use send_queue::{Channel as ChannelState, MessageChannel, MessageSendQueue};
#[cfg(any(feature = "queue", feature = "dispatcher"))]
pub use simple_mpsc::{ReceiveError, Receiver};

Expand Down Expand Up @@ -80,13 +80,24 @@ pub mod traits {
type Signer;

/// Like push_data but returns the SigningMessage rather than pushes it into the egress queue.
fn prepare_with_data(&self, data: alloc::vec::Vec<u8>, to: impl Into<Path>) -> SigningMessage<Self::Signer>;
fn prepare_with_data(
&self,
data: alloc::vec::Vec<u8>,
to: impl Into<Path>,
) -> SigningMessage<Self::Signer>;
/// Like push_message_to but returns the SigningMessage rather than pushes it into the egress queue.
fn prepare_message_to(&self, message: &impl Encode, to: impl Into<Path>) -> SigningMessage<Self::Signer> {
fn prepare_message_to(
&self,
message: &impl Encode,
to: impl Into<Path>,
) -> SigningMessage<Self::Signer> {
self.prepare_with_data(message.encode(), to)
}
/// Like push_message but returns the SigningMessage rather than pushes it into the egress queue.
fn prepare_message<M: Encode + BindTopic>(&self, message: &M) -> SigningMessage<Self::Signer> {
fn prepare_message<M: Encode + BindTopic>(
&self,
message: &M,
) -> SigningMessage<Self::Signer> {
self.prepare_message_to(message, M::topic())
}
}
Expand Down
14 changes: 12 additions & 2 deletions crates/phala-mq/src/send_queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::{
use alloc::{collections::BTreeMap, sync::Arc, vec::Vec};
use serde::{Deserialize, Serialize};

#[derive(Default, Serialize, Deserialize)]
struct Channel {
#[derive(Default, Serialize, Deserialize, Clone)]
pub struct Channel {
sequence: u64,
messages: Vec<SignedMessage>,
dummy: bool,
Expand Down Expand Up @@ -127,6 +127,16 @@ impl MessageSendQueue {
v.messages.retain(|msg| msg.sequence >= seq);
}
}

pub fn dump_state(&self, sender: &SenderId) -> Option<Channel> {
let inner = self.inner.lock();
inner.get(sender).cloned()
}

pub fn load_state(&self, sender: &SenderId, state: Channel) {
let mut inner = self.inner.lock();
inner.insert(sender.clone(), state);
}
}

pub use msg_channel::*;
Expand Down
15 changes: 11 additions & 4 deletions crates/phala-mq/src/simple_mpsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use derive_more::Display;
struct Channel<T> {
deque: VecDeque<T>,
sender_count: usize,
receiver_gone: bool,
receiver_count: usize,
}

impl<T> Channel<T> {
Expand All @@ -19,7 +19,7 @@ impl<T> Channel<T> {
Self {
deque: VecDeque::with_capacity(cap),
sender_count: 1,
receiver_gone: false,
receiver_count: 1,
}
}
}
Expand All @@ -36,7 +36,7 @@ pub enum SendError {
impl<T> Sender<T> {
pub fn send(&self, value: T) -> Result<(), SendError> {
let mut ch = self.0.lock();
if ch.receiver_gone {
if ch.receiver_count == 0 {
Err(SendError::ReceiverGone)
} else {
ch.deque.push_back(value);
Expand Down Expand Up @@ -112,7 +112,14 @@ impl<T: Seq> Receiver<T> {

impl<T> Drop for Receiver<T> {
fn drop(&mut self) {
self.0.lock().receiver_gone = true;
self.0.lock().receiver_count -= 1;
}
}

impl<T> Clone for Receiver<T> {
fn clone(&self) -> Receiver<T> {
self.0.lock().receiver_count += 1;
Receiver(self.0.clone())
}
}

Expand Down
12 changes: 6 additions & 6 deletions crates/phala-node-rpc-ext/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ codec = { package = "parity-scale-codec", version = "3.3" }
scale-info = { version = "2.3", default-features = false }

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

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

phala-mq = { path = "../../crates/phala-mq" }
phala-pallets = { path = "../../pallets/phala" }
Expand Down
28 changes: 14 additions & 14 deletions crates/phala-pallet-common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,25 @@ codec = { package = "parity-scale-codec", version = "3.0", default-features = fa
scale-info = { version = "2.0", default-features = false, features = ["derive", "serde", "decode"] }

# Substrate
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }

# Polkadot
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.39", default-features = false }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.40", default-features = false }

[dev-dependencies]
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
frame-system = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
# Polkadot
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.39" }
xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.40" }

[features]
default = ["std"]
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.39", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }
scale = { package = "parity-scale-codec", version = "3.3", default-features = false }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }

Expand Down
16 changes: 8 additions & 8 deletions crates/phala-trie-storage/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ repository = "https://github.com/Phala-Network/phala-blockchain"
[dependencies]
parity-scale-codec = { version = "3.3", default-features = false }
scale-info = { version = "2.3", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", features = ["full_crypto"] }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", features = ["full_crypto"] }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-state-machine = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }

serde = { version = "1.0", default-features = false, features = ["derive", "alloc"], optional = true }
hash-db = "0.15.2"
trie-db = "0.25.1"
hash-db = "0.16.0"
trie-db = "0.27.1"
im = { version = "15", features = ["serde"] }
log = "0.4"

[dev-dependencies]
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", features = ["full_crypto"] }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40" }
sp-application-crypto = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", features = ["full_crypto"] }
hash256-std-hasher = { version = "0.15", default-features = false }
hex = "0.4"
serde_json = "1.0"
impl-serde = "0.4.0"
keccak-hasher = "0.15.3"
keccak-hasher = "0.16.0"

[features]
default = ["serde"]
2 changes: 1 addition & 1 deletion crates/phala-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ 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.3", default-features = false, features = ["full"] }
scale-info = { version = "2.3", default-features = false, features = ["derive"] }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.39", default-features = false }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.40", default-features = false }

phala-mq = { path = "../../crates/phala-mq", default-features = false }
prpc = { path = "../../crates/prpc", default-features = false }
Expand Down
6 changes: 5 additions & 1 deletion crates/phala-types/src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ pub mod messaging {
DispatchKeys(BatchDispatchClusterKeyEvent),
/// Force destroying a cluster.
///
/// This leaves a door to clean up the beta clusters in fat v1.
/// This leaves a door to clean up the beta clusters in phat v1.
/// We might need to redesign a more graceful one in the future.
DestroyCluster(ContractClusterId),
/// Upload ink code to the cluster.
Expand All @@ -159,6 +159,10 @@ pub mod messaging {
account: AccountId,
amount: u128,
},
RemoveWorker {
cluster_id: ContractClusterId,
worker: WorkerPublicKey,
},
}

impl<AccountId> ClusterOperation<AccountId> {
Expand Down
1 change: 1 addition & 0 deletions crates/phala-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,7 @@ pub enum SignedContentType {
EndpointInfo = 2,
MasterKeyRotation = 3,
MasterKeyStore = 4,
ClusterStateRequest = 5,
}

pub fn wrap_content_to_sign(data: &[u8], sigtype: SignedContentType) -> Cow<[u8]> {
Expand Down
Loading