-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
66 lines (59 loc) · 1.33 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
package main
import (
"fmt"
"math/big"
"time"
"github.com/hadv/go-chan/core/types"
"github.com/hadv/go-chan/log"
"github.com/hadv/go-chan/miner"
)
const (
// resultQueueSize is the size of channel listening to sealing result.
resultQueueSize = 10
)
func main() {
fmt.Println("Starting . . . ")
var (
stopCh chan struct{}
resultCh chan *types.Block
)
// interrupt aborts the in-flight sealing task.
interrupt := func() {
if stopCh != nil {
close(stopCh)
stopCh = nil
}
}
number := big.NewInt(0)
miner := miner.New()
go func() {
for {
interrupt()
stopCh = make(chan struct{})
resultCh = make(chan *types.Block)
if err := miner.Seal(&types.Block{
Difficulty: big.NewInt(1000000),
Time: big.NewInt(time.Now().Unix()),
Number: number,
}, resultCh, stopCh); err != nil {
log.Warn("Block sealing failed", "err", err)
}
select {
case result := <-resultCh:
fmt.Println("🔨 mined potential block,", "number =", result.Number.Uint64(), "time =", result.Time.String())
}
number.Add(number, big.NewInt(1))
}
}()
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
api := miner.API()
for {
select {
case <-ticker.C:
if ok := api.SubmitWork(types.EncodeNonce(1)); ok {
fmt.Println("API: submit work from remote is correct...")
}
}
}
}