Skip to content
This repository has been archived by the owner on Feb 14, 2021. It is now read-only.

Commit

Permalink
Feature payment (#8)
Browse files Browse the repository at this point in the history
* Update client package dependencies
* Update enclave package dependencies
* Update substrate-api dependencies
* Add complete task api
* Add get_balance api support
* Modify advanca-sgx-helper to latest ver
* Add support for balance printing
* Update Cargo.toml and lock files
* Cargo fmt
* refactor client to use subxt
* format code
* substrate-api v0.3.0: use subxt internally
* Update to use advanca-node-primitives
* address multiple issues in substrate-api
- fix `TypeSizeUnavailable` issue on `TaskId` when `transfer_balance`
-fix `TypeSizeUnavailable` issue on `Status` when `complete_task`
- fix function `wait_all_task_aborted`
* update Cargo.lock and format code
* format code
* cleanup
* Swap to serde_json from serde_cbor
* Add sr25519 key support + refactor worker->enclave
* Update sr25519 support for enclave
* Add TaskCompletedEvent listening api
* Add sr25519 support, add TaskCompleted event check
* Update sr25519 support
* Cargo fmt
* Update Cargo.toml and Cargo.lock
* Update sgx-helper, substrate-subxt, node => tag
* Update Cargo.lock

Co-authored-by: Gong Deli <[email protected]>
  • Loading branch information
melynx and Gnnng authored Aug 22, 2020
1 parent 749afa3 commit c0790d4
Show file tree
Hide file tree
Showing 17 changed files with 2,396 additions and 2,056 deletions.
2,692 changes: 1,408 additions & 1,284 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ exclude = [
#substrate-api-client = { path = "../substrate-api-client" }

#[patch.'https://github.com/advanca/advanca-sgx-helper']
#advanca-crypto-types = { path = "../advanca-sgx-helper/advanca-crypto-types" }
#advanca-crypto = { path = "../../advanca-sgx-helper/advanca-crypto" }
#advanca-crypto-types = { path = "../../advanca-sgx-helper/advanca-crypto-types" }
#advanca-macros = { path = "../../advanca-sgx-helper/advanca-macros" }

[patch.crates-io]
protobuf-build = { rev = "1c76baf642c9abc6428c94c66f4f16304c77950e", git = "https://github.com/advanca/protobuf-build.git" }
Expand Down
30 changes: 17 additions & 13 deletions app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ version = "0.4.0"
log = "0.4"
env_logger = "0.7.1"
serde_json = "1.0"
serde_cbor = { version = "0.11.1"}
hex = "0.4.2"
futures = "0.3"
grpcio = { rev = "249e21b3593a8bdd58429d2552b9ab72d6d46526", git = "https://github.com/tikv/grpc-rs.git", default-features = false, features = ["openssl", "protobuf-codec"] }
Expand All @@ -21,30 +20,35 @@ structopt = "0.3"
jsonrpsee = { version = "0.1", features = ["ws"] }
async-std = { version = "1" }

sp-core = { git = "https://github.com/paritytech/substrate", rev = "00a400f82539e2f78e8ddbcd98aea512c87c5f3c"}
sp-keyring = { git = "https://github.com/paritytech/substrate", rev = "00a400f82539e2f78e8ddbcd98aea512c87c5f3c"}
sp-runtime = {rev = '00a400f82539e2f78e8ddbcd98aea512c87c5f3c', git = 'https://github.com/paritytech/substrate.git'}
sp-core = { tag = "v2.0.0-rc5+2", git = "https://github.com/paritytech/substrate"}
sp-keyring = { tag = "v2.0.0-rc5+2", git = "https://github.com/paritytech/substrate"}
sp-runtime = { tag = "v2.0.0-rc5+2", git = 'https://github.com/paritytech/substrate.git'}
# use the pinned substrate revision's primitive type version
primitive-types = "0.6.2"
primitive-types = "0.7.2"

sgx_types = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
sgx_urts = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
sgx_tseal = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-sdk.git" }
sgx_crypto_helper = { rev = "v1.1.2", git = "https://github.com/apache/teaclave-sgx-sdk.git" }

advanca-macros = { tag = "v0.3.0", git = "https://github.com/advanca/advanca-sgx-helper" }
advanca-crypto = { tag = "v0.3.0", git = "https://github.com/advanca/advanca-sgx-helper" }
advanca-crypto-types = { tag = "v0.3.0", git = "https://github.com/advanca/advanca-sgx-helper" }
sgx-ra = { tag = "v0.3.0", git = "https://github.com/advanca/advanca-sgx-helper" }
advanca-runtime = { tag = "v0.2.0", git = "https://github.com/advanca/advanca-node/" }
advanca-core = { tag = "v0.2.0", git = "https://github.com/advanca/advanca-node/" }
advanca-macros = { tag = "v0.4.0", git = "https://github.com/advanca/advanca-sgx-helper" }
advanca-crypto = { tag = "v0.4.0", git = "https://github.com/advanca/advanca-sgx-helper", features = ["std_env"] }
advanca-crypto-types = { tag = "v0.4.0", git = "https://github.com/advanca/advanca-sgx-helper", features = ["std_env"] }
sgx-ra = { tag = "v0.4.0", git = "https://github.com/advanca/advanca-sgx-helper" }
advanca-node-primitives = { tag = "v0.4.0", git = "https://github.com/advanca/advanca-node/" }

aas-protos-std = { tag = "v0.2.0", git = "https://github.com/advanca/advanca-attestation-service" }
#advanca-macros = { path = "../../../advanca-sgx-helper/advanca-macros/" }
#advanca-crypto = { path = "../../../advanca-sgx-helper/advanca-crypto/", features = ["std_env"] }
#advanca-crypto-types = { path = "../../../advanca-sgx-helper/advanca-crypto-types/", features = ["std_env"] }
#sgx-ra = { path = "../../../advanca-sgx-helper/sgx-ra" }
#advanca-node-primitives = { path = "../../advanca-node/advanca-node-primitives/" }

aas-protos-std = { tag = "v0.3.0", git = "https://github.com/advanca/advanca-attestation-service" }
worker-protos-std = { path = "../worker-protos/worker-protos-std" }

[dependencies.codec]
package = "parity-scale-codec"
version = "1.0.0"
version = "1.3.0"

[dependencies.substrate-api]
path = "../substrate-api"
14 changes: 8 additions & 6 deletions app/src/enclave/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,23 +112,25 @@ pub fn init() -> SgxResult<SgxEnclave> {
Ok(enclave)
}

pub fn sr25519_public_key(eid: sgx_enclave_id_t) -> SgxResult<Vec<u8>> {
let public_key_size = 32;
let mut public_key = vec![0u8; public_key_size as usize];
pub fn enclave_sr25519_public_key(eid: sgx_enclave_id_t) -> SgxResult<Sr25519PublicKey> {
let mut buf_size = 1024;
let mut buf = vec![0_u8; buf_size];

let _ = unsafe {
handle_ecall!(
eid,
get_sr25519_public_key(public_key.as_mut_ptr(), public_key_size)
get_worker_sr25519_pubkey(buf.as_mut_ptr(), &mut buf_size)
)
.unwrap()
};

Ok(public_key)
let publickey: Sr25519PublicKey = serde_json::from_slice(&buf[..buf_size]).unwrap();

Ok(publickey)
}

pub fn create_storage(eid: sgx_enclave_id_t, owner: Secp256r1PublicKey) -> SgxError {
let public_key_bytes = serde_cbor::to_vec(&owner).unwrap();
let public_key_bytes = serde_json::to_vec(&owner).unwrap();

// we'll keep the current interface
let mut status = sgx_status_t::SGX_SUCCESS;
Expand Down
110 changes: 87 additions & 23 deletions app/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

use std::convert::TryFrom;
use std::str;
use std::sync::mpsc;
use std::thread;
Expand All @@ -29,8 +28,7 @@ use sp_keyring::AccountKeyring;

use substrate_api::SubstrateApi;

use advanca_core::Enclave;
use advanca_runtime::AccountId;
use advanca_node_primitives::{AccountId, Enclave};

mod enclave;
mod grpc;
Expand Down Expand Up @@ -96,6 +94,16 @@ struct Opt {
aas_url: String,
}

fn display_balance(account_id: AccountId, api: &SubstrateApi) {
let accountdata = api.get_balance(account_id);
info!("{:=^80}", "Worker Balance Information");
info!("Free : {:?}", accountdata.free);
info!("Reserved : {:?}", accountdata.reserved);
info!("Misc Frozen : {:?}", accountdata.misc_frozen);
info!("Fee Frozen : {:?}", accountdata.fee_frozen);
info!("{:=^80}", "");
}

async fn aas_remote_attest(
client: Arc<Mutex<AasServerClient>>,
eid: sgx_enclave_id_t,
Expand Down Expand Up @@ -185,7 +193,7 @@ async fn aas_remote_attest(
// aas accepted our attestation, we'll prepare the request
let mut buf = [0_u8; 4096];
let mut buf_size: usize = buf.len();
let _ = unsafe { handle_ecall!(eid, gen_worker_ec256_pubkey()).unwrap() };
let _ = unsafe { handle_ecall!(eid, gen_worker_key()).unwrap() };
let _ = unsafe {
handle_ecall!(
eid,
Expand All @@ -205,7 +213,7 @@ async fn aas_remote_attest(

assert_eq!(msg_aas_report.get_msg_type(), MsgType::AAS_RA_REG_REPORT);
let aas_report_bytes = msg_aas_report.get_msg_bytes();
let aas_report: AasRegReport = serde_cbor::from_slice(aas_report_bytes).unwrap();
let aas_report: AasRegReport = serde_json::from_slice(aas_report_bytes).unwrap();
// 04:1a:4f:ea:0d:04:bd:ed:7d:c1:43:ee:74:cb:8e:
// 56:9e:6e:49:1c:89:bc:d6:5c:34:8f:8a:5b:40:5f:
// 79:53:e3:89:7d:0f:0c:bc:cf:f0:45:ce:c9:a9:1d:
Expand Down Expand Up @@ -306,12 +314,31 @@ fn main() {
)
.unwrap()
};
let worker_pubkey: Secp256r1PublicKey = serde_cbor::from_slice(&buf[..buf_size]).unwrap();
info!("ec256 pubkey generated {:?}", worker_pubkey);
let enclave_sec256p1_pubkey: Secp256r1PublicKey =
serde_json::from_slice(&buf[..buf_size]).unwrap();
info!("ec256 pubkey generated {:?}", enclave_sec256p1_pubkey);

buf_size = buf.len();
let _ = unsafe {
handle_ecall!(
eid,
get_worker_sr25519_pubkey(buf.as_mut_ptr(), &mut buf_size)
)
.unwrap()
};
let enclave_sr25519_pubkey: Sr25519PublicKey =
serde_json::from_slice(&buf[..buf_size]).unwrap();
info!(
"enclave sr25519 pubkey generated {:?}",
enclave_sr25519_pubkey
);

let (worker_keypair, _) = sr25519::Pair::generate();
let worker_account: AccountId = worker_keypair.public().as_array_ref().to_owned().into();
info!("sr25519 keypair generated {:?}", worker_keypair.public());
info!(
"worker sr25519 keypair generated {:?}",
worker_keypair.public()
);

// inject funds into worker account
fund_account(&opt.ws_url, &worker_account);
Expand All @@ -320,22 +347,30 @@ fn main() {
let mut api = SubstrateApi::new(&opt.ws_url);
api.set_signer(worker_keypair.clone());

display_balance(worker_account.clone(), &api);

// get the keys from enclave
let sr25519_public_key = sr25519::Public::try_from(
&enclave::sr25519_public_key(e.geteid()).expect("enclave sr25519 public key")[..],
)
.unwrap(); //.try_into().unwrap();
let sr25519_public_key = enclave::enclave_sr25519_public_key(e.geteid())
.expect("enclave sr25519 public key")
.to_schnorrkel_public();

let enclave_pubkeys = advanca_node_primitives::PublicKeys {
secp256r1_public_key: serde_json::to_vec(&enclave_sec256p1_pubkey).unwrap(),
sr25519_public_key: serde_json::to_vec(&enclave_sr25519_pubkey).unwrap(),
};

let enclave = Enclave::<AccountId> {
account_id: sr25519_public_key.as_array_ref().to_owned().into(),
public_key: serde_cbor::to_vec(&worker_pubkey).unwrap(),
attestation: serde_cbor::to_vec(&aas_report).unwrap(),
account_id: sr25519_public_key.to_bytes().to_owned().into(),
public_keys: enclave_pubkeys,
attestation: serde_json::to_vec(&aas_report).unwrap(),
};

info!("registering worker ...");
let hash = api.register_worker(10, enclave);
info!("registered worker (extrinsic={:?})", hash);

display_balance(worker_account.clone(), &api);

// listen for new task
info!("listening for new task ...");
let task_id = api.listen_for_task_submitted();
Expand All @@ -360,10 +395,11 @@ fn main() {
let user = api.get_user(owner.clone());
info!("received user information (id={})", owner.clone());

let user_pubkey: Secp256r1PublicKey = serde_cbor::from_slice(&user.public_key).unwrap();
let user_pubkey: Secp256r1PublicKey =
serde_json::from_slice(&user.public_keys.secp256r1_public_key).unwrap();
info!("user public_key: {:?}", user_pubkey);
let signed_owner_task_pubkey: Secp256r1SignedMsg =
serde_cbor::from_slice(&task.signed_owner_task_pubkey).unwrap();
serde_json::from_slice(&task.signed_owner_task_secp256r1_pubkey).unwrap();
let verified = secp256r1_verify_msg(&user_pubkey, &signed_owner_task_pubkey).unwrap();
info!("verifying owner task pubkey ... {:?}", verified);
assert_eq!(verified, true);
Expand All @@ -387,10 +423,32 @@ fn main() {
get_task_ec256_pubkey(buf.as_mut_ptr(), &mut buf_size, task_id.as_ptr())
)
};
let signed_task_pubkey: Secp256r1SignedMsg = serde_cbor::from_slice(&buf[..buf_size]).unwrap();
let signed_task_pubkey_bytes = serde_cbor::to_vec(&signed_task_pubkey).unwrap();
let signed_enclave_task_secp256r1_pubkey: Secp256r1SignedMsg =
serde_json::from_slice(&buf[..buf_size]).unwrap();
let signed_enclave_task_secp256r1_pubkey_bytes =
serde_json::to_vec(&signed_enclave_task_secp256r1_pubkey).unwrap();
debug!("user public key is {:?}", user_pubkey);
debug!("signed task public key is {:?}", signed_task_pubkey);
debug!(
"signed task public key is {:?}",
signed_enclave_task_secp256r1_pubkey
);

buf_size = buf.len();
let _ = unsafe {
handle_ecall!(
eid,
get_task_sr25519_pubkey(buf.as_mut_ptr(), &mut buf_size, task_id.as_ptr())
)
};
let signed_enclave_task_sr25519_pubkey: Sr25519SignedMsg =
serde_json::from_slice(&buf[..buf_size]).unwrap();
let signed_enclave_task_sr25519_pubkey_bytes =
serde_json::to_vec(&signed_enclave_task_sr25519_pubkey).unwrap();
debug!("user public key is {:?}", user_pubkey);
debug!(
"signed task public key is {:?}",
signed_enclave_task_sr25519_pubkey
);

let msg = opt.grpc_url.as_bytes();
debug!("url: {:?}", opt.grpc_url);
Expand All @@ -410,7 +468,7 @@ fn main() {
)
.unwrap()
};
let url_encrypted: Aes128EncryptedMsg = serde_cbor::from_slice(&buf[..buf_size]).unwrap();
let url_encrypted: Aes128EncryptedMsg = serde_json::from_slice(&buf[..buf_size]).unwrap();
debug!("msg len: {:?}", msg.len());
debug!("ivcipher len: {:?}", buf_size);
debug!("url_encrypted: {:?}", url_encrypted);
Expand Down Expand Up @@ -449,8 +507,9 @@ fn main() {
);
let hash = api_wrapper.lock().unwrap().accept_task(
task_id,
signed_task_pubkey_bytes,
serde_cbor::to_vec(&url_encrypted).unwrap(),
signed_enclave_task_secp256r1_pubkey_bytes,
signed_enclave_task_sr25519_pubkey_bytes,
serde_json::to_vec(&url_encrypted).unwrap(),
);
info!("accepted task (extrinsic={:?})", hash);

Expand All @@ -469,6 +528,11 @@ fn main() {
.expect("Couldn't join on the watchdog");
info!("watchdong killed");

api_wrapper.lock().unwrap().complete_task(task_id);
info!("complete task: {:?}", task_id);

display_balance(worker_account.clone(), &api_wrapper.lock().unwrap());

let sgx_return = unsafe { enclave_ra_close(eid, &mut retval, ra_context) };
info!("enclave_ra_close: {}", sgx_return);
info!("freeing ra_context: {}", ra_context);
Expand Down
32 changes: 16 additions & 16 deletions app/src/watchdog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,22 +48,22 @@ pub fn print_task_stats(task_id: H256, api: Arc<Mutex<SubstrateApi>>) {
trace!("{:?}", worker_info);
debug!("verifying worker's attestation report");
let worker_attestation_report =
serde_cbor::from_slice(&worker_info.enclave.attestation).unwrap();
serde_json::from_slice(&worker_info.enclave.attestation).unwrap();
assert_eq!(
true,
aas_verify_reg_report(&AAS_PUB_KEY, &worker_attestation_report).unwrap()
);
debug!("get worker's attested pubkey");
let worker_pubkey: Secp256r1PublicKey = worker_attestation_report.worker_pubkey;
debug!("verifying worker's task pubkey");
let signed_worker_task_pubkey =
serde_cbor::from_slice(&task.signed_worker_task_pubkey.unwrap()).unwrap();
debug!("get enclave's attested pubkey");
let enclave_sr25519_pubkey: Sr25519PublicKey = worker_attestation_report.enclave_sr25519_pubkey;
debug!("verifying enclave's task secp256r1 pubkey");
let signed_enclave_task_sr25519_pubkey =
serde_json::from_slice(&task.signed_enclave_task_sr25519_pubkey.unwrap()).unwrap();
assert_eq!(
true,
secp256r1_verify_msg(&worker_pubkey, &signed_worker_task_pubkey).unwrap()
sr25519_verify_msg(&enclave_sr25519_pubkey, &signed_enclave_task_sr25519_pubkey).unwrap()
);
let worker_task_pubkey: Secp256r1PublicKey =
serde_cbor::from_slice(&signed_worker_task_pubkey.msg).unwrap();
let enclave_task_sr25519_pubkey: Sr25519PublicKey =
serde_json::from_slice(&signed_enclave_task_sr25519_pubkey.msg).unwrap();
debug!("iterating over the evidences ...");
let mut verified_evidence = 0;
let mut alive_blocks = HashSet::new();
Expand All @@ -73,20 +73,20 @@ pub fn print_task_stats(task_id: H256, api: Arc<Mutex<SubstrateApi>>) {
let mut data_in = 0;
let mut data_out = 0;
for evidence_bytes in task.worker_heartbeat_evidence {
let signed_timestamp: Secp256r1SignedMsg = serde_cbor::from_slice(&evidence_bytes).unwrap();
let signed_timestamp: Secp256r1SignedMsg = serde_json::from_slice(&evidence_bytes).unwrap();
trace!("verifying aas timestamp...");
assert_eq!(
true,
secp256r1_verify_msg(&AAS_PUB_KEY, &signed_timestamp).unwrap()
);
let timestamp: AasTimestamp = serde_cbor::from_slice(&signed_timestamp.msg).unwrap();
let timestamp: AasTimestamp = serde_json::from_slice(&signed_timestamp.msg).unwrap();
trace!("verifying signed evidence ...");
let signed_evidence: Secp256r1SignedMsg = serde_cbor::from_slice(&timestamp.data).unwrap();
let signed_evidence: Sr25519SignedMsg = serde_json::from_slice(&timestamp.data).unwrap();
assert_eq!(
true,
secp256r1_verify_msg(&worker_task_pubkey, &signed_evidence).unwrap()
sr25519_verify_msg(&enclave_task_sr25519_pubkey, &signed_evidence).unwrap()
);
let evidence: AliveEvidence = serde_cbor::from_slice(&signed_evidence.msg).unwrap();
let evidence: AliveEvidence = serde_json::from_slice(&signed_evidence.msg).unwrap();
trace!("Evidence: {:?}", evidence);
alive_blocks.insert(evidence.block_hash.clone());
storage_in = evidence.storage_in;
Expand Down Expand Up @@ -155,10 +155,10 @@ pub fn watchdog_loop(
.timestamp(&timestamp_request)
.unwrap();
let timestamp: Secp256r1SignedMsg =
serde_cbor::from_slice(&timestamp_response.signed_data).unwrap();
serde_json::from_slice(&timestamp_response.signed_data).unwrap();
trace!("timestamp response: {:?}", timestamp);
// save to vec, prepare to post to chain when watching ends
alive_evidence.push(serde_cbor::to_vec(&timestamp).unwrap());
alive_evidence.push(serde_json::to_vec(&timestamp).unwrap());
if *is_done.lock().unwrap() {
break;
}
Expand Down
Loading

0 comments on commit c0790d4

Please sign in to comment.