-
Notifications
You must be signed in to change notification settings - Fork 234
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
93 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use pathfinder_common::ReorgCounter; | ||
|
||
use crate::prelude::*; | ||
|
||
pub(super) fn increment_reorg_counter(tx: &Transaction<'_>) -> anyhow::Result<()> { | ||
tx.inner().execute( | ||
"UPDATE reorg_counter SET counter=counter+1 WHERE id = 1", | ||
[], | ||
)?; | ||
|
||
Ok(()) | ||
} | ||
|
||
pub(super) fn reorg_counter(tx: &Transaction<'_>) -> anyhow::Result<ReorgCounter> { | ||
// This table always contains exactly one row. | ||
tx.inner() | ||
.query_row( | ||
"SELECT counter FROM reorg_counter WHERE id = 1", | ||
[], | ||
|row| row.get_reorg_counter(0), | ||
) | ||
.map_err(|e| e.into()) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use crate::Storage; | ||
|
||
use super::*; | ||
|
||
#[test] | ||
fn empty_is_zero() { | ||
let storage = Storage::in_memory().unwrap(); | ||
let mut connection = storage.connection().unwrap(); | ||
let tx = connection.transaction().unwrap(); | ||
|
||
let result = reorg_counter(&tx).unwrap(); | ||
assert_eq!(result, ReorgCounter::new(0)); | ||
} | ||
|
||
#[test] | ||
fn increment() { | ||
let storage = Storage::in_memory().unwrap(); | ||
let mut connection = storage.connection().unwrap(); | ||
let tx = connection.transaction().unwrap(); | ||
|
||
increment_reorg_counter(&tx).unwrap(); | ||
let result = reorg_counter(&tx).unwrap(); | ||
assert_eq!(result, ReorgCounter::new(1)); | ||
|
||
increment_reorg_counter(&tx).unwrap(); | ||
let result = reorg_counter(&tx).unwrap(); | ||
assert_eq!(result, ReorgCounter::new(2)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters