-
Notifications
You must be signed in to change notification settings - Fork 21
/
unlocker.go
68 lines (58 loc) · 1.5 KB
/
unlocker.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
package main
import (
"encoding/json"
"net/http"
"strconv"
"time"
)
type BlockUnlocker struct {
db *database
conf *config
lastMinedInFoundPos int64
}
func NewBlockUnlocker(db *database, conf *config) *BlockUnlocker {
return &BlockUnlocker{
db: db,
conf: conf,
lastMinedInFoundPos: 0,
}
}
func (u *BlockUnlocker) readLatestFoundHashes() []string {
return u.db.getAllBlockHashesFrom(u.lastMinedInFoundPos)
}
func (u *BlockUnlocker) checkMature(hash string) int64 {
req, _ := http.NewRequest("GET", "http://"+u.conf.Node.Address+":"+strconv.Itoa(u.conf.Node.APIPort)+"/v1/blocks/"+hash, nil)
req.SetBasicAuth(u.conf.Node.AuthUser, u.conf.Node.AuthPass)
client := &http.Client{}
res, err := client.Do(req)
if err != nil {
log.Error(err)
}
if res.StatusCode == 200 {
content := make(map[string]interface{})
dec := json.NewDecoder(res.Body)
if err := dec.Decode(&content); err == nil || content["header"] != nil {
if headerMap, ok := content["header"].(map[string]interface{}); ok || headerMap["height"] != nil {
return headerMap["height"].(int64)
}
}
}
return -1
}
func (u *BlockUnlocker) watch() {
timeIntervalCh := time.Tick(time.Minute * 5)
for {
select {
case <-timeIntervalCh:
for _, hash := range u.readLatestFoundHashes() {
if h := u.checkMature(hash); h >= 0 {
u.db.putMinedBlock(uint64(h), hash)
}
}
}
}
}
func initUnlocker(db *database, conf *config) {
unlocker := NewBlockUnlocker(db, conf)
unlocker.watch()
}