mirror of
https://gitlab.com/pulsechaincom/prysm-pulse.git
synced 2024-12-29 06:37:17 +00:00
5bb4b56a25
Former-commit-id: 72a6194db8e0586e9b4b05f16898fff1516a07a3 [formerly 4353ca90022670878d3ccaef6d6ec886d110df0e] Former-commit-id: 3bbf849e7f2cf1c846f58f7a1d916fa1276279fc
131 lines
4.3 KiB
Go
131 lines
4.3 KiB
Go
package sharding
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"math/big"
|
|
|
|
ethereum "github.com/ethereum/go-ethereum"
|
|
"github.com/ethereum/go-ethereum/accounts"
|
|
"github.com/ethereum/go-ethereum/accounts/abi/bind"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/log"
|
|
"github.com/ethereum/go-ethereum/sharding/contracts"
|
|
)
|
|
|
|
type collator interface {
|
|
Account() *accounts.Account
|
|
ChainReader() ethereum.ChainReader
|
|
SMCCaller() *contracts.SMCCaller
|
|
}
|
|
|
|
// SubscribeBlockHeaders checks incoming block headers and determines if
|
|
// we are an eligible collator for collations. Then, it finds the pending tx's
|
|
// from the running geth node and sorts them by descending order of gas price,
|
|
// eliminates those that ask for too much gas, and routes them over
|
|
// to the SMC to create a collation
|
|
func subscribeBlockHeaders(c collator) error {
|
|
headerChan := make(chan *types.Header, 16)
|
|
|
|
account := c.Account()
|
|
|
|
_, err := c.ChainReader().SubscribeNewHead(context.Background(), headerChan)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to subscribe to incoming headers. %v", err)
|
|
}
|
|
|
|
log.Info("Listening for new headers...")
|
|
|
|
for {
|
|
// TODO: Error handling for getting disconnected from the client
|
|
head := <-headerChan
|
|
// Query the current state to see if we are an eligible collator
|
|
log.Info(fmt.Sprintf("Received new header: %v", head.Number.String()))
|
|
|
|
// Check if we are in the collator pool before checking if we are an eligible collator
|
|
v, err := isAccountInCollatorPool(c)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to verify client in collator pool. %v", err)
|
|
}
|
|
|
|
if v {
|
|
if err := checkSMCForCollator(c, head); err != nil {
|
|
return fmt.Errorf("unable to watch shards. %v", err)
|
|
}
|
|
} else {
|
|
log.Warn(fmt.Sprintf("Account %s not in collator pool.", account.Address.String()))
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
// checkSMCForCollator checks if we are an eligible collator for
|
|
// collation for the available shards in the SMC. The function calls
|
|
// getEligibleCollator from the SMC and collator a collation if
|
|
// conditions are met
|
|
func checkSMCForCollator(c collator, head *types.Header) error {
|
|
account := c.Account()
|
|
|
|
log.Info("Checking if we are an eligible collation collator for a shard...")
|
|
period := big.NewInt(0).Div(head.Number, big.NewInt(periodLength))
|
|
for s := int64(0); s < shardCount; s++ {
|
|
// Checks if we are an eligible collator according to the SMC
|
|
addr, err := c.SMCCaller().GetEligibleCollator(&bind.CallOpts{}, big.NewInt(s), period)
|
|
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
// If output is non-empty and the addr == coinbase
|
|
if addr == account.Address {
|
|
log.Info(fmt.Sprintf("Selected as collator on shard: %d", s))
|
|
err := submitCollation(s)
|
|
if err != nil {
|
|
return fmt.Errorf("could not add collation. %v", err)
|
|
}
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// isAccountInCollatorPool checks if the client is in the collator pool because
|
|
// we can't guarantee our tx for deposit will be in the next block header we receive.
|
|
// The function calls IsCollatorDeposited from the SMC and returns true if
|
|
// the client is in the collator pool
|
|
func isAccountInCollatorPool(c collator) (bool, error) {
|
|
account := c.Account()
|
|
// Checks if our deposit has gone through according to the SMC
|
|
return c.SMCCaller().IsCollatorDeposited(&bind.CallOpts{}, account.Address)
|
|
}
|
|
|
|
// submitCollation interacts with the SMC directly to add a collation header
|
|
func submitCollation(shardID int64) error {
|
|
// TODO: Adds a collation header to the SMC with the following fields:
|
|
// [
|
|
// shard_id: uint256,
|
|
// expected_period_number: uint256,
|
|
// period_start_prevhash: bytes32,
|
|
// parent_hash: bytes32,
|
|
// transactions_root: bytes32,
|
|
// coinbase: address,
|
|
// state_root: bytes32,
|
|
// receipts_root: bytes32,
|
|
// number: uint256,
|
|
// sig: bytes
|
|
// ]
|
|
//
|
|
// Before calling this, we would need to have access to the state of
|
|
// the period_start_prevhash. Refer to the comments in:
|
|
// https://github.com/ethereum/py-evm/issues/258#issuecomment-359879350
|
|
//
|
|
// This function will call FetchCandidateHead() of the SMC to obtain
|
|
// more necessary information.
|
|
//
|
|
// This functions will fetch the transactions in the proposer tx pool and and apply
|
|
// them to finish up the collation. It will then need to broadcast the
|
|
// collation to the main chain using JSON-RPC.
|
|
log.Info("Submit collation function called")
|
|
return nil
|
|
}
|