-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[indexer-alt] Add obj_info pipeline #20436
Open
lxfind
wants to merge
1
commit into
main
Choose a base branch
from
indexer-alt-add-object-info-pipeline
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+235
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
1 change: 1 addition & 0 deletions
1
crates/sui-indexer-alt/migrations/2024-11-25-211949_obj_info/down.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE IF EXISTS obj_info; |
67 changes: 67 additions & 0 deletions
67
crates/sui-indexer-alt/migrations/2024-11-25-211949_obj_info/up.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
-- A table that keeps track of all the updates to object type and owner information. | ||
-- In particular, whenever an object's presence or ownership changes, we insert a | ||
-- new row into this table. Each row should have a unique (object_id, cp_sequence_number) | ||
-- pair. | ||
-- When implementing consistency queries, we will use this table to find all | ||
-- object IDs that match the given filters bounded by the cursor checkpoint. | ||
-- These object IDs can then be used to look up the latest version of the objects | ||
-- bounded by the given checkpoint in the object_versions table. | ||
CREATE TABLE IF NOT EXISTS obj_info | ||
( | ||
object_id BYTEA NOT NULL, | ||
cp_sequence_number BIGINT NOT NULL, | ||
-- An enum describing the object's ownership model: | ||
-- | ||
-- Immutable = 0, | ||
-- Address-owned = 1, | ||
-- Object-owned (dynamic field) = 2, | ||
-- Shared = 3. | ||
-- | ||
-- Note that there is a distinction between an object that is owned by | ||
-- another object (kind 2), which relates to dynamic fields, and an object | ||
-- that is owned by another object's address (kind 1), which relates to | ||
-- transfer-to-object. | ||
owner_kind SMALLINT, | ||
-- The address for address-owned objects, and the parent object for | ||
-- object-owned objects. | ||
owner_id BYTEA, | ||
-- The following fields relate to the object's type. These only apply to | ||
-- Move Objects. For Move Packages they will all be NULL. | ||
-- | ||
-- The type's package ID. | ||
package BYTEA, | ||
-- The type's module name. | ||
module TEXT, | ||
-- The type's name. | ||
name TEXT, | ||
-- The type's type parameters, as a BCS-encoded array of TypeTags. | ||
instantiation BYTEA, | ||
PRIMARY KEY (object_id, cp_sequence_number) | ||
); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_owner | ||
ON obj_info (owner_kind, owner_id, cp_sequence_number, object_id); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_pkg | ||
ON obj_info (package, cp_sequence_number, object_id); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_mod | ||
ON obj_info (package, module, cp_sequence_number, object_id); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_name | ||
ON obj_info (package, module, name, cp_sequence_number, object_id); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_inst | ||
ON obj_info (package, module, name, instantiation, cp_sequence_number, object_id); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_owner_pkg | ||
ON obj_info (owner_kind, owner_id, package, cp_sequence_number, object_id); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_owner_mod | ||
ON obj_info (owner_kind, owner_id, package, module, cp_sequence_number, object_id); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_owner_name | ||
ON obj_info (owner_kind, owner_id, package, module, name, cp_sequence_number, object_id); | ||
|
||
CREATE INDEX IF NOT EXISTS obj_info_owner_inst | ||
ON obj_info (owner_kind, owner_id, package, module, name, instantiation, cp_sequence_number, object_id); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) Mysten Labs, Inc. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
use std::{collections::BTreeMap, sync::Arc}; | ||
|
||
use anyhow::{anyhow, Result}; | ||
use diesel_async::RunQueryDsl; | ||
use sui_types::{base_types::ObjectID, full_checkpoint_content::CheckpointData, object::Owner}; | ||
|
||
use crate::{ | ||
db, | ||
models::objects::{StoredObjInfo, StoredOwnerKind}, | ||
pipeline::{concurrent::Handler, Processor}, | ||
schema::obj_info, | ||
}; | ||
|
||
pub struct ObjInfo; | ||
|
||
impl Processor for ObjInfo { | ||
const NAME: &'static str = "obj_info"; | ||
type Value = StoredObjInfo; | ||
|
||
fn process(&self, checkpoint: &Arc<CheckpointData>) -> Result<Vec<Self::Value>> { | ||
let cp_sequence_number = checkpoint.checkpoint_summary.sequence_number as i64; | ||
let checkpoint_input_objects = checkpoint.checkpoint_input_objects(); | ||
let latest_live_output_objects = checkpoint | ||
.latest_live_output_objects() | ||
.into_iter() | ||
.map(|o| (o.id(), o)) | ||
.collect::<BTreeMap<_, _>>(); | ||
let mut values: BTreeMap<ObjectID, Self::Value> = BTreeMap::new(); | ||
for object_id in checkpoint_input_objects.keys() { | ||
if !latest_live_output_objects.contains_key(object_id) { | ||
// If an input object is not in the latest live output objects, it must have been deleted | ||
// or wrapped in this checkpoint. We keep an entry for it in the table. | ||
// This is necessary when we query objects and iterating over them, so that we don't | ||
// include the object in the result if it was deleted. | ||
values.insert( | ||
*object_id, | ||
StoredObjInfo { | ||
object_id: object_id.to_vec(), | ||
cp_sequence_number, | ||
owner_kind: None, | ||
owner_id: None, | ||
package: None, | ||
module: None, | ||
name: None, | ||
instantiation: None, | ||
}, | ||
); | ||
} | ||
} | ||
for (object_id, object) in latest_live_output_objects.iter() { | ||
// If an object is newly created/unwrapped in this checkpoint, or if the owner changed, | ||
// we need to insert an entry for it in the table. | ||
let should_insert = match checkpoint_input_objects.get(object_id) { | ||
Some(input_object) => input_object.owner() != object.owner(), | ||
None => true, | ||
}; | ||
if should_insert { | ||
let type_ = object.type_(); | ||
values.insert( | ||
*object_id, | ||
StoredObjInfo { | ||
object_id: object_id.to_vec(), | ||
cp_sequence_number, | ||
owner_kind: Some(match object.owner() { | ||
Owner::AddressOwner(_) => StoredOwnerKind::Address, | ||
Owner::ObjectOwner(_) => StoredOwnerKind::Object, | ||
Owner::Shared { .. } => StoredOwnerKind::Shared, | ||
Owner::Immutable => StoredOwnerKind::Immutable, | ||
Owner::ConsensusV2 { .. } => todo!(), | ||
}), | ||
|
||
owner_id: match object.owner() { | ||
Owner::AddressOwner(a) => Some(a.to_vec()), | ||
Owner::ObjectOwner(o) => Some(o.to_vec()), | ||
Owner::Shared { .. } | Owner::Immutable { .. } => None, | ||
Owner::ConsensusV2 { .. } => todo!(), | ||
}, | ||
|
||
package: type_.map(|t| t.address().to_vec()), | ||
module: type_.map(|t| t.module().to_string()), | ||
name: type_.map(|t| t.name().to_string()), | ||
instantiation: type_ | ||
.map(|t| bcs::to_bytes(&t.type_params())) | ||
.transpose() | ||
.map_err(|e| { | ||
anyhow!( | ||
"Failed to serialize type parameters for {}: {e}", | ||
object.id().to_canonical_display(/* with_prefix */ true), | ||
) | ||
})?, | ||
}, | ||
); | ||
} | ||
} | ||
|
||
Ok(values.into_values().collect()) | ||
} | ||
} | ||
|
||
#[async_trait::async_trait] | ||
impl Handler for ObjInfo { | ||
async fn commit(values: &[Self::Value], conn: &mut db::Connection<'_>) -> Result<usize> { | ||
Ok(diesel::insert_into(obj_info::table) | ||
.values(values) | ||
.on_conflict_do_nothing() | ||
.execute(conn) | ||
.await?) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,14 +3,15 @@ | |
|
||
use std::collections::BTreeMap; | ||
|
||
use crate::base_types::ObjectRef; | ||
use crate::base_types::{ObjectID, ObjectRef}; | ||
use crate::effects::{ | ||
IDOperation, ObjectIn, ObjectOut, TransactionEffects, TransactionEffectsAPI, TransactionEvents, | ||
}; | ||
use crate::messages_checkpoint::{CertifiedCheckpointSummary, CheckpointContents}; | ||
use crate::object::Object; | ||
use crate::storage::BackingPackageStore; | ||
use crate::transaction::Transaction; | ||
use im::HashSet; | ||
use itertools::Either; | ||
use serde::{Deserialize, Serialize}; | ||
use tap::Pipe; | ||
|
@@ -51,11 +52,24 @@ impl CheckpointData { | |
eventually_removed_object_refs.into_values().collect() | ||
} | ||
|
||
pub fn input_objects(&self) -> Vec<&Object> { | ||
self.transactions | ||
.iter() | ||
.flat_map(|tx| &tx.input_objects) | ||
.collect() | ||
/// Returns all objects that are used as input to the transactions in the checkpoint, | ||
/// and already exist prior to the checkpoint. | ||
pub fn checkpoint_input_objects(&self) -> BTreeMap<ObjectID, &Object> { | ||
let mut output_objects_seen = HashSet::new(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what's the consideration here of suing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. oops that was a typo |
||
let mut checkpoint_input_objects = BTreeMap::new(); | ||
for tx in self.transactions.iter() { | ||
for obj in tx.input_objects.iter() { | ||
let id = obj.id(); | ||
if output_objects_seen.contains(&id) || checkpoint_input_objects.contains_key(&id) { | ||
continue; | ||
} | ||
checkpoint_input_objects.insert(id, obj); | ||
} | ||
for obj in tx.output_objects.iter() { | ||
output_objects_seen.insert(obj.id()); | ||
} | ||
} | ||
checkpoint_input_objects | ||
} | ||
|
||
pub fn all_objects(&self) -> Vec<&Object> { | ||
|
@@ -73,7 +87,7 @@ pub struct CheckpointTransaction { | |
pub transaction: Transaction, | ||
/// The effects produced by executing this transaction | ||
pub effects: TransactionEffects, | ||
/// The events, if any, emitted by this transaciton during execution | ||
/// The events, if any, emitted by this transactions during execution | ||
pub events: Option<TransactionEvents>, | ||
/// The state of all inputs to this transaction as they were prior to execution. | ||
pub input_objects: Vec<Object>, | ||
|
@@ -87,7 +101,7 @@ impl CheckpointTransaction { | |
// Iterator over id and versions for all deleted or wrapped objects | ||
match &self.effects { | ||
TransactionEffects::V1(v1) => Either::Left( | ||
// Effects v1 has delted and wrapped objects versions as the "new" version, not the | ||
// Effects v1 has deleted and wrapped objects versions as the "new" version, not the | ||
// old one that was actually removed. So we need to take these and then look them | ||
// up in the `modified_at_versions`. | ||
// No need to chain unwrapped_then_deleted because these objects must have been wrapped | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not following here, can you elaborate on the query that needs to use deleted / wrapped entries?
also looks like there is no "marker column" marking if the object ID is deleted/wrapped, would that be problematic, for example if a query asking for 50 IDs ended up getting some deleted objects, and as a result end graphql response has < 50 results?