-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store validators in IndexedDB and serve StakingService from the exten…
…sion (#700) * Create a StakingQuerier * Store validator infos in IndexedDB * Fix bug with how new epochs were created; save updated validator infos during epoch transition * Keep latest known block height up to date * Remove unneeded package * Tweak comment * Fix dependency * Fix typo * Add new IDB method to iterate validator infos * Implement StakingService in the extension * Add comment * Add comment * Format * Add brackets * Add comment
- Loading branch information
1 parent
873303d
commit 3754c70
Showing
14 changed files
with
250 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
|
||
PRAX=lkpmkhpnhknhmibgnmmhdhgdilepfghe | ||
IDB_VERSION=26 | ||
IDB_VERSION=27 | ||
USDC_ASSET_ID="reum7wQmk/owgvGMWMZn/6RFPV24zIKq3W6In/WwZgg=" | ||
MINIFRONT_URL=https://app.testnet.penumbra.zone/ | ||
PENUMBRA_NODE_PD_URL=https://grpc.testnet.penumbra.zone/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { PromiseClient } from '@connectrpc/connect'; | ||
import { createClient } from './utils'; | ||
import { StakingQuerierInterface } from '@penumbra-zone/types'; | ||
import { QueryService as StakingService } from '@buf/penumbra-zone_penumbra.connectrpc_es/penumbra/core/component/stake/v1/stake_connect'; | ||
import { ValidatorInfoResponse } from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/stake/v1/stake_pb'; | ||
|
||
export class StakingQuerier implements StakingQuerierInterface { | ||
private readonly client: PromiseClient<typeof StakingService>; | ||
|
||
constructor({ grpcEndpoint }: { grpcEndpoint: string }) { | ||
this.client = createClient(grpcEndpoint, StakingService); | ||
} | ||
|
||
allValidatorInfos(): AsyncIterable<ValidatorInfoResponse> { | ||
/** | ||
* Include inactive validators when saving to our local database, since we | ||
* serve the `ValidatorInfo` RPC method from the extension, and may receive | ||
* requests for inactive validators. | ||
*/ | ||
return this.client.validatorInfo({ showInactive: true }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { QueryService as StakingService } from '@buf/penumbra-zone_penumbra.connectrpc_es/penumbra/core/component/stake/v1/stake_connect'; | ||
import { ServiceImpl } from '@connectrpc/connect'; | ||
import { validatorInfo } from './validator-info'; | ||
|
||
export type Impl = ServiceImpl<typeof StakingService>; | ||
|
||
export const stakingImpl: Pick<Impl, 'validatorInfo'> = { validatorInfo }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import { beforeEach, describe, expect, it, vi } from 'vitest'; | ||
import { validatorInfo } from './validator-info'; | ||
import { IndexedDbMock, MockServices } from '../test-utils'; | ||
import { HandlerContext, createContextValues, createHandlerContext } from '@connectrpc/connect'; | ||
import { QueryService as StakingService } from '@buf/penumbra-zone_penumbra.connectrpc_es/penumbra/core/component/stake/v1/stake_connect'; | ||
import { ServicesInterface } from '@penumbra-zone/types'; | ||
import { servicesCtx } from '../../ctx'; | ||
import { | ||
ValidatorInfoRequest, | ||
ValidatorInfoResponse, | ||
ValidatorState_ValidatorStateEnum, | ||
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/stake/v1/stake_pb'; | ||
import { PartialMessage } from '@bufbuild/protobuf'; | ||
|
||
describe('ValidatorInfo request handler', () => { | ||
let mockServices: MockServices; | ||
let mockIndexedDb: IndexedDbMock; | ||
let mockCtx: HandlerContext; | ||
const mockValidatorInfoResponse1 = new ValidatorInfoResponse({ | ||
validatorInfo: { | ||
validator: { name: 'Validator 1' }, | ||
status: { state: { state: ValidatorState_ValidatorStateEnum.ACTIVE } }, | ||
}, | ||
}); | ||
const mockValidatorInfoResponse2 = new ValidatorInfoResponse({ | ||
validatorInfo: { | ||
validator: { name: 'Validator 2' }, | ||
status: { state: { state: ValidatorState_ValidatorStateEnum.INACTIVE } }, | ||
}, | ||
}); | ||
|
||
beforeEach(() => { | ||
vi.resetAllMocks(); | ||
|
||
const mockIterateValidatorInfos = { | ||
next: vi.fn(), | ||
[Symbol.asyncIterator]: () => mockIterateValidatorInfos, | ||
}; | ||
mockIterateValidatorInfos.next.mockResolvedValueOnce({ | ||
value: mockValidatorInfoResponse1.validatorInfo, | ||
}); | ||
mockIterateValidatorInfos.next.mockResolvedValueOnce({ | ||
value: mockValidatorInfoResponse2.validatorInfo, | ||
}); | ||
mockIterateValidatorInfos.next.mockResolvedValueOnce({ done: true }); | ||
|
||
mockIndexedDb = { | ||
iterateValidatorInfos: () => mockIterateValidatorInfos, | ||
}; | ||
mockServices = { | ||
getWalletServices: vi.fn(() => | ||
Promise.resolve({ indexedDb: mockIndexedDb }), | ||
) as MockServices['getWalletServices'], | ||
}; | ||
mockCtx = createHandlerContext({ | ||
service: StakingService, | ||
method: StakingService.methods.validatorInfo, | ||
protocolName: 'mock', | ||
requestMethod: 'MOCK', | ||
url: '/mock', | ||
contextValues: createContextValues().set( | ||
servicesCtx, | ||
mockServices as unknown as ServicesInterface, | ||
), | ||
}); | ||
}); | ||
|
||
it('streams `ValidatorInfoResponse`s from the results of the database query', async () => { | ||
const req = new ValidatorInfoRequest({ showInactive: true }); | ||
|
||
const results: (ValidatorInfoResponse | PartialMessage<ValidatorInfoResponse>)[] = []; | ||
for await (const result of validatorInfo(req, mockCtx)) { | ||
results.push(result); | ||
} | ||
|
||
expect(results).toEqual([mockValidatorInfoResponse1, mockValidatorInfoResponse2]); | ||
}); | ||
|
||
it('does not include inactive validators by default', async () => { | ||
const req = new ValidatorInfoRequest(); | ||
|
||
const results: (ValidatorInfoResponse | PartialMessage<ValidatorInfoResponse>)[] = []; | ||
for await (const result of validatorInfo(req, mockCtx)) { | ||
results.push(result); | ||
} | ||
|
||
expect(results).toEqual([mockValidatorInfoResponse1]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { getStateEnumFromValidatorInfo } from '@penumbra-zone/getters'; | ||
import { Impl } from '.'; | ||
import { servicesCtx } from '../../ctx'; | ||
import { | ||
ValidatorInfoResponse, | ||
ValidatorState_ValidatorStateEnum, | ||
} from '@buf/penumbra-zone_penumbra.bufbuild_es/penumbra/core/component/stake/v1/stake_pb'; | ||
|
||
export const validatorInfo: Impl['validatorInfo'] = async function* (req, ctx) { | ||
const services = ctx.values.get(servicesCtx); | ||
const { indexedDb } = await services.getWalletServices(); | ||
|
||
for await (const validatorInfo of indexedDb.iterateValidatorInfos()) { | ||
if ( | ||
!req.showInactive && | ||
getStateEnumFromValidatorInfo(validatorInfo) === ValidatorState_ValidatorStateEnum.INACTIVE | ||
) | ||
continue; | ||
|
||
yield new ValidatorInfoResponse({ validatorInfo }); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.