From fd09543719513cba45c0817953387f4504174c0b Mon Sep 17 00:00:00 2001 From: Kewe63 <86300262+Kewe63@users.noreply.github.com> Date: Thu, 3 Sep 2026 10:56:51 +0300 Subject: [PATCH] fix: bound genesis consensus params --- scripts/genesis/ProtocolConfig.ts | 18 ++--- tests/unit/protocol-config-genesis.test.ts | 82 ++++++++++++++++++++++ 2 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 tests/unit/protocol-config-genesis.test.ts diff --git a/scripts/genesis/ProtocolConfig.ts b/scripts/genesis/ProtocolConfig.ts index 6b52ce6b..1d27c5aa 100644 --- a/scripts/genesis/ProtocolConfig.ts +++ b/scripts/genesis/ProtocolConfig.ts @@ -44,6 +44,8 @@ const PROTOCOL_CONFIG_CONTROLLER_STORAGE_LOCATION = 0x958f8fec699b51a1249f513ece const PAUSABLE_STORAGE_LOCATION = 0x0642d7922329a434cf4fd17a3c95eb692c24fd95f9f94d0b55420a5d895f4a00n const maxUint64 = 18446744073709551615n +const maxUint16 = 65535n +const schemaUint16 = schemaBigInt.min(0n).max(maxUint16) export const schemaProtocolConfig = z .object({ @@ -75,14 +77,14 @@ export const schemaProtocolConfig = z }), consensusParams: z .object({ - timeoutProposeMs: schemaBigInt, - timeoutProposeDeltaMs: schemaBigInt, - timeoutPrevoteMs: schemaBigInt, - timeoutPrevoteDeltaMs: schemaBigInt, - timeoutPrecommitMs: schemaBigInt, - timeoutPrecommitDeltaMs: schemaBigInt, - timeoutRebroadcastMs: schemaBigInt, - targetBlockTimeMs: schemaBigInt, + timeoutProposeMs: schemaUint16, + timeoutProposeDeltaMs: schemaUint16, + timeoutPrevoteMs: schemaUint16, + timeoutPrevoteDeltaMs: schemaUint16, + timeoutPrecommitMs: schemaUint16, + timeoutPrecommitDeltaMs: schemaUint16, + timeoutRebroadcastMs: schemaUint16, + targetBlockTimeMs: schemaUint16, }) .optional(), }) diff --git a/tests/unit/protocol-config-genesis.test.ts b/tests/unit/protocol-config-genesis.test.ts new file mode 100644 index 00000000..41379e31 --- /dev/null +++ b/tests/unit/protocol-config-genesis.test.ts @@ -0,0 +1,82 @@ +// Copyright 2026 Circle Internet Group, Inc. All rights reserved. +// +// SPDX-License-Identifier: Apache-2.0 +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +import { expect } from 'chai' +import { schemaProtocolConfig } from '../../scripts/genesis/ProtocolConfig' + +const proxyAdmin = '0x0000000000000000000000000000000000000001' +const owner = '0x0000000000000000000000000000000000000002' +const controller = '0x0000000000000000000000000000000000000003' +const pauser = '0x0000000000000000000000000000000000000004' + +const validConsensusParams = { + timeoutProposeMs: 3000n, + timeoutProposeDeltaMs: 500n, + timeoutPrevoteMs: 1000n, + timeoutPrevoteDeltaMs: 500n, + timeoutPrecommitMs: 1000n, + timeoutPrecommitDeltaMs: 500n, + timeoutRebroadcastMs: 2000n, + targetBlockTimeMs: 1000n, +} + +const protocolConfig = { + proxy: { + admin: proxyAdmin, + }, + owner, + controller, + pauser, + feeParams: { + alpha: 1n, + kRate: 1n, + inverseElasticityMultiplier: 1n, + minBaseFee: 0n, + maxBaseFee: 1_000_000n, + blockGasLimit: 30_000_000n, + }, + consensusParams: validConsensusParams, +} + +describe('ProtocolConfig genesis schema', () => { + it('accepts consensusParams at the uint16 upper bound', () => { + expect(() => + schemaProtocolConfig.parse({ + ...protocolConfig, + consensusParams: { + ...validConsensusParams, + timeoutProposeMs: 65535n, + }, + }), + ).to.not.throw() + }) + + it('rejects consensusParams values above the uint16 upper bound', () => { + for (const key of Object.keys(validConsensusParams) as Array) { + expect( + () => + schemaProtocolConfig.parse({ + ...protocolConfig, + consensusParams: { + ...validConsensusParams, + [key]: 65536n, + }, + }), + `${key} should reject 65536`, + ).to.throw() + } + }) +})