diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index ada426292a..83bc99ed9e 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -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" @@ -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) { @@ -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 } diff --git a/eth/backend.go b/eth/backend.go index d4244bbda7..4381a8ed3b 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -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