hashrate.go raw
1 // Package hashrate is a message type for Simplebuffers generated by miners to broadcast an IP address, a count and
2 // version number and current height of mining work just completed. This data should be stored in a log file and added
3 // together to generate hashrate reporting in nodes when their controller is running
4 package hashrate
5
6 import (
7 "crypto/rand"
8 "encoding/binary"
9 "io"
10 "net"
11 "time"
12
13 "github.com/niubaoshu/gotiny"
14
15 "github.com/p9c/p9/pkg/util/routeable"
16 )
17
18 var Magic = []byte{'h', 'a', 's', 1}
19
20 //
21 // type Container struct {
22 // simplebuffer.Container
23 // }
24
25 type Hashrate struct {
26 Time time.Time
27 IP net.IP
28 Count int
29 Version int32
30 Height int32
31 Nonce int32
32 ID string
33 }
34
35 func Get(count int32, version int32, height int32, id string) []byte {
36 nonce := make([]byte, 4)
37 var e error
38 if _, e = io.ReadFull(rand.Reader, nonce); E.Chk(e) {
39 }
40 hr := Hashrate{
41 Time: time.Now(),
42 IP: routeable.GetListenable(),
43 Count: int(count),
44 Version: version,
45 Height: height,
46 Nonce: int32(binary.LittleEndian.Uint32(nonce)),
47 ID: id,
48 }
49 srlz := gotiny.Marshal(&hr)
50 // D.S(srlz)
51 return srlz
52 // return Container{*simplebuffer.Serializers{
53 // Time.New().Put(time.Now()),
54 // IPs.GetListenable(),
55 // Int32.New().Put(count),
56 // Int32.New().Put(version),
57 // Int32.New().Put(height),
58 // Int32.New().Put(int32(binary.BigEndian.Uint32(nonce))),
59 // String.New().Put(id),
60 // }.CreateContainer(Magic)}
61 }
62
63 //
64 // // LoadContainer takes a message byte slice payload and loads it into a container
65 // // ready to be decoded
66 // func LoadContainer(b []byte) (out Container) {
67 // out.Data = b
68 // return
69 // }
70 //
71 // func (j *Container) GetTime() time.Time {
72 // return Time.New().DecodeOne(j.Get(0)).Get()
73 // }
74 //
75 // func (j *Container) GetIPs() []*net.IP {
76 // return IPs.New().DecodeOne(j.Get(1)).Get()
77 // }
78 //
79 // func (j *Container) GetCount() int {
80 // return int(Int32.New().DecodeOne(j.Get(2)).Get())
81 // }
82 //
83 // func (j *Container) GetVersion() int32 {
84 // return Int32.New().DecodeOne(j.Get(3)).Get()
85 // }
86 //
87 // func (j *Container) GetHeight() int32 {
88 // return Int32.New().DecodeOne(j.Get(4)).Get()
89 // }
90 //
91 // func (j *Container) GetNonce() int32 {
92 // return Int32.New().DecodeOne(j.Get(5)).Get()
93 // }
94 //
95 // func (j *Container) GetID() string {
96 // return String.New().DecodeOne(j.Get(6)).Get()
97 // }
98 //
99 // func (j *Container) String() (s string) {
100 // s += fmt.Sprint("\ntype '"+string(Magic)+"' elements:", j.Count())
101 // s += "\n"
102 // t := j.GetTime()
103 // s += "1 Time: "
104 // s += fmt.Sprint(t)
105 // s += "\n"
106 // ips := j.GetIPs()
107 // s += "2 IPs:"
108 // for i := range ips {
109 // s += fmt.Sprint(" ", ips[i].String())
110 // }
111 // s += "\n"
112 // count := j.GetCount()
113 // s += "3 Count: "
114 // s += fmt.Sprint(count)
115 // s += "\n"
116 // version := j.GetVersion()
117 // s += "4 Version: "
118 // s += fmt.Sprint(version)
119 // s += "\n"
120 // nonce := j.GetNonce()
121 // s += "5 Nonce: "
122 // s += fmt.Sprint(nonce)
123 // s += "\n"
124 // ID := j.GetID()
125 // s += "6 ID: "
126 // s += fmt.Sprint(ID)
127 // s += "\n"
128 // return
129 // }
130 //
131 // // Struct deserializes the data all in one go by calling the field deserializing functions into a structure containing
132 // // the fields. The height is given in this report as it is part of the job message and makes it faster for clients to
133 // // look up the algorithm name according to the block height, which can change between hard fork versions
134 // func (j *Container) Struct() (out Hashrate) {
135 // out = Hashrate{
136 // Time: j.GetTime(),
137 // IPs: j.GetIPs(),
138 // Count: j.GetCount(),
139 // Version: j.GetVersion(),
140 // Height: j.GetHeight(),
141 // Nonce: j.GetNonce(),
142 // ID: j.GetID(),
143 // }
144 // return
145 // }
146