Skip to content
This repository was archived by the owner on Jun 6, 2025. It is now read-only.

Commit c7ec136

Browse files
committed
chaincmd: correctly create consortium engine in import chain
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.
1 parent 3815939 commit c7ec136

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed

cmd/utils/flags.go

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,11 @@ import (
3838
"github.com/ethereum/go-ethereum/common/fdlimit"
3939
"github.com/ethereum/go-ethereum/consensus"
4040
"github.com/ethereum/go-ethereum/consensus/clique"
41+
"github.com/ethereum/go-ethereum/consensus/consortium"
4142
"github.com/ethereum/go-ethereum/consensus/ethash"
4243
"github.com/ethereum/go-ethereum/core"
4344
"github.com/ethereum/go-ethereum/core/rawdb"
45+
"github.com/ethereum/go-ethereum/core/state"
4446
"github.com/ethereum/go-ethereum/core/vm"
4547
"github.com/ethereum/go-ethereum/crypto"
4648
"github.com/ethereum/go-ethereum/eth"
@@ -2218,9 +2220,17 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
22182220
if err != nil {
22192221
Fatalf("%v", err)
22202222
}
2221-
var engine consensus.Engine
2223+
var (
2224+
engine consensus.Engine
2225+
setupAPIBackend func(chain *core.BlockChain, engine consensus.Engine)
2226+
ethApiBackend *eth.EthAPIBackend
2227+
)
22222228
if config.Clique != nil {
22232229
engine = clique.New(config.Clique, chainDb)
2230+
} else if config.Consortium != nil {
2231+
ethApiBackend, setupAPIBackend = eth.MakeEthApiBackend(chainDb)
2232+
ethApi := ethapi.NewPublicBlockChainAPI(ethApiBackend)
2233+
engine = consortium.New(config, chainDb, ethApi)
22242234
} else {
22252235
engine = ethash.NewFaker()
22262236
if !ctx.Bool(FakePoWFlag.Name) {
@@ -2269,6 +2279,26 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai
22692279
if err != nil {
22702280
Fatalf("Can't create BlockChain: %v", err)
22712281
}
2282+
setupAPIBackend(chain, engine)
2283+
if config.Consortium != nil {
2284+
c := engine.(*consortium.Consortium)
2285+
c.SetGetSCValidatorsFn(func() ([]common.Address, error) {
2286+
stateDb, err := chain.State()
2287+
if err != nil {
2288+
log.Crit("Cannot get state of blockchain", "err", err)
2289+
return nil, err
2290+
}
2291+
return state.GetSCValidators(stateDb), nil
2292+
})
2293+
c.SetGetFenixValidators(func() ([]common.Address, error) {
2294+
stateDb, err := chain.State()
2295+
if err != nil {
2296+
log.Crit("Cannot get state of blockchain", "err", err)
2297+
return nil, err
2298+
}
2299+
return state.GetFenixValidators(stateDb, config.FenixValidatorContractAddress), nil
2300+
})
2301+
}
22722302
return chain, chainDb
22732303
}
22742304

eth/backend.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,24 @@ func New(stack *node.Node, config *ethconfig.Config) (*Ethereum, error) {
352352
return eth, nil
353353
}
354354

355+
// MakeEthApiBackend is used by MakeChain to create a minimal eth API backend for consortium
356+
// engine.
357+
// This code looks hacky as it returns a function to set the private blockchain field. This is
358+
// due to the circular dependency as blockchain needs consortium engine which requires
359+
// eth API backend. As the eth API backend is not used right after being created, we create a
360+
// eth API backend without blockchain here and set that field later when blockchain is available.
361+
func MakeEthApiBackend(chainDb ethdb.Database) (*EthAPIBackend, func(chain *core.BlockChain, engine consensus.Engine)) {
362+
eth := &Ethereum{
363+
chainDb: chainDb,
364+
config: &ethconfig.Defaults,
365+
}
366+
apiBackend := &EthAPIBackend{eth: eth}
367+
return apiBackend, func(chain *core.BlockChain, engine consensus.Engine) {
368+
eth.blockchain = chain
369+
eth.engine = engine
370+
}
371+
}
372+
355373
func makeExtraData(extra []byte) []byte {
356374
if len(extra) == 0 {
357375
// create default extradata

0 commit comments

Comments
 (0)