Skip to content

Commit 61787a0

Browse files
committed
db/block: add turnover metric
Defined as the sum of all outputs of all transactions. Using the sum across all outputs instead of across all inputs is not mere convenience due to the amount being available there, but we also want to capture the subsidy in this number.
1 parent 2d83ce1 commit 61787a0

File tree

5 files changed

+19
-1
lines changed

5 files changed

+19
-1
lines changed

migrations/2023-08-09-202146_add_opreturn_table/up.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ CREATE TABLE blocks (
66
nonce BIGINT NOT NULL,
77
tx_count INTEGER NOT NULL,
88
size INTEGER NOT NULL,
9-
weight BIGINT NOT NULL
9+
weight BIGINT NOT NULL,
10+
turnover BIGINT NOT NULL
1011
);

src/db/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ pub struct Block {
2121
pub tx_count: i32,
2222
pub size: i32,
2323
pub weight: i64,
24+
pub turnover: i64,
2425
}
2526

2627
const MIGRATIONS: diesel_migrations::EmbeddedMigrations = diesel_migrations::embed_migrations!();

src/db/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ diesel::table! {
1010
tx_count -> Integer,
1111
size -> Integer,
1212
weight -> BigInt,
13+
turnover -> BigInt,
1314
}
1415
}

src/parser/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ impl BlockchainParser {
5959
Self::on_header(&header, self.cur_height);
6060
let block = self.chain_storage.get_block(self.cur_height).unwrap();
6161

62+
let turnover: u64 = block
63+
.txdata
64+
.iter()
65+
.flat_map(|tx| tx.output.iter().map(|output| output.value))
66+
.sum();
67+
6268
tracing::trace!(target: "parser", "on_block(height={}) called", self.cur_height);
6369
blocks.push(crate::db::Block {
6470
height: self.cur_height.try_into()?,
@@ -69,6 +75,7 @@ impl BlockchainParser {
6975
tx_count: block.txdata.len().try_into()?,
7076
size: block.size().try_into()?,
7177
weight: block.weight().to_wu().try_into()?,
78+
turnover: turnover.try_into()?,
7279
});
7380
if blocks.len() == block_buffer_size {
7481
self.db.insert_blocks(blocks)?;

tests/bitcoin.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,12 @@ fn test_blocks_db() {
121121
let mut parser = parser();
122122
parser.start().unwrap();
123123
assert_eq!(parser.db().blocks_count().unwrap(), 171);
124+
assert_eq!(
125+
u64::try_from(parser.db().block(100).unwrap().turnover).unwrap(),
126+
50 * bitcoin::Amount::ONE_BTC.to_sat()
127+
);
128+
assert_eq!(
129+
u64::try_from(parser.db().block(170).unwrap().turnover).unwrap(),
130+
100 * bitcoin::Amount::ONE_BTC.to_sat()
131+
);
124132
}

0 commit comments

Comments
 (0)