All Projects → hirosystems → stacks-blockchain-api

hirosystems / stacks-blockchain-api

Licence: GPL-3.0 license
API for the Stacks 2.0 blockchain

Programming Languages

typescript
32286 projects

Projects that are alternatives of or similar to stacks-blockchain-api

clarity-smart-contracts
Collection of Smart Contract for Stacks Blockchain in Clarity
Stars: ✭ 61 (-47.86%)
Mutual labels:  stacks, blockstack
micro-stacks
Tiny libraries for building the next generation of Stacks apps.
Stars: ✭ 56 (-52.14%)
Mutual labels:  stacks, blockstack
Fullstack-projects-frontend-with-react-and-backend-with-various-stacks
* W.I.P *🛠 Full-on full stack front end and a bit of back end web development challenge. Challenging every day to learn new stuffs about react & its in-depth features and also to explore the taste of various stacks.
Stars: ✭ 100 (-14.53%)
Mutual labels:  stacks
clarity-lsp
Language Server Protocol implementation for Clarity (including VS code extension).
Stars: ✭ 47 (-59.83%)
Mutual labels:  stacks
DSA--GeeksForGeeks
DSA course solutions in C++ Jump to below directly for more problems
Stars: ✭ 47 (-59.83%)
Mutual labels:  stacks
Awesome Stacks
A curated list of tech stacks for building different applications & features
Stars: ✭ 2,631 (+2148.72%)
Mutual labels:  stacks
envelop-web
🌐 Envelop web app
Stars: ✭ 54 (-53.85%)
Mutual labels:  blockstack
DSA
Data Structures and Algorithms
Stars: ✭ 13 (-88.89%)
Mutual labels:  stacks
designs
Blockstack designs
Stars: ✭ 26 (-77.78%)
Mutual labels:  blockstack
ui
The Blockstack design system implemented with react, styled-components, and styled-system.
Stars: ✭ 34 (-70.94%)
Mutual labels:  blockstack
blockstack.js-old
The Blockstack JS library for identity and authentication
Stars: ✭ 20 (-82.91%)
Mutual labels:  blockstack
block-photos
A photos app built with Ionic and Blockstack as backend.
Stars: ✭ 24 (-79.49%)
Mutual labels:  blockstack
envelop-android
🤖 Envelop Android app
Stars: ✭ 39 (-66.67%)
Mutual labels:  blockstack
blockstack-browser
The Blockstack Browser
Stars: ✭ 1,130 (+865.81%)
Mutual labels:  blockstack
py-algorithms
Algorithms and Data Structures, solutions to common CS problems.
Stars: ✭ 26 (-77.78%)
Mutual labels:  stacks
AStack
The Missing SwiftUI Adaptive and Accessible Stacks Library.
Stars: ✭ 110 (-5.98%)
Mutual labels:  stacks
kool
From local development to the cloud: development workflow made easy.
Stars: ✭ 588 (+402.56%)
Mutual labels:  stacks
algoexpert
AlgoExpert is an online platform that helps software engineers to prepare for coding and technical interviews.
Stars: ✭ 8 (-93.16%)
Mutual labels:  stacks
docs
Unleash Bitcoin's full potential with decentralized apps and smart contracts. The documentation covers key aspects of the Stacks network and technology and provides tutorials and other helpful content for developers.
Stars: ✭ 134 (+14.53%)
Mutual labels:  stacks
rpncalc
RPNCalc: The Command Line Reverse Polish Notation (RPN) Calculator
Stars: ✭ 19 (-83.76%)
Mutual labels:  stacks

@hirosystems/stacks-blockchain-api

CI GitHub Releases Docker Pulls NPM client package

Quick start

A self-contained Docker image is provided which starts a Stacks 2.05 blockchain and API instance.

Ensure Docker is installed, then run the command:

docker run -p 3999:3999 hirosystems/stacks-blockchain-api-standalone

Similarly, a "mocknet" instance can be started. This runs a local node, isolated from the testnet/mainnet:

docker run -p 3999:3999 -e STACKS_NETWORK=mocknet hirosystems/stacks-blockchain-api-standalone

Once the blockchain has synced with network, the API will be available at: http://localhost:3999

Development quick start

First, ensure Docker is installed on your machine.

Clone repo and install dependencies with npm install.

Run npm run dev:integrated.

This command will concurrently start the API server app and the service dependencies.

Check to see if the server started successfully by visiting http://localhost:3999/extended/v1/status

Local Development

Setup Services

Then run npm run devenv:deploy which uses docker-compose to deploy the service dependencies (e.g. PostgreSQL, Stacks core node, etc).

Running the server

To run the server in 'watch' mode (restart for every code change), run npm run dev:watch. You'll have a server on port 3999.

Architecture

API architecture!

See overview.md for architecture details.

Deployment

For optimal performance, we recommend running the API database on PostgreSQL version 14 or newer.

Upgrading

If upgrading the API to a new major version (e.g. 3.0.0 to 4.0.0) then the Postgres database from the previous version will not be compatible and the process will fail to start.

Event Replay must be used when upgrading major versions. Follow the event replay instructions below. Failure to do so will require wiping both the Stacks Blockchain chainstate data and the API Postgres database, and re-syncing from scratch.

API Run Modes

The API supports a series of run modes, each accomodating different use cases for scaling and data access by toggling architecture components on or off depending on its objective.

Default mode (Read-write)

The default mode runs with all components enabled. It consumes events from a Stacks node, writes them to a postgres database, and serves API endpoints.

Write-only mode

During Write-only mode, the API only runs the Stacks node events server to populate the postgres database but it does not serve any API endpoints.

This mode is very useful when you need to consume blockchain data from the postgres database directly and you're not interested in taking on the overhead of running an API web server.

For write-only mode, set the environment variable STACKS_API_MODE=writeonly.

Read-only mode

During Read-only mode, the API runs without an internal event server that listens to events from a Stacks node. The API only reads data from the connected postgres database when building endpoint responses. In order to keep serving updated blockchain data, this mode requires the presence of another API instance that keeps writing stacks-node events to the same database.

This mode is very useful when building an environment that load-balances incoming HTTP requests between multiple API instances that can be scaled up and down very quickly. Read-only instances support websockets and socket.io clients.

For read-only mode, set the environment variable STACKS_API_MODE=readonly.

Offline mode

In Offline mode app runs without a stacks-node or postgres connection. In this mode, only the given rosetta endpoints are supported: https://www.rosetta-api.org/docs/node_deployment.html#offline-mode-endpoints.

For running offline mode set an environment variable STACKS_API_MODE=offline

Event Replay

The stacks-node is only able to emit events live as they happen. This poses a problem in the scenario where the stacks-blockchain-api needs to be upgraded and its database cannot be migrated to a new schema. One way to handle this upgrade is to wipe the stacks-blockchain-api's database and stacks-node working directory, and re-sync from scratch.

Alternatively, an event-replay feature is available where the API records the HTTP POST requests from the stacks-node event emitter, then streams these events back to itself. Essentially simulating a wipe & full re-sync, but much quicker.

The feature can be used via program args. For example, if there are breaking changes in the API's sql schema, like adding a new column which requires event's to be re-played, the following steps could be ran:

Event Replay Instructions

V1 BNS Data

Optional but recommended - If you want the V1 BNS data, there are going to be a few extra steps:

  1. Download BNS data:

    curl -L https://storage.googleapis.com/blockstack-v1-migration-data/export-data.tar.gz -o /stacks-node/bns/export-data.tar.gz
  2. Extract it:

    tar -xzvf ./bns/export-data.tar.gz -C /stacks-node/bns/
  3. Each file in ./bns will have a corresponding sha256 value. To Verify, run a script like the following to check the sha256sum:

    for file in `ls /stacks-node/bns/* | grep -v sha256 | grep -v .tar.gz`; do
        if [ $(sha256sum $file | awk {'print $1'}) == $(cat ${file}.sha256 ) ]; then
            echo "sha256 Matched $file"
        else
            echo "sha256 Mismatch $file"
        fi
    done
  4. Set the data's location as the value of BNS_IMPORT_DIR in your .env file.

Export and Import

  1. Ensure the API process is not running. When stopping the API, let the process exit gracefully so that any in-progress SQL writes can finish.

  2. Export event data to disk with the export-events command:

    node ./lib/index.js export-events --file /tmp/stacks-node-events.tsv
  3. Update to the new stacks-blockchain-api version.

  4. Perform the event playback using the import-events command:

    WARNING: This will drop all tables from the configured Postgres database, including any tables not automatically added by the API.

    node ./lib/index.js import-events --file /tmp/stacks-node-events.tsv --wipe-db --force

    This command has two modes of operation, specified by the --mode option:

    • archival (default): The process will import and ingest all blockchain events that have happened since the first block.
    • pruned: The import process will ignore some prunable events (mempool, microblocks) until the import block height has reached chain tip - 256 blocks. This saves a considerable amount of time during import, but sacrifices some historical data. You can use this mode if you're mostly interested in running an API that prioritizes real time information.

Client library

You can use the Stacks Blockchain API Client library if you require a way to call the API via JavaScript or receive real-time updates via Websockets or Socket.io. Learn more here.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].