-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
1,000 additions
and
28 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
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
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
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
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
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
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,76 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2024, 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 merkle_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" | ||
"github.com/berachain/beacon-kit/mod/node-api/handlers/proof/merkle" | ||
"github.com/berachain/beacon-kit/mod/primitives/pkg/common" | ||
"github.com/berachain/beacon-kit/mod/primitives/pkg/math" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestProveBeaconStateInBlock tests the ProveBeaconStateInBlock function and | ||
// that the generated proof correctly verifies. | ||
func TestProveBeaconStateInBlock(t *testing.T) { | ||
bbh := (&types.BeaconBlockHeader{}).Empty() | ||
|
||
testCases := []struct { | ||
name string | ||
slot math.Slot | ||
proposerIndex math.ValidatorIndex | ||
parentBlockRoot common.Root | ||
bodyRoot common.Root | ||
stateRoot common.Root | ||
expectedProofFile string | ||
}{ | ||
{ | ||
name: "Empty block with non-empty state root", | ||
stateRoot: common.Root{1, 2, 3, 4, 5, 6, 7, 8, 9}, | ||
expectedProofFile: "empty_state_proof.json", | ||
}, | ||
{ | ||
name: "Non-empty block with empty state root", | ||
slot: 4, | ||
proposerIndex: 5, | ||
parentBlockRoot: common.Root{1, 2, 3, 4, 5, 6, 7, 8, 9}, | ||
bodyRoot: common.Root{9, 8, 7, 6, 5, 4, 3, 2, 1}, | ||
expectedProofFile: "non_empty_state_proof.json", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
bbh.SetSlot(tc.slot) | ||
bbh.SetProposerIndex(tc.proposerIndex) | ||
bbh.SetParentBlockRoot(tc.parentBlockRoot) | ||
bbh.SetBodyRoot(tc.bodyRoot) | ||
bbh.SetStateRoot(tc.stateRoot) | ||
|
||
proof, err := merkle.ProveBeaconStateInBlock(bbh, true) | ||
require.NoError(t, err) | ||
expectedProof := ReadProofFromFile(t, tc.expectedProofFile) | ||
require.Equal(t, expectedProof, proof) | ||
}) | ||
} | ||
} |
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,97 @@ | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
// | ||
// Copyright (C) 2024, 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 merkle_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types" | ||
"github.com/berachain/beacon-kit/mod/node-api/handlers/proof/merkle" | ||
"github.com/berachain/beacon-kit/mod/node-api/handlers/proof/merkle/mock" | ||
"github.com/berachain/beacon-kit/mod/primitives/pkg/common" | ||
"github.com/berachain/beacon-kit/mod/primitives/pkg/crypto" | ||
"github.com/berachain/beacon-kit/mod/primitives/pkg/math" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// TestBlockProposerProof tests the ProveProposerInBlock function and | ||
// that the generated proof correctly verifies. | ||
func TestBlockProposerProof(t *testing.T) { | ||
testCases := []struct { | ||
name string | ||
numValidators int | ||
slot math.Slot | ||
proposerIndex math.ValidatorIndex | ||
parentBlockRoot common.Root | ||
bodyRoot common.Root | ||
pubKey crypto.BLSPubkey | ||
expectedProofFile string | ||
}{ | ||
{ | ||
name: "1 Validator Set", | ||
numValidators: 1, | ||
slot: 4, | ||
proposerIndex: 0, | ||
parentBlockRoot: common.Root{1, 2, 3}, | ||
bodyRoot: common.Root{3, 2, 1}, | ||
pubKey: [48]byte{9, 8, 7, 6, 5, 4, 3, 2, 1}, | ||
expectedProofFile: "one_validator_proposer_proof.json", | ||
}, | ||
{ | ||
name: "Many Validator Set", | ||
numValidators: 100, | ||
slot: 5, | ||
proposerIndex: 95, | ||
parentBlockRoot: common.Root{1, 2, 3, 4, 5, 6}, | ||
bodyRoot: common.Root{3, 2, 1, 9, 8, 7}, | ||
pubKey: [48]byte{9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 1, 2}, | ||
expectedProofFile: "many_validators_proposer_proof.json", | ||
}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
vals := make(types.Validators, tc.numValidators) | ||
for i := range vals { | ||
vals[i] = &types.Validator{} | ||
} | ||
vals[tc.proposerIndex] = &types.Validator{Pubkey: tc.pubKey} | ||
|
||
bs, err := mock.NewBeaconState( | ||
tc.slot, vals, 0, common.ExecutionAddress{}, | ||
) | ||
require.NoError(t, err) | ||
|
||
bbh := (&types.BeaconBlockHeader{}).New( | ||
tc.slot, | ||
tc.proposerIndex, | ||
tc.parentBlockRoot, | ||
bs.HashTreeRoot(), | ||
tc.bodyRoot, | ||
) | ||
|
||
proof, _, err := merkle.ProveProposerInBlock(bbh, bs) | ||
require.NoError(t, err) | ||
expectedProof := ReadProofFromFile(t, tc.expectedProofFile) | ||
require.Equal(t, expectedProof, proof) | ||
}) | ||
} | ||
} |
Oops, something went wrong.