From 529ce4421900ecab96148e91393f377ba74a1b2b Mon Sep 17 00:00:00 2001 From: thaddeus <79182310+flomang@users.noreply.github.com> Date: Fri, 7 Oct 2022 03:37:32 -0600 Subject: [PATCH] Override global one times. (#3737) * Minor update for foreign api docs. Peers seems to be no longer needed as an arg in the grin_api::Foreign::new. * This changes foreign_rpc and owner_rpc modules to public so the helper modules for the rpc clients can be accessible in external projects. * Updated test seeds. * Added functions that override the one time globals. This is needed for the grin-gui so it can change the global contexts when it needs to switch from testnet to mainnet. * Refactor * Fix panick at 'attempt to subtract with overflow' when grin-wallet sends in a start_index of 0 for new wallets. * Fix overflow panic using saturating_sub(1). --- core/src/core/pmmr/readonly_pmmr.rs | 3 ++- core/src/global.rs | 20 ++++++++++++++++++++ servers/src/grin/seed.rs | 5 +++-- util/src/lib.rs | 9 ++++++++- 4 files changed, 33 insertions(+), 4 deletions(-) diff --git a/core/src/core/pmmr/readonly_pmmr.rs b/core/src/core/pmmr/readonly_pmmr.rs index 9ecc92186..4c693f3c6 100644 --- a/core/src/core/pmmr/readonly_pmmr.rs +++ b/core/src/core/pmmr/readonly_pmmr.rs @@ -73,7 +73,8 @@ where Some(p) => p, None => self.size, }; - let mut pmmr_index = pmmr_index1 - 1; + let mut pmmr_index = pmmr_index1.saturating_sub(1); + while return_vec.len() < max_count as usize && pmmr_index < size { if let Some(t) = self.get_data(pmmr_index) { return_vec.push(t); diff --git a/core/src/global.rs b/core/src/global.rs index 00cc3ef61..fb335dc0b 100644 --- a/core/src/global.rs +++ b/core/src/global.rs @@ -181,6 +181,11 @@ pub fn init_global_chain_type(new_type: ChainTypes) { GLOBAL_CHAIN_TYPE.init(new_type) } +/// Set the global chain_type using an override +pub fn set_global_chain_type(new_type: ChainTypes) { + GLOBAL_CHAIN_TYPE.set(new_type, true); +} + /// Set the chain type on a per-thread basis via thread_local storage. pub fn set_local_chain_type(new_type: ChainTypes) { CHAIN_TYPE.with(|chain_type| chain_type.set(Some(new_type))) @@ -207,12 +212,22 @@ pub fn init_global_future_time_limit(new_ftl: u64) { GLOBAL_FUTURE_TIME_LIMIT.init(new_ftl) } +/// The global future time limit may be reset again using the override +pub fn set_global_future_time_limit(new_ftl: u64) { + GLOBAL_FUTURE_TIME_LIMIT.set(new_ftl, true) +} + /// One time initialization of the global accept fee base /// Will panic if we attempt to re-initialize this (via OneTime). pub fn init_global_accept_fee_base(new_base: u64) { GLOBAL_ACCEPT_FEE_BASE.init(new_base) } +/// The global accept fee base may be reset using override. +pub fn set_global_accept_fee_base(new_base: u64) { + GLOBAL_ACCEPT_FEE_BASE.set(new_base, true) +} + /// Set the accept fee base on a per-thread basis via thread_local storage. pub fn set_local_accept_fee_base(new_base: u64) { ACCEPT_FEE_BASE.with(|base| base.set(Some(new_base))) @@ -265,6 +280,11 @@ pub fn init_global_nrd_enabled(enabled: bool) { GLOBAL_NRD_FEATURE_ENABLED.init(enabled) } +/// Set the global NRD feature flag using override. +pub fn set_global_nrd_enabled(enabled: bool) { + GLOBAL_NRD_FEATURE_ENABLED.set(enabled, true) +} + /// Explicitly enable the local NRD feature flag. pub fn set_local_nrd_enabled(enabled: bool) { NRD_FEATURE_ENABLED.with(|flag| flag.set(Some(enabled))) diff --git a/servers/src/grin/seed.rs b/servers/src/grin/seed.rs index 959653fad..b2034cf90 100644 --- a/servers/src/grin/seed.rs +++ b/servers/src/grin/seed.rs @@ -44,10 +44,11 @@ pub const MAINNET_DNS_SEEDS: &[&str] = &[ ]; /// DNS Seeds with contact email associated - Testnet pub const TESTNET_DNS_SEEDS: &[&str] = &[ - "floonet.seed.grin.icu", // gary.peverell@protonmail.com - "floonet.seed.713.mw", // jasper@713.mw "floonet.seed.grin.lesceller.com", // q.lesceller@gmail.com "floonet.seed.grin.prokapi.com", // hendi@prokapi.com + "grintestseed.revcore.net", // yeastplume@protonmail.com + "testnet.grin.punksec.de", // grin@punksec.de + "testnet.grinnode.30-r.com", // trinitron@30-r.com ]; pub fn connect_and_monitor( diff --git a/util/src/lib.rs b/util/src/lib.rs index e1237c965..a3a2bf3ab 100644 --- a/util/src/lib.rs +++ b/util/src/lib.rs @@ -86,8 +86,15 @@ where /// Initializes the OneTime, should only be called once after construction. /// Will panic (via assert) if called more than once. pub fn init(&self, value: T) { + self.set(value, false); + } + + /// Allows the one time to be set again with an override. + pub fn set(&self, value: T, is_override: bool) { let mut inner = self.inner.write(); - assert!(inner.is_none()); + if !is_override { + assert!(inner.is_none()); + } *inner = Some(value); }