This repository has been archived by the owner on Aug 27, 2022. It is now read-only.
/
ledger.go
126 lines (111 loc) · 3.42 KB
/
ledger.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
package ledger
import (
"encoding/hex"
"fmt"
"github.com/Qitmeer/qitmeer/core/protocol"
"github.com/Qitmeer/qitmeer/core/types"
)
type LedgerParams struct {
UnlocksPerHeight int64 // How many will be unlocked at each DAG main height.
UnlocksPerHeightStep int64 // How many height will lock a tx.
GenesisAmountUnit int64 // the unit amount of equally divided.
}
// TokenPayout is a payout for block 1 which specifies an address and an amount
// to pay to that address in a transaction output.
type TokenPayout struct {
Address string
PkScript []byte
Amount types.Amount
}
type TokenPayoutReGen struct {
Payout TokenPayout
GenAmount types.Amount
}
type PayoutList []TokenPayoutReGen
func (p PayoutList) Len() int { return len(p) }
func (p PayoutList) Less(i, j int) bool {
x, _ := (&types.Amount{Value: 0, Id: types.MEERID}).Add(&p[i].GenAmount, &p[i].Payout.Amount)
y, _ := (&types.Amount{Value: 0, Id: types.MEERID}).Add(&p[j].GenAmount, &p[j].Payout.Amount)
return x.Value < y.Value
}
func (p PayoutList) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
type PayoutList2 []TokenPayoutReGen
func (p PayoutList2) Len() int { return len(p) }
func (p PayoutList2) Less(i, j int) bool {
return p[i].Payout.Address < p[j].Payout.Address
}
func (p PayoutList2) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
// GenesisLedger specifies the list of payouts in the coinbase of
// genesis. Must be a constant fixed in the code.
// If there are no payouts to be given, set this
// to an empty slice.
var GenesisLedger []*TokenPayout
// BlockOneSubsidy returns the total subsidy of block height 1 for the
// network.
func GenesisLedgerSubsidy() types.Amount {
zero := &types.Amount{Value: 0, Id: types.MEERID}
if len(GenesisLedger) == 0 {
return *zero
}
sum := zero
for _, output := range GenesisLedger {
sum.Add(sum, &output.Amount)
}
return *sum
}
func addPayout(addr string, amount uint64, pksStr string) {
pks, err := hex.DecodeString(pksStr)
if err != nil {
fmt.Printf("Error %v - address:%s amount:%d\n", err, addr, amount)
return
}
var amt *types.Amount
amt, err = types.NewMeer(amount)
if err != nil {
fmt.Printf("Error %v - address:%s amount:%d\n", err, addr, amount)
return
}
GenesisLedger = append(GenesisLedger, &TokenPayout{addr, pks, *amt})
//fmt.Printf("Add payout (%d) - address:%s amount:%d\n",len(GenesisLedger),addr,amount)
}
func addPayout2(addr string, amount types.Amount, pksStr string) {
pks, err := hex.DecodeString(pksStr)
if err != nil {
fmt.Printf("Error %v - address:%s amount:%d\n", err, addr, amount)
return
}
//TODO input check for amout
GenesisLedger = append(GenesisLedger, &TokenPayout{addr, pks, amount})
}
// pay out tokens to a ledger.
func Ledger(tx *types.Transaction, netType protocol.Network) {
if len(GenesisLedger) != 0 {
GenesisLedger = GenesisLedger[:0]
}
switch netType {
case protocol.MainNet:
initMain()
case protocol.TestNet:
initTest()
case protocol.MixNet:
initMix()
case protocol.PrivNet:
initPriv()
}
// Block one is a special block that might pay out tokens to a ledger.
if len(GenesisLedger) != 0 {
// Convert the addresses in the ledger into useable format.
for _, payout := range GenesisLedger {
// Make payout to this address.
tx.AddTxOut(&types.TxOutput{
Amount: payout.Amount,
PkScript: payout.PkScript,
})
}
}
if len(tx.TxOut) <= 0 {
tx.AddTxOut(&types.TxOutput{
Amount: types.Amount{Value: 0, Id: types.MEERID},
})
}
}