Skip to content

Commit

Permalink
feat(genesis): Re-work the entire genesis process (#1176)
Browse files Browse the repository at this point in the history
* bet

* x

* bet

* bet

* bet

* bet

* bet

* bet

* bet

* bet

* bet

* x

* bet

* rename

* bet

* bet
  • Loading branch information
itsdevbear authored May 24, 2024
1 parent 0bd5c01 commit 3fd1607
Show file tree
Hide file tree
Showing 37 changed files with 497 additions and 770 deletions.
2 changes: 1 addition & 1 deletion .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ linters:
- misspell # [useless] finds commonly misspelled English words in comments
#- nlreturn # [too strict and mostly code is not more readable] checks for a new line before return and branch statements to increase code clarity
#- paralleltest # [too many false positives] detects missing usage of t.Parallel() method in your Go test
- tagliatelle # checks the struct tags
# - tagliatelle # checks the struct tags
- thelper # detects golang test helpers without t.Helper() call and checks the consistency of test helpers
#- wsl # [too strict and mostly code is not more readable] whitespace linter forces you to use empty lines

Expand Down
1 change: 0 additions & 1 deletion beacond/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,6 @@ require (
github.com/lib/pq v1.10.9 // indirect
github.com/libp2p/go-buffer-pool v0.1.0 // indirect
github.com/linxGnu/grocksdb v1.8.14 // indirect
github.com/logrusorgru/aurora v2.0.3+incompatible // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/manifoldco/promptui v0.9.0 // indirect
Expand Down
2 changes: 0 additions & 2 deletions beacond/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -434,8 +434,6 @@ github.com/libp2p/go-buffer-pool v0.1.0 h1:oK4mSFcQz7cTQIfqbe4MIj9gLW+mnanjyFtc6
github.com/libp2p/go-buffer-pool v0.1.0/go.mod h1:N+vh8gMqimBzdKkSMVuydVDq+UV5QTWy5HSiZacSbPg=
github.com/linxGnu/grocksdb v1.8.14 h1:HTgyYalNwBSG/1qCQUIott44wU5b2Y9Kr3z7SK5OfGQ=
github.com/linxGnu/grocksdb v1.8.14/go.mod h1:QYiYypR2d4v63Wj1adOOfzglnoII0gLj3PNh4fZkcFA=
github.com/logrusorgru/aurora v2.0.3+incompatible h1:tOpm7WcpBTn4fjmVfgpQq0EfczGlG91VSDkswnjF5A8=
github.com/logrusorgru/aurora v2.0.3+incompatible/go.mod h1:7rIyQOR62GCctdiQpZ/zOJlFyk6y+94wXzv6RNZgaR4=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
Expand Down
2 changes: 2 additions & 0 deletions go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1048,6 +1048,8 @@ github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515 h1:T+h1c/A9Gawja4Y9mFVWj
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
github.com/kr/pty v1.1.1 h1:VkoXIwSboBpnk99O/KFauAEILuNHv5DVFKZMBN/gUgw=
github.com/kurtosis-tech/kurtosis/api/golang v0.89.11 h1:+bkOA6UMOchLHZ27n09JLy3TudyBwZZyUUgg87jnN7Y=
github.com/kurtosis-tech/kurtosis/api/golang v0.89.11/go.mod h1:9T22P7Vv3j5g6sbm78DxHQ4s9C4Cj3s9JjFQ7DFyYpM=
github.com/kurtosis-tech/kurtosis/cloud/api/golang v0.0.0-20230803130419-099ee7a4e3dc h1:/6Ffp1LIbBf8nL9EyCSmNps5PGwWu65ogHk8LBGaP5E=
github.com/kurtosis-tech/kurtosis/cloud/api/golang v0.0.0-20230803130419-099ee7a4e3dc/go.mod h1:+dhneovUimcuMPeYhMRXbit5kII/SIqvtfj6jTShOLo=
github.com/labstack/echo/v4 v4.1.11/go.mod h1:i541M3Fj6f76NZtHSj7TXnyM8n2gaodfvfxNnFqi74g=
Expand Down
6 changes: 3 additions & 3 deletions kurtosis/src/nodes/consensus/beacond/launcher.star
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,9 @@ def init_consensus_nodes():
genesis_file = "{}/config/genesis.json".format("$BEACOND_HOME")

# Check if genesis file exists, if not then initialize the beacond
init_node = "if [ ! -f {} ]; then /usr/bin/beacond init --chain-id {} {} --home {} --beacon-kit.accept-tos; fi".format(genesis_file, "$BEACOND_CHAIN_ID", "$BEACOND_MONIKER", "$BEACOND_HOME")
add_validator = "/usr/bin/beacond genesis add-validator --home {} --beacon-kit.accept-tos".format("$BEACOND_HOME")
collect_gentx = "/usr/bin/beacond genesis collect-validators --home {}".format("$BEACOND_HOME")
init_node = "if [ ! -f {} ]; then /usr/bin/beacond init --chain-id {} {} --home {}; fi".format(genesis_file, "$BEACOND_CHAIN_ID", "$BEACOND_MONIKER", "$BEACOND_HOME")
add_validator = "/usr/bin/beacond genesis add-premined-deposit --home {}".format("$BEACOND_HOME")
collect_gentx = "/usr/bin/beacond genesis collect-premined-deposits --home {}".format("$BEACOND_HOME")
return "{} && {} && {}".format(init_node, add_validator, collect_gentx)

def create_full_node_config(plan, cl_image, peers, paired_el_client_name, jwt_file = None, kzg_trusted_setup_file = None, index = 0):
Expand Down
12 changes: 6 additions & 6 deletions kurtosis/src/nodes/consensus/beacond/node.star
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ def create_beacond_config_directory(plan, cl_service_name):
TMP_GENESIS = "{}/config/tmp_genesis.json".format("$BEACOND_HOME")

def add_validator(plan, cl_service_name):
command = "/usr/bin/beacond genesis add-validator --home {} --beacon-kit.accept-tos".format("$BEACOND_HOME")
command = "/usr/bin/beacond genesis add-premined-deposit --home {}".format("$BEACOND_HOME")
bash.exec_on_service(plan, cl_service_name, command)

def collect_validator(plan, cl_service_name):
command = "/usr/bin/beacond genesis collect-validators --home {}".format("$BEACOND_HOME")
command = "/usr/bin/beacond genesis collect-premined-deposits --home {}".format("$BEACOND_HOME")
bash.exec_on_service(plan, cl_service_name, command)

def start(persistent_peers):
Expand All @@ -34,7 +34,7 @@ def start(persistent_peers):
start_node = "/usr/bin/beacond start \
--beacon-kit.engine.jwt-secret-path=/root/jwt/jwt-secret.hex \
--beacon-kit.kzg.trusted-setup-path=/root/kzg/kzg-trusted-setup.json \
--beacon-kit.accept-tos --beacon-kit.engine.rpc-dial-url {} \
--beacon-kit.engine.rpc-dial-url {} \
--rpc.laddr tcp://0.0.0.0:26657 \
--grpc.address 0.0.0.0:9090 --api.address tcp://0.0.0.0:1317 \
--api.enable {}".format("$BEACOND_ENGINE_DIAL_URL", persistent_peers_option)
Expand All @@ -45,15 +45,15 @@ def get_init_sh():
genesis_file = "{}/config/genesis.json".format("$BEACOND_HOME")

# Check if genesis file exists, if not then initialize the beacond
command = "if [ ! -f {} ]; then /usr/bin/beacond init --chain-id {} {} --home {} --consensus-key-algo {} --beacon-kit.accept-tos; fi".format(genesis_file, "$BEACOND_CHAIN_ID", "$BEACOND_MONIKER", "$BEACOND_HOME", "$BEACOND_CONSENSUS_KEY_ALGO")
command = "if [ ! -f {} ]; then /usr/bin/beacond init --chain-id {} {} --home {} --consensus-key-algo {}; fi".format(genesis_file, "$BEACOND_CHAIN_ID", "$BEACOND_MONIKER", "$BEACOND_HOME", "$BEACOND_CONSENSUS_KEY_ALGO")
return command

def get_add_validator_sh():
command = "/usr/bin/beacond genesis add-validator --home {} --beacon-kit.accept-tos".format("$BEACOND_HOME")
command = "/usr/bin/beacond genesis add-premined-deposit --home {}".format("$BEACOND_HOME")
return command

def get_collect_validator_sh():
command = "/usr/bin/beacond genesis collect-validators --home {}".format("$BEACOND_HOME")
command = "/usr/bin/beacond genesis collect-premined-deposits --home {}".format("$BEACOND_HOME")
return command

def get_execution_payload_sh():
Expand Down
12 changes: 6 additions & 6 deletions kurtosis/src/nodes/consensus/beacond/scripts/multiple-gentx.sh
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
#!/bin/bash

/usr/bin/beacond init --beacon-kit.accept-tos --chain-id $BEACOND_CHAIN_ID $BEACOND_MONIKER --home /tmp/config0/.beacond --consensus-key-algo $BEACOND_CONSENSUS_KEY_ALGO
/usr/bin/beacond genesis add-validator --beacon-kit.accept-tos --home /tmp/config0/.beacond
/usr/bin/beacond init --chain-id $BEACOND_CHAIN_ID $BEACOND_MONIKER --home /tmp/config0/.beacond --consensus-key-algo $BEACOND_CONSENSUS_KEY_ALGO
/usr/bin/beacond genesis add-premined-deposit --home /tmp/config0/.beacond
cp -r /tmp/config0 /tmp/config_genesis

for ((i=1; i<$NUM_VALS; i++)); do
BEACOND_HOME=/tmp/config${i}/.beacond
echo $BEACOND_HOME
BEACOND_MONIKER=cl-validator-beaconkit-${i}
/usr/bin/beacond init --beacon-kit.accept-tos --chain-id $BEACOND_CHAIN_ID $BEACOND_MONIKER --home $BEACOND_HOME --consensus-key-algo $BEACOND_CONSENSUS_KEY_ALGO
/usr/bin/beacond genesis add-validator --beacon-kit.accept-tos --home $BEACOND_HOME
/usr/bin/beacond init --chain-id $BEACOND_CHAIN_ID $BEACOND_MONIKER --home $BEACOND_HOME --consensus-key-algo $BEACOND_CONSENSUS_KEY_ALGO
/usr/bin/beacond genesis add-premined-deposit --home $BEACOND_HOME
cp -r /tmp/config${i}/.beacond/config/gentx/gen* /tmp/config_genesis/.beacond/config/gentx/
done

/usr/bin/beacond genesis execution-payload --beacon-kit.accept-tos $ETH_GENESIS --home /tmp/config_genesis/.beacond
/usr/bin/beacond genesis collect-validators --beacon-kit.accept-tos --home /tmp/config_genesis/.beacond
/usr/bin/beacond genesis execution-payload $ETH_GENESIS --home /tmp/config_genesis/.beacond
/usr/bin/beacond genesis collect-premined-deposits --home /tmp/config_genesis/.beacond

21 changes: 8 additions & 13 deletions mod/beacon/blockchain/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ package blockchain
import (
"context"

"github.com/berachain/beacon-kit/mod/consensus-types/pkg/state/deneb"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/genesis"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
engineprimitives "github.com/berachain/beacon-kit/mod/primitives-engine"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
Expand All @@ -45,21 +45,16 @@ func (s *Service[
DepositStoreT,
]) ProcessGenesisState(
ctx context.Context,
genesisData *deneb.BeaconState,
genesisData *genesis.Genesis[
*types.Deposit, *types.ExecutionPayloadHeaderDeneb,
],
) ([]*transition.ValidatorUpdate, error) {
// Process the genesis state using the state processor.
valUpdates, err := s.sp.ProcessGenesisState(
&transition.Context{
Context: ctx,
},
return s.sp.InitializePreminedBeaconStateFromEth1(
s.sb.StateFromContext(ctx),
genesisData,
genesisData.Deposits,
genesisData.ExecutionPayloadHeader,
genesisData.ForkVersion,
)
if err != nil {
return nil, err
}

return valUpdates, nil
}

// ProcessStateTransition receives an incoming beacon block, it first validates
Expand Down
9 changes: 5 additions & 4 deletions mod/beacon/blockchain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ package blockchain
import (
"context"

"github.com/berachain/beacon-kit/mod/consensus-types/pkg/state/deneb"
"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives"
engineprimitives "github.com/berachain/beacon-kit/mod/primitives-engine"
Expand Down Expand Up @@ -183,11 +182,13 @@ type StateProcessor[
] interface {
// ProcessGenesisState processes the genesis state.
// TODO: move to an actual genesis type.
ProcessGenesisState(
ctx ContextT,
InitializePreminedBeaconStateFromEth1(
st BeaconStateT,
data *deneb.BeaconState,
deposits []*types.Deposit,
executionPayloadHeader engineprimitives.ExecutionPayloadHeader,
genesisVersion primitives.Version,
) ([]*transition.ValidatorUpdate, error)

// ProcessSlot processes the state transition for a single slot.
//
// TODO: This eventually needs to be deprecated.
Expand Down
1 change: 1 addition & 0 deletions mod/beacon/validator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,5 +149,6 @@ func (s *Service[BeaconStateT, BlobSidecarsT]) prepareStateForBuilding(
stateSlot,
)
}

return nil
}
137 changes: 137 additions & 0 deletions mod/consensus-types/pkg/genesis/genesis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
// SPDX-License-Identifier: MIT
//
// Copyright (c) 2024 Berachain Foundation
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

package genesis

import (
"context"
"math/big"

"github.com/berachain/beacon-kit/mod/consensus-types/pkg/types"
"github.com/berachain/beacon-kit/mod/primitives"
engineprimitives "github.com/berachain/beacon-kit/mod/primitives-engine"
"github.com/berachain/beacon-kit/mod/primitives/pkg/common"
"github.com/berachain/beacon-kit/mod/primitives/pkg/constants"
"github.com/berachain/beacon-kit/mod/primitives/pkg/math"
"github.com/berachain/beacon-kit/mod/primitives/pkg/version"
"golang.org/x/sync/errgroup"
)

// Genesis is a struct that contains the genesis information
// need to start the beacon chain.
type Genesis[
DepositT any,
ExecutonPayloadHeaderT engineprimitives.ExecutionPayloadHeader,
] struct {
// ForkVersion is the fork version of the genesis slot.
ForkVersion primitives.Version `json:"fork_version"`

// Deposits represents the deposits in the genesis. Deposits are
// used to initialize the validator set.
Deposits []DepositT `json:"deposits"`

// ExecutionPayloadHeader is the header of the execution payload
// in the genesis.
ExecutionPayloadHeader ExecutonPayloadHeaderT `json:"execution_payload_header"`
}

// DefaultGenesis returns a the default genesis.
func DefaultGenesisDeneb() *Genesis[
*types.Deposit, *types.ExecutionPayloadHeaderDeneb,
] {
defaultHeader, err :=
DefaultGenesisExecutionPayloadHeaderDeneb()
if err != nil {
panic(err)
}

return &Genesis[*types.Deposit, *types.ExecutionPayloadHeaderDeneb]{
ForkVersion: version.FromUint32[primitives.Version](
version.Deneb,
),
Deposits: make([]*types.Deposit, 0),
ExecutionPayloadHeader: defaultHeader,
}
}

// DefaultGenesisExecutionPayloadHeaderDeneb returns a default
// ExecutionPayloadHeaderDeneb.
func DefaultGenesisExecutionPayloadHeaderDeneb() (
*types.ExecutionPayloadHeaderDeneb, error,
) {
// Get the merkle roots of empty transactions and withdrawals in parallel.
var (
g, _ = errgroup.WithContext(context.Background())
emptyTxsRoot primitives.Root
emptyWithdrawalsRoot primitives.Root
)

g.Go(func() error {
var err error
emptyTxsRoot, err = engineprimitives.Transactions{}.HashTreeRoot()
return err
})

g.Go(func() error {
var err error
emptyWithdrawalsRoot, err = engineprimitives.Withdrawals{}.HashTreeRoot()
return err
})

// If deriving either of the roots fails, return the error.
if err := g.Wait(); err != nil {
return nil, err
}

return &types.ExecutionPayloadHeaderDeneb{
ParentHash: common.ZeroHash,
FeeRecipient: common.ZeroAddress,
StateRoot: primitives.Bytes32(common.Hex2BytesFixed(
"0x12965ab9cbe2d2203f61d23636eb7e998f167cb79d02e452f532535641e35bcc",
constants.RootLength,
)),
ReceiptsRoot: primitives.Bytes32(common.Hex2BytesFixed(
"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
constants.RootLength,
)),
LogsBloom: make([]byte, constants.LogsBloomLength),
Random: primitives.Bytes32{},
Number: 0,
//nolint:mnd // default value.
GasLimit: math.U64(30000000),
GasUsed: 0,
Timestamp: 0,
ExtraData: make([]byte, constants.ExtraDataLength),
//nolint:mnd // default value.
BaseFeePerGas: math.MustNewU256LFromBigInt(big.NewInt(3906250)),
BlockHash: common.HexToHash(
"0xcfff92cd918a186029a847b59aca4f83d3941df5946b06bca8de0861fc5d0850",
),
TransactionsRoot: emptyTxsRoot,
WithdrawalsRoot: emptyWithdrawalsRoot,
BlobGasUsed: 0,
ExcessBlobGas: 0,
}, nil
}
Loading

0 comments on commit 3fd1607

Please sign in to comment.