Skip to content

Commit 14b4708

Browse files
committed
refactor: encapsulate oracle config access via getter methods
- Add getter methods for oracle_address, oracle_mnemonic, change_address, and network_prefix - Prefer reading oracle_mnemonic from env vars or oracle-config file - Replace direct field access with getter methods throughout the codebase - Remove unnecessary address cloning and unwrapping - Improve encapsulation and enable more secure config handling
1 parent f6e7c59 commit 14b4708

File tree

14 files changed

+178
-86
lines changed

14 files changed

+178
-86
lines changed

core/src/actions.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fn execute_refresh_action(
6464
node_api: &NodeApi,
6565
) -> Result<(), ActionExecError> {
6666
let tx_id = node_api.sign_and_submit_transaction(action.transaction_context)?;
67-
let network_prefix = &ORACLE_CONFIG.oracle_address.network();
67+
let network_prefix = &ORACLE_CONFIG.get_network_prefix();
6868
log::info!(
6969
"Refresh tx published. Check status: {}",
7070
ergo_explorer_transaction_link(tx_id, *network_prefix)
@@ -77,7 +77,7 @@ fn execute_publish_datapoint_action(
7777
node_api: &NodeApi,
7878
) -> Result<(), ActionExecError> {
7979
let tx_id = node_api.sign_and_submit_transaction(action.transaction_context)?;
80-
let network_prefix = &ORACLE_CONFIG.oracle_address.network();
80+
let network_prefix = &ORACLE_CONFIG.get_network_prefix();
8181
log::info!(
8282
"Datapoint tx published. Check status: {}",
8383
ergo_explorer_transaction_link(tx_id, *network_prefix)

core/src/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ async fn root() -> &'static str {
3737
async fn oracle_info() -> impl IntoResponse {
3838
let conf = &ORACLE_CONFIG;
3939
Json(json! ( {
40-
"oracle_address": conf.oracle_address.to_base58(),
40+
"oracle_address": conf.get_oracle_address().to_base58(),
4141
"base_fee": conf.base_fee,
4242
} ))
4343
}
@@ -79,7 +79,7 @@ fn oracle_status_sync(oracle_pool: Arc<OraclePool>) -> Result<Json<serde_json::V
7979
// Basic information about the oracle pool
8080
async fn pool_info() -> impl IntoResponse {
8181
let conf = &POOL_CONFIG;
82-
let network = &ORACLE_CONFIG.oracle_address.network();
82+
let network = &ORACLE_CONFIG.get_network_prefix();
8383
let address_encoder = AddressEncoder::new(*network);
8484
let pool_box_address = Address::P2S(
8585
conf.pool_box_wrapper_inputs
@@ -243,7 +243,7 @@ fn pool_health_sync(oracle_pool: Arc<OraclePool>) -> Result<PoolHealth, ApiError
243243
let current_height = (node_api.node.current_block_height()? as u32).into();
244244
let pool_box = &oracle_pool.get_pool_box_source().get_pool_box()?;
245245
let pool_box_height = pool_box.get_box().creation_height.into();
246-
let network_prefix = ORACLE_CONFIG.change_address.clone().unwrap().network();
246+
let network_prefix = ORACLE_CONFIG.get_network_prefix();
247247
let pool_health = check_pool_health(
248248
current_height,
249249
pool_box_height,

core/src/cli_commands/bootstrap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ pub fn bootstrap(config_file_name: String, node_api: &NodeApi) -> Result<(), any
6464
let oracle_config = &ORACLE_CONFIG;
6565
let s = std::fs::read_to_string(config_file_name)?;
6666
let config: BootstrapConfig = serde_yaml::from_str(&s)?;
67-
let change_address = ORACLE_CONFIG.change_address.clone().unwrap();
67+
let change_address = ORACLE_CONFIG.get_change_address().clone();
6868
debug!("Change address: {:?}", change_address);
6969
let erg_value_per_box = config.oracle_contract_parameters.min_storage_rent;
7070
let input = BootstrapInput {
71-
oracle_address: oracle_config.oracle_address.clone(),
71+
oracle_address: oracle_config.get_oracle_address().clone(),
7272
config,
7373
node_api,
7474
tx_fee: *BASE_FEE,

core/src/cli_commands/extract_reward_tokens.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,15 +74,15 @@ pub fn extract_reward_tokens(
7474
let rewards_destination =
7575
AddressEncoder::unchecked_parse_network_address_from_str(&rewards_destination_str)?;
7676
let network_prefix = rewards_destination.network();
77-
let oracle_address = ORACLE_CONFIG.oracle_address.clone();
78-
let change_address = ORACLE_CONFIG.change_address.clone();
77+
let oracle_address = ORACLE_CONFIG.get_oracle_address().clone();
78+
let change_address = ORACLE_CONFIG.get_change_address().clone();
7979
let (context, num_reward_tokens) = build_extract_reward_tokens_tx(
8080
local_datapoint_box_source,
8181
node_api,
8282
rewards_destination.address(),
8383
height,
8484
oracle_address,
85-
change_address.unwrap().address(),
85+
change_address.address(),
8686
)?;
8787

8888
println!(

core/src/cli_commands/prepare_update.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ pub fn prepare_update(
9191
let s = std::fs::read_to_string(config_file_name)?;
9292
let config_serde: UpdateBootstrapConfigSerde = serde_yaml::from_str(&s)?;
9393

94-
let change_address = ORACLE_CONFIG.change_address.clone().unwrap().address();
94+
let change_address = ORACLE_CONFIG.get_change_address().clone().address();
9595
let config = UpdateBootstrapConfig::try_from(config_serde)?;
9696
let update_bootstrap_input = PrepareUpdateInput {
9797
node_api,
@@ -199,7 +199,7 @@ impl<'a> PrepareUpdate<'a> {
199199
pool_config: &'a PoolConfig,
200200
oracle_config: &'a OracleConfig,
201201
) -> Result<Self, PrepareUpdateError> {
202-
let wallet_pk_ergo_tree = oracle_config.oracle_address.address().script()?;
202+
let wallet_pk_ergo_tree = oracle_config.get_oracle_address().address().script()?;
203203
Ok(Self {
204204
input,
205205
wallet_pk_ergo_tree,
@@ -341,7 +341,7 @@ impl<'a> PrepareUpdate<'a> {
341341
let target_balance = self.calc_target_balance(self.num_transactions_left)?;
342342
debug!("target_balance: {:?}", target_balance);
343343
let unspent_boxes = self.input.node_api.get_unspent_boxes_by_address(
344-
&self.oracle_config.oracle_address.to_base58(),
344+
&self.oracle_config.get_oracle_address().to_base58(),
345345
target_balance,
346346
[].into(),
347347
)?;
@@ -617,16 +617,16 @@ token_ids:
617617
rescan_height: 141887
618618
"#).unwrap();
619619

620-
let old_oracle_config: OracleConfig = serde_yaml::from_str(
620+
let mut old_oracle_config: OracleConfig = serde_yaml::from_str(
621621
r#"
622622
node_url: http://10.94.77.47:9052
623623
base_fee: 1100000
624624
scan_start_height: 0
625625
log_level: ~
626626
core_api_port: 9010
627-
oracle_address: 3Wy3BaCjGDWE3bjjZkNo3aWaMz3cYrePMFhchcKovY9uG9vhpAuW
628627
data_point_source: NanoErgXau
629628
data_point_source_custom_script: ~
629+
oracle_network: testnet
630630
"#,
631631
)
632632
.unwrap();
@@ -638,10 +638,7 @@ data_point_source_custom_script: ~
638638
NetworkPrefix::Testnet,
639639
&Address::P2Pk(secret.public_image()),
640640
);
641-
let old_oracle_config = OracleConfig {
642-
oracle_address: network_address.clone(),
643-
..old_oracle_config
644-
};
641+
old_oracle_config.set_oracle_address(Some(network_address.clone()));
645642
let ergo_tree = network_address.address().script().unwrap();
646643

647644
let value = BASE_FEE.checked_mul_u32(10000).unwrap();
@@ -710,6 +707,6 @@ data_point_source_custom_script: ~
710707
let prepare =
711708
PrepareUpdate::new(prepare_update_input, &old_pool_config, &old_oracle_config).unwrap();
712709
let (new_pool_config, _) = prepare.execute(state).unwrap();
713-
assert!(new_pool_config.token_ids != old_pool_config.token_ids);
710+
assert_ne!(new_pool_config.token_ids, old_pool_config.token_ids);
714711
}
715712
}

core/src/cli_commands/transfer_oracle_token.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,9 @@ pub fn transfer_oracle_token(
7272
) -> Result<(), anyhow::Error> {
7373
let rewards_destination =
7474
AddressEncoder::unchecked_parse_network_address_from_str(&rewards_destination_str)?;
75-
let oracle_address = ORACLE_CONFIG.oracle_address.clone();
76-
let (change_address, network_prefix) = {
77-
let net_address = ORACLE_CONFIG.change_address.clone().unwrap();
78-
(net_address.address(), net_address.network())
79-
};
75+
let oracle_address = ORACLE_CONFIG.get_oracle_address().clone();
76+
let change_address = ORACLE_CONFIG.get_change_address().clone().address();
77+
let network_prefix = ORACLE_CONFIG.get_network_prefix();
8078
let context = build_transfer_oracle_token_tx(
8179
local_datapoint_box_source,
8280
node_api,

core/src/cli_commands/update_pool.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,9 @@ pub fn update_pool(
8585
"Reward token id in pool_config_updated.yaml does not match the one from the command line"
8686
);
8787
}
88-
let oracle_address = ORACLE_CONFIG.oracle_address.clone();
89-
let (change_address, network_prefix) = {
90-
let net_addr = ORACLE_CONFIG.change_address.clone().unwrap();
91-
(net_addr.address(), net_addr.network())
92-
};
88+
let oracle_address = ORACLE_CONFIG.get_oracle_address().clone();
89+
let change_address = ORACLE_CONFIG.get_change_address().clone().address();
90+
let network_prefix = ORACLE_CONFIG.get_network_prefix();
9391

9492
let new_pool_contract =
9593
PoolContract::checked_load(&new_pool_config.pool_box_wrapper_inputs.contract_inputs)?;

core/src/cli_commands/vote_update_pool.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ pub fn vote_update_pool(
6868
height: BlockHeight,
6969
ballot_contract: &BallotContract,
7070
) -> Result<(), anyhow::Error> {
71-
let oracle_address = ORACLE_CONFIG.oracle_address.clone();
72-
let change_network_address = ORACLE_CONFIG.change_address.clone().unwrap();
71+
let oracle_address = ORACLE_CONFIG.get_oracle_address().clone();
72+
let change_network_address = ORACLE_CONFIG.get_change_address().clone();
7373
let network_prefix = change_network_address.network();
7474
let new_pool_box_address_hash = Digest32::try_from(new_pool_box_address_hash_str)?;
7575
let ballot_token_owner =
76-
if let Address::P2Pk(ballot_token_owner) = ORACLE_CONFIG.oracle_address.address() {
76+
if let Address::P2Pk(ballot_token_owner) = oracle_address.address() {
7777
ballot_token_owner.h
7878
} else {
7979
return Err(VoteUpdatePoolError::IncorrectBallotTokenOwnerAddress.into());

core/src/explorer_api.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ pub fn wait_for_tx_confirmation(tx_id: TxId) {
8484
}
8585

8686
pub fn wait_for_txs_confirmation(tx_ids: Vec<TxId>) {
87-
let network = ORACLE_CONFIG.oracle_address.network();
87+
let network = ORACLE_CONFIG.get_network_prefix();
8888
let timeout = Duration::from_secs(1200);
8989
let explorer_url = ORACLE_CONFIG
9090
.explorer_url

core/src/main.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -274,14 +274,15 @@ fn main() {
274274
log_on_launch();
275275
let node_api = NodeApi::new(&ORACLE_CONFIG.node_url);
276276

277+
let _ = node_api.get_wallet();
277278
if !node_api.node.indexer_status().unwrap().is_active {
278279
error!("Blockchain indexer is not active on the node");
279280
std::process::exit(exitcode::SOFTWARE);
280281
}
281282
let _ = node_api.wait_for_indexer_sync();
282283

283-
let change_address = ORACLE_CONFIG.change_address.clone().unwrap();
284-
let network_prefix = change_address.network();
284+
let change_address = ORACLE_CONFIG.get_change_address().clone();
285+
let network_prefix = ORACLE_CONFIG.get_network_prefix();
285286

286287
#[allow(clippy::wildcard_enum_match_arm)]
287288
match args.command {
@@ -574,11 +575,6 @@ fn log_and_continue_if_non_fatal(
574575

575576
fn log_on_launch() {
576577
log::info!("{}", APP_VERSION);
577-
let oracle_address_opt = ORACLE_CONFIG_OPT.as_ref().map(|c| c.oracle_address.clone());
578-
if let Ok(oracle_address) = oracle_address_opt {
579-
// log::info!("Token ids: {:?}", config.token_ids);
580-
log::info!("Oracle address: {}", oracle_address.to_base58());
581-
}
582578
}
583579

584580
fn check_reward_token_opt(

0 commit comments

Comments
 (0)