Skip to content

Commit

Permalink
Merge pull request #222 from Concordium/sendUpdateInstruction
Browse files Browse the repository at this point in the history
Send update instruction + Fix getBlockChainParameters
  • Loading branch information
shjortConcordium authored Sep 5, 2023
2 parents c89ac23 + 9a3982c commit d28cbe0
Show file tree
Hide file tree
Showing 6 changed files with 375 additions and 3 deletions.
9 changes: 9 additions & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
# Changelog

## Unreleased

### Fixed
- Added missing fields to `getBlockChainParameters` response. (rootKeys, level1Keys, level2Keys)

### Added

- `sendUpdateInstruction` to the gRPC Client.

## 9.3.0

### Added
Expand Down
54 changes: 54 additions & 0 deletions packages/common/src/GRPCClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
isHex,
isValidHash,
isValidIp,
mapRecord,
mapStream,
unwrap,
wasmToSchema,
Expand Down Expand Up @@ -430,6 +431,59 @@ export class ConcordiumGRPCClient {
return Buffer.from(response.value).toString('hex');
}

/**
* Sends an update instruction transaction for updating a chain parameter
* to the node to be put in a block on the chain.
*
* @param updateInstructionTransaction the update instruction transaction to send to the node
* @param signatures map of the signatures on the hash of the serialized unsigned update instruction, with the key index as map key
* @returns The transaction hash as a hex string
*/
async sendUpdateInstruction(
updateInstructionTransaction: v1.UpdateInstruction,
signatures: Record<number, HexString>
): Promise<HexString> {
const header = updateInstructionTransaction.header;
const updateInstruction: v2.UpdateInstruction = {
header: {
sequenceNumber: {
value: header.sequenceNumber,
},
effectiveTime: {
value: header.effectiveTime,
},
timeout: {
value: header.timeout,
},
},
payload: {
payload: {
oneofKind: 'rawPayload',
rawPayload: Buffer.from(
updateInstructionTransaction.payload,
'hex'
),
},
},
signatures: {
signatures: mapRecord(signatures, (x) => ({
value: Buffer.from(x, 'hex'),
})),
},
};

const sendBlockItemRequest: v2.SendBlockItemRequest = {
blockItem: {
oneofKind: 'updateInstruction',
updateInstruction: updateInstruction,
},
};

const response = await this.client.sendBlockItem(sendBlockItemRequest)
.response;
return Buffer.from(response.value).toString('hex');
}

/**
* Retrieves the status of the block chain parameters at the given blockHash.
*
Expand Down
14 changes: 14 additions & 0 deletions packages/common/src/GRPCTypeTranslation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,15 @@ function trBaker(baker: v2.AccountStakingInfo_Baker): v1.AccountBakerDetails {
};
}

function trHigherLevelKeysUpdate(
update: v2.HigherLevelKeys
): v1.KeysWithThreshold {
return {
keys: update.keys.map(trUpdatePublicKey),
threshold: unwrap(update.threshold?.value),
};
}

function translateChainParametersCommon(
params: v2.ChainParametersV1 | v2.ChainParametersV0
): v1.ChainParametersCommon {
Expand All @@ -258,6 +267,8 @@ function translateChainParametersCommon(
microGTUPerEuro: unwrap(params.microCcdPerEuro?.value),
accountCreationLimit: unwrap(params.accountCreationLimit?.value),
foundationAccount: unwrapToBase58(params.foundationAccount),
level1Keys: trHigherLevelKeysUpdate(unwrap(params.level1Keys)),
rootKeys: trHigherLevelKeysUpdate(unwrap(params.rootKeys)),
};
}

Expand Down Expand Up @@ -404,6 +415,7 @@ function trChainParametersV0(v0: v2.ChainParametersV0): v1.ChainParametersV0 {
const commonRewardParameters = translateRewardParametersCommon(v0);
return {
...common,
level2Keys: trAuthorizationsV0(unwrap(v0.level2Keys)),
electionDifficulty: trAmountFraction(v0.electionDifficulty?.value),
bakerCooldownEpochs: unwrap(v0.bakerCooldownEpochs?.value),
minimumThresholdForBaking: unwrap(v0.minimumThresholdForBaking?.value),
Expand Down Expand Up @@ -439,6 +451,7 @@ function trChainParametersV1(
const commonRewardParameters = translateRewardParametersCommon(params);
return {
...common,
level2Keys: trAuthorizationsV1(unwrap(params.level2Keys)),
electionDifficulty: trAmountFraction(params.electionDifficulty?.value),
rewardPeriodLength: unwrap(
params.timeParameters?.rewardPeriodLength?.value?.value
Expand Down Expand Up @@ -507,6 +520,7 @@ function trChainParametersV2(

return {
...common,
level2Keys: trAuthorizationsV1(unwrap(params.level2Keys)),
rewardPeriodLength: unwrap(
params.timeParameters?.rewardPeriodLength?.value?.value
),
Expand Down
25 changes: 22 additions & 3 deletions packages/common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,10 @@ export interface ChainParametersCommon {
foundationAccount: Base58String;
/** The chain foundation account index */
foundationAccountIndex?: bigint;
/** Keys allowed to do level1 updates */
level1Keys: KeysWithThreshold;
/** Keys allowed to do root updates */
rootKeys: KeysWithThreshold;
}

/** Chain parameters used from protocol version 1-3 */
Expand All @@ -469,6 +473,8 @@ export type ChainParametersV0 = ChainParametersCommon &
electionDifficulty: number;
/** The election difficulty for consensus lottery */
rewardParameters: RewardParametersV0;
/** Keys allowed to do parameter updates */
level2Keys: AuthorizationsV0;
};

/** Chain parameters used in protocol versions 4 and 5 */
Expand All @@ -480,6 +486,8 @@ export type ChainParametersV1 = ChainParametersCommon &
electionDifficulty: number;
/** The election difficulty for consensus lottery */
rewardParameters: RewardParametersV1;
/** Keys allowed to do parameter updates */
level2Keys: AuthorizationsV1;
};

/** Chain parameters used from protocol version 6 */
Expand All @@ -492,6 +500,8 @@ export type ChainParametersV2 = ChainParametersCommon &
ConsensusParameters & {
/** The election difficulty for consensus lottery */
rewardParameters: RewardParametersV2;
/** Keys allowed to do parameter updates */
level2Keys: AuthorizationsV1;
};

/** Union of all chain parameters across all protocol versions */
Expand Down Expand Up @@ -639,7 +649,10 @@ interface UpdatesCommon {
* @deprecated This is type describing return types from the JSON-RPC client and the V1 gRPC client, both of which have been deprecated
*/
export interface UpdatesV0 extends UpdatesCommon {
chainParameters: ChainParametersV0;
chainParameters: Omit<
ChainParametersV0,
'level1Keys' | 'level2Keys' | 'rootKeys'
>;
updateQueues: UpdateQueuesV0;
keys: KeysV0;
}
Expand All @@ -649,7 +662,10 @@ export interface UpdatesV0 extends UpdatesCommon {
* @deprecated This is type describing return types from the JSON-RPC client and the V1 gRPC client, both of which have been deprecated
*/
export interface UpdatesV1 extends UpdatesCommon {
chainParameters: ChainParametersV1;
chainParameters: Omit<
ChainParametersV1,
'level1Keys' | 'level2Keys' | 'rootKeys'
>;
updateQueues: UpdateQueuesV1;
keys: KeysV1;
}
Expand All @@ -659,7 +675,10 @@ export interface UpdatesV1 extends UpdatesCommon {
* @deprecated This is type describing return types from the JSON-RPC client and the V1 gRPC client, both of which have been deprecated
*/
export interface UpdatesV2 extends UpdatesCommon {
chainParameters: ChainParametersV2;
chainParameters: Omit<
ChainParametersV2,
'level1Keys' | 'level2Keys' | 'rootKeys'
>;
updateQueues: UpdateQueuesV2;
keys: KeysV1;
}
Expand Down
11 changes: 11 additions & 0 deletions packages/common/src/types/chainUpdate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,3 +312,14 @@ export type AuthorizationKeysUpdate =
typeOfUpdate: AuthorizationKeysUpdateType.Level2KeysUpdateV1;
updatePayload: AuthorizationsV1;
};

export type UpdateInstructionHeader = {
sequenceNumber: bigint;
effectiveTime: bigint;
timeout: bigint;
};

export type UpdateInstruction = {
header: UpdateInstructionHeader;
payload: HexString;
};
Loading

0 comments on commit d28cbe0

Please sign in to comment.