/
block_pov_btc.go
195 lines (162 loc) · 4.35 KB
/
block_pov_btc.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
package types
import (
"bytes"
"github.com/qlcchain/qlc-go-sdk/pkg/util"
)
//go:generate msgp
type PovBtcHeader struct {
Version uint32 `msg:"v" json:"version"`
Previous Hash `msg:"p,extension" json:"previous"`
MerkleRoot Hash `msg:"mr,extension" json:"merkleRoot"`
Timestamp uint32 `msg:"ts" json:"timestamp"`
Bits uint32 `msg:"b" json:"bits"`
Nonce uint32 `msg:"n" json:"nonce"`
}
func (h *PovBtcHeader) Serialize() ([]byte, error) {
return h.MarshalMsg(nil)
}
func (h *PovBtcHeader) Deserialize(text []byte) error {
_, err := h.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
func (h *PovBtcHeader) BuildHashData() []byte {
buf := new(bytes.Buffer)
buf.Write(util.LE_Uint32ToBytes(h.Version))
buf.Write(h.Previous.Bytes())
buf.Write(h.MerkleRoot.Bytes())
buf.Write(util.LE_Uint32ToBytes(h.Timestamp))
buf.Write(util.LE_Uint32ToBytes(h.Bits))
buf.Write(util.LE_Uint32ToBytes(h.Nonce))
return buf.Bytes()
}
func (h *PovBtcHeader) ComputePowHash(algo PovAlgoType) Hash {
d := h.BuildHashData()
switch algo {
case ALGO_SHA256D:
powHash := Sha256DHashData(d)
return powHash
case ALGO_SCRYPT:
powHash := ScryptHashData(d)
return powHash
case ALGO_X11:
powHash := X11HashData(d)
return powHash
case ALGO_HYBRID:
powHash := HybridHashData(d)
return powHash
}
return FFFFHash
}
func (h *PovBtcHeader) ComputeHash() Hash {
d := h.BuildHashData()
powHash := Sha256DHashData(d)
return powHash
}
type PovBtcOutPoint struct {
Hash Hash `msg:"h,extension" json:"hash"`
Index uint32 `msg:"i" json:"index"`
}
func (p *PovBtcOutPoint) Serialize() ([]byte, error) {
return p.MarshalMsg(nil)
}
func (p *PovBtcOutPoint) Deserialize(text []byte) error {
_, err := p.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
type PovBtcTxIn struct {
PreviousOutPoint PovBtcOutPoint `msg:"pop" json:"previousOutPoint"`
SignatureScript []byte `msg:"ss" json:"signatureScript"`
Sequence uint32 `msg:"s" json:"sequence"`
}
func (ti *PovBtcTxIn) Serialize() ([]byte, error) {
return ti.MarshalMsg(nil)
}
func (ti *PovBtcTxIn) Deserialize(text []byte) error {
_, err := ti.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
func (ti *PovBtcTxIn) BuildHashData() []byte {
buf := new(bytes.Buffer)
buf.Write(ti.PreviousOutPoint.Hash.Bytes())
buf.Write(util.LE_Uint32ToBytes(ti.PreviousOutPoint.Index))
buf.Write(util.LE_EncodeVarInt(uint64(len(ti.SignatureScript))))
buf.Write(ti.SignatureScript)
buf.Write(util.LE_Uint32ToBytes(ti.Sequence))
return buf.Bytes()
}
type PovBtcTxOut struct {
Value int64 `msg:"v" json:"value"`
PkScript []byte `msg:"pks" json:"pkScript"`
}
func (to *PovBtcTxOut) Serialize() ([]byte, error) {
return to.MarshalMsg(nil)
}
func (to *PovBtcTxOut) Deserialize(text []byte) error {
_, err := to.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
func (to *PovBtcTxOut) BuildHashData() []byte {
buf := new(bytes.Buffer)
buf.Write(util.LE_Uint64ToBytes(uint64(to.Value)))
buf.Write(util.LE_EncodeVarInt(uint64(len(to.PkScript))))
buf.Write(to.PkScript)
return buf.Bytes()
}
type PovBtcTx struct {
Version int32 `msg:"v" json:"version"`
TxIn []*PovBtcTxIn `msg:"ti" json:"txIn"`
TxOut []*PovBtcTxOut `msg:"to" json:"txOut"`
LockTime uint32 `msg:"lt" json:"lockTime"`
}
func (tx *PovBtcTx) Serialize() ([]byte, error) {
return tx.MarshalMsg(nil)
}
func (tx *PovBtcTx) Deserialize(text []byte) error {
_, err := tx.UnmarshalMsg(text)
if err != nil {
return err
}
return nil
}
func (tx *PovBtcTx) BuildHashData() []byte {
buf := new(bytes.Buffer)
buf.Write(util.LE_Uint32ToBytes(uint32(tx.Version)))
buf.Write(util.LE_EncodeVarInt(uint64(len(tx.TxIn))))
for _, ti := range tx.TxIn {
buf.Write(ti.BuildHashData())
}
buf.Write(util.LE_EncodeVarInt(uint64(len(tx.TxOut))))
for _, to := range tx.TxOut {
buf.Write(to.BuildHashData())
}
buf.Write(util.LE_Uint32ToBytes(tx.LockTime))
return buf.Bytes()
}
func (tx *PovBtcTx) ComputeHash() Hash {
d := tx.BuildHashData()
powHash := Sha256DHashData(d)
return powHash
}
func NewPovBtcTx(txIn []*PovBtcTxIn, txOut []*PovBtcTxOut) *PovBtcTx {
tx := &PovBtcTx{
Version: 1,
TxIn: make([]*PovBtcTxIn, 0),
TxOut: make([]*PovBtcTxOut, 0),
LockTime: 0,
}
tx.TxIn = append(tx.TxIn, txIn...)
tx.TxOut = append(tx.TxOut, txOut...)
return tx
}