Skip to content

Commit

Permalink
[cherry-pick][rpc][bugfix] Remove the usage of get_past_object_read f…
Browse files Browse the repository at this point in the history
…rom get_coin_… (#11999)

…metadata (#11971)

## Description 

The current implementation of `get_coin_metadata` relies on
`get_past_object_read`, which stops working if the fullnode has pruning
enabled. This is unnecessary because we only need the latest version of
the coinmetada object(if it is wrapped, then it's okay to return null).

## Test Plan 

tested locally

```
curl --location 'https://mainnet.sui.rpcpool.com' \
--header 'Content-Type: application/json' \
--data '{
    "jsonrpc":"2.0",
    "id":1,
    "method":"suix_getCoinMetadata",
    "params":["0x2::sui::SUI"]
}'
```

```
{
    "jsonrpc": "2.0",
    "result": {
        "decimals": 9,
        "name": "Sui",
        "symbol": "SUI",
        "description": "",
        "iconUrl": null,
        "id": "0x9258181f5ceac8dbffb7030890243caed69a9599d2886d957a9cb7656af3bdb3"
    },
    "id": 1
}
```

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [x] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes

Fixed a bug for `get_coin_metadata` which can return null when the
fullnode is pruned

## Description 

Describe the changes or additions included in this PR.

## Test Plan 

How did you test the new or updated feature?

---
If your changes are not user-facing and not a breaking change, you can
skip the following section. Otherwise, please indicate what changed, and
then add to the Release Notes section as highlighted during the release
process.

### Type of Change (Check all that apply)

- [ ] user-visible impact
- [ ] breaking change for a client SDKs
- [ ] breaking change for FNs (FN binary must upgrade)
- [ ] breaking change for validators or node operators (must upgrade
binaries)
- [ ] breaking change for on-chain data layout
- [ ] necessitate either a data wipe or data migration

### Release notes
  • Loading branch information
666lcz authored May 15, 2023
1 parent 1f46cc0 commit 4c9993f
Showing 1 changed file with 11 additions and 24 deletions.
35 changes: 11 additions & 24 deletions crates/sui-json-rpc/src/coin_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ use sui_json_rpc_types::{Balance, Coin as SuiCoin};
use sui_json_rpc_types::{CoinPage, SuiCoinMetadata};
use sui_open_rpc::Module;
use sui_types::balance::Supply;
use sui_types::base_types::{ObjectID, ObjectRef, SuiAddress};
use sui_types::base_types::{ObjectID, SuiAddress};
use sui_types::coin::{CoinMetadata, TreasuryCap};
use sui_types::effects::TransactionEffectsAPI;
use sui_types::error::SuiError;
use sui_types::gas_coin::GAS;
use sui_types::object::{Object, Owner};
use sui_types::object::Object;
use sui_types::parse_sui_struct_tag;

use crate::api::{cap_page_limit, CoinReadApiServer, JsonRpcMetrics};
Expand Down Expand Up @@ -118,32 +118,19 @@ async fn find_package_object_id(
.get_executed_transaction_and_effects(publish_txn_digest)
.await?;

async fn find_object_with_type(
state: &Arc<AuthorityState>,
created: &[(ObjectRef, Owner)],
object_struct_tag: &StructTag,
package_id: &ObjectID,
) -> Result<ObjectID, anyhow::Error> {
for ((id, version, _), _) in created {
if let Ok(past_object) = state.get_past_object_read(id, *version) {
if let Ok(object) = past_object.into_object() {
if matches!(object.type_(), Some(type_) if type_.is(object_struct_tag)) {
return Ok(*id);
}
for ((id, _, _), _) in effect.created() {
if let Ok(object_read) = state.get_object_read(id) {
if let Ok(object) = object_read.into_object() {
if matches!(object.type_(), Some(type_) if type_.is(&object_struct_tag)) {
return Ok(*id);
}
}
}
Err(anyhow!(
"Cannot find object [{}] from [{}] package event.",
object_struct_tag,
package_id
))
}

let object_id =
find_object_with_type(&state, effect.created(), &object_struct_tag, &package_id)
.await?;
Ok(object_id)
Err(Error::UnexpectedError(format!(
"Cannot find object [{}] from [{}] package event.",
object_struct_tag, package_id,
)))
})
.await?
}
Expand Down

0 comments on commit 4c9993f

Please sign in to comment.