Skip to content

Commit

Permalink
Show display fields of object states from indexer (#1670)
Browse files Browse the repository at this point in the history
* indexer support batch querying with object ids

* add query option

* IndexerObjectStateView add display_fields

* fix indexer unit test for batch object ids

* try to parse display states

* complete show display field

* fix query param of integration tests
  • Loading branch information
pause125 committed May 21, 2024
1 parent 3a00ae2 commit 5cfcc59
Show file tree
Hide file tree
Showing 16 changed files with 308 additions and 233 deletions.
224 changes: 83 additions & 141 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 7 additions & 2 deletions crates/rooch-indexer/src/indexer_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,8 +374,13 @@ impl IndexerReader {
ObjectStateFilter::Owner(owner) => {
format!("{STATE_OWNER_STR} = \"{}\"", owner.to_hex_literal())
}
ObjectStateFilter::ObjectId(object_id) => {
format!("{OBJECT_ID_STR} = \"{}\"", object_id)
ObjectStateFilter::ObjectId(object_ids) => {
let object_ids_str = object_ids
.into_iter()
.map(|obj_id| format!("\"{}\"", obj_id))
.collect::<Vec<_>>()
.join(",");
format!("{OBJECT_ID_STR} IN ({object_ids_str})")
}
};

Expand Down
7 changes: 6 additions & 1 deletion crates/rooch-indexer/src/models/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,12 @@ impl StoredObjectState {
pub fn try_into_indexer_global_state(&self) -> Result<IndexerObjectState, anyhow::Error> {
let object_id = ObjectID::from_str(self.object_id.as_str())?;
let owner = AccountAddress::from_hex_literal(self.owner.as_str())?;
let object_type = StructTag::from_str(self.object_type.as_str())?;
let obj_type_str = if !self.object_type.starts_with("0x") {
format!("0x{}", self.object_type)
} else {
self.object_type.clone()
};
let object_type = StructTag::from_str(obj_type_str.as_str())?;
let state_root = AccountAddress::from_hex_literal(self.state_root.as_str())?;

let state = IndexerObjectState {
Expand Down
13 changes: 12 additions & 1 deletion crates/rooch-indexer/src/tests/test_indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,10 @@ fn test_state_store() -> Result<()> {
let indexer_reader = IndexerReader::new(indexer_db)?;

let mut new_object_states = random_new_object_states()?;
let new_object_ids = new_object_states
.iter()
.map(|state| state.object_id.clone())
.collect::<Vec<ObjectID>>();
let mut update_object_states = random_update_object_states(new_object_states.clone());
let remove_object_states = random_remove_object_states();

Expand All @@ -233,7 +237,7 @@ fn test_state_store() -> Result<()> {

//Merge new global states and update global states
new_object_states.append(&mut update_object_states);
indexer_store.persist_or_update_object_states(new_object_states)?;
indexer_store.persist_or_update_object_states(new_object_states.clone())?;
indexer_store.delete_object_states(remove_object_states)?;

//Merge new table states and update table states
Expand All @@ -248,6 +252,13 @@ fn test_state_store() -> Result<()> {
indexer_reader.query_object_states_with_filter(filter, None, 1, true)?;
assert_eq!(query_object_states.len(), 0);

// test for querying batch objects with filter ObjectStateFilter::ObjectId
let num_objs = new_object_ids.len();
let filter = ObjectStateFilter::ObjectId(new_object_ids);
let query_object_states =
indexer_reader.query_object_states_with_filter(filter, None, num_objs, true)?;
assert_eq!(query_object_states.len(), num_objs);

let talbe_handle = ObjectID::from_str("0x0")?;
let filter = FieldStateFilter::ObjectId(talbe_handle);
let query_field_states =
Expand Down
46 changes: 37 additions & 9 deletions crates/rooch-open-rpc-spec/schemas/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -411,9 +411,9 @@
}
},
{
"name": "descending_order",
"name": "query_option",
"schema": {
"type": "boolean"
"$ref": "#/components/schemas/QueryOptions"
}
}
],
Expand Down Expand Up @@ -449,9 +449,9 @@
}
},
{
"name": "descending_order",
"name": "query_option",
"schema": {
"type": "boolean"
"$ref": "#/components/schemas/QueryOptions"
}
}
],
Expand Down Expand Up @@ -487,9 +487,9 @@
}
},
{
"name": "descending_order",
"name": "query_option",
"schema": {
"type": "boolean"
"$ref": "#/components/schemas/QueryOptions"
}
}
],
Expand Down Expand Up @@ -525,9 +525,9 @@
}
},
{
"name": "descending_order",
"name": "query_option",
"schema": {
"type": "boolean"
"$ref": "#/components/schemas/QueryOptions"
}
}
],
Expand Down Expand Up @@ -1205,6 +1205,16 @@
"format": "uint64",
"minimum": 0.0
},
"display_fields": {
"anyOf": [
{
"$ref": "#/components/schemas/DisplayFieldsView"
},
{
"type": "null"
}
]
},
"flag": {
"type": "integer",
"format": "uint8",
Expand Down Expand Up @@ -1849,7 +1859,10 @@
],
"properties": {
"object_id": {
"$ref": "#/components/schemas/ObjectID"
"type": "array",
"items": {
"$ref": "#/components/schemas/ObjectID"
}
}
},
"additionalProperties": false
Expand Down Expand Up @@ -2234,6 +2247,21 @@
}
}
},
"QueryOptions": {
"type": "object",
"properties": {
"descending": {
"description": "If true, return query items in descending order.",
"default": true,
"type": "boolean"
},
"showDisplay": {
"description": "If true, result with display rendered is returned",
"default": false,
"type": "boolean"
}
}
},
"ScriptCallView": {
"type": "object",
"required": [
Expand Down
12 changes: 6 additions & 6 deletions crates/rooch-rpc-api/src/api/rooch_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::jsonrpc_types::{
AccessPathView, AccountAddressView, AnnotatedFunctionResultView, BalanceInfoPageView,
BytesView, EventOptions, EventPageView, ExecuteTransactionResponseView, FieldStateFilterView,
FunctionCallView, H256View, IndexerEventPageView, IndexerFieldStatePageView,
IndexerObjectStatePageView, ObjectStateFilterView, StateOptions, StatePageView, StateView,
StrView, StructTagView, TransactionWithInfoPageView,
IndexerObjectStatePageView, ObjectStateFilterView, QueryOptions, StateOptions, StatePageView,
StateView, StrView, StructTagView, TransactionWithInfoPageView,
};
use jsonrpsee::core::RpcResult;
use jsonrpsee::proc_macros::rpc;
Expand Down Expand Up @@ -117,7 +117,7 @@ pub trait RoochAPI {
// exclusive cursor if `Some`, otherwise start from the beginning
cursor: Option<StrView<u64>>,
limit: Option<StrView<usize>>,
descending_order: Option<bool>,
query_option: Option<QueryOptions>,
) -> RpcResult<TransactionWithInfoPageView>;

/// Query the events indexer by event filter
Expand All @@ -128,7 +128,7 @@ pub trait RoochAPI {
// exclusive cursor if `Some`, otherwise start from the beginning
cursor: Option<IndexerEventID>,
limit: Option<StrView<usize>>,
descending_order: Option<bool>,
query_option: Option<QueryOptions>,
) -> RpcResult<IndexerEventPageView>;

/// Query the object states indexer by state filter
Expand All @@ -139,7 +139,7 @@ pub trait RoochAPI {
// exclusive cursor if `Some`, otherwise start from the beginning
cursor: Option<IndexerStateID>,
limit: Option<StrView<usize>>,
descending_order: Option<bool>,
query_option: Option<QueryOptions>,
) -> RpcResult<IndexerObjectStatePageView>;

/// Query the Object field states indexer by state filter
Expand All @@ -150,6 +150,6 @@ pub trait RoochAPI {
// exclusive cursor if `Some`, otherwise start from the beginning
cursor: Option<IndexerStateID>,
limit: Option<StrView<usize>>,
descending_order: Option<bool>,
query_option: Option<QueryOptions>,
) -> RpcResult<IndexerFieldStatePageView>;
}
6 changes: 4 additions & 2 deletions crates/rooch-rpc-api/src/jsonrpc_types/btc/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ impl InscriptionFilterView {
let txid = hex_to_txid(txid.as_str())?;
let inscription_id = InscriptionID::new(txid.into_address(), index);
let obj_id = ord::derive_inscription_id(&inscription_id);
ObjectStateFilter::ObjectId(obj_id)
ObjectStateFilter::ObjectId(vec![obj_id])
}
InscriptionFilterView::ObjectId(object_id) => {
ObjectStateFilter::ObjectId(vec![object_id])
}
InscriptionFilterView::ObjectId(object_id) => ObjectStateFilter::ObjectId(object_id),
InscriptionFilterView::All => ObjectStateFilter::ObjectType(Inscription::struct_tag()),
})
}
Expand Down
4 changes: 2 additions & 2 deletions crates/rooch-rpc-api/src/jsonrpc_types/btc/utxo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ impl UTXOFilterView {
let outpoint =
rooch_types::bitcoin::types::OutPoint::new(txid.into_address(), vout);
let utxo_id = utxo::derive_utxo_id(&outpoint);
ObjectStateFilter::ObjectId(utxo_id)
ObjectStateFilter::ObjectId(vec![utxo_id])
}
UTXOFilterView::ObjectId(object_id) => ObjectStateFilter::ObjectId(object_id),
UTXOFilterView::ObjectId(object_id) => ObjectStateFilter::ObjectId(vec![object_id]),
UTXOFilterView::All => ObjectStateFilter::ObjectType(UTXO::struct_tag()),
})
}
Expand Down
30 changes: 30 additions & 0 deletions crates/rooch-rpc-api/src/jsonrpc_types/rpc_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,33 @@ impl TxOptions {
self
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema, Eq, PartialEq)]
#[serde(rename_all = "camelCase", default)]
pub struct QueryOptions {
/// If true, return query items in descending order.
pub descending: bool,
/// If true, result with display rendered is returned
pub show_display: bool,
}

impl QueryOptions {
pub fn descending(mut self, descending: bool) -> Self {
self.descending = descending;
self
}

pub fn show_display(mut self, show_display: bool) -> Self {
self.show_display = show_display;
self
}
}

impl Default for QueryOptions {
fn default() -> Self {
Self {
descending: true,
show_display: false,
}
}
}
11 changes: 9 additions & 2 deletions crates/rooch-rpc-api/src/jsonrpc_types/state_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,7 @@ pub struct IndexerObjectStateView {
pub state_index: u64,
pub created_at: u64,
pub updated_at: u64,
pub display_fields: Option<DisplayFieldsView>,
}

impl IndexerObjectStateView {
Expand All @@ -400,8 +401,14 @@ impl IndexerObjectStateView {
state_index: state.state_index,
created_at: state.created_at,
updated_at: state.updated_at,
display_fields: None,
}
}

pub fn with_display_fields(mut self, display_fields: Option<DisplayFieldsView>) -> Self {
self.display_fields = display_fields;
self
}
}

#[derive(Debug, Clone, Deserialize, Serialize, JsonSchema)]
Expand All @@ -417,7 +424,7 @@ pub enum ObjectStateFilterView {
/// Query by owner.
Owner(AccountAddressView),
/// Query by object id.
ObjectId(ObjectID),
ObjectId(Vec<ObjectID>),
/// Query by multi chain address
MultiChainAddress { multichain_id: u64, address: String },
}
Expand All @@ -438,7 +445,7 @@ impl ObjectStateFilterView {
ObjectStateFilter::ObjectType(object_type.into())
}
ObjectStateFilterView::Owner(owner) => ObjectStateFilter::Owner(owner.into()),
ObjectStateFilterView::ObjectId(object_id) => ObjectStateFilter::ObjectId(object_id),
ObjectStateFilterView::ObjectId(object_ids) => ObjectStateFilter::ObjectId(object_ids),
ObjectStateFilterView::MultiChainAddress {
multichain_id: _,
address: _,
Expand Down

0 comments on commit 5cfcc59

Please sign in to comment.