Skip to content

About

No description, website, or topics provided.

Resources

Security policy

Stars

14 stars

Watchers

9 watching

Forks

Latest commit

 

History

1,817 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

OpenSSF Scorecard CodeQL RSK Logo

Rsk Explorer API

Requisites

  • postgres
  • node: v24+
  • access to JSON/RPC interface of a rskj node >= 2.0.1 with this modules enabled: eth, net, web3, txpool, debug and trace.

Clone the repository

The Prisma schema and database SQL live in a Git submodule (prisma/). You must clone with submodules, otherwise database setup and Prisma Client generation will fail.

git clone --recurse-submodules git@github.com:rsksmart/rsk-explorer-api.git
cd rsk-explorer-api
npm install
npx prisma@6.19.3 generate

If you already cloned without submodules:

git submodule update --init --recursive
npm install
npx prisma@6.19.3 generate

npx prisma@6.19.3 generate is required after the submodule is available (Prisma v6; plain npx prisma generate may resolve to v7 and fail). If @prisma/client was not generated during npm install (common when prisma/schema.prisma was missing), the app will fail at runtime until you run it.

Configuration steps

Section 1: Environment setup

  • create a database 'explorer_db'
  • create the sql tables by running: npx prisma@6.19.3 migrate deploy
  • Install pm2:
    • npm install -g pm2
    • Enable pm2 log rotation: pm2 install pm2-logrotate
    • Enable log-rotation compression: pm2 set pm2-logrotate:compress true
    • For more options:
  • Configure the database connection and the RSK node URL in a .env file at the project root (DATABASE_URL is required by Prisma and the app; RSK_NODE_URL is optional and defaults to http://localhost:4444):
DATABASE_URL=postgres://USER:PASSWORD@HOST:PORT/DATABASE_NAME
RSK_NODE_URL=http://HOST:PORT

Example:

DATABASE_URL=postgres://postgres:12345678@localhost:5432/explorer_db
RSK_NODE_URL=http://localhost:4444
  • The RSK node URL comes from RSK_NODE_URL (see the .env above). Other settings default to src/lib/defaultConfig.js and can be overridden with a config.json at the project root (see config-example.json):
{
  sourceRoutes: { // Nod3Router routes, used as default when source is an array of sources
    subscribe: 0, // delegates subscriptions to the first node
    rsk: 0, // delegates rsk module to the node that handle subscriptions
    trace: 1 // delegates trace_ module to the second node
  },
  api: {
    address: 'localhost',
    port: 3003,
    lastBlocks: 30,
    MIN_LIMIT: 10,
    LIMIT: 50,
    MAX_LIMIT: 500,
    MAX_PAGES: 10,
    allowUserEvents: false,
    exposeDoc: false,
    // All modules are enabled as default
    modules: setAllModules(true),
    delayedFields,
    allowCountQueries: false
  },
  blocks: {
    blocksQueueSize: 10,
    bcTipSize: 120,
    batchRequestSize: 20,
    debug: false,
    ports: [3010], // list of services ports, if the list runs out, the services will try to take the next  ports starting from the last
    address: '127.0.0.1',
    services
  },
  forceSaveBcStats: true,
  enableTxPoolFromApi: true
}
  • RSK_NODE_URL in the environment or the .env file is the RSK node(s) every process connects to: one http or https URL, or a comma-separated list for multiple nodes (each validated when set). It is the only place the node is configured; when it is unset, the explorer logs a warning and uses the built-in default (http://localhost:4444) for local dev.
    • With more than one URL, the nodes are wired through sourceRoutes (see src/lib/defaultConfig.js), so order matters: by default the first node serves subscriptions and the rsk module, and the second serves trace_. This is module routing, not failover.

api

  • address [string] api server bind address
  • port [number] api server port
  • exposeDoc [boolean]: serve rsk-openapi-ui on /doc to render swagger.json
  • allowUserEvents [boolean]: allow contractVerifier

To enable contract verifier module:

The contractVerifier module requires a connection to a rsk-contract-verifier instance. The url must be provided on api section:

  • set api.allowUserEvents to true
  • add the contract verifier url inside api:
api:{
  //... other configs,
  contractVerifier: {
      url: 'ws://localhost:3008'
    },
  //... other configs
}

Section 3: Build process

Run commands:

  • npm install
  • npm run build
  • npx prisma@6.19.3 generate

Start

  • Start block service: npm run start-blocks
  • Start api: npm run start-api

Note: an rsk node must be running at the URL configured by RSK_NODE_URL (or the default http://localhost:4444). Otherwise, the blocks service will crash.

Section 4: Logs

To see block service logs:

  • npm run blocks-logs-raw (production)
  • npm run blocks-logs-pretty (development)

To see api logs:

  • npm run api-logs-raw (production)
  • npm run api-logs-pretty (development)

Section 5: Tools

Get a block:

  • node dist/tools/getBlock.js 4000 (print in console)
  • node dist/tools/getBlock.js 4000 --save (print in console and also store in database)

Get missing segments in database:

  • node dist/tools/missingSegments.js

Components

The logic in charge of indexing the blockchain in the database and maintaining data integrity consists of 4 services:

  • [blocks-checker-service]: ensures integrity of the most recent 120 blocks in database at the moment of running the service
  • [savetip-service]: requests blocks inside the tip size threshold (the top 120 newest, starting from latest block)
  • [live-syncer-service]: requests new blocks to the RSK node, starting from latest block
  • [static-syncer-service]: Stores immutable blocks (starting from latest - 120). To do so, it checks database gaps between already indexed blocks, and requests the missing ones by iterating the gaps

API server: HTTP/WS server

Development

Run api in development mode: npm run dev Run blocks in development mode:

  • npm run build
  • npx prisma@6.19.3 generate
  • npm run blocks-start

Note Before uploading changes, remember to execute npm run build after upgrading version in package.version, so swagger docs compile the version number too.

About

No description, website, or topics provided.

Resources

Security policy

Stars

14 stars

Watchers

9 watching

Forks

Releases

Packages

Used by

Contributors

Languages