Skip to content

Commit

Permalink
[3/n][gql-performance] add min_tx_sequence_number and max_tx_sequence…
Browse files Browse the repository at this point in the history
…_number to checkpoints table (#18244)

## Description 

Per title - add `min_tx_sequence_number` and `max_tx_sequence_number` to
`checkpoints` table to support queries mapping `cp` to a `tx` range.
This is so that when given a `checkpoint_sequence_number` range, we can
map it to the corresponding `tx_sequence_number` range to facilitate
transactions queries.

The two new columns are nullable, because it is possible for a
checkpoint to not contain any transactions. In practice, we'll typically
have at least a commit prologue txn. The nullable columns do not impact
the queries we intend to run on the table. If through a CTE, a null
value will eventually resolve to FALSE when we attempt to use the min or
max tx sequence number in a query against the transactions and tx lookup
tables. If we make multiple roundtrips, we can stop early if we see that
the range is empty for a checkpoint.

## Test plan 

Existing tests

---

## 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): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:
  • Loading branch information
wlmyng committed Jun 21, 2024
1 parent 7d5fe08 commit d3375ee
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,29 @@
CREATE TABLE checkpoints
(
sequence_number bigint PRIMARY KEY,
checkpoint_digest bytea NOT NULL,
epoch bigint NOT NULL,
sequence_number BIGINT PRIMARY KEY,
checkpoint_digest BYTEA NOT NULL,
epoch BIGINT NOT NULL,
-- total transactions in the network at the end of this checkpoint (including itself)
network_total_transactions bigint NOT NULL,
previous_checkpoint_digest bytea,
network_total_transactions BIGINT NOT NULL,
previous_checkpoint_digest BYTEA,
-- if this checkpoitn is the last checkpoint of an epoch
end_of_epoch boolean NOT NULL,
-- array of TranscationDigest in bytes included in this checkpoint
tx_digests bytea[] NOT NULL,
tx_digests BYTEA[] NOT NULL,
timestamp_ms BIGINT NOT NULL,
total_gas_cost BIGINT NOT NULL,
computation_cost BIGINT NOT NULL,
storage_cost BIGINT NOT NULL,
storage_rebate BIGINT NOT NULL,
non_refundable_storage_fee BIGINT NOT NULL,
-- bcs serialized Vec<CheckpointCommitment> bytes
checkpoint_commitments bytea NOT NULL,
checkpoint_commitments BYTEA NOT NULL,
-- bcs serialized AggregateAuthoritySignature bytes
validator_signature bytea NOT NULL,
validator_signature BYTEA NOT NULL,
-- bcs serialzied EndOfEpochData bytes, if the checkpoint marks end of an epoch
end_of_epoch_data bytea
end_of_epoch_data BYTEA,
min_tx_sequence_number BIGINT,
max_tx_sequence_number BIGINT
);

CREATE INDEX checkpoints_epoch ON checkpoints (epoch, sequence_number);
Expand Down
3 changes: 3 additions & 0 deletions crates/sui-indexer/src/handlers/checkpoint_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,8 @@ where
&checkpoint_summary,
&checkpoint_contents,
successful_tx_num as usize,
db_transactions.first().map(|t| t.tx_sequence_number),
db_transactions.last().map(|t| t.tx_sequence_number),
),
db_transactions,
db_events,
Expand Down Expand Up @@ -397,6 +399,7 @@ where
checkpoint_seq, tx_digest, sender_signed_data.digest()
)));
}

let tx = sender_signed_data.transaction_data();
let events = events
.as_ref()
Expand Down
8 changes: 4 additions & 4 deletions crates/sui-indexer/src/indexer_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -777,26 +777,26 @@ impl<U: R2D2Connection> IndexerReader<U> {
let package = Hex::encode(package.to_vec());
match (module, function) {
(Some(module), Some(function)) => (
"tx_calls".into(),
"tx_calls_fun".into(),
format!(
"package = '\\x{}'::bytea AND module = '{}' AND func = '{}'",
package, module, function
),
),
(Some(module), None) => (
"tx_calls".into(),
"tx_calls_mod".into(),
format!(
"package = '\\x{}'::bytea AND module = '{}'",
package, module
),
),
(None, Some(_)) => {
return Err(IndexerError::InvalidArgumentError(
"Function cannot be present wihtout Module.".into(),
"Function cannot be present without Module.".into(),
));
}
(None, None) => (
"tx_calls".into(),
"tx_calls_pkg".into(),
format!("package = '\\x{}'::bytea", package),
),
}
Expand Down
4 changes: 4 additions & 0 deletions crates/sui-indexer/src/models/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub struct StoredCheckpoint {
pub checkpoint_commitments: Vec<u8>,
pub validator_signature: Vec<u8>,
pub end_of_epoch_data: Option<Vec<u8>>,
pub min_tx_sequence_number: Option<i64>,
pub max_tx_sequence_number: Option<i64>,
}

impl From<&IndexedCheckpoint> for StoredCheckpoint {
Expand Down Expand Up @@ -77,6 +79,8 @@ impl From<&IndexedCheckpoint> for StoredCheckpoint {
.as_ref()
.map(|d| bcs::to_bytes(d).unwrap()),
end_of_epoch: c.end_of_epoch_data.is_some(),
min_tx_sequence_number: c.min_tx_sequence_number.map(|t| t as i64),
max_tx_sequence_number: c.max_tx_sequence_number.map(|t| t as i64),
}
}
}
Expand Down
6 changes: 5 additions & 1 deletion crates/sui-indexer/src/schema/pg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ diesel::table! {
checkpoint_commitments -> Bytea,
validator_signature -> Bytea,
end_of_epoch_data -> Nullable<Bytea>,
min_tx_sequence_number -> Nullable<Int8>,
max_tx_sequence_number -> Nullable<Int8>
}
}

Expand Down Expand Up @@ -323,7 +325,9 @@ macro_rules! for_all_tables {
packages,
transactions,
transactions_partition_0,
tx_calls,
tx_calls_pkg,
tx_calls_mod,
tx_calls_fun,
tx_changed_objects,
tx_digests,
tx_input_objects,
Expand Down
6 changes: 6 additions & 0 deletions crates/sui-indexer/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,17 @@ pub struct IndexedCheckpoint {
pub successful_tx_num: usize,
pub end_of_epoch_data: Option<EndOfEpochData>,
pub end_of_epoch: bool,
pub min_tx_sequence_number: Option<u64>,
pub max_tx_sequence_number: Option<u64>,
}

impl IndexedCheckpoint {
pub fn from_sui_checkpoint(
checkpoint: &sui_types::messages_checkpoint::CertifiedCheckpointSummary,
contents: &sui_types::messages_checkpoint::CheckpointContents,
successful_tx_num: usize,
min_tx_sequence_number: Option<u64>,
max_tx_sequence_number: Option<u64>,
) -> Self {
let total_gas_cost = checkpoint.epoch_rolling_gas_cost_summary.computation_cost as i64
+ checkpoint.epoch_rolling_gas_cost_summary.storage_cost as i64
Expand All @@ -78,6 +82,8 @@ impl IndexedCheckpoint {
timestamp_ms: checkpoint.timestamp_ms,
validator_signature: auth_sig.clone(),
checkpoint_commitments: checkpoint.checkpoint_commitments.clone(),
min_tx_sequence_number,
max_tx_sequence_number,
}
}
}
Expand Down

0 comments on commit d3375ee

Please sign in to comment.