This repository organizes and processes Bitcoin blockchain data within Snowflake. The ETL pipeline follows a sequential structure, where each directory and script must be executed in order. The final output, F_input_address_pairs.sql, is consumed by the Scala entity mapping code.
-
0_stnd: Standardizes raw blockchain data.
stnd_blocks.sql: Standardizes block data.stnd_txins.sql: Standardizes transaction input data.stnd_txouts.sql: Standardizes transaction output data.stnd_txs.sql: Standardizes transaction data.
-
1_core: Processes core features of the blockchain.
bitcoin.sql: Processes fundamental Bitcoin data.core_txouts.sql: Processes transaction outputs.- public_keys:
core_drop_new_public_keys.sql: Drops temporary public key tables.core_new_public_keys.sql: Identifies new public keys.core_public_keys_insert_records.sql: Inserts new public key records.core_public_keys.sql: Maintains the complete public key table.
-
1_work: Additional exploratory features (not currently used).
fees_per_block.sql: Calculates transaction fees per block.time_between_blocks.sql: Measures time intervals between blocks.
-
2_base: Constructs base tables and contextual data.
A_blocks_since_halving.sql: Aggregates block data since the last halving.B_txs_since_halving.sql: Aggregates transaction data since the last halving.C_txins_since_halving.sql: Aggregates transaction input data since the last halving.C_txouts_since_halving.sql: Aggregates transaction output data since the last halving.D_txins.sql: Processes all transaction input data.D_txouts_context.sql: Adds context to transaction outputs.D_txs_context.sql: Adds context to transactions.E_blocks_context.sql: Adds context to blocks.E_txouts.sql: Processes final transaction outputs.E_txs.sql: Processes final transactions.F_blocks.sql: Processes final block data.F_input_address_pairs.sql: Generates the final dataset consumed by the Scala entity mapper.
- Scripts within each subfolder depend on prior scripts in numerical and alphabetical order (e.g.,
0_stndmust run before1_core;A_must run beforeB_within2_base).
-
Configure Snowflake Connection: Set up your Snowflake connection settings within your environment or preferred query tool.
-
Run Scripts in Order: Execute scripts in their respective order, starting from
0_stndand progressing to2_base. For example:- Navigate to
sql/0_stndand run:-- Example: Run stnd_blocks.sql SOURCE stnd_blocks.sql;
- Proceed sequentially through all directories and scripts.
- Navigate to
-
Verify Outputs: After running the scripts, verify that the final output table,
F_input_address_pairs, is correctly populated:SELECT * FROM F_input_address_pairs;
The _since_halving.sql tables (e.g., A_blocks_since_halving.sql) include approximately 2016 blocks (~two weeks of data starting at the last halving height of 840,000). These are exploratory features and are not currently used in downstream processes.
The F_input_address_pairs table contains transaction input pairs. Example:
| tx_id | public_key_uuid_1 | public_key_uuid_2 |
|---|---|---|
| tx12345 | uuid1 | uuid2 |
| tx67890 | uuid3 | uuid4 |
For additional details or troubleshooting, consult the team or Snowflake documentation.
This project utilizes Spark and GraphX to perform entity mapping based on transaction input pairs. The FastEntityMapper.scala script reads transaction data from Snowflake, builds a graph of co-occurring public keys, computes connected components, and writes the resulting entity mapping back to Snowflake. Follow the steps below to set up and execute the script.
- Spark: Ensure Apache Spark is installed and configured.
- Snowflake Connector: The Snowflake Spark connector must be available.
- Docker (optional): If running with Docker, ensure Docker and Docker Compose are installed.
-
Set up Snowflake credentials: Ensure the following environment variables are defined:
export SNOWFLAKE_URL=<your_snowflake_url> export SNOWFLAKE_USER=<your_username> export SNOWFLAKE_PASSWORD=<your_password> export SNOWFLAKE_WAREHOUSE=<your_warehouse> export SNOWFLAKE_ROLE=<your_role>
-
Build the project: Use
sbtto compile the project:sbt compile
-
Run the script locally: Execute the main method of the
FastEntityMapperobject:sbt run
-
Run using Docker (optional): Build the Docker image:
docker build -t entity-mapper .Run the container:
docker-compose up
-
Verify output: The script writes the entity mapping to the
entity_mapping_50table in Snowflake. You can verify the output by querying Snowflake:SELECT * FROM warehouse.shared_sandbox.entity_mapping_50;
- Input Table: The script reads data from
warehouse.shared_sandbox.input_address_pairs_50in Snowflake. - Checkpoint Directory: Spark checkpoints intermediate data to
/tmp/spark-checkpointto improve fault tolerance. - Output Table: The resulting entity mapping is saved to
entity_mapping_50.
- Graph Construction:
- Public keys involved in the same transaction are treated as nodes.
- Edges are created between co-occurring public keys.
- GraphX's connected components algorithm identifies entities.
- Data Transformation:
- Transaction input pairs are grouped by
tx_id. - Unique pairs of public keys (
srcanddst) are extracted.
- Transaction input pairs are grouped by
- Entity Mapping:
- Public keys are mapped to unique entity IDs based on connected components.
The resulting Snowflake table will contain two columns:
public_key_uuid: Unique identifier for each public key.entity_id: ID of the entity to which the public key belongs.
For example:
| public_key_uuid | entity_id |
|---|---|
| 123e4567-e89b-12d3-a456 | 1 |
| 987f6543-e21b-34c6-d789 | 1 |
| abc12345-d678-90ef-ghij | 2 |
The provided Python script determines the optimal number of clusters and prepares the dataset for visualization. Follow the steps below to set up and run the script:
-
Create a new Conda environment:
Run the command:conda env create -f environment.yml
-
Activate the Conda environment:
conda activate cse6242_project
-
Set parameters based on your goal:
- To find the optimal number of clusters, set the parameter:
Ensure other parameters are configured as needed for the experiment.
find_clustering: bool = True
- To prepare the dataset based on a specific number of clusters, set the parameters:
find_clustering: bool = False n_components: int = <desired_number_of_clusters>
- To find the optimal number of clusters, set the parameter:
-
Run the application:
Execute the following command:python -m bitcoin_app.bitcoin
This backend system facilitates dimensional analysis of Bitcoin entities through Principal Component Analysis (PCA) and clustering techniques. Built with FastAPI and SQLite, it provides efficient querying of high-dimensional clustering results for visualization purposes.
app/ main.py
- Contains the FastAPI application and endpoint definitions.
models.py
- Defines the SQLAlchemy models for database interactions.
database.py
- Handles the database connection setup and session management.
import_data.py
- A script for ingesting data into the application.
requirements.txt
- Lists all the dependencies required for the project.
.env
- Stores environment variables for database credentials and configuration details.
data/ dataset_pca_cluster_sample.csv
- A sample dataset containing entity clustering information used in the application.
- Consoldiated Command to do everything below at once
source ./viz/dash/backend/app/btc/Scripts/activate && \
pip install -r ./viz/dash/backend/app/requirements.txt && \
python ./viz/dash/backend/app/import_data.py && \
uvicorn --app-dir ./viz/dash/backend/app main:app --reload- Create and activate virtual environment:
source ./viz/dash/backend/app/btc/Scripts/activate- Install dependencies:
pip install -r viz/dash/backend/app/requirements.txt-
change interpreter to use btc venv path..
-
Import data:
python ./viz/dash/backend/app/import_data.py- Start server:
uvicorn --app-dir ./viz/dash/backend/app main:app --reloadThe frontend system provides a highly interactive interface for visualizing and analyzing Bitcoin entity clustering data. Built with React, D3.js, and Three.js, it seamlessly integrates 2D statistical analysis and 3D clustering visualizations to enhance user understanding of Bitcoin clustering results.
public/data/
entity2.csv
- Data file containing network statistics.
src/components/
Clustering3D.jsx
- Component for 3D clustering visualization using the dataset.
StatAnalysisChart.jsx
- Renders statistical analysis charts for visual insights.
StatisticsPanel.jsx
- Displays key statistical summaries in the application.
src/ App.jsx
- The main React component serving as the application entry point.
index.css
- Defines global CSS styles for the application.
index.js
- Entry point for the React application, rendering the root component.
utils.js
- Contains utility functions used across the application.
Root Files
package.json
- Manages project dependencies and scripts for building, testing, and running the application.
README.md
- Documentation for the project, including instructions and usage details.
- Consolidated Command to do everything below at once
(cd viz/dash/frontend && npm install && npm run dev)- Install dependencies
npm install - Start the development server
npm run dev- Access the application on localhost to view visulizations