-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from blockworks-foundation/postgres_saving_bl…
…ocks Saving block and transactions in postgres table
- Loading branch information
Showing
12 changed files
with
315 additions
and
156 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
---|---|---|
@@ -1 +1,101 @@ | ||
use std::sync::Arc; | ||
|
||
use anyhow::Result; | ||
use async_trait::async_trait; | ||
use itertools::Itertools; | ||
use solana_lite_rpc_core::{ | ||
structures::{epoch::EpochCache, produced_block::ProducedBlock}, | ||
traits::block_storage_interface::BlockStorageInterface, | ||
}; | ||
use solana_rpc_client_api::config::RpcBlockConfig; | ||
use solana_sdk::{slot_history::Slot, stake_history::Epoch}; | ||
use tokio::sync::RwLock; | ||
|
||
use crate::postgres::{ | ||
postgres_block::PostgresBlock, postgres_session::PostgresSessionCache, | ||
postgres_transaction::PostgresTransaction, | ||
}; | ||
|
||
#[derive(Default, Clone, Copy)] | ||
pub struct PostgresData { | ||
from_slot: Slot, | ||
to_slot: Slot, | ||
current_epoch: Epoch, | ||
} | ||
|
||
pub struct PostgresBlockStore { | ||
session_cache: PostgresSessionCache, | ||
epoch_cache: EpochCache, | ||
postgres_data: Arc<RwLock<PostgresData>>, | ||
} | ||
|
||
impl PostgresBlockStore { | ||
pub async fn start_new_epoch(&self, schema: &String) -> Result<()> { | ||
// create schema for new epoch | ||
let session = self | ||
.session_cache | ||
.get_session() | ||
.await | ||
.expect("should get new postgres session"); | ||
|
||
let statement = format!("CREATE SCHEMA {};", schema); | ||
session.execute(&statement, &[]).await?; | ||
|
||
// Create blocks table | ||
let statement = PostgresBlock::create_statement(schema); | ||
session.execute(&statement, &[]).await?; | ||
|
||
// create transaction table | ||
let statement = PostgresTransaction::create_statement(schema); | ||
session.execute(&statement, &[]).await?; | ||
Ok(()) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl BlockStorageInterface for PostgresBlockStore { | ||
async fn save(&self, block: ProducedBlock) -> Result<()> { | ||
let PostgresData { current_epoch, .. } = { *self.postgres_data.read().await }; | ||
|
||
let slot = block.slot; | ||
let transactions = block | ||
.transactions | ||
.iter() | ||
.map(|x| PostgresTransaction::new(x, slot)) | ||
.collect_vec(); | ||
let postgres_block = PostgresBlock::from(&block); | ||
|
||
let epoch = self.epoch_cache.get_epoch_at_slot(slot); | ||
let schema = format!("EPOCH_{}", epoch.epoch); | ||
if current_epoch == 0 || current_epoch < epoch.epoch { | ||
self.postgres_data.write().await.current_epoch = epoch.epoch; | ||
self.start_new_epoch(&schema).await?; | ||
} | ||
|
||
const NUMBER_OF_TRANSACTION: usize = 20; | ||
|
||
// save transaction | ||
let chunks = transactions.chunks(NUMBER_OF_TRANSACTION); | ||
let session = self | ||
.session_cache | ||
.get_session() | ||
.await | ||
.expect("should get new postgres session"); | ||
for chunk in chunks { | ||
PostgresTransaction::save_transactions(&session, &schema, chunk).await?; | ||
} | ||
postgres_block.save(&session, &schema).await?; | ||
Ok(()) | ||
} | ||
|
||
async fn get(&self, slot: Slot, _config: RpcBlockConfig) -> Result<ProducedBlock> { | ||
let range = self.get_slot_range().await; | ||
if range.contains(&slot) {} | ||
todo!() | ||
} | ||
|
||
async fn get_slot_range(&self) -> std::ops::Range<Slot> { | ||
let lk = self.postgres_data.read().await; | ||
lk.from_slot..lk.to_slot + 1 | ||
} | ||
} |
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 |
---|---|---|
@@ -1,45 +1,83 @@ | ||
use super::postgres_session::SchemaSize; | ||
use solana_lite_rpc_core::{ | ||
commitment_utils::Commitment, encoding::BASE64, structures::produced_block::ProducedBlock, | ||
}; | ||
use solana_lite_rpc_core::{encoding::BASE64, structures::produced_block::ProducedBlock}; | ||
use tokio_postgres::types::ToSql; | ||
|
||
use super::postgres_session::PostgresSession; | ||
|
||
#[derive(Debug)] | ||
pub struct PostgresBlock { | ||
pub leader_id: Option<String>, | ||
pub slot: i64, | ||
pub blockhash: String, | ||
pub block_height: i64, | ||
pub slot: i64, | ||
pub parent_slot: i64, | ||
pub block_time: i64, | ||
pub commitment_config: i8, | ||
pub previous_blockhash: String, | ||
pub rewards: Option<String>, | ||
} | ||
|
||
impl SchemaSize for PostgresBlock { | ||
const DEFAULT_SIZE: usize = 4 * 8; | ||
const MAX_SIZE: usize = Self::DEFAULT_SIZE + 8; | ||
} | ||
const NB_ARUMENTS: usize = 7; | ||
|
||
impl From<ProducedBlock> for PostgresBlock { | ||
fn from(value: ProducedBlock) -> Self { | ||
let commitment = Commitment::from(&value.commitment_config); | ||
impl From<&ProducedBlock> for PostgresBlock { | ||
fn from(value: &ProducedBlock) -> Self { | ||
let rewards = value | ||
.rewards | ||
.as_ref() | ||
.map(|x| BASE64.serialize(x).ok()) | ||
.unwrap_or(None); | ||
|
||
Self { | ||
leader_id: value.leader_id, | ||
blockhash: value.blockhash, | ||
blockhash: value.blockhash.clone(), | ||
block_height: value.block_height as i64, | ||
slot: value.slot as i64, | ||
parent_slot: value.parent_slot as i64, | ||
block_time: value.block_time as i64, | ||
commitment_config: commitment as i8, | ||
previous_blockhash: value.previous_blockhash, | ||
previous_blockhash: value.previous_blockhash.clone(), | ||
rewards, | ||
} | ||
} | ||
} | ||
|
||
impl PostgresBlock { | ||
pub fn create_statement(schema: &String) -> String { | ||
format!( | ||
" | ||
CREATE TABLE {}.BLOCKS ( | ||
slot BIGINT PRIMARY KEY, | ||
blockhash STRING NOT NULL, | ||
leader_id STRING, | ||
block_height BIGINT NOT NULL, | ||
parent_slot BIGINT NOT NULL, | ||
block_time BIGINT NOT NULL, | ||
previous_blockhash STRING NOT NULL, | ||
rewards STRING, | ||
); | ||
", | ||
schema | ||
) | ||
} | ||
|
||
pub async fn save( | ||
&self, | ||
postgres_session: &PostgresSession, | ||
schema: &String, | ||
) -> anyhow::Result<()> { | ||
let mut query = format!( | ||
r#" | ||
INSERT INTO {}.BLOCKS (slot, blockhash, block_height, parent_slot, block_time, previous_blockhash, rewards) VALUES | ||
"#, | ||
schema | ||
); | ||
|
||
let mut args: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(NB_ARUMENTS); | ||
args.push(&self.slot); | ||
args.push(&self.blockhash); | ||
args.push(&self.block_height); | ||
args.push(&self.parent_slot); | ||
args.push(&self.block_time); | ||
args.push(&self.previous_blockhash); | ||
args.push(&self.rewards); | ||
|
||
PostgresSession::multiline_query(&mut query, NB_ARUMENTS, 1, &[]); | ||
postgres_session.execute(&query, &args).await?; | ||
Ok(()) | ||
} | ||
} |
Oops, something went wrong.