img
  • By Franz Inc.
  • 10 December, 2019

Bitcoin Blockchain RDF Model in AllegroGraph: Setup & Query Examples

For more examples visit – https://github.com/franzinc/agraph-examples

Quick Answer

AllegroGraph offers an RDF model for representing Bitcoin chain data, enabling users to store and query blockchain information within a graph database. This model allows for the semantic representation of Bitcoin entities such as blocks and transactions, facilitating complex analytical queries using SPARQL. Franz Inc. provides a Python tool to extract data from a Bitcoin node and load it into an AllegroGraph instance, demonstrating how to analyze the blockchain's structure. For instance, the model can represent the genesis block, allowing users to query specific details like transaction IDs or total amounts.

Introduction
This example demonstrates an RDF model for Bitcoin chain data as well as a Python tool to pull the data from a Bitcoin node into an instance of an AllegroGraph graph database. The model description itself can be found in the Turtle file model.ttl.

The following Turtle example demonstrates how this RDF model can be used to represent complete chain entities (given example is a genesis block – the first block in the mainnet Bitcoin chain; script strings omitted for brevity):

@prefix : <https://raw.githubusercontent.com/franzinc/agraph-examples/master/data/bitcoin/model.ttl#>
@prefix btc: <bitcoin://>

btc:blk0
:height 0;
:hash “000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f”;
:time 1231006505;
:version 1;
:transaction btc:4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b.

btc:4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b
:lockTime 0;
:input [:unlockScript “…”.];
:output [:amount 5000000000; :lockScript “…”.].

Setup
The following examples assume AllegroGraph triple store and assume it is already installed and running on the target machine. The following AG instance settings settings are assumed as well:

host: localhost (default);
port: 10035 (default);
username: aguser;
password: agpassword.

We also assume the following bitcoind settings:

host: localhost (default);
port: 8332 (default);
username: btcuser;
password: btcpassword.

First, install the tool by cloning this repository, setting up virtual environment and installing the dependencies:

git clone http://github.com/franzinc/agraph-examples
cd agraph-examples/data/bitcoin
python3 -m venv .
source ./bin/activate
pip3 install -r requirements.txt

The following command starts the process of loading bitcoin chain data into an AG repository named bitcoin using 4 loader processes:

./convert.py \
–source=http://btcuser:btcpassword@localhost:8332 \
–destination=http://aguser:agpassword@localhost:10035 \
–name=bitcoin \
–workers=4 \
–clear

Example queries
Following are the examples of using SPARQL to extract different information about block data:

  • number of known blocks:

PREFIX : <https://raw.githubusercontent.com/franzinc/agraph-examples/master/data/bitcoin/model.ttl#>
SELECT (COUNT(*) AS ?count) WHERE { ?b a btcm:Block. }

  • total number of transactions:

PREFIX : <https://raw.githubusercontent.com/franzinc/agraph-examples/master/data/bitcoin/model.ttl#>
SELECT (COUNT(*) AS ?count) WHERE { ?tx a btcm:Transaction. }

  • transaction in block 400:

PREFIX : <https://raw.githubusercontent.com/franzinc/agraph-examples/master/data/bitcoin/model.ttl#>
SELECT ?txid
WHERE {
?b a :Block.
?b :height “570001”^^xsd:int.
?b :transaction ?tx.
?tx :txid ?txid.
}

  • transactions sending more than 1000 BTC:

PREFIX : <https://raw.githubusercontent.com/franzinc/agraph-examples/master/data/bitcoin/model.ttl#>
SELECT ?tx
WHERE {
?b a :Block.
?b :transaction ?tx.
?tx :output ?out.
?out :amount ?amt.
}
GROUP BY ?tx
HAVING (SUM(?amt) > 100000000000)

  • transactions sending BTC to Pirate Bay’s address:

PREFIX : <https://raw.githubusercontent.com/franzinc/agraph-examples/master/data/bitcoin/model.ttl#>
SELECT ?tx
WHERE {
?tx :output ?out.
?out :lockScript ?s.
FILTER REGEX (?s, “<tpb address>”).
}

 

Back to Blog

Related articles

Frequently Asked Questions

1 How does AllegroGraph's RDF model for Bitcoin data facilitate semantic representation and querying?

The page demonstrates an RDF model for Bitcoin chain data, defined in a Turtle file, which represents entities like blocks and transactions. This allows users to load Bitcoin data into AllegroGraph and query it using SPARQL, enabling semantic analysis of the blockchain.

2 What are the initial steps to set up AllegroGraph and ingest Bitcoin chain data using the provided tool?

To get started, users need an installed and running AllegroGraph triple store and a bitcoind instance. The page provides instructions to clone a GitHub repository, set up a Python virtual environment, install dependencies, and then run a Python script to load Bitcoin data into an AllegroGraph repository.

3 Can the Bitcoin RDF model be customized or extended for specific analytical needs?

The page presents a specific RDF model for Bitcoin chain data in a Turtle file ("model.ttl"). While the example focuses on representing core chain entities, RDF models are inherently extensible, allowing users to modify or expand the schema to suit specific analytical or integration requirements.

4 What types of information can be extracted from the Bitcoin RDF model using SPARQL queries?

Once Bitcoin data is loaded into AllegroGraph using the RDF model, SPARQL queries can be used to extract various information. The page provides examples demonstrating how to query for details such as the number of known blocks within the dataset.