1.0.32 • Published 2 years ago

run-db v1.0.32

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

Run-DB

tests codecov

npm.io

Crawls the blockchain and indexes RUN state.

Using Run-DB, you can self-host the State APIs that Run uses to work well.

Use Run-DB to:

  • Operate a State Server to improve RUN performance by pre-loading jigs
  • Query balances, volume, history, and other information across many users and contracts
  • Blacklist individual transactions and their descendants in your app
  • Create your own local database of transactions your app uses

Requirements

Node 10+

Getting started

  1. Install npm run install
  2. Download a db snapshot: wget https://run.network/run-db-snapshots/main/latest -O run.db (optional)
  3. Run npm run start
  4. Install a trustlist: curl -s https://api.run.network/v1/main/trust | curl -H "Content-Type: application/json" -X POST -d @- http://localhost:8000/trust (optional)

Note: For testnet, you may use test in place of main in the above commands.

Use with your App Server

Setup your server's Run instance as follows:

const client = true
const state = new Run.plugins.RunDB('http://localhost:8000')
const trust = ['state']
const run = new Run({ client, state, trust })

Client mode makes Run-DB the source of truth for your server for all jig information. RUN will not load jigs that are not in your database, and your inventory will only be populated by jig UTXOs known to your database.

Setting trust to 'state' makes Run use your database for its trustlist too. This means you only have to setup trust in one place using:

curl -X POST localhost:8000/trust/<txid>

You may also want to run additional instance of Run-DB in SERVE_ONLY mode. That allows you to have an writer that crawls transactions and puts data into the database, and multiple readers that serve your application servers.

Use with a Browser or Mobile Client

The same approach taken for servers can be used to improve performance of client Run instances. You should expose your Run-DB endpoints on a public or private domain rather than connect to localhost. If your client connections are not authenticated, be sure to only expose the GET endpoints and never the POST or DELETE endpoints, and use HTTPS to prevent MITM attacks.

Configuration

Create a .env file or set the following environment variables before running to configure the DB.

NameDescriptionDefault
APImattercloud, planaria, bitcoin-node, run, or nonemattercloud
MATTERCLOUD_KEYMattercloud API keyundefined
PLANARIA_TOKENPlanaria API keyundefined
ZMQ_URLOnly for bitcoin-node. ZMQ tcp urlnull
RPC_URLOnly for bitcoin-node. bitcoin RPC http urlnull
NETWORKBitcoin network (main or test)main
DBDatabase filerun.db
PORTPort used for the REST serverrandomly generated
WORKERSNumber of threads used to index4
FETCH_LIMITNumber of parallel downloads20
START_HEIGHTBlock height to start indexingblock shortly before sep 2020
TIMEOUTNetwork timeout in milliseconds10000
MEMPOOL_EXPIRATIONSeconds until transactions are removed from the mempool86400
DEFAULT_TRUSTLISTComma-separated values of trusted txidspredefined trustlist
SERVE_ONLYWhether to only serve data and not index transactionsfalse

Connecting with a bitcoin node

During development is useful to connect to a local node. In order to do this you need to provide RUN-db with access to a bitcoin node trough RPC and ZMQ.

export API="bitcoin-node"
export ZMQ_URL="tcp://your-node-uri:port"
export RPC_URL="http://user:password@your-node-uri:port"

The only zmq message is needed is rawtx. ZMQ is only used to get the new transactions in the mempool.

Direct connection with the node is tested in regtest and testnet, but it's not recommeded for production environments in mainnet at the moment.

Endpoints

  • GET /jig/:location - Gets the state for a jig at a particular location
  • GET /berry/:location - Gets the state for a berry at a particular location
  • GET /tx/:txid - Gets the raw transaction hex for an added transaction
  • GET /time/:txid - Gets the block or mempool time of a transaction in seconds since unix epoch
  • GET /spends/:location - Gets the spending txid for an output at a particular location
  • GET /unspent - Gets the locations of all unspent jigs that are trusted. You may optionally pass in the following query params: class to filter by contract origin, address to filter by owner address, pubkey to filter by owner pubkey, scripthash to filter by hash of the owner script, lock to filter by lock class origin.
  • GET /trust/:txid? - Gets whether a particular txid is trusted, or the entire trust list
  • GET /ban/:txid? - Gets whether a particular txid is banned, or the entire ban list
  • GET /status - Prints status information

  • POST /trust/:txid? - Trusts a transaction to execute its code, as well as any untrusted ancestors. To trust multiple transactions at once, you may add an array of txids in the body as application/json.

  • POST /ban/:txid - Bans a transaction from being executed, and unindexes it and its descendents
  • POST /tx/:txid? - Indexes a transaction and any ancestors. You may optionally add the raw hex data for the transaction in the body as text/plain.

  • DELETE /trust/:txid - Removes trust for a transaction, and unindexes it and its descendents

  • DELETE /ban/:txid - Removes a transaction ban, and reindexes it and its descendents
  • DELETE /tx/:txid - Removes a transaction, its descendents, and any connected state

Performing Custom Queries

Run-DB uses SQLite as its underlying database in WAL mode. SQLite and WAL allows multiple connections to the database so long as there is only one writer, which should be Run-DB. Alternatively, forking Run-DB to create new endpoints for your application may be simpler.

Example Queries

For some of these queries, you will need the JSON1 SQLite extension.

Calculate SHUA supply

SELECT SUM(amount) as supply
FROM (
    SELECT
        json_extract(jig.state, '$.props.amount') AS amount
    FROM jig JOIN spends ON jig.location = spends.location
    WHERE spends.spend_txid IS NULL
    AND jig.class = 'ce8629aa37a1777d6aa64d0d33cd739fd4e231dc85cfe2f9368473ab09078b78_o1')

Calculate SHUA token balances by owner

SELECT owner, SUM(amount) as amount
FROM (SELECT
        json_extract(jig.state, '$.props.owner') AS owner,
        json_extract(jig.state, '$.props.amount') AS amount
    FROM jig JOIN spends ON jig.location = spends.location
    WHERE spends.spend_txid IS NULL
    AND jig.class = 'ce8629aa37a1777d6aa64d0d33cd739fd4e231dc85cfe2f9368473ab09078b78_o1')
GROUP BY owner
ORDER BY amount DESC

Get transaction hex

SELECT HEX(bytes) AS hex
FROM tx
WERE txid = 'ce8629aa37a1777d6aa64d0d33cd739fd4e231dc85cfe2f9368473ab09078b78'

Re-execute all transactions

UPDATE tx SET executed = 0; DELETE FROM jig; DELETE FROM berry;

Database Schema

There are currently 8 tables updated by Run-DB.

jig

Stores jig and code states at output locations or destroyed locations.

ColumnTypeDescription
locationTEXTJig or code location
stateTEXTJSON string describing the object state
classTEXTContract origin if this state is a jig
scripthashTEXTHex string of the reversed sha256 of the owner script
lockTEXTLock class origin if this state has a custom lock

tx

Stores all transactions known by Run-DB and their indexing state.

ColumnTypeDescription
txidTEXTHex string for the transaction hash
heightINTEGERBlock height for this transaction, or -1 for mempool, or NULL for unknown
timeINTEGERTransaction or bock time in seconds since the unix epoch
bytesBLOBRaw transaction data, or NULL if not downloaded
has_codeINTEGER1 if this transaction deployed or upgraded code and requires trust, 0 otherwise
executableINTEGER1 if this transaction is a valid RUN transaction, 0 otherwise
executedINTEGER1 if this transaction was executed, even if it failed, 0 otherwise
indexedINTEGER1 if this transaction's jig states were calculated successfully, 0 otherwise

spends

Stores spend information about transaction outputs.

ColumnTypeDescription
locationTEXT\<txid>_o\<output-index> string describing an output
spend_txidTXIDHex txid that spent this output, or NULL if unspent

deps

Stores the transaction needed to load a RUN transaction.

ColumnTypeDescription
upTEXTA transaction ID in hex
downTEXTHex txid for a transaction that depends on up

berry

Stores berry states for third-party protocol data.

ColumnTypeDescription
locationTEXTBerry location without the &hash query param
stateTEXTJSON string describing the object state

trust

Stores the transactions which have been trusted and whose code will be executed.

ColumnTypeDescription
txidTEXTHex string txid
valueINTEGER1 if trusted, 0 if untrusted

ban

Stores the transactions which have been blacklisted.

ColumnTypeDescription
txidTEXTHex string txid
valueINTEGER1 if blacklisted, 0 otherwise

crawl

Stores the crawled block tip height and hash for data in the database.

ColumnTypeDescription
keyTEXT'height' or 'hash'
valueTEXTString value for the key
1.0.32

2 years ago

1.1.0-beta.10

2 years ago

1.1.0-beta.9

2 years ago

1.1.0-beta.8

2 years ago

1.1.0-beta.2

2 years ago

1.1.0-beta.1

2 years ago

1.1.0-beta.6

2 years ago

1.1.0-beta.5

2 years ago

1.1.0-beta.4

2 years ago

1.1.0-beta.3

2 years ago

1.0.31

2 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.22

3 years ago

1.0.23

3 years ago

1.0.21

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.15

3 years ago

1.0.14

3 years ago

1.0.13

3 years ago

1.0.11

3 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago