Skip to content

Commit

Permalink
feat(primitives): Move fork and signature prims to consensus primitiv…
Browse files Browse the repository at this point in the history
…es (#753)
  • Loading branch information
itsdevbear committed Apr 15, 2024
1 parent 75ba5ae commit 81aff01
Show file tree
Hide file tree
Showing 11 changed files with 36 additions and 30 deletions.
7 changes: 4 additions & 3 deletions mod/core/randao/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
beacontypes "github.com/berachain/beacon-kit/mod/core/types"
"github.com/berachain/beacon-kit/mod/node-builder/config"
"github.com/berachain/beacon-kit/mod/primitives"
consensusprimitives "github.com/berachain/beacon-kit/mod/primitives-consensus"
"github.com/berachain/beacon-kit/mod/primitives/constants"
"github.com/go-faster/xor"
blst "github.com/itsdevbear/comet-bls12-381/bls/blst"
Expand Down Expand Up @@ -194,18 +195,18 @@ func (p *Processor) computeSigningRoot(
epoch primitives.Epoch,
genesisValidatorsRoot primitives.Root,
) (primitives.Root, error) {
fd := primitives.NewForkData(
fd := consensusprimitives.NewForkData(
version.FromUint32(
p.cfg.Beacon.ActiveForkVersionForEpoch(epoch),
), genesisValidatorsRoot,
)

signingDomain, err := fd.ComputeDomain(primitives.DomainTypeRandao)
signingDomain, err := fd.ComputeDomain(consensusprimitives.DomainTypeRandao)
if err != nil {
return primitives.Root{}, err
}

signingRoot, err := primitives.ComputeSigningRootUInt64(
signingRoot, err := consensusprimitives.ComputeSigningRootUInt64(
uint64(epoch),
signingDomain,
)
Expand Down
2 changes: 1 addition & 1 deletion mod/core/state_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ func (sp *StateProcessor) createValidator(
epoch = sp.cfg.SlotToEpoch(slot)

// Get the fork data for the current epoch.
fd := primitives.NewForkData(
fd := consensusprimitives.NewForkData(
version.FromUint32(
sp.cfg.ActiveForkVersionForEpoch(epoch),
), genesisValidatorsRoot,
Expand Down
2 changes: 1 addition & 1 deletion mod/core/types/validator.ssz.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion mod/node-builder/commands/deposit/deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func validateDepositMessage(_ *cobra.Command, args []string) error {
}

return depositMessage.VerifyCreateValidator(
primitives.NewForkData(currentVersion, genesisValidatorRoot),
consensusprimitives.NewForkData(currentVersion, genesisValidatorRoot),
signature,
blst.VerifySignaturePubkeyBytes,
)
Expand Down
6 changes: 3 additions & 3 deletions mod/primitives-consensus/deposit_message.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,16 @@ type DepositMessage struct {
// VerifyDeposit verifies the deposit data when attempting to create a
// new validator from a given deposit.
func (d *DepositMessage) VerifyCreateValidator(
forkData *primitives.ForkData,
forkData *ForkData,
signature primitives.BLSSignature,
isSignatureValid SigVerificationFn,
) error {
domain, err := forkData.ComputeDomain(primitives.DomainTypeDeposit)
domain, err := forkData.ComputeDomain(DomainTypeDeposit)
if err != nil {
return err
}

signingRoot, err := primitives.ComputeSigningRoot(d, domain)
signingRoot, err := ComputeSigningRoot(d, domain)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion mod/primitives-consensus/deposit_message.ssz.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

package primitives
package consensusprimitives

import "github.com/berachain/beacon-kit/mod/primitives"

type (
// Domain as per the Ethereum 2.0 Specification:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
//nolint:lll
Domain = Bytes32
Domain = primitives.Bytes32

// DomainType as per the Ethereum 2.0 Specification:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#custom-types
//nolint:lll
DomainType = Bytes4
DomainType = primitives.Bytes4
)

// Domain constants as defined in the Ethereum 2.0 specification:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,25 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

package primitives
package consensusprimitives

import "github.com/berachain/beacon-kit/mod/primitives"

// ForkData as defined in the Ethereum 2.0 specification:
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#forkdata
//
//go:generate go run github.com/ferranbt/fastssz/sszgen -path fork_data.go -objs ForkData -include ./bytes.go,./primitives.go -output fork_data.ssz.go
//go:generate go run github.com/ferranbt/fastssz/sszgen -path fork_data.go -objs ForkData -include ../primitives//bytes.go,../primitives/primitives.go -output fork_data.ssz.go
//nolint:lll
type ForkData struct {
// CurrentVersion is the current version of the fork.
CurrentVersion Version `ssz-size:"4"`
CurrentVersion primitives.Version `ssz-size:"4"`
// GenesisValidatorsRoot is the root of the genesis validators.
GenesisValidatorsRoot Root `ssz-size:"32"`
GenesisValidatorsRoot primitives.Root `ssz-size:"32"`
}

// NewForkData creates a new ForkData struct.
func NewForkData(
currentVersion Version, genesisValidatorsRoot Root,
currentVersion primitives.Version, genesisValidatorsRoot primitives.Root,
) *ForkData {
return &ForkData{
CurrentVersion: currentVersion,
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

package primitives
package consensusprimitives

import (
"encoding/binary"

"github.com/berachain/beacon-kit/mod/primitives"
"github.com/berachain/beacon-kit/mod/primitives/constants"
)

// SigningData as defined in the Ethereum 2.0 specification.
// https://github.com/ethereum/consensus-specs/blob/dev/specs/phase0/beacon-chain.md#signingdata
//
//nolint:lll
//go:generate go run github.com/ferranbt/fastssz/sszgen -path signing_data.go -objs SigningData -include bytes.go,domain.go,primitives.go -output signing_data.ssz.go
//go:generate go run github.com/ferranbt/fastssz/sszgen -path signing_data.go -objs SigningData -include ../primitives/bytes.go,./domain.go,../primitives/primitives.go -output signing_data.ssz.go
type SigningData struct {
ObjectRoot Root `ssz-size:"32"`
Domain Domain `ssz-size:"32"`
ObjectRoot primitives.Root `ssz-size:"32"`
Domain Domain `ssz-size:"32"`
}

// ComputeSigningRoot as defined in the Ethereum 2.0 specification.
Expand All @@ -48,10 +49,10 @@ type SigningData struct {
func ComputeSigningRoot(
sszObject interface{ HashTreeRoot() ([32]byte, error) },
domain Domain,
) (Root, error) {
) (primitives.Root, error) {
objectRoot, err := sszObject.HashTreeRoot()
if err != nil {
return Root{}, err
return primitives.Root{}, err
}
return (&SigningData{
ObjectRoot: objectRoot,
Expand All @@ -63,11 +64,11 @@ func ComputeSigningRoot(
func ComputeSigningRootUInt64(
value uint64,
domain Domain,
) (Root, error) {
) (primitives.Root, error) {
bz := make([]byte, constants.RootLength)
binary.LittleEndian.PutUint64(bz, value)
return (&SigningData{
ObjectRoot: Root(bz),
ObjectRoot: primitives.Root(bz),
Domain: domain,
}).HashTreeRoot()
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 81aff01

Please sign in to comment.