Skip to content
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

[RPC Server] Simplify the response schema for object_info #1440

Merged
merged 3 commits into from
Apr 20, 2022

Conversation

666lcz
Copy link
Contributor

@666lcz 666lcz commented Apr 19, 2022

The response returned by the existing object_info is too complicated for the client applications

Before

{
    "Exists": [
        [
            "024f81362efe7404119b931bd5daa66e35d4e704",
            0,
            "ntEwRW9UM/UNNQkkAuunTtvranJ+3G5xjS7qDhz8A70="
        ],
        {
            "data": {
                "Move": {
                    "type_": {
                        "address": "0000000000000000000000000000000000000002",
                        "module": "Coin",
                        "name": "Coin",
                        "type_args": [
                            {
                                "struct": {
                                    "address": "0000000000000000000000000000000000000002",
                                    "module": "SUI",
                                    "name": "SUI",
                                    "type_args": []
                                }
                            }
                        ]
                    },
                    "contents": "Ak+BNi7+dAQRm5Mb1dqmbjXU5wQAAAAAAAAAAKCGAQAAAAAA"
                }
            },
            "owner": {
                "AddressOwner": "836f28fce574a84e4135725fdcb549679b09e62d"
            },
            "previous_transaction": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=",
            "storage_rebate": 0
        },
        {
            "WithTypes": {
                "type_": {
                    "address": "0000000000000000000000000000000000000002",
                    "module": "Coin",
                    "name": "Coin",
                    "type_args": [
                        {
                            "struct": {
                                "address": "0000000000000000000000000000000000000002",
                                "module": "SUI",
                                "name": "SUI",
                                "type_args": []
                            }
                        }
                    ]
                },
                "fields": [
                    {
                        "name": "id",
                        "layout": {
                            "struct": {
                                "WithTypes": {
                                    "type_": {
                                        "address": "0000000000000000000000000000000000000002",
                                        "module": "ID",
                                        "name": "VersionedID",
                                        "type_args": []
                                    },
                                    "fields": [
                                        {
                                            "name": "id",
                                            "layout": {
                                                "struct": {
                                                    "WithTypes": {
                                                        "type_": {
                                                            "address": "0000000000000000000000000000000000000002",
                                                            "module": "ID",
                                                            "name": "UniqueID",
                                                            "type_args": []
                                                        },
                                                        "fields": [
                                                            {
                                                                "name": "id",
                                                                "layout": {
                                                                    "struct": {
                                                                        "WithTypes": {
                                                                            "type_": {
                                                                                "address": "0000000000000000000000000000000000000002",
                                                                                "module": "ID",
                                                                                "name": "ID",
                                                                                "type_args": []
                                                                            },
                                                                            "fields": [
                                                                                {
                                                                                    "name": "bytes",
                                                                                    "layout": "address"
                                                                                }
                                                                            ]
                                                                        }
                                                                    }
                                                                }
                                                            }
                                                        ]
                                                    }
                                                }
                                            }
                                        },
                                        {
                                            "name": "version",
                                            "layout": "u64"
                                        }
                                    ]
                                }
                            }
                        }
                    },
                    {
                        "name": "value",
                        "layout": "u64"
                    }
                ]
            }
        }
    ]
}

After

{
    "status": "Exists",
    "details": {
        "objectRef": {
            "objectId": "024f81362efe7404119b931bd5daa66e35d4e704",
            "version": 0,
            "digest": "ntEwRW9UM/UNNQkkAuunTtvranJ+3G5xjS7qDhz8A70="
        },
        "object": {
            "contents": {
                "fields": {
                    "id": {
                        "fields": {
                            "id": {
                                "fields": {
                                    "id": {
                                        "fields": {
                                            "bytes": "024f81362efe7404119b931bd5daa66e35d4e704"
                                        },
                                        "type": "0x2::ID::ID"
                                    }
                                },
                                "type": "0x2::ID::UniqueID"
                            },
                            "version": 0
                        },
                        "type": "0x2::ID::VersionedID"
                    },
                    "value": 100000
                },
                "type": "0x2::Coin::Coin<0x2::SUI::SUI>"
            },
            "owner": {
                "AddressOwner": "836f28fce574a84e4135725fdcb549679b09e62d"
            },
            "tx_digest": "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
        }
    }
}

There are still plenty of room for improvement(e.g., simplify the contents field), but that's for a separate PR

@patrickkuo
Copy link
Contributor

Unfortunately this will break RestGatewayClient

sui/sui/src/rest_gateway.rs

Lines 191 to 195 in 72dbf0b

async fn get_object_info(&self, object_id: ObjectID) -> Result<ObjectRead, anyhow::Error> {
Ok(self
.get("object_info", "objectId", &object_id.to_hex())
.await?)
}

The plan was to simplifying the model once the JSON-RPC work is in, we will need to make this changes in GatewayAPI

async fn get_object_info(&self, object_id: ObjectID) -> Result<ObjectRead, anyhow::Error>;

instead of changing the model in the Rest layer, or else the RestGatewayClient implementation won't be able to talk to the rest server.

Also isn't the plan to move to JSON-RPC and remove the Rest server all together? The rest server will be removed soon....

@666lcz
Copy link
Contributor Author

666lcz commented Apr 19, 2022

@patrickkuo thanks for flagging this! The reason I was working on this PR is because I was working on the TypeScript SDK and found ObjectRead difficult to define with TypeScript.

The plan was to simplifying the model once the JSON-RPC work is in, we will need to make this changes in GatewayAPI

This sounds great! I'll then base this PR against pat/jsonrpsee and change on the GatewayAPI level then. What do you think?

Also isn't the plan to move to JSON-RPC and remove the Rest server all together? The rest server will be removed soon

I thought the conclusion is to keep the REST server code and revisit the decision sometime later. However, I am totally okay with remove the REST server code for now if it slows us down. The primary change in this PR is the conversion from ObjectRead to a response object, which can be used for json-rpc as well.

@666lcz 666lcz marked this pull request as draft April 19, 2022 19:06
Base automatically changed from chris/fix-rest-bug to main April 19, 2022 19:52
@666lcz 666lcz force-pushed the chris/object-read branch from b540d25 to 74f8cdd Compare April 19, 2022 21:39
@patrickkuo
Copy link
Contributor

This PR will fix part of this issue #237

@666lcz 666lcz force-pushed the chris/object-read branch from 74f8cdd to a83e0a7 Compare April 19, 2022 22:32
@666lcz 666lcz changed the base branch from main to pat/json-rpc April 19, 2022 22:32
@666lcz 666lcz force-pushed the chris/object-read branch 2 times, most recently from 0baf028 to a1e1cc9 Compare April 19, 2022 22:44
@666lcz 666lcz changed the base branch from pat/json-rpc to main April 19, 2022 22:44
@666lcz 666lcz changed the title [REST Server] Simplify the response schema for object_info [RPC Server] Simplify the response schema for object_info Apr 19, 2022
@666lcz 666lcz marked this pull request as ready for review April 19, 2022 22:46
@666lcz
Copy link
Contributor Author

666lcz commented Apr 19, 2022

@patrickkuo , there're two ways for fixing the issue you mentioned

  1. Change the response at the GatewayAPI level as you suggested
  2. Add a new RPC endpoint to return the "Parsed" object info in addition to the existing get_object_info endpoint.

I ended up implementing Option 2 for the following reasons:

  1. Option 2 takes much less change, so that I can unblock client side work earlier
  2. Right now the RPC server serves two purpose: One is to act as a GatewayAPI(which needs to be compatible with the embedded gateway) for the wallet CLI; and the other is to provide a developer friendly interface to all other external and internal applications. I consider the former to be more barebone and low-level, while the latter is more processed and more developer friendly. It makes sense to provide both a high level API and a low level API.

Let me know if you have any concern.

@patrickkuo
Copy link
Contributor

@patrickkuo , there're two ways for fixing the issue you mentioned

  1. Change the response at the GatewayAPI level as you suggested
  2. Add a new RPC endpoint to return the "Parsed" object info in addition to the existing get_object_info endpoint.

I ended up implementing Option 2 for the following reasons:

  1. Option 2 takes much less change, so that I can unblock client side work earlier
  2. Right now the RPC server serves two purpose: One is to act as a GatewayAPI(which needs to be compatible with the embedded gateway) for the wallet CLI; and the other is to provide a developer friendly interface to all other external and internal applications. I consider the former to be more barebone and low-level, while the latter is more processed and more developer friendly. It makes sense to provide both a high level API and a low level API.

Let me know if you have any concern.

Sounds good to me, let's have this endpoint to unblock others, I will investigate if it make sense to implement option 1 after this.

Copy link
Contributor

@patrickkuo patrickkuo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@666lcz 666lcz enabled auto-merge (squash) April 19, 2022 23:30
@666lcz 666lcz force-pushed the chris/object-read branch from a1e1cc9 to 1f91097 Compare April 19, 2022 23:44
@666lcz 666lcz merged commit d6a9285 into main Apr 20, 2022
@666lcz 666lcz deleted the chris/object-read branch April 20, 2022 00:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants