forked from berachain/beacon-kit
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1/2 fix(cli): Align
genesis validator-root
cmd with processing of g…
…enesis validators (berachain#2393) Co-authored-by: aBear <[email protected]> Co-authored-by: Friðrik Ásmundsson <[email protected]>
- Loading branch information
1 parent
1a275ea
commit c1ae2d7
Showing
8 changed files
with
347 additions
and
158 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
//go:build test | ||
// +build test | ||
|
||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2025, Berachain Foundation. All rights reserved. | ||
// Use of this software is governed by the Business Source License included | ||
// in the LICENSE file of this repository and at www.mariadb.com/bsl11. | ||
// | ||
// ANY USE OF THE LICENSED WORK IN VIOLATION OF THIS LICENSE WILL AUTOMATICALLY | ||
// TERMINATE YOUR RIGHTS UNDER THIS LICENSE FOR THE CURRENT AND ALL OTHER | ||
// VERSIONS OF THE LICENSED WORK. | ||
// | ||
// THIS LICENSE DOES NOT GRANT YOU ANY RIGHT IN ANY TRADEMARK OR LOGO OF | ||
// LICENSOR OR ITS AFFILIATES (PROVIDED THAT YOU MAY USE A TRADEMARK OR LOGO OF | ||
// LICENSOR AS EXPRESSLY REQUIRED BY THIS LICENSE). | ||
// | ||
// TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON | ||
// AN “AS IS” BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS, | ||
// EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF | ||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND | ||
// TITLE. | ||
|
||
package genesis_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"math/rand" | ||
"reflect" | ||
"testing" | ||
"testing/quick" | ||
|
||
"github.com/berachain/beacon-kit/cli/commands/genesis" | ||
"github.com/berachain/beacon-kit/config/spec" | ||
"github.com/berachain/beacon-kit/consensus-types/types" | ||
"github.com/berachain/beacon-kit/primitives/common" | ||
"github.com/berachain/beacon-kit/primitives/crypto" | ||
"github.com/berachain/beacon-kit/primitives/math" | ||
"github.com/berachain/beacon-kit/primitives/version" | ||
statetransition "github.com/berachain/beacon-kit/testing/state-transition" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// generator for random deposits. | ||
type TestDeposits []types.Deposit | ||
|
||
func (TestDeposits) Generate(rand *rand.Rand, size int) reflect.Value { | ||
res := make(TestDeposits, size) | ||
for i := range size { | ||
var ( | ||
pubKey crypto.BLSPubkey | ||
creds types.WithdrawalCredentials | ||
sign crypto.BLSSignature | ||
|
||
err error | ||
) | ||
_, err = rand.Read(pubKey[:]) | ||
if err != nil { | ||
panic(fmt.Errorf("failed generating random pubKey: %w", err)) | ||
} | ||
_, err = rand.Read(creds[:]) | ||
if err != nil { | ||
panic(fmt.Errorf("failed generating random cred: %w", err)) | ||
} | ||
_, err = rand.Read(sign[:]) | ||
if err != nil { | ||
panic(fmt.Errorf("failed generating random sign: %w", err)) | ||
} | ||
|
||
res[i] = types.Deposit{ | ||
Pubkey: pubKey, | ||
Credentials: creds, | ||
Amount: math.Gwei(rand.Uint64()), | ||
Signature: sign, | ||
Index: 0, // indexes will be set in order in the test | ||
} | ||
} | ||
return reflect.ValueOf(res) | ||
} | ||
|
||
func TestCompareGenesisCmdWithStateProcessor(t *testing.T) { | ||
qc := &quick.Config{MaxCount: 1_000} | ||
cs, err := spec.MainnetChainSpec() | ||
require.NoError(t, err) | ||
|
||
f := func(inputs TestDeposits) bool { | ||
deposits := make(types.Deposits, len(inputs)) | ||
for i, input := range inputs { | ||
deposits[i] = &types.Deposit{ | ||
Pubkey: input.Pubkey, | ||
Credentials: input.Credentials, | ||
Amount: input.Amount, | ||
Signature: input.Signature, | ||
Index: uint64(i), | ||
} | ||
} | ||
// genesis validators root from CLI | ||
cliValRoot := genesis.ValidatorsRoot(deposits, cs) | ||
|
||
// genesis validators root from StateProcessor | ||
sp, st, _, _ := statetransition.SetupTestState(t, cs) | ||
var ( | ||
genPayloadHeader = new(types.ExecutionPayloadHeader).Empty() | ||
genVersion = version.FromUint32[common.Version](version.Deneb) | ||
) | ||
_, err = sp.InitializePreminedBeaconStateFromEth1( | ||
st, | ||
deposits, | ||
genPayloadHeader, | ||
genVersion, | ||
) | ||
require.NoError(t, err) | ||
|
||
var processorRoot common.Root | ||
processorRoot, err = st.GetGenesisValidatorsRoot() | ||
require.NoError(t, err) | ||
|
||
// assert that they generate the same root, given the same list of deposits | ||
return bytes.Equal(cliValRoot[:], processorRoot[:]) | ||
} | ||
|
||
require.NoError(t, quick.Check(f, qc)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.