Skip to content

Commit

Permalink
use Any.pack instead of manually creating Any message (#1566)
Browse files Browse the repository at this point in the history
  • Loading branch information
turbocrime authored Aug 3, 2024
1 parent 69b0837 commit 457e04f
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 47 deletions.
5 changes: 5 additions & 0 deletions .changeset/chilled-wombats-approve.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@penumbra-zone/services': patch
---

use `Any.pack` to create `Any` messages
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb.js';
import { ValidatorInfo } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/stake/v1/stake_pb.js';
import { bech32mIdentityKey } from '@penumbra-zone/bech32m/penumbravalid';
import { Any } from '@bufbuild/protobuf';

const u8 = (length: number) => Uint8Array.from({ length }, () => Math.floor(Math.random() * 256));

Expand Down Expand Up @@ -90,10 +91,7 @@ const valueView = new ValueView({
},
],

extendedMetadata: {
typeUrl: ValidatorInfo.typeName,
value: validatorInfo.toBinary(),
},
extendedMetadata: Any.pack(validatorInfo),
},
},
});
Expand Down
31 changes: 7 additions & 24 deletions apps/minifront/src/state/staking/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/keys/v1/keys_pb.js';
import { THROTTLE_MS } from '.';
import { DelegationsByAddressIndexResponse } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/view/v1/view_pb.js';
import { Any } from '@bufbuild/protobuf';

const u8 = (length: number) => Uint8Array.from({ length }, () => Math.floor(Math.random() * 256));
const validator1IdentityKey = new IdentityKey({ ik: u8(32) });
Expand Down Expand Up @@ -83,10 +84,7 @@ vi.mock('../../fetchers/balances', () => ({
metadata: {
display: `delegation_${validator1Bech32IdentityKey}`,
},
extendedMetadata: {
typeUrl: ValidatorInfo.typeName,
value: validatorInfo1.toBinary(),
},
extendedMetadata: Any.pack(validatorInfo1),
},
},
}),
Expand All @@ -111,10 +109,7 @@ vi.mock('../../fetchers/balances', () => ({
metadata: {
display: `delegation_${validator2Bech32IdentityKey}`,
},
extendedMetadata: {
typeUrl: ValidatorInfo.typeName,
value: validatorInfo2.toBinary(),
},
extendedMetadata: Any.pack(validatorInfo2),
},
},
}),
Expand Down Expand Up @@ -168,10 +163,7 @@ const mockViewClient = vi.hoisted(() => ({
case: 'knownAssetId',
value: {
amount: { hi: 0n, lo: 1n },
extendedMetadata: {
typeUrl: ValidatorInfo.typeName,
value: validatorInfo1.toBinary(),
},
extendedMetadata: Any.pack(validatorInfo1),
},
},
},
Expand All @@ -184,10 +176,7 @@ const mockViewClient = vi.hoisted(() => ({
case: 'knownAssetId',
value: {
amount: { hi: 0n, lo: 2n },
extendedMetadata: {
typeUrl: ValidatorInfo.typeName,
value: validatorInfo2.toBinary(),
},
extendedMetadata: Any.pack(validatorInfo2),
},
},
},
Expand All @@ -200,10 +189,7 @@ const mockViewClient = vi.hoisted(() => ({
case: 'knownAssetId',
value: {
amount: { hi: 0n, lo: 0n },
extendedMetadata: {
typeUrl: ValidatorInfo.typeName,
value: validatorInfo3.toBinary(),
},
extendedMetadata: Any.pack(validatorInfo3),
},
},
},
Expand All @@ -216,10 +202,7 @@ const mockViewClient = vi.hoisted(() => ({
case: 'knownAssetId',
value: {
amount: { hi: 0n, lo: 0n },
extendedMetadata: {
typeUrl: ValidatorInfo.typeName,
value: validatorInfo4.toBinary(),
},
extendedMetadata: Any.pack(validatorInfo4),
},
},
},
Expand Down
6 changes: 2 additions & 4 deletions packages/services/src/view-service/auctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { IndexedDbMock, MockQuerier, MockServices } from '../test-utils.js';
import { StateCommitment } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/crypto/tct/v1/tct_pb.js';
import { Value } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/asset/v1/asset_pb.js';
import { Amount } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/num/v1/num_pb.js';
import { Any } from '@bufbuild/protobuf';

const AUCTION_ID_1 = new AuctionId({ inner: new Uint8Array(Array(32).fill(1)) });
const BECH32M_AUCTION_ID_1 = bech32mAuctionId(AUCTION_ID_1);
Expand Down Expand Up @@ -165,10 +166,7 @@ describe('Auctions request handler', () => {
expect(results[0]).toEqual(
new AuctionsResponse({
id: AUCTION_ID_1,
auction: {
typeUrl: DutchAuction.typeName,
value: new DutchAuction({ description: MOCK_AUCTION_1, state: { seq: 0n } }).toBinary(),
},
auction: Any.pack(new DutchAuction({ description: MOCK_AUCTION_1, state: { seq: 0n } })),
noteRecord: MOCK_SPENDABLE_NOTE_RECORD,
}),
);
Expand Down
9 changes: 4 additions & 5 deletions packages/services/src/view-service/auctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,16 @@ export const auctions: Impl['auctions'] = async function* (req, ctx) {
let auction: Any | undefined;
if (!!value.auction || state) {
const outstandingReserves = await indexedDb.getAuctionOutstandingReserves(id);
auction = new Any({
typeUrl: DutchAuction.typeName,
value: new DutchAuction({
auction = Any.pack(
new DutchAuction({
state: state ?? {
seq: value.seqNum,
inputReserves: outstandingReserves?.input.amount,
outputReserves: outstandingReserves?.output.amount,
},
description: value.auction,
}).toBinary(),
});
}),
);
}

yield new AuctionsResponse({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ export const delegationsByAddressIndex: Impl['delegationsByAddressIndex'] = asyn

for await (const validatorInfoResponse of mockStakeClient.validatorInfo({ showInactive })) {
const validatorInfo = getValidatorInfo(validatorInfoResponse);
const extendedMetadata = new Any({
typeUrl: ValidatorInfo.typeName,
value: validatorInfo.toBinary(),
});
const extendedMetadata = Any.pack(validatorInfo);

const identityKey = getValidatorInfo.pipe(getIdentityKeyFromValidatorInfo)(
validatorInfoResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Impl } from '../index.js';
import { balances } from '../balances.js';
import { getIsClaimable, isUnbondingTokenBalance } from './helpers.js';
import { Any } from '@bufbuild/protobuf';
import { ValidatorInfo } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/stake/v1/stake_pb.js';
import { stakeClientCtx } from '../../ctx/stake-client.js';
import { getValidatorInfo } from '@penumbra-zone/getters/get-validator-info-response';
import { assetPatterns } from '@penumbra-zone/types/assets';
Expand Down Expand Up @@ -59,17 +58,13 @@ export const unbondingTokensByAddressIndex: Impl['unbondingTokensByAddressIndex'
identityKey: identityKeyFromBech32m(regexResult.idKey),
});
const validatorInfo = getValidatorInfo(validatorInfoResponse);
const extendedMetadata = new Any({
typeUrl: ValidatorInfo.typeName,
value: validatorInfo.toBinary(),
});

const withValidatorInfo = getBalanceView(new BalancesResponse(balancesResponse));
if (withValidatorInfo.valueView.case !== 'knownAssetId') {
throw new Error(`Unexpected ValueView case: ${withValidatorInfo.valueView.case}`);
}

withValidatorInfo.valueView.value.extendedMetadata = extendedMetadata;
withValidatorInfo.valueView.value.extendedMetadata = Any.pack(validatorInfo);

yield new UnbondingTokensByAddressIndexResponse({
claimable,
Expand Down

0 comments on commit 457e04f

Please sign in to comment.