/
block.go
269 lines (236 loc) · 6.73 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
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
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package types
import (
"bytes"
"runtime"
"sync"
"github.com/33cn/chain33/common"
"github.com/33cn/chain33/common/crypto"
)
// Hash 获取block的hash值
func (block *Block) Hash(cfg *Chain33Config) []byte {
if cfg.IsFork(block.Height, "ForkBlockHash") {
return block.HashNew()
}
return block.HashOld()
}
//HashByForkHeight hash 通过自己设置的fork 高度计算 hash
func (block *Block) HashByForkHeight(forkheight int64) []byte {
if block.Height >= forkheight {
return block.HashNew()
}
return block.HashOld()
}
//HashNew 新版本的Hash
func (block *Block) HashNew() []byte {
data := Encode(block.getHeaderHashNew())
return common.Sha256(data)
}
//HashOld 老版本的hash
func (block *Block) HashOld() []byte {
data := Encode(block.getHeaderHashOld())
return common.Sha256(data)
}
// Size 获取block的Size
func (block *Block) Size() int {
return Size(block)
}
// GetHeader 获取block的Header信息
func (block *Block) GetHeader(cfg *Chain33Config) *Header {
head := &Header{}
head.Version = block.Version
head.ParentHash = block.ParentHash
head.TxHash = block.TxHash
head.BlockTime = block.BlockTime
head.Height = block.Height
head.Difficulty = block.Difficulty
head.StateHash = block.StateHash
head.TxCount = int64(len(block.Txs))
head.Hash = block.Hash(cfg)
return head
}
// SetHeader 设置header, 注意相关字段必须和GetHeader一致, 一起维护
func (block *Block) SetHeader(header *Header) {
block.Version = header.Version
block.ParentHash = header.ParentHash
block.TxHash = header.TxHash
block.BlockTime = header.BlockTime
block.Height = header.Height
block.Difficulty = header.Difficulty
block.StateHash = header.StateHash
block.Signature = header.Signature
}
func (block *Block) getHeaderHashOld() *Header {
head := &Header{}
head.Version = block.Version
head.ParentHash = block.ParentHash
head.TxHash = block.TxHash
head.BlockTime = block.BlockTime
head.Height = block.Height
return head
}
func (block *Block) getHeaderHashNew() *Header {
head := &Header{}
head.Version = block.Version
head.ParentHash = block.ParentHash
head.TxHash = block.TxHash
head.BlockTime = block.BlockTime
head.Height = block.Height
head.Difficulty = block.Difficulty
head.StateHash = block.StateHash
head.TxCount = int64(len(block.Txs))
return head
}
// VerifySignature 验证区块和交易的签名,支持指定需要验证的交易
func VerifySignature(cfg *Chain33Config, block *Block, txs []*Transaction) bool {
//检查区块的签名
if !block.verifySignature(cfg) {
return false
}
//检查交易的签名
return verifyTxsSignature(txs, block.GetHeight())
}
// CheckSign 检测block的签名,以及交易的签名
func (block *Block) CheckSign(cfg *Chain33Config) bool {
return VerifySignature(cfg, block, block.Txs)
}
func (block *Block) verifySignature(cfg *Chain33Config) bool {
if block.GetSignature() == nil {
return true
}
hash := block.Hash(cfg)
return CheckSign(hash, "", block.GetSignature(), block.GetHeight())
}
func verifyTxsSignature(txs []*Transaction, blockHeight int64) bool {
//没有需要要验签的交易,直接返回
if len(txs) == 0 {
return true
}
done := make(chan struct{})
defer close(done)
taskes := gen(done, txs)
cpuNum := runtime.NumCPU()
// Start a fixed number of goroutines to read and digest files.
c := make(chan result) // HLc
var wg sync.WaitGroup
wg.Add(cpuNum)
for i := 0; i < cpuNum; i++ {
go func() {
checksign(done, taskes, c, blockHeight) // HLc
wg.Done()
}()
}
go func() {
wg.Wait()
close(c) // HLc
}()
// End of pipeline. OMIT
for r := range c {
if !r.isok {
return false
}
}
return true
}
func gen(done <-chan struct{}, task []*Transaction) <-chan *Transaction {
ch := make(chan *Transaction)
go func() {
defer func() {
close(ch)
}()
for i := 0; i < len(task); i++ {
select {
case ch <- task[i]:
case <-done:
return
}
}
}()
return ch
}
type result struct {
isok bool
}
func check(data *Transaction, blockHeight int64) bool {
return data.CheckSign(blockHeight)
}
func checksign(done <-chan struct{}, taskes <-chan *Transaction, c chan<- result, blockHeight int64) {
for task := range taskes {
select {
case c <- result{check(task, blockHeight)}:
case <-done:
return
}
}
}
// CheckSign 检测签名
func CheckSign(data []byte, execer string, sign *Signature, blockHeight int64) bool {
//GetDefaultSign: 系统内置钱包,非插件中的签名
c, err := crypto.Load(GetSignName(execer, int(sign.Ty)), blockHeight)
if err != nil {
return false
}
return c.Validate(data, sign.Pubkey, sign.Signature) == nil
}
//FilterParaTxsByTitle 过滤指定title的平行链交易
//1,单笔平行连交易
//2,交易组中的平行连交易,需要将整个交易组都过滤出来
//目前暂时不返回单个交易的proof证明路径,
//后面会将平行链的交易组装到一起,构成一个子roothash。会返回子roothash的proof证明路径
func (blockDetail *BlockDetail) FilterParaTxsByTitle(cfg *Chain33Config, title string) *ParaTxDetail {
var paraTx ParaTxDetail
paraTx.Header = blockDetail.Block.GetHeader(cfg)
for i := 0; i < len(blockDetail.Block.Txs); i++ {
tx := blockDetail.Block.Txs[i]
if IsSpecificParaExecName(title, string(tx.Execer)) {
//过滤交易组中的para交易,需要将整个交易组都过滤出来
if tx.GroupCount >= 2 {
txDetails, endIdx := blockDetail.filterParaTxGroup(tx, i)
paraTx.TxDetails = append(paraTx.TxDetails, txDetails...)
i = endIdx - 1
continue
}
//单笔para交易
var txDetail TxDetail
txDetail.Tx = tx
txDetail.Receipt = blockDetail.Receipts[i]
txDetail.Index = uint32(i)
paraTx.TxDetails = append(paraTx.TxDetails, &txDetail)
}
}
return ¶Tx
}
//filterParaTxGroup 获取para交易所在交易组信息
func (blockDetail *BlockDetail) filterParaTxGroup(tx *Transaction, index int) ([]*TxDetail, int) {
var headIdx int
var txDetails []*TxDetail
for i := index; i >= 0; i-- {
if bytes.Equal(tx.Header, blockDetail.Block.Txs[i].Hash()) {
headIdx = i
break
}
}
endIdx := headIdx + int(tx.GroupCount)
for i := headIdx; i < endIdx; i++ {
var txDetail TxDetail
txDetail.Tx = blockDetail.Block.Txs[i]
txDetail.Receipt = blockDetail.Receipts[i]
txDetail.Index = uint32(i)
txDetails = append(txDetails, &txDetail)
}
return txDetails, endIdx
}
// Size 获取blockDetail的Size
func (blockDetail *BlockDetail) Size() int {
return Size(blockDetail)
}
// Size 获取header的Size
func (header *Header) Size() int {
return Size(header)
}
// Size 获取paraTxDetail的Size
func (paraTxDetail *ParaTxDetail) Size() int {
return Size(paraTxDetail)
}