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).
This commit is contained in:
thaddeus 2022-10-07 03:37:32 -06:00 committed by GitHub
parent 3119899551
commit 529ce44219
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 33 additions and 4 deletions

View file

@ -73,7 +73,8 @@ where
Some(p) => p, Some(p) => p,
None => self.size, 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 { while return_vec.len() < max_count as usize && pmmr_index < size {
if let Some(t) = self.get_data(pmmr_index) { if let Some(t) = self.get_data(pmmr_index) {
return_vec.push(t); return_vec.push(t);

View file

@ -181,6 +181,11 @@ pub fn init_global_chain_type(new_type: ChainTypes) {
GLOBAL_CHAIN_TYPE.init(new_type) 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. /// Set the chain type on a per-thread basis via thread_local storage.
pub fn set_local_chain_type(new_type: ChainTypes) { pub fn set_local_chain_type(new_type: ChainTypes) {
CHAIN_TYPE.with(|chain_type| chain_type.set(Some(new_type))) 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) 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 /// One time initialization of the global accept fee base
/// Will panic if we attempt to re-initialize this (via OneTime). /// Will panic if we attempt to re-initialize this (via OneTime).
pub fn init_global_accept_fee_base(new_base: u64) { pub fn init_global_accept_fee_base(new_base: u64) {
GLOBAL_ACCEPT_FEE_BASE.init(new_base) 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. /// Set the accept fee base on a per-thread basis via thread_local storage.
pub fn set_local_accept_fee_base(new_base: u64) { pub fn set_local_accept_fee_base(new_base: u64) {
ACCEPT_FEE_BASE.with(|base| base.set(Some(new_base))) 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) 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. /// Explicitly enable the local NRD feature flag.
pub fn set_local_nrd_enabled(enabled: bool) { pub fn set_local_nrd_enabled(enabled: bool) {
NRD_FEATURE_ENABLED.with(|flag| flag.set(Some(enabled))) NRD_FEATURE_ENABLED.with(|flag| flag.set(Some(enabled)))

View file

@ -44,10 +44,11 @@ pub const MAINNET_DNS_SEEDS: &[&str] = &[
]; ];
/// DNS Seeds with contact email associated - Testnet /// DNS Seeds with contact email associated - Testnet
pub const TESTNET_DNS_SEEDS: &[&str] = &[ 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.lesceller.com", // q.lesceller@gmail.com
"floonet.seed.grin.prokapi.com", // hendi@prokapi.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( pub fn connect_and_monitor(

View file

@ -86,8 +86,15 @@ where
/// Initializes the OneTime, should only be called once after construction. /// Initializes the OneTime, should only be called once after construction.
/// Will panic (via assert) if called more than once. /// Will panic (via assert) if called more than once.
pub fn init(&self, value: T) { 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(); let mut inner = self.inner.write();
assert!(inner.is_none()); if !is_override {
assert!(inner.is_none());
}
*inner = Some(value); *inner = Some(value);
} }