Skip to content

Commit

Permalink
Remove legacy encoding for validator metadata (MystenLabs#8640)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan-Mysten authored Feb 27, 2023
1 parent ec16fd7 commit c7e8b00
Show file tree
Hide file tree
Showing 14 changed files with 32 additions and 296 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
});
}
Expand Down
25 changes: 4 additions & 21 deletions apps/explorer/src/components/validator/ValidatorMeta.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down
174 changes: 0 additions & 174 deletions apps/explorer/src/pages/validator/ValidatorDataTypes.ts

This file was deleted.

9 changes: 2 additions & 7 deletions apps/explorer/src/pages/validators/Validators.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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,
Expand Down
23 changes: 0 additions & 23 deletions apps/explorer/src/utils/getName.ts

This file was deleted.

19 changes: 0 additions & 19 deletions apps/wallet/src/ui/app/staking/useSystemState.tsx
Original file line number Diff line number Diff line change
@@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -57,19 +57,15 @@ 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(
BigInt(validator.delegation_staking_pool.sui_balance) +
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') {
Expand Down
11 changes: 3 additions & 8 deletions apps/wallet/src/ui/app/staking/validators/ValidatorLogo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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]);

Expand Down
Loading

0 comments on commit c7e8b00

Please sign in to comment.