Skip to content

Commit c56c227

Browse files
author
Adrian Nagy
committed
refactor: Remove is_archive in long call chains, use a global OnceLock instead
1 parent fb1c689 commit c56c227

File tree

20 files changed

+83
-111
lines changed

20 files changed

+83
-111
lines changed

cli/src/commands/node/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ impl Node {
273273
}
274274

275275
if let Some(address) = self.archive_address {
276+
openmina_core::IS_ARCHIVE
277+
.set(true)
278+
.expect("IS_ARCHIVE already set");
276279
node::core::info!(
277280
summary = "Archive mode enabled",
278281
address = address.to_string()
@@ -282,6 +285,10 @@ impl Node {
282285

283286
let socket_addr = socket_addrs.first().expect("No socket address found");
284287
node_builder.archive(*socket_addr);
288+
} else {
289+
openmina_core::IS_ARCHIVE
290+
.set(false)
291+
.expect("IS_ARCHIVE already set");
285292
}
286293

287294
if let Some(sec_key) = self.run_snarker {

core/src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ pub mod transaction;
2323
pub mod consensus;
2424

2525
mod substate;
26+
use std::sync::OnceLock;
27+
2628
pub use substate::{Substate, SubstateAccess, SubstateResult};
2729

2830
pub mod network;
@@ -34,6 +36,8 @@ pub use chain_id::*;
3436
pub mod encrypted_key;
3537
pub use encrypted_key::*;
3638

39+
pub static IS_ARCHIVE: OnceLock<bool> = OnceLock::new();
40+
3741
mod work_dir {
3842
use once_cell::sync::OnceCell;
3943
use std::path::PathBuf;

docker-compose.archive.devnet.compare.yml

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ services:
55
env_file:
66
- .env
77
healthcheck:
8-
test: ["CMD-SHELL", "psql -U postgres -d archive -tAc \"SELECT COUNT(*) FROM pg_database WHERE datname='archive';\" | grep -q '^1$'"]
9-
# test: ["CMD-SHELL", "pg_isready -U postgres"]
8+
# test: ["CMD-SHELL", "psql -U postgres -d archive -tAc \"SELECT COUNT(*) FROM pg_database WHERE datname='archive';\" | grep -q '^1$'"]
9+
test: ["CMD-SHELL", "pg_isready -U postgres"]
1010
interval: 5s
1111
timeout: 10s
1212
retries: 10
@@ -21,8 +21,8 @@ services:
2121
env_file:
2222
- .env
2323
healthcheck:
24-
test: ["CMD-SHELL", "psql -U postgres -d archive -tAc \"SELECT COUNT(*) FROM pg_database WHERE datname='archive';\" | grep -q '^1$'"]
25-
# test: ["CMD-SHELL", "pg_isready -U postgres"]
24+
# test: ["CMD-SHELL", "psql -U postgres -d archive -tAc \"SELECT COUNT(*) FROM pg_database WHERE datname='archive';\" | grep -q '^1$'"]
25+
test: ["CMD-SHELL", "pg_isready -U postgres"]
2626
interval: 5s
2727
timeout: 10s
2828
retries: 10
@@ -59,9 +59,9 @@ services:
5959
- ./producer-dashboard/src/archive/sql:/sql
6060
depends_on:
6161
postgres-ocaml:
62-
condition: service_started
62+
condition: service_healthy
6363
postgres-openmina:
64-
condition: service_started
64+
condition: service_healthy
6565

6666
archive-ocaml:
6767
image: adrnagy/mina-archive
@@ -107,22 +107,21 @@ services:
107107
VERBOSE: true
108108
LOG_LEVEL: Info
109109
MINA_LIBP2P_PASS: v3rys3cr3t
110-
PEERS: |
111-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40101/p2p/12D3KooWNGY3guz8pYHrVEqs8se4MSnnmpgguyQYDazMbVCyrMnS
112-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40102/p2p/12D3KooWSqZ4qtysb8Du4yVpcc5SYc3gsRuNqgMomggw6hekATWg
113-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40103/p2p/12D3KooWSHiGavQpamDPEc6rPaqT4PoS1Lr9aDfrfg5dKM2V6x3H
114-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40104/p2p/12D3KooWA3yPrTaLXsggVSCG4mr7c33YNdz5DSs87LszRUVt9vLT
115-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40105/p2p/12D3KooWCLcUWCdU4VstETztxE3feQyS57dVDdzBhmkj5tiCaha8
116-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40106/p2p/12D3KooWNZWqEoCuhMrc9tTMxtEsfxmeFhjh2agUcmzJFNKxQnNA
117-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40107/p2p/12D3KooWAMSP94SM3icSeAXeBmPUuZ5JvwrZ5w87fpRHVeJkdboe
118-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40108/p2p/12D3KooWL5gPf5CrARVPhBi6KsDHmB1gsJKZ4vWrcLweWyMjpB5e
119-
/dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40109/p2p/12D3KooWT1nNJLGE8jWcshPSq3FmSXmmNn2MzfmvJcWYZ1HrtHnZ
120110
command: >
121111
daemon
122112
--archive-address archive-ocaml:3086
123113
--insecure-rest-server
124114
--rest-port 5000
125115
--enable-peer-exchange true
116+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40101/p2p/12D3KooWNGY3guz8pYHrVEqs8se4MSnnmpgguyQYDazMbVCyrMnS
117+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40102/p2p/12D3KooWSqZ4qtysb8Du4yVpcc5SYc3gsRuNqgMomggw6hekATWg
118+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40103/p2p/12D3KooWSHiGavQpamDPEc6rPaqT4PoS1Lr9aDfrfg5dKM2V6x3H
119+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40104/p2p/12D3KooWA3yPrTaLXsggVSCG4mr7c33YNdz5DSs87LszRUVt9vLT
120+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40105/p2p/12D3KooWCLcUWCdU4VstETztxE3feQyS57dVDdzBhmkj5tiCaha8
121+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40106/p2p/12D3KooWNZWqEoCuhMrc9tTMxtEsfxmeFhjh2agUcmzJFNKxQnNA
122+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40107/p2p/12D3KooWAMSP94SM3icSeAXeBmPUuZ5JvwrZ5w87fpRHVeJkdboe
123+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40108/p2p/12D3KooWL5gPf5CrARVPhBi6KsDHmB1gsJKZ4vWrcLweWyMjpB5e
124+
--peer /dns4/primary-tcp-proxy.hz.minaprotocol.network/tcp/40109/p2p/12D3KooWT1nNJLGE8jWcshPSq3FmSXmmNn2MzfmvJcWYZ1HrtHnZ
126125
# volumes:
127126
# # - /tmp/ocaml-node-keys:/keys
128127
ports:

docker-compose.archive.devnet.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ services:
6161
- "127.0.0.1:3086:3086"
6262

6363
node-openmina:
64-
# TODO: Update once we have a new release
65-
image: openmina/openmina:0.13.0
64+
image: openmina/openmina:latest
6665
container_name: node-openmina
6766
command: >
6867
node

ledger/src/database/database.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,20 +49,16 @@ impl Database<V2> {
4949
}
5050

5151
impl Database<V2> {
52-
pub fn create_with_dir(depth: u8, dir_name: Option<PathBuf>, is_archive: bool) -> Self {
53-
let db = DatabaseImpl::<V2>::create_with_dir(depth, dir_name, is_archive);
52+
pub fn create_with_dir(depth: u8, dir_name: Option<PathBuf>) -> Self {
53+
let db = DatabaseImpl::<V2>::create_with_dir(depth, dir_name);
5454

5555
Self {
5656
inner: Arc::new(Mutex::new(db)),
5757
}
5858
}
5959

6060
pub fn create(depth: u8) -> Self {
61-
Self::create_with_dir(depth, None, false)
62-
}
63-
64-
pub fn create_with_archive(depth: u8) -> Self {
65-
Self::create_with_dir(depth, None, true)
61+
Self::create_with_dir(depth, None)
6662
}
6763

6864
pub fn root_hash(&mut self) -> Fp {

ledger/src/database/database_impl.rs

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::{
66

77
use mina_hasher::Fp;
88
use mina_signer::CompressedPubKey;
9+
use openmina_core::IS_ARCHIVE;
910

1011
use crate::{
1112
next_uuid, Account, AccountId, AccountIndex, AccountLegacy, Address, AddressIterator,
@@ -34,7 +35,7 @@ impl<T: TreeVersion> std::fmt::Debug for DatabaseImpl<T> {
3435
// .field("accounts", &self.accounts)
3536
.field("hashes_matrix", &self.hashes_matrix)
3637
// .field("id_to_addr", &self.id_to_addr)
37-
// .field("token_to_account", &self.token_to_account)
38+
// .field("token_owners", &self.token_owners)
3839
// .field("depth", &self.depth)
3940
// .field("last_location", &self.last_location)
4041
.field("naccounts", &self.naccounts)
@@ -106,8 +107,8 @@ impl DatabaseImpl<V2> {
106107
self.naccounts += 1;
107108

108109
if !token_id.is_default() {
109-
if let Some(token_to_account) = self.token_owners.as_mut() {
110-
token_to_account.insert(account_id.derive_token_id(), account_id.clone());
110+
if let Some(token_owners) = self.token_owners.as_mut() {
111+
token_owners.insert(account_id.derive_token_id(), account_id.clone());
111112
}
112113
}
113114
self.id_to_addr.insert(account_id, location.clone());
@@ -310,7 +311,7 @@ impl DatabaseImpl<V1> {
310311
}
311312

312313
impl DatabaseImpl<V2> {
313-
pub fn create_with_dir(depth: u8, dir_name: Option<PathBuf>, is_archive: bool) -> Self {
314+
pub fn create_with_dir(depth: u8, dir_name: Option<PathBuf>) -> Self {
314315
assert!((1..0xfe).contains(&depth));
315316

316317
const NACCOUNTS: usize = 10_000;
@@ -338,7 +339,7 @@ impl DatabaseImpl<V2> {
338339

339340
// std::fs::create_dir_all(&path).ok();
340341

341-
let token_to_account = if is_archive {
342+
let token_owners = if IS_ARCHIVE.get().cloned().unwrap_or_default() {
342343
Some(HashMap::with_capacity(NACCOUNTS))
343344
} else {
344345
None
@@ -350,16 +351,16 @@ impl DatabaseImpl<V2> {
350351
last_location: None,
351352
naccounts: 0,
352353
id_to_addr: HashMap::with_capacity(NACCOUNTS),
353-
token_owners: token_to_account,
354+
token_owners,
354355
uuid,
355356
directory: path,
356357
hashes_matrix: HashesMatrix::new(depth as usize),
357358
// root_hash: Default::default(),
358359
}
359360
}
360361

361-
pub fn create(depth: u8, is_archive: bool) -> Self {
362-
Self::create_with_dir(depth, None, is_archive)
362+
pub fn create(depth: u8) -> Self {
363+
Self::create_with_dir(depth, None)
363364
}
364365

365366
pub fn root_hash(&mut self) -> Fp {
@@ -534,7 +535,7 @@ impl BaseLedger for DatabaseImpl<V2> {
534535
fn token_owner(&self, token_id: TokenId) -> Option<AccountId> {
535536
self.token_owners
536537
.as_ref()
537-
.and_then(|token_to_account| token_to_account.get(&token_id).cloned())
538+
.and_then(|token_owners| token_owners.get(&token_id).cloned())
538539
}
539540

540541
fn tokens(&self, public_key: CompressedPubKey) -> HashSet<TokenId> {
@@ -695,17 +696,17 @@ impl BaseLedger for DatabaseImpl<V2> {
695696
let id = account.id();
696697
self.id_to_addr.remove(&id);
697698
if !id.token_id.is_default() {
698-
if let Some(token_to_account) = self.token_owners.as_mut() {
699-
token_to_account.remove(&id.derive_token_id());
699+
if let Some(token_owners) = self.token_owners.as_mut() {
700+
token_owners.remove(&id.derive_token_id());
700701
}
701702
}
702703
} else {
703704
self.naccounts += 1;
704705
}
705706

706707
if !account.token_id.is_default() {
707-
if let Some(token_to_account) = self.token_owners.as_mut() {
708-
token_to_account.insert(account.id().derive_token_id(), id.clone());
708+
if let Some(token_owners) = self.token_owners.as_mut() {
709+
token_owners.insert(account.id().derive_token_id(), id.clone());
709710
}
710711
}
711712
self.id_to_addr.insert(id, addr.clone());
@@ -838,8 +839,8 @@ impl BaseLedger for DatabaseImpl<V2> {
838839
let id = account.id();
839840
self.id_to_addr.remove(&id);
840841
if !id.token_id.is_default() {
841-
if let Some(token_to_account) = self.token_owners.as_mut() {
842-
token_to_account.remove(&id.derive_token_id());
842+
if let Some(token_owners) = self.token_owners.as_mut() {
843+
token_owners.remove(&id.derive_token_id());
843844
}
844845
}
845846

ledger/src/mask/mask.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ use std::{
77
use mina_hasher::Fp;
88
use mina_signer::CompressedPubKey;
99

10+
use openmina_core::IS_ARCHIVE;
11+
1012
use crate::{
1113
account::{Account, AccountId, TokenId},
1214
address::Address,
@@ -97,10 +99,16 @@ impl Mask {
9799
pub fn new_unattached(depth: usize) -> Self {
98100
let uuid = next_uuid();
99101

102+
let is_archive = IS_ARCHIVE.get().cloned().unwrap_or_default();
103+
100104
let mask = Self {
101105
inner: Arc::new(Mutex::new(MaskImpl::Unattached {
102106
owning_account: Default::default(),
103-
token_owners: Default::default(),
107+
token_owners: if is_archive {
108+
Some(Default::default())
109+
} else {
110+
None
111+
},
104112
id_to_addr: Default::default(),
105113
last_location: None,
106114
depth: depth as u8,

ledger/src/mask/mask_impl.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ impl MaskImpl {
393393

394394
let (accounts, hashes) = {
395395
if let Some(to) = token_owners.as_mut() {
396-
to.clear()
396+
to.clear();
397397
}
398398
id_to_addr.clear();
399399
(std::mem::take(owning_account), hashes.take())

node/common/src/service/service.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,8 @@ impl node::service::LedgerService for NodeService {
181181
}
182182

183183
impl node::service::TransitionFrontierGenesisService for NodeService {
184-
fn load_genesis(&mut self, config: Arc<GenesisConfig>, is_archive: bool) {
185-
let res = match config.load(is_archive) {
184+
fn load_genesis(&mut self, config: Arc<GenesisConfig>) {
185+
let res = match config.load() {
186186
Err(err) => Err(err.to_string()),
187187
Ok((masks, data)) => {
188188
masks

node/native/src/node/builder.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,6 @@ impl NodeBuilder {
345345
snarker: self.snarker,
346346
consensus_constants: consensus_consts.clone(),
347347
testing_run: false,
348-
is_archive: self.archive.is_some(),
349348
},
350349
p2p: self.p2p,
351350
ledger: LedgerConfig {},

0 commit comments

Comments
 (0)