Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BlockEnergyLimit Update #343

Merged
merged 5 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Added

- Support for block energy limit chain update.

## 1.5.0

### Added
Expand Down
15 changes: 15 additions & 0 deletions app/features/ledger/ConcordiumLedgerClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
TimeParameters,
CooldownParameters,
PoolParameters,
BlockEnergyLimit,
} from '~/utils/types';
import { pipe } from '~/utils/basicHelpers';

Expand Down Expand Up @@ -336,4 +337,18 @@ export default class ConcordiumLedgerClient {
)
);
}

signBlockEnergyLimit(
transaction: UpdateInstruction<BlockEnergyLimit>,
serializedPayload: Buffer,
path: number[]
): Promise<Buffer> {
return toBuffer(
window.ledger.signBlockEnergyLimit(
transaction,
serializedPayload,
path
)
);
}
}
15 changes: 15 additions & 0 deletions app/features/ledger/ConcordiumLedgerClientMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
TimeParameters,
CooldownParameters,
PoolParameters,
BlockEnergyLimit,
} from '~/utils/types';
import { AccountPathInput, getAccountPath } from './Path';
import getAppAndVersion, { AppAndVersion } from './GetAppAndVersion';
Expand Down Expand Up @@ -400,6 +401,20 @@ export default class ConcordiumLedgerClientMain {
);
}

signBlockEnergyLimit(
transaction: UpdateInstruction<BlockEnergyLimit>,
serializedPayload: Buffer,
path: number[]
): Promise<Buffer> {
return signUpdateTransaction(
this.transport,
0x45,
path,
transaction,
serializedPayload
);
}

getAppAndVersion(): Promise<AppAndVersion> {
return getAppAndVersion(this.transport);
}
Expand Down
11 changes: 10 additions & 1 deletion app/pages/multisig/menu/MultiSignatureCreateProposalList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import { proposalsSelector } from '~/features/MultiSignatureSlice';
import { expireProposals } from '~/utils/ProposalHelper';
import routes from '~/constants/routes.json';
import { useProtocolVersion } from '~/utils/dataHooks';
import { hasDelegationProtocol } from '~/utils/protocolVersion';
import {
hasDelegationProtocol,
hasConsensusUpdateProtocol,
} from '~/utils/protocolVersion';
import { not } from '~/utils/functionHelpers';

import styles from '../MultiSignaturePage/MultiSignaturePage.module.scss';
Expand Down Expand Up @@ -127,6 +130,12 @@ const updateInstructionTypes: TypeTuple[] = [
'Update time parameters',
hasDelegationProtocol,
],
[
TransactionTypes.UpdateInstruction,
UpdateType.BlockEnergyLimit,
'Update block energy limit',
hasConsensusUpdateProtocol,
],
];

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import React from 'react';
import Loading from '~/cross-app-components/Loading';
import { BlockEnergyLimit } from '~/utils/types';
import withChainData, { ChainData } from '~/utils/withChainData';
import Label from '~/components/Label';
import { assertChainParametersV2OrHigher } from '~/utils/blockSummaryHelpers';

interface Props extends ChainData {
blockEnergyLimit: BlockEnergyLimit;
}

/**
* Displays an overview of a block energy limit transaction.
*/
export default withChainData(function BlockEnergyLimitView({
blockEnergyLimit,
chainParameters,
}: Props) {
if (!chainParameters) {
return <Loading inline />;
}
assertChainParametersV2OrHigher(chainParameters);

return (
<>
<div>
<Label className="mB5">Current block energy limit:</Label>
<div className="body3 mono">
{chainParameters.blockEnergyLimit.toString()}
</div>
</div>
<div>
<Label className="mB5">New block energy limit:</Label>
<div className="body3 mono">
{blockEnergyLimit.blockEnergyLimit.toString()}
</div>
</div>
</>
);
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import React from 'react';
import { EqualRecord } from '~/utils/types';
import { UpdateProps } from '~/utils/transactionTypes';
import Form from '~/components/Form/';
import { mustBeAnInteger, requiredMessage, enterHere } from '../common/util';
import { assertChainParametersV2OrHigher } from '~/utils/blockSummaryHelpers';

export interface BlockEnergyLimitFields {
blockEnergyLimit: bigint;
}

const fieldNames: EqualRecord<BlockEnergyLimitFields> = {
blockEnergyLimit: 'blockEnergyLimit',
};

export const fieldDisplays = {
blockEnergyLimit: 'block energy limit',
};

/**
* Component for creating an update block energy limit transaction.
*/
export default function BlockEnergyLimit({
defaults,
chainParameters,
}: UpdateProps): JSX.Element | null {
assertChainParametersV2OrHigher(chainParameters);

const currentBlockEnergyLimit = chainParameters.blockEnergyLimit;

return (
<div>
<div className="body3 mono mB10">
Current {fieldDisplays.blockEnergyLimit}:{' '}
{currentBlockEnergyLimit.toString()} NRG
</div>
<Form.Input
className="body2"
name={fieldNames.blockEnergyLimit}
defaultValue={
defaults.blockEnergyLimit ||
currentBlockEnergyLimit.toString()
}
label={`New ${fieldDisplays.blockEnergyLimit} (NRG)`}
placeholder={enterHere(fieldDisplays.blockEnergyLimit)}
rules={{
required: requiredMessage(fieldDisplays.blockEnergyLimit),
min: {
value: 1,
message: 'Block energy limit must be positive',
},
max: {
value: '18446744073709551615',
message:
'Block energy limit must be below 18446744073709551615',
},
validate: {
mustBeAnInteger,
},
}}
/>
</div>
);
}
12 changes: 12 additions & 0 deletions app/preload/ledger/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
TimeParameters,
CooldownParameters,
PoolParameters,
BlockEnergyLimit,
} from '~/utils/types';
import { LedgerCommands } from '~/preload/preloadTypes';

Expand Down Expand Up @@ -277,6 +278,17 @@ export default function exposedMethods(
keypath
);
},
signBlockEnergyLimit: (
transaction: UpdateInstruction<BlockEnergyLimit>,
serializedPayload: Buffer,
keypath: number[]
) => {
return getLedgerClient().signBlockEnergyLimit(
transaction,
serializedPayload,
keypath
);
},
getAppAndVersion: () => getLedgerClient().getAppAndVersion(),
subscribe: () => subscribeLedger(eventEmitter),
closeTransport,
Expand Down
2 changes: 2 additions & 0 deletions app/preload/preloadLedgerTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
TimeParameters,
CooldownParameters,
PoolParameters,
BlockEnergyLimit,
} from '~/utils/types';
import { AppAndVersion } from '../features/ledger/GetAppAndVersion';
import { AccountPathInput } from '../features/ledger/Path';
Expand Down Expand Up @@ -107,6 +108,7 @@ type LedgerCommands = {
signTimeParameters: SignUpdate<TimeParameters>;
signCooldownParameters: SignUpdate<CooldownParameters>;
signPoolParameters: SignUpdate<PoolParameters>;
signBlockEnergyLimit: SignUpdate<BlockEnergyLimit>;
signHigherLevelKeysUpdate: SignKeyUpdate<HigherLevelKeyUpdate>;
signAuthorizationKeysUpdate: SignVersionedKeyUpdate<AuthorizationKeysUpdate>;
getAppAndVersion: () => Promise<AppAndVersion>;
Expand Down
15 changes: 15 additions & 0 deletions app/utils/UpdateSerialization.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
CommissionRates,
CommissionRanges,
AuthorizationKeysUpdateType,
BlockEnergyLimit,
} from './types';

/**
Expand Down Expand Up @@ -62,6 +63,7 @@ export enum OnChainUpdateType {
UpdatePoolParameters = 15,
UpdateTimeParameters = 16,
UpdateMintDistributionV1 = 17,
UpdateBlockEnergyLimit = 20,
}

/**
Expand Down Expand Up @@ -209,6 +211,17 @@ export function serializeBakerStakeThreshold(
return serializedBakerStakeThreshold;
}

/**
* Serializes a BlockEnergyLimit to the byte format expected
* by the chain.
*/
export function serializeBlockEnergyLimit(blockEnergyLimit: BlockEnergyLimit) {
const serializedBlockEnergyLimit = encodeWord64(
BigInt(blockEnergyLimit.blockEnergyLimit)
);
return serializedBlockEnergyLimit;
}

/**
* Serializes an ElectionDifficulty to bytes.
*/
Expand Down Expand Up @@ -579,6 +592,8 @@ function mapUpdateTypeToOnChainUpdateType(type: UpdateType): OnChainUpdateType {
return OnChainUpdateType.UpdateTimeParameters;
case UpdateType.UpdateMintDistributionV1:
return OnChainUpdateType.UpdateMintDistributionV1;
case UpdateType.BlockEnergyLimit:
return OnChainUpdateType.UpdateBlockEnergyLimit;
default:
throw new Error(`An invalid update type was given: ${type}`);
}
Expand Down
38 changes: 35 additions & 3 deletions app/utils/blockSummaryHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable import/prefer-default-export */
import { isChainParametersV0 } from '@concordium/web-sdk';
import { ChainParameters } from '~/node/NodeApiTypes';
import {
ChainParameters,
ChainParametersV0,
ChainParametersV1,
isChainParametersV0,
isChainParametersV1,
} from '@concordium/web-sdk';

export function getMinimumStakeForBaking(
chainParameters: ChainParameters
Expand All @@ -10,3 +14,31 @@ export function getMinimumStakeForBaking(
}
return chainParameters.minimumEquityCapital;
}

export function isChainParametersV2OrHigher(
chainParameters: ChainParameters
): chainParameters is Exclude<
ChainParameters,
ChainParametersV0 | ChainParametersV1
> {
return (
!isChainParametersV0(chainParameters) &&
!isChainParametersV1(chainParameters)
);
}

/**
* Checks that the given chainParameters are version 2 or above.
* If the given parameters are version 0 or 1, this throws.
*/
export function assertChainParametersV2OrHigher(
chainParameters: ChainParameters,
errorMessage = 'Connected node used outdated chainParameters format'
): asserts chainParameters is Exclude<
ChainParameters,
ChainParametersV0 | ChainParametersV1
> {
if (!isChainParametersV2OrHigher(chainParameters)) {
throw new Error(errorMessage);
}
}
2 changes: 2 additions & 0 deletions app/utils/protocolVersion.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
// eslint-disable-next-line import/prefer-default-export
export const hasDelegationProtocol = (pv: bigint) => pv >= 4n;

export const hasConsensusUpdateProtocol = (pv: bigint) => pv >= 6n;
Loading