Skip to content

Commit

Permalink
sui ns rpc: add option for resolve_name_service_address (#11105) (#11132
Browse files Browse the repository at this point in the history
)
  • Loading branch information
gegaowp authored Apr 20, 2023
1 parent 1ef9109 commit 133fd85
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 40 deletions.
2 changes: 1 addition & 1 deletion crates/sui-indexer/src/apis/indexer_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ where
Ok(())
}

async fn resolve_name_service_address(&self, _name: String) -> RpcResult<SuiAddress> {
async fn resolve_name_service_address(&self, _name: String) -> RpcResult<Option<SuiAddress>> {
// TODO(gegaowp): implement name service resolver in indexer
todo!()
}
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-json-rpc/src/api/indexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ pub trait IndexerApi {
&self,
/// The name to resolve
name: String,
) -> RpcResult<SuiAddress>;
) -> RpcResult<Option<SuiAddress>>;

/// Return the resolved names given address,
/// if multiple names are resolved, the first one is the primary name.
Expand Down
78 changes: 41 additions & 37 deletions crates/sui-json-rpc/src/indexer_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ impl<R: ReadApiServer> IndexerApiServer for IndexerApi<R> {
.get_object(id, Some(SuiObjectDataOptions::full_content()))
}

async fn resolve_name_service_address(&self, name: String) -> RpcResult<SuiAddress> {
async fn resolve_name_service_address(&self, name: String) -> RpcResult<Option<SuiAddress>> {
let dynmaic_field_table_object_id =
self.get_name_service_dynamic_field_table_object_id(/* reverse_lookup */ false)?;
// NOTE: 0x1::string::String is the type tag of fields in dynamic_field_table
Expand All @@ -287,7 +287,7 @@ impl<R: ReadApiServer> IndexerApiServer for IndexerApi<R> {
}));
let name_bcs_value = bcs::to_bytes(&name).context("Unable to serialize name")?;
// record of the input `name`
let record_object_id = self
let record_object_id_option = self
.state
.get_dynamic_field_object_id(
dynmaic_field_table_object_id,
Expand All @@ -299,42 +299,46 @@ impl<R: ReadApiServer> IndexerApiServer for IndexerApi<R> {
"Read name service dynamic field table failed with error: {:?}",
e
)
})?
.ok_or_else(|| anyhow!("Record not found for name: {:?}", name,))?;
let record_object_read = self.state.get_object_read(&record_object_id).map_err(|e| {
warn!(
"Failed to get object read of name: {:?} with error: {:?}",
record_object_id, e
);
anyhow!("{e}")
})?;
let record_parsed_move_object =
SuiParsedMoveObject::try_from_object_read(record_object_read)?;
// NOTE: "value" is the field name to get the address info
let address_info_move_value = record_parsed_move_object
.read_dynamic_field_value(NAME_SERVICE_VALUE)
.ok_or_else(|| anyhow!("Cannot find value field in record Move struct"))?;
let address_info_move_struct = match address_info_move_value {
SuiMoveValue::Struct(a) => Ok(a),
_ => Err(anyhow!("value field is not found.")),
}?;
// NOTE: "marker" is the field name to get the address
let address_str_move_value = address_info_move_struct
.read_dynamic_field_value(NAME_SERVICE_MARKER)
.ok_or_else(|| {
anyhow!(
"Cannot find marker field in address info Move struct: {:?}",
address_info_move_struct
)
})?;
let addr = match address_str_move_value {
SuiMoveValue::Address(addr) => Ok(addr),
_ => Err(anyhow!(
"No SuiAddress found in: {:?}",
address_str_move_value
)),
}?;
Ok(addr)
if let Some(record_object_id) = record_object_id_option {
let record_object_read =
self.state.get_object_read(&record_object_id).map_err(|e| {
warn!(
"Failed to get object read of name: {:?} with error: {:?}",
record_object_id, e
);
anyhow!("{e}")
})?;
let record_parsed_move_object =
SuiParsedMoveObject::try_from_object_read(record_object_read)?;
// NOTE: "value" is the field name to get the address info
let address_info_move_value = record_parsed_move_object
.read_dynamic_field_value(NAME_SERVICE_VALUE)
.ok_or_else(|| anyhow!("Cannot find value field in record Move struct"))?;
let address_info_move_struct = match address_info_move_value {
SuiMoveValue::Struct(a) => Ok(a),
_ => Err(anyhow!("value field is not found.")),
}?;
// NOTE: "marker" is the field name to get the address
let address_str_move_value = address_info_move_struct
.read_dynamic_field_value(NAME_SERVICE_MARKER)
.ok_or_else(|| {
anyhow!(
"Cannot find marker field in address info Move struct: {:?}",
address_info_move_struct
)
})?;
let addr = match address_str_move_value {
SuiMoveValue::Address(addr) => Ok(addr),
_ => Err(anyhow!(
"No SuiAddress found in: {:?}",
address_str_move_value
)),
}?;
return Ok(Some(addr));
} else {
return Ok(None);
}
}

async fn resolve_name_service_names(
Expand Down
1 change: 0 additions & 1 deletion crates/sui-open-rpc/spec/openrpc.json
Original file line number Diff line number Diff line change
Expand Up @@ -1789,7 +1789,6 @@
],
"result": {
"name": "SuiAddress",
"required": true,
"schema": {
"$ref": "#/components/schemas/SuiAddress"
}
Expand Down

0 comments on commit 133fd85

Please sign in to comment.