Unverified Commit 3a5aceed authored by Felföldi Zsolt's avatar Felföldi Zsolt Committed by GitHub

beacon/engine: move core/beacon to beacon/engine (#26616)

This PR moves core/beacon to beacon/engine so that beacon-chain related code has its own top level package which also can house the the beacon lightclient-code.
parent 8860b397
......@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package beacon
package engine
import (
"github.com/ethereum/go-ethereum/common"
......
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package beacon
package engine
import (
"encoding/json"
......
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package beacon
package engine
import (
"encoding/json"
......
// Code generated by github.com/fjl/gencodec. DO NOT EDIT.
package beacon
package engine
import (
"encoding/json"
......
......@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
package beacon
package engine
import (
"fmt"
......
This diff is collapsed.
This diff is collapsed.
......@@ -19,8 +19,8 @@ package catalyst
import (
"sync"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/miner"
)
......@@ -38,7 +38,7 @@ const maxTrackedHeaders = 10
// payloadQueueItem represents an id->payload tuple to store until it's retrieved
// or evicted.
type payloadQueueItem struct {
id beacon.PayloadID
id engine.PayloadID
payload *miner.Payload
}
......@@ -58,7 +58,7 @@ func newPayloadQueue() *payloadQueue {
}
// put inserts a new payload into the queue at the given id.
func (q *payloadQueue) put(id beacon.PayloadID, payload *miner.Payload) {
func (q *payloadQueue) put(id engine.PayloadID, payload *miner.Payload) {
q.lock.Lock()
defer q.lock.Unlock()
......@@ -70,7 +70,7 @@ func (q *payloadQueue) put(id beacon.PayloadID, payload *miner.Payload) {
}
// get retrieves a previously stored payload item or nil if it does not exist.
func (q *payloadQueue) get(id beacon.PayloadID) *beacon.ExecutionPayloadEnvelope {
func (q *payloadQueue) get(id engine.PayloadID) *engine.ExecutionPayloadEnvelope {
q.lock.RLock()
defer q.lock.RUnlock()
......@@ -86,7 +86,7 @@ func (q *payloadQueue) get(id beacon.PayloadID) *beacon.ExecutionPayloadEnvelope
}
// has checks if a particular payload is already tracked.
func (q *payloadQueue) has(id beacon.PayloadID) bool {
func (q *payloadQueue) has(id engine.PayloadID) bool {
q.lock.RLock()
defer q.lock.RUnlock()
......
......@@ -21,9 +21,9 @@ import (
"errors"
"fmt"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/les"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/node"
......@@ -70,43 +70,43 @@ func NewConsensusAPI(les *les.LightEthereum) *ConsensusAPI {
//
// If there are payloadAttributes: we return an error since block creation is not
// supported in les mode.
func (api *ConsensusAPI) ForkchoiceUpdatedV1(heads beacon.ForkchoiceStateV1, payloadAttributes *beacon.PayloadAttributes) (beacon.ForkChoiceResponse, error) {
func (api *ConsensusAPI) ForkchoiceUpdatedV1(heads engine.ForkchoiceStateV1, payloadAttributes *engine.PayloadAttributes) (engine.ForkChoiceResponse, error) {
if heads.HeadBlockHash == (common.Hash{}) {
log.Warn("Forkchoice requested update to zero hash")
return beacon.STATUS_INVALID, nil // TODO(karalabe): Why does someone send us this?
return engine.STATUS_INVALID, nil // TODO(karalabe): Why does someone send us this?
}
if err := api.checkTerminalTotalDifficulty(heads.HeadBlockHash); err != nil {
if header := api.les.BlockChain().GetHeaderByHash(heads.HeadBlockHash); header == nil {
// TODO (MariusVanDerWijden) trigger sync
return beacon.STATUS_SYNCING, nil
return engine.STATUS_SYNCING, nil
}
return beacon.STATUS_INVALID, err
return engine.STATUS_INVALID, err
}
// If the finalized block is set, check if it is in our blockchain
if heads.FinalizedBlockHash != (common.Hash{}) {
if header := api.les.BlockChain().GetHeaderByHash(heads.FinalizedBlockHash); header == nil {
// TODO (MariusVanDerWijden) trigger sync
return beacon.STATUS_SYNCING, nil
return engine.STATUS_SYNCING, nil
}
}
// SetHead
if err := api.setCanonical(heads.HeadBlockHash); err != nil {
return beacon.STATUS_INVALID, err
return engine.STATUS_INVALID, err
}
if payloadAttributes != nil {
return beacon.STATUS_INVALID, errors.New("not supported")
return engine.STATUS_INVALID, errors.New("not supported")
}
return api.validForkChoiceResponse(), nil
}
// GetPayloadV1 returns a cached payload by id. It's not supported in les mode.
func (api *ConsensusAPI) GetPayloadV1(payloadID beacon.PayloadID) (*beacon.ExecutableData, error) {
return nil, beacon.GenericServerError.With(errors.New("not supported in light client mode"))
func (api *ConsensusAPI) GetPayloadV1(payloadID engine.PayloadID) (*engine.ExecutableData, error) {
return nil, engine.GenericServerError.With(errors.New("not supported in light client mode"))
}
// ExecutePayloadV1 creates an Eth1 block, inserts it in the chain, and returns the status of the chain.
func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableData) (beacon.PayloadStatusV1, error) {
block, err := beacon.ExecutableDataToBlock(params)
func (api *ConsensusAPI) ExecutePayloadV1(params engine.ExecutableData) (engine.PayloadStatusV1, error) {
block, err := engine.ExecutableDataToBlock(params)
if err != nil {
return api.invalid(), err
}
......@@ -118,7 +118,7 @@ func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableData) (beacon.
}
*/
// TODO (MariusVanDerWijden) we should return nil here not empty hash
return beacon.PayloadStatusV1{Status: beacon.SYNCING, LatestValidHash: nil}, nil
return engine.PayloadStatusV1{Status: engine.SYNCING, LatestValidHash: nil}, nil
}
parent := api.les.BlockChain().GetHeaderByHash(params.ParentHash)
if parent == nil {
......@@ -136,20 +136,20 @@ func (api *ConsensusAPI) ExecutePayloadV1(params beacon.ExecutableData) (beacon.
merger.ReachTTD()
}
hash := block.Hash()
return beacon.PayloadStatusV1{Status: beacon.VALID, LatestValidHash: &hash}, nil
return engine.PayloadStatusV1{Status: engine.VALID, LatestValidHash: &hash}, nil
}
func (api *ConsensusAPI) validForkChoiceResponse() beacon.ForkChoiceResponse {
func (api *ConsensusAPI) validForkChoiceResponse() engine.ForkChoiceResponse {
currentHash := api.les.BlockChain().CurrentHeader().Hash()
return beacon.ForkChoiceResponse{
PayloadStatus: beacon.PayloadStatusV1{Status: beacon.VALID, LatestValidHash: &currentHash},
return engine.ForkChoiceResponse{
PayloadStatus: engine.PayloadStatusV1{Status: engine.VALID, LatestValidHash: &currentHash},
}
}
// invalid returns a response "INVALID" with the latest valid hash set to the current head.
func (api *ConsensusAPI) invalid() beacon.PayloadStatusV1 {
func (api *ConsensusAPI) invalid() engine.PayloadStatusV1 {
currentHash := api.les.BlockChain().CurrentHeader().Hash()
return beacon.PayloadStatusV1{Status: beacon.INVALID, LatestValidHash: &currentHash}
return engine.PayloadStatusV1{Status: engine.INVALID, LatestValidHash: &currentHash}
}
func (api *ConsensusAPI) checkTerminalTotalDifficulty(head common.Hash) error {
......@@ -193,7 +193,7 @@ func (api *ConsensusAPI) setCanonical(newHead common.Hash) error {
// ExchangeTransitionConfigurationV1 checks the given configuration against
// the configuration of the node.
func (api *ConsensusAPI) ExchangeTransitionConfigurationV1(config beacon.TransitionConfigurationV1) (*beacon.TransitionConfigurationV1, error) {
func (api *ConsensusAPI) ExchangeTransitionConfigurationV1(config engine.TransitionConfigurationV1) (*engine.TransitionConfigurationV1, error) {
log.Trace("Engine API request received", "method", "ExchangeTransitionConfiguration", "ttd", config.TerminalTotalDifficulty)
if config.TerminalTotalDifficulty == nil {
return nil, errors.New("invalid terminal total difficulty")
......@@ -207,7 +207,7 @@ func (api *ConsensusAPI) ExchangeTransitionConfigurationV1(config beacon.Transit
if config.TerminalBlockHash != (common.Hash{}) {
if hash := api.les.BlockChain().GetCanonicalHash(uint64(config.TerminalBlockNumber)); hash == config.TerminalBlockHash {
return &beacon.TransitionConfigurationV1{
return &engine.TransitionConfigurationV1{
TerminalTotalDifficulty: (*hexutil.Big)(ttd),
TerminalBlockHash: config.TerminalBlockHash,
TerminalBlockNumber: config.TerminalBlockNumber,
......@@ -216,5 +216,5 @@ func (api *ConsensusAPI) ExchangeTransitionConfigurationV1(config beacon.Transit
return nil, fmt.Errorf("invalid terminal block hash")
}
return &beacon.TransitionConfigurationV1{TerminalTotalDifficulty: (*hexutil.Big)(ttd)}, nil
return &engine.TransitionConfigurationV1{TerminalTotalDifficulty: (*hexutil.Big)(ttd)}, nil
}
......@@ -20,10 +20,10 @@ import (
"math/big"
"testing"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/eth/downloader"
......@@ -84,7 +84,7 @@ func TestSetHeadBeforeTotalDifficulty(t *testing.T) {
defer n.Close()
api := NewConsensusAPI(lesService)
fcState := beacon.ForkchoiceStateV1{
fcState := engine.ForkchoiceStateV1{
HeadBlockHash: blocks[5].Hash(),
SafeBlockHash: common.Hash{},
FinalizedBlockHash: common.Hash{},
......@@ -101,7 +101,7 @@ func TestExecutePayloadV1(t *testing.T) {
defer n.Close()
api := NewConsensusAPI(lesService)
fcState := beacon.ForkchoiceStateV1{
fcState := engine.ForkchoiceStateV1{
HeadBlockHash: postBlocks[0].Hash(),
SafeBlockHash: common.Hash{},
FinalizedBlockHash: common.Hash{},
......@@ -130,7 +130,7 @@ func TestExecutePayloadV1(t *testing.T) {
BaseFee: block.BaseFee(),
}, nil, nil, nil, trie.NewStackTrie(nil))
_, err := api.ExecutePayloadV1(beacon.ExecutableData{
_, err := api.ExecutePayloadV1(engine.ExecutableData{
ParentHash: fakeBlock.ParentHash(),
FeeRecipient: fakeBlock.Coinbase(),
StateRoot: fakeBlock.Root(),
......@@ -153,7 +153,7 @@ func TestExecutePayloadV1(t *testing.T) {
if headHeader.Number.Uint64() != fakeBlock.NumberU64()-1 {
t.Fatal("Unexpected chain head update")
}
fcState = beacon.ForkchoiceStateV1{
fcState = engine.ForkchoiceStateV1{
HeadBlockHash: fakeBlock.Hash(),
SafeBlockHash: common.Hash{},
FinalizedBlockHash: common.Hash{},
......
......@@ -23,8 +23,8 @@ import (
"sync"
"time"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/params"
......@@ -42,14 +42,14 @@ type BuildPayloadArgs struct {
}
// Id computes an 8-byte identifier by hashing the components of the payload arguments.
func (args *BuildPayloadArgs) Id() beacon.PayloadID {
func (args *BuildPayloadArgs) Id() engine.PayloadID {
// Hash
hasher := sha256.New()
hasher.Write(args.Parent[:])
binary.Write(hasher, binary.BigEndian, args.Timestamp)
hasher.Write(args.Random[:])
hasher.Write(args.FeeRecipient[:])
var out beacon.PayloadID
var out engine.PayloadID
copy(out[:], hasher.Sum(nil)[:8])
return out
}
......@@ -60,7 +60,7 @@ func (args *BuildPayloadArgs) Id() beacon.PayloadID {
// the revenue. Therefore, the empty-block here is always available and full-block
// will be set/updated afterwards.
type Payload struct {
id beacon.PayloadID
id engine.PayloadID
empty *types.Block
full *types.Block
fullFees *big.Int
......@@ -70,7 +70,7 @@ type Payload struct {
}
// newPayload initializes the payload object.
func newPayload(empty *types.Block, id beacon.PayloadID) *Payload {
func newPayload(empty *types.Block, id engine.PayloadID) *Payload {
payload := &Payload{
id: id,
empty: empty,
......@@ -108,7 +108,7 @@ func (payload *Payload) update(block *types.Block, fees *big.Int, elapsed time.D
// Resolve returns the latest built payload and also terminates the background
// thread for updating payload. It's safe to be called multiple times.
func (payload *Payload) Resolve() *beacon.ExecutionPayloadEnvelope {
func (payload *Payload) Resolve() *engine.ExecutionPayloadEnvelope {
payload.lock.Lock()
defer payload.lock.Unlock()
......@@ -118,23 +118,23 @@ func (payload *Payload) Resolve() *beacon.ExecutionPayloadEnvelope {
close(payload.stop)
}
if payload.full != nil {
return beacon.BlockToExecutableData(payload.full, payload.fullFees)
return engine.BlockToExecutableData(payload.full, payload.fullFees)
}
return beacon.BlockToExecutableData(payload.empty, big.NewInt(0))
return engine.BlockToExecutableData(payload.empty, big.NewInt(0))
}
// ResolveEmpty is basically identical to Resolve, but it expects empty block only.
// It's only used in tests.
func (payload *Payload) ResolveEmpty() *beacon.ExecutionPayloadEnvelope {
func (payload *Payload) ResolveEmpty() *engine.ExecutionPayloadEnvelope {
payload.lock.Lock()
defer payload.lock.Unlock()
return beacon.BlockToExecutableData(payload.empty, big.NewInt(0))
return engine.BlockToExecutableData(payload.empty, big.NewInt(0))
}
// ResolveFull is basically identical to Resolve, but it expects full block only.
// It's only used in tests.
func (payload *Payload) ResolveFull() *beacon.ExecutionPayloadEnvelope {
func (payload *Payload) ResolveFull() *engine.ExecutionPayloadEnvelope {
payload.lock.Lock()
defer payload.lock.Unlock()
......@@ -146,7 +146,7 @@ func (payload *Payload) ResolveFull() *beacon.ExecutionPayloadEnvelope {
}
payload.cond.Wait()
}
return beacon.BlockToExecutableData(payload.full, payload.fullFees)
return engine.BlockToExecutableData(payload.full, payload.fullFees)
}
// buildPayload builds the payload according to the provided parameters.
......
......@@ -21,9 +21,9 @@ import (
"testing"
"time"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/rawdb"
"github.com/ethereum/go-ethereum/params"
)
......@@ -47,7 +47,7 @@ func TestBuildPayload(t *testing.T) {
if err != nil {
t.Fatalf("Failed to build payload %v", err)
}
verify := func(outer *beacon.ExecutionPayloadEnvelope, txs int) {
verify := func(outer *engine.ExecutionPayloadEnvelope, txs int) {
payload := outer.ExecutionPayload
if payload.ParentHash != b.chain.CurrentBlock().Hash() {
t.Fatal("Unexpect parent hash")
......
......@@ -27,11 +27,11 @@ import (
"time"
"github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/beacon/engine"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/fdlimit"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/beacon"
"github.com/ethereum/go-ethereum/core/txpool"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/crypto"
......@@ -142,7 +142,7 @@ func newNode(typ nodetype, genesis *core.Genesis, enodes []*enode.Node) *ethNode
}
}
func (n *ethNode) assembleBlock(parentHash common.Hash, parentTimestamp uint64) (*beacon.ExecutableData, error) {
func (n *ethNode) assembleBlock(parentHash common.Hash, parentTimestamp uint64) (*engine.ExecutableData, error) {
if n.typ != eth2MiningNode {
return nil, errors.New("invalid node type")
}
......@@ -150,12 +150,12 @@ func (n *ethNode) assembleBlock(parentHash common.Hash, parentTimestamp uint64)
if timestamp <= parentTimestamp {
timestamp = parentTimestamp + 1
}
payloadAttribute := beacon.PayloadAttributes{
payloadAttribute := engine.PayloadAttributes{
Timestamp: timestamp,
Random: common.Hash{},
SuggestedFeeRecipient: common.HexToAddress("0xdeadbeef"),
}
fcState := beacon.ForkchoiceStateV1{
fcState := engine.ForkchoiceStateV1{
HeadBlockHash: parentHash,
SafeBlockHash: common.Hash{},
FinalizedBlockHash: common.Hash{},
......@@ -168,7 +168,7 @@ func (n *ethNode) assembleBlock(parentHash common.Hash, parentTimestamp uint64)
return n.api.GetPayloadV1(*payload.PayloadID)
}
func (n *ethNode) insertBlock(eb beacon.ExecutableData) error {
func (n *ethNode) insertBlock(eb engine.ExecutableData) error {
if !eth2types(n.typ) {
return errors.New("invalid node type")
}
......@@ -194,18 +194,18 @@ func (n *ethNode) insertBlock(eb beacon.ExecutableData) error {
}
}
func (n *ethNode) insertBlockAndSetHead(parent *types.Header, ed beacon.ExecutableData) error {
func (n *ethNode) insertBlockAndSetHead(parent *types.Header, ed engine.ExecutableData) error {
if !eth2types(n.typ) {
return errors.New("invalid node type")
}
if err := n.insertBlock(ed); err != nil {
return err
}
block, err := beacon.ExecutableDataToBlock(ed)
block, err := engine.ExecutableDataToBlock(ed)
if err != nil {
return err
}
fcState := beacon.ForkchoiceStateV1{
fcState := engine.ForkchoiceStateV1{
HeadBlockHash: block.ParentHash(),
SafeBlockHash: common.Hash{},
FinalizedBlockHash: common.Hash{},
......@@ -319,7 +319,7 @@ func (mgr *nodeManager) run() {
nodes = append(nodes, mgr.getNodes(eth2NormalNode)...)
//nodes = append(nodes, mgr.getNodes(eth2LightClient)...)
for _, node := range nodes {
fcState := beacon.ForkchoiceStateV1{
fcState := engine.ForkchoiceStateV1{
HeadBlockHash: parentBlock.Hash(),
SafeBlockHash: oldest.Hash(),
FinalizedBlockHash: oldest.Hash(),
......@@ -362,7 +362,7 @@ func (mgr *nodeManager) run() {
log.Error("Failed to assemble the block", "err", err)
continue
}
block, _ := beacon.ExecutableDataToBlock(*ed)
block, _ := engine.ExecutableDataToBlock(*ed)
nodes := mgr.getNodes(eth2MiningNode)
nodes = append(nodes, mgr.getNodes(eth2NormalNode)...)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment