Skip to content

Commit

Permalink
[Indexer] Index Package original ID, version, cp sequence number (#17726
Browse files Browse the repository at this point in the history
)

## Description

Recreating #17690 which was accidentally closed.

Adding data to the `packages` table, to support the following GraphQL
queries:

```graphql
type Query {
  # Fetch all packages created strictly `afterCheckpoint` and strictly
  # before `beforeCheckpoint`.
  packages(
    afterCheckpoint: Int,
    beforeCheckpoint: Int,
    first: Int,
    after: String,
    last: Int,
    before: String,
  ): MovePackageConnection

  # Fetch all packages in the same family as the package at `address`
  # with versions strictly after `afterVersion` and strictly before 
  # `beforeVersion`.
  packageVersions(
    address: SuiAddress!,
    afterVersion: Int,
    beforeVersion: Int,
    first: Int,
    after: String,
    last: Int,
    before: String,
  ): MovePackageConnection

  # Fetch a package by its address, and optionally supplying a
  # version. If the version is supplied, returns the package whose
  # Original ID matches the Original ID of the package at `address`,
  # but whose version is `version`, otherwise just fetches the package
  # directly.
  package(address: SuiAddress!, version: Int): MovePackage
}

type MovePackage {
  # Return the package whose Original ID matches this package's but
  # whose version matches `version`. If `version` is not supplied,
  # defaults to the latest version.
  atVersion(version: Int): MovePackage

  # Fetch all packages in the same family as this package, with
  # versions strictly after `afterVersion` and strictly before
  # `beforeVersion`.
  versions(
    afterVersion: Int,
    beforeVersion: Int,
    first: Int,
    after: String,
    last: Int,
    before: String,
  ): MovePackageConnection
}
```

These queries are important for writing tools that perform whole-chain
package analyses, and also for the .move Registry.

## Test plan

Make sure nothing is broken:

```
sui$ cargo nextest run -p sui-indexer
```

Tests of new features will be included in a stacked change that uses
these tables and indices in GraphQL.

## Stack

- #17686 
- #17687 
- #17688 
- #17689 
- #17691
- #17694 
- #17695 
- #17542  

---

## Release notes

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): 
- [x] Indexer: Adds the following fields: `packages.original_id`,
`packages.package_version`, `packages.checkpoint_sequence_number` to
support queries about package upgrades.
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
  • Loading branch information
amnn authored and emmazzz committed Aug 2, 2024
1 parent 634790a commit 96b9f13
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
CREATE TABLE packages
(
package_id blob NOT NULL,
package_id BLOB NOT NULL,
original_id BLOB NOT NULL,
package_version BIGINT NOT NULL,
-- bcs serialized MovePackage
move_package MEDIUMBLOB NOT NULL,
CONSTRAINT packages_pk PRIMARY KEY (package_id(255))
move_package MEDIUMBLOB NOT NULL,
checkpoint_sequence_number BIGINT NOT NULL,
CONSTRAINT packages_pk PRIMARY KEY (package_id(32), original_id(32), package_version),
CONSTRAINT packages_unique_package_id UNIQUE (package_id(32))
);

CREATE INDEX packages_cp_id_version ON packages (checkpoint_sequence_number, original_id(32), package_version);
CREATE INDEX packages_id_version_cp ON packages (original_id(32), package_version, checkpoint_sequence_number);
14 changes: 11 additions & 3 deletions crates/sui-indexer/migrations/pg/2023-08-19-060729_packages/up.sql
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
CREATE TABLE packages
CREATE TABLE packages
(
package_id bytea PRIMARY KEY,
package_id bytea NOT NULL,
original_id bytea NOT NULL,
package_version bigint NOT NULL,
-- bcs serialized MovePackage
move_package bytea NOT NULL
move_package bytea NOT NULL,
checkpoint_sequence_number bigint NOT NULL,
CONSTRAINT packages_pkey PRIMARY KEY (package_id, original_id, package_version),
CONSTRAINT packages_unique_package_id UNIQUE (package_id)
);

CREATE INDEX packages_cp_id_version ON packages (checkpoint_sequence_number, original_id, package_version);
CREATE INDEX packages_id_version_cp ON packages (original_id, package_version, checkpoint_sequence_number);
6 changes: 6 additions & 0 deletions crates/sui-indexer/src/models/packages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,20 @@ use diesel::prelude::*;
#[diesel(table_name = packages, primary_key(package_id))]
pub struct StoredPackage {
pub package_id: Vec<u8>,
pub original_id: Vec<u8>,
pub package_version: i64,
pub move_package: Vec<u8>,
pub checkpoint_sequence_number: i64,
}

impl From<IndexedPackage> for StoredPackage {
fn from(p: IndexedPackage) -> Self {
Self {
package_id: p.package_id.to_vec(),
original_id: p.move_package.original_package_id().to_vec(),
package_version: p.move_package.version().value() as i64,
move_package: bcs::to_bytes(&p.move_package).unwrap(),
checkpoint_sequence_number: p.checkpoint_sequence_number as i64,
}
}
}
3 changes: 3 additions & 0 deletions crates/sui-indexer/src/schema/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ diesel::table! {
diesel::table! {
packages (package_id) {
package_id -> Blob,
original_id -> Blob,
package_version -> Bigint,
move_package -> Mediumblob,
checkpoint_sequence_number -> Bigint,
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/sui-indexer/src/schema/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,10 @@ diesel::table! {
diesel::table! {
packages (package_id) {
package_id -> Bytea,
original_id -> Bytea,
package_version -> Int8,
move_package -> Bytea,
checkpoint_sequence_number -> Int8,
}
}

Expand Down

0 comments on commit 96b9f13

Please sign in to comment.