Skip to content

[chain] ChainStore::new won't check if the FlatChainStore is already populated #911

@luisschwab

Description

@luisschwab

ChainStore::new() won't check if the FlatChainStore is already populated. This requires calling ChainState::load_chain_state(), and then calling ChainState::new() if it throws BlockchainError::ChainNotInitialized.

The example below will always create a new ChainState at height 0, even if the underlying FlatChainStore is already populated.

// Create configuration for the chain store.
let chain_store_cfg = FlatChainStoreConfig::new(
    self.node_configuration
        .data_directory
        .join("chain")
        .to_string_lossy()
        .to_string(),
);

// Try to load an existing [`FlatChainStore`]
// from the file system, or create a new one.
let chain_store: FlatChainStore = match FlatChainStore::new(chain_store_cfg.clone()) {
    Ok(store) => store,
    Err(e) => {
        error!("Failed to open FlatChainStore: {:?}", e);
        return Err(e.into());
    }
};

// Create a [`ChainState`] from the [`FlatChainStore`].
let chain_state: Arc<ChainState<FlatChainStore>> = Arc::new(ChainState::new(
    chain_store,
    self.node_configuration.network,
    AssumeValidArg::Hardcoded,
));

I think having a ChainStore::open() that handles both cases internally would be better. For example:

// Create configuration for the chain store.
let chain_store_cfg = FlatChainStoreConfig::new(
    self.node_configuration
        .data_directory
        .join("chain")
        .to_string_lossy()
        .to_string(),
);

// Load or create a [`ChainState`] from the `chain_store_cfg`,
let chain_state: Arc<ChainState<FlatChainStore>> = Arc::new(
    ChainState::open(chain_store_cfg, self.node_configuration.network, AssumeValidArg::Hardcoded)
        .map_err(|e| BuilderError::ChainState(Arc::new(e)))?
);

Metadata

Metadata

Labels

bugSomething isn't working

Type

Projects

Status

Done

Relationships

None yet

Development

No branches or pull requests

Issue actions