Skip to content

Commit

Permalink
feat: Add ClientUpdateProposal type.
Browse files Browse the repository at this point in the history
  • Loading branch information
octalmage authored and hanjukim committed Mar 26, 2022
1 parent c677df4 commit 88bd751
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/core/gov/Proposal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Int } from '../numeric';
import { JSONSerializable } from '../../util/json';
import { CommunityPoolSpendProposal } from '../distribution/proposals';
import { ParameterChangeProposal } from '../params/proposals';
import { ClientUpdateProposal } from '../ibc/proposals';
import { TextProposal } from './proposals';
import {
SoftwareUpgradeProposal,
Expand Down Expand Up @@ -229,29 +230,33 @@ export namespace Proposal {
| CommunityPoolSpendProposal
| ParameterChangeProposal
| SoftwareUpgradeProposal
| CancelSoftwareUpgradeProposal;
| CancelSoftwareUpgradeProposal
| ClientUpdateProposal;

export namespace Content {
export type Amino =
| TextProposal.Amino
| CommunityPoolSpendProposal.Amino
| ParameterChangeProposal.Amino
| SoftwareUpgradeProposal.Amino
| CancelSoftwareUpgradeProposal.Amino;
| CancelSoftwareUpgradeProposal.Amino
| ClientUpdateProposal.Amino;

export type Data =
| TextProposal.Data
| CommunityPoolSpendProposal.Data
| ParameterChangeProposal.Data
| SoftwareUpgradeProposal.Data
| CancelSoftwareUpgradeProposal.Data;
| CancelSoftwareUpgradeProposal.Data
| ClientUpdateProposal.Data;

export type Proto =
| TextProposal.Proto
| CommunityPoolSpendProposal.Proto
| ParameterChangeProposal.Proto
| SoftwareUpgradeProposal.Proto
| CancelSoftwareUpgradeProposal.Proto;
| CancelSoftwareUpgradeProposal.Proto
| ClientUpdateProposal.Proto;

export function fromAmino(amino: Proposal.Content.Amino): Proposal.Content {
switch (amino.type) {
Expand All @@ -265,6 +270,8 @@ export namespace Proposal {
return SoftwareUpgradeProposal.fromAmino(amino);
case 'upgrade/CancelSoftwareUpgradeProposal':
return CancelSoftwareUpgradeProposal.fromAmino(amino);
case 'ibc/ClientUpdateProposal':
return ClientUpdateProposal.fromAmino(amino);
}
}

Expand All @@ -280,6 +287,8 @@ export namespace Proposal {
return SoftwareUpgradeProposal.fromData(data);
case '/cosmos.upgrade.v1beta1.CancelSoftwareUpgradeProposal':
return CancelSoftwareUpgradeProposal.fromData(data);
case '/ibc.core.client.v1.ClientUpdateProposal':
return ClientUpdateProposal.fromData(data);
}
}

Expand All @@ -296,6 +305,8 @@ export namespace Proposal {
return SoftwareUpgradeProposal.unpackAny(anyProto);
case '/cosmos.upgrade.v1beta1.CancelSoftwareUpgradeProposal':
return CancelSoftwareUpgradeProposal.unpackAny(anyProto);
case '/ibc.core.client.v1.ClientUpdateProposal':
return ClientUpdateProposal.unpackAny(anyProto);
}

throw `Proposal content ${typeUrl} not recognized`;
Expand Down
15 changes: 15 additions & 0 deletions src/core/ibc/proposals/ClientUpdateProposal.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { ClientUpdateProposal } from './ClientUpdateProposal';

describe('ClientUpdateProposal', () => {
const update: ClientUpdateProposal.Data = {
'@type': '/ibc.core.client.v1.ClientUpdateProposal',
title: 'Update expired ibc client',
description: 'Proposal to update an IBC client which has expired.',
subject_client_id: '07-tendermint-19',
substitute_client_id: '07-tendermint-64',
};

it('parses IBC client upgrade proposal', () => {
expect(ClientUpdateProposal.fromData(update)).toBeTruthy();
});
});
139 changes: 139 additions & 0 deletions src/core/ibc/proposals/ClientUpdateProposal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
import { JSONSerializable } from '../../../util/json';
import { Any } from '@terra-money/terra.proto/google/protobuf/any';
import { ClientUpdateProposal as ClientUpdateProposal_pb } from '@terra-money/terra.proto/ibc/core/client/v1/client';

/**
* Proposal that allows updating IBC clients. If it passes, the substitute
* client's latest consensus state is copied over to the subject client.
*/
export class ClientUpdateProposal extends JSONSerializable<
ClientUpdateProposal.Amino,
ClientUpdateProposal.Data,
ClientUpdateProposal.Proto
> {
public subjectClientId: string;
public substituteClientId: string;
/**
* @param title proposal's title
* @param description proposal's description
* @param subjectClientId client to update
* @param substituteClientId client to copy
*/
constructor(
public title: string,
public description: string,
subjectClientId: string,
substituteClientId: string
) {
super();
this.subjectClientId = subjectClientId;
this.substituteClientId = substituteClientId;
}

public static fromAmino(
data: ClientUpdateProposal.Amino
): ClientUpdateProposal {
const {
value: { title, description, subjectClientId, substituteClientId },
} = data;
return new ClientUpdateProposal(
title,
description,
subjectClientId,
substituteClientId
);
}

public toAmino(): ClientUpdateProposal.Amino {
const { title, description, subjectClientId, substituteClientId } = this;
return {
type: 'ibc/ClientUpdateProposal',
value: {
title,
description,
subjectClientId,
substituteClientId,
},
};
}

public static fromData(
data: ClientUpdateProposal.Data
): ClientUpdateProposal {
const { title, description, subject_client_id, substitute_client_id } =
data;
return new ClientUpdateProposal(
title,
description,
subject_client_id,
substitute_client_id
);
}

public toData(): ClientUpdateProposal.Data {
const { title, description, subjectClientId, substituteClientId } = this;
return {
'@type': '/ibc.core.client.v1.ClientUpdateProposal',
title,
description,
subject_client_id: subjectClientId,
substitute_client_id: substituteClientId,
};
}

public static fromProto(
proto: ClientUpdateProposal.Proto
): ClientUpdateProposal {
return new ClientUpdateProposal(
proto.title,
proto.description,
proto.subjectClientId,
proto.substituteClientId
);
}

public toProto(): ClientUpdateProposal.Proto {
const { title, description, subjectClientId, substituteClientId } = this;
return ClientUpdateProposal_pb.fromPartial({
subjectClientId,
substituteClientId,
description,
title,
});
}

public packAny(): Any {
return Any.fromPartial({
typeUrl: '/ibc.core.client.v1.ClientUpdateProposal',
value: ClientUpdateProposal_pb.encode(this.toProto()).finish(),
});
}

public static unpackAny(msgAny: Any): ClientUpdateProposal {
return ClientUpdateProposal.fromProto(
ClientUpdateProposal_pb.decode(msgAny.value)
);
}
}

export namespace ClientUpdateProposal {
export interface Amino {
type: 'ibc/ClientUpdateProposal';
value: {
title: string;
description: string;
subjectClientId: string;
substituteClientId: string;
};
}

export interface Data {
'@type': '/ibc.core.client.v1.ClientUpdateProposal';
title: string;
description: string;
subject_client_id: string;
substitute_client_id: string;
}

export type Proto = ClientUpdateProposal_pb;
}
1 change: 1 addition & 0 deletions src/core/ibc/proposals/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './ClientUpdateProposal';

0 comments on commit 88bd751

Please sign in to comment.