Skip to content

Commit

Permalink
chaincmd: correctly create consortium engine in import chain
Browse files Browse the repository at this point in the history
Currently, when import chain is used, we don't recognize the chain config to
create the consortium engine. This commit creates the consortium engine when it
is set in the chain config.
  • Loading branch information
minh-bq committed Oct 20, 2023
1 parent 821b308 commit 2c0e312
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
12 changes: 11 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"github.com/ethereum/go-ethereum/common/fdlimit"
"github.com/ethereum/go-ethereum/consensus"
"github.com/ethereum/go-ethereum/consensus/clique"
"github.com/ethereum/go-ethereum/consensus/consortium"
"github.com/ethereum/go-ethereum/consensus/ethash"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/rawdb"
Expand Down Expand Up @@ -2036,9 +2037,17 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
if err != nil {
Fatalf("%v", err)
}
var engine consensus.Engine
var (
engine consensus.Engine
setBlockChain func(chain *core.BlockChain)
ethApiBackend *eth.EthAPIBackend
)
if config.Clique != nil {
engine = clique.New(config.Clique, chainDb)
} else if config.Consortium != nil {
ethApiBackend, setBlockChain = eth.MakeEthApiBackend(chainDb)
ethApi := ethapi.NewPublicBlockChainAPI(ethApiBackend)
engine = consortium.New(config, chainDb, ethApi)
} else {
engine = ethash.NewFaker()
if !ctx.GlobalBool(FakePoWFlag.Name) {
Expand Down Expand Up @@ -2087,6 +2096,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
if err != nil {
Fatalf("Can't create BlockChain: %v", err)
}
setBlockChain(chain)
return chain, chainDb
}

Expand Down
16 changes: 16 additions & 0 deletions eth/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,22 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
return eth, nil
}

// MakeEthApiBackend is used by MakeChain to create a minimal eth API backend for consortium
// engine.
// This code looks hacky as it returns a function to set the private blockchain field. This is
// due to the circular dependency as blockchain needs consortium engine which requires
// eth API backend. As the eth API backend is not used right after being created, we create a
// eth API backend without blockchain here and set that field later when blockchain is available.
func MakeEthApiBackend(chainDb ethdb.Database) (*EthAPIBackend, func(chain *core.BlockChain)) {
eth := &Ethereum{
chainDb: chainDb,
}
apiBackend := &EthAPIBackend{eth: eth}
return apiBackend, func(chain *core.BlockChain) {
eth.blockchain = chain
}
}

func makeExtraData(extra []byte) []byte {
if len(extra) == 0 {
// create default extradata
Expand Down

0 comments on commit 2c0e312

Please sign in to comment.