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

[jsonrpc] - update governance api #8848

Merged
merged 18 commits into from
Mar 9, 2023
Merged

[jsonrpc] - update governance api #8848

merged 18 commits into from
Mar 9, 2023

Conversation

patrickkuo
Copy link
Contributor

@patrickkuo patrickkuo commented Mar 4, 2023

Description

  • adapt get_delegated_stake to the new staking flow
  • grouped all StakedSui by staking pool to reduce duplicate validator info in the response
  • improve ValidatorMetadata readable json response, which affects getSuiSystemState as well.
  • make SUiSystemState response camelCased
  • added --epoch-duration-ms option to sui genesis for configuring localnet epoch duration

Test Plan

Running local network to confirm staking json response

sui_getDelegatedStake response

{
    "jsonrpc": "2.0",
    "result": [
        {
            "validatorAddress": "0x8760b337dcb641811414daff8f98e6824caf7e5ca28530c4248557057ddc9004",
            "stakingPool": "0x628ffd0e51e9a6ea32c13c2739a31a8f344b557d3429e057b377a9c499b9bb13",
            "stakes": [
                {
                    "stakedSuiId": "0xa3cc3319d355dc92afee3669cd8f545de98c5ee380b6e1275b891bebdd82ad28",
                    "stakeRequestEpoch": 3,
                    "stakeActiveEpoch": 4,
                    "principal": 99999999998977,
                    "tokenLock": null,
                    "status": "Active",
                    "estimatedReward": 998
                }
            ]
        },
        {
            "validatorAddress": "0x6b34f8d6d70676db526017b03ec35f8f74ec67ee10426e2a3049a42045c90913",
            "stakingPool": "0xd5d9aa879b78dc1f516d71ab979189086eff752f65e4b0dea15829e3157962e1",
            "stakes": [
                {
                    "stakedSuiId": "0x5ee438610276e8fcfe0c0615caf4a2ca7c408569f47c5a622be58863c35b357b",
                    "stakeRequestEpoch": 2,
                    "stakeActiveEpoch": 3,
                    "principal": 100000000000000,
                    "tokenLock": null,
                    "status": "Active",
                    "estimatedReward": 1998
                },
                {
                    "stakedSuiId": "0x9eac8bd615977f8d635dc2054d13c463829161be41425d701ba8f9a444ca69e9",
                    "stakeRequestEpoch": 5,
                    "stakeActiveEpoch": 6,
                    "principal": 100000000000000,
                    "tokenLock": null,
                    "status": "Pending"
                }
            ]
        }
    ],
    "id": 1
}

sui_getLatestSystemState response

{
    "jsonrpc": "2.0",
    "result": {
        "epoch": 5,
        "protocolVersion": 1,
        "storageFund": 0,
        "referenceGasPrice": 1,
        "safeMode": false,
        "epochStartTimestampMs": 1678368858178,
        "minValidatorStake": 25000000000000000,
        "maxValidatorCount": 100,
        "governanceStartEpoch": 0,
        "stakeSubsidyEpochCounter": 5,
        "stakeSubsidyBalance": 899999999995000000,
        "stakeSubsidyCurrentEpochAmount": 1000000,
        "totalStake": 100000000005000000,
        "activeValidators": [
            {
                "suiAddress": "0x2a18d74abf2de7860f85a0d9da68bd77014fc0bbfd3263ac318b76b69c2f5192",
                "protocolPubkeyBytes": "tC1wNntCBnG92S3tURsqKa22hCGjc2bPcE46NSxSqs6tEedAKWH5qVfPF87LiNRRFxsLDzK2Yu6nPCnZjtS271nH7tCKDWF9zEH9n8Zc5KdMaeCh3c7SyUpxMZtiVXowgyN",
                "networkPubkeyBytes": "9Su1CHDxyefb2qTfsWjfo2U8KqyrQxteJ6kuCAeXkDNr",
                "workerPubkeyBytes": "EeAcLcFCPoe2ALaB48F2L9utWUMwq6a1VgrZWLqyBjF1",
                "proofOfPossessionBytes": "6cjHQsZxLDHydVGFc6uVuF14dm5FnYAXrmjrM8hNSddg4cUwg5hcjwG7Hs4SRTBLt7",
                "name": "validator-3",
                "description": "",
                "imageUrl": "",
                "projectUrl": "",
                "netAddress": "/ip4/127.0.0.1/tcp/65169/http",
                "p2pAddress": "/ip4/127.0.0.1/udp/65171",
                "primaryAddress": "/ip4/127.0.0.1/udp/65177",
                "workerAddress": "/ip4/127.0.0.1/udp/65179",
                "nextEpochProtocolPubkeyBytes": null,
                "nextEpochProofOfPossession": null,
                "nextEpochNetworkPubkeyBytes": null,
                "nextEpochWorkerPubkeyBytes": null,
                "nextEpochNetAddress": null,
                "nextEpochP2pAddress": null,
                "nextEpochPrimaryAddress": null,
                "nextEpochWorkerAddress": null,
                "votingPower": 2500,
                "gasPrice": 1,
                "commissionRate": 0,
                "nextEpochStake": 25000000001250000,
                "nextEpochGasPrice": 1,
                "nextEpochCommissionRate": 0,
                "stakingPoolId": "0xd5d9aa879b78dc1f516d71ab979189086eff752f65e4b0dea15829e3157962e1",
                "stakingPoolActivationEpoch": 0,
                "stakingPoolDeactivationEpoch": null,
                "stakingPoolSuiBalance": 25000000001250000,
                "rewardsPool": 1250000,
                "poolTokenBalance": 25000000000000000,
                "pendingDelegation": 0,
                "pendingTotalSuiWithdraw": 0,
                "pendingPoolTokenWithdraw": 0,
                "exchangeRatesId": "0xe63945cec193ca7ac76960370028110a907ad1c22ff90b1fd57ea3c441c766b4",
                "exchangeRatesSize": 6
            },
            {
                "suiAddress": "0xebac4cfcd5ffb630dfef51fa6452b6382c3e12f3f3e1f529071fd8c6dbd72429",
                "protocolPubkeyBytes": "tfXdQjF9kB2vSQZHKGxpeVixhVgGMYwWcGpmMBhDvaNzKiH9XgDzrh8upua8oUA1Y4QsQPE7JTUFnN153nJKUxevcQQMXdZqbUd9TKYuhAs2Qzd8mFmCWy3pChGHrQBHKNy",
                "networkPubkeyBytes": "3ZjLJ1rEYM2Df2b4PsTmXdJakieKEBXv7CeamWuVu6py",
                "workerPubkeyBytes": "2QXqXwnn25koEwriDwbDQ378cgexHNqnKFUBu78DG7b6",
                "proofOfPossessionBytes": "5vehDvA2xp1pQDpdeTfn91cty2FyNVYzfRzQ4bFaYKWEjgcy44iSnXcEfvXvYktqxX",
                "name": "validator-0",
                "description": "",
                "imageUrl": "",
                "projectUrl": "",
                "netAddress": "/ip4/127.0.0.1/tcp/65127/http",
                "p2pAddress": "/ip4/127.0.0.1/udp/65129",
                "primaryAddress": "/ip4/127.0.0.1/udp/65135",
                "workerAddress": "/ip4/127.0.0.1/udp/65137",
                "nextEpochProtocolPubkeyBytes": null,
                "nextEpochProofOfPossession": null,
                "nextEpochNetworkPubkeyBytes": null,
                "nextEpochWorkerPubkeyBytes": null,
                "nextEpochNetAddress": null,
                "nextEpochP2pAddress": null,
                "nextEpochPrimaryAddress": null,
                "nextEpochWorkerAddress": null,
                "votingPower": 2500,
                "gasPrice": 1,
                "commissionRate": 0,
                "nextEpochStake": 25000000001250000,
                "nextEpochGasPrice": 1,
                "nextEpochCommissionRate": 0,
                "stakingPoolId": "0x628ffd0e51e9a6ea32c13c2739a31a8f344b557d3429e057b377a9c499b9bb13",
                "stakingPoolActivationEpoch": 0,
                "stakingPoolDeactivationEpoch": null,
                "stakingPoolSuiBalance": 25000000001250000,
                "rewardsPool": 1250000,
                "poolTokenBalance": 25000000000000000,
                "pendingDelegation": 0,
                "pendingTotalSuiWithdraw": 0,
                "pendingPoolTokenWithdraw": 0,
                "exchangeRatesId": "0x18b8140c8a6ea340142f128061016fc4bd8220ec88875a8e573a5f72a85a15c1",
                "exchangeRatesSize": 6
            },
            {
                "suiAddress": "0x0759a534294282bceb2e9f9c111f97ed50bb41355e2af3cd42ff76f86cb5a7e9",
                "protocolPubkeyBytes": "zZegkZETxUyUhui46dAwnLneKPpvboCrPDHzuPEYPiQr82c2iNxuj8BaopKXQ3Zvnb2sVzmVRSu8SDd7jP76iuA8zEcFeurYjvcMUQ8MFox8ERuSSn3WhjQLv48zdTLBj4a",
                "networkPubkeyBytes": "HCMWqGnxPwcPZW7S5c5zXJJV8pdfYw7LGY5Umc4fjo2V",
                "workerPubkeyBytes": "4T2v2xR89dxhmMPyqG1sE2ibFesz1E4RPn7W8SHs9dLh",
                "proofOfPossessionBytes": "7cfZpx3WnjArFZ46p6cEjZzbzFQC5QW5s2stDLb5YKaXxnczaHQDjoFVU344FS44Sx",
                "name": "validator-1",
                "description": "",
                "imageUrl": "",
                "projectUrl": "",
                "netAddress": "/ip4/127.0.0.1/tcp/65141/http",
                "p2pAddress": "/ip4/127.0.0.1/udp/65143",
                "primaryAddress": "/ip4/127.0.0.1/udp/65149",
                "workerAddress": "/ip4/127.0.0.1/udp/65151",
                "nextEpochProtocolPubkeyBytes": null,
                "nextEpochProofOfPossession": null,
                "nextEpochNetworkPubkeyBytes": null,
                "nextEpochWorkerPubkeyBytes": null,
                "nextEpochNetAddress": null,
                "nextEpochP2pAddress": null,
                "nextEpochPrimaryAddress": null,
                "nextEpochWorkerAddress": null,
                "votingPower": 2500,
                "gasPrice": 1,
                "commissionRate": 0,
                "nextEpochStake": 25000000001250000,
                "nextEpochGasPrice": 1,
                "nextEpochCommissionRate": 0,
                "stakingPoolId": "0x6d3ffc5213ed4df6802cd4535d3c18f66d85bab5e9f004a7087a005e2f8dc455",
                "stakingPoolActivationEpoch": 0,
                "stakingPoolDeactivationEpoch": null,
                "stakingPoolSuiBalance": 25000000001250000,
                "rewardsPool": 1250000,
                "poolTokenBalance": 25000000000000000,
                "pendingDelegation": 0,
                "pendingTotalSuiWithdraw": 0,
                "pendingPoolTokenWithdraw": 0,
                "exchangeRatesId": "0x9dc97c4be6f1d0e61fbdf2cc9d78f9df04cdff7a473e2ec3a65853766dca1177",
                "exchangeRatesSize": 6
            },
            {
                "suiAddress": "0x8d35d589197cb153f21c115090163f54a5fb10d0449ac8acd85fca27be229983",
                "protocolPubkeyBytes": "22iMQy5CjHNR3erxo76fTRk7fnxVjHARfsWr9iLkCrbSdUCBSVhMC2QEDiFjGvPimhfmqzFG6Z6tLoY6GNN8yxgckEDKdRYeDGsKYHXyQesBWWyKeazGpXBroxecppCLyoSu",
                "networkPubkeyBytes": "AzHGVoDxW2rawqV7E1ypfgu6gGXDmc7q4244sw3m9kaS",
                "workerPubkeyBytes": "9dt6yMm3eUDMqUV8bpXkfhVNWVeWEvtEQkBNRYHJ1heT",
                "proofOfPossessionBytes": "6w3NuyoHoeW4npKBYTscUAcAEwQ7mryckW2tWAvfuhyxNHWzPjcggscF7EV7mLi5zU",
                "name": "validator-2",
                "description": "",
                "imageUrl": "",
                "projectUrl": "",
                "netAddress": "/ip4/127.0.0.1/tcp/65155/http",
                "p2pAddress": "/ip4/127.0.0.1/udp/65157",
                "primaryAddress": "/ip4/127.0.0.1/udp/65163",
                "workerAddress": "/ip4/127.0.0.1/udp/65165",
                "nextEpochProtocolPubkeyBytes": null,
                "nextEpochProofOfPossession": null,
                "nextEpochNetworkPubkeyBytes": null,
                "nextEpochWorkerPubkeyBytes": null,
                "nextEpochNetAddress": null,
                "nextEpochP2pAddress": null,
                "nextEpochPrimaryAddress": null,
                "nextEpochWorkerAddress": null,
                "votingPower": 2500,
                "gasPrice": 1,
                "commissionRate": 0,
                "nextEpochStake": 25000000001250000,
                "nextEpochGasPrice": 1,
                "nextEpochCommissionRate": 0,
                "stakingPoolId": "0x01b3b1dd18a3b775fe0e0d4b873c0aa0d0cd2acfecd309f83140a19fdfea51fa",
                "stakingPoolActivationEpoch": 0,
                "stakingPoolDeactivationEpoch": null,
                "stakingPoolSuiBalance": 25000000001250000,
                "rewardsPool": 1250000,
                "poolTokenBalance": 25000000000000000,
                "pendingDelegation": 0,
                "pendingTotalSuiWithdraw": 0,
                "pendingPoolTokenWithdraw": 0,
                "exchangeRatesId": "0x1e80f25048bf1274dbe7f4cbbe4abfd75b911faaa14e9297cccd0d8bdb4ad143",
                "exchangeRatesSize": 6
            }
        ],
        "pendingActiveValidatorsId": "0x36f2a5e23e3707da1fa5cb373449bbed4d76944233eb364e9eca4a759a6eff5a",
        "pendingActiveValidatorsSize": 0,
        "pendingRemovals": [],
        "stakingPoolMappingsId": "0x3e7ceb09c198b80953d3e1869ba4dbc63201193fa32c6393ac8e4c41aed6620f",
        "stakingPoolMappingsSize": 4,
        "inactivePoolsId": "0x0fe8a01f3bee6806293987fc588a1c6caa898c7037cdf72b5f07dce8170d7160",
        "inactivePoolsSize": 0,
        "validatorCandidatesId": "0x81d51f48e5dfc02dbc8f6003517274f7ef2250a243d4d7133e4a6e35e73a8d8b",
        "validatorCandidatesSize": 0,
        "validatorReportRecords": []
    },
    "id": 1
}

@vercel
Copy link

vercel bot commented Mar 4, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated
explorer ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 9, 2023 at 9:01PM (UTC)
explorer-storybook ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 9, 2023 at 9:01PM (UTC)
wallet-adapter ✅ Ready (Inspect) Visit Preview 💬 Add your feedback Mar 9, 2023 at 9:01PM (UTC)

@@ -1952,6 +1953,20 @@ impl AuthorityState {
}
}

pub async fn read_table<K, V>(&self, table: &Table) -> SuiResult<BTreeMap<K, V>>
Copy link
Contributor

Choose a reason for hiding this comment

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

We will need a bound on the number of results?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have changed read_table to read_table_value, which use a iterator to find the value instead of reading the whole table in mem.

@666lcz 666lcz force-pushed the pat/staking_api branch from 4bc4dd6 to fb698f5 Compare March 5, 2023 03:37
#[derive(Clone, Serialize, Deserialize, JsonSchema, Debug)]
#[serde(rename_all = "camelCase")]
pub struct DynamicFieldInfo {
pub name: DynamicFieldName,
#[schemars(with = "Base58")]
#[serde_as(as = "Readable<Base58, _>")]
pub bcs_name: Vec<u8>,
Copy link
Contributor

Choose a reason for hiding this comment

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

n00b q: what is this used for?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the name of the dynamic field in bcs, DynamicFieldName is the parsed json version of this.

This is added so we can use it in get_table_value to get the bcs name without reading the Field object.

pub async fn read_table_value<K, V>(&self, table: &Table, key: &K) -> Option<V>
where
K: DeserializeOwned + Serialize,
V: DeserializeOwned,
{
let key_bcs = bcs::to_bytes(key).ok()?;
let df = self
.get_dynamic_fields_iterator(table.id, None)
.ok()?
.find(|df| key_bcs == df.bcs_name)?;
let field: Field<K, V> = self.get_move_object(&df.object_id).await.ok()?;
Some(field.value)
}

This will also be useful for client with bcs support where they can use the bcs bytes instead of the parsed json dynamic field name.

@vercel vercel bot temporarily deployed to Preview – wallet-adapter March 5, 2023 05:02 Inactive
@vercel vercel bot temporarily deployed to Preview – explorer-storybook March 5, 2023 05:04 Inactive
.get_move_objects(owner, &Delegation::type_())
.await?)

async fn get_delegated_stakes(&self, owner: SuiAddress) -> Result<Vec<DelegatedStake>, Error> {
Copy link
Contributor

@666lcz 666lcz Mar 5, 2023

Choose a reason for hiding this comment

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

I am trying to understand the delta between the previous return type vs the current return type:

  1. Previously the return type was "flat": a list of DelegatedStake where there could be multiple DelegatedStake for the same validator.
  2. This PR aggregates the result by the validator address. It still returns a list of DelegatedStake, but there will only be exactly one DelegatedStake for the same validator. Each DelegatedStake now contains a list of Delegation objects that share the same validator.

This change makes it a bit involved to change on the wallet side since there are a lot of places in the wallet code that has strong assumptions(example) on this and it would take us some time to fully understand the wallet logic and make the right changes.

cc @Jibz-Mysten @Jordan-Mysten it would be great if we could get some help from the FE team to advise on these changes. Thanks!

Copy link
Contributor

Choose a reason for hiding this comment

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

@666lcz I think these changes are fine. I am working on staking changes this week, I can make this update on the wallet site.

@lxfind
Copy link
Contributor

lxfind commented Mar 5, 2023

One thing worth discussing is how do we plan to evolve all these types.
I was initially thinking that eventually we need to introduce a new RPC API to get a flatten return type that's easier to evolve (less layers and hence number of data structures to evolve).

@666lcz 666lcz force-pushed the pat/staking_api branch from 24bb4c4 to 9f7674a Compare March 5, 2023 05:48
@vercel vercel bot temporarily deployed to Preview – wallet-adapter March 5, 2023 05:48 Inactive
@vercel vercel bot temporarily deployed to Preview – explorer-storybook March 5, 2023 05:49 Inactive
@666lcz 666lcz force-pushed the pat/staking_api branch from 9f7674a to acb7f87 Compare March 5, 2023 05:51
@vercel vercel bot temporarily deployed to Preview – explorer-storybook March 5, 2023 05:52 Inactive
@vercel vercel bot temporarily deployed to Preview – wallet-adapter March 5, 2023 05:52 Inactive
@666lcz 666lcz force-pushed the pat/staking_api branch from acb7f87 to cf5c208 Compare March 5, 2023 05:53
@vercel vercel bot temporarily deployed to Preview – wallet-adapter March 5, 2023 05:54 Inactive
@vercel vercel bot temporarily deployed to Preview – explorer-storybook March 5, 2023 05:54 Inactive
@666lcz 666lcz force-pushed the pat/staking_api branch from cf5c208 to 417d448 Compare March 5, 2023 06:15
@vercel vercel bot temporarily deployed to Preview – wallet-adapter March 5, 2023 06:15 Inactive
@vercel vercel bot temporarily deployed to Preview – explorer-storybook March 5, 2023 06:16 Inactive
patrickkuo and others added 17 commits March 9, 2023 19:39
improve get_sui_system_state json response
* change read_table to read_table_value to avoid reading the whole table in memory
## 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

---------

Co-authored-by: patrick <[email protected]>
Co-authored-by: Chris Li <[email protected]>
@vercel vercel bot temporarily deployed to Preview – explorer-storybook March 9, 2023 19:52 Inactive
@vercel vercel bot temporarily deployed to Preview – wallet-adapter March 9, 2023 19:53 Inactive
@patrickkuo patrickkuo enabled auto-merge (squash) March 9, 2023 20:09
@vercel vercel bot temporarily deployed to Preview – explorer-storybook March 9, 2023 21:00 Inactive
@vercel vercel bot temporarily deployed to Preview – wallet-adapter March 9, 2023 21:01 Inactive
@patrickkuo patrickkuo merged commit acc2edb into main Mar 9, 2023
@patrickkuo patrickkuo deleted the pat/staking_api branch March 9, 2023 21:19
@@ -356,6 +356,19 @@ pub struct StakingPoolV1 {
pub pending_pool_token_withdraw: u64,
}

#[derive(Debug, Serialize, Deserialize, Clone, Eq, PartialEq)]
pub struct PoolTokenExchangeRate {
Copy link
Contributor

Choose a reason for hiding this comment

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

This is no longer used, right?

let inactive_validators = system_state.inactive_pools_id;
let inactive_validators = self
.state
.read_table_value::<ObjectID, ValidatorV1>(inactive_validators, pool_id)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not going to work once we have a new version for the Validator type.
There are two ways to go about this:

  1. Rely on SuiSystemStateSummary exclusively.
  2. Add a trait function to SuiSystemStateTrait to get some basic info to construct this, but then be mindful that that API will have to be respected for all future versions

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.

6 participants