Skip to content

Commit

Permalink
add comments to bitcoin
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Nov 22, 2024
1 parent 006bdba commit 1d40d06
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 65 deletions.
27 changes: 18 additions & 9 deletions blocks/bitcoin/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,36 @@ use substreams_bitcoin::pb::btc::v1::Block;
use crate::pb::bitcoin::Block as OutputBlock;

pub fn collect_block(block: &Block, timestamp: &BlockTimestamp) -> OutputBlock {
// Get the coinbase from the first transaction
let coinbase = block.tx.first().unwrap().vin.first().unwrap().coinbase.clone();

OutputBlock {
// clock
time: Some(timestamp.time),
height: block.height as u32,
date: timestamp.date.clone(),
hash: block.hash.clone(),

// block
bits: block.bits.clone(),
chainwork: block.chainwork.clone(),
difficulty: block.difficulty,
total_fees: calculate_total_fees(block),
total_reward: calculate_total_reward(block),
mint_reward: calculate_mint_reward(block.height),
merkle_root: block.merkle_root.clone(),
transaction_count: block.tx.len() as u64,
nonce: block.nonce as u32,
// Get the coinbase from the first transaction
coinbase: block.tx.first().unwrap().vin.first().unwrap().coinbase.clone(),
nonce: block.nonce,
coinbase,
previous_block_hash: block.previous_hash.clone(),
size: block.size,
stripped_size: block.stripped_size,
version: block.version,
weight: block.weight,

// counters
size: block.size,
stripped_size: block.stripped_size,
transaction_count: block.tx.len() as u64,
total_fees: calculate_total_fees(block),
total_reward: calculate_total_reward(block),
mint_reward: calculate_mint_reward(block.height),
total_inputs: block.tx.iter().map(|tx| tx.vin.len() as u32).sum(),
total_outputs: block.tx.iter().map(|tx| tx.vout.len() as u32).sum(),
}
}

Expand Down
55 changes: 35 additions & 20 deletions blocks/bitcoin/src/pb/bitcoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Events {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Block {
/// clock
#[prost(message, optional, tag="1")]
pub time: ::core::option::Option<::prost_types::Timestamp>,
#[prost(uint32, tag="2")]
Expand All @@ -23,50 +24,58 @@ pub struct Block {
pub date: ::prost::alloc::string::String,
#[prost(string, tag="4")]
pub hash: ::prost::alloc::string::String,
/// block
#[prost(string, tag="5")]
pub bits: ::prost::alloc::string::String,
#[prost(string, tag="6")]
pub chainwork: ::prost::alloc::string::String,
#[prost(double, tag="7")]
pub difficulty: f64,
#[prost(double, tag="8")]
pub total_fees: f64,
#[prost(double, tag="9")]
pub total_reward: f64,
#[prost(double, tag="10")]
pub mint_reward: f64,
#[prost(string, tag="11")]
#[prost(string, tag="8")]
pub merkle_root: ::prost::alloc::string::String,
#[prost(uint64, tag="12")]
#[prost(uint64, tag="9")]
pub transaction_count: u64,
#[prost(uint32, tag="13")]
#[prost(uint32, tag="10")]
pub nonce: u32,
#[prost(string, tag="14")]
#[prost(string, tag="11")]
pub coinbase: ::prost::alloc::string::String,
#[prost(string, tag="15")]
#[prost(string, tag="12")]
pub previous_block_hash: ::prost::alloc::string::String,
#[prost(int32, tag="16")]
pub size: i32,
#[prost(int32, tag="17")]
pub stripped_size: i32,
#[prost(int32, tag="18")]
#[prost(int32, tag="13")]
pub version: i32,
#[prost(int32, tag="19")]
#[prost(int32, tag="14")]
pub weight: i32,
/// counters
#[prost(int32, tag="20")]
pub size: i32,
#[prost(int32, tag="21")]
pub stripped_size: i32,
#[prost(double, tag="22")]
pub total_fees: f64,
#[prost(double, tag="23")]
pub total_reward: f64,
#[prost(double, tag="24")]
pub mint_reward: f64,
#[prost(uint32, tag="25")]
pub total_inputs: u32,
#[prost(uint32, tag="26")]
pub total_outputs: u32,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Transaction {
/// block
#[prost(message, optional, tag="1")]
pub block_time: ::core::option::Option<::prost_types::Timestamp>,
#[prost(string, tag="2")]
pub block_date: ::prost::alloc::string::String,
#[prost(uint32, tag="3")]
pub block_height: u32,
#[prost(uint32, tag="4")]
pub index: u32,
#[prost(string, tag="5")]
#[prost(string, tag="4")]
pub block_hash: ::prost::alloc::string::String,
/// transaction
#[prost(uint32, tag="5")]
pub index: u32,
#[prost(string, tag="6")]
pub id: ::prost::alloc::string::String,
#[prost(uint32, tag="7")]
Expand Down Expand Up @@ -97,6 +106,7 @@ pub struct Transaction {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Input {
/// block
#[prost(message, optional, tag="1")]
pub block_time: ::core::option::Option<::prost_types::Timestamp>,
#[prost(string, tag="2")]
Expand All @@ -105,8 +115,10 @@ pub struct Input {
pub block_height: u32,
#[prost(string, tag="4")]
pub block_hash: ::prost::alloc::string::String,
/// transaction
#[prost(string, tag="5")]
pub tx_id: ::prost::alloc::string::String,
/// input
#[prost(uint32, tag="6")]
pub index: u32,
#[prost(uint32, tag="7")]
Expand Down Expand Up @@ -143,6 +155,7 @@ pub struct Input {
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
pub struct Output {
/// block
#[prost(message, optional, tag="1")]
pub block_time: ::core::option::Option<::prost_types::Timestamp>,
#[prost(string, tag="2")]
Expand All @@ -151,8 +164,10 @@ pub struct Output {
pub block_height: u32,
#[prost(string, tag="4")]
pub block_hash: ::prost::alloc::string::String,
/// transaction
#[prost(string, tag="5")]
pub tx_id: ::prost::alloc::string::String,
/// output
#[prost(uint32, tag="6")]
pub index: u32,
#[prost(double, tag="7")]
Expand Down
92 changes: 56 additions & 36 deletions proto/bitcoin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -12,54 +12,69 @@ message Events {
}

message Block {
// clock
google.protobuf.Timestamp time = 1;
uint32 height = 2;
string date = 3;
string hash = 4;
string bits = 5;
string chainwork = 6;
double difficulty = 7;
double total_fees = 8;
double total_reward = 9;
double mint_reward = 10;
string merkle_root = 11;
uint64 transaction_count = 12;
uint32 nonce = 13;
string coinbase = 14;
string previous_block_hash = 15;
int32 size = 16;
int32 stripped_size = 17;
int32 version = 18;
int32 weight = 19;
uint32 height = 2;
string date = 3;
string hash = 4;

// block
string bits = 5;
string chainwork = 6;
double difficulty = 7;
string merkle_root = 8;
uint64 transaction_count = 9;
uint32 nonce = 10;
string coinbase = 11;
string previous_block_hash = 12;
int32 version = 13;
int32 weight = 14;

// counters
int32 size = 20;
int32 stripped_size = 21;
double total_fees = 22;
double total_reward = 23;
double mint_reward = 24;
uint32 total_inputs = 25;
uint32 total_outputs = 26;
}

message Transaction {
google.protobuf.Timestamp block_time = 1;
string block_date = 2;
uint32 block_height = 3;
uint32 index = 4;
string block_hash = 5;
string id = 6;
uint32 lock_time = 7;
int32 size = 8;
int32 virtual_size = 9;
string coinbase = 10;
bool is_coinbase = 11;
int64 version = 12;
int32 input_count = 13;
int32 output_count = 14;
repeated string input_tx_ids = 15;
repeated uint32 input_output_indices = 16;
repeated double output_values = 17;
string hex = 18;
// block
google.protobuf.Timestamp block_time = 1;
string block_date = 2;
uint32 block_height = 3;
string block_hash = 4;

// transaction
uint32 index = 5;
string id = 6;
uint32 lock_time = 7;
int32 size = 8;
int32 virtual_size = 9;
string coinbase = 10;
bool is_coinbase = 11;
int64 version = 12;
int32 input_count = 13;
int32 output_count = 14;
repeated string input_tx_ids = 15;
repeated uint32 input_output_indices = 16;
repeated double output_values = 17;
string hex = 18;
}

message Input {
// block
google.protobuf.Timestamp block_time = 1;
string block_date = 2;
uint32 block_height = 3;
string block_hash = 4;

// transaction
string tx_id = 5;

// input
uint32 index = 6;
uint32 spent_block_height = 7;
string spent_tx_id = 8;
Expand All @@ -80,11 +95,16 @@ message Input {


message Output {
// block
google.protobuf.Timestamp block_time = 1;
string block_date = 2;
uint32 block_height = 3;
string block_hash = 4;

// transaction
string tx_id = 5;

// output
uint32 index = 6;
double value = 7;
string address = 8;
Expand Down

0 comments on commit 1d40d06

Please sign in to comment.