/
main.go
113 lines (95 loc) · 2.51 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package main
import (
"fmt"
"math/big"
"strconv"
"strings"
"sync"
"time"
"github.com/ellcrys/go-prompt"
"github.com/dustin/go-humanize"
"github.com/ellcrys/elld/play/simulations/difficulty/helpers"
"github.com/ellcrys/elld/util/logger"
funk "github.com/thoas/go-funk"
)
var log logger.Logger
func init() {
log = logger.NewLogrus()
}
// DiffSim performs difficulty calculations simulation
type DiffSim struct {
sync.Mutex
MinMineTime int
MaxMineTime int
Blocks []*helpers.Block
}
// mine mines a block
func (d *DiffSim) mine(dur time.Duration) *helpers.Block {
now := time.Now()
parentBlock := d.Blocks[len(d.Blocks)-1]
diff := helpers.CalcDifficultyInception(uint64(now.Unix()), parentBlock)
time.Sleep(dur)
return &helpers.Block{
Number: parentBlock.Number + 1,
Timestamp: now,
Difficulty: diff,
}
}
func (d *DiffSim) getMinMax() (int, int) {
d.Lock()
defer d.Unlock()
return d.MinMineTime, d.MaxMineTime
}
func (d *DiffSim) setMinMax(min, max int) {
d.Lock()
defer d.Unlock()
d.MinMineTime = min
d.MaxMineTime = max
}
// run runs the simulation
func (d *DiffSim) run() {
for {
min, max := d.getMinMax()
mineTime := time.Duration(funk.RandomInt(min, max)) * time.Second
parentBlock := d.Blocks[len(d.Blocks)-1]
newBlock := d.mine(mineTime)
big100 := new(big.Float).SetInt64(100)
newBlockDiffFl := new(big.Float).SetInt(newBlock.Difficulty)
parentBlockDiffFl := new(big.Float).SetInt(parentBlock.Difficulty)
x := new(big.Float).Sub(newBlockDiffFl, parentBlockDiffFl)
y := x.Quo(x, parentBlockDiffFl)
diffChange := new(big.Float).Mul(y, big100)
log.Info("Mined block: "+fmt.Sprintf("%d", newBlock.Number),
"Difficulty", newBlock.Difficulty.Int64(),
"DifficultyChange(%)", diffChange.String(),
"BlockTime", newBlock.Timestamp.Unix(),
"ParentBlockTime", parentBlock.Timestamp.Unix(),
"TimeDiff", humanize.RelTime(parentBlock.Timestamp, newBlock.Timestamp, "ago", "from now"),
)
d.Blocks = append(d.Blocks, newBlock)
}
}
func main() {
diffSim := &DiffSim{
MinMineTime: 1,
MaxMineTime: 15,
}
// Add genesis block
diffSim.Blocks = append(diffSim.Blocks, &helpers.Block{
Number: 95,
Timestamp: time.Unix(1540742728, 0),
Difficulty: new(big.Int).SetInt64(100000),
})
for {
inp := prompt.String(">")
if inp == "s" {
go diffSim.run()
continue
}
minMaxPars := strings.Split(inp, ",")
min, _ := strconv.Atoi(minMaxPars[0])
max, _ := strconv.Atoi(minMaxPars[1])
diffSim.setMinMax(min, max)
log.Info("Min/Max mine time changed")
}
}