MCPcopy
hub / github.com/canopy-network/canopy / Run

Method Run

lib/vdf.go:50–101  ·  view source on GitHub ↗

Run() *blocking call*: generates a VDF proof using the current params state of the VDF Service object The design is to save the results

(seed []byte)

Source from the content-addressed store, hash-verified

48// Run() *blocking call*: generates a VDF proof using the current params state of the VDF Service object
49// The design is to save the results
50func (vdf *VDFService) Run(seed []byte) {
51 // if the vdf service is nil
52 if vdf == nil {
53 vdf.log.Debugf("Not running VDF - empty VDF service")
54 // exit
55 return
56 }
57 // log the initialization of the vdf services
58 vdf.log.Debugf("Starting the VDF service with %d iterations", vdf.Iterations)
59 // - Run() and not running locks and starts a run
60 // - Run() and already running returns
61 if !vdf.running.CompareAndSwap(false, true) {
62 // log the active status of the VDF
63 vdf.log.Debug("VDF service is already running")
64 // exit without running again
65 return
66 }
67 // clear the results object
68 vdf.Results = crypto.VDF{}
69 // at the end of this function, reset the sync variable
70 defer vdf.running.Store(false)
71 // track the start time to measure the 'processing time'
72 startTime := time.Now()
73 // ensure no 0 iteration vdf
74 if vdf.Iterations == 0 {
75 // set iterations to a target of 1
76 vdf.Iterations = 1
77 }
78 // run the VDF generation - if Stop() called, this will exit prematurely with y and proof being nil
79 y, proof := crypto.GenerateVDF(seed, vdf.Iterations, vdf.stopChan)
80 // adjusting variables so must lock for thread safety as the Stop() function may be accessing the `Output`
81 // if prematurely stopped
82 if y == nil || proof == nil {
83 // don't know how long was left in the VDF so decrease iterations by a fixed amount
84 // example: 10% fixed decrease on 500 iterations = 450 next iterations
85 vdf.Iterations = int(float64(vdf.Iterations) * (1 - IterationsFixedDecreasePercent/100))
86 // exit
87 return
88 }
89 // get the duration of the VDF run
90 duration := time.Since(startTime)
91 // log the completion of the service
92 vdf.log.Debugf("VDF service completed with %d iterations in %s", vdf.Iterations, duration.String())
93 // save the result
94 vdf.Results = crypto.VDF{
95 Proof: proof,
96 Output: y,
97 Iterations: uint64(vdf.Iterations),
98 }
99 // adjust the iterations based on completion time
100 vdf.adjustIterations(duration)
101}
102
103// Finish() signals the service to complete and returns the output
104// - already running signals a stop in the running thread and returns

Callers 15

TestVDFFunction · 0.95
TestVDFPrematureExitFunction · 0.95
TestNewValidatorSetFunction · 0.80
TestGetNonSignersFunction · 0.80
TestGetDoubleSignersFunction · 0.80
TestViewCheckBasicFunction · 0.80
TestViewCheckFunction · 0.80
TestViewEqualsFunction · 0.80

Calls 6

adjustIterationsMethod · 0.95
GenerateVDFFunction · 0.92
StoreMethod · 0.80
DebugfMethod · 0.65
DebugMethod · 0.65
StringMethod · 0.65

Tested by 15

TestVDFFunction · 0.76
TestVDFPrematureExitFunction · 0.76
TestNewValidatorSetFunction · 0.64
TestGetNonSignersFunction · 0.64
TestGetDoubleSignersFunction · 0.64
TestViewCheckBasicFunction · 0.64
TestViewCheckFunction · 0.64
TestViewEqualsFunction · 0.64