From 758ce567e5949dd666c285b3695182ef7485a3e9 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Fri, 10 Jan 2020 15:58:56 +0100 Subject: [PATCH] Failsafe on proposal decode (fallback ro preimage only) (#1710) --- packages/api-derive/src/democracy/proposals.ts | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/api-derive/src/democracy/proposals.ts b/packages/api-derive/src/democracy/proposals.ts index f74f76fdb6..f69b0f35f6 100644 --- a/packages/api-derive/src/democracy/proposals.ts +++ b/packages/api-derive/src/democracy/proposals.ts @@ -2,7 +2,7 @@ // This software may be modified and distributed under the terms // of the Apache-2.0 license. See the LICENSE file for details. -import { AccountId, Balance, BlockNumber, Hash, PropIndex } from '@polkadot/types/interfaces'; +import { AccountId, Balance, BlockNumber, Hash, PropIndex, Proposal } from '@polkadot/types/interfaces'; import { ITuple } from '@polkadot/types/types'; import { DeriveProposal } from '../types'; @@ -32,6 +32,16 @@ function parse (api: ApiInterfaceRx, { depositors, proposals, preimages }: Resul .map(([propIndex, hash, proposer], index): DeriveProposal => { const preimage = preimages[index].unwrapOr(null); const depositor = depositors[index].unwrap(); + let proposal: undefined | Proposal; + + // we could end up in a situation where the proposal is non-decodable, e.g. after an upgrade + if (preimage) { + try { + proposal = createType(api.registry, 'Proposal', preimage[0].toU8a(true)); + } catch (error) { + console.error(error); + } + } return { balance: depositor[0], @@ -44,9 +54,7 @@ function parse (api: ApiInterfaceRx, { depositors, proposals, preimages }: Resul proposer: preimage[1] } : undefined, - proposal: preimage - ? createType(api.registry, 'Proposal', preimage[0].toU8a(true)) - : undefined, + proposal, proposer, seconds: depositor[1] };