Skip to content

Commit

Permalink
refactor beacon protobuf
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisCarriere committed Nov 21, 2024
1 parent 43a16e5 commit a36e450
Show file tree
Hide file tree
Showing 18 changed files with 630 additions and 91 deletions.
2 changes: 1 addition & 1 deletion blocks/beacon/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ crate-type = ["cdylib"]

[dependencies]
common = { path = "../../common" }
substreams-ethereum = { workspace = true }
# substreams-beacon = { workspace = true }
substreams = { workspace = true }
prost = { workspace = true }
prost-types = { workspace = true }
35 changes: 35 additions & 0 deletions blocks/beacon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# `Beacon` Raw Blockchain Data

> Cosmos
> [`sf.beacon.type.v1.Block`](https://buf.build/pinax/firehose-beacon/docs/main:sf.beacon.type.v1)
- [x] **Block**
- [x] **Blob**
- [x] **Deposit**
- [x] **Withdrawal**
- [x] **Attestation**
- [x] **AttesterSlashing**
- [x] **BlsToExecutionChange**
- [x] **ProposerSlashing**
- [x] **VoluntaryExit**

## Graph

```mermaid
graph TD;
map_events[map: map_events];
sf.substreams.v1.Clock[source: sf.substreams.v1.Clock] --> map_events;
sf.beacon.type.v1.Block[source: sf.beacon.type.v1.Block] --> map_events;
```

## Modules

```bash
Name: map_events
Initial block: 0
Kind: map
Input: source: sf.substreams.v1.Clock
Input: source: sf.beacon.type.v1.Block
Output Type: proto:beacon.Events
Hash: fa22a8c409b87871edb85e753a7feda2e94bbdb9
```
2 changes: 1 addition & 1 deletion blocks/beacon/src/attestations.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use common::utils::bytes_to_hex;

use crate::{
pb::{beacon::Attestation as RawAttestation, sf::beacon::r#type::v1::Attestation},
pb::{pinax::beacon::v1::Attestation as RawAttestation, sf::beacon::r#type::v1::Attestation},
structs::BlockTimestamp,
};

Expand Down
57 changes: 30 additions & 27 deletions blocks/beacon/src/attester_slashings.rs
Original file line number Diff line number Diff line change
@@ -1,39 +1,42 @@
use common::utils::{bytes_to_hex, number_array_to_string};
use common::utils::bytes_to_hex;

use crate::{
pb::{beacon::AttesterSlashing as RawAttesterSlashing, sf::beacon::r#type::v1::AttesterSlashing},
pb::{
pinax::beacon::v1::AttesterSlashing as RawAttesterSlashing,
sf::beacon::r#type::v1::{AttesterSlashing, IndexedAttestation},
},
structs::BlockTimestamp,
};

pub fn collect_attester_slashings(attester_slashings: &Vec<AttesterSlashing>, timestamp: &BlockTimestamp) -> Vec<RawAttesterSlashing> {
let mut vec = Vec::<RawAttesterSlashing>::new();

for (index, attester_slashing) in attester_slashings.iter().enumerate() {
vec.push(RawAttesterSlashing {
block_time: Some(timestamp.time),
block_number: timestamp.number,
block_date: timestamp.date.clone(),
block_hash: timestamp.hash.clone(),
index: index as u64,
attestation_1_attesting_indices: number_array_to_string(&attester_slashing.attestation_1.as_ref().unwrap().attesting_indices),
attestation_1_slot: attester_slashing.attestation_1.as_ref().unwrap().data.as_ref().unwrap().slot,
attestation_1_committee_index: attester_slashing.attestation_1.as_ref().unwrap().data.as_ref().unwrap().committee_index,
attestation_1_beacon_block_root: bytes_to_hex(&attester_slashing.attestation_1.as_ref().unwrap().data.as_ref().unwrap().beacon_block_root),
attestation_1_source_epoch: attester_slashing.attestation_1.as_ref().unwrap().data.as_ref().unwrap().source.as_ref().unwrap().epoch,
attestation_1_source_root: bytes_to_hex(&attester_slashing.attestation_1.as_ref().unwrap().data.as_ref().unwrap().source.as_ref().unwrap().root),
attestation_1_target_epoch: attester_slashing.attestation_1.as_ref().unwrap().data.as_ref().unwrap().target.as_ref().unwrap().epoch,
attestation_1_target_root: bytes_to_hex(&attester_slashing.attestation_1.as_ref().unwrap().data.as_ref().unwrap().target.as_ref().unwrap().root),
attestation_1_signature: bytes_to_hex(&attester_slashing.attestation_1.as_ref().unwrap().signature),
attestation_2_attesting_indices: number_array_to_string(&attester_slashing.attestation_2.as_ref().unwrap().attesting_indices),
attestation_2_slot: attester_slashing.attestation_2.as_ref().unwrap().data.as_ref().unwrap().slot,
attestation_2_committee_index: attester_slashing.attestation_2.as_ref().unwrap().data.as_ref().unwrap().committee_index,
attestation_2_beacon_block_root: bytes_to_hex(&attester_slashing.attestation_2.as_ref().unwrap().data.as_ref().unwrap().beacon_block_root),
attestation_2_source_epoch: attester_slashing.attestation_2.as_ref().unwrap().data.as_ref().unwrap().source.as_ref().unwrap().epoch,
attestation_2_source_root: bytes_to_hex(&attester_slashing.attestation_2.as_ref().unwrap().data.as_ref().unwrap().source.as_ref().unwrap().root),
attestation_2_target_epoch: attester_slashing.attestation_2.as_ref().unwrap().data.as_ref().unwrap().target.as_ref().unwrap().epoch,
attestation_2_target_root: bytes_to_hex(&attester_slashing.attestation_2.as_ref().unwrap().data.as_ref().unwrap().target.as_ref().unwrap().root),
attestation_2_signature: bytes_to_hex(&attester_slashing.attestation_2.as_ref().unwrap().signature),
});
vec.push(parse_attester_slashings(index as u64, &attester_slashing.attestation_1.as_ref().unwrap(), timestamp));
vec.push(parse_attester_slashings(index as u64, &attester_slashing.attestation_2.as_ref().unwrap(), timestamp));
}
vec
}

pub fn parse_attester_slashings(index: u64, indexed_attestation: &IndexedAttestation, timestamp: &BlockTimestamp) -> RawAttesterSlashing {
let data = indexed_attestation.data.as_ref().unwrap();
let source = data.source.as_ref().unwrap();
let target = data.target.as_ref().unwrap();

RawAttesterSlashing {
block_time: Some(timestamp.time),
block_number: timestamp.number,
block_date: timestamp.date.clone(),
block_hash: timestamp.hash.clone(),
index,
attesting_indices: indexed_attestation.attesting_indices.clone(),
slot: data.slot,
committee_index: data.committee_index,
beacon_block_root: bytes_to_hex(&data.beacon_block_root),
source_epoch: source.epoch,
source_root: bytes_to_hex(&source.root),
target_epoch: target.epoch,
target_root: bytes_to_hex(&target.root),
signature: bytes_to_hex(&indexed_attestation.signature),
}
}
2 changes: 1 addition & 1 deletion blocks/beacon/src/blobs.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use common::utils::bytes_to_hex;

use crate::{
pb::{beacon::Blob as RawBlob, sf::beacon::r#type::v1::Blob},
pb::{pinax::beacon::v1::Blob as RawBlob, sf::beacon::r#type::v1::Blob},
structs::BlockTimestamp,
utils::encode_hex_2d_array,
};
Expand Down
2 changes: 1 addition & 1 deletion blocks/beacon/src/blocks.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use common::utils::bytes_to_hex;

use crate::{
pb::{beacon::Block, sf::beacon::r#type::v1::Block as BeaconBlock},
pb::{pinax::beacon::v1::Block, sf::beacon::r#type::v1::Block as BeaconBlock},
structs::BlockTimestamp,
};

Expand Down
2 changes: 1 addition & 1 deletion blocks/beacon/src/bls_to_execution_changes.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use common::utils::bytes_to_hex;

use crate::{
pb::{beacon::BlsToExecutionChange as RawBlsToExecutionChange, sf::beacon::r#type::v1::SignedBlsToExecutionChange},
pb::{pinax::beacon::v1::BlsToExecutionChange as RawBlsToExecutionChange, sf::beacon::r#type::v1::SignedBlsToExecutionChange},
structs::BlockTimestamp,
};

Expand Down
2 changes: 1 addition & 1 deletion blocks/beacon/src/deposits.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use common::utils::bytes_to_hex;

use crate::{
pb::{beacon::Deposit as RawDeposit, sf::beacon::r#type::v1::Deposit},
pb::{pinax::beacon::v1::Deposit as RawDeposit, sf::beacon::r#type::v1::Deposit},
structs::BlockTimestamp,
utils::encode_hex_2d_array,
};
Expand Down
2 changes: 1 addition & 1 deletion blocks/beacon/src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use substreams::{errors::Error, pb::substreams::Clock};
use crate::{
blocks::collect_blocks,
pb::{
beacon::Events,
pinax::beacon::v1::Events,
sf::beacon::r#type::v1::{AltairBody, BellatrixBody, Block as BeaconBlock, CapellaBody, DenebBody, Phase0Body},
},
utils::build_timestamp,
Expand Down
11 changes: 11 additions & 0 deletions blocks/beacon/src/pb/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,14 @@ pub mod pinax {
}
}
}
pub mod sf {
pub mod beacon {
pub mod r#type {
// @@protoc_insertion_point(attribute:sf.beacon.type.v1)
pub mod v1 {
include!("sf.beacon.type.v1.rs");
// @@protoc_insertion_point(sf.beacon.type.v1)
}
}
}
}
32 changes: 16 additions & 16 deletions blocks/beacon/src/pb/pinax.beacon.v1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,24 +178,24 @@ pub struct AttesterSlashing {
#[prost(uint64, tag="5")]
pub index: u64,
/// attestation
#[prost(string, tag="6")]
pub attestation_attesting_indices: ::prost::alloc::string::String,
#[prost(uint64, repeated, tag="6")]
pub attesting_indices: ::prost::alloc::vec::Vec<u64>,
#[prost(uint64, tag="7")]
pub attestation_slot: u64,
pub slot: u64,
#[prost(uint64, tag="8")]
pub attestation_committee_index: u64,
pub committee_index: u64,
#[prost(string, tag="9")]
pub attestation_beacon_block_root: ::prost::alloc::string::String,
pub beacon_block_root: ::prost::alloc::string::String,
#[prost(uint64, tag="10")]
pub attestation_source_epoch: u64,
pub source_epoch: u64,
#[prost(string, tag="11")]
pub attestation_source_root: ::prost::alloc::string::String,
pub source_root: ::prost::alloc::string::String,
#[prost(uint64, tag="12")]
pub attestation_target_epoch: u64,
pub target_epoch: u64,
#[prost(string, tag="13")]
pub attestation_target_root: ::prost::alloc::string::String,
pub target_root: ::prost::alloc::string::String,
#[prost(string, tag="14")]
pub attestation_signature: ::prost::alloc::string::String,
pub signature: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down Expand Up @@ -238,17 +238,17 @@ pub struct ProposerSlashing {
pub index: u64,
/// signed header
#[prost(uint64, tag="6")]
pub signed_header_slot: u64,
pub slot: u64,
#[prost(uint64, tag="7")]
pub signed_header_proposer_index: u64,
pub proposer_index: u64,
#[prost(string, tag="8")]
pub signed_header_parent_root: ::prost::alloc::string::String,
pub parent_root: ::prost::alloc::string::String,
#[prost(string, tag="9")]
pub signed_header_state_root: ::prost::alloc::string::String,
pub state_root: ::prost::alloc::string::String,
#[prost(string, tag="10")]
pub signed_header_body_root: ::prost::alloc::string::String,
pub body_root: ::prost::alloc::string::String,
#[prost(string, tag="11")]
pub signed_header_signature: ::prost::alloc::string::String,
pub signature: ::prost::alloc::string::String,
}
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Clone, PartialEq, ::prost::Message)]
Expand Down
Loading

0 comments on commit a36e450

Please sign in to comment.