Skip to content

Commit

Permalink
Merge branch 'master' into ETCM-8896-migration-guide-updates
Browse files Browse the repository at this point in the history
  • Loading branch information
ladamesny authored Dec 18, 2024
2 parents 9c9d156 + faa8d42 commit 4fc338f
Show file tree
Hide file tree
Showing 20 changed files with 434 additions and 292 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This changelog is based on [Keep A Changelog](https://keepachangelog.com/en/1.1.

## Fixed

* Cache returning invalid results when native token MainChainScripts has changed.
* Crash of parnter-chain-node smart-contracts command. Logging is now set independently.
* Renamed of argument 'ogmios-host' to 'ogmios-url' in smart-contracts subcommands.

Expand Down
1 change: 0 additions & 1 deletion node/node/src/command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ pub fn run() -> sc_cli::Result<()> {
match &cli.subcommand {
Some(Subcommand::Key(cmd)) => cmd.run(&cli),
Some(Subcommand::PartnerChains(cmd)) => {
partner_chains_node_commands::setup_log4rs()?;
let make_dependencies = |config| {
let components = service::new_partial(&config)?;
Ok((
Expand Down
5 changes: 4 additions & 1 deletion toolkit/cli/node-commands/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,10 @@ where
})
},
PartnerChainsSubcommand::RegistrationSignatures(cmd) => Ok(println!("{}", cmd.execute())),
PartnerChainsSubcommand::SmartContracts(cmd) => Ok(cmd.execute_blocking()?),
PartnerChainsSubcommand::SmartContracts(cmd) => {
crate::setup_log4rs()?;
Ok(cmd.execute_blocking()?)
},
}
}

Expand Down
12 changes: 6 additions & 6 deletions toolkit/cli/smart-contracts-commands/src/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ pub struct RegisterCmd {
alias = "sidechain-public-keys",
value_parser = parse_partnerchain_public_keys
)]
partnerchain_public_keys: PermissionedCandidateData,
partner_chain_public_keys: PermissionedCandidateData,
#[arg(long, alias = "sidechain-signature")]
partnerchain_signature: SidechainSignature,
partner_chain_signature: SidechainSignature,
#[arg(long)]
spo_public_key: MainchainPublicKey,
#[arg(long)]
Expand All @@ -44,12 +44,12 @@ impl RegisterCmd {
pub_key: self.spo_public_key,
signature: self.spo_signature,
},
partnerchain_pub_key: self.partnerchain_public_keys.sidechain_public_key,
partnerchain_signature: self.partnerchain_signature,
partner_chain_pub_key: self.partner_chain_public_keys.sidechain_public_key,
partner_chain_signature: self.partner_chain_signature,
own_pkh: payment_key.to_pub_key_hash(),
registration_utxo: self.registration_utxo,
aura_pub_key: self.partnerchain_public_keys.aura_public_key,
grandpa_pub_key: self.partnerchain_public_keys.grandpa_public_key,
aura_pub_key: self.partner_chain_public_keys.aura_public_key,
grandpa_pub_key: self.partner_chain_public_keys.grandpa_public_key,
};

run_register(
Expand Down
59 changes: 30 additions & 29 deletions toolkit/mainchain-follower/db-sync-follower/src/native_token/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{DataSourceError, Result};
use derive_new::new;
use itertools::Itertools;
use sidechain_domain::*;
use sp_native_token_management::NativeTokenManagementDataSource;
use sp_native_token_management::{MainChainScripts, NativeTokenManagementDataSource};
use sqlx::PgPool;
use std::sync::{Arc, Mutex};

Expand All @@ -30,14 +30,12 @@ impl NativeTokenManagementDataSource for NativeTokenManagementDataSourceImpl {
&self,
after_block: Option<McBlockHash>,
to_block: McBlockHash,
policy_id: PolicyId,
asset_name: AssetName,
address: MainchainAddress,
scripts: MainChainScripts,
) -> std::result::Result<NativeTokenAmount, Box<dyn std::error::Error + Send + Sync>> {
if let Some(after_block) = after_block {
if after_block == to_block {
Ok(NativeTokenAmount(0))
} else if let Some(amount) = self.get_from_cache(&after_block, &to_block) {
} else if let Some(amount) = self.get_from_cache(&after_block, &to_block, &scripts) {
log::debug!(
"Illiquid supply transfers sum from cache after block '{:?}' to block '{:?}' is {}",
after_block, to_block, amount.0
Expand All @@ -46,7 +44,7 @@ impl NativeTokenManagementDataSource for NativeTokenManagementDataSourceImpl {
} else {
log::debug!("Illiquid supply transfers after block '{:?}' to block '{:?}' not found in cache.", after_block, to_block);
let block_to_amount = self
.get_data_to_cache(&after_block, &to_block, &policy_id, &asset_name, &address)
.get_data_to_cache(&after_block, &to_block, &scripts)
.await?;
log::debug!("Caching illiquid supply transfers from {} blocks", block_to_amount.len());

Expand All @@ -59,13 +57,13 @@ impl NativeTokenManagementDataSource for NativeTokenManagementDataSourceImpl {
log::debug!("Amount of illiquid supply transfers is {}", amount);

if let Ok(mut cache) = self.cache.lock() {
cache.update(block_to_amount)
cache.update(block_to_amount, scripts)
}
Ok(NativeTokenAmount(amount))
}
} else {
let amount = self
.query_transfers_from_genesis(&to_block, &policy_id, &asset_name, &address)
.query_transfers_from_genesis(&to_block, &scripts)
.await?;
log::debug!("Amount of illiquid supply transfers from genesis to {} is {}", to_block, amount.0);
Ok(amount)
Expand Down Expand Up @@ -95,9 +93,14 @@ impl NativeTokenManagementDataSourceImpl {
&self,
after_block: &McBlockHash,
to_block: &McBlockHash,
scripts: &MainChainScripts,
) -> Option<NativeTokenAmount> {
let cache = self.cache.lock().ok()?;
cache.get_sum_in_range(after_block, to_block).map(NativeTokenAmount)
if cache.scripts.as_ref() == Some(scripts) {
cache.get_sum_in_range(after_block, to_block).map(NativeTokenAmount)
} else {
None
}
}

// invariant: to_block is always a stable block
Expand All @@ -106,9 +109,7 @@ impl NativeTokenManagementDataSourceImpl {
&self,
from_block: &McBlockHash,
to_block: &McBlockHash,
policy_id: &PolicyId,
asset_name: &AssetName,
address: &MainchainAddress,
scripts: &MainChainScripts,
) -> Result<Vec<(McBlockHash, u128)>> {
let (from_block_no, to_block_no, latest_block) = futures::try_join!(
get_from_block_no(from_block, &self.pool),
Expand All @@ -124,22 +125,18 @@ impl NativeTokenManagementDataSourceImpl {
std::cmp::max(to_block_no.0, from_block_no.0.saturating_add(self.cache_size.into())),
));
// transfers starts with block having hash equal to after_block or genesis
let transfers = self
.query_db(from_block_no, cache_to_block_no, policy_id, asset_name, address)
.await?;
let transfers = self.query_db(from_block_no, cache_to_block_no, scripts).await?;
Ok(transfers.iter().map(|t| (McBlockHash(t.block_hash), t.amount.0)).collect())
}

async fn query_db(
&self,
from_block: BlockNumber,
to_block: BlockNumber,
native_token_policy_id: &PolicyId,
native_token_asset_name: &AssetName,
illiquid_supply_address: &MainchainAddress,
scripts: &MainChainScripts,
) -> Result<Vec<crate::db_model::BlockTokenAmount>> {
let address = illiquid_supply_address.clone().into();
let asset = to_db_asset(native_token_policy_id, native_token_asset_name);
let address = scripts.illiquid_supply_validator_address.clone().into();
let asset = to_db_asset(scripts);
Ok(crate::db_model::get_native_token_transfers(
&self.pool, from_block, to_block, asset, address,
)
Expand All @@ -149,26 +146,24 @@ impl NativeTokenManagementDataSourceImpl {
async fn query_transfers_from_genesis(
&self,
to_block: &McBlockHash,
policy_id: &PolicyId,
asset_name: &AssetName,
address: &MainchainAddress,
scripts: &MainChainScripts,
) -> Result<NativeTokenAmount> {
let to_block = get_to_block_no(to_block, &self.pool).await?;
Ok(crate::db_model::get_total_native_tokens_transfered(
&self.pool,
to_block,
to_db_asset(policy_id, asset_name),
address.clone().into(),
to_db_asset(scripts),
scripts.illiquid_supply_validator_address.clone().into(),
)
.await?
.into())
}
}

fn to_db_asset(policy_id: &PolicyId, asset_name: &AssetName) -> crate::db_model::Asset {
fn to_db_asset(scripts: &MainChainScripts) -> crate::db_model::Asset {
crate::db_model::Asset {
policy_id: policy_id.clone().into(),
asset_name: asset_name.clone().into(),
policy_id: scripts.native_token_policy_id.clone().into(),
asset_name: scripts.native_token_asset_name.clone().into(),
}
}

Expand Down Expand Up @@ -202,6 +197,7 @@ async fn get_latest_block(pool: &PgPool) -> Result<Block> {
pub(crate) struct Cache {
/// Continous blocks with their respective total native token transfer amount
block_hash_to_amount: Vec<(McBlockHash, u128)>,
pub(crate) scripts: Option<MainChainScripts>,
}

impl Cache {
Expand All @@ -215,7 +211,12 @@ impl Cache {
Some(after_to.iter().map(|(_, amount)| amount).sum())
}

pub fn update(&mut self, block_hash_to_amount: Vec<(McBlockHash, u128)>) {
pub fn update(
&mut self,
block_hash_to_amount: Vec<(McBlockHash, u128)>,
scripts: MainChainScripts,
) {
self.block_hash_to_amount = block_hash_to_amount;
self.scripts = Some(scripts);
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,38 @@
use super::NativeTokenManagementDataSourceImpl;
use sidechain_domain::{AssetName, MainchainAddress, McBlockHash, PolicyId};
use sp_native_token_management::NativeTokenManagementDataSource;
use sp_native_token_management::{MainChainScripts, NativeTokenManagementDataSource};
use sqlx::PgPool;
use std::str::FromStr;

fn native_token_policy_id() -> PolicyId {
PolicyId::from_hex_unsafe("6c969320597b755454ff3653ad09725d590c570827a129aeb4385526")
}
// Transfers in the migrations file are:
// block 1: 11 for scripts 1
// block 3: 12 for scripts 1
// block 5: 13 and 14 for scripts 1, 37 for scripts 2

fn native_token_asset_name() -> AssetName {
AssetName::from_hex_unsafe("546573744275647a507265766965775f3335")
fn scripts() -> MainChainScripts {
MainChainScripts {
native_token_policy_id: PolicyId::from_hex_unsafe(
"6c969320597b755454ff3653ad09725d590c570827a129aeb4385526",
),
native_token_asset_name: AssetName::from_hex_unsafe("546573744275647a507265766965775f3335"),
illiquid_supply_validator_address: MainchainAddress::from_str(
"addr_test1wrhvtvx3f0g9wv9rx8kfqc60jva3e07nqujk2cspekv4mqs9rjdvz",
)
.unwrap(),
}
}

fn illiquid_supply_address() -> MainchainAddress {
MainchainAddress::from_str("addr_test1wrhvtvx3f0g9wv9rx8kfqc60jva3e07nqujk2cspekv4mqs9rjdvz")
.unwrap()
fn scripts2() -> MainChainScripts {
MainChainScripts {
native_token_policy_id: PolicyId::from_hex_unsafe(
"aaaabbaa597b755454ff3653ad09725d590c570827a129aeb438ffff",
),
native_token_asset_name: AssetName::from_hex_unsafe("656565"),
illiquid_supply_validator_address: MainchainAddress::from_str(
"addr_test1aaaabbaaf0g9wv9rx8kfqc60jva3e07nqujk2cspekv4mqs9ffff",
)
.unwrap(),
}
}

fn block_hash(i: u32) -> McBlockHash {
Expand Down Expand Up @@ -69,15 +87,27 @@ async fn query_for_each_blocks_pair(pool: PgPool) {
assert_eq!(vec![r1, r2, r3, r4, r5], vec![11, 0, 12, 0, 13 + 14])
}

#[sqlx::test(migrations = "./testdata/native-token/migrations")]
async fn change_of_scripts_invalidates_cache(pool: PgPool) {
let source = make_source(pool.clone());
let scripts1_transfers = run_for_scripts(&source, Some(1), 3, scripts()).await;
assert_eq!(scripts1_transfers, 12);
let scripts2_transfers = run_for_scripts(&source, Some(4), 6, scripts2()).await;
assert_eq!(scripts2_transfers, 37);
}

async fn run(source: &NativeTokenManagementDataSourceImpl, from: Option<u32>, to: u32) -> u128 {
run_for_scripts(source, from, to, scripts()).await
}

async fn run_for_scripts(
source: &NativeTokenManagementDataSourceImpl,
from: Option<u32>,
to: u32,
scripts: MainChainScripts,
) -> u128 {
source
.get_total_native_token_transfer(
from.map(block_hash),
block_hash(to),
native_token_policy_id(),
native_token_asset_name(),
illiquid_supply_address(),
)
.get_total_native_token_transfer(from.map(block_hash), block_hash(to), scripts)
.await
.unwrap()
.0
Expand Down
Loading

0 comments on commit 4fc338f

Please sign in to comment.