Skip to content

Commit 11a1aa4

Browse files
committed
db/block: add miner reward metric
1 parent 61787a0 commit 11a1aa4

File tree

5 files changed

+13
-1
lines changed

5 files changed

+13
-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
@@ -7,5 +7,6 @@ CREATE TABLE blocks (
77
tx_count INTEGER NOT NULL,
88
size INTEGER NOT NULL,
99
weight BIGINT NOT NULL,
10-
turnover BIGINT NOT NULL
10+
turnover BIGINT NOT NULL,
11+
miner_reward BIGINT NOT NULL
1112
);

src/db/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct Block {
2222
pub size: i32,
2323
pub weight: i64,
2424
pub turnover: i64,
25+
pub miner_reward: i64,
2526
}
2627

2728
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
@@ -11,5 +11,6 @@ diesel::table! {
1111
size -> Integer,
1212
weight -> BigInt,
1313
turnover -> BigInt,
14+
miner_reward -> BigInt,
1415
}
1516
}

src/parser/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ impl BlockchainParser {
6565
.flat_map(|tx| tx.output.iter().map(|output| output.value))
6666
.sum();
6767

68+
let miner_reward = block
69+
.coinbase()
70+
.map_or_else(|| 0, |cb| cb.output.iter().map(|output| output.value).sum());
71+
6872
tracing::trace!(target: "parser", "on_block(height={}) called", self.cur_height);
6973
blocks.push(crate::db::Block {
7074
height: self.cur_height.try_into()?,
@@ -76,6 +80,7 @@ impl BlockchainParser {
7680
size: block.size().try_into()?,
7781
weight: block.weight().to_wu().try_into()?,
7882
turnover: turnover.try_into()?,
83+
miner_reward: miner_reward.try_into()?,
7984
});
8085
if blocks.len() == block_buffer_size {
8186
self.db.insert_blocks(blocks)?;

tests/bitcoin.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,4 +129,8 @@ fn test_blocks_db() {
129129
u64::try_from(parser.db().block(170).unwrap().turnover).unwrap(),
130130
100 * bitcoin::Amount::ONE_BTC.to_sat()
131131
);
132+
assert_eq!(
133+
u64::try_from(parser.db().block(50).unwrap().miner_reward).unwrap(),
134+
50 * bitcoin::Amount::ONE_BTC.to_sat()
135+
);
132136
}

0 commit comments

Comments
 (0)