Skip to content

Commit

Permalink
Added the blake3 consensus engine back in
Browse files Browse the repository at this point in the history
If in flags consensus.engine is set to "blake3", blake3 consensus engine is
initialized in the backend instead of the progpow, but the progpow wil remain
as the default
  • Loading branch information
gameofpointers committed Jul 20, 2023
1 parent bdd84ce commit f22eeb7
Show file tree
Hide file tree
Showing 19 changed files with 2,340 additions and 90 deletions.
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ ifeq ($(SHOW_COLORS),true)
BASE_CMD += --showcolors
endif

ifeq ($(RUN_BLAKE3),true)
BASE_CMD += --consensus.engine "blake3"
endif

# Build suburl strings for slice specific subclient groups
# WARNING: Only connect to dom/sub clients over a trusted network.
ifeq ($(REGION),2)
Expand Down
1 change: 1 addition & 0 deletions cmd/go-quai/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ var (
utils.CacheTrieJournalFlag,
utils.CacheTrieRejournalFlag,
utils.ColosseumFlag,
utils.ConsensusEngineFlag,
utils.DNSDiscoveryFlag,
utils.DataDirFlag,
utils.DeveloperFlag,
Expand Down
6 changes: 6 additions & 0 deletions cmd/go-quai/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,12 @@ var AppHelpFlagGroups = []flags.FlagGroup{
utils.MinerEtherbaseFlag,
},
},
{
Name: "CONSENSUS",
Flags: []cli.Flag{
utils.ConsensusEngineFlag,
},
},
{
Name: "GAS PRICE ORACLE",
Flags: []cli.Flag{
Expand Down
130 changes: 96 additions & 34 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/fdlimit"
"github.com/dominant-strategies/go-quai/consensus"
"github.com/dominant-strategies/go-quai/consensus/blake3pow"
"github.com/dominant-strategies/go-quai/consensus/progpow"
"github.com/dominant-strategies/go-quai/core"
"github.com/dominant-strategies/go-quai/core/rawdb"
Expand Down Expand Up @@ -343,6 +344,12 @@ var (
Name: "cache.preimages",
Usage: "Enable recording the SHA3/keccak preimages of trie keys",
}
// Consensus settings
ConsensusEngineFlag = cli.StringFlag{
Name: "consensus.engine",
Usage: "Consensus engine that the blockchain will run and verify blocks using",
Value: "progpow",
}
// Miner settings
MinerGasPriceFlag = BigFlag{
Name: "miner.gasprice",
Expand Down Expand Up @@ -1206,24 +1213,45 @@ func setTxPool(ctx *cli.Context, cfg *core.TxPoolConfig) {
}
}

func setProgpow(ctx *cli.Context, cfg *ethconfig.Config) {
// Override any default configs for hard coded networks.
switch {
case ctx.GlobalBool(ColosseumFlag.Name):
cfg.Progpow.DurationLimit = params.DurationLimit
case ctx.GlobalBool(GardenFlag.Name):
cfg.Progpow.DurationLimit = params.GardenDurationLimit
case ctx.GlobalBool(OrchardFlag.Name):
cfg.Progpow.DurationLimit = params.OrchardDurationLimit
case ctx.GlobalBool(GalenaFlag.Name):
cfg.Progpow.DurationLimit = params.GalenaDurationLimit
case ctx.GlobalBool(LocalFlag.Name):
cfg.Progpow.DurationLimit = params.LocalDurationLimit
case ctx.GlobalBool(DeveloperFlag.Name):
cfg.Progpow.DurationLimit = params.DurationLimit
default:
cfg.Progpow.DurationLimit = params.DurationLimit
func setDurationLimit(ctx *cli.Context, cfg *ethconfig.Config) {
if cfg.ConsensusEngine == "blake3" {
// Override any default configs for hard coded networks.
switch {
case ctx.GlobalBool(ColosseumFlag.Name):
cfg.Blake3Pow.DurationLimit = params.DurationLimit
case ctx.GlobalBool(GardenFlag.Name):
cfg.Blake3Pow.DurationLimit = params.GardenDurationLimit
case ctx.GlobalBool(OrchardFlag.Name):
cfg.Blake3Pow.DurationLimit = params.OrchardDurationLimit
case ctx.GlobalBool(GalenaFlag.Name):
cfg.Blake3Pow.DurationLimit = params.GalenaDurationLimit
case ctx.GlobalBool(LocalFlag.Name):
cfg.Blake3Pow.DurationLimit = params.LocalDurationLimit
case ctx.GlobalBool(DeveloperFlag.Name):
cfg.Blake3Pow.DurationLimit = params.DurationLimit
default:
cfg.Blake3Pow.DurationLimit = params.DurationLimit

}
} else {
// Override any default configs for hard coded networks.
switch {
case ctx.GlobalBool(ColosseumFlag.Name):
cfg.Progpow.DurationLimit = params.DurationLimit
case ctx.GlobalBool(GardenFlag.Name):
cfg.Progpow.DurationLimit = params.GardenDurationLimit
case ctx.GlobalBool(OrchardFlag.Name):
cfg.Progpow.DurationLimit = params.OrchardDurationLimit
case ctx.GlobalBool(GalenaFlag.Name):
cfg.Progpow.DurationLimit = params.GalenaDurationLimit
case ctx.GlobalBool(LocalFlag.Name):
cfg.Progpow.DurationLimit = params.LocalDurationLimit
case ctx.GlobalBool(DeveloperFlag.Name):
cfg.Progpow.DurationLimit = params.DurationLimit
default:
cfg.Progpow.DurationLimit = params.DurationLimit

}
}
}

Expand Down Expand Up @@ -1363,7 +1391,15 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
}
setGPO(ctx, &cfg.GPO, ctx.GlobalString(SyncModeFlag.Name) == "light")
setTxPool(ctx, &cfg.TxPool)
setProgpow(ctx, cfg)

// If blake3 consensus engine is specifically asked use the blake3 engine
if ctx.GlobalString(ConsensusEngineFlag.Name) == "blake3" {
cfg.ConsensusEngine = "blake3"
} else {
cfg.ConsensusEngine = "progpow"
}
setDurationLimit(ctx, cfg)

setWhitelist(ctx, cfg)

// set the dominant chain websocket url
Expand Down Expand Up @@ -1489,32 +1525,52 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 1
}
cfg.Genesis = core.DefaultColosseumGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.ColosseumGenesisHash)
cfg.Genesis = core.DefaultColosseumGenesisBlock(cfg.ConsensusEngine)
if cfg.ConsensusEngine == "blake3" {
SetDNSDiscoveryDefaults(cfg, params.Blake3PowGardenGenesisHash)
} else {
SetDNSDiscoveryDefaults(cfg, params.ProgpowGardenGenesisHash)
}
case ctx.GlobalBool(GardenFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 2
}
cfg.Genesis = core.DefaultGardenGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.GardenGenesisHash)
cfg.Genesis = core.DefaultGardenGenesisBlock(cfg.ConsensusEngine)
if cfg.ConsensusEngine == "blake3" {
SetDNSDiscoveryDefaults(cfg, params.Blake3PowGardenGenesisHash)
} else {
SetDNSDiscoveryDefaults(cfg, params.ProgpowGardenGenesisHash)
}
case ctx.GlobalBool(OrchardFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 3
}
cfg.Genesis = core.DefaultOrchardGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.OrchardGenesisHash)
cfg.Genesis = core.DefaultOrchardGenesisBlock(cfg.ConsensusEngine)
if cfg.ConsensusEngine == "blake3" {
SetDNSDiscoveryDefaults(cfg, params.Blake3PowOrchardGenesisHash)
} else {
SetDNSDiscoveryDefaults(cfg, params.ProgpowOrchardGenesisHash)
}
case ctx.GlobalBool(LocalFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 4
}
cfg.Genesis = core.DefaultLocalGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.LocalGenesisHash)
cfg.Genesis = core.DefaultLocalGenesisBlock(cfg.ConsensusEngine)
if cfg.ConsensusEngine == "blake3" {
SetDNSDiscoveryDefaults(cfg, params.Blake3PowLocalGenesisHash)
} else {
SetDNSDiscoveryDefaults(cfg, params.ProgpowLocalGenesisHash)
}
case ctx.GlobalBool(GalenaFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 5
}
cfg.Genesis = core.DefaultGalenaGenesisBlock()
SetDNSDiscoveryDefaults(cfg, params.GalenaGenesisHash)
cfg.Genesis = core.DefaultGalenaGenesisBlock(cfg.ConsensusEngine)
if cfg.ConsensusEngine == "blake3" {
SetDNSDiscoveryDefaults(cfg, params.Blake3PowGalenaGenesisHash)
} else {
SetDNSDiscoveryDefaults(cfg, params.ProgpowGalenaGenesisHash)
}
case ctx.GlobalBool(DeveloperFlag.Name):
if !ctx.GlobalIsSet(NetworkIdFlag.Name) {
cfg.NetworkId = 1337
Expand All @@ -1535,7 +1591,7 @@ func SetEthConfig(ctx *cli.Context, stack *node.Node, cfg *ethconfig.Config) {
}
default:
if cfg.NetworkId == 1 {
SetDNSDiscoveryDefaults(cfg, params.ColosseumGenesisHash)
SetDNSDiscoveryDefaults(cfg, params.ProgpowColosseumGenesisHash)
}
}
if !ctx.GlobalBool(ColosseumFlag.Name) {
Expand Down Expand Up @@ -1641,15 +1697,15 @@ func MakeGenesis(ctx *cli.Context) *core.Genesis {
var genesis *core.Genesis
switch {
case ctx.GlobalBool(ColosseumFlag.Name):
genesis = core.DefaultColosseumGenesisBlock()
genesis = core.DefaultColosseumGenesisBlock(ctx.GlobalString(ConsensusEngineFlag.Name))
case ctx.GlobalBool(GardenFlag.Name):
genesis = core.DefaultGardenGenesisBlock()
genesis = core.DefaultGardenGenesisBlock(ctx.GlobalString(ConsensusEngineFlag.Name))
case ctx.GlobalBool(OrchardFlag.Name):
genesis = core.DefaultOrchardGenesisBlock()
genesis = core.DefaultOrchardGenesisBlock(ctx.GlobalString(ConsensusEngineFlag.Name))
case ctx.GlobalBool(GalenaFlag.Name):
genesis = core.DefaultGalenaGenesisBlock()
genesis = core.DefaultGalenaGenesisBlock(ctx.GlobalString(ConsensusEngineFlag.Name))
case ctx.GlobalBool(LocalFlag.Name):
genesis = core.DefaultLocalGenesisBlock()
genesis = core.DefaultLocalGenesisBlock(ctx.GlobalString(ConsensusEngineFlag.Name))
case ctx.GlobalBool(DeveloperFlag.Name):
Fatalf("Developer chains are ephemeral")
}
Expand All @@ -1670,6 +1726,12 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (*core.Core, ethdb.Database)
if !ctx.GlobalBool(FakePoWFlag.Name) {
engine = progpow.New(progpow.Config{}, nil, false)
}

// If blake3 consensus engine is selected use the blake3 engine
if ctx.GlobalString(ConsensusEngineFlag.Name) == "blake3" {
engine = blake3pow.New(blake3pow.Config{}, nil, false)
}

if gcmode := ctx.GlobalString(GCModeFlag.Name); gcmode != "full" && gcmode != "archive" {
Fatalf("--%s must be either 'full' or 'archive'", GCModeFlag.Name)
}
Expand Down
112 changes: 112 additions & 0 deletions consensus/blake3pow/api.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
// Copyright 2018 The go-ethereum Authors
// This file is part of the go-ethereum library.
//
// The go-ethereum library is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// The go-ethereum library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// 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 blake3pow

import (
"errors"

"github.com/dominant-strategies/go-quai/common"
"github.com/dominant-strategies/go-quai/common/hexutil"
"github.com/dominant-strategies/go-quai/core/types"
)

var errBlake3powStopped = errors.New("blake3pow stopped")

// API exposes blake3pow related methods for the RPC interface.
type API struct {
blake3pow *Blake3pow
}

// GetWork returns a work package for external miner.
//
// The work package consists of 3 strings:
//
// result[0] - 32 bytes hex encoded current block header pow-hash
// result[1] - 32 bytes hex encoded seed hash used for DAG
// result[2] - 32 bytes hex encoded boundary condition ("target"), 2^256/difficulty
// result[3] - hex encoded block number
func (api *API) GetWork() ([4]string, error) {
if api.blake3pow.remote == nil {
return [4]string{}, errors.New("not supported")
}

var (
workCh = make(chan [4]string, 1)
errc = make(chan error, 1)
)
select {
case api.blake3pow.remote.fetchWorkCh <- &sealWork{errc: errc, res: workCh}:
case <-api.blake3pow.remote.exitCh:
return [4]string{}, errBlake3powStopped
}
select {
case work := <-workCh:
return work, nil
case err := <-errc:
return [4]string{}, err
}
}

// SubmitWork can be used by external miner to submit their POW solution.
// It returns an indication if the work was accepted.
// Note either an invalid solution, a stale work a non-existent work will return false.
func (api *API) SubmitWork(nonce types.BlockNonce, hash, digest common.Hash) bool {
if api.blake3pow.remote == nil {
return false
}

var errc = make(chan error, 1)
select {
case api.blake3pow.remote.submitWorkCh <- &mineResult{
nonce: nonce,
hash: hash,
errc: errc,
}:
case <-api.blake3pow.remote.exitCh:
return false
}
err := <-errc
return err == nil
}

// SubmitHashrate can be used for remote miners to submit their hash rate.
// This enables the node to report the combined hash rate of all miners
// which submit work through this node.
//
// It accepts the miner hash rate and an identifier which must be unique
// between nodes.
func (api *API) SubmitHashrate(rate hexutil.Uint64, id common.Hash) bool {
if api.blake3pow.remote == nil {
return false
}

var done = make(chan struct{}, 1)
select {
case api.blake3pow.remote.submitRateCh <- &hashrate{done: done, rate: uint64(rate), id: id}:
case <-api.blake3pow.remote.exitCh:
return false
}

// Block until hash rate submitted successfully.
<-done
return true
}

// Gblake3powrate returns the current hashrate for local CPU miner and remote miner.
func (api *API) Gblake3powrate() uint64 {
return uint64(api.blake3pow.Hashrate())
}
Loading

0 comments on commit f22eeb7

Please sign in to comment.