-
Notifications
You must be signed in to change notification settings - Fork 3.9k
[annotator] Avoid constructing and scanning values for tables #18088
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
Open
wrwg
wants to merge
1
commit into
main
Choose a base branch
from
wrwg/table-check
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.
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
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -10,15 +10,14 @@ use aptos_db_indexer_schemas::{ | |
| schema::{indexer_metadata::IndexerMetadataSchema, table_info::TableInfoSchema}, | ||
| }; | ||
| use aptos_logger::{info, sample, sample::SampleRate}; | ||
| use aptos_resource_viewer::{AnnotatedMoveValue, AptosValueAnnotator}; | ||
| use aptos_resource_viewer::AptosValueAnnotator; | ||
| use aptos_schemadb::{batch::SchemaBatch, DB}; | ||
| use aptos_storage_interface::{ | ||
| db_other_bail as bail, state_store::state_view::db_state_view::DbStateViewAtVersion, | ||
| AptosDbError, DbReader, Result, | ||
| }; | ||
| use aptos_types::{ | ||
| access_path::Path, | ||
| account_address::AccountAddress, | ||
| state_store::{ | ||
| state_key::{inner::StateKeyInner, StateKey}, | ||
| table::{TableHandle, TableInfo}, | ||
|
|
@@ -30,8 +29,8 @@ use aptos_types::{ | |
| use bytes::Bytes; | ||
| use dashmap::{DashMap, DashSet}; | ||
| use move_core_types::{ | ||
| ident_str, | ||
| language_storage::{StructTag, TypeTag}, | ||
| value::{MoveStruct, MoveValue}, | ||
| }; | ||
| use std::{ | ||
| collections::{BTreeMap, HashMap}, | ||
|
|
@@ -98,7 +97,7 @@ impl IndexerAsyncV2 { | |
| let mut table_info_parser = TableInfoParser::new(self, annotator, &self.pending_on); | ||
| for write_set in write_sets { | ||
| for (state_key, write_op) in write_set.write_op_iter() { | ||
| table_info_parser.parse_write_op(state_key, write_op)?; | ||
| table_info_parser.collect_table_info_from_write_op(state_key, write_op)?; | ||
| } | ||
| } | ||
| let mut batch = SchemaBatch::new(); | ||
|
|
@@ -231,45 +230,66 @@ impl<'a, R: StateView> TableInfoParser<'a, R> { | |
| } | ||
|
|
||
| /// Parses a write operation and extracts table information from it. | ||
| pub fn parse_write_op(&mut self, state_key: &'a StateKey, write_op: &'a WriteOp) -> Result<()> { | ||
| pub fn collect_table_info_from_write_op( | ||
| &mut self, | ||
| state_key: &'a StateKey, | ||
| write_op: &'a WriteOp, | ||
| ) -> Result<()> { | ||
| if let Some(bytes) = write_op.bytes() { | ||
| match state_key.inner() { | ||
| StateKeyInner::AccessPath(access_path) => { | ||
| let path: Path = (&access_path.path).try_into()?; | ||
| match path { | ||
| Path::Code(_) => (), | ||
| Path::Resource(struct_tag) => self.parse_struct(struct_tag, bytes)?, | ||
| Path::ResourceGroup(_struct_tag) => self.parse_resource_group(bytes)?, | ||
| Path::Resource(struct_tag) => { | ||
| self.collect_table_info_from_struct(struct_tag, bytes)? | ||
| }, | ||
| Path::ResourceGroup(_struct_tag) => { | ||
| self.collect_table_info_from_resource_group(bytes)? | ||
| }, | ||
| } | ||
| }, | ||
| StateKeyInner::TableItem { handle, .. } => self.parse_table_item(*handle, bytes)?, | ||
| StateKeyInner::TableItem { handle, .. } => { | ||
| self.collect_table_info_from_table_item(*handle, bytes)? | ||
| }, | ||
| StateKeyInner::Raw(_) => (), | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn parse_struct(&mut self, struct_tag: StructTag, bytes: &Bytes) -> Result<()> { | ||
| self.parse_move_value( | ||
| &self | ||
| .annotator | ||
| .view_value(&TypeTag::Struct(Box::new(struct_tag)), bytes)?, | ||
| ) | ||
| fn collect_table_info_from_struct( | ||
| &mut self, | ||
| struct_tag: StructTag, | ||
| bytes: &Bytes, | ||
| ) -> Result<()> { | ||
| let ty_tag = TypeTag::Struct(Box::new(struct_tag)); | ||
| let mut infos = vec![]; | ||
| self.annotator | ||
| .collect_table_info(&ty_tag, bytes, &mut infos)?; | ||
| self.process_table_infos(infos) | ||
| } | ||
|
|
||
| fn parse_resource_group(&mut self, bytes: &Bytes) -> Result<()> { | ||
| fn collect_table_info_from_resource_group(&mut self, bytes: &Bytes) -> Result<()> { | ||
| type ResourceGroup = BTreeMap<StructTag, Bytes>; | ||
|
|
||
| for (struct_tag, bytes) in bcs::from_bytes::<ResourceGroup>(bytes)? { | ||
| self.parse_struct(struct_tag, &bytes)?; | ||
| self.collect_table_info_from_struct(struct_tag, &bytes)?; | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn parse_table_item(&mut self, handle: TableHandle, bytes: &Bytes) -> Result<()> { | ||
| fn collect_table_info_from_table_item( | ||
| &mut self, | ||
| handle: TableHandle, | ||
| bytes: &Bytes, | ||
| ) -> Result<()> { | ||
| match self.get_table_info(handle)? { | ||
| Some(table_info) => { | ||
| self.parse_move_value(&self.annotator.view_value(&table_info.value_type, bytes)?)?; | ||
| let mut infos = vec![]; | ||
| self.annotator | ||
| .collect_table_info(&table_info.value_type, bytes, &mut infos)?; | ||
| self.process_table_infos(infos)? | ||
| }, | ||
| None => { | ||
| self.pending_on | ||
|
|
@@ -281,70 +301,22 @@ impl<'a, R: StateView> TableInfoParser<'a, R> { | |
| Ok(()) | ||
| } | ||
|
|
||
| /// Parses a write operation and extracts table information from it. | ||
| /// | ||
| /// The `parse_move_value` function is a recursive method that traverses | ||
| /// through the `AnnotatedMoveValue` structure. Depending on the type of | ||
| /// `AnnotatedMoveValue` (e.g., Vector, Struct, or primitive types), it | ||
| /// performs different parsing actions. For Vector and Struct, it recursively | ||
| /// calls itself to parse each element or field. This recursive approach allows | ||
| /// the function to handle nested data structures in Move values. | ||
| fn parse_move_value(&mut self, move_value: &AnnotatedMoveValue) -> Result<()> { | ||
| match move_value { | ||
| AnnotatedMoveValue::Vector(_type_tag, items) => { | ||
| for item in items { | ||
| self.parse_move_value(item)?; | ||
| } | ||
| }, | ||
| AnnotatedMoveValue::Struct(struct_value) => { | ||
| let struct_tag = &struct_value.ty_tag; | ||
| if Self::is_table(struct_tag) { | ||
| assert_eq!(struct_tag.type_args.len(), 2); | ||
| let table_info = TableInfo { | ||
| key_type: struct_tag.type_args[0].clone(), | ||
| value_type: struct_tag.type_args[1].clone(), | ||
| }; | ||
| let table_handle = match &struct_value.value[0] { | ||
| (name, AnnotatedMoveValue::Address(handle)) => { | ||
| assert_eq!(name.as_ref(), ident_str!("handle")); | ||
| TableHandle(*handle) | ||
| }, | ||
| _ => bail!("Table struct malformed. {:?}", struct_value), | ||
| }; | ||
| self.save_table_info(table_handle, table_info)?; | ||
| } else { | ||
| for (_identifier, field) in &struct_value.value { | ||
| self.parse_move_value(field)?; | ||
| } | ||
| } | ||
| }, | ||
| AnnotatedMoveValue::RawStruct(struct_value) => { | ||
| for val in &struct_value.field_values { | ||
| self.parse_move_value(val)? | ||
| } | ||
| }, | ||
| AnnotatedMoveValue::Closure(closure_value) => { | ||
| for capture in &closure_value.captured { | ||
| self.parse_move_value(capture)? | ||
| } | ||
| }, | ||
|
|
||
| // there won't be tables in primitives | ||
| AnnotatedMoveValue::U8(_) => {}, | ||
| AnnotatedMoveValue::U16(_) => {}, | ||
| AnnotatedMoveValue::U32(_) => {}, | ||
| AnnotatedMoveValue::U64(_) => {}, | ||
| AnnotatedMoveValue::U128(_) => {}, | ||
| AnnotatedMoveValue::U256(_) => {}, | ||
| AnnotatedMoveValue::I8(_) => {}, | ||
| AnnotatedMoveValue::I16(_) => {}, | ||
| AnnotatedMoveValue::I32(_) => {}, | ||
| AnnotatedMoveValue::I64(_) => {}, | ||
| AnnotatedMoveValue::I128(_) => {}, | ||
| AnnotatedMoveValue::I256(_) => {}, | ||
| AnnotatedMoveValue::Bool(_) => {}, | ||
| AnnotatedMoveValue::Address(_) => {}, | ||
| AnnotatedMoveValue::Bytes(_) => {}, | ||
| fn process_table_infos(&mut self, infos: Vec<(StructTag, MoveStruct)>) -> Result<()> { | ||
| for (tag, val) in infos { | ||
| let StructTag { mut type_args, .. } = tag; | ||
| if type_args.len() == 2 | ||
| && let MoveStruct::Runtime(vals) = &val | ||
| && let Some(MoveValue::Address(handle)) = vals.first() | ||
|
Contributor
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. why do we need value in the first place? seems like we use types only
Contributor
Author
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. The handle is used in save_table_info, so we need it. |
||
| { | ||
| let value_type = type_args.pop().unwrap(); | ||
| let key_type = type_args.pop().unwrap(); | ||
| self.save_table_info(TableHandle(*handle), TableInfo { | ||
| key_type, | ||
| value_type, | ||
| })? | ||
| } else { | ||
| bail!("Table struct malformed. {:?}", val) | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
@@ -354,19 +326,13 @@ impl<'a, R: StateView> TableInfoParser<'a, R> { | |
| self.result.insert(handle, info); | ||
| if let Some(pending_items) = self.pending_on.remove(&handle) { | ||
| for bytes in pending_items.1 { | ||
| self.parse_table_item(handle, &bytes)?; | ||
| self.collect_table_info_from_table_item(handle, &bytes)?; | ||
| } | ||
| } | ||
| } | ||
| Ok(()) | ||
| } | ||
|
|
||
| fn is_table(struct_tag: &StructTag) -> bool { | ||
| struct_tag.address == AccountAddress::ONE | ||
| && struct_tag.module.as_ident_str() == ident_str!("table") | ||
| && struct_tag.name.as_ident_str() == ident_str!("Table") | ||
| } | ||
|
|
||
| /// Retrieves table information either from the in-memory results or from the database. | ||
| /// | ||
| /// This method first checks if the table information for the given handle exists in the | ||
|
|
||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.