Skip to content

Commit a5b26ca

Browse files
authored
Merge pull request #365 from input-output-hk/gd/handle-byron-blocks-mint
fix: handle byron blocks mint where there is no issuer
2 parents a8a36b4 + cf2bdef commit a5b26ca

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

modules/chain_store/src/stores/fjall.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ pub struct FjallStore {
1111
keyspace: Keyspace,
1212
blocks: FjallBlockStore,
1313
txs: FjallTXStore,
14-
last_persisted_block: Option<u64>,
14+
last_persisted_block: u64,
1515
}
1616

1717
const DEFAULT_DATABASE_PATH: &str = "fjall-blocks";
@@ -36,11 +36,17 @@ impl FjallStore {
3636
let txs = FjallTXStore::new(&keyspace)?;
3737

3838
let last_persisted_block = if !clear {
39-
blocks.block_hashes_by_number.iter().next_back().and_then(|res| {
40-
res.ok().and_then(|(key, _)| key.as_ref().try_into().ok().map(u64::from_be_bytes))
41-
})
39+
blocks
40+
.block_hashes_by_number
41+
.iter()
42+
.next_back()
43+
.and_then(|res| {
44+
res.ok()
45+
.and_then(|(key, _)| key.as_ref().try_into().ok().map(u64::from_be_bytes))
46+
})
47+
.unwrap_or(0)
4248
} else {
43-
None
49+
0
4450
};
4551

4652
Ok(Self {
@@ -81,10 +87,7 @@ impl super::Store for FjallStore {
8187
}
8288

8389
fn should_persist(&self, block_number: u64) -> bool {
84-
match self.last_persisted_block {
85-
Some(last) => block_number > last,
86-
None => false,
87-
}
90+
block_number > self.last_persisted_block
8891
}
8992

9093
fn get_block_by_hash(&self, hash: &[u8]) -> Result<Option<Block>> {

modules/epochs_state/src/epochs_state.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,7 @@ impl EpochsState {
150150
let span = info_span!("epochs_state.handle_mint", block = block_info.number);
151151
span.in_scope(|| {
152152
if let Some(header) = header.as_ref() {
153-
if let Some(issuer_vkey) = header.issuer_vkey() {
154-
state.handle_mint(block_info, issuer_vkey);
155-
}
153+
state.handle_mint(block_info, header.issuer_vkey());
156154
}
157155
});
158156
}

modules/epochs_state/src/state.rs

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -183,14 +183,15 @@ impl State {
183183

184184
// Handle mint
185185
// This will update last block time
186-
pub fn handle_mint(&mut self, block_info: &BlockInfo, issuer_vkey: &[u8]) {
186+
pub fn handle_mint(&mut self, block_info: &BlockInfo, issuer_vkey: Option<&[u8]>) {
187187
self.last_block_time = block_info.timestamp;
188188
self.last_block_height = block_info.number;
189189
self.epoch_blocks += 1;
190-
let spo_id = PoolId::from(keyhash_224(issuer_vkey));
191-
192-
// Count one on this hash
193-
*(self.blocks_minted.entry(spo_id).or_insert(0)) += 1;
190+
if let Some(issuer_vkey) = issuer_vkey {
191+
let spo_id = PoolId::from(keyhash_224(issuer_vkey));
192+
// Count one on this hash
193+
*(self.blocks_minted.entry(spo_id).or_insert(0)) += 1;
194+
}
194195
}
195196

196197
// Handle Block Txs
@@ -329,9 +330,9 @@ mod tests {
329330
let mut state = State::new(&GenesisValues::mainnet());
330331
let issuer = b"issuer_key";
331332
let mut block = make_block(100);
332-
state.handle_mint(&block, issuer);
333+
state.handle_mint(&block, Some(issuer));
333334
block.number += 1;
334-
state.handle_mint(&block, issuer);
335+
state.handle_mint(&block, Some(issuer));
335336

336337
assert_eq!(state.epoch_blocks, 2);
337338
assert_eq!(state.blocks_minted.len(), 1);
@@ -345,11 +346,11 @@ mod tests {
345346
fn handle_mint_multiple_issuer_records_counts() {
346347
let mut state = State::new(&GenesisValues::mainnet());
347348
let mut block = make_block(100);
348-
state.handle_mint(&block, b"issuer_1");
349+
state.handle_mint(&block, Some(b"issuer_1"));
349350
block.number += 1;
350-
state.handle_mint(&block, b"issuer_2");
351+
state.handle_mint(&block, Some(b"issuer_2"));
351352
block.number += 1;
352-
state.handle_mint(&block, b"issuer_2");
353+
state.handle_mint(&block, Some(b"issuer_2"));
353354

354355
assert_eq!(state.epoch_blocks, 3);
355356
assert_eq!(state.blocks_minted.len(), 2);
@@ -408,7 +409,7 @@ mod tests {
408409
let genesis = GenesisValues::mainnet();
409410
let mut state = State::new(&genesis);
410411
let block = make_block(1);
411-
state.handle_mint(&block, b"issuer_1");
412+
state.handle_mint(&block, Some(b"issuer_1"));
412413
state.handle_block_txs(
413414
&block,
414415
&BlockTxsMessage {
@@ -464,7 +465,7 @@ mod tests {
464465
)));
465466
let mut state = history.lock().await.get_current_state();
466467
let mut block = make_block(1);
467-
state.handle_mint(&block, b"issuer_1");
468+
state.handle_mint(&block, Some(b"issuer_1"));
468469
state.handle_block_txs(
469470
&block,
470471
&BlockTxsMessage {
@@ -477,7 +478,7 @@ mod tests {
477478

478479
let mut state = history.lock().await.get_current_state();
479480
block.number += 1;
480-
state.handle_mint(&block, b"issuer_1");
481+
state.handle_mint(&block, Some(b"issuer_1"));
481482
state.handle_block_txs(
482483
&block,
483484
&BlockTxsMessage {
@@ -494,7 +495,7 @@ mod tests {
494495

495496
block = make_rolled_back_block(0);
496497
let mut state = history.lock().await.get_rolled_back_state(block.number);
497-
state.handle_mint(&block, b"issuer_2");
498+
state.handle_mint(&block, Some(b"issuer_2"));
498499
state.handle_block_txs(
499500
&block,
500501
&BlockTxsMessage {

0 commit comments

Comments
 (0)