-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
enhancement: store blocks while crawling transactions and chain
- Loading branch information
Showing
16 changed files
with
240 additions
and
7 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
use anyhow::Context; | ||
use diesel::upsert::excluded; | ||
use diesel::{ExpressionMethods, PgConnection, RunQueryDsl}; | ||
use orm::blocks::BlockInsertDb; | ||
use orm::schema::blocks; | ||
use shared::block::Block; | ||
use tendermint_rpc::endpoint::block::Response as TendermintBlockResponse; | ||
|
||
pub fn upsert_block( | ||
transaction_conn: &mut PgConnection, | ||
block: Block, | ||
tm_block_response: TendermintBlockResponse, | ||
) -> anyhow::Result<()> { | ||
diesel::insert_into(blocks::table) | ||
.values::<&BlockInsertDb>(&BlockInsertDb::from(( | ||
block, | ||
tm_block_response, | ||
))) | ||
.on_conflict(blocks::height) | ||
.do_update() | ||
.set(( | ||
blocks::hash.eq(excluded(blocks::hash)), | ||
blocks::app_hash.eq(excluded(blocks::app_hash)), | ||
blocks::timestamp.eq(excluded(blocks::timestamp)), | ||
blocks::proposer.eq(excluded(blocks::proposer)), | ||
blocks::epoch.eq(excluded(blocks::epoch)), | ||
)) | ||
.execute(transaction_conn) | ||
.context("Failed to insert block in db")?; | ||
|
||
anyhow::Ok(()) | ||
} |
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,4 +1,5 @@ | ||
pub mod balance; | ||
pub mod block; | ||
pub mod crawler_state; | ||
pub mod gov; | ||
pub mod pos; | ||
|
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- This file should undo anything in `up.sql` | ||
ALTER TABLE balance_changes | ||
DROP CONSTRAINT fk_balance_changes_height; | ||
|
||
ALTER TABLE wrapper_transactions | ||
DROP CONSTRAINT fk_wrapper_transactions_height; | ||
|
||
DROP TABLE IF EXISTS blocks; | ||
|
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 |
---|---|---|
@@ -0,0 +1,40 @@ | ||
-- Your SQL goes here | ||
CREATE TABLE blocks ( | ||
height integer PRIMARY KEY, | ||
hash VARCHAR(64), | ||
app_hash varchar(64), | ||
timestamp timestamp, | ||
proposer varchar, | ||
epoch int | ||
); | ||
|
||
ALTER TABLE blocks | ||
ADD UNIQUE (hash); | ||
|
||
CREATE INDEX index_blocks_epoch ON blocks (epoch); | ||
|
||
-- Populate null blocks for all existing wrapper_transactions and balance_changes to satisfy foreign key constraints | ||
INSERT INTO blocks ( SELECT DISTINCT | ||
height, | ||
NULL::varchar AS hash, | ||
NULL::varchar AS app_hash, | ||
NULL::timestamp AS timestamp, | ||
NULL::varchar AS proposer, | ||
NULL::int AS epoch | ||
FROM ( SELECT DISTINCT | ||
block_height AS height | ||
FROM | ||
wrapper_transactions | ||
UNION | ||
SELECT DISTINCT | ||
height | ||
FROM | ||
balance_changes)); | ||
|
||
-- Create foreign key constraints for wrapper_transactions and balance_changes | ||
ALTER TABLE wrapper_transactions | ||
ADD CONSTRAINT fk_wrapper_transactions_height FOREIGN KEY (block_height) REFERENCES blocks (height) ON DELETE RESTRICT; | ||
|
||
ALTER TABLE balance_changes | ||
ADD CONSTRAINT fk_balance_changes_height FOREIGN KEY (height) REFERENCES blocks (height) ON DELETE RESTRICT; | ||
|
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use diesel::{Insertable, Queryable, Selectable}; | ||
use shared::block::Block; | ||
use tendermint_rpc::endpoint::block::Response as TendermintBlockResponse; | ||
|
||
use crate::schema::blocks; | ||
|
||
#[derive(Insertable, Clone, Queryable, Selectable, Debug)] | ||
#[diesel(table_name = blocks)] | ||
#[diesel(check_for_backend(diesel::pg::Pg))] | ||
pub struct BlockInsertDb { | ||
pub height: i32, | ||
pub hash: String, | ||
pub app_hash: String, | ||
pub timestamp: chrono::NaiveDateTime, | ||
pub proposer: String, | ||
pub epoch: i32, | ||
} | ||
|
||
pub type BlockDb = BlockInsertDb; | ||
|
||
impl From<(Block, TendermintBlockResponse)> for BlockInsertDb { | ||
fn from( | ||
(block, tm_block_response): (Block, TendermintBlockResponse), | ||
) -> Self { | ||
let timestamp = chrono::DateTime::from_timestamp( | ||
tm_block_response.block.header.time.unix_timestamp(), | ||
0, | ||
) | ||
.expect("Invalid timestamp") | ||
.naive_utc(); | ||
|
||
Self { | ||
height: block.header.height as i32, | ||
hash: block.hash.to_string(), | ||
app_hash: block.header.app_hash.to_string(), | ||
timestamp, | ||
proposer: block.header.proposer_address, | ||
epoch: block.epoch as i32, | ||
} | ||
} | ||
} | ||
|
||
impl BlockInsertDb { | ||
pub fn fake(height: i32) -> Self { | ||
Self { | ||
height, | ||
hash: height.to_string(), // fake hash but ensures uniqueness with height | ||
app_hash: "fake_app_hash".to_string(), // doesn't require uniqueness | ||
timestamp: chrono::DateTime::from_timestamp(0, 0) | ||
.unwrap() | ||
.naive_utc(), | ||
proposer: "fake_proposer".to_string(), | ||
epoch: 0, | ||
} | ||
} | ||
} |
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,4 +1,5 @@ | ||
pub mod balances; | ||
pub mod blocks; | ||
pub mod bond; | ||
pub mod crawler_state; | ||
pub mod gas; | ||
|
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
Oops, something went wrong.