forked from hacash/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coinbase.go
executable file
·437 lines (390 loc) · 11.7 KB
/
coinbase.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
package transactions
import (
"bytes"
"encoding/hex"
"fmt"
"github.com/hacash/core/account"
"github.com/hacash/core/actions"
"github.com/hacash/core/fields"
"github.com/hacash/core/interfaces"
"github.com/hacash/core/interfacev2"
"github.com/hacash/core/stores"
"math/big"
)
type Transaction_0_Coinbase struct {
// type
Address fields.Address
Reward fields.Amount
Message fields.TrimString16
// Version number
ExtendDataVersion fields.VarUint1 // Equal to 0 before 220000 height
// When extenddataversion > = 1, it has the following fields:
MinerNonce fields.Bytes32
WitnessCount fields.VarUint1 // Number of voting witnesses
WitnessSigs []uint8 // Witness specified hash mantissa
Witnesses []fields.Sign // Signature of prev block hash, voting fork
/* -------- -------- */
// cache data
TotalFeeUserPayed fields.Amount // Total transaction fee of the block
TotalFeeMinerReceived fields.Amount // Total transaction fee of the block
}
func NewTransaction_0_CoinbaseV0() *Transaction_0_Coinbase {
return &Transaction_0_Coinbase{
ExtendDataVersion: 0,
}
}
func NewTransaction_0_CoinbaseV1() *Transaction_0_Coinbase {
return &Transaction_0_Coinbase{
ExtendDataVersion: 1,
MinerNonce: make([]byte, 32),
WitnessCount: 0,
}
}
func (trs *Transaction_0_Coinbase) Describe(isUnitMei, isForMining bool) map[string]interface{} {
cbinfo := make(map[string]interface{})
cbinfo["address"] = trs.Address.ToReadable()
cbinfo["reward"] = trs.Reward.ToMeiOrFinString(isUnitMei)
msg, _ := trs.Message.Serialize()
if isForMining {
cbinfo["message_hex"] = hex.EncodeToString(msg)
}
cbinfo["message"] = trs.Message
extendDataVersion := uint8(trs.ExtendDataVersion)
cbinfo["extend_data_version"] = extendDataVersion
if extendDataVersion >= 1 {
if !isForMining {
cbinfo["miner_nonce"] = trs.MinerNonce.ToHex() // unwanted
}
wcnum := int(trs.WitnessCount)
cbinfo["witness_count"] = wcnum
wtnum := make([]int, wcnum)
wtsig := make([]string, wcnum)
for i := 0; i < wcnum; i++ {
wtnum[i] = int(trs.WitnessSigs[i])
sigbts, _ := trs.Witnesses[i].Serialize()
wtsig[i] = hex.EncodeToString(sigbts)
}
cbinfo["witness_sigs"] = wtnum
cbinfo["witnesses"] = wtsig
}
return cbinfo
}
func (trs *Transaction_0_Coinbase) ClearHash() {
}
func (trs *Transaction_0_Coinbase) CopyForMining() *Transaction_0_Coinbase {
// copy
bodys, _ := trs.Serialize()
newtrsbts := make([]byte, len(bodys))
copy(newtrsbts, bodys)
// create
var newtrs = new(Transaction_0_Coinbase)
newtrs.Parse(newtrsbts, 1) // over type
return newtrs
}
func (trs *Transaction_0_Coinbase) Copy() interfacev2.Transaction {
return trs.CopyForMining()
}
func (trs *Transaction_0_Coinbase) Clone() interfaces.Transaction {
return trs.CopyForMining()
}
func (trs *Transaction_0_Coinbase) GetReward() *fields.Amount {
return &trs.Reward
}
func (trs *Transaction_0_Coinbase) Type() uint8 {
return 0
}
func (trs *Transaction_0_Coinbase) Serialize() ([]byte, error) {
var buffer bytes.Buffer
b1, _ := trs.Address.Serialize()
b2, _ := trs.Reward.Serialize()
b3, _ := trs.Message.Serialize()
b4, _ := trs.ExtendDataVersion.Serialize()
// fmt.Println("trs.Message=", trs.Message)
buffer.Write([]byte{trs.Type()}) // type
buffer.Write(b1)
buffer.Write(b2)
buffer.Write(b3)
buffer.Write(b4)
// Version number
version := uint8(trs.ExtendDataVersion)
// ----------- 按版本区分 ----------- //
if version == 0 {
// No subsequent fields
}
if version >= 1 {
// Additional nonce values
buffer.Write(trs.MinerNonce)
// witness
witnessCount := uint8(trs.WitnessCount)
buffer.Write([]byte{witnessCount})
for i := uint8(0); i < witnessCount; i++ {
b := trs.WitnessSigs[i]
buffer.Write([]byte{b})
}
for i := uint8(0); i < witnessCount; i++ {
b := trs.Witnesses[i]
s1, e := b.Serialize()
if e != nil {
return nil, e
}
buffer.Write(s1)
}
}
return buffer.Bytes(), nil
}
func (trs *Transaction_0_Coinbase) Parse(buf []byte, seek uint32) (uint32, error) {
var e error
seek, e = trs.ParseHead(buf, seek)
if e != nil {
return 0, e
}
seek, e = trs.ExtendDataVersion.Parse(buf, seek)
if e != nil {
return 0, e
}
// ----------- 按版本区分 ----------- //
// Judge version number
version := uint8(trs.ExtendDataVersion)
if version == 0 {
// No subsequent fields
}
if version >= 1 {
// Nonce value
seek, e = trs.MinerNonce.Parse(buf, seek)
if e != nil {
return 0, e
}
// witness
seek, e = trs.WitnessCount.Parse(buf, seek)
if e != nil {
return 0, e
}
if trs.WitnessCount > 0 {
lenwc := int(trs.WitnessCount)
trs.WitnessSigs = make([]uint8, lenwc)
trs.Witnesses = make([]fields.Sign, lenwc)
for i := 0; i < lenwc; i++ {
if seek >= uint32(len(buf)) {
return 0, fmt.Errorf("seek out of buf len.")
}
trs.WitnessSigs[i] = buf[seek]
seek++
}
for i := 0; i < lenwc; i++ {
var sign fields.Sign
if seek >= uint32(len(buf)) {
return 0, fmt.Errorf("seek out of buf len.")
}
seek, e = sign.Parse(buf, seek)
if e != nil {
return 0, e
}
trs.Witnesses[i] = sign
}
}
}
return seek, nil
}
func (trs *Transaction_0_Coinbase) ParseHead(buf []byte, seek uint32) (uint32, error) {
var e error
seek, e = trs.Address.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = trs.Reward.Parse(buf, seek)
if e != nil {
return 0, e
}
seek, e = trs.Message.Parse(buf, seek)
if e != nil {
return 0, e
}
return seek, nil
}
func (trs *Transaction_0_Coinbase) Size() uint32 {
base := 1 +
trs.Address.Size() +
trs.Reward.Size() +
trs.Message.Size() +
trs.ExtendDataVersion.Size()
// ----------- 按版本区分 ----------- //
// Judge version number
version := uint8(trs.ExtendDataVersion)
if version == 0 {
// No subsequent fields
}
if version >= 1 {
base += 32 // nonce
base += 1 // WitnessCount
length := int(trs.WitnessCount) // WitnessSigs size
base += uint32(length)
for i := 0; i < length; i++ {
base += trs.Witnesses[i].Size()
}
}
return base
}
// Transaction unique hash value
func (trs *Transaction_0_Coinbase) HashWithFee() fields.Hash {
stuff, _ := trs.Serialize()
digest := fields.CalculateHash(stuff)
return digest
}
func (trs *Transaction_0_Coinbase) Hash() fields.Hash {
return trs.HashWithFee()
}
// Balance check required
func (trs *Transaction_0_Coinbase) RequestAddressBalance() ([][]byte, []big.Int, error) {
panic("cannot RequestAddressBalance for Transaction_0_Coinbase")
}
// Get the address to be signed from actions
func (trs *Transaction_0_Coinbase) RequestSignAddresses([]fields.Address, bool) ([]fields.Address, error) {
panic("never call Transaction_0_Coinbase.RequestSignAddresses")
return []fields.Address{}, nil
}
func (trs *Transaction_0_Coinbase) VerifyTargetSigns([]fields.Address) (bool, error) {
panic("never call Transaction_0_Coinbase.VerifyTargetSigns")
return true, nil
}
// Clear all signatures
func (trs *Transaction_0_Coinbase) CleanSigns() {
panic("cannot CleanSigns for Transaction_0_Coinbase")
}
// Return all signatures
func (trs *Transaction_0_Coinbase) GetSigns() []fields.Sign {
panic("cannot GetSigns for Transaction_0_Coinbase")
}
// Set signature
func (trs *Transaction_0_Coinbase) SetSigns([]fields.Sign) {
panic("cannot SetSigns for Transaction_0_Coinbase")
}
// Fill in all required signatures
func (trs *Transaction_0_Coinbase) FillTargetSign(*account.Account) error {
panic("cannot FillTargetSign for Transaction_0_Coinbase")
}
// Fill in signature
func (trs *Transaction_0_Coinbase) FillNeedSigns(map[string][]byte, []fields.Address) error {
panic("cannot FillNeedSigns for Transaction_0_Coinbase")
}
// Verify required signatures
func (trs *Transaction_0_Coinbase) VerifyAllNeedSigns() (bool, error) {
return true, nil
}
func (trs *Transaction_0_Coinbase) WriteInChainState(state interfaces.ChainStateOperation) error {
// Total supply statistics
// reward
totalsupply, e2 := state.ReadTotalSupply()
if e2 != nil {
return e2
}
totalsupply.DoAdd(stores.TotalSupplyStoreTypeOfBlockReward, trs.Reward.ToMei())
// feeBurning
if trs.TotalFeeMinerReceived.NotEqual(&trs.TotalFeeUserPayed) {
// With destruction
burnamt, e := trs.TotalFeeUserPayed.Sub(&trs.TotalFeeMinerReceived)
if e != nil {
return e
}
totalsupply.DoAdd(stores.TotalSupplyStoreTypeOfBurningFee, burnamt.ToMei())
}
// update total supply
e3 := state.UpdateSetTotalSupply(totalsupply)
if e3 != nil {
return e3
}
// fmt.Printf("trs.TotalFee = %s\n", trs.TotalFee.ToFinString())
rwd_and_txfee, _ := trs.Reward.Add(&trs.TotalFeeMinerReceived)
// addr, _ := base58check.Encode(trs.Address)
// fmt.Printf("coinbase.ChangeChainState, %s += %s\n", addr, rwd.ToFinString())
return actions.DoAddBalanceFromChainStateV3(state, trs.Address, *rwd_and_txfee)
}
// 修改 / 恢复 状态数据库
func (trs *Transaction_0_Coinbase) WriteinChainState(state interfacev2.ChainStateOperation) error {
// Total supply statistics
// reward
totalsupply, e2 := state.ReadTotalSupply()
if e2 != nil {
return e2
}
totalsupply.DoAdd(stores.TotalSupplyStoreTypeOfBlockReward, trs.Reward.ToMei())
// feeBurning
if trs.TotalFeeMinerReceived.NotEqual(&trs.TotalFeeUserPayed) {
// With destruction
burnamt, e := trs.TotalFeeUserPayed.Sub(&trs.TotalFeeMinerReceived)
if e != nil {
return e
}
totalsupply.DoAdd(stores.TotalSupplyStoreTypeOfBurningFee, burnamt.ToMei())
}
// update total supply
e3 := state.UpdateSetTotalSupply(totalsupply)
if e3 != nil {
return e3
}
// fmt.Printf("trs.TotalFee = %s\n", trs.TotalFee.ToFinString())
rwd_and_txfee, _ := trs.Reward.Add(&trs.TotalFeeMinerReceived)
// addr, _ := base58check.Encode(trs.Address)
// fmt.Printf("coinbase.ChangeChainState, %s += %s\n", addr, rwd.ToFinString())
return actions.DoAddBalanceFromChainState(state, trs.Address, *rwd_and_txfee)
}
func (trs *Transaction_0_Coinbase) RecoverChainState(state interfacev2.ChainStateOperation) error {
panic("RecoverChainState be deprecated")
// Total supply statistics
// reward
totalsupply, e2 := state.ReadTotalSupply()
if e2 != nil {
return e2
}
totalsupply.DoSub(stores.TotalSupplyStoreTypeOfBlockReward, trs.Reward.ToMei())
// feeBurning
if trs.TotalFeeMinerReceived.NotEqual(&trs.TotalFeeUserPayed) {
// With destruction
burnamt, e := trs.TotalFeeUserPayed.Sub(&trs.TotalFeeMinerReceived)
if e != nil {
return e
}
totalsupply.DoSub(stores.TotalSupplyStoreTypeOfBurningFee, burnamt.ToMei())
}
// update total supply
e3 := state.UpdateSetTotalSupply(totalsupply)
if e3 != nil {
return e3
}
// Address balance
rwd_and_txfee, _ := trs.Reward.Add(&trs.TotalFeeMinerReceived)
return actions.DoSubBalanceFromChainState(state, trs.Address, *rwd_and_txfee)
}
func (trs *Transaction_0_Coinbase) FeePurity() uint64 {
panic("cannot GetFeePurity for Transaction_0_Coinbase")
}
// query
func (trs *Transaction_0_Coinbase) GetAddress() fields.Address {
return trs.Address
}
func (trs *Transaction_0_Coinbase) SetAddress(addr fields.Address) {
trs.Address = addr
}
func (trs *Transaction_0_Coinbase) GetFeeOfMinerRealReceived() *fields.Amount {
return &trs.TotalFeeMinerReceived
}
func (trs *Transaction_0_Coinbase) GetFee() *fields.Amount {
return &trs.TotalFeeUserPayed
}
func (trs *Transaction_0_Coinbase) SetFee(fee *fields.Amount) {
panic("cannot SetFee for Transaction_0_Coinbase")
}
func (trs *Transaction_0_Coinbase) GetActions() []interfacev2.Action {
return nil
}
func (trs *Transaction_0_Coinbase) GetActionList() []interfaces.Action {
return nil
}
func (trs *Transaction_0_Coinbase) GetTimestamp() uint64 { // time stamp
panic("cannot GetTimestamp for Transaction_0_Coinbase")
}
func (trs *Transaction_0_Coinbase) SetMessage(msg fields.TrimString16) {
trs.Message = msg
}
func (trs *Transaction_0_Coinbase) GetMessage() fields.TrimString16 {
return trs.Message
}