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

send personhood to node template #37

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
494 changes: 448 additions & 46 deletions Cargo.lock

Large diffs are not rendered by default.

19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ cargo build --release
cp target/release/integritee-node ../personhood-oracle/bin/
```

build substrate node-template

```
git clone https://github.com/paritytech/substrate.git
cd substrate
git checkout polkadot-v0.9.42
cargo build --release -p node-template
cp target/release/node-template ../personhood-oracle/bin/
```

setup development environment in docker:

```
Expand Down Expand Up @@ -104,3 +114,12 @@ you may need to make sure to subscribe to relay.damus.io in settings

find the badge definition published here:
https://badges.page/b/naddr1qqx8qetjwdhku6r0daj97vgzypz2eydm7k6h8cs4wf9n5ylwux8vatzc9sdhjqnw02nnnx7kkuvluqcyqqq82wgtjchvs


## helpful snippets for demo screencast

echo "" > ../log/node1.log
export GREP_COLOR='1;33;96'
clear && tail -f ../log/node1.log | sed -u 's/^.*tokio-runtime-worker\s*//' | grep --color 'teerex\|$'
source ~/.bashrc > ../log/worker1.log
clear && tail -n20 -f ../log/worker1.log | grep --color 'SomethingStored\|$'
89 changes: 89 additions & 0 deletions cli/src/personhood_oracle/commands/issue_node_template_xt.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2021 Integritee AG and Supercomputing Systems AG
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

use crate::{
command_utils::{get_accountid_from_str, get_chain_api, get_worker_api_direct},
Cli,
};
use codec::{Decode, Encode};
use encointer_primitives::communities::CommunityIdentifier;
use itc_rpc_client::direct_client::DirectApi;
use itp_rpc::{RpcRequest, RpcResponse, RpcReturnValue};
use itp_types::DirectRequestStatus;
use itp_utils::FromHexPrefixed;
use log::*;
use std::str::FromStr;

#[derive(Debug, Clone, Parser)]
pub struct IssueNodeTemplateXtCmd {
pub account: String,
pub node_template_idx: u32,
pub cid: String,
// TODO add proofs
// pub proofs: Vec<ProofOfAttendance>
}

use crate::personhood_oracle::commands::fetch_reputation::get_ceremony_index;

impl IssueNodeTemplateXtCmd {
pub fn run(&self, cli: &Cli) {
//todo!();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this todo about?

let api = get_chain_api(cli);
let direct_api = get_worker_api_direct(cli);
let cindex = get_ceremony_index(&api);

let cid = CommunityIdentifier::from_str(&self.cid).unwrap();
let account = get_accountid_from_str(&self.account);
let node_template_idx = self.node_template_idx;

let rpc_params =
vec![cid.encode(), cindex.encode(), account.encode(), node_template_idx.encode()];
trace!("rpc_params is : {:?}", &rpc_params);

let rpc_params = rpc_params
.into_iter()
.map(|p| itp_utils::hex::hex_encode(p.as_slice()))
.collect();
Comment on lines +53 to +56
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

uii, if this is necessary for our rpc api, then we have to improve it, but it indeed seems that we use a similar approach in the worker.


let rpc_method = "personhoodoracle_issueNodeTemplateXt".to_owned();
let jsonrpc_call: String =
RpcRequest::compose_jsonrpc_call(rpc_method, rpc_params).unwrap();

let rpc_response_str_result = direct_api.get(&jsonrpc_call);
debug!("rpc_response_str_result is:{:?}", &rpc_response_str_result);
let rpc_response_str = rpc_response_str_result.unwrap();

// Decode RPC response.
let Ok(rpc_response) = serde_json::from_str::<RpcResponse>(&rpc_response_str) else {
panic!("Can't parse RPC response: '{rpc_response_str}'");
};
let rpc_return_value = match RpcReturnValue::from_hex(&rpc_response.result) {
Ok(rpc_return_value) => rpc_return_value,
Err(e) => panic!("Failed to decode RpcReturnValue: {:?}", e),
};

match rpc_return_value.status {
DirectRequestStatus::Ok => {
println!("NodeTemplate Index has been issued successfully.");
},
_ => {
let error_msg = "Node Template Xt issuance failed";
error!("{}", &error_msg);
let inner_error_msg: String =
Decode::decode(&mut rpc_return_value.value.as_slice())
.expect("Failed to decode node tempölate xt issuing RPC error msg");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo

error!("Node Template issuing failed: {:#?}", &inner_error_msg);
},
}
}
}
6 changes: 5 additions & 1 deletion cli/src/personhood_oracle/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
*/

mod fetch_reputation;
mod issue_node_template_xt;
mod issue_nostr_badge;

pub use self::{fetch_reputation::FetchReputationCmd, issue_nostr_badge::IssueNostrBadgeCmd};
pub use self::{
fetch_reputation::FetchReputationCmd, issue_node_template_xt::IssueNodeTemplateXtCmd,
issue_nostr_badge::IssueNostrBadgeCmd,
};
7 changes: 4 additions & 3 deletions cli/src/personhood_oracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,27 @@

use crate::{
personhood_oracle::{
commands::{FetchReputationCmd, IssueNostrBadgeCmd},
PersonhoodOracleCommand::{FetchReputation, IssueNostrBadge},
commands::{FetchReputationCmd, IssueNodeTemplateXtCmd, IssueNostrBadgeCmd},
PersonhoodOracleCommand::{FetchReputation, IssueNodeTemplateXt, IssueNostrBadge},
},
Cli,
};

mod commands;

/// Oracle subcommands for the cli.
#[derive(Debug, clap::Subcommand)]
pub enum PersonhoodOracleCommand {
FetchReputation(FetchReputationCmd),
IssueNostrBadge(IssueNostrBadgeCmd),
IssueNodeTemplateXt(IssueNodeTemplateXtCmd),
}

impl PersonhoodOracleCommand {
pub fn run(&self, cli: &Cli) {
match self {
FetchReputation(fetch_reputation_cmd) => fetch_reputation_cmd.run(cli),
IssueNostrBadge(issue_nostr_badge_cmd) => issue_nostr_badge_cmd.run(cli),
IssueNodeTemplateXt(issue_node_template_xt_cmd) => issue_node_template_xt_cmd.run(cli),
}
}
}
1 change: 1 addition & 0 deletions core-primitives/extrinsics-factory/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ thiserror = { version = "1.0", optional = true }
# no-std dependencies
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
log = { version = "0.4", default-features = false }
hex = { version = "0.4.3", default-features = false, features = ["alloc"] }
sp-core = { default-features = false, features = ["full_crypto"], git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.42" }

Expand Down
24 changes: 21 additions & 3 deletions core-primitives/extrinsics-factory/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use itp_node_api::{
};
use itp_nonce_cache::{MutateNonce, Nonce};
use itp_types::{parentchain::AccountId, OpaqueCall};
use log::*;
use sp_core::H256;
use sp_runtime::{generic::Era, OpaqueExtrinsic};
use std::{sync::Arc, vec::Vec};
Expand Down Expand Up @@ -112,6 +113,16 @@ where
(m.get_runtime_version(), m.get_runtime_transaction_version())
})?;

debug!(
"extrinsic factory creates {} extrinsics for chain with genesis hash: {:?}",
calls.len(),
self.genesis_hash
);
trace!("runtime_spec_version: {}", runtime_spec_version);
trace!("runtime_transaction_version: {}", runtime_spec_version);
trace!("additional extrinsic params: {:?}", additional_extrinsic_params);
trace!("start with nonce: {:?}", nonce_value);

let extrinsics_buffer: Vec<OpaqueExtrinsic> = calls
.iter()
.map(|call| {
Expand All @@ -123,17 +134,24 @@ where
additional_extrinsic_params,
);
let xt = compose_extrinsic_offline!(&self.signer, call, extrinsic_params).encode();
trace!(
"encoded extrinsic with nonce {}: 0x{}",
nonce_value,
hex::encode(xt.clone())
Comment on lines +137 to +140
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd remove this trace as it introduces a clone

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the idea here is to format the encoded call exactly the way it could be copy-pasted and inspected with js/apps. So I'd rather leave it as is - unless you have a way to achieve exactly the same without the clone

);
nonce_value += 1;
xt
})
.map(|xt| {
OpaqueExtrinsic::from_bytes(&xt)
.expect("A previously encoded extrinsic has valid codec; qed.")
let oe = OpaqueExtrinsic::from_bytes(&xt)
.expect("A previously encoded extrinsic has valid codec; qed.");
trace!("opaque extrinsic: {:?}", oe);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this the log that is empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

possibly

oe
})
.collect();

*nonce_lock = Nonce(nonce_value);

trace!("created {} extrinsics", extrinsics_buffer.len());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have the same log outside of the enclave, do we want to keep this one?

Ok(extrinsics_buffer)
}
}
Expand Down
15 changes: 15 additions & 0 deletions core-primitives/types/src/parentchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ use codec::{Decode, Encode};
use sp_runtime::{generic::Header as HeaderG, traits::BlakeTwo256, MultiAddress, MultiSignature};
use sp_std::vec::Vec;

#[cfg(feature = "std")]
use std::fmt::Display;

pub type StorageProof = Vec<Vec<u8>>;

// Basic Types.
Expand Down Expand Up @@ -52,6 +55,18 @@ pub enum ParentchainId {
TargetB,
}

#[cfg(feature = "std")]
impl Display for ParentchainId {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
let message = match self {
ParentchainId::Integritee => "L1:Integritee",
ParentchainId::TargetA => "L1:Encointer",
ParentchainId::TargetB => "L1:NodeTemplate",
};
write!(f, "{}", message)
}
}

Comment on lines +58 to +69
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like this to be upstreamed and customizeable. but it should work inside enclave too and should replace all log prefixes

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Display is part of core, so it can work inside the enclave too.

I am unsure if it makes sense to replace the general log prefixes. It is not the same as in the substrate runtime, where we know that the encointer logger target is always in the runtime. Here it can be any crate in a fairly complex setup, so we should keep the crate path in in the log IMO.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pardon, I didn't mean the pallet log prefixes. I meant the ones where we deliberately printed TargetA/B/Integritee before.
crate path in the log is fine, but the parentchain should become part of the prefix or be therwise prepended to the log message where it makes sense

pub trait IdentifyParentchain {
fn parentchain_id(&self) -> ParentchainId;
}
2 changes: 1 addition & 1 deletion core/parentchain/light-client/src/light_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ where
for xt in extrinsics.iter() {
self.submit_xt_to_be_included(xt.clone());
}

trace!("ocall: sending {} extrinsics to {:?}", extrinsics.len(), self.parentchain_id);
self.ocall_api
.send_to_parentchain(extrinsics, &self.parentchain_id)
.map_err(|e| {
Expand Down
1 change: 1 addition & 0 deletions enclave-runtime/Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2071,6 +2071,7 @@ dependencies = [
name = "itp-extrinsics-factory"
version = "0.9.0"
dependencies = [
"hex",
"itp-node-api",
"itp-nonce-cache",
"itp-types",
Expand Down
24 changes: 10 additions & 14 deletions enclave-runtime/src/rpc/encointer_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use itp_component_container::ComponentGetter;
use itp_ocall_api::EnclaveOnChainOCallApi;
use itp_stf_primitives::types::AccountId;
use itp_storage::{storage_double_map_key, StorageHasher};
use itp_types::{WorkerRequest, WorkerResponse, parentchain::ParentchainId};
use itp_types::{parentchain::ParentchainId, WorkerRequest, WorkerResponse};
use log::*;
use std::cmp::min;

Expand Down Expand Up @@ -52,12 +52,7 @@ fn get_reputation_ocall_api(
cid: CommunityIdentifier,
cindex: CeremonyIndexType,
) -> Reputation {
println!(
"requesting reputation for {:?}: cid is :{}, cindex is: {}",
prover,
cid,
cindex.clone()
);
info!("requesting reputation for {:?}: cid is :{}, cindex is: {}", prover, cid, cindex.clone());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cindex is a copy type, so we shouldn't need the clone.

let unverified_reputation = Reputation::Unverified;

let ocall_api = GLOBAL_OCALL_API_COMPONENT.get();
Expand All @@ -77,13 +72,14 @@ fn get_reputation_ocall_api(
trace!("storage_hash is : {}", hex::encode(storage_hash.clone()));

let requests = vec![WorkerRequest::ChainStorage(storage_hash, None)];
let mut resp: Vec<WorkerResponse<Vec<u8>>> = match ocall_api.worker_request(requests, &ParentchainId::TargetA) {
Ok(response) => response,
Err(e) => {
error!("Worker response decode failed. Error: {:?}", e);
return unverified_reputation
},
};
let mut resp: Vec<WorkerResponse<Vec<u8>>> =
match ocall_api.worker_request(requests, &ParentchainId::TargetA) {
Ok(response) => response,
Err(e) => {
error!("Worker response decode failed. Error: {:?}", e);
return unverified_reputation
},
};

let first = match resp.pop() {
None => {
Expand Down
Loading