This repository has been archived by the owner on Jul 30, 2019. It is now read-only.
/
block_header.go
230 lines (195 loc) · 5.1 KB
/
block_header.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package core
import (
"github.com/gogo/protobuf/proto"
"github.com/medibloc/go-medibloc/common"
corepb "github.com/medibloc/go-medibloc/core/pb"
"github.com/medibloc/go-medibloc/crypto"
"github.com/medibloc/go-medibloc/crypto/signature/algorithm"
"github.com/medibloc/go-medibloc/util"
"github.com/medibloc/go-medibloc/util/byteutils"
"github.com/medibloc/go-medibloc/util/logging"
"github.com/sirupsen/logrus"
)
// BlockHeader is block header
type BlockHeader struct {
hash []byte
parentHash []byte
accStateRoot []byte
txStateRoot []byte
dposRoot []byte
coinbase common.Address
reward *util.Uint128
supply *util.Uint128
timestamp int64
chainID uint32
sign []byte
cpuPrice *util.Uint128
cpuUsage uint64
netPrice *util.Uint128
netUsage uint64
}
// toProto converts BlockHeader to corepb.BlockHeader
func (b *BlockHeader) toProto() (proto.Message, error) {
reward, err := b.reward.ToFixedSizeByteSlice()
if err != nil {
return nil, err
}
supply, err := b.supply.ToFixedSizeByteSlice()
if err != nil {
return nil, err
}
cpuPrice, err := b.cpuPrice.ToFixedSizeByteSlice()
if err != nil {
return nil, err
}
netPrice, err := b.netPrice.ToFixedSizeByteSlice()
if err != nil {
return nil, err
}
return &corepb.BlockHeader{
Hash: b.hash,
ParentHash: b.parentHash,
Coinbase: b.coinbase.Bytes(),
Reward: reward,
Supply: supply,
Timestamp: b.timestamp,
ChainId: b.chainID,
Sign: b.sign,
AccStateRoot: b.accStateRoot,
TxStateRoot: b.txStateRoot,
DposRoot: b.dposRoot,
CpuPrice: cpuPrice,
CpuUsage: b.cpuUsage,
NetPrice: netPrice,
NetUsage: b.netUsage,
}, nil
}
// fromProto converts corepb.BlockHeader to BlockHeader
func (b *BlockHeader) fromProto(msg proto.Message) error {
if msg, ok := msg.(*corepb.BlockHeader); ok {
b.hash = msg.Hash
b.parentHash = msg.ParentHash
b.accStateRoot = msg.AccStateRoot
b.txStateRoot = msg.TxStateRoot
b.dposRoot = msg.DposRoot
err := b.coinbase.FromBytes(msg.Coinbase)
if err != nil {
return err
}
reward, err := util.NewUint128FromFixedSizeByteSlice(msg.Reward)
if err != nil {
return err
}
b.reward = reward
supply, err := util.NewUint128FromFixedSizeByteSlice(msg.Supply)
if err != nil {
return err
}
b.supply = supply
b.timestamp = msg.Timestamp
b.chainID = msg.ChainId
b.sign = msg.Sign
cpuPrice, err := util.NewUint128FromFixedSizeByteSlice(msg.CpuPrice)
if err != nil {
return err
}
netPrice, err := util.NewUint128FromFixedSizeByteSlice(msg.NetPrice)
if err != nil {
return err
}
b.cpuPrice = cpuPrice
b.cpuUsage = msg.CpuUsage
b.netPrice = netPrice
b.netUsage = msg.NetUsage
return nil
}
return ErrInvalidProtoToBlockHeader
}
// Hash returns block header's hash
func (b *BlockHeader) Hash() []byte {
return b.hash
}
// HexHash returns block hex encoded header's hash
func (b *BlockHeader) HexHash() string {
return byteutils.Bytes2Hex(b.hash)
}
// ParentHash returns block header's parent hash
func (b *BlockHeader) ParentHash() []byte {
return b.parentHash
}
// AccStateRoot returns block header's accStateRoot
func (b *BlockHeader) AccStateRoot() []byte {
return b.accStateRoot
}
// TxStateRoot returns block header's txsRoot
func (b *BlockHeader) TxStateRoot() []byte {
return b.txStateRoot
}
// DposRoot returns block header's dposRoot
func (b *BlockHeader) DposRoot() []byte {
return b.dposRoot
}
// Coinbase returns coinbase
func (b *BlockHeader) Coinbase() common.Address {
return b.coinbase
}
// Reward returns reward
func (b *BlockHeader) Reward() *util.Uint128 {
return b.reward
}
// Supply returns supply
func (b *BlockHeader) Supply() *util.Uint128 {
return b.supply.DeepCopy()
}
// Timestamp returns timestamp of block
func (b *BlockHeader) Timestamp() int64 {
return b.timestamp
}
// ChainID returns chainID
func (b *BlockHeader) ChainID() uint32 {
return b.chainID
}
// CPUPrice returns cpuPrice
func (b *BlockHeader) CPUPrice() *util.Uint128 {
return b.cpuPrice
}
// NetPrice returns netPrice
func (b *BlockHeader) NetPrice() *util.Uint128 {
return b.netPrice
}
// Sign returns sign
func (b *BlockHeader) Sign() []byte {
return b.sign
}
// CPUUsage returns cpuUsage
func (b *BlockHeader) CPUUsage() uint64 {
return b.cpuUsage
}
// NetUsage returns netUsage
func (b *BlockHeader) NetUsage() uint64 {
return b.netUsage
}
// Proposer returns proposer address from block sign
func (b *BlockHeader) Proposer() (common.Address, error) {
if b.sign == nil {
return common.Address{}, ErrBlockSignatureNotExist
}
sig, err := crypto.NewSignature(algorithm.SECP256K1)
if err != nil {
logging.WithFields(logrus.Fields{
"err": err,
"algorithm": algorithm.SECP256K1,
}).Debug("Invalid sign algorithm.")
return common.Address{}, err
}
pubKey, err := sig.RecoverPublic(b.hash, b.sign)
if err != nil {
logging.WithFields(logrus.Fields{
"err": err,
"plain": byteutils.Bytes2Hex(b.hash),
"cipher": byteutils.Bytes2Hex(b.sign),
}).Debug("Failed to recover public key from cipher text.")
return common.Address{}, err
}
return common.PublicKeyToAddress(pubKey)
}