-
Notifications
You must be signed in to change notification settings - Fork 92
/
EntryEndOfMinute.go
112 lines (90 loc) · 2.47 KB
/
EntryEndOfMinute.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
package adminBlock
import (
"fmt"
"os"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
type EndOfMinuteEntry struct {
AdminIDType uint32 `json:"adminidtype"`
MinuteNumber byte `json:"minutenumber"`
}
var _ interfaces.Printable = (*EndOfMinuteEntry)(nil)
var _ interfaces.BinaryMarshallable = (*EndOfMinuteEntry)(nil)
var _ interfaces.IABEntry = (*EndOfMinuteEntry)(nil)
func (m *EndOfMinuteEntry) Type() byte {
return constants.TYPE_MINUTE_NUM
}
func (c *EndOfMinuteEntry) UpdateState(state interfaces.IState) error {
return nil
}
func NewEndOfMinuteEntry(minuteNumber byte) *EndOfMinuteEntry {
e := new(EndOfMinuteEntry)
e.MinuteNumber = minuteNumber
return e
}
func (e *EndOfMinuteEntry) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "EndOfMinuteEntry.MarshalBinary err:%v", *pe)
}
}(&err)
var buf primitives.Buffer
e.AdminIDType = uint32(e.Type())
err = buf.PushByte(e.Type())
if err != nil {
return nil, err
}
err = buf.PushByte(e.MinuteNumber)
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (e *EndOfMinuteEntry) UnmarshalBinaryData(data []byte) ([]byte, error) {
buf := primitives.NewBuffer(data)
b, err := buf.PopByte()
if err != nil {
return nil, err
}
if b != e.Type() {
return nil, fmt.Errorf("Invalid Entry type")
}
e.MinuteNumber, err = buf.PopByte()
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (e *EndOfMinuteEntry) UnmarshalBinary(data []byte) (err error) {
_, err = e.UnmarshalBinaryData(data)
return
}
func (e *EndOfMinuteEntry) JSONByte() ([]byte, error) {
e.AdminIDType = uint32(e.Type())
return primitives.EncodeJSON(e)
}
func (e *EndOfMinuteEntry) JSONString() (string, error) {
e.AdminIDType = uint32(e.Type())
return primitives.EncodeJSONString(e)
}
func (e *EndOfMinuteEntry) String() string {
return fmt.Sprintf(" E: %35s -- %17s %d",
"EndOfMinuteEntry",
"Minute", e.MinuteNumber)
}
func (e *EndOfMinuteEntry) IsInterpretable() bool {
return true
}
func (e *EndOfMinuteEntry) Interpret() string {
return fmt.Sprintf("End of Minute %v", e.MinuteNumber)
}
func (e *EndOfMinuteEntry) Hash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "EndOfMinuteEntry.Hash") }()
bin, err := e.MarshalBinary()
if err != nil {
panic(err)
}
return primitives.Sha(bin)
}