-
Notifications
You must be signed in to change notification settings - Fork 0
feat: batch insert #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
7758114
batch insert
pthmas 832c37f
reorganize files
pthmas 216f66b
move from insert to binary copy
pthmas b6b0099
make tls optional for db connection for binary copy
pthmas 3d65fb6
derive Default for BlockBatch instead of manual initialization
pthmas 90ecc6f
simplify touch_addr using min() and |=
pthmas 7d58f29
treat missing receipts response as error to trigger retry
pthmas 953e6ae
allow writting a batch without updating last processed block
pthmas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,143 @@ | ||
| use bigdecimal::BigDecimal; | ||
| use std::collections::{HashMap, HashSet}; | ||
|
|
||
| // --------------------------------------------------------------------------- | ||
| // Batch accumulator - collects data from multiple blocks before writing to DB | ||
| // --------------------------------------------------------------------------- | ||
|
|
||
| pub(crate) struct AddrState { | ||
| pub(crate) first_seen_block: i64, | ||
| pub(crate) is_contract: bool, | ||
| pub(crate) tx_count_delta: i64, | ||
| } | ||
|
|
||
| pub(crate) struct NftTokenState { | ||
| pub(crate) owner: String, | ||
| pub(crate) last_transfer_block: i64, | ||
| } | ||
|
|
||
| pub(crate) struct BalanceDelta { | ||
| pub(crate) delta: BigDecimal, | ||
| pub(crate) last_block: i64, | ||
| } | ||
|
|
||
| /// Holds all data collected across a batch of blocks, ready for bulk insert. | ||
| /// Fields are columnar (parallel Vecs) so they can be passed directly to | ||
| /// PostgreSQL UNNEST without any further transformation. | ||
| #[derive(Default)] | ||
| pub(crate) struct BlockBatch { | ||
| // blocks | ||
| pub(crate) b_numbers: Vec<i64>, | ||
| pub(crate) b_hashes: Vec<String>, | ||
| pub(crate) b_parent_hashes: Vec<String>, | ||
| pub(crate) b_timestamps: Vec<i64>, | ||
| pub(crate) b_gas_used: Vec<i64>, | ||
| pub(crate) b_gas_limits: Vec<i64>, | ||
| pub(crate) b_tx_counts: Vec<i32>, | ||
|
|
||
| // transactions (receipt data merged in at collection time) | ||
| pub(crate) t_hashes: Vec<String>, | ||
| pub(crate) t_block_numbers: Vec<i64>, | ||
| pub(crate) t_block_indices: Vec<i32>, | ||
| pub(crate) t_froms: Vec<String>, | ||
| pub(crate) t_tos: Vec<Option<String>>, | ||
| pub(crate) t_values: Vec<String>, // BigDecimal as string → cast to numeric in SQL | ||
| pub(crate) t_gas_prices: Vec<String>, // BigDecimal as string → cast to numeric in SQL | ||
| pub(crate) t_gas_used: Vec<i64>, | ||
| pub(crate) t_input_data: Vec<Vec<u8>>, | ||
| pub(crate) t_statuses: Vec<bool>, | ||
| pub(crate) t_timestamps: Vec<i64>, | ||
| pub(crate) t_contracts_created: Vec<Option<String>>, | ||
|
|
||
| // tx_hash_lookup | ||
| pub(crate) tl_hashes: Vec<String>, | ||
| pub(crate) tl_block_numbers: Vec<i64>, | ||
|
|
||
| // addresses — deduplicated by address in Rust | ||
| pub(crate) addr_map: HashMap<String, AddrState>, | ||
|
|
||
| // event_logs | ||
| pub(crate) el_tx_hashes: Vec<String>, | ||
| pub(crate) el_log_indices: Vec<i32>, | ||
| pub(crate) el_addresses: Vec<String>, | ||
| pub(crate) el_topic0s: Vec<String>, | ||
| pub(crate) el_topic1s: Vec<Option<String>>, | ||
| pub(crate) el_topic2s: Vec<Option<String>>, | ||
| pub(crate) el_topic3s: Vec<Option<String>>, | ||
| pub(crate) el_datas: Vec<Vec<u8>>, | ||
| pub(crate) el_block_numbers: Vec<i64>, | ||
|
|
||
| // nft_contracts — deduplicated | ||
| pub(crate) nft_contract_addrs: Vec<String>, | ||
| pub(crate) nft_contract_first_seen: Vec<i64>, | ||
|
|
||
| // nft_transfers | ||
| pub(crate) nt_tx_hashes: Vec<String>, | ||
| pub(crate) nt_log_indices: Vec<i32>, | ||
| pub(crate) nt_contracts: Vec<String>, | ||
| pub(crate) nt_token_ids: Vec<String>, // BigDecimal as string | ||
| pub(crate) nt_froms: Vec<String>, | ||
| pub(crate) nt_tos: Vec<String>, | ||
| pub(crate) nt_block_numbers: Vec<i64>, | ||
| pub(crate) nt_timestamps: Vec<i64>, | ||
|
|
||
| // nft_tokens — deduplicated: only the latest transfer per token is kept | ||
| pub(crate) nft_token_map: HashMap<(String, String), NftTokenState>, | ||
|
|
||
| // erc20_contracts — new contracts only (no inline metadata fetch) | ||
| pub(crate) ec_addresses: Vec<String>, | ||
| pub(crate) ec_first_seen_blocks: Vec<i64>, | ||
|
|
||
| // erc20_transfers | ||
| pub(crate) et_tx_hashes: Vec<String>, | ||
| pub(crate) et_log_indices: Vec<i32>, | ||
| pub(crate) et_contracts: Vec<String>, | ||
| pub(crate) et_froms: Vec<String>, | ||
| pub(crate) et_tos: Vec<String>, | ||
| pub(crate) et_values: Vec<String>, // BigDecimal as string | ||
| pub(crate) et_block_numbers: Vec<i64>, | ||
| pub(crate) et_timestamps: Vec<i64>, | ||
|
|
||
| // erc20_balances — aggregated deltas per (address, contract) | ||
| pub(crate) balance_map: HashMap<(String, String), BalanceDelta>, | ||
|
|
||
| // Contracts newly discovered in this batch. | ||
| // These are NOT merged into the persistent known_* sets until after a | ||
| // successful write, so a failed write doesn't leave the in-memory sets | ||
| // ahead of the database. | ||
| pub(crate) new_erc20: HashSet<String>, | ||
| pub(crate) new_nft: HashSet<String>, | ||
|
|
||
| pub(crate) last_block: u64, | ||
| } | ||
|
|
||
| impl BlockBatch { | ||
| pub(crate) fn new() -> Self { | ||
| Self::default() | ||
| } | ||
pthmas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /// Upsert an address into the in-memory deduplication map. | ||
| /// tx_count_delta is added to whatever was already accumulated for this address. | ||
| pub(crate) fn touch_addr(&mut self, address: String, block_num: i64, is_contract: bool, tx_count_delta: i64) { | ||
| let entry = self.addr_map.entry(address).or_insert(AddrState { | ||
| first_seen_block: block_num, | ||
| is_contract: false, | ||
| tx_count_delta: 0, | ||
| }); | ||
| entry.first_seen_block = entry.first_seen_block.min(block_num); | ||
| entry.is_contract |= is_contract; | ||
| entry.tx_count_delta += tx_count_delta; | ||
| } | ||
|
|
||
| /// Add a balance delta for (address, contract). | ||
| /// Multiple transfers in the same batch are aggregated into one row. | ||
| pub(crate) fn apply_balance_delta(&mut self, address: String, contract: String, delta: BigDecimal, block: i64) { | ||
| let entry = self.balance_map.entry((address, contract)).or_insert(BalanceDelta { | ||
| delta: BigDecimal::from(0), | ||
| last_block: block, | ||
| }); | ||
| entry.delta += delta; | ||
| entry.last_block = entry.last_block.max(block); | ||
| } | ||
| } | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.