Skip to content

Commit

Permalink
Merge pull request ethereum#302 from nextyio/vdf-gen
Browse files Browse the repository at this point in the history
vdf.gen param
  • Loading branch information
Zergity authored Oct 7, 2019
2 parents 44fe281 + 2f7e4b3 commit 30f7b51
Show file tree
Hide file tree
Showing 15 changed files with 184 additions and 89 deletions.
3 changes: 3 additions & 0 deletions cmd/gonex/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions cmd/gonex/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ var (
utils.EWASMInterpreterFlag,
utils.EVMInterpreterFlag,
utils.PriceServiceURLFlag,
utils.VDFGen,
utils.RollbackFlag,
configFileFlag,
}
Expand Down
1 change: 1 addition & 0 deletions cmd/gonex/usage.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ var AppHelpFlagGroups = []flagGroup{
utils.LightKDFFlag,
utils.WhitelistFlag,
utils.PriceServiceURLFlag,
utils.VDFGen,
utils.RollbackFlag,
},
},
Expand Down
6 changes: 5 additions & 1 deletion cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -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|<external VDF command>)",
}
RollbackFlag = cli.Int64Flag{
Name: "rollback",
Usage: "Block number to rollback the chain before starting",
Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion consensus/dccs/2_dccs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(d.vdfGen, randomSeedSize)
return d
}

Expand Down
4 changes: 3 additions & 1 deletion consensus/dccs/dccs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand Down
10 changes: 5 additions & 5 deletions core/vdf/delayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ package vdf
import (
"bytes"
"encoding/binary"
"fmt"
"runtime"
"sync"

Expand All @@ -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
Expand All @@ -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),
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down
41 changes: 23 additions & 18 deletions core/vdf/delayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -269,6 +274,6 @@ func TestLeakage(t *testing.T) {
}

func TestLeakageGo(t *testing.T) {
NoCLI()
UseGoVDF()
TestLeakage(t)
}
32 changes: 22 additions & 10 deletions core/vdf/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,48 @@ 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
}

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)
}

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{})
Expand All @@ -70,7 +82,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
Expand All @@ -81,18 +93,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))
Expand All @@ -102,6 +114,6 @@ func TestLeak(t *testing.T) {
}

func TestLeakGo(t *testing.T) {
NoCLI()
UseGoVDF()
TestLeak(t)
}
Loading

0 comments on commit 30f7b51

Please sign in to comment.