forked from hacash/miner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
powmsg.go
73 lines (65 loc) · 1.78 KB
/
powmsg.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
package message
import (
"bytes"
"fmt"
"github.com/hacash/core/blocks"
"github.com/hacash/core/fields"
"github.com/hacash/core/interfaces"
)
const (
PowMasterMsgSize = blocks.BlockHeadSize + blocks.BlockMetaSizeV1 + 1 + 4 + 4
PowMasterMsgStatusContinue fields.VarUint1 = 0
PowMasterMsgStatusSuccess fields.VarUint1 = 1
PowMasterMsgStatusStop fields.VarUint1 = 2
PowMasterMsgStatusMostPowerHash fields.VarUint1 = 3
PowMasterMsgStatusMostPowerHashAndRequestNextMining fields.VarUint1 = 4
)
type PowMasterMsg struct {
Status fields.VarUint1 //
CoinbaseMsgNum fields.VarUint4
NonceBytes fields.Bytes4
BlockHeadMeta interfaces.Block
}
func NewPowMasterMsg() *PowMasterMsg {
return &PowMasterMsg{
0, 0, []byte{0, 0, 0, 0}, blocks.NewEmptyBlockVersion1(nil),
}
}
func (p *PowMasterMsg) Serialize() ([]byte, error) {
buf := bytes.NewBuffer([]byte{})
b1, _ := p.Status.Serialize()
b2, _ := p.CoinbaseMsgNum.Serialize()
b3, _ := p.NonceBytes.Serialize()
b4, _ := p.BlockHeadMeta.SerializeExcludeTransactions()
buf.Write(b1)
buf.Write(b2)
buf.Write(b3)
buf.Write(b4)
return buf.Bytes(), nil
}
func (p *PowMasterMsg) Parse(buf []byte, seek uint32) (uint32, error) {
var e error = nil
if uint32(len(buf))-seek < PowMasterMsgSize {
return 0, fmt.Errorf("size error.")
}
seek, e = p.Status.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = p.CoinbaseMsgNum.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = p.NonceBytes.Parse(buf, seek)
if e != nil {
return 0, e
}
p.BlockHeadMeta, seek, e = blocks.ParseExcludeTransactions(buf, seek)
if e != nil {
return 0, e
}
return seek, nil
}
func (p *PowMasterMsg) Size() uint32 {
return PowMasterMsgSize
}