Skip to content

Commit

Permalink
add sui-field-count for sui-indexer-alt (#20126)
Browse files Browse the repository at this point in the history
## Description 

title, so that we can replace hard-coded field count in pipelines.
a crate did very similar things but it's archived, so prob better to
have our own impl, also b/c the functionality is relatively simple
https://github.com/discosultan/field-count

## Test plan 

added tests in models/

---

## 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:
- [ ] REST API:
  • Loading branch information
gegaowp authored Oct 31, 2024
1 parent c92dd1b commit ef0d78c
Show file tree
Hide file tree
Showing 14 changed files with 196 additions and 11 deletions.
21 changes: 21 additions & 0 deletions Cargo.lock

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

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ members = [
"crates/sui-e2e-tests",
"crates/sui-enum-compat-util",
"crates/sui-faucet",
"crates/sui-field-count",
"crates/sui-field-count-derive",
"crates/sui-field-count-main",
"crates/sui-framework",
"crates/sui-framework-snapshot",
"crates/sui-framework-tests",
Expand Down Expand Up @@ -631,6 +634,9 @@ sui-data-ingestion-core = { path = "crates/sui-data-ingestion-core" }
sui-e2e-tests = { path = "crates/sui-e2e-tests" }
sui-enum-compat-util = { path = "crates/sui-enum-compat-util" }
sui-faucet = { path = "crates/sui-faucet" }
sui-field-count = { path = "crates/sui-field-count" }
sui-field-count-main = { path = "crates/sui-field-count-main" }
sui-field-count-derive = { path = "crates/sui-field-count-derive" }
sui-framework = { path = "crates/sui-framework" }
sui-framework-snapshot = { path = "crates/sui-framework-snapshot" }
sui-framework-tests = { path = "crates/sui-framework-tests" }
Expand Down
14 changes: 14 additions & 0 deletions crates/sui-field-count-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "sui-field-count-derive"
version.workspace = true
authors = ["Mysten Labs <[email protected]>"]
license = "Apache-2.0"
publish = false
edition = "2021"

[lib]
proc-macro = true

[dependencies]
syn.workspace = true
quote.workspace = true
29 changes: 29 additions & 0 deletions crates/sui-field-count-derive/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use proc_macro::TokenStream;
use quote::quote;
use syn::{parse_macro_input, DeriveInput};

#[proc_macro_derive(FieldCount)]
pub fn field_count_derive(input: TokenStream) -> TokenStream {
let input = parse_macro_input!(input as DeriveInput);
let name = input.ident;
let generics = input.generics;
let (impl_generics, ty_generics, where_clause) = generics.split_for_impl();

let fields_count = if let syn::Data::Struct(data_struct) = input.data {
data_struct.fields.len()
} else {
panic!("FieldCount can only be derived for structs");
};

let expanded = quote! {
impl #impl_generics FieldCount for #name #ty_generics #where_clause {
fn field_count() -> usize {
#fields_count
}
}
};

TokenStream::from(expanded)
}
7 changes: 7 additions & 0 deletions crates/sui-field-count-main/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "sui-field-count-main"
version.workspace = true
authors = ["Mysten Labs <[email protected]>"]
license = "Apache-2.0"
publish = false
edition = "2021"
6 changes: 6 additions & 0 deletions crates/sui-field-count-main/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

pub trait FieldCount {
fn field_count() -> usize;
}
11 changes: 11 additions & 0 deletions crates/sui-field-count/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "sui-field-count"
version.workspace = true
authors = ["Mysten Labs <[email protected]>"]
license = "Apache-2.0"
publish = false
edition = "2021"

[dependencies]
sui-field-count-derive.workspace = true
sui-field-count-main.workspace = true
5 changes: 5 additions & 0 deletions crates/sui-field-count/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

pub use sui_field_count_derive::*;
pub use sui_field_count_main::*;
1 change: 1 addition & 0 deletions crates/sui-indexer-alt/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ url.workspace = true
diesel_migrations.workspace = true

mysten-metrics.workspace = true
sui-field-count.workspace = true
sui-storage.workspace = true
sui-types.workspace = true

Expand Down
13 changes: 12 additions & 1 deletion crates/sui-indexer-alt/src/models/checkpoints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

use crate::schema::kv_checkpoints;
use diesel::prelude::*;
use sui_field_count::FieldCount;

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = kv_checkpoints)]
pub struct StoredCheckpoint {
pub sequence_number: i64,
Expand All @@ -13,3 +14,13 @@ pub struct StoredCheckpoint {
/// BCS serialized CheckpointContents
pub checkpoint_contents: Vec<u8>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_stored_checkpoint_field_count() {
assert_eq!(StoredCheckpoint::field_count(), 3);
}
}
20 changes: 18 additions & 2 deletions crates/sui-indexer-alt/src/models/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@

use crate::schema::{ev_emit_mod, ev_struct_inst};
use diesel::prelude::*;
use sui_field_count::FieldCount;

#[derive(Insertable, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Insertable, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, FieldCount)]
#[diesel(table_name = ev_emit_mod)]
pub struct StoredEvEmitMod {
pub package: Vec<u8>,
Expand All @@ -13,7 +14,7 @@ pub struct StoredEvEmitMod {
pub sender: Vec<u8>,
}

#[derive(Insertable, Debug, Clone, Eq, PartialEq, Ord, PartialOrd)]
#[derive(Insertable, Debug, Clone, Eq, PartialEq, Ord, PartialOrd, FieldCount)]
#[diesel(table_name = ev_struct_inst)]
pub struct StoredEvStructInst {
pub package: Vec<u8>,
Expand All @@ -23,3 +24,18 @@ pub struct StoredEvStructInst {
pub tx_sequence_number: i64,
pub sender: Vec<u8>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_stored_event_field_count() {
assert_eq!(StoredEvEmitMod::field_count(), 4);
}

#[test]
fn test_stored_struct_inst_field_count() {
assert_eq!(StoredEvStructInst::field_count(), 6);
}
}
27 changes: 24 additions & 3 deletions crates/sui-indexer-alt/src/models/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ use diesel::{
backend::Backend, deserialize, expression::AsExpression, prelude::*, serialize,
sql_types::SmallInt, FromSqlRow,
};
use sui_field_count::FieldCount;
use sui_types::base_types::ObjectID;

use crate::schema::{kv_objects, sum_coin_balances, sum_obj_types};

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = kv_objects, primary_key(object_id, object_version))]
pub struct StoredObject {
pub object_id: Vec<u8>,
Expand Down Expand Up @@ -37,7 +38,7 @@ pub enum StoredOwnerKind {
Shared = 3,
}

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = sum_coin_balances, primary_key(object_id))]
pub struct StoredSumCoinBalance {
pub object_id: Vec<u8>,
Expand All @@ -47,7 +48,7 @@ pub struct StoredSumCoinBalance {
pub coin_balance: i64,
}

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = sum_obj_types, primary_key(object_id))]
pub struct StoredSumObjType {
pub object_id: Vec<u8>,
Expand Down Expand Up @@ -88,3 +89,23 @@ where
})
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_stored_owner_kind_field_count() {
assert_eq!(StoredObject::field_count(), 3);
}

#[test]
fn test_stored_sum_coin_balance_field_count() {
assert_eq!(StoredSumCoinBalance::field_count(), 5);
}

#[test]
fn test_stored_sum_obj_type_field_count() {
assert_eq!(StoredSumObjType::field_count(), 8);
}
}
27 changes: 24 additions & 3 deletions crates/sui-indexer-alt/src/models/transactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::schema::{kv_transactions, tx_affected_objects, tx_balance_changes};
use diesel::prelude::*;
use serde::{Deserialize, Serialize};
use sui_field_count::FieldCount;
use sui_types::object::Owner;

#[derive(Debug, Clone, Serialize, Deserialize)]
Expand All @@ -21,7 +22,7 @@ pub enum BalanceChange {
},
}

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = kv_transactions)]
pub struct StoredTransaction {
pub tx_digest: Vec<u8>,
Expand All @@ -32,17 +33,37 @@ pub struct StoredTransaction {
pub events: Vec<u8>,
}

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = tx_affected_objects)]
pub struct StoredTxAffectedObject {
pub tx_sequence_number: i64,
pub affected: Vec<u8>,
pub sender: Vec<u8>,
}

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = tx_balance_changes)]
pub struct StoredTxBalanceChange {
pub tx_sequence_number: i64,
pub balance_changes: Vec<u8>,
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_stored_transaction_field_count() {
assert_eq!(StoredTransaction::field_count(), 6);
}

#[test]
fn test_stored_tx_affected_object_field_count() {
assert_eq!(StoredTxAffectedObject::field_count(), 3);
}

#[test]
fn test_stored_tx_balance_change_field_count() {
assert_eq!(StoredTxBalanceChange::field_count(), 2);
}
}
20 changes: 18 additions & 2 deletions crates/sui-indexer-alt/src/models/watermarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::{borrow::Cow, cmp};
use crate::{db::Connection, schema::watermarks};
use diesel::prelude::*;
use diesel_async::RunQueryDsl;
use sui_field_count::FieldCount;

#[derive(Insertable, Debug, Clone)]
#[derive(Insertable, Debug, Clone, FieldCount)]
#[diesel(table_name = watermarks)]
pub struct StoredWatermark {
pub pipeline: String,
Expand All @@ -21,7 +22,7 @@ pub struct StoredWatermark {
}

/// Fields that the committer is responsible for setting.
#[derive(AsChangeset, Selectable, Queryable, Debug, Clone)]
#[derive(AsChangeset, Selectable, Queryable, Debug, Clone, FieldCount)]
#[diesel(table_name = watermarks)]
pub struct CommitterWatermark<'p> {
pub pipeline: Cow<'p, str>,
Expand Down Expand Up @@ -111,3 +112,18 @@ impl PartialOrd for CommitterWatermark<'_> {
Some(self.cmp(other))
}
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_stored_watermark_field_count() {
assert_eq!(StoredWatermark::field_count(), 8);
}

#[test]
fn test_committer_watermark_field_count() {
assert_eq!(CommitterWatermark::<'static>::field_count(), 4);
}
}

0 comments on commit ef0d78c

Please sign in to comment.