Skip to content

Commit

Permalink
encointer_utils: fetch with readproof
Browse files Browse the repository at this point in the history
  • Loading branch information
OverOrion committed Jul 31, 2023
1 parent f282e53 commit 4f3f802
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions enclave-runtime/src/rpc/encointer_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,20 @@ use crate::{
initialization::global_components::GLOBAL_OCALL_API_COMPONENT,
utils::get_validator_accessor_from_solo_or_parachain, Vec,
};
use codec::Decode;
use encointer_primitives::{
ceremonies::Reputation, communities::CommunityIdentifier, scheduler::CeremonyIndexType,
};
use ita_stf::helpers::get_storage_by_key_hash;
use itc_parentchain::light_client::{concurrent_access::ValidatorAccess, LightClientState};
use itp_types::storage::StorageEntryVerified;
use sp_core::crypto::Ss58Codec;

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_utils::stringify::account_id_to_string;
use itp_storage::{storage_double_map_key, StorageHasher, VerifyStorageProof};
use itp_types::{
storage::{StorageEntry, StorageEntryVerified},
WorkerRequest, WorkerResponse,
};
use log::error;

pub fn fetch_reputation(
Expand Down Expand Up @@ -73,7 +75,6 @@ fn get_reputation_ocall_api(
cindex: CeremonyIndexType,
) -> Reputation {
println!("cid is :{}, cindex is: {}", cid, cindex.clone());
println!("prover is :{}", prover.to_ss58check());
let validator_access =
get_validator_accessor_from_solo_or_parachain().expect("Failed to get validator access");
let current_parentchain_header = validator_access
Expand All @@ -86,7 +87,6 @@ fn get_reputation_ocall_api(
.expect("Failed to get current_parentchain_header");

let ocall_api = GLOBAL_OCALL_API_COMPONENT.get().expect("Failed to get OCALL API");
// let storage_hash = storage_double_map_key::<K, Q>(
let storage_hash = storage_double_map_key(
"EncointerCeremonies",
"ParticipantReputation",
Expand All @@ -95,11 +95,27 @@ fn get_reputation_ocall_api(
prover,
&StorageHasher::Blake2_128Concat,
);
let key_and_value: StorageEntryVerified<Reputation> = ocall_api
.get_storage_verified(storage_hash, &current_parentchain_header)
.expect("Failed to read storage");
match key_and_value.value() {
println!("storage_hash is :{:#?}", &storage_hash);

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

let first = resp.pop().expect("Worker should have responded");
println!("Worker response: {:?}", first);

let (_key, value, _proof) = match first {
WorkerResponse::ChainStorage(storage_key, value, proof) => (storage_key, value, proof),
};

match value {
None => Reputation::Unverified,
Some(v) => *v,
Some(v) => {
let reputation: Reputation =
Decode::decode(&mut v.as_slice()).expect("Failed to decode value after fetching");
reputation
},
}
}

0 comments on commit 4f3f802

Please sign in to comment.