-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4976 from eshark9312/verify-validator-membership
Verify Validator Membership
- Loading branch information
Showing
8 changed files
with
168 additions
and
0 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
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
70 changes: 70 additions & 0 deletions
70
tests/network-tests/src/fixtures/membership/VerifyValidatorMembershipFixture.ts
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,70 @@ | ||
import Long from 'long' | ||
import { SubmittableExtrinsic } from '@polkadot/api/types' | ||
import { ISubmittableResult } from '@polkadot/types/types/' | ||
import { WorkerId, MemberId } from '@joystream/types/primitives' | ||
import { RemarkMetadataAction } from '@joystream/metadata-protobuf' | ||
import { Api } from '../../Api' | ||
import { QueryNodeApi } from '../../QueryNodeApi' | ||
import { AnyQueryNodeEvent, EventDetails } from '../../types' | ||
import { Utils } from '../../utils' | ||
import { MembershipFieldsFragment } from '../../graphql/generated/queries' | ||
import { WithMembershipWorkersFixture } from './WithMembershipWorkersFixture' | ||
|
||
export type VerifyValidatorInput = { | ||
memberId: MemberId | ||
isVerified: boolean | ||
asWorker?: WorkerId | ||
expectFailure?: boolean | ||
} | ||
|
||
export class VerifyValidatorMembershipFixture extends WithMembershipWorkersFixture { | ||
protected verifications: VerifyValidatorInput[] | ||
|
||
public constructor(api: Api, query: QueryNodeApi, verifications: VerifyValidatorInput[]) { | ||
super(api, query) | ||
this.verifications = verifications | ||
} | ||
|
||
protected async getSignerAccountOrAccounts(): Promise<string[]> { | ||
return this.getSignersFromInput(this.verifications) | ||
} | ||
|
||
protected async getExtrinsics(): Promise<SubmittableExtrinsic<'promise'>[]> { | ||
return this.verifications.map((u) => { | ||
const metadata = Utils.metadataToBytes(RemarkMetadataAction, { | ||
verifyValidator: { memberId: Long.fromString(u.memberId.toString()), isVerified: u.isVerified }, | ||
}) | ||
return u.memberId | ||
? this.api.tx.membershipWorkingGroup.workerRemark(u.memberId, metadata) | ||
: this.api.tx.membershipWorkingGroup.leadRemark(metadata) | ||
}) | ||
} | ||
|
||
protected async getEventFromResult(result: ISubmittableResult): Promise<EventDetails> { | ||
if (this.api.findEvent(result, 'membershipWorkingGroup', 'WorkerRemarked')) { | ||
return this.api.getEventDetails(result, 'membershipWorkingGroup', 'WorkerRemarked') | ||
} else { | ||
return this.api.getEventDetails(result, 'membershipWorkingGroup', 'LeadRemarked') | ||
} | ||
} | ||
|
||
private assertQueriedMembershipsAreValid(qMembers: MembershipFieldsFragment[]): void { | ||
this.events.map((e, i) => { | ||
const verification = this.verifications[i] | ||
if (verification.expectFailure) return | ||
|
||
const qMembership = qMembers.find((p) => p.id === verification.memberId.toString()) | ||
Utils.assert(qMembership, 'Query node: Membership not found') | ||
}) | ||
} | ||
|
||
protected assertQueryNodeEventIsValid(qEvent: AnyQueryNodeEvent, i: number): void { | ||
// TODO: implement QN checks | ||
} | ||
|
||
async runQueryNodeChecks(): Promise<void> { | ||
await super.runQueryNodeChecks() | ||
const qMembers = await this.query.getMembersByIds(this.verifications.map((m) => m.memberId)) | ||
this.assertQueriedMembershipsAreValid(qMembers) | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
tests/network-tests/src/fixtures/membership/WithMembershipWorkersFixture.ts
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,28 @@ | ||
import { WorkerId } from '@joystream/types/primitives' | ||
import { StandardizedFixture } from '../../Fixture' | ||
|
||
export abstract class WithMembershipWorkersFixture extends StandardizedFixture { | ||
protected membershipLeadId?: WorkerId | ||
|
||
protected async getMembershipLeadId(): Promise<WorkerId> { | ||
if (!this.membershipLeadId) { | ||
const optMembershipLeadId = await this.api.query.membershipWorkingGroup.currentLead() | ||
if (optMembershipLeadId.isNone) { | ||
throw new Error('Membership working group lead not set!') | ||
} | ||
|
||
this.membershipLeadId = optMembershipLeadId.unwrap() | ||
} | ||
|
||
return this.membershipLeadId | ||
} | ||
|
||
protected async getSignersFromInput(input: { asWorker?: WorkerId }[]): Promise<string[]> { | ||
return Promise.all( | ||
input.map(async (r) => { | ||
const workerId = r.asWorker || (await this.getMembershipLeadId()) | ||
return (await this.api.query.membershipWorkingGroup.workerById(workerId)).unwrap().roleAccountId.toString() | ||
}) | ||
) | ||
} | ||
} |
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
32 changes: 32 additions & 0 deletions
32
tests/network-tests/src/flows/membership/updateValidatorVerifications.ts
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,32 @@ | ||
import { FlowProps } from '../../Flow' | ||
import { | ||
BuyMembershipHappyCaseFixture, | ||
VerifyValidatorInput, | ||
VerifyValidatorMembershipFixture, | ||
} from '../../fixtures/membership' | ||
|
||
import { extendDebug } from '../../Debugger' | ||
import { FixtureRunner } from '../../Fixture' | ||
|
||
export default async function updateValidatorVerificationStatus({ api, query }: FlowProps): Promise<void> { | ||
const debug = extendDebug('flow:updating-validator-verification-status') | ||
debug('Started') | ||
api.enableDebugTxLogs() | ||
|
||
const accounts = (await api.createKeyPairs(2)).map(({ key }) => key.address) | ||
const buyMembershipsFixture = new BuyMembershipHappyCaseFixture(api, query, accounts) | ||
|
||
const updates1: VerifyValidatorInput[] = buyMembershipsFixture | ||
.getCreatedMembers() | ||
.map((memberId) => ({ memberId, isVerified: true })) | ||
|
||
const verifyFixture = new VerifyValidatorMembershipFixture(api, query, updates1) | ||
await new FixtureRunner(verifyFixture).runWithQueryNodeChecks() | ||
|
||
const updates2 = updates1.map(({ memberId }) => ({ memberId, isVerified: false })) | ||
|
||
const unverifyFixture = new VerifyValidatorMembershipFixture(api, query, updates2) | ||
await new FixtureRunner(unverifyFixture).runWithQueryNodeChecks() | ||
|
||
debug('Done') | ||
} |
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