Skip to content

Commit

Permalink
pink: Connect pink egress to phala-mq
Browse files Browse the repository at this point in the history
  • Loading branch information
kvinwang committed Oct 20, 2021
1 parent 3f909c4 commit f02a901
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
5 changes: 2 additions & 3 deletions crates/phactory/src/contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ mod support {

pub struct NativeContext<'a> {
pub block: &'a BlockInfo<'a>,
mq: &'a SignedMessageChannel,
#[allow(unused)] // TODO.kevin: remove this.
secret_mq: SecretMessageChannel<'a, SignedMessageChannel>,
pub mq: &'a SignedMessageChannel,
pub secret_mq: SecretMessageChannel<'a, SignedMessageChannel>,
pub contract_groups: &'a mut GroupKeeper,
}

Expand Down
34 changes: 33 additions & 1 deletion crates/phactory/src/contracts/pink.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ use crate::contracts;
use crate::system::{TransactionError, TransactionResult};
use anyhow::{anyhow, Result};
use parity_scale_codec::{Decode, Encode};
use phala_mq::traits::MessageChannel;
use phala_mq::{ContractGroupId, ContractId, MessageOrigin};
use runtime::AccountId;
use scopeguard::ScopeGuard;

#[derive(Debug, Encode, Decode)]
pub enum Command {
Expand Down Expand Up @@ -71,6 +73,11 @@ impl contracts::NativeContract for Pink {
Query::InkMessage(input_data) => {
let storage = group_storage(&mut context.contract_groups, &self.group)
.expect("Pink group should always exists!");

scopeguard::defer! {
let _ = ::pink::runtime::take_mq_egress();
};

let ret = self
.instance
.bare_call(storage, origin.clone(), input_data, true)
Expand Down Expand Up @@ -99,15 +106,40 @@ impl contracts::NativeContract for Pink {
let storage = group_storage(&mut context.contract_groups, &self.group)
.expect("Pink group should always exists!");

let msgs = ::pink::runtime::take_mq_egress();
if !msgs.is_empty() {
error!(target: "pink", "BUG: some messages in runtime cache before call");
}

let clear_mq_guard = scopeguard::guard((), |()| {
let _ = ::pink::runtime::take_mq_egress();
});

let ret = self
.instance
.bare_call(storage, origin.clone(), message, false)
.map_err(|err| {
log::error!("Pink [{:?}] command exec error: {:?}", self.id(), err);
TransactionError::Other(format!("Call contract method failed: {:?}", err))
})?;
// TODO.kevin: report the output to the chain?

// TODO.kevin: store the output to some where.
let _ = ret;

let _ = ScopeGuard::into_inner(clear_mq_guard);

let msgs = ::pink::runtime::take_mq_egress();

for message in msgs.messages {
context.mq.push_data(message.payload, message.topic);
}

for message in msgs.osp_messages {
context
.secret_mq
.bind_remote_key(message.remote_pubkey.as_ref())
.push_data(message.payload, message.topic);
}
}
}
Ok(())
Expand Down

0 comments on commit f02a901

Please sign in to comment.