From c7e8b00b5a05e48e03c8dd8de0a5d9ab22d6c5cc Mon Sep 17 00:00:00 2001 From: Jordan Gensler Date: Mon, 27 Feb 2023 10:50:47 -0800 Subject: [PATCH] Remove legacy encoding for validator metadata (#8640) --- .../top-validators-card/TopValidatorsCard.tsx | 9 +- .../components/validator/ValidatorMeta.tsx | 25 +-- .../src/pages/validator/ValidatorDataTypes.ts | 174 ------------------ .../src/pages/validators/Validators.tsx | 9 +- apps/explorer/src/utils/getName.ts | 23 --- .../src/ui/app/staking/useSystemState.tsx | 19 -- .../validators/SelectValidatorCard.tsx | 10 +- .../app/staking/validators/ValidatorLogo.tsx | 11 +- .../src/components/Assignment/index.tsx | 4 +- .../src/components/Validators/Validator.tsx | 9 +- .../src/components/your-score/Table.tsx | 12 +- .../src/network/queries/sui-system.ts | 6 - dapps/frenemies/src/routes/Home.tsx | 8 +- sdk/typescript/src/types/validator.ts | 9 +- 14 files changed, 32 insertions(+), 296 deletions(-) delete mode 100644 apps/explorer/src/pages/validator/ValidatorDataTypes.ts delete mode 100644 apps/explorer/src/utils/getName.ts diff --git a/apps/explorer/src/components/top-validators-card/TopValidatorsCard.tsx b/apps/explorer/src/components/top-validators-card/TopValidatorsCard.tsx index f9eb011a9a25e..47273bf2658c3 100644 --- a/apps/explorer/src/components/top-validators-card/TopValidatorsCard.tsx +++ b/apps/explorer/src/components/top-validators-card/TopValidatorsCard.tsx @@ -15,24 +15,19 @@ import { Link } from '~/ui/Link'; import { PlaceholderTable } from '~/ui/PlaceholderTable'; import { TableCard } from '~/ui/TableCard'; import { Text } from '~/ui/Text'; -import { getName } from '~/utils/getName'; const NUMBER_OF_VALIDATORS = 10; export function processValidators(set: Validator[]) { return set.map((av) => { - const rawName = av.metadata.name; const delegatedStake = +av.delegation_staking_pool.sui_balance; const selfStake = +av.stake_amount; const totalValidatorStake = selfStake + delegatedStake; return { - name: getName(rawName), + name: av.metadata.name, address: av.metadata.sui_address, stake: totalValidatorStake, - logo: - typeof av.metadata.image_url === 'string' - ? av.metadata.image_url - : null, + logo: av.metadata.image_url, }; }); } diff --git a/apps/explorer/src/components/validator/ValidatorMeta.tsx b/apps/explorer/src/components/validator/ValidatorMeta.tsx index c4ebd2c8c284d..0bf2b46616936 100644 --- a/apps/explorer/src/components/validator/ValidatorMeta.tsx +++ b/apps/explorer/src/components/validator/ValidatorMeta.tsx @@ -11,37 +11,20 @@ import { Heading } from '~/ui/Heading'; import { ImageIcon } from '~/ui/ImageIcon'; import { AddressLink } from '~/ui/InternalLink'; import { Text } from '~/ui/Text'; -import { getName } from '~/utils/getName'; type ValidatorMetaProps = { validatorData: Validator; }; export function ValidatorMeta({ validatorData }: ValidatorMetaProps) { - const validatorName = getName(validatorData.metadata.name); - const validatorPublicKey = toB64( new Uint8Array(validatorData.metadata.pubkey_bytes) ); - // NOTE: We only support the string-encoded metadata fields, which will become the only encoding soon: - const logo = - !validatorData.metadata.image_url || - typeof validatorData.metadata.image_url !== 'string' - ? null - : validatorData.metadata.image_url; - - const description = - !validatorData.metadata.description || - typeof validatorData.metadata.description !== 'string' - ? null - : validatorData.metadata.description; - - const projectUrl = - !validatorData.metadata.project_url || - typeof validatorData.metadata.project_url !== 'string' - ? null - : validatorData.metadata.project_url; + const validatorName = validatorData.metadata.name; + const logo = validatorData.metadata.image_url; + const description = validatorData.metadata.description; + const projectUrl = validatorData.metadata.project_url; return ( <> diff --git a/apps/explorer/src/pages/validator/ValidatorDataTypes.ts b/apps/explorer/src/pages/validator/ValidatorDataTypes.ts deleted file mode 100644 index 51ef46984dee1..0000000000000 --- a/apps/explorer/src/pages/validator/ValidatorDataTypes.ts +++ /dev/null @@ -1,174 +0,0 @@ -// Copyright (c) Mysten Labs, Inc. -// SPDX-License-Identifier: Apache-2.0 - -//TODO pull from the SDK -export const VALDIATOR_NAME = /^[A-Z-_.\s0-9]+$/i; - -export interface Validators { - dataType: string; - type: string; - has_public_transfer: boolean; - fields: ValidatorsFields; -} - -export interface ValidatorsFields { - chain_id: number; - epoch: string; - id: ID; - parameters: Parameters; - reference_gas_price: string; - stake_subsidy: StakeSubsidy; - storage_fund: string; - sui_supply: Supply; - validator_report_records: ValidatorReportRecords; - validators: ValidatorsClass; -} - -export interface ID { - id: string; -} - -export interface Parameters { - type: string; - fields: ParametersFields; -} - -export interface ParametersFields { - max_validator_candidate_count: string; - min_validator_stake: string; - storage_gas_price: string; -} - -export interface StakeSubsidy { - type: string; - fields: StakeSubsidyFields; -} - -export interface StakeSubsidyFields { - balance: Value; - current_epoch_amount: number; - epoch_counter: number; -} - -export interface Supply { - type: string; - fields: SuiSupplyFields; -} - -export interface SuiSupplyFields { - value: number; -} - -export interface ValidatorReportRecords { - type: string; - fields: ValidatorReportRecordsFields; -} - -export interface ValidatorReportRecordsFields { - contents: any[]; -} - -export interface ValidatorsClass { - type: string; - fields: ValidatorsFieldsClass; -} - -export interface ValidatorsFieldsClass { - active_validators: ActiveValidator[]; - next_epoch_validators: NextEpochValidator[]; - pending_delegation_switches: ValidatorReportRecords; - pending_removals: number[]; - pending_validators: number[]; - quorum_stake_threshold: string; - total_delegation_stake: string; - total_validator_stake: string; -} - -export interface ActiveValidator { - type: string; - fields: ActiveValidatorFields; -} - -export interface ActiveValidatorFields { - commission_rate: string; - delegation_staking_pool: DelegationStakingPool; - gas_price: string; - metadata: NextEpochValidator; - pending_stake: string; - pending_withdraw: string; - stake_amount: string; - voting_power: string | null; -} - -export interface DelegationStakingPool { - type: string; - fields: DelegationStakingPoolFields; -} - -export interface DelegationStakingPoolFields { - delegation_token_supply: SuiSupplyFields; - pending_delegations: ContentsFields; - pending_withdraws: PendingDelegationsFields; - rewards_pool: Value; - starting_epoch: number; - sui_balance: number; - validator_address: string; -} - -export interface Value { - value: number; -} - -export interface Pending { - type: string; - fields: PendingDelegationsFields; -} - -export interface PendingDelegationsFields { - contents: ContentsFieldsWithdraw; -} - -export interface ContentsFieldsWithdraw { - id: string; - size: number; -} - -export interface Contents { - type: string; - fields: ContentsFields; -} - -export interface ContentsFields { - id: string; - size: number; - head: Vector; - tail: Vector; -} - -export interface Vector { - vec: any[]; -} - -export interface NextEpochValidator { - type: string; - fields: NextEpochValidatorFields; -} - -export interface NextEpochValidatorFields { - consensus_address: number[]; - name: number[] | string; - image_url?: number[] | string | null; - description?: number[] | string | null; - project_url?: number[] | string | null; - net_address: number[]; - network_pubkey_bytes: number[]; - next_epoch_commission_rate: string; - next_epoch_delegation: string; - next_epoch_gas_price: string; - next_epoch_stake: string; - proof_of_possession: number[]; - pubkey_bytes: number[]; - sui_address: string; - worker_address: number[]; - worker_pubkey_bytes: number[]; -} diff --git a/apps/explorer/src/pages/validators/Validators.tsx b/apps/explorer/src/pages/validators/Validators.tsx index 99c9e9efe569e..ecc85fae9c10d 100644 --- a/apps/explorer/src/pages/validators/Validators.tsx +++ b/apps/explorer/src/pages/validators/Validators.tsx @@ -20,7 +20,6 @@ import { Stats } from '~/ui/Stats'; import { TableCard } from '~/ui/TableCard'; import { TableHeader } from '~/ui/TableHeader'; import { Text } from '~/ui/Text'; -import { getName } from '~/utils/getName'; import { getValidatorMoveEvent } from '~/utils/getValidatorMoveEvent'; import { roundFloat } from '~/utils/roundFloat'; @@ -35,16 +34,12 @@ function validatorsTableData( ) { return { data: validators.map((validator) => { - const validatorName = getName(validator.metadata.name); + const validatorName = validator.metadata.name; const delegatedStake = +validator.delegation_staking_pool.sui_balance; const selfStake = +validator.stake_amount; const totalStake = selfStake + delegatedStake; - const img = - validator.metadata.image_url && - typeof validator.metadata.image_url === 'string' - ? validator.metadata.image_url - : null; + const img = validator.metadata.image_url; const event = getValidatorMoveEvent( validatorsEvents, diff --git a/apps/explorer/src/utils/getName.ts b/apps/explorer/src/utils/getName.ts deleted file mode 100644 index bb6a51b1f8a4f..0000000000000 --- a/apps/explorer/src/utils/getName.ts +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) Mysten Labs, Inc. -// SPDX-License-Identifier: Apache-2.0 - -import { fromB64 } from '@mysten/sui.js'; - -// TODO : Import from SDK -import { VALDIATOR_NAME } from '~/pages/validator/ValidatorDataTypes'; - -const textDecoder = new TextDecoder(); - -export function getName(rawName: string | number[]) { - let name: string; - - if (Array.isArray(rawName)) { - name = String.fromCharCode(...rawName); - } else { - name = textDecoder.decode(fromB64(rawName)); - if (!VALDIATOR_NAME.test(name)) { - name = rawName; - } - } - return name; -} diff --git a/apps/wallet/src/ui/app/staking/useSystemState.tsx b/apps/wallet/src/ui/app/staking/useSystemState.tsx index 0f8d58260f46a..fba352a6eb58e 100644 --- a/apps/wallet/src/ui/app/staking/useSystemState.tsx +++ b/apps/wallet/src/ui/app/staking/useSystemState.tsx @@ -1,29 +1,10 @@ // Copyright (c) Mysten Labs, Inc. // SPDX-License-Identifier: Apache-2.0 -import { fromB64 } from '@mysten/sui.js'; import { useQuery } from '@tanstack/react-query'; import { useRpc } from '../hooks'; -export const VALDIATOR_NAME = /^[A-Z-_.\s0-9]+$/i; - -const textDecoder = new TextDecoder(); - -export function getName(rawName: string | number[]) { - let name: string; - - if (Array.isArray(rawName)) { - name = String.fromCharCode(...rawName); - } else { - name = textDecoder.decode(fromB64(rawName)); - if (!VALDIATOR_NAME.test(name)) { - name = rawName; - } - } - return name; -} - export function useSystemState() { const rpc = useRpc(); return useQuery(['system', 'state'], () => rpc.getSuiSystemState()); diff --git a/apps/wallet/src/ui/app/staking/validators/SelectValidatorCard.tsx b/apps/wallet/src/ui/app/staking/validators/SelectValidatorCard.tsx index 06714485046a2..0fe1cbb08fe7f 100644 --- a/apps/wallet/src/ui/app/staking/validators/SelectValidatorCard.tsx +++ b/apps/wallet/src/ui/app/staking/validators/SelectValidatorCard.tsx @@ -6,7 +6,7 @@ import { useState, useMemo } from 'react'; import { calculateAPY } from '../calculateAPY'; import { calculateStakeShare } from '../calculateStakeShare'; -import { getName, useSystemState } from '../useSystemState'; +import { useSystemState } from '../useSystemState'; import { ValidatorListItem } from './ValidatorListItem'; import { Content, Menu } from '_app/shared/bottom-menu-layout'; import Button from '_app/shared/button'; @@ -57,7 +57,7 @@ export function SelectValidatorCard() { const sortedAsc = data.validators.active_validators .map((validator) => ({ - name: getName(validator.metadata.name), + name: validator.metadata.name, address: validator.metadata.sui_address, apy: calculateAPY(validator, +data.epoch), stakeShare: calculateStakeShare( @@ -65,11 +65,7 @@ export function SelectValidatorCard() { BigInt(validator.stake_amount), BigInt(totalStake) ), - logo: - validator.metadata.image_url && - typeof validator.metadata.image_url === 'string' - ? validator.metadata.image_url - : null, + logo: validator.metadata.image_url, })) .sort((a, b) => { if (sortKey === 'name') { diff --git a/apps/wallet/src/ui/app/staking/validators/ValidatorLogo.tsx b/apps/wallet/src/ui/app/staking/validators/ValidatorLogo.tsx index e471bccf453c8..4fdd7f071fb0a 100644 --- a/apps/wallet/src/ui/app/staking/validators/ValidatorLogo.tsx +++ b/apps/wallet/src/ui/app/staking/validators/ValidatorLogo.tsx @@ -6,7 +6,6 @@ import cl from 'classnames'; import { useMemo } from 'react'; import { useGetValidatorMetaData } from '../useGetDelegatedStake'; -import { getName } from '../useSystemState'; import { Heading } from '_app/shared/heading'; import { ImageIcon } from '_app/shared/image-icon'; import { Text } from '_app/shared/text'; @@ -40,15 +39,11 @@ export function ValidatorLogo({ ); if (!validator) return null; - const logo = - (validator.image_url && typeof validator.image_url === 'string') || - Array.isArray(validator.image_url) - ? validator.image_url - : null; + const logo = validator.image_url; return { - name: getName(validator.name), - logo: logo && getName(logo), + name: validator.name, + logo: logo, }; }, [validatorAddress, validatorsData]); diff --git a/dapps/frenemies/src/components/Assignment/index.tsx b/dapps/frenemies/src/components/Assignment/index.tsx index 3dbdfe4b8c52f..d307f0de23256 100644 --- a/dapps/frenemies/src/components/Assignment/index.tsx +++ b/dapps/frenemies/src/components/Assignment/index.tsx @@ -2,7 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 import { useScorecard } from "../../network/queries/scorecard"; -import { convertToString, useValidators } from "../../network/queries/sui-system"; +import { useValidators } from "../../network/queries/sui-system"; import { Goal } from "../../network/types"; import { formatBalance } from "../../utils/format"; import { Card } from "../Card"; @@ -59,7 +59,7 @@ export function Assignment() { if (!assignedValidator) return null; - const name = convertToString(assignedValidator.name); + const name = assignedValidator.name; const selfStake = BigInt(assignedValidator.next_epoch_stake); const delegatedStake = BigInt(assignedValidator.next_epoch_delegation); const totalStake = selfStake + delegatedStake; diff --git a/dapps/frenemies/src/components/Validators/Validator.tsx b/dapps/frenemies/src/components/Validators/Validator.tsx index eafc8cb24515e..6a7d8073da66c 100644 --- a/dapps/frenemies/src/components/Validators/Validator.tsx +++ b/dapps/frenemies/src/components/Validators/Validator.tsx @@ -5,7 +5,6 @@ import { ValidatorMetaData } from "@mysten/sui.js"; import clsx from "clsx"; import { FormEvent, useState } from "react"; import { useScorecard } from "../../network/queries/scorecard"; -import { convertToString } from "../../network/queries/sui-system"; import { ObjectData } from "../../network/rawObject"; import { Delegation, StakedSui } from "../../network/types"; import { formatBalance } from "../../utils/format"; @@ -51,15 +50,15 @@ export function ValidatorItem({ index, validator, stake, delegation }: Props) {
- {convertToString(validator.name)} + {validator.name}
diff --git a/dapps/frenemies/src/components/your-score/Table.tsx b/dapps/frenemies/src/components/your-score/Table.tsx index 4fd74e790202d..21c47805d711d 100644 --- a/dapps/frenemies/src/components/your-score/Table.tsx +++ b/dapps/frenemies/src/components/your-score/Table.tsx @@ -6,10 +6,7 @@ import { ReactNode } from "react"; import { ROUND_OFFSET } from "../../config"; import { useScorecard } from "../../network/queries/scorecard"; import { useScorecardHistory } from "../../network/queries/scorecard-history"; -import { - convertToString, - useValidators, -} from "../../network/queries/sui-system"; +import { useValidators } from "../../network/queries/sui-system"; import { Leaderboard, ScorecardUpdatedEvent } from "../../network/types"; import { formatGoal } from "../../utils/format"; import { Logo } from "../Validators/Logo"; @@ -86,13 +83,12 @@ export function Table({ data, round, leaderboard }: Props) {
- {convertToString(validatorMeta?.name) || - formatAddress(validator)} + {validatorMeta?.name || formatAddress(validator)}
{evt.epochScore !== 0 ? "Achieved" : "Failed"} diff --git a/dapps/frenemies/src/network/queries/sui-system.ts b/dapps/frenemies/src/network/queries/sui-system.ts index fcad5dd7f27b6..3a0f727948293 100644 --- a/dapps/frenemies/src/network/queries/sui-system.ts +++ b/dapps/frenemies/src/network/queries/sui-system.ts @@ -11,12 +11,6 @@ import provider from "../provider"; */ export const SUI_SYSTEM_ID: string = normalizeSuiAddress("0x5"); -export function convertToString(raw?: string | number[] | null) { - if (!raw) return null; - if (typeof raw === 'string') return raw; - return String.fromCharCode(...raw); -} - export function useValidators() { return useQuery( ["validators"], diff --git a/dapps/frenemies/src/routes/Home.tsx b/dapps/frenemies/src/routes/Home.tsx index 1005e8614a509..9a931b3daa1de 100644 --- a/dapps/frenemies/src/routes/Home.tsx +++ b/dapps/frenemies/src/routes/Home.tsx @@ -16,7 +16,7 @@ import { Goal } from "../network/types"; import { Assignment } from "../components/Assignment"; import { Logo } from "../components/Validators/Logo"; import { Refresh } from "../components/your-score/Refresh"; -import { convertToString, useValidators } from "../network/queries/sui-system"; +import { useValidators } from "../network/queries/sui-system"; import { TimeRemaining } from "../components/TimeRemaining"; import { GameEnding, useGameOverRedirect } from "../components/GameEnding"; @@ -89,12 +89,12 @@ export function Home() { }} > -
{convertToString(assignedValidator.name)}
+
{assignedValidator.name}
) : ( "--" diff --git a/sdk/typescript/src/types/validator.ts b/sdk/typescript/src/types/validator.ts index f7033f2180e2f..f5cd9909970bf 100644 --- a/sdk/typescript/src/types/validator.ts +++ b/sdk/typescript/src/types/validator.ts @@ -10,7 +10,6 @@ import { string, union, Infer, - nullable, tuple, optional, } from 'superstruct'; @@ -25,10 +24,10 @@ export const ValidatorMetaData = object({ network_pubkey_bytes: array(number()), worker_pubkey_bytes: array(number()), proof_of_possession_bytes: array(number()), - name: union([string(), array(number())]), - description: union([string(), nullable(array(number()))]), - image_url: union([string(), nullable(array(number()))]), - project_url: union([string(), nullable(array(number()))]), + name: string(), + description: string(), + image_url: string(), + project_url: string(), p2p_address: array(number()), net_address: array(number()), consensus_address: array(number()),