0

in this pull request, i attempt to add support for looking up the collator account associated with a given nimbus session key.

the method implementation in the pr is as follows:

export function getAuthorDetails (header: Header, queryAt: QueryableStorage<'rxjs'>): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
  // nimbus consensus stores authorship in the header logs
  const { logs: [log] } = header.digest;
  const authorSessionKey = (log && (
    (log.isConsensus && log.asConsensus[0].isNimbus && log.asConsensus[1]) ||
    (log.isPreRuntime && log.asPreRuntime[0].isNimbus && log.asPreRuntime[1])
  )) || null;

  const validators = (queryAt.session)
    ? queryAt.session.validators()
    : of(null);

  const author = (authorSessionKey)
    // use the author mapping pallet if available (ie: moonbeam, moonriver)
    ? (queryAt.authorMapping && queryAt.authorMapping.mappingWithDeposit)
      ? queryAt.authorMapping.mappingWithDeposit<IOption<{ account: AccountId } & Codec>>(authorSessionKey).pipe(
          map(opt => opt.unwrapOr({ account: null }).account)
        )
      // use the author session pallet if available (ie: manta, calamari)
      : (queryAt.session && queryAt.session.queuedKeys)
        ? queryAt.session.queuedKeys<Vec<(AccountId, { nimbus: Address })>>().pipe(
            catchError(() => of(null)), // handle scenarios where queuedKeys is not of the expected type
            map((queuedKeys) => queuedKeys.find(([_, { nimbus }]) => nimbus.toHex() === authorSessionKey.toHex())),
            map(([collator]) => collator || null)
          )
        : null
    : null;

  return combineLatest([of(header), validators, author]);
}

the problematic line is this one:

queryAt.session.queuedKeys<Vec<(AccountId, { nimbus: Address })>>()

where the build error message reads:

/home/runner/work/api/api/packages/api-derive/src/chain/util.ts
  46:73  error  Parsing error: '=>' expected

i assume the double angle brackets following the closing parenthesis is syntactically problematic.

what is the correct way to do this?

also, what is the best way to run tests similar to what the ci runs, but locally?

edit: i've learned to use yarn lint and have gotten this far:

export function getAuthorDetails (header: Header, queryAt: QueryableStorage<'rxjs'>): Observable<[Header, Vec<AccountId> | null, AccountId | null]> {
  // nimbus consensus stores authorship in the header logs
  const { logs: [log] } = header.digest;
  const authorSessionKey = (log && (
    (log.isConsensus && log.asConsensus[0].isNimbus && log.asConsensus[1]) ||
    (log.isPreRuntime && log.asPreRuntime[0].isNimbus && log.asPreRuntime[1])
  )) || null;

  const validators = (queryAt.session)
    ? queryAt.session.validators()
    : null;

  const author = (authorSessionKey)
    // use the author mapping pallet if available (ie: moonbeam, moonriver)
    ? (queryAt.authorMapping && queryAt.authorMapping.mappingWithDeposit)
      ? queryAt.authorMapping.mappingWithDeposit<IOption<{ account: AccountId } & Codec>>(authorSessionKey)
        .pipe(map((opt) => opt.unwrapOr({ account: null }).account))
      // use the session pallet if available (ie: manta, calamari)
      : (queryAt.session && queryAt.session.queuedKeys)
        ? queryAt.session.queuedKeys<Vec<[AccountId, { nimbus: Address }]>>()
          .pipe(map((keys) => keys.find(([_, { nimbus }]) => nimbus.toHex() === authorSessionKey.toHex())[0]))
        : null
    : null;

  return combineLatest([of(header), validators, author]);
}

just need to figure out how to deal with the deprecation error on combineLatest...

1 Answer 1

2

Looks like you have a syntax error in your TypeScript. TS does not use the Rust syntax of (value1, value2, ...) for tuples, but it uses the same syntax as an Array: [value1, value2, ...]

Correcting that in my IDE resolved that particular error.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.