Skip to content

Commit

Permalink
Merge branch 'main' into nits
Browse files Browse the repository at this point in the history
  • Loading branch information
abi87 authored Sep 23, 2024
2 parents baf279e + 3c142f7 commit e93bd80
Show file tree
Hide file tree
Showing 32 changed files with 1,000 additions and 28 deletions.
2 changes: 1 addition & 1 deletion beacond/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ require (
github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240821052951-c15422305b4e
github.com/berachain/beacon-kit/mod/cli v0.0.0-20240822173558-4e2a8018ae21
github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240821053614-036c5d2945f0
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f
github.com/berachain/beacon-kit/mod/da v0.0.0-20240820191615-398849c34954
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720
github.com/berachain/beacon-kit/mod/execution v0.0.0-20240820191615-398849c34954
Expand Down
4 changes: 2 additions & 2 deletions beacond/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8 h1:
github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8/go.mod h1:3wR01SglBZ+HvDvhbugz/Mxem22JUThJRwo2Ec8Q0No=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685 h1:pTvESOpJ0a6oCV81+pec+zoTBmojZ8C1+7fd4HtKpbI=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685/go.mod h1:jduqdSJEtO5eL43l/MpfnIK81o81m10wzt6Gb++Qi9M=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f h1:Vzglhdv60M7LBS3FBuqK0eUX8vYJBJnL/RwYpxUswpo=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f/go.mod h1:cZd8cFZ+ylhh3/NUbrdXO2ri1/7KOaYBjo1B8MgbgMM=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 h1:rVltwwt4JAhpvqeRDZ8J07e9XM+jzfDCJEghPA4r5lg=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720/go.mod h1:kGxAesqTkt75+QYQ73O9I5PqAicSL9JGh5wypiWgb8I=
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A=
Expand Down
2 changes: 1 addition & 1 deletion mod/cli/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ require (
cosmossdk.io/log v1.4.1
github.com/berachain/beacon-kit/mod/config v0.0.0-20240705193247-d464364483df
github.com/berachain/beacon-kit/mod/consensus v0.0.0-20240821053614-036c5d2945f0
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0
github.com/berachain/beacon-kit/mod/geth-primitives v0.0.0-20240806160829-cde2d1347e7e
Expand Down
4 changes: 2 additions & 2 deletions mod/cli/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@ github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8 h1:
github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8/go.mod h1:3wR01SglBZ+HvDvhbugz/Mxem22JUThJRwo2Ec8Q0No=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685 h1:pTvESOpJ0a6oCV81+pec+zoTBmojZ8C1+7fd4HtKpbI=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685/go.mod h1:jduqdSJEtO5eL43l/MpfnIK81o81m10wzt6Gb++Qi9M=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f h1:Vzglhdv60M7LBS3FBuqK0eUX8vYJBJnL/RwYpxUswpo=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f/go.mod h1:cZd8cFZ+ylhh3/NUbrdXO2ri1/7KOaYBjo1B8MgbgMM=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 h1:rVltwwt4JAhpvqeRDZ8J07e9XM+jzfDCJEghPA4r5lg=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720/go.mod h1:kGxAesqTkt75+QYQ73O9I5PqAicSL9JGh5wypiWgb8I=
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A=
Expand Down
2 changes: 1 addition & 1 deletion mod/config/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ require (
github.com/VictoriaMetrics/fastcache v1.12.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8 // indirect
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e // indirect
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f // indirect
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 // indirect
github.com/bits-and-blooms/bitset v1.13.0 // indirect
github.com/btcsuite/btcd/btcec/v2 v2.3.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions mod/config/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240821052951-c15422305b4e h1
github.com/berachain/beacon-kit/mod/beacon v0.0.0-20240821052951-c15422305b4e/go.mod h1:hyB6jdTwCalJ6IMwCtYOXjXrqIg3T1QruIzs4NPrNkw=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e h1:GTeZshNZaH5MnVhSSGj//vxJfv1kM9d6w2CA7O64gJk=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240806160829-cde2d1347e7e/go.mod h1:ZU1bq1BMt6b0kPRAw+A3kP7FlSd5DSQNYePD5qL9zfQ=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f h1:Vzglhdv60M7LBS3FBuqK0eUX8vYJBJnL/RwYpxUswpo=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f/go.mod h1:cZd8cFZ+ylhh3/NUbrdXO2ri1/7KOaYBjo1B8MgbgMM=
github.com/berachain/beacon-kit/mod/da v0.0.0-20240610210054-bfdc14c4013c h1:WKjF2xYQ3jwTNauJbs34bTzCgf49uHYy7/f+z1DfmyI=
github.com/berachain/beacon-kit/mod/da v0.0.0-20240610210054-bfdc14c4013c/go.mod h1:4hmlXJkl1cwCnZvEISy4K2x9Z7O3Pgtijz1HS/P/uY8=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 h1:rVltwwt4JAhpvqeRDZ8J07e9XM+jzfDCJEghPA4r5lg=
Expand Down
4 changes: 3 additions & 1 deletion mod/consensus-types/pkg/types/payload_header.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,9 @@ type ExecutionPayloadHeader struct {

// Empty returns an empty ExecutionPayload for the given fork version.
func (h *ExecutionPayloadHeader) Empty() *ExecutionPayloadHeader {
return &ExecutionPayloadHeader{}
return &ExecutionPayloadHeader{
BaseFeePerGas: &uint256.Int{},
}
}

// NewFromSSZ returns a new ExecutionPayloadHeader from the given SSZ bytes.
Expand Down
2 changes: 1 addition & 1 deletion mod/consensus/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ require (
cosmossdk.io/store v1.1.1-0.20240418092142-896cdf1971bc
github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8
github.com/berachain/beacon-kit/mod/cli v0.0.0-00010101000000-000000000000
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0
github.com/berachain/beacon-kit/mod/log v0.0.0-20240821000339-4d4242ba4a50
github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240911165923-82f71ec86570
Expand Down
4 changes: 2 additions & 2 deletions mod/consensus/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8 h1:
github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8/go.mod h1:3wR01SglBZ+HvDvhbugz/Mxem22JUThJRwo2Ec8Q0No=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685 h1:pTvESOpJ0a6oCV81+pec+zoTBmojZ8C1+7fd4HtKpbI=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240821182712-08bbb9c7d685/go.mod h1:jduqdSJEtO5eL43l/MpfnIK81o81m10wzt6Gb++Qi9M=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f h1:Vzglhdv60M7LBS3FBuqK0eUX8vYJBJnL/RwYpxUswpo=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f/go.mod h1:cZd8cFZ+ylhh3/NUbrdXO2ri1/7KOaYBjo1B8MgbgMM=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720 h1:rVltwwt4JAhpvqeRDZ8J07e9XM+jzfDCJEghPA4r5lg=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240809202957-3e3f169ad720/go.mod h1:kGxAesqTkt75+QYQ73O9I5PqAicSL9JGh5wypiWgb8I=
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A=
Expand Down
2 changes: 1 addition & 1 deletion mod/node-api/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.23.0

require (
github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0
github.com/berachain/beacon-kit/mod/log v0.0.0-20240807213340-5779c7a563cd
github.com/berachain/beacon-kit/mod/primitives v0.0.0-20240911165923-82f71ec86570
Expand Down Expand Up @@ -82,7 +83,6 @@ require (
github.com/yusufpapurcu/wmi v1.2.4 // indirect
golang.org/x/crypto v0.26.0 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/net v0.28.0 // indirect
golang.org/x/sync v0.8.0 // indirect
golang.org/x/sys v0.24.0 // indirect
golang.org/x/text v0.17.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions mod/node-api/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8 h1:
github.com/berachain/beacon-kit/mod/async v0.0.0-20240821213929-f32b8e2dc5c8/go.mod h1:3wR01SglBZ+HvDvhbugz/Mxem22JUThJRwo2Ec8Q0No=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df h1:mnD1LKqDQ0n+OFdDqOuvKaEiUKRJzsO4V0wyyn/gJYg=
github.com/berachain/beacon-kit/mod/chain-spec v0.0.0-20240705193247-d464364483df/go.mod h1:bTFB4Rdvm7D/WdwPYkqQ+8T0XOMBv0pzXfp1E46BFX8=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f h1:Vzglhdv60M7LBS3FBuqK0eUX8vYJBJnL/RwYpxUswpo=
github.com/berachain/beacon-kit/mod/consensus-types v0.0.0-20240904192942-99aeabe6bb1f/go.mod h1:cZd8cFZ+ylhh3/NUbrdXO2ri1/7KOaYBjo1B8MgbgMM=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197 h1:wVWkiiERY/7kaXvE/VNPPUtYp/l8ky6QSuKM3ThVMXU=
github.com/berachain/beacon-kit/mod/engine-primitives v0.0.0-20240808194557-e72e74f58197/go.mod h1:LiOiqrJhhLH/GPo0XE5fel3EYyi7X6dwBOyTqZakTeQ=
github.com/berachain/beacon-kit/mod/errors v0.0.0-20240806211103-d1105603bfc0 h1:kCSrkb/uVXfMKJPKjf0c7nlJkwn5cNwMxtzRW4zNq2A=
Expand Down
36 changes: 34 additions & 2 deletions mod/node-api/handlers/proof/merkle/beacon_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,16 @@
package merkle

import (
"github.com/berachain/beacon-kit/mod/errors"
"github.com/berachain/beacon-kit/mod/node-api/handlers/proof/types"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/encoding/ssz/merkle"
)

// ProveBeaconStateInBlock generates a proof for the beacon state in the
// beacon block. It uses the fastssz library to generate the proof.
func ProveBeaconStateInBlock(
bbh types.BeaconBlockHeader,
bbh types.BeaconBlockHeader, verifyProof bool,
) ([]common.Root, error) {
blockProofTree, err := bbh.GetTree()
if err != nil {
Expand All @@ -42,7 +44,37 @@ func ProveBeaconStateInBlock(

proof := make([]common.Root, len(stateInBlockProof.Hashes))
for i, hash := range stateInBlockProof.Hashes {
proof[i] = common.Root(hash)
proof[i] = common.NewRootFromBytes(hash)
}

if verifyProof {
if err = verifyBeaconStateInBlock(
bbh, proof, common.NewRootFromBytes(stateInBlockProof.Leaf),
); err != nil {
return nil, err
}
}

return proof, nil
}

// verifyBeaconStateInBlock verifies the beacon state proof in the block.
//
// TODO: verifying the proof is not absolutely necessary.
func verifyBeaconStateInBlock(
bbh types.BeaconBlockHeader,
proof []common.Root,
leaf common.Root,
) error {
beaconRoot := bbh.HashTreeRoot()
if beaconRootVerified, err := merkle.VerifyProof(
StateGIndexDenebBlock, leaf, proof, beaconRoot,
); err != nil {
return err
} else if !beaconRootVerified {
return errors.New(
"beacon state proof failed to verify against beacon root",
)
}
return nil
}
76 changes: 76 additions & 0 deletions mod/node-api/handlers/proof/merkle/beacon_state_test.go
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)
})
}
}
6 changes: 3 additions & 3 deletions mod/node-api/handlers/proof/merkle/block_proposer.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func ProveProposerInBlock[
}

// Then get the proof of the beacon state in the beacon block.
stateInBlockProof, err := ProveBeaconStateInBlock(bbh)
stateInBlockProof, err := ProveBeaconStateInBlock(bbh, false)
if err != nil {
return nil, common.Root{}, err
}
Expand Down Expand Up @@ -102,9 +102,9 @@ func ProveProposerPubkeyInState[

proof := make([]common.Root, len(valPubkeyInStateProof.Hashes))
for i, hash := range valPubkeyInStateProof.Hashes {
proof[i] = common.Root(hash)
proof[i] = common.NewRootFromBytes(hash)
}
return proof, common.Root(valPubkeyInStateProof.Leaf), nil
return proof, common.NewRootFromBytes(valPubkeyInStateProof.Leaf), nil
}

// verifyProposerInBlock verifies the proposer pubkey in the beacon block,
Expand Down
97 changes: 97 additions & 0 deletions mod/node-api/handlers/proof/merkle/block_proposer_test.go
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)
})
}
}
Loading

0 comments on commit e93bd80

Please sign in to comment.