forked from hacash/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.go
executable file
·196 lines (179 loc) · 4.84 KB
/
block.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
package blocks
import (
"bytes"
"fmt"
"github.com/hacash/core/fields"
"github.com/hacash/core/interfaces"
"github.com/hacash/x16rs"
)
const (
BlockHeadSize = 1 + 5 + 5 + fields.HashSize + fields.HashSize + 4 // = 79
BlockMetaSizeV1 = 4 + 4 + 2 // = 10
)
// protocol
const (
BlockVersion = fields.VarUint1(1) // uint8
TransactionType = fields.VarUint1(2) // uint8
ActionKind = fields.VarUint2(12) // uint16
RepairVersion = fields.VarUint2(1) // uint16
)
////////////////////////////////////////////////////////////
func NewBlockByVersion(ty uint8) (interfaces.Block, error) {
switch ty {
//////////////////// BLOCK ////////////////////
case 1:
return new(Block_v1), nil
//////////////////// END ////////////////////
}
return nil, fmt.Errorf("Cannot find Block type of " + string(ty))
}
func ParseBlock(buf []byte, seek uint32) (interfaces.Block, uint32, error) {
if len(buf) < 1 {
return nil, 0, fmt.Errorf("ParseBlock: buf too short")
}
version := uint8(buf[seek])
var blk, e = NewBlockByVersion(version)
if e != nil {
return nil, 0, e
}
var mv, err = blk.Parse(buf, seek+1)
return blk, mv, err
}
func ParseBlockHead(buf []byte, seek uint32) (interfaces.Block, uint32, error) {
if int(seek)+1 > len(buf) {
return nil, 0, fmt.Errorf("ParseBlockHead: buf too short")
}
version := uint8(buf[seek])
var blk, ee = NewBlockByVersion(version)
if ee != nil {
fmt.Println("Block not Find. Version:", version)
return nil, 0, ee
}
var mv, err = blk.ParseHead(buf, seek+1)
return blk, mv, err
}
func ParseExcludeTransactions(buf []byte, seek uint32) (interfaces.Block, uint32, error) {
if buf == nil || len(buf) < 1 {
return nil, 0, fmt.Errorf("buf is too short")
}
version := uint8(buf[seek])
var blk, ee = NewBlockByVersion(version)
if ee != nil {
fmt.Println("Block not Find. Version:", version)
return nil, 0, ee
}
var mv, err = blk.ParseExcludeTransactions(buf, seek+1)
return blk, mv, err
}
//////////////////////////////////
func CalculateBlockHash(block interfaces.Block) fields.Hash {
stuff := CalculateBlockHashBaseStuff(block)
return x16rs.CalculateBlockHash(block.GetHeight(), stuff)
}
/*
func CalculateBlockHashByStuff(loopnum int, stuff []byte) fields.Hash {
hashbase := sha3.Sum256(stuff)
return x16rs.HashX16RS(loopnum, hashbase[:])
}
*/
func CalculateBlockHashBaseStuff(block interfaces.Block) []byte {
var buffer bytes.Buffer
head, _ := block.SerializeHead()
meta, _ := block.SerializeMeta()
buffer.Write(head)
buffer.Write(meta)
return buffer.Bytes()
}
func CalculateMrklRootByHashWithFee(hashsWithFee []fields.Hash) fields.Hash {
trslen := len(hashsWithFee)
if trslen == 0 {
return fields.EmptyZeroBytes32
}
for true {
if len(hashsWithFee) == 1 {
return hashsWithFee[0]
}
hashsWithFee = hashMerge(hashsWithFee) // Merge two
}
return nil
}
func CalculateMrklRoot(transactions []interfaces.Transaction) fields.Hash {
trslen := len(transactions)
if trslen == 0 {
return fields.EmptyZeroBytes32
}
hashs := make([]fields.Hash, trslen)
for i := 0; i < trslen; i++ {
hashs[i] = transactions[i].HashWithFee()
}
for true {
if len(hashs) == 1 {
return hashs[0]
}
hashs = hashMerge(hashs) // Merge two
}
return nil
}
func hashMerge(hashs []fields.Hash) []fields.Hash {
length := len(hashs)
mgsize := length / 2
if length%2 == 1 {
mgsize = (length + 1) / 2
}
var mergehashs = make([]fields.Hash, mgsize)
for m := 0; m < length; m += 2 {
var buf bytes.Buffer
h1 := hashs[m]
buf.Write(hashs[m])
if m+1 < length {
h2 := hashs[m+1]
buf.Write(h2)
} else {
buf.Write(h1) // repeat h1
}
digest := fields.CalculateHash(buf.Bytes())
mergehashs[m/2] = digest
}
return mergehashs
}
// Recalculate Merkel root by modifying the coinbase TX hash
func CalculateMrklRootByCoinbaseTxModify(coinbasetxhx fields.Hash, mdftree []fields.Hash) fields.Hash {
mrklroot := append([]byte{}, coinbasetxhx...)
for i := 0; i < len(mdftree); i++ {
mghx := hashMerge([]fields.Hash{mrklroot, mdftree[i]})
mrklroot = mghx[0]
}
return mrklroot
}
// Calculate and obtain the Merkel related Hash list related to the coinbase TX modification
func PickMrklListForCoinbaseTxModify(transactions []interfaces.Transaction) []fields.Hash {
hxlist := make([]fields.Hash, 0)
trslen := len(transactions)
if trslen == 0 {
panic("len(transactions) must not be empty") // Cannot be empty
}
if trslen == 1 {
return hxlist
}
if trslen == 2 {
hxlist = append(hxlist, transactions[1].HashWithFee())
return hxlist
}
// Compute hash relation tree
hashs := make([]fields.Hash, trslen)
for i := 0; i < trslen; i++ {
hashs[i] = transactions[i].HashWithFee()
}
for true {
lhx := len(hashs)
if lhx == 1 {
break
}
if lhx >= 2 {
hxlist = append(hxlist, hashs[1]) // Get relationship hash
}
hashs = hashMerge(hashs) // Merge two
}
// ok
return hxlist
}