-
Notifications
You must be signed in to change notification settings - Fork 1
/
hashrate.go
145 lines (137 loc) · 3.62 KB
/
hashrate.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
// Package hashrate is a message type for Simplebuffers generated by miners to broadcast an IP address, a count and
// version number and current height of mining work just completed. This data should be stored in a log file and added
// together to generate hashrate reporting in nodes when their controller is running
package hashrate
import (
"crypto/rand"
"encoding/binary"
"io"
"net"
"time"
"github.com/cybriq/gotiny"
"github.com/cybriq/p9/pkg/util/routeable"
)
var Magic = []byte{'h', 'a', 's', 1}
//
// type Container struct {
// simplebuffer.Container
// }
type Hashrate struct {
Time time.Time
IP net.IP
Count int
Version int32
Height int32
Nonce int32
ID string
}
func Get(count int32, version int32, height int32, id string) []byte {
nonce := make([]byte, 4)
var e error
if _, e = io.ReadFull(rand.Reader, nonce); E.Chk(e) {
}
hr := Hashrate{
Time: time.Now(),
IP: routeable.GetListenable(),
Count: int(count),
Version: version,
Height: height,
Nonce: int32(binary.LittleEndian.Uint32(nonce)),
ID: id,
}
srlz := gotiny.Marshal(&hr)
// D.S(srlz)
return srlz
// return Container{*simplebuffer.Serializers{
// Time.New().Put(time.Now()),
// IPs.GetListenable(),
// Int32.New().Put(count),
// Int32.New().Put(version),
// Int32.New().Put(height),
// Int32.New().Put(int32(binary.BigEndian.Uint32(nonce))),
// String.New().Put(id),
// }.CreateContainer(Magic)}
}
//
// // LoadContainer takes a message byte slice payload and loads it into a container
// // ready to be decoded
// func LoadContainer(b []byte) (out Container) {
// out.Data = b
// return
// }
//
// func (j *Container) GetTime() time.Time {
// return Time.New().DecodeOne(j.Get(0)).Get()
// }
//
// func (j *Container) GetIPs() []*net.IP {
// return IPs.New().DecodeOne(j.Get(1)).Get()
// }
//
// func (j *Container) GetCount() int {
// return int(Int32.New().DecodeOne(j.Get(2)).Get())
// }
//
// func (j *Container) GetVersion() int32 {
// return Int32.New().DecodeOne(j.Get(3)).Get()
// }
//
// func (j *Container) GetHeight() int32 {
// return Int32.New().DecodeOne(j.Get(4)).Get()
// }
//
// func (j *Container) GetNonce() int32 {
// return Int32.New().DecodeOne(j.Get(5)).Get()
// }
//
// func (j *Container) GetID() string {
// return String.New().DecodeOne(j.Get(6)).Get()
// }
//
// func (j *Container) String() (s string) {
// s += fmt.Sprint("\ntype '"+string(Magic)+"' elements:", j.Count())
// s += "\n"
// t := j.GetTime()
// s += "1 Time: "
// s += fmt.Sprint(t)
// s += "\n"
// ips := j.GetIPs()
// s += "2 IPs:"
// for i := range ips {
// s += fmt.Sprint(" ", ips[i].String())
// }
// s += "\n"
// count := j.GetCount()
// s += "3 Count: "
// s += fmt.Sprint(count)
// s += "\n"
// version := j.GetVersion()
// s += "4 Version: "
// s += fmt.Sprint(version)
// s += "\n"
// nonce := j.GetNonce()
// s += "5 Nonce: "
// s += fmt.Sprint(nonce)
// s += "\n"
// ID := j.GetID()
// s += "6 ID: "
// s += fmt.Sprint(ID)
// s += "\n"
// return
// }
//
// // Struct deserializes the data all in one go by calling the field deserializing functions into a structure containing
// // the fields. The height is given in this report as it is part of the job message and makes it faster for clients to
// // look up the algorithm name according to the block height, which can change between hard fork versions
// func (j *Container) Struct() (out Hashrate) {
// out = Hashrate{
// Time: j.GetTime(),
// IPs: j.GetIPs(),
// Count: j.GetCount(),
// Version: j.GetVersion(),
// Height: j.GetHeight(),
// Nonce: j.GetNonce(),
// ID: j.GetID(),
// }
// return
// }