-
Notifications
You must be signed in to change notification settings - Fork 92
/
EntryCoinbaseDescriptor.go
188 lines (150 loc) · 3.81 KB
/
EntryCoinbaseDescriptor.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package adminBlock
import (
"fmt"
"os"
"github.com/FactomProject/factomd/common/constants"
"github.com/FactomProject/factomd/common/factoid"
"github.com/FactomProject/factomd/common/interfaces"
"github.com/FactomProject/factomd/common/primitives"
)
// CoinbaseDescriptor Entry -------------------------
type CoinbaseDescriptor struct {
AdminIDType uint32 `json:"adminidtype"`
Outputs []interfaces.ITransAddress
}
var _ interfaces.IABEntry = (*CoinbaseDescriptor)(nil)
var _ interfaces.BinaryMarshallable = (*CoinbaseDescriptor)(nil)
func (e *CoinbaseDescriptor) Init() {
e.AdminIDType = uint32(e.Type())
}
func (a *CoinbaseDescriptor) IsSameAs(b *CoinbaseDescriptor) bool {
if a.Type() != b.Type() {
return false
}
for i := range a.Outputs {
if !a.Outputs[i].IsSameAs(b.Outputs[i]) {
return false
}
}
return true
}
func (e *CoinbaseDescriptor) String() string {
e.Init()
var out primitives.Buffer
out.WriteString(fmt.Sprintf(" E: %20s -- %17s %d",
"CoinbaseDescriptor",
"Number of Outputs", len(e.Outputs)))
return (string)(out.DeepCopyBytes())
}
func (c *CoinbaseDescriptor) UpdateState(state interfaces.IState) error {
c.Init()
return nil
}
func NewCoinbaseDescriptor(outputs []interfaces.ITransAddress) (e *CoinbaseDescriptor) {
e = new(CoinbaseDescriptor)
e.Init()
e.Outputs = outputs
return
}
func (e *CoinbaseDescriptor) Type() byte {
return constants.TYPE_COINBASE_DESCRIPTOR
}
func (e *CoinbaseDescriptor) MarshalBinary() (rval []byte, err error) {
defer func(pe *error) {
if *pe != nil {
fmt.Fprintf(os.Stderr, "CoinbaseDescriptor.MarshalBinary err:%v", *pe)
}
}(&err)
e.Init()
var buf primitives.Buffer
err = buf.PushByte(e.Type())
if err != nil {
return nil, err
}
// Need the size of the body
var bodybuf primitives.Buffer
for _, t := range e.Outputs {
err = bodybuf.PushBinaryMarshallable(t)
if err != nil {
return nil, err
}
}
err = buf.PushVarInt(uint64(bodybuf.Len()))
if err != nil {
return nil, err
}
err = buf.Push(bodybuf.Bytes())
if err != nil {
return nil, err
}
return buf.DeepCopyBytes(), nil
}
func (e *CoinbaseDescriptor) UnmarshalBinaryData(data []byte) ([]byte, error) {
buf := primitives.NewBuffer(data)
e.Init()
b, err := buf.PopByte()
if err != nil {
return nil, err
}
if b != e.Type() {
return nil, fmt.Errorf("Invalid Entry type")
}
bl, err := buf.PopVarInt()
if err != nil {
return nil, err
}
body := make([]byte, bl)
n, err := buf.Read(body)
if err != nil {
return nil, err
}
if uint64(n) != bl {
return nil, fmt.Errorf("Expected to read %d bytes, but got %d", bl, n)
}
bodyBuf := primitives.NewBuffer(body)
if uint64(n) != bl {
return nil, fmt.Errorf("Unable to unmarshal body")
}
if e.Outputs == nil {
e.Outputs = make([]interfaces.ITransAddress, 0)
}
for {
if bodyBuf.Len() == 0 {
break
}
it := new(factoid.TransAddress)
err = bodyBuf.PopBinaryMarshallable(it)
if err != nil {
return nil, err
}
it.SetUserAddress(primitives.ConvertFctAddressToUserStr(it.Address))
e.Outputs = append(e.Outputs, it)
}
return buf.DeepCopyBytes(), nil
}
func (e *CoinbaseDescriptor) UnmarshalBinary(data []byte) (err error) {
_, err = e.UnmarshalBinaryData(data)
return
}
func (e *CoinbaseDescriptor) JSONByte() ([]byte, error) {
e.AdminIDType = uint32(e.Type())
return primitives.EncodeJSON(e)
}
func (e *CoinbaseDescriptor) JSONString() (string, error) {
e.AdminIDType = uint32(e.Type())
return primitives.EncodeJSONString(e)
}
func (e *CoinbaseDescriptor) IsInterpretable() bool {
return false
}
func (e *CoinbaseDescriptor) Interpret() string {
return ""
}
func (e *CoinbaseDescriptor) Hash() (rval interfaces.IHash) {
defer func() { rval = primitives.CheckNil(rval, "CoinbaseDescriptor.Hash") }()
bin, err := e.MarshalBinary()
if err != nil {
panic(err)
}
return primitives.Sha(bin)
}