Skip to content

Commit 64e6a25

Browse files
CopilotSteake
andcommitted
Changes before error encountered
Co-authored-by: Steake <530040+Steake@users.noreply.github.com>
1 parent a0f1620 commit 64e6a25

2 files changed

Lines changed: 44 additions & 21 deletions

File tree

crates/bitcell-state/src/storage.rs

Lines changed: 36 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,17 @@ impl StorageManager {
4848

4949
/// Store a block header
5050
pub fn store_header(&self, height: u64, hash: &[u8], header: &[u8]) -> Result<(), rocksdb::Error> {
51-
let cf = self.db.cf_handle(CF_HEADERS).unwrap();
51+
let cf = self.db.cf_handle(CF_HEADERS)
52+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
5253

5354
let mut batch = WriteBatch::default();
5455
// Store by height
5556
batch.put_cf(cf, height.to_be_bytes(), header);
5657
// Store by hash
5758
batch.put_cf(cf, hash, header);
5859
// Update chain index
59-
let index_cf = self.db.cf_handle(CF_CHAIN_INDEX).unwrap();
60+
let index_cf = self.db.cf_handle(CF_CHAIN_INDEX)
61+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
6062
batch.put_cf(index_cf, b"latest_height", height.to_be_bytes());
6163
batch.put_cf(index_cf, b"latest_hash", hash);
6264

@@ -65,33 +67,41 @@ impl StorageManager {
6567

6668
/// Store a full block
6769
pub fn store_block(&self, hash: &[u8], block: &[u8]) -> Result<(), rocksdb::Error> {
68-
let cf = self.db.cf_handle(CF_BLOCKS).unwrap();
70+
let cf = self.db.cf_handle(CF_BLOCKS)
71+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
6972
self.db.put_cf(cf, hash, block)
7073
}
7174

7275
/// Get block by hash
7376
pub fn get_block(&self, hash: &[u8]) -> Result<Option<Vec<u8>>, rocksdb::Error> {
74-
let cf = self.db.cf_handle(CF_BLOCKS).unwrap();
77+
let cf = self.db.cf_handle(CF_BLOCKS)
78+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
7579
self.db.get_cf(cf, hash)
7680
}
7781

7882
/// Get header by height
7983
pub fn get_header_by_height(&self, height: u64) -> Result<Option<Vec<u8>>, rocksdb::Error> {
80-
let cf = self.db.cf_handle(CF_HEADERS).unwrap();
84+
let cf = self.db.cf_handle(CF_HEADERS)
85+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
8186
self.db.get_cf(cf, height.to_be_bytes())
8287
}
8388

8489
/// Get header by hash
8590
pub fn get_header_by_hash(&self, hash: &[u8]) -> Result<Option<Vec<u8>>, rocksdb::Error> {
86-
let cf = self.db.cf_handle(CF_HEADERS).unwrap();
91+
let cf = self.db.cf_handle(CF_HEADERS)
92+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
8793
self.db.get_cf(cf, hash)
8894
}
8995

9096
/// Get latest chain height
9197
pub fn get_latest_height(&self) -> Result<Option<u64>, rocksdb::Error> {
92-
let cf = self.db.cf_handle(CF_CHAIN_INDEX).unwrap();
98+
let cf = self.db.cf_handle(CF_CHAIN_INDEX)
99+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
93100
if let Some(bytes) = self.db.get_cf(cf, b"latest_height")? {
94-
let height = u64::from_be_bytes(bytes.as_slice().try_into().unwrap());
101+
let height = u64::from_be_bytes(
102+
bytes.as_slice().try_into()
103+
.map_err(|_| rocksdb::Error::new("Invalid height data".to_string()))?
104+
);
95105
Ok(Some(height))
96106
} else {
97107
Ok(None)
@@ -100,14 +110,17 @@ impl StorageManager {
100110

101111
/// Store account state
102112
pub fn store_account(&self, address: &[u8], account: &Account) -> Result<(), rocksdb::Error> {
103-
let cf = self.db.cf_handle(CF_ACCOUNTS).unwrap();
104-
let data = bincode::serialize(account).unwrap();
113+
let cf = self.db.cf_handle(CF_ACCOUNTS)
114+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
115+
let data = bincode::serialize(account)
116+
.map_err(|e| rocksdb::Error::new(format!("Serialization error: {}", e)))?;
105117
self.db.put_cf(cf, address, data)
106118
}
107119

108120
/// Get account state
109121
pub fn get_account(&self, address: &[u8]) -> Result<Option<Account>, rocksdb::Error> {
110-
let cf = self.db.cf_handle(CF_ACCOUNTS).unwrap();
122+
let cf = self.db.cf_handle(CF_ACCOUNTS)
123+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
111124
if let Some(data) = self.db.get_cf(cf, address)? {
112125
Ok(bincode::deserialize(&data).ok())
113126
} else {
@@ -117,14 +130,17 @@ impl StorageManager {
117130

118131
/// Store bond state
119132
pub fn store_bond(&self, miner_id: &[u8], bond: &BondState) -> Result<(), rocksdb::Error> {
120-
let cf = self.db.cf_handle(CF_BONDS).unwrap();
121-
let data = bincode::serialize(bond).unwrap();
133+
let cf = self.db.cf_handle(CF_BONDS)
134+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
135+
let data = bincode::serialize(bond)
136+
.map_err(|e| rocksdb::Error::new(format!("Serialization error: {}", e)))?;
122137
self.db.put_cf(cf, miner_id, data)
123138
}
124139

125140
/// Get bond state
126141
pub fn get_bond(&self, miner_id: &[u8]) -> Result<Option<BondState>, rocksdb::Error> {
127-
let cf = self.db.cf_handle(CF_BONDS).unwrap();
142+
let cf = self.db.cf_handle(CF_BONDS)
143+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
128144
if let Some(data) = self.db.get_cf(cf, miner_id)? {
129145
Ok(bincode::deserialize(&data).ok())
130146
} else {
@@ -134,13 +150,15 @@ impl StorageManager {
134150

135151
/// Store state root for a given height
136152
pub fn store_state_root(&self, height: u64, root: &[u8]) -> Result<(), rocksdb::Error> {
137-
let cf = self.db.cf_handle(CF_STATE_ROOTS).unwrap();
153+
let cf = self.db.cf_handle(CF_STATE_ROOTS)
154+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
138155
self.db.put_cf(cf, height.to_be_bytes(), root)
139156
}
140157

141158
/// Get state root for a given height
142159
pub fn get_state_root(&self, height: u64) -> Result<Option<Vec<u8>>, rocksdb::Error> {
143-
let cf = self.db.cf_handle(CF_STATE_ROOTS).unwrap();
160+
let cf = self.db.cf_handle(CF_STATE_ROOTS)
161+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
144162
self.db.get_cf(cf, height.to_be_bytes())
145163
}
146164

@@ -152,7 +170,8 @@ impl StorageManager {
152170
}
153171

154172
let prune_until = latest - keep_last;
155-
let cf = self.db.cf_handle(CF_BLOCKS).unwrap();
173+
let cf = self.db.cf_handle(CF_BLOCKS)
174+
.ok_or_else(|| rocksdb::Error::new("Column family not found".to_string()))?;
156175

157176
// This is a simplified version - in production would iterate and delete
158177
for height in 0..prune_until {

crates/bitcell-zkp/src/battle_constraints.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -216,12 +216,14 @@ fn conway_step<F: PrimeField>(
216216
let cell = &grid[i][j];
217217
// Check if cell is alive (value > 0) by checking all bits
218218
let cell_bits = cell.to_bits_le()?;
219-
let is_alive = cell_bits.iter().fold(Boolean::FALSE, |acc, bit| acc.or(bit).unwrap());
219+
let is_alive = cell_bits.iter().try_fold(Boolean::FALSE, |acc, bit| {
220+
acc.or(bit).map_err(|_| SynthesisError::Unsatisfiable)
221+
})?;
220222

221223
// Survival: 2 or 3 neighbors
222224
let count_bits = neighbor_count.to_bits_le()?;
223-
let two_bits = UInt8::constant(2).to_bits_le().unwrap();
224-
let three_bits = UInt8::constant(3).to_bits_le().unwrap();
225+
let two_bits = UInt8::constant(2).to_bits_le()?;
226+
let three_bits = UInt8::constant(3).to_bits_le()?;
225227

226228
let has_2_neighbors = check_bits_equal(&count_bits, &two_bits)?;
227229
let has_3_neighbors = check_bits_equal(&count_bits, &three_bits)?;
@@ -270,7 +272,9 @@ fn count_neighbors<F: PrimeField>(
270272

271273
let neighbor = &grid[ni][nj];
272274
let neighbor_bits = neighbor.to_bits_le()?;
273-
let is_alive = neighbor_bits.iter().fold(Boolean::FALSE, |acc, bit| acc.or(bit).unwrap());
275+
let is_alive = neighbor_bits.iter().try_fold(Boolean::FALSE, |acc, bit| {
276+
acc.or(bit).map_err(|_| SynthesisError::Unsatisfiable)
277+
})?;
274278

275279
let one = UInt8::constant(1);
276280
// Manual addition for UInt8 by converting to bits and adding

0 commit comments

Comments
 (0)