Skip to content

Commit

Permalink
aggregate account getters to AccountInfo and rpc `author_getFingerpri…
Browse files Browse the repository at this point in the history
…nt` (#1611)

* aggregate account getters to AccountInfo

* fix clippy and bump version

* add explicit fingerprint getter to simplify dynamic UI setup

* fix clippy

* fix the fix

* revert mrenclave fetch logic for teeracle

* add parentchains info public getter struct

* cleanup

* nesting guess-the-number calls and getters
  • Loading branch information
brenzi authored Oct 10, 2024
1 parent 31f8c7a commit 2dbbfb4
Show file tree
Hide file tree
Showing 36 changed files with 599 additions and 287 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2557,7 +2557,7 @@ dependencies = [

[[package]]
name = "integritee-cli"
version = "0.14.1"
version = "0.14.2"
dependencies = [
"array-bytes 6.1.0",
"base58",
Expand Down Expand Up @@ -2607,7 +2607,7 @@ dependencies = [

[[package]]
name = "integritee-service"
version = "0.14.1"
version = "0.14.2"
dependencies = [
"anyhow",
"async-trait",
Expand Down
166 changes: 96 additions & 70 deletions app-libs/stf/src/getter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
*/

use codec::{Decode, Encode};
use ita_sgx_runtime::{Balances, GuessTheNumber, GuessType, System};
use ita_sgx_runtime::{
Balances, ParentchainIntegritee, ParentchainTargetA, ParentchainTargetB, System,
};
use itp_stf_interface::ExecuteGetter;
use itp_stf_primitives::{
traits::GetterAuthorization,
Expand All @@ -34,9 +36,13 @@ use ita_sgx_runtime::{AddressMapping, HashedAddressMapping};
#[cfg(feature = "evm")]
use crate::evm_helpers::{get_evm_account, get_evm_account_codes, get_evm_account_storages};

use crate::helpers::wrap_bytes;
use itp_sgx_runtime_primitives::types::{Balance, Moment};
use crate::{
guess_the_number::{GuessTheNumberPublicGetter, GuessTheNumberTrustedGetter},
helpers::{shard_vault, wrap_bytes},
};
use itp_sgx_runtime_primitives::types::Moment;
use itp_stf_primitives::traits::PoolTransactionValidation;
use itp_types::parentchain::{BlockNumber, Hash, ParentchainId};
#[cfg(feature = "evm")]
use sp_core::{H160, H256};
use sp_runtime::transaction_validity::{
Expand Down Expand Up @@ -99,19 +105,17 @@ impl PoolTransactionValidation for Getter {
pub enum PublicGetter {
some_value = 0,
total_issuance = 1,
guess_the_number_last_lucky_number = 50,
guess_the_number_last_winning_distance = 51,
guess_the_number_info = 52,
parentchains_info = 10,
guess_the_number(GuessTheNumberPublicGetter) = 50,
}

#[derive(Encode, Decode, Clone, Debug, PartialEq, Eq)]
#[allow(non_camel_case_types)]
#[repr(u8)]
#[allow(clippy::unnecessary_cast)]
pub enum TrustedGetter {
free_balance(AccountId) = 0,
reserved_balance(AccountId) = 1,
nonce(AccountId) = 2,
account_info(AccountId) = 0,
guess_the_number(GuessTheNumberTrustedGetter) = 50,
#[cfg(feature = "evm")]
evm_nonce(AccountId) = 90,
#[cfg(feature = "evm")]
Expand All @@ -123,9 +127,8 @@ pub enum TrustedGetter {
impl TrustedGetter {
pub fn sender_account(&self) -> &AccountId {
match self {
TrustedGetter::free_balance(sender_account) => sender_account,
TrustedGetter::reserved_balance(sender_account) => sender_account,
TrustedGetter::nonce(sender_account) => sender_account,
TrustedGetter::account_info(sender_account) => sender_account,
TrustedGetter::guess_the_number(getter) => getter.sender_account(),
#[cfg(feature = "evm")]
TrustedGetter::evm_nonce(sender_account) => sender_account,
#[cfg(feature = "evm")]
Expand Down Expand Up @@ -184,26 +187,14 @@ impl ExecuteGetter for Getter {
impl ExecuteGetter for TrustedGetterSigned {
fn execute(self) -> Option<Vec<u8>> {
match self.getter {
TrustedGetter::free_balance(who) => {
let info = System::account(&who);
debug!("TrustedGetter free_balance");
debug!("AccountInfo for {} is {:?}", account_id_to_string(&who), info);
std::println!("⣿STF⣿ 🔍 TrustedGetter query: free balance for ⣿⣿⣿ is ⣿⣿⣿",);
Some(info.data.free.encode())
},
TrustedGetter::reserved_balance(who) => {
TrustedGetter::account_info(who) => {
let info = System::account(&who);
debug!("TrustedGetter reserved_balance");
debug!("TrustedGetter account_data");
debug!("AccountInfo for {} is {:?}", account_id_to_string(&who), info);
debug!("Account reserved balance is {}", info.data.reserved);
Some(info.data.reserved.encode())
},
TrustedGetter::nonce(who) => {
let nonce = System::account_nonce(&who);
debug!("TrustedGetter nonce");
debug!("Account nonce is {}", nonce);
Some(nonce.encode())
std::println!("⣿STF⣿ 🔍 TrustedGetter query: account info for ⣿⣿⣿ is ⣿⣿⣿",);
Some(info.encode())
},
TrustedGetter::guess_the_number(getter) => getter.execute(),
#[cfg(feature = "evm")]
TrustedGetter::evm_nonce(who) => {
let evm_account = get_evm_account(&who);
Expand Down Expand Up @@ -237,7 +228,13 @@ impl ExecuteGetter for TrustedGetterSigned {
}

fn get_storage_hashes_to_update(self) -> Vec<Vec<u8>> {
Vec::new()
let mut key_hashes = Vec::new();
match self.getter {
TrustedGetter::guess_the_number(getter) =>
key_hashes.append(&mut getter.get_storage_hashes_to_update()),
_ => debug!("No storage updates needed..."),
};
key_hashes
}
}

Expand All @@ -246,56 +243,85 @@ impl ExecuteGetter for PublicGetter {
match self {
PublicGetter::some_value => Some(42u32.encode()),
PublicGetter::total_issuance => Some(Balances::total_issuance().encode()),
PublicGetter::guess_the_number_last_lucky_number => {
// todo! return suiting value, not this one
GuessTheNumber::lucky_number().map(|guess| guess.encode())
},
PublicGetter::guess_the_number_last_winning_distance => {
// todo! return suiting value, not this one
GuessTheNumber::lucky_number().map(|guess| guess.encode())
},
PublicGetter::guess_the_number_info => {
let account = GuessTheNumber::get_pot_account();
let winnings = GuessTheNumber::winnings();
let next_round_timestamp = GuessTheNumber::next_round_timestamp();
let maybe_last_winning_distance = GuessTheNumber::last_winning_distance();
let last_winners = GuessTheNumber::last_winners();
let maybe_last_lucky_number = GuessTheNumber::last_lucky_number();
let info = System::account(&account);
debug!("TrustedGetter GuessTheNumber Pot Info");
debug!("AccountInfo for pot {} is {:?}", account_id_to_string(&account), info);
std::println!("⣿STF⣿ 🔍 TrustedGetter query: guess-the-number pot info");
Some(
GuessTheNumberInfo {
account,
balance: info.data.free,
winnings,
next_round_timestamp,
last_winners,
maybe_last_lucky_number,
maybe_last_winning_distance,
}
.encode(),
)
PublicGetter::parentchains_info => {
let integritee = ParentchainInfo {
id: ParentchainId::Integritee,
genesis_hash: ParentchainIntegritee::parentchain_genesis_hash(),
block_number: ParentchainIntegritee::block_number(),
now: ParentchainIntegritee::now(),
creation_block_number: ParentchainIntegritee::creation_block_number(),
creation_timestamp: ParentchainIntegritee::creation_timestamp(),
};
let target_a = ParentchainInfo {
id: ParentchainId::TargetA,
genesis_hash: ParentchainTargetA::parentchain_genesis_hash(),
block_number: ParentchainTargetA::block_number(),
now: ParentchainTargetA::now(),
creation_block_number: ParentchainTargetA::creation_block_number(),
creation_timestamp: ParentchainTargetA::creation_timestamp(),
};
let target_b = ParentchainInfo {
id: ParentchainId::TargetB,
genesis_hash: ParentchainTargetB::parentchain_genesis_hash(),
block_number: ParentchainTargetB::block_number(),
now: ParentchainTargetB::now(),
creation_block_number: ParentchainTargetB::creation_block_number(),
creation_timestamp: ParentchainTargetB::creation_timestamp(),
};
let parentchains_info = ParentchainsInfo {
integritee,
target_a,
target_b,
shielding_target: shard_vault()
.map(|v| v.1)
.unwrap_or(ParentchainId::Integritee),
};
Some(parentchains_info.encode())
},
PublicGetter::guess_the_number(getter) => getter.execute(),
}
}

fn get_storage_hashes_to_update(self) -> Vec<Vec<u8>> {
Vec::new()
let mut key_hashes = Vec::new();
match self {
Self::guess_the_number(getter) =>
key_hashes.append(&mut getter.get_storage_hashes_to_update()),
_ => debug!("No storage updates needed..."),
};
key_hashes
}
}

/// General public information about the sync status of all parentchains
#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq)]
pub struct GuessTheNumberInfo {
pub account: AccountId,
pub balance: Balance,
pub winnings: Balance,
pub next_round_timestamp: Moment,
pub last_winners: Vec<AccountId>,
pub maybe_last_lucky_number: Option<GuessType>,
pub maybe_last_winning_distance: Option<GuessType>,
pub struct ParentchainsInfo {
/// info for the integritee network parentchain
pub integritee: ParentchainInfo,
/// info for the target A parentchain
pub target_a: ParentchainInfo,
/// info for the target B parentchain
pub target_b: ParentchainInfo,
/// which of the parentchains is used as a shielding target?
pub shielding_target: ParentchainId,
}

/// General public information about the sync status of a parentchain
#[derive(Encode, Decode, Debug, Clone, PartialEq, Eq)]
pub struct ParentchainInfo {
/// the parentchain id for internal use
id: ParentchainId,
/// the genesis hash of the parentchain
genesis_hash: Option<Hash>,
/// the last imported parentchain block number
block_number: Option<BlockNumber>,
/// the timestamp of the last imported parentchain block
now: Option<Moment>,
/// the parentchain block number which preceded the creation of this shard
creation_block_number: Option<BlockNumber>,
/// the timestamp of creation for this shard
creation_timestamp: Option<Moment>,
}
mod tests {
use super::*;

Expand Down
Loading

0 comments on commit 2dbbfb4

Please sign in to comment.