-
Notifications
You must be signed in to change notification settings - Fork 31
/
generate-genesis.go
264 lines (218 loc) · 5.61 KB
/
generate-genesis.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package main
import (
"crypto/sha256"
"encoding/binary"
"flag"
"fmt"
"math/big"
"os"
"runtime"
"runtime/pprof"
"strconv"
quark "github.com/mycroft/goquarkhash"
x11 "gitlab.com/nitya-sattva/go-x11"
"golang.org/x/crypto/scrypt"
)
var (
algo string
psz string
coins uint64
pubkey string
timestamp, nonce uint
bits string
profile string
maxprocs, workers int
stepsize int
verbose bool
)
func init() {
flag.StringVar(&algo, "algo", "sha256", "Algo to use: sha256, scrypt, x11, quark")
flag.StringVar(&psz, "psz", "The Times 03/Jan/2009 Chancellor on brink of second bailout for banks", "pszTimestamp")
flag.Uint64Var(&coins, "coins", uint64(50*100000000), "Number of coins")
flag.StringVar(&pubkey, "pubkey", "04678afdb0fe5548271967f1a67130b7105cd6a828e03909a67962e0ea1f61deb649f6bc3f4cef38c4f35504e51ec112de5c384df7ba0b8d578a4c702b6bf11d5f", "Pubkey (required)")
flag.UintVar(×tamp, "timestamp", 1231006505, "Timestamp to use")
flag.UintVar(&nonce, "nonce", 2083236893, "Nonce value")
flag.StringVar(&bits, "bits", "1d00ffff", "Bits")
flag.StringVar(&profile, "profile", "", "Write profile information into file (debug)")
flag.IntVar(&workers, "workers", 0, "Number of workers (goroutine) to use (if unset, use the CPU numbers)")
flag.IntVar(&maxprocs, "maxprocs", 0, "Number of max CPUs that are simultaneously used")
flag.IntVar(&stepsize, "stepsize", 1024*1000, "Number of hashes computed per worker job")
flag.BoolVar(&verbose, "verbose", false, "Show some messages")
}
func ComputeSha256(content []byte) []byte {
m := sha256.New()
m.Write(content)
return m.Sum(nil)
}
func ComputeScrypt(content []byte) []byte {
scryptHash, err := scrypt.Key(content, content, 1024, 1, 1, 32)
if err != nil {
panic(err)
}
return scryptHash
}
func ComputeX11(content []byte) []byte {
out := make([]byte, 32)
hasher := x11.New()
hasher.Hash(content, out)
return out
}
func ComputeQuark(content []byte) []byte {
return quark.QuarkHash(content)
}
func Reverse(in []byte) []byte {
out := make([]byte, len(in))
for i := 0; i < len(in); i++ {
out[i] = in[len(in)-i-1]
}
return out
}
type GenesisParams struct {
Algo string
Psz string
Coins uint64
Pubkey string
Timestamp uint32
Nonce uint32
Bits uint32
}
func ComputeTarget(bits uint32) big.Int {
var target big.Int
target_bytes := make([]byte, bits>>24)
binary.BigEndian.PutUint32(target_bytes, uint32(bits%(1<<24)<<8))
target.SetBytes(target_bytes)
return target
}
type Job struct {
StartingNonce uint32
MaxNonce uint32
Timestamp uint32
}
func main() {
flag.Parse()
if profile != "" {
f, err := os.Create(profile)
if err != nil {
panic(err)
}
pprof.StartCPUProfile(f)
defer pprof.StopCPUProfile()
}
if 0 != maxprocs {
runtime.GOMAXPROCS(maxprocs)
}
if psz == "" {
fmt.Printf("Require a psz. Please set -psz")
os.Exit(1)
}
jobs_num := runtime.NumCPU()
if workers != 0 {
jobs_num = workers
}
jobs := make(chan Job, jobs_num)
results := make(chan bool, jobs_num)
for i := 0; i < jobs_num; i++ {
go SearchWorker(i, jobs, results)
}
nonce_current := uint32(nonce)
nonce_iterator := uint32(stepsize)
for {
var res bool
if jobs_num > 0 {
next_max_nonce := nonce_current + nonce_iterator
jobs <- Job{
StartingNonce: nonce_current,
MaxNonce: next_max_nonce,
Timestamp: uint32(timestamp),
}
if next_max_nonce < nonce_current {
timestamp++
fmt.Println("nonce was reset. Timestamp is now", timestamp)
}
nonce_current = next_max_nonce
jobs_num--
} else if jobs_num == 0 {
// Wait for a job to be completed
res = <-results
jobs_num++
}
if res {
break
}
}
}
func SearchWorker(instance int, jobs <-chan Job, results chan<- bool) {
var current big.Int
var found bool
bits_uint32, err := strconv.ParseUint(bits, 16, 32)
if err != nil {
panic(err)
}
target := ComputeTarget(uint32(bits_uint32))
for job := range jobs {
if verbose {
fmt.Printf(
"Worker %2d: Nonce: %10d to Nonce: %10d timestamp: %10d\n",
instance,
job.StartingNonce,
job.MaxNonce,
job.Timestamp,
)
}
params := new(GenesisParams)
params.Algo = algo
params.Psz = psz
params.Coins = coins
params.Pubkey = pubkey
params.Timestamp = job.Timestamp
params.Nonce = job.StartingNonce
params.Bits = uint32(bits_uint32)
tx := CreateTransaction(
params.Psz,
params.Coins,
params.Pubkey,
)
tx.ComputeHash()
blk := CreateBlock(params, tx)
for {
switch params.Algo {
case "sha256":
blk.ComputeHash()
case "scrypt":
blk.Hash = ComputeScrypt(blk.Serialize())
case "x11":
blk.Hash = ComputeX11(blk.Serialize())
case "quark":
blk.Hash = quark.QuarkHash(blk.Serialize())
}
current.SetBytes(Reverse(blk.Hash))
if 1 == target.Cmp(¤t) {
found = true
PrintFound(blk, blk.Hash, target)
break
}
blk.Nonce++
if blk.Nonce == job.MaxNonce {
break
}
}
if found == true {
results <- true
break
}
results <- false
}
}
func PrintFound(blk *Block, hash []byte, target big.Int) {
fmt.Printf("Ctrl Hash:\t0x%x\n", Reverse(hash))
target_hash := make([]byte, 32)
copy(target_hash[32-len(target.Bytes()):], target.Bytes())
fmt.Printf("Target:\t\t0x%x\n", target_hash)
fmt.Printf("Blk Hash:\t0x%x\n", Reverse(blk.Hash))
fmt.Printf("Mkl Hash:\t0x%x\n", Reverse(blk.MerkleRoot))
fmt.Printf("Nonce:\t\t%d\n", blk.Nonce)
fmt.Printf("Timestamp:\t%d\n", blk.Timestamp)
fmt.Printf("Pubkey:\t\t%s\n", pubkey)
fmt.Printf("Coins:\t\t%d\n", coins)
fmt.Printf("Psz:\t\t'%s'\n", psz)
}