/
blocktemplatecache.go
130 lines (110 loc) · 3.96 KB
/
blocktemplatecache.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
package blkmgr
import (
"github.com/Qitmeer/qng/core/types"
)
// getCurrentTemplateMsg handles a request for the current mining block template.
type getCurrentTemplateMsg struct {
reply chan getCurrentTemplateResponse
}
// getCurrentTemplateResponse is a response sent to the reply channel of a
// getCurrentTemplateMsg.
type getCurrentTemplateResponse struct {
Template *types.BlockTemplate
}
// setCurrentTemplateMsg handles a request to change the current mining block
// template.
type setCurrentTemplateMsg struct {
Template *types.BlockTemplate
reply chan setCurrentTemplateResponse
}
// setCurrentTemplateResponse is a response sent to the reply channel of a
// setCurrentTemplateMsg.
type setCurrentTemplateResponse struct {
}
// getParentTemplateMsg handles a request for the current parent mining block
// template.
type getParentTemplateMsg struct {
reply chan getParentTemplateResponse
}
// getParentTemplateResponse is a response sent to the reply channel of a
// getParentTemplateMsg.
type getParentTemplateResponse struct {
Template *types.BlockTemplate
}
// setParentTemplateMsg handles a request to change the parent mining block
// template.
type setParentTemplateMsg struct {
Template *types.BlockTemplate
reply chan setParentTemplateResponse
}
// setParentTemplateResponse is a response sent to the reply channel of a
// setParentTemplateMsg.
type setParentTemplateResponse struct {
}
// GetCurrentTemplate gets the current block template for mining.
func (b *BlockManager) GetCurrentTemplate() *types.BlockTemplate {
reply := make(chan getCurrentTemplateResponse)
b.msgChan <- getCurrentTemplateMsg{reply: reply}
response := <-reply
return response.Template
}
// SetCurrentTemplate sets the current block template for mining.
func (b *BlockManager) SetCurrentTemplate(bt *types.BlockTemplate) {
reply := make(chan setCurrentTemplateResponse)
b.msgChan <- setCurrentTemplateMsg{Template: bt, reply: reply}
<-reply
}
// GetParentTemplate gets the current parent block template for mining.
func (b *BlockManager) GetParentTemplate() *types.BlockTemplate {
reply := make(chan getParentTemplateResponse)
b.msgChan <- getParentTemplateMsg{reply: reply}
response := <-reply
return response.Template
}
// SetParentTemplate sets the current parent block template for mining.
func (b *BlockManager) SetParentTemplate(bt *types.BlockTemplate) {
reply := make(chan setParentTemplateResponse)
b.msgChan <- setParentTemplateMsg{Template: bt, reply: reply}
<-reply
}
// deepCopyBlockTemplate returns a deeply copied block template that copies all
// data except a block's references to transactions, which are kept as pointers
// in the block. This is considered safe because transaction data is generally
// immutable, with the exception of coinbases which we alternatively also
// deep copy.
func deepCopyBlockTemplate(blockTemplate *types.BlockTemplate) *types.BlockTemplate {
if blockTemplate == nil {
return nil
}
// Deep copy the header, which we hash on.
headerCopy := blockTemplate.Block.Header
// Copy transactions pointers. Duplicate the coinbase
// transaction, because it might update it by modifying
// the extra nonce.
transactionsCopy := make([]*types.Transaction, len(blockTemplate.Block.Transactions))
coinbaseCopy :=
types.NewTxDeep(blockTemplate.Block.Transactions[0])
for i, mtx := range blockTemplate.Block.Transactions {
if i == 0 {
transactionsCopy[i] = coinbaseCopy.Transaction()
} else {
transactionsCopy[i] = mtx
}
}
msgBlockCopy := &types.Block{
Header: headerCopy,
Transactions: transactionsCopy,
}
fees := make([]int64, len(blockTemplate.Fees))
copy(fees, blockTemplate.Fees)
sigOps := make([]int64, len(blockTemplate.SigOpCounts))
copy(sigOps, blockTemplate.SigOpCounts)
return &types.BlockTemplate{
Block: msgBlockCopy,
Fees: fees,
SigOpCounts: sigOps,
Height: blockTemplate.Height,
Blues: blockTemplate.Blues,
ValidPayAddress: blockTemplate.ValidPayAddress,
}
}