Skip to content

Commit

Permalink
[mvr] renaming to sui-mvr-indexer and Dockerfile (#20099)
Browse files Browse the repository at this point in the history
Describe the changes or additions included in this PR.

How did you test the new or updated feature?

---

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol:
- [ ] Nodes (Validators and Full nodes):
- [ ] Indexer:
- [ ] JSON-RPC:
- [ ] GraphQL:
- [ ] CLI:
- [ ] Rust SDK:
- [ ] REST API:
  • Loading branch information
wlmyng committed Nov 12, 2024
1 parent 7e617be commit bd9a23c
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 145 deletions.
75 changes: 75 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ members = [
"crates/sui-move",
"crates/sui-move-build",
"crates/sui-move-lsp",
"crates/sui-mvr-indexer",
"crates/sui-network",
"crates/sui-node",
"crates/sui-open-rpc",
Expand Down Expand Up @@ -646,6 +647,7 @@ sui-metric-checker = { path = "crates/sui-metric-checker" }
sui-move = { path = "crates/sui-move" }
sui-move-build = { path = "crates/sui-move-build" }
sui-move-lsp = { path = "crates/sui-move-lsp" }
sui-mvr-indexer = { path = "crates/sui-mvr-indexer" }
sui-network = { path = "crates/sui-network" }
sui-node = { path = "crates/sui-node" }
sui-open-rpc = { path = "crates/sui-open-rpc" }
Expand Down
4 changes: 2 additions & 2 deletions crates/sui-mvr-indexer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "sui-indexer"
name = "sui-mvr-indexer"
version.workspace = true
authors = ["Mysten Labs <[email protected]>"]
license = "Apache-2.0"
Expand Down Expand Up @@ -83,5 +83,5 @@ ntest.workspace = true
criterion.workspace = true

[[bin]]
name = "sui-indexer"
name = "sui-mvr-indexer"
path = "src/main.rs"
124 changes: 9 additions & 115 deletions crates/sui-mvr-indexer/README.md
Original file line number Diff line number Diff line change
@@ -1,132 +1,26 @@
Sui indexer is an off-fullnode service to serve data from Sui protocol, including both data directly generated from chain and derivative data.
The MVR indexer is a spin-off of the Sui indexer. It has a subset of the full indexer schema, limited to just the tables needed to support MVR. The required tables are `epochs`, `checkpoints`, `packages`, `objects_snapshot`, and `objects_history`. This enables the custom indexer to support the `package_by_name` and `type_by_name` queries on GraphQL.

&#9888; **Warning:** Sui indexer is still experimental and we expect occasional breaking changes that require backfills.

## Architecture
![enhanced_FN](https://user-images.githubusercontent.com/106119108/221022505-a1d873c6-60e2-45f1-b2aa-e50192c4dfbb.png)

## Steps to run locally
### Prerequisites
- install local [Postgres server](https://www.postgresql.org/download/). You can also `brew install postgresql@15` and then add the following to your `~/.zshrc` or `~/.zprofile`, etc:
```sh
export LDFLAGS="-L/opt/homebrew/opt/postgresql@15/lib"
export CPPFLAGS="-I/opt/homebrew/opt/postgresql@15/include"
export PATH="/opt/homebrew/opt/postgresql@15/bin:$PATH"
```
- make sure you have libpq installed: `brew install libpq`, and in your profile, add `export PATH="/opt/homebrew/opt/libpq/bin:$PATH"`. If this doesn't work, try `brew link --force libpq`.

- install Diesel CLI with `cargo install diesel_cli --no-default-features --features postgres`, refer to [Diesel Getting Started guide](https://diesel.rs/guides/getting-started) for more details
- [optional but handy] Postgres client like [Postico](https://eggerapps.at/postico2/), for local check, query execution etc.

### Start the Postgres Service
# Running this indexer
## Start the Postgres Service

Postgres must run as a service in the background for other tools to communicate with. If it was installed using homebrew, it can be started as a service with:

``` sh
brew services start postgresql@version
```

### Local Development(Recommended)

See the [docs](https://docs.sui.io/guides/developer/getting-started/local-network) for detailed information. Below is a quick start guide:

Start a local network using the `sui` binary:
```sh
cargo run --bin sui -- start --with-faucet --force-regenesis
```

If you want to run a local network with the indexer enabled (note that `libpq` is required), you can run the following command after following the steps in the next section to set up an indexer DB:
```sh
cargo run --bin sui -- start --with-faucet --force-regenesis --with-indexer --pg-port 5432 --pg-db-name sui_indexer_v2
```

### Running standalone indexer
1. DB setup, under `sui/crates/sui-indexer` run:
```sh
# an example DATABASE_URL is "postgres://postgres:postgres@localhost/exampledb"
diesel setup --database-url="<DATABASE_URL>"
diesel database reset --database-url="<DATABASE_URL>"
```
Note that you need an existing database for this to work. Using the DATABASE_URL example in the comment of the previous code, replace `exampledb` with the name of your database.

2. Checkout to your target branch

For example, if you want to be on the DevNet branch
```sh
git fetch upstream devnet && git reset --hard upstream/devnet
```
3. Start indexer binary, under `sui/crates/sui-indexer` run:
- run indexer as a writer, which pulls data from fullnode and writes data to DB
```sh
# Change the RPC_CLIENT_URL to http://0.0.0.0:9000 to run indexer against local validator & fullnode
cargo run --bin sui-indexer -- --db-url "<DATABASE_URL>" --rpc-client-url "https://fullnode.devnet.sui.io:443" --fullnode-sync-worker --reset-db
```
- run indexer as a reader, which is a JSON RPC server with the [interface](https://docs.sui.io/sui-api-ref#suix_getallbalances)
```
cargo run --bin sui-indexer -- --db-url "<DATABASE_URL>" --rpc-client-url "https://fullnode.devnet.sui.io:443" --rpc-server-worker
```
More flags info can be found in this [file](src/main.rs#L41).

### DB reset
## DB reset
When making db-related changes, you may find yourself having to run migrations and reset dbs often. The commands below are how you can invoke these actions.
```sh
cargo run --bin sui-indexer -- --database-url "<DATABASE_URL>" reset-database --force
```

## Steps to run locally (TiDB)

### Prerequisites

1. Install TiDB

``` sh
curl --proto '=https' --tlsv1.2 -sSf https://tiup-mirrors.pingcap.com/install.sh | sh
```

2. Install a compatible version of MySQL (At the time of writing, this is MySQL 8.0 -- note that 8.3 is incompatible).

``` sh
brew install [email protected]
```

3. Install a version of `diesel_cli` that supports MySQL (and probably also Postgres). This version of the CLI needs to be built against the version of MySQL that was installed in the previous step (compatible with the local installation of TiDB, 8.0.37 at time of writing).

``` sh
MYSQLCLIENT_LIB_DIR=/opt/homebrew/Cellar/[email protected]/8.0.37/lib/ cargo install diesel_cli --no-default-features --features postgres --features mysql --force
```

### Run the indexer

1.Run TiDB

```sh
tiup playground
```

2.Verify tidb is running by connecting to it using the mysql client, create database `test`

```sh
mysql --comments --host 127.0.0.1 --port 4000 -u root
create database test;
cargo run --bin sui-mvr-indexer -- --database-url "<DATABASE_URL>" reset-database --force
```

3.DB setup, under `sui/crates/sui-indexer` run:

```sh
# an example DATABASE_URL is "mysql://root:[email protected]:4000/test"
diesel setup --database-url="<DATABASE_URL>" --migration-dir='migrations/mysql'
diesel database reset --database-url="<DATABASE_URL>" --migration-dir='migrations/mysql'
```

Note that you need an existing database for this to work. Using the DATABASE_URL example in the comment of the previous code, replace `test` with the name of your database.
4. Run indexer as a writer, which pulls data from fullnode and writes data to DB

```sh
# Change the RPC_CLIENT_URL to http://0.0.0.0:9000 to run indexer against local validator & fullnode
cargo run --bin sui-indexer --features mysql-feature --no-default-features -- --db-url "<DATABASE_URL>" --rpc-client-url "https://fullnode.devnet.sui.io:443" --fullnode-sync-worker --reset-db
## Start the indexer
```SH
cargo run --bin sui-mvr-indexer -- --db-url "<DATABASE_URL>" indexer --rpc-client-url "https://fullnode.devnet.sui.io:443" --remote-store-url http://lax-suifn-t99eb.devnet.sui.io:9000/rest
```

### Extending the indexer
## Migrations

To add a new table, run `diesel migration generate your_table_name`, and modify the newly created `up.sql` and `down.sql` files.

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0

INDEXER=${INDEXER:-"sui-indexer"}
INDEXER=${INDEXER:-"sui-mvr-indexer"}
DB=${DB:-"postgres://postgres:postgrespw@localhost:5432/postgres"}
"$INDEXER" --database-url "$DB" run-back-fill "$1" "$2" sql "UPDATE events SET sender = CASE WHEN cardinality(senders) > 0 THEN senders[1] ELSE NULL END" checkpoint_sequence_number
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0

INDEXER=${INDEXER:-"sui-indexer"}
INDEXER=${INDEXER:-"sui-mvr-indexer"}
DB=${DB:-"postgres://postgres:postgrespw@localhost:5432/postgres"}
"$INDEXER" --database-url "$DB" run-back-fill "$1" "$2" sql "INSERT INTO full_objects_history (object_id, object_version, serialized_object) SELECT object_id, object_version, serialized_object FROM objects_history" checkpoint_sequence_number
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0

INDEXER=${INDEXER:-"sui-indexer"}
INDEXER=${INDEXER:-"sui-mvr-indexer"}
DB=${DB:-"postgres://postgres:postgrespw@localhost:5432/postgres"}
"$INDEXER" --database-url "$DB" run-back-fill "$1" "$2" sql "INSERT INTO tx_affected_addresses SELECT tx_sequence_number, sender AS affected, sender FROM tx_senders" tx_sequence_number
"$INDEXER" --database-url "$DB" run-back-fill "$1" "$2" sql "INSERT INTO tx_affected_addresses SELECT tx_sequence_number, recipient AS affected, sender FROM tx_recipients" tx_sequence_number
18 changes: 9 additions & 9 deletions crates/sui-mvr-indexer/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,24 @@
// SPDX-License-Identifier: Apache-2.0

use clap::Parser;
use sui_indexer::backfill::backfill_runner::BackfillRunner;
use sui_indexer::config::{Command, UploadOptions};
use sui_indexer::database::ConnectionPool;
use sui_indexer::db::{
use sui_mvr_indexer::backfill::backfill_runner::BackfillRunner;
use sui_mvr_indexer::config::{Command, UploadOptions};
use sui_mvr_indexer::database::ConnectionPool;
use sui_mvr_indexer::db::{
check_db_migration_consistency, check_prunable_tables_valid, reset_database, run_migrations,
};
use sui_indexer::indexer::Indexer;
use sui_indexer::metrics::{
use sui_mvr_indexer::indexer::Indexer;
use sui_mvr_indexer::metrics::{
spawn_connection_pool_metric_collector, start_prometheus_server, IndexerMetrics,
};
use sui_indexer::restorer::formal_snapshot::IndexerFormalSnapshotRestorer;
use sui_indexer::store::PgIndexerStore;
use sui_mvr_indexer::restorer::formal_snapshot::IndexerFormalSnapshotRestorer;
use sui_mvr_indexer::store::PgIndexerStore;
use tokio_util::sync::CancellationToken;
use tracing::warn;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
let opts = sui_indexer::config::IndexerConfig::parse();
let opts = sui_mvr_indexer::config::IndexerConfig::parse();

// NOTE: this is to print out tracing like info, warn & error.
let _guard = telemetry_subscribers::TelemetryConfig::new()
Expand Down
6 changes: 3 additions & 3 deletions crates/sui-mvr-indexer/src/schema.patch
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
diff --git a/crates/sui-indexer/src/schema.rs b/crates/sui-indexer/src/schema.rs
--- a/crates/sui-indexer/src/schema.rs
+++ b/crates/sui-indexer/src/schema.rs
diff --git a/crates/sui-mvr-indexer/src/schema.rs b/crates/sui-mvr-indexer/src/schema.rs
--- a/crates/sui-mvr-indexer/src/schema.rs
+++ b/crates/sui-mvr-indexer/src/schema.rs
@@ -1 +1,3 @@
+// Copyright (c) Mysten Labs, Inc.
+// SPDX-License-Identifier: Apache-2.0
Expand Down
20 changes: 10 additions & 10 deletions crates/sui-mvr-indexer/tests/ingestion_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,19 @@ use diesel::ExpressionMethods;
use diesel::QueryDsl;
use diesel_async::RunQueryDsl;
use simulacrum::Simulacrum;
use sui_indexer::errors::IndexerError;
use sui_indexer::handlers::TransactionObjectChangesToCommit;
use sui_indexer::models::{
use sui_mvr_indexer::errors::IndexerError;
use sui_mvr_indexer::handlers::TransactionObjectChangesToCommit;
use sui_mvr_indexer::models::{
checkpoints::StoredCheckpoint, objects::StoredObject, objects::StoredObjectSnapshot,
transactions::StoredTransaction,
};
use sui_indexer::schema::{checkpoints, objects, objects_snapshot, transactions};
use sui_indexer::store::indexer_store::IndexerStore;
use sui_indexer::test_utils::{set_up, wait_for_checkpoint, wait_for_objects_snapshot};
use sui_indexer::types::EventIndex;
use sui_indexer::types::IndexedDeletedObject;
use sui_indexer::types::IndexedObject;
use sui_indexer::types::TxIndex;
use sui_mvr_indexer::schema::{checkpoints, objects, objects_snapshot, transactions};
use sui_mvr_indexer::store::indexer_store::IndexerStore;
use sui_mvr_indexer::test_utils::{set_up, wait_for_checkpoint, wait_for_objects_snapshot};
use sui_mvr_indexer::types::EventIndex;
use sui_mvr_indexer::types::IndexedDeletedObject;
use sui_mvr_indexer::types::IndexedObject;
use sui_mvr_indexer::types::TxIndex;
use sui_types::base_types::SuiAddress;
use sui_types::effects::TransactionEffectsAPI;
use sui_types::gas_coin::GasCoin;
Expand Down
6 changes: 3 additions & 3 deletions crates/sui-mvr-indexer/tests/read_api_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
use jsonrpsee::core::RpcResult;
use simulacrum::Simulacrum;
use std::sync::Arc;
use sui_indexer::apis::read_api::ReadApi;
use sui_indexer::indexer_reader::IndexerReader;
use sui_indexer::test_utils::{set_up, wait_for_checkpoint};
use sui_json_rpc_api::ReadApiServer;
use sui_mvr_indexer::apis::read_api::ReadApi;
use sui_mvr_indexer::indexer_reader::IndexerReader;
use sui_mvr_indexer::test_utils::{set_up, wait_for_checkpoint};
use tempfile::tempdir;

#[tokio::test]
Expand Down
Loading

0 comments on commit bd9a23c

Please sign in to comment.