-
Notifications
You must be signed in to change notification settings - Fork 48
/
cuckoo.go
298 lines (257 loc) · 7.42 KB
/
cuckoo.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package cuckoo
import (
"errors"
"github.com/CortexFoundation/CortexTheseus/common"
"github.com/CortexFoundation/CortexTheseus/common/mclock"
"github.com/CortexFoundation/CortexTheseus/consensus"
"github.com/CortexFoundation/CortexTheseus/core/types"
"github.com/CortexFoundation/CortexTheseus/log"
"github.com/CortexFoundation/CortexTheseus/metrics"
"github.com/CortexFoundation/CortexTheseus/rpc"
"github.com/elastic/gosigar"
"math/big"
"math/rand"
"plugin"
"sync"
"time"
)
var sharedCuckoo = New(Config{PowMode: ModeNormal})
var ErrInvalidDumpMagic = errors.New("invalid dump magic")
var (
// maxUint256 is a big integer representing 2^256-1
maxUint256 = new(big.Int).Exp(big.NewInt(2), big.NewInt(256), big.NewInt(0))
)
type Mode uint
const (
ModeNormal Mode = iota
ModeShared
ModeTest
ModeFake
ModeFullFake
)
// mineResult wraps the pow solution parameters for the specified block.
type mineResult struct {
nonce types.BlockNonce
//mixDigest common.Hash
hash common.Hash
solution types.BlockSolution
errc chan error
}
// hashrate wraps the hash rate submitted by the remote sealer.
type hashrate struct {
id common.Hash
ping time.Time
rate uint64
done chan struct{}
}
// sealWork wraps a seal work package for remote sealer.
type sealWork struct {
errc chan error
res chan [4]string
}
// compatiable with cuckoo interface
type Config struct {
CacheDir string
CachesInMem int
CachesOnDisk int
DatasetDir string
DatasetsInMem int
DatasetsOnDisk int
PowMode Mode
UseCuda bool
UseOpenCL bool
StrDeviceIds string
Threads int
Algorithm string
Mine bool
}
type Cuckoo struct {
config Config
rand *rand.Rand
// Current version allows single thread only
threads int
update chan struct{}
hashrate metrics.Meter
// Remote sealer related fields
workCh chan *types.Block // Notification channel to push new work to remote sealer
resultCh chan *types.Block // Channel used by mining threads to return result
fetchWorkCh chan *sealWork // Channel used for remote sealer to fetch mining work
submitWorkCh chan *mineResult // Channel used for remote sealer to submit their mining result
fetchRateCh chan chan uint64 // Channel used to gather submitted hash rate for local or remote sealer.
submitRateCh chan *hashrate // Channel used for remote sealer to submit their mining hashrate
shared *Cuckoo
fakeFail uint64 // Block number which fails PoW check even in fake mode
fakeDelay time.Duration // Time delay to sleep for before returning from verify
lock sync.Mutex // Ensures thread safety for the in-memory caches and mining fields
once sync.Once // Ensures cuckoo-cycle algorithm initialize once
closeOnce sync.Once // Ensures exit channel will not be closed twice.
exitCh chan chan error // Notification channel to exiting backend threads
cMutex sync.Mutex
minerPlugin *plugin.Plugin
wg sync.WaitGroup
}
func New(config Config) *Cuckoo {
// C.CuckooInit()
// CuckooInit(2)
cuckoo := &Cuckoo{
config: config,
update: make(chan struct{}),
hashrate: metrics.NewMeter(),
threads: 1,
workCh: make(chan *types.Block),
resultCh: make(chan *types.Block),
fetchWorkCh: make(chan *sealWork),
submitWorkCh: make(chan *mineResult),
fetchRateCh: make(chan chan uint64),
submitRateCh: make(chan *hashrate),
exitCh: make(chan chan error),
}
//if config.Mine {
// miner algorithm use cuckaroo by default.
cuckoo.wg.Add(1)
go func() {
defer cuckoo.wg.Done()
cuckoo.remote()
}()
//}
return cuckoo
}
func NewTester() *Cuckoo {
cuckoo := New(Config{PowMode: ModeTest})
// go cuckoo.remote()
return cuckoo
}
func DeleteTester() {
// C.CuckooRelease()
// CuckooFinalize()
}
// NewShared() func in tests/block_tests_util.go
func NewShared() *Cuckoo {
return &Cuckoo{shared: sharedCuckoo}
}
const PLUGIN_PATH string = "plugins/"
const PLUGIN_POST_FIX string = "_helper_for_node.so"
func (cuckoo *Cuckoo) initPlugin() error {
start := mclock.Now()
var minerName string = "cpu"
if cuckoo.config.UseCuda == true {
minerName = "cuda"
cuckoo.threads = 1
} else if cuckoo.config.UseOpenCL == true {
minerName = "opencl"
cuckoo.threads = 1
}
if cuckoo.config.StrDeviceIds == "" {
cuckoo.config.StrDeviceIds = "0" //default gpu device 0
}
var errc error
so_path := PLUGIN_PATH + minerName + PLUGIN_POST_FIX
cuckoo.minerPlugin, errc = plugin.Open(so_path)
if errc != nil || cuckoo.minerPlugin == nil {
log.Error("Cuckoo Init Plugin", "error", errc)
return errors.New("Cuckoo plugins init failed")
}
elapsed := time.Duration(mclock.Now() - start)
log.Info("Cuckoo Init Plugin", "name", minerName, "library path", so_path,
"threads", cuckoo.threads, "device ids", cuckoo.config.StrDeviceIds, "elapsed", common.PrettyDuration(elapsed))
return errc
}
func (cuckoo *Cuckoo) InitOnce() error {
var err error
cuckoo.once.Do(func() {
if cuckoo.minerPlugin != nil {
return
}
errc := cuckoo.initPlugin()
if errc != nil {
log.Error("Cuckoo Init Plugin", "error", errc)
err = errc //errors.New("Cuckoo plugins init failed")
return
} else {
m, errc := cuckoo.minerPlugin.Lookup("CuckooInitialize")
if errc != nil || m == nil {
log.Error("Cuckoo Init Plugin lookup", "error", errc)
err = errors.New("Cuckoo plugins CuckooInitialize lookup failed")
return
}
// miner algorithm use cuckaroo by default.
if cuckoo.config.Threads > 0 && cuckoo.config.UseCuda {
errc = m.(func(int, string, string) error)(cuckoo.config.Threads, cuckoo.config.StrDeviceIds, cuckoo.config.Algorithm)
} else {
//cuckoo.config.Threads = 0
cuckoo.threads = 0
}
err = errc
var mem gosigar.Mem
if err := mem.Get(); err == nil {
allowance := int(mem.Total / 1024 / 1024 / 3)
log.Warn("Memory status", "total", mem.Total/1024/1024, "allowance", allowance, "cuda", cuckoo.config.UseCuda, "device", cuckoo.config.StrDeviceIds, "threads", cuckoo.config.Threads, "algo", cuckoo.config.Algorithm, "mine", cuckoo.config.Mine)
}
}
})
return err
}
// Close closes the exit channel to notify all backend threads exiting.
func (cuckoo *Cuckoo) Close() error {
close(cuckoo.exitCh)
cuckoo.wg.Wait()
cuckoo.closeOnce.Do(func() {
if cuckoo.minerPlugin == nil {
return
}
m, e := cuckoo.minerPlugin.Lookup("CuckooFinalize")
if e != nil || m == nil {
log.Error("Cuckoo cycle closed error", "error", e)
return
}
m.(func())()
})
return nil
/*
var err error
cuckoo.closeOnce.Do(func() {
// Short circuit if the exit channel is not allocated.
if cuckoo.exitCh == nil {
return
}
errc := make(chan error)
cuckoo.exitCh <- errc
err = <-errc
close(cuckoo.exitCh)
if cuckoo.minerPlugin == nil {
return
}
m, e := cuckoo.minerPlugin.Lookup("CuckooFinalize")
if e != nil {
err = e
return
}
m.(func())()
})
return err
*/
}
func (cuckoo *Cuckoo) Threads() int {
cuckoo.lock.Lock()
defer cuckoo.lock.Unlock()
return cuckoo.threads
}
func (cuckoo *Cuckoo) Hashrate() float64 {
return cuckoo.hashrate.Rate1()
}
func (cuckoo *Cuckoo) APIs(chain consensus.ChainReader) []rpc.API {
// In order to ensure backward compatibility, we exposes cuckoo RPC APIs
// to both ctxc and cuckoo namespaces.
return []rpc.API{
{
Namespace: "ctxc",
Version: "1.0",
Service: &API{cuckoo},
Public: true,
},
}
}
func SeedHash(block uint64) []byte {
seed := make([]byte, 32)
return seed
}