Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor the miner #1545

Merged
merged 98 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
98 commits
Select commit Hold shift + click to select a range
e1e51cd
Remove unused tests
May 11, 2021
0f8e175
Disable the rest of the worker tests
May 11, 2021
d0c6de0
Export templates in mycelo
May 11, 2021
4f0c951
Add golang mycelo testnet
May 11, 2021
6d09400
Fix contracts path
May 11, 2021
ab86aab
Disable IPC in favor of ws
May 11, 2021
6a93759
Adds more to the network
May 11, 2021
fe4c813
Modify network config
May 11, 2021
5107233
Expose more of loadbot
May 11, 2021
8e38a1b
Add sending and waiting for transactions
May 11, 2021
a852cb0
Add block production test to the miner
May 11, 2021
7172c42
Fix loadbot for replay protection
May 11, 2021
498fda5
Set a long timeout on network startup
May 11, 2021
8623e59
REVERT ME: Extra block validity check on writeWithState
May 11, 2021
8a0ac2f
Remove unused miner methods
May 10, 2021
98da0c6
Remove unconfirmed
May 11, 2021
965c634
Pull out recover randomness function
May 11, 2021
fbf9dc0
Remove account manager
May 11, 2021
0d4694c
Use upstream channels instead of sync to manage the worker
May 11, 2021
152e24c
Remove recommitting
May 11, 2021
55e5304
Remove side channel sub & timer
May 11, 2021
e029737
Remove ancestors
May 11, 2021
ec3ea4a
Split worker into two
May 11, 2021
c29c60b
Remove tx sub channel
May 11, 2021
06d9136
Operate on a separate block state
May 11, 2021
00aebc6
Remove interrupts from commitTransactions
May 11, 2021
694c922
Remove noempty
May 11, 2021
271a39c
Remove the task loop
May 11, 2021
1a153bb
Clean up fees calculation
May 11, 2021
0e42c75
Remove interrupts in the miner
May 11, 2021
0f1f0ad
Merge mainloop and new work loop
May 11, 2021
7c3ae70
Lift istanbul start/stop from worker to miner
May 11, 2021
29f94a4
Remove isLocalBlock
May 11, 2021
2f82cad
Pull out interrupt sealing task
May 11, 2021
3ae13df
Did interrupt tasks on miner exit
May 11, 2021
b722863
Split out block processing into steps
May 12, 2021
5a5fe3e
Remove unused function
May 12, 2021
1fbbaa6
Pull out selectAndApplyTransactions
May 12, 2021
731fe33
Remove receipts copying
May 12, 2021
ff7e02b
Copy the state into itself
May 12, 2021
c1d65cd
Split out finalizeBlock
May 12, 2021
9202dfc
Remove the commit function
May 12, 2021
8c436c6
Organize miner code
May 12, 2021
fcb9b66
Rename submitNewWork and properly cancel everything
May 12, 2021
f400ee5
Track in flight block constructions
May 12, 2021
a8cd3f7
Pull logging into main runner function
May 12, 2021
bdfab30
Move the sleep from consensus into the miner
May 12, 2021
94f8f15
Add todo
May 12, 2021
8c70080
Fix lint
May 12, 2021
277ebec
Properly record the sleep time
May 12, 2021
70c67bf
Re-enable tests
May 12, 2021
1fe552d
Add todos
May 12, 2021
7ee8200
Only engine.Prepare as a validator
May 12, 2021
5d695c6
Second version of removing blocking
May 12, 2021
4935874
Revert "Set a long timeout on network startup"
May 12, 2021
bd63e26
Revert "Fix loadbot for replay protection"
May 12, 2021
77eada2
Revert "Add sending and waiting for transactions"
May 12, 2021
27c46e0
Revert "Expose more of loadbot"
May 12, 2021
bee98a8
Revert "Modify network config"
May 12, 2021
3dd4973
Revert "Adds more to the network"
May 12, 2021
3fd457a
Revert "Disable IPC in favor of ws"
May 12, 2021
1ce06f4
Revert "Fix contracts path"
May 12, 2021
aec7759
Revert "Add golang mycelo testnet"
May 12, 2021
19b625f
Revert "Export templates in mycelo"
May 12, 2021
95cda4d
Revert "Remove unused tests"
May 12, 2021
0b84d18
Remove e2e test
May 12, 2021
a466ab4
Revert "REVERT ME: Extra block validity check on writeWithState"
May 12, 2021
6054b2a
Remove unconfirmed test
May 12, 2021
b591def
Remove unused tests
May 12, 2021
8e9c2ac
Revert "Lift istanbul start/stop from worker to miner"
May 12, 2021
858ebd1
Only push pending logs when not interrupted
May 13, 2021
c3c83f7
Set gas pool on blockState creation
May 13, 2021
ed78fdc
Rename function
May 13, 2021
22bd0e2
Sleep in makeBlock
May 13, 2021
72588fc
Rename function
May 17, 2021
8aceb44
Update comment
May 17, 2021
1eb63ae
Add pending creator for full nodes
May 17, 2021
cbe352d
Re-introduce tx sub channel
May 17, 2021
82de5d4
Move functions around
May 18, 2021
0aea1b4
Return on cancelled tx sub channel
May 18, 2021
33e7ff4
Switch between full node loop and validator loop
May 18, 2021
c4046e5
Remove the waitgroup from validatorLoop
May 18, 2021
d5ec1e6
Pull subscriptions into the using function
May 18, 2021
a351398
Remove init bool from newWorker
May 18, 2021
83affa3
Partial Revert "Pull subscriptions into the using function"
May 18, 2021
ed47233
Drain txs sub channel
May 18, 2021
7567d7f
Unsubscribe from the subscription at close
May 18, 2021
2466105
Fix typo
May 18, 2021
96fb0dc
Merge branch 'master' into trianglesphere/new_miner_no_e2e_test
May 19, 2021
9a2bc6b
Switch from receiver to first argument
May 19, 2021
0c89ebe
Merge branch 'master' into trianglesphere/new_miner_no_e2e_test
May 27, 2021
bfa3dd9
Merge validator and full node loop into one
May 27, 2021
1b44835
Move unsubscribe to mainloop
May 27, 2021
345f10c
Remove Miner Flags (#1556)
mcortesi May 28, 2021
26560bc
PR comment
May 28, 2021
91d5178
Add wait group
May 28, 2021
57ceb42
Remove txFeeRecipient
May 28, 2021
90c2e5a
Assert that transactions are included
May 28, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 3 additions & 20 deletions cmd/geth/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,17 +110,9 @@ var (
utils.MaxPendingPeersFlag,
utils.MiningEnabledFlag,
utils.MinerValidatorFlag,
utils.MinerThreadsFlag,
utils.LegacyMinerThreadsFlag,
utils.MinerNotifyFlag,
utils.MinerGasTargetFlag,
utils.LegacyMinerGasTargetFlag,
utils.MinerGasLimitFlag,
utils.MinerGasPriceFlag,
utils.LegacyMinerGasPriceFlag,
utils.MinerExtraDataFlag,
utils.LegacyMinerExtraDataFlag,
utils.MinerRecommitIntervalFlag,
utils.MinerNoVerfiyFlag,
utils.NATFlag,
utils.NoDiscoverFlag,
utils.DiscoveryV5Flag,
Expand Down Expand Up @@ -469,19 +461,10 @@ func startNode(ctx *cli.Context, stack *node.Node) {
utils.Fatalf("Ethereum service not running: %v", err)
}
// Set the gas price to the limits from the CLI and start mining
gasprice := utils.GlobalBig(ctx, utils.MinerGasPriceFlag.Name)
if ctx.GlobalIsSet(utils.LegacyMinerGasPriceFlag.Name) && !ctx.GlobalIsSet(utils.MinerGasPriceFlag.Name) {
gasprice = utils.GlobalBig(ctx, utils.LegacyMinerGasPriceFlag.Name)
}
gasprice := utils.GlobalBig(ctx, utils.LegacyMinerGasPriceFlag.Name)
ethereum.TxPool().SetGasPrice(gasprice)

threads := ctx.GlobalInt(utils.MinerThreadsFlag.Name)
if ctx.GlobalIsSet(utils.LegacyMinerThreadsFlag.Name) && !ctx.GlobalIsSet(utils.MinerThreadsFlag.Name) {
threads = ctx.GlobalInt(utils.LegacyMinerThreadsFlag.Name)
log.Warn("The flag --minerthreads is deprecated and will be removed in the future, please use --miner.threads")
}

if err := ethereum.StartMining(threads); err != nil {
if err := ethereum.StartMining(); err != nil {
utils.Fatalf("Failed to start mining: %v", err)
}
}
Expand Down
8 changes: 1 addition & 7 deletions cmd/geth/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,14 +192,7 @@ var AppHelpFlagGroups = []flagGroup{
Flags: []cli.Flag{
utils.MiningEnabledFlag,
utils.MinerValidatorFlag,
utils.MinerThreadsFlag,
utils.MinerNotifyFlag,
utils.MinerGasPriceFlag,
utils.MinerGasTargetFlag,
utils.MinerGasLimitFlag,
utils.MinerExtraDataFlag,
utils.MinerRecommitIntervalFlag,
utils.MinerNoVerfiyFlag,
},
},
{
Expand Down Expand Up @@ -252,6 +245,7 @@ var AppHelpFlagGroups = []flagGroup{
Name: "DEPRECATED",
Flags: append([]cli.Flag{
utils.EtherbaseFlag,
utils.LegacyMinerGasPriceFlag,
utils.LegacyProxyEnodeURLPairsFlag,
utils.LegacyIstanbulRequestTimeoutFlag,
utils.LegacyIstanbulBlockPeriodFlag,
Expand Down
63 changes: 1 addition & 62 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,43 +398,10 @@ var (
Usage: "Public address for participation in consensus",
Value: "0",
}
MinerThreadsFlag = cli.IntFlag{
Name: "miner.threads",
Usage: "Number of CPU threads to use for mining",
Value: 0,
}
MinerNotifyFlag = cli.StringFlag{
Name: "miner.notify",
Usage: "Comma separated HTTP URL list to notify of new work packages",
}
MinerGasTargetFlag = cli.Uint64Flag{
Name: "miner.gastarget",
Usage: "Target gas floor for mined blocks",
Value: eth.DefaultConfig.Miner.GasFloor,
}
MinerGasLimitFlag = cli.Uint64Flag{
Name: "miner.gaslimit",
Usage: "Target gas ceiling for mined blocks",
Value: eth.DefaultConfig.Miner.GasCeil,
}
MinerGasPriceFlag = BigFlag{
Name: "miner.gasprice",
Usage: "Minimum gas price for mining a transaction",
Value: eth.DefaultConfig.Miner.GasPrice,
}
MinerExtraDataFlag = cli.StringFlag{
Name: "miner.extradata",
Usage: "Block extra data set by the miner (default = client version)",
}
MinerRecommitIntervalFlag = cli.DurationFlag{
Name: "miner.recommit",
Usage: "Time interval to recreate the block being mined",
Value: eth.DefaultConfig.Miner.Recommit,
}
MinerNoVerfiyFlag = cli.BoolFlag{
Name: "miner.noverify",
Usage: "Disable remote sealing verification",
}

// Account settings

Expand Down Expand Up @@ -1381,39 +1348,14 @@ func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) {
}

func setMiner(ctx *cli.Context, cfg *miner.Config) {
if ctx.GlobalIsSet(MinerNotifyFlag.Name) {
cfg.Notify = strings.Split(ctx.GlobalString(MinerNotifyFlag.Name), ",")
}

if ctx.GlobalIsSet(LegacyMinerExtraDataFlag.Name) {
cfg.ExtraData = []byte(ctx.GlobalString(LegacyMinerExtraDataFlag.Name))
log.Warn("The flag --extradata is deprecated and will be removed in the future, please use --miner.extradata")
}
if ctx.GlobalIsSet(MinerExtraDataFlag.Name) {
cfg.ExtraData = []byte(ctx.GlobalString(MinerExtraDataFlag.Name))
}
if ctx.GlobalIsSet(LegacyMinerGasTargetFlag.Name) {
cfg.GasFloor = ctx.GlobalUint64(LegacyMinerGasTargetFlag.Name)
log.Warn("The flag --targetgaslimit is deprecated and will be removed in the future, please use --miner.gastarget")
}
if ctx.GlobalIsSet(MinerGasTargetFlag.Name) {
cfg.GasFloor = ctx.GlobalUint64(MinerGasTargetFlag.Name)
}
if ctx.GlobalIsSet(MinerGasLimitFlag.Name) {
cfg.GasCeil = ctx.GlobalUint64(MinerGasLimitFlag.Name)
}
if ctx.GlobalIsSet(LegacyMinerGasPriceFlag.Name) {
cfg.GasPrice = GlobalBig(ctx, LegacyMinerGasPriceFlag.Name)
log.Warn("The flag --gasprice is deprecated and will be removed in the future, please use --miner.gasprice")
}
if ctx.GlobalIsSet(MinerGasPriceFlag.Name) {
cfg.GasPrice = GlobalBig(ctx, MinerGasPriceFlag.Name)
}
if ctx.GlobalIsSet(MinerRecommitIntervalFlag.Name) {
cfg.Recommit = ctx.Duration(MinerRecommitIntervalFlag.Name)
}
if ctx.GlobalIsSet(MinerNoVerfiyFlag.Name) {
cfg.Noverify = ctx.Bool(MinerNoVerfiyFlag.Name)
}
}

func setWhitelist(ctx *cli.Context, cfg *eth.Config) {
Expand Down Expand Up @@ -1767,9 +1709,6 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *eth.Config) {
log.Info("Using developer account", "address", developer.Address)

cfg.Genesis = core.DeveloperGenesisBlock(uint64(ctx.GlobalInt(DeveloperPeriodFlag.Name)), developer.Address)
if !ctx.GlobalIsSet(MinerGasPriceFlag.Name) && !ctx.GlobalIsSet(LegacyMinerGasPriceFlag.Name) {
cfg.Miner.GasPrice = big.NewInt(1)
}
default:
if cfg.NetworkId == params.MainnetNetworkId {
setDNSDiscoveryDefaults(cfg, params.MainnetGenesisHash)
Expand Down
27 changes: 9 additions & 18 deletions cmd/utils/flags_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package utils

import (
"fmt"
"math/big"
"strings"

"github.com/celo-org/celo-blockchain/eth"
Expand All @@ -37,29 +38,12 @@ var ShowDeprecated = cli.Command{
var DeprecatedFlags = []cli.Flag{
LegacyLightServFlag,
LegacyLightPeersFlag,
LegacyMinerThreadsFlag,
LegacyMinerGasTargetFlag,
LegacyMinerGasPriceFlag,
LegacyMinerExtraDataFlag,
LegacyMinerGasPriceFlag,
}

var (
// (Deprecated April 2018)
LegacyMinerThreadsFlag = cli.IntFlag{
Name: "minerthreads",
Usage: "Number of CPU threads to use for mining (deprecated, use --miner.threads)",
Value: 0,
}
LegacyMinerGasTargetFlag = cli.Uint64Flag{
Name: "targetgaslimit",
Usage: "Target gas floor for mined blocks (deprecated, use --miner.gastarget)",
Value: eth.DefaultConfig.Miner.GasFloor,
}
LegacyMinerGasPriceFlag = BigFlag{
Name: "gasprice",
Usage: "Minimum gas price for mining a transaction (deprecated, use --miner.gasprice)",
Value: eth.DefaultConfig.Miner.GasPrice,
}
LegacyMinerExtraDataFlag = cli.StringFlag{
Name: "extradata",
Usage: "Block extra data set by the miner (default = client version, deprecated, use --miner.extradata)",
Expand Down Expand Up @@ -174,6 +158,13 @@ var (
Usage: "Comma separated enode URLs for P2P v5 discovery bootstrap (light server, light nodes) (deprecated, use --bootnodes)",
Value: "",
}

// Deprecated in celo-blockchain 1.4.0
LegacyMinerGasPriceFlag = BigFlag{
Name: "miner.gasprice",
Usage: "Minimum gas price for mining a transaction",
Value: big.NewInt(1),
}
)

// showDeprecated displays deprecated flags that will be soon removed from the codebase.
Expand Down
24 changes: 15 additions & 9 deletions consensus/istanbul/backend/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,7 @@ func (sb *Backend) VerifySeal(chain consensus.ChainReader, header *types.Header)

// Prepare initializes the consensus fields of a block header according to the
// rules of a particular engine. The changes are executed inline.
// The parent seal is not included when the node is not validating.
func (sb *Backend) Prepare(chain consensus.ChainReader, header *types.Header) error {
// copy the parent extra data as the header extra data
number := header.Number.Uint64()
Expand All @@ -383,20 +384,26 @@ func (sb *Backend) Prepare(chain consensus.ChainReader, header *types.Header) er
header.Time = nowTime
}

if err := writeEmptyIstanbulExtra(header); err != nil {
return err
}

// wait for the timestamp of header, use this to adjust the block period
delay := time.Unix(int64(header.Time), 0).Sub(now())
time.Sleep(delay)
// Record what the delay should be, but sleep in the miner, not the consensus engine.
delay := time.Until(time.Unix(int64(header.Time), 0))
if delay < 0 {
sb.sleepGauge.Update(0)
} else {
sb.sleepGauge.Update(delay.Nanoseconds())
}

return sb.addParentSeal(chain, header)
if err := writeEmptyIstanbulExtra(header); err != nil {
return err
}

// addParentSeal blocks for up to 500ms waiting for the core to reach the target sequence.
// Prepare is called from non-validators, so don't bother with the parent seal unless this
// block is to be proposed instead of for the local state.
if sb.IsValidating() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is awckward...

we have a method call addParentSeal() that does a sleep. As a reader, why would adding a seal implies sleeping?

And then i have a Prepare() than ask if we are validating...

I know this is not from this PR, and the ware only bringing up the awckarness to the surface. But i wonder if there's a clearer way to express this.

return sb.addParentSeal(chain, header)
} else {
return nil
}
}

// UpdateValSetDiff will update the validator set diff in the header, if the mined header is the last block of the epoch
Expand Down Expand Up @@ -1192,7 +1199,6 @@ func waitCoreToReachSequence(core istanbulCore.Engine, expectedSequence *big.Int
return view.Sequence
}
case <-timeout:
// TODO(asa): Why is this logged by full nodes?
log.Trace("Timed out while waiting for core to sequence change, unable to combine commit messages with ParentAggregatedSeal", "cur_view", core.CurrentView())
return nil
}
Expand Down
1 change: 1 addition & 0 deletions consensus/istanbul/backend/test_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ func makeBlockWithoutSeal(chain *core.BlockChain, engine *Backend, parent *types
// The worker that calls Prepare is the one filling the Coinbase
header.Coinbase = engine.address
engine.Prepare(chain, header)
time.Sleep(time.Until(time.Unix(int64(header.Time), 0)))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to sleep? would be ncie if we didn't as i've seen we are not really testing this behaviuor, but just need to create blocks on the test

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests have issues if we don't sleep at all, but it's definitely possible to sleep for less time.


state, err := chain.StateAt(parent.Root())
if err != nil {
Expand Down
34 changes: 3 additions & 31 deletions eth/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@ import (
"io"
"math/big"
"os"
"runtime"
"strings"
"time"

"github.com/celo-org/celo-blockchain/common"
"github.com/celo-org/celo-blockchain/common/hexutil"
Expand Down Expand Up @@ -66,11 +64,6 @@ func (api *PublicEthereumAPI) Coinbase() (common.Address, error) {
return api.TxFeeRecipient()
}

// Hashrate returns the POW hashrate
func (api *PublicEthereumAPI) Hashrate() hexutil.Uint64 {
return hexutil.Uint64(api.e.Miner().HashRate())
}

// ChainId is the EIP-155 replay-protection chain id for the current ethereum chain config.
func (api *PublicEthereumAPI) ChainId() hexutil.Uint64 {
chainID := new(big.Int)
Expand Down Expand Up @@ -107,20 +100,13 @@ func NewPrivateMinerAPI(e *Ethereum) *PrivateMinerAPI {
return &PrivateMinerAPI{e: e}
}

// Start starts the miner with the given number of threads. If threads is nil,
// the number of workers started is equal to the number of logical CPUs that are
// usable by this process. If mining is already running, this method adjust the
// number of threads allowed to use and updates the minimum price required by the
// transaction pool.
func (api *PrivateMinerAPI) Start(threads *int) error {
// Start starts the miner
func (api *PrivateMinerAPI) Start() error {
if api.e.config.Istanbul.Proxy {
return errors.New("Can't mine if node is a proxy")
}

if threads == nil {
return api.e.StartMining(runtime.NumCPU())
}
return api.e.StartMining(*threads)
return api.e.StartMining()
}

// Stop terminates the miner, both at the consensus engine level as well as at
Expand All @@ -139,10 +125,6 @@ func (api *PrivateMinerAPI) SetExtra(extra string) (bool, error) {

// SetGasPrice sets the minimum accepted gas price for the miner.
func (api *PrivateMinerAPI) SetGasPrice(gasPrice hexutil.Big) bool {
api.e.lock.Lock()
api.e.gasPrice = (*big.Int)(&gasPrice)
api.e.lock.Unlock()

api.e.txPool.SetGasPrice((*big.Int)(&gasPrice))
return true
}
Expand All @@ -154,16 +136,6 @@ func (api *PrivateMinerAPI) SetEtherbase(etherbase common.Address) bool {
return true
}

// SetRecommitInterval updates the interval for miner sealing work recommitting.
func (api *PrivateMinerAPI) SetRecommitInterval(interval int) {
api.e.Miner().SetRecommitInterval(time.Duration(interval) * time.Millisecond)
}

// GetHashrate returns the current hashrate of the miner.
func (api *PrivateMinerAPI) GetHashrate() uint64 {
return api.e.miner.HashRate()
}

// PrivateAdminAPI is the collection of Ethereum full node-related APIs
// exposed over the private admin endpoint.
type PrivateAdminAPI struct {
Expand Down
Loading