From 6588c7958241c77c7ca625be0f4046910bc8711e Mon Sep 17 00:00:00 2001 From: Zergity Date: Sun, 6 Oct 2019 15:39:21 +0700 Subject: [PATCH 1/3] core,consensus/dccs: separate vdf verificator vs generator and cache the verified result --- consensus/dccs/2_dccs.go | 2 +- core/vdf/delayer.go | 10 ++-- core/vdf/delayer_test.go | 41 +++++++------- core/vdf/engine_test.go | 22 ++++---- core/vdf/{engine.go => generator.go} | 81 ++++++++++++---------------- core/vdf/verify.go | 79 +++++++++++++++++++++++++++ core/vm/contracts.go | 2 +- 7 files changed, 154 insertions(+), 83 deletions(-) rename core/vdf/{engine.go => generator.go} (73%) create mode 100644 core/vdf/verify.go diff --git a/consensus/dccs/2_dccs.go b/consensus/dccs/2_dccs.go index 085a56956f4c..96fd55173682 100644 --- a/consensus/dccs/2_dccs.go +++ b/consensus/dccs/2_dccs.go @@ -70,7 +70,7 @@ func (d *Dccs) init2() *Dccs { d.sealingQueueCache, _ = lru.NewARC(inmemorySealingQueues) d.extDataCache, _ = lru.NewARC(inmemoryExtDatas) d.anchorExtraCache, _ = lru.NewARC(inmemoryAnchorExtras) - d.queueShuffler = vdf.NewDelayer(randomSeedSize) + d.queueShuffler = vdf.NewDelayer("vdf-cli", randomSeedSize) return d } diff --git a/core/vdf/delayer.go b/core/vdf/delayer.go index f7df59bfde4c..4fa36ac69ca1 100644 --- a/core/vdf/delayer.go +++ b/core/vdf/delayer.go @@ -20,7 +20,6 @@ package vdf import ( "bytes" "encoding/binary" - "fmt" "runtime" "sync" @@ -40,6 +39,7 @@ import ( // bitSize value of 2^n-1 is recommened // output size (in bytes) will be ((bitSize+16)>>4)*4 type Delayer struct { + vdfGen *generator bitSize uint64 loopOnce sync.Once stopCh chan struct{} // to stop all running vdf routines @@ -50,9 +50,10 @@ type Delayer struct { } // NewDelayer creates a new Delayer instance -func NewDelayer(outputSize uint64) *Delayer { +func NewDelayer(genName string, outputSize uint64) *Delayer { outputCache, _ := lru.NewARC(8) return &Delayer{ + vdfGen: Generator(genName), bitSize: outputSize<<2 - 1, stopCh: make(chan struct{}), reqCh: make(chan task), @@ -71,7 +72,7 @@ func (d *Delayer) Verify(seed, output []byte, iteration uint64) bool { if cached, ok := d.outputCache.Get(t.GetKey()); ok { return bytes.Equal(output, cached.([]byte)) } - return Instance().Verify(seed, output, iteration, d.bitSize) + return Verify(seed, output, iteration, d.bitSize) } // Get request new delay task and block for output. @@ -159,11 +160,10 @@ func (d *Delayer) loop() { // start new worker routine go func(t task, resCh chan<- []byte) { - output, err := Instance().Generate(t.seed, t.iteration, d.bitSize, d.stopCh) + output, err := d.vdfGen.Generate(t.seed, t.iteration, d.bitSize, d.stopCh) defer close(resCh) if err != nil { log.Error("Delayer: VDF worker loop failed", "err", err) - fmt.Printf("Delayer: VDF worker loop failed, err=%v\n", err) return } if len(output) == 0 { diff --git a/core/vdf/delayer_test.go b/core/vdf/delayer_test.go index 3040d1103de3..c0c2d844278e 100644 --- a/core/vdf/delayer_test.go +++ b/core/vdf/delayer_test.go @@ -27,19 +27,24 @@ import ( ) func TestOutput(t *testing.T) { - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) output := delayer.Get(input0, iteration0) t.Log("main routine", "output", common.Bytes2Hex(output)) } func TestOutputGo(t *testing.T) { - NoCLI() + UseGoVDF() + TestOutput(t) +} + +func TestOutputNil(t *testing.T) { + vdfGen = "" TestOutput(t) } func TestSequentialOutput(t *testing.T) { var wg sync.WaitGroup - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) wg.Add(1) go func() { defer wg.Done() @@ -68,13 +73,13 @@ func TestSequentialOutput(t *testing.T) { } func TestSequentialOutputGo(t *testing.T) { - NoCLI() + UseGoVDF() TestSequentialOutput(t) } func TestReplacedOutput(t *testing.T) { var wg sync.WaitGroup - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) wg.Add(1) go func() { defer wg.Done() @@ -104,13 +109,13 @@ func TestReplacedOutput(t *testing.T) { } func TestReplacedOutputGo(t *testing.T) { - NoCLI() + UseGoVDF() TestReplacedOutput(t) } func TestRacingOutput(t *testing.T) { var wg sync.WaitGroup - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) wg.Add(1) go func() { defer wg.Done() @@ -139,13 +144,13 @@ func TestRacingOutput(t *testing.T) { } func TestRacingOutputGo(t *testing.T) { - NoCLI() + UseGoVDF() TestRacingOutput(t) } func TestOutputBroadcasting(t *testing.T) { var wg sync.WaitGroup - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) for i := 0; i < 3; i++ { wg.Add(1) go func(i int) { @@ -170,13 +175,13 @@ func TestOutputBroadcasting(t *testing.T) { } func TestOutputBroadcastingGo(t *testing.T) { - NoCLI() + UseGoVDF() TestOutputBroadcasting(t) } func TestOutputReplacedBroadcasting(t *testing.T) { var wg sync.WaitGroup - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) for i := 0; i < 3; i++ { wg.Add(1) go func(i int) { @@ -214,12 +219,12 @@ func TestOutputReplacedBroadcasting(t *testing.T) { } func TestOutputReplacedBroadcastingGo(t *testing.T) { - NoCLI() + UseGoVDF() TestOutputReplacedBroadcasting(t) } func TestCache(t *testing.T) { - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) output := delayer.Peek(input0, iteration0) t.Log("peek", "output", common.Bytes2Hex(output)) output = delayer.Get(input0, iteration0) @@ -231,12 +236,12 @@ func TestCache(t *testing.T) { } func TestCacheGo(t *testing.T) { - NoCLI() + UseGoVDF() TestCache(t) } func TestRequest(t *testing.T) { - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) delayer.Request(input0, iteration0) delayer.Request(input1, iteration1) time.Sleep(time.Second / 4) @@ -249,13 +254,13 @@ func TestRequest(t *testing.T) { } func TestRequestGo(t *testing.T) { - NoCLI() + UseGoVDF() TestRequest(t) } func TestLeakage(t *testing.T) { defer leaktest.Check(t)() - delayer := NewDelayer(32) + delayer := NewDelayer(vdfGen, 32) for i := uint64(1); i < iteration0/1000; i++ { output := delayer.Get(input0, i) if output != nil { @@ -269,6 +274,6 @@ func TestLeakage(t *testing.T) { } func TestLeakageGo(t *testing.T) { - NoCLI() + UseGoVDF() TestLeakage(t) } diff --git a/core/vdf/engine_test.go b/core/vdf/engine_test.go index 88b210ae5a38..6091f4d72dcc 100644 --- a/core/vdf/engine_test.go +++ b/core/vdf/engine_test.go @@ -30,14 +30,16 @@ const ( ) var ( + vdfGen = "vdf-cli" + input0 = []byte{0x01, 0x02, 0x03, 0x04} input1 = []byte{0x04, 0x03, 0x01, 0x01} iteration0 = ITERATION_0 iteration1 = ITERATION_1 ) -func NoCLI() { - InitCLI("non-exist") // use the vdf-go instead +func UseGoVDF() { + vdfGen = vdfGenInternal iteration0 = ITERATION_0 / 20 iteration1 = ITERATION_1 / 20 } @@ -45,18 +47,18 @@ func NoCLI() { func TestGenerateVerify(t *testing.T) { bitSize := uint64(127) stopCh := make(chan struct{}) - output, err := Instance().Generate(input0, iteration0, bitSize, stopCh) + output, err := Generator(vdfGen).Generate(input0, iteration0, bitSize, stopCh) if err != nil { t.Error("Error", "err", err) return } - if !Instance().Verify(input0, output, iteration0, bitSize) { + if !Verify(input0, output, iteration0, bitSize) { t.Error("Invalid ouput", "output", common.Bytes2Hex(output)) } } func TestGenerateVerifyGo(t *testing.T) { - NoCLI() + UseGoVDF() TestGenerateVerify(t) } @@ -70,7 +72,7 @@ func TestInterruptedGenerator(t *testing.T) { default: } }() - output, err := Instance().Generate(input0, iteration0, bitSize, stopCh) + output, err := Generator(vdfGen).Generate(input0, iteration0, bitSize, stopCh) if err != nil { t.Error("Error", "err", err) return @@ -81,18 +83,18 @@ func TestInterruptedGenerator(t *testing.T) { } func TestInterruptedGeneratorGo(t *testing.T) { - NoCLI() + UseGoVDF() TestInterruptedGenerator(t) } func TestLeak(t *testing.T) { for i := uint64(1); i < iteration0/300; i++ { - output, err := Instance().Generate(input0, i, 127, nil) + output, err := Generator(vdfGen).Generate(input0, i, 127, nil) if err != nil { t.Error("error", "err", err) } if output != nil { - if Instance().Verify(input0, output, i, 127) { + if Verify(input0, output, i, 127) { t.Log("success", "output", common.Bytes2Hex(output)) } else { t.Error("failed", "output", common.Bytes2Hex(output)) @@ -102,6 +104,6 @@ func TestLeak(t *testing.T) { } func TestLeakGo(t *testing.T) { - NoCLI() + UseGoVDF() TestLeak(t) } diff --git a/core/vdf/engine.go b/core/vdf/generator.go similarity index 73% rename from core/vdf/engine.go rename to core/vdf/generator.go index a920a54f8ac4..dc62a27873e2 100644 --- a/core/vdf/engine.go +++ b/core/vdf/generator.go @@ -18,6 +18,7 @@ package vdf import ( + "errors" "fmt" "os/exec" "runtime" @@ -32,62 +33,46 @@ import ( "github.com/harmony-one/vdf/src/vdf_go" ) -var ( - engine *Engine - engineOnce sync.Once -) +const vdfGenInternal = "internal" +const vdfGenDisable = "disable" -// Engine defines the structure of the engine -type Engine struct { +type generator struct { cli string } -// Instance returns the singleton instance of the VDF Engine -func Instance() *Engine { - engineOnce.Do(func() { - engine = newEngine("vdf-cli") - }) - return engine -} - -// InitCLI inits the instance with the specific cli name. -// Must be called before any call to Instance() to override the -// default cli name "vdf-cli". -// Useful unit test. -func InitCLI(cliName string) { - engineOnce.Do(func() { - engine = newEngine(cliName) - }) -} +var goGenerator = generator{} +var generators sync.Map -func newEngine(cliName string) *Engine { +func Generator(cliName string) *generator { + if len(cliName) == 0 || cliName == vdfGenDisable { + log.Warn("VDF generator: disabled") + return nil + } + if cliName == vdfGenInternal { + log.Warn("VDF generator: internal") + return &goGenerator + } + if val, ok := generators.Load(cliName); ok { + return val.(*generator) + } cli, err := exec.LookPath(cliName) if err != nil { - log.Warn("vdf.newEngine", cliName, "not found") + log.Error("VDF generator: disabled", cliName, "not found") + return nil } - return &Engine{cli} -} - -// IsCLI returns whether cli is used for this engine -func (e *Engine) IsCLI() bool { - return len(e.cli) > 0 -} - -// Verify verifies the generated output against the seed -func (e *Engine) Verify(seed, output []byte, iteration uint64, bitSize uint64) (valid bool) { - defer func() { - if x := recover(); x != nil { - log.Error("vdf.Verify: verification process panic", "reason", x) - valid = false - } - }() - return vdf_go.VerifyVDF(seed, output, int(iteration), int(bitSize)) + gen := generator{cli} + ret, _ := generators.LoadOrStore(cliName, &gen) + log.Info("VDF generator", "cli", cli) + return ret.(*generator) } // Generate generates the vdf output = (y, proof) -func (e *Engine) Generate(seed []byte, iteration uint64, bitSize uint64, stop <-chan struct{}) (output []byte, err error) { - if len(e.cli) > 0 { - return e.generateCLI(seed, iteration, bitSize, stop) +func (g *generator) Generate(seed []byte, iteration uint64, bitSize uint64, stop <-chan struct{}) (output []byte, err error) { + if g == nil { + return nil, errors.New("VDF generator disabled") + } + if len(g.cli) > 0 { + return g.generateCLI(seed, iteration, bitSize, stop) } defer func() { if x := recover(); x != nil { @@ -130,13 +115,13 @@ func (e *Engine) Generate(seed []byte, iteration uint64, bitSize uint64, stop <- return append(y, proof...), nil } -func (e *Engine) generateCLI(seed []byte, iteration uint64, bitSize uint64, stop <-chan struct{}) (output []byte, err error) { - cmd := exec.Command(e.cli, +func (g *generator) generateCLI(seed []byte, iteration uint64, bitSize uint64, stop <-chan struct{}) (output []byte, err error) { + cmd := exec.Command(g.cli, "-l"+strconv.Itoa(int(bitSize)), common.Bytes2Hex(seed), strconv.Itoa(int(iteration))) - log.Trace(e.cli + " -l" + strconv.Itoa(int(bitSize)) + " " + common.Bytes2Hex(seed) + " " + strconv.Itoa(int(iteration))) + log.Trace(g.cli + " -l" + strconv.Itoa(int(bitSize)) + " " + common.Bytes2Hex(seed) + " " + strconv.Itoa(int(iteration))) var done chan struct{} if stop != nil { diff --git a/core/vdf/verify.go b/core/vdf/verify.go new file mode 100644 index 000000000000..4d372f354d71 --- /dev/null +++ b/core/vdf/verify.go @@ -0,0 +1,79 @@ +// Copyright 2019 The gonex 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 . + +// Package vdf implements the VDF engine. +package vdf + +import ( + "bytes" + "encoding/binary" + "hash/fnv" + "sync" + + lru "github.com/hashicorp/golang-lru" + + "github.com/ethereum/go-ethereum/log" + "github.com/harmony-one/vdf/src/vdf_go" +) + +const ( + inmemoryVDFCache = 32 +) + +var ( + cache *lru.ARCCache // vdfInput => []byte + cacheOnce sync.Once +) + +type vdfInput struct { + seed []byte + iteration uint64 + bitSize uint64 +} + +func inputKey(seed []byte, iteration uint64, bitSize uint64) uint64 { + hasher := fnv.New64a() + hasher.Write(seed) + b := make([]byte, 8) + binary.BigEndian.PutUint64(b, iteration) + hasher.Write(b) + binary.BigEndian.PutUint64(b, bitSize) + hasher.Write(b) + return hasher.Sum64() +} + +// Verify verifies the generated output against the seed +func Verify(seed, output []byte, iteration uint64, bitSize uint64) (valid bool) { + cacheOnce.Do(func() { + cache, _ = lru.NewARC(inmemoryVDFCache) + }) + key := inputKey(seed, iteration, bitSize) + if value, ok := cache.Get(key); ok { + return bytes.Compare(output, value.([]byte)) == 0 + } + defer func() { + if x := recover(); x != nil { + log.Error("vdf.Verify: verification process panic", "reason", x) + valid = false + } + }() + if !vdf_go.VerifyVDF(seed, output, int(iteration), int(bitSize)) { + return false + } + // verify success, cache the output + cache.Add(key, output) + return true +} diff --git a/core/vm/contracts.go b/core/vm/contracts.go index b141a2b474ac..795be6379f3d 100644 --- a/core/vm/contracts.go +++ b/core/vm/contracts.go @@ -564,7 +564,7 @@ func (c *vdfVerify) Run(input []byte) (valid []byte, err error) { "seed", common.ToHex(seed), "output", common.ToHex(output)) - ok := vdf.Instance().Verify(seed, output, iteration, bitSize) + ok := vdf.Verify(seed, output, iteration, bitSize) log.Trace("VDFVerify", "valid", ok) if ok { return true32Byte, nil From d049dc4a300f54feb798c1463bb0ecdf9688320d Mon Sep 17 00:00:00 2001 From: Zergity Date: Sun, 6 Oct 2019 22:10:07 +0700 Subject: [PATCH 2/3] core/vdf: unit test for verify cache --- core/vdf/engine_test.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/core/vdf/engine_test.go b/core/vdf/engine_test.go index 6091f4d72dcc..2a62bb64b3b6 100644 --- a/core/vdf/engine_test.go +++ b/core/vdf/engine_test.go @@ -62,6 +62,16 @@ func TestGenerateVerifyGo(t *testing.T) { TestGenerateVerify(t) } +func TestVerifyCache(t *testing.T) { + input := common.Hex2Bytes("0123456789abcdef") + output := common.Hex2Bytes("31f44950a7467e3eb03d5e2c1513eea580ed5de129d9603ed1a34a2921555e59713deaaccb01fc8ca5c7bc4e017bc6a6520b2de17ae3065d5f7fc8023c663f6efe9a2e04fd7bfe28722c27fe0ca849c134f106f77b06375bd7b9c681d28f5da8a3404447c42082e0e5c87d57acd89d45f8de23ce10a811f61a68a80632a8326a309f2e569475fad67b52c59ae4c8a435e44c34f3e4ba08bd77319a916f820f2bc5bd830e76de7b6d4383a28769420fc0b01b8c39ad29f578bd3e957374597f69f7137ab26c2424297d7e5ef7c2ac62a802641496b9abfa28ad4185ec65ac2696e2882f07e8222333ee8b11e1562436632fb2900b0ecbc915d8f952407c203b470f6fa753415bf33621ecb09614dfe7a8f8ac37e3d8e51d97aacfdfdb821979c4241459b235d6e6f158cae17c5ffb284b2ab05317059eea9a4f471a7cde7e791107db93e2c61616cb6120e1c2143b498237623aa962551501bb5520eefe822da1bca7296ae803bc775708e2cccfddbc35652a09ec3c0350369a77bd1ce103a8acfd0b43c663f1e93ceec32e1ea05f6d7e9e47a5036caa7aaf6dfc7d443084dfe06997c15e126d7c9786a32ba94c764a42c742c85decd02b46f0e504f0e4a7923d68349009df078804535b5f95a62647487dd018a89469938db478d4b54190dddd22c8ec70ebae842b2422ff159afee0e245dc933241ebf9874e0a8c56c7158539") + for i := 0; i < 999999; i++ { + if !Verify(input, output, 1234567, 2047) { + t.Error("Invalid ouput", "output", common.Bytes2Hex(output)) + } + } +} + func TestInterruptedGenerator(t *testing.T) { bitSize := uint64(127) stopCh := make(chan struct{}) From 2f7e4b3dd8463bfcb112b8c9b97b3962111fd016 Mon Sep 17 00:00:00 2001 From: Zergity Date: Sun, 6 Oct 2019 20:04:11 +0700 Subject: [PATCH 3/3] eth,les,consensus,cmd: expose the vdf.gen command --- cmd/gonex/config.go | 3 +++ cmd/gonex/main.go | 1 + cmd/gonex/usage.go | 1 + cmd/utils/flags.go | 6 +++++- consensus/dccs/2_dccs.go | 2 +- consensus/dccs/dccs.go | 4 +++- eth/backend.go | 6 +++--- eth/config.go | 3 +++ les/client.go | 2 +- 9 files changed, 21 insertions(+), 7 deletions(-) diff --git a/cmd/gonex/config.go b/cmd/gonex/config.go index 8f2e023afd5f..9c604396ca60 100644 --- a/cmd/gonex/config.go +++ b/cmd/gonex/config.go @@ -161,6 +161,9 @@ func makeFullNode(ctx *cli.Context) *node.Node { if ctx.GlobalIsSet(utils.PriceServiceURLFlag.Name) { cfg.Eth.PriceServiceURL = ctx.GlobalString(utils.PriceServiceURLFlag.Name) } + if ctx.GlobalIsSet(utils.VDFGen.Name) { + cfg.Eth.VDFGen = ctx.GlobalString(utils.VDFGen.Name) + } utils.RegisterEthService(stack, &cfg.Eth) if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) { diff --git a/cmd/gonex/main.go b/cmd/gonex/main.go index 82459260aa13..c070de26bfca 100644 --- a/cmd/gonex/main.go +++ b/cmd/gonex/main.go @@ -151,6 +151,7 @@ var ( utils.EWASMInterpreterFlag, utils.EVMInterpreterFlag, utils.PriceServiceURLFlag, + utils.VDFGen, utils.RollbackFlag, configFileFlag, } diff --git a/cmd/gonex/usage.go b/cmd/gonex/usage.go index 917641a73a17..991a1227ff06 100644 --- a/cmd/gonex/usage.go +++ b/cmd/gonex/usage.go @@ -86,6 +86,7 @@ var AppHelpFlagGroups = []flagGroup{ utils.LightKDFFlag, utils.WhitelistFlag, utils.PriceServiceURLFlag, + utils.VDFGen, utils.RollbackFlag, }, }, diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 0ba939083279..060abd49275f 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -240,6 +240,10 @@ var ( Name: "price.url", Usage: "Fetching URL of a price service (host:port)", } + VDFGen = cli.StringFlag{ + Name: "vdf.gen", + Usage: "VDF generator (disable|internal|)", + } RollbackFlag = cli.Int64Flag{ Name: "rollback", Usage: "Block number to rollback the chain before starting", @@ -1736,7 +1740,7 @@ func MakeChain(ctx *cli.Context, stack *node.Node) (chain *core.BlockChain, chai if config.Clique != nil { engine = clique.New(config.Clique, chainDb) } else if config.Dccs != nil { - engine = dccs.New(config.Dccs, chainDb, "") + engine = dccs.New(config.Dccs, chainDb, "", "") } else { engine = ethash.NewFaker() if !ctx.GlobalBool(FakePoWFlag.Name) { diff --git a/consensus/dccs/2_dccs.go b/consensus/dccs/2_dccs.go index 96fd55173682..a4a89f2751da 100644 --- a/consensus/dccs/2_dccs.go +++ b/consensus/dccs/2_dccs.go @@ -70,7 +70,7 @@ func (d *Dccs) init2() *Dccs { d.sealingQueueCache, _ = lru.NewARC(inmemorySealingQueues) d.extDataCache, _ = lru.NewARC(inmemoryExtDatas) d.anchorExtraCache, _ = lru.NewARC(inmemoryAnchorExtras) - d.queueShuffler = vdf.NewDelayer("vdf-cli", randomSeedSize) + d.queueShuffler = vdf.NewDelayer(d.vdfGen, randomSeedSize) return d } diff --git a/consensus/dccs/dccs.go b/consensus/dccs/dccs.go index 21b160369284..f883ed36b1a2 100644 --- a/consensus/dccs/dccs.go +++ b/consensus/dccs/dccs.go @@ -156,11 +156,12 @@ type Dccs struct { priceEngineOnce sync.Once priceURL string + vdfGen string } // New creates a Dccs proof-of-foundation consensus engine with the initial // signers set to the ones provided by the user. -func New(config *params.DccsConfig, db ethdb.Database, priceServiceURL string) *Dccs { +func New(config *params.DccsConfig, db ethdb.Database, priceServiceURL, vdfGen string) *Dccs { // Set any missing consensus parameters to their defaults conf := *config if conf.Epoch == 0 { @@ -176,6 +177,7 @@ func New(config *params.DccsConfig, db ethdb.Database, priceServiceURL string) * recents: recents, signatures: signatures, priceURL: priceServiceURL, + vdfGen: vdfGen, } return dccs.init2() diff --git a/eth/backend.go b/eth/backend.go index ef5f7624395a..bec2a342ae2e 100644 --- a/eth/backend.go +++ b/eth/backend.go @@ -147,7 +147,7 @@ func New(ctx *node.ServiceContext, config *Config) (*Ethereum, error) { chainDb: chainDb, eventMux: ctx.EventMux, accountManager: ctx.AccountManager, - engine: CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.Miner.Notify, config.Miner.Noverify, config.PriceServiceURL, chainDb), + engine: CreateConsensusEngine(ctx, chainConfig, &config.Ethash, config.Miner.Notify, config.Miner.Noverify, config.PriceServiceURL, config.VDFGen, chainDb), shutdownChan: make(chan bool), networkID: config.NetworkId, gasPrice: config.Miner.GasPrice, @@ -243,14 +243,14 @@ func makeExtraData(extra []byte) []byte { } // CreateConsensusEngine creates the required type of consensus engine instance for an Ethereum service -func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, priceURL string, db ethdb.Database) consensus.Engine { +func CreateConsensusEngine(ctx *node.ServiceContext, chainConfig *params.ChainConfig, config *ethash.Config, notify []string, noverify bool, priceURL, vdfGen string, db ethdb.Database) consensus.Engine { // If proof-of-authority is requested, set it up if chainConfig.Clique != nil { return clique.New(chainConfig.Clique, db) } // If proof-of-foundation is requested, set it up if chainConfig.Dccs != nil { - return dccs.New(chainConfig.Dccs, db, priceURL) + return dccs.New(chainConfig.Dccs, db, priceURL, vdfGen) } // Otherwise assume proof-of-work switch config.PowMode { diff --git a/eth/config.go b/eth/config.go index 312be45e01e8..41a534b54097 100644 --- a/eth/config.go +++ b/eth/config.go @@ -163,4 +163,7 @@ type Config struct { // PriceServiceURL is service enpoint for the consensus. PriceServiceURL string + + // VDFGen is the VDF generator (disable|internal|) + VDFGen string } diff --git a/les/client.go b/les/client.go index 3a48a1dbce0f..a5e5c9f1eefa 100644 --- a/les/client.go +++ b/les/client.go @@ -92,7 +92,7 @@ func New(ctx *node.ServiceContext, config *eth.Config) (*LightEthereum, error) { eventMux: ctx.EventMux, reqDist: newRequestDistributor(peers, &mclock.System{}), accountManager: ctx.AccountManager, - engine: eth.CreateConsensusEngine(ctx, chainConfig, &config.Ethash, nil, false, config.PriceServiceURL, chainDb), + engine: eth.CreateConsensusEngine(ctx, chainConfig, &config.Ethash, nil, false, config.PriceServiceURL, config.VDFGen, chainDb), bloomRequests: make(chan chan *bloombits.Retrieval), bloomIndexer: eth.NewBloomIndexer(chainDb, params.BloomBitsBlocksClient, params.HelperTrieConfirmations), serverPool: newServerPool(chainDb, config.UltraLightServers),