forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blockfactory.go
247 lines (207 loc) · 5.98 KB
/
blockfactory.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
/**
* @file
* @copyright defined in aergo/LICENSE.txt
*/
package dpos
import (
"runtime"
"time"
"github.com/aergoio/aergo-lib/log"
bc "github.com/aergoio/aergo/chain"
"github.com/aergoio/aergo/consensus/chain"
"github.com/aergoio/aergo/contract"
"github.com/aergoio/aergo/internal/enc"
"github.com/aergoio/aergo/p2p"
"github.com/aergoio/aergo/pkg/component"
"github.com/aergoio/aergo/state"
"github.com/aergoio/aergo/types"
"github.com/davecgh/go-spew/spew"
"github.com/libp2p/go-libp2p-crypto"
)
const (
slotQueueMax = 100
)
type txExec struct {
execTx bc.TxExecFn
}
func newTxExec(blockNo types.BlockNo, ts int64) chain.TxOp {
// Block hash not determined yet
return &txExec{
execTx: bc.NewTxExecutor(blockNo, ts, contract.BlockFactory),
}
}
func (te *txExec) Apply(bState *state.BlockState, tx *types.Tx) error {
err := te.execTx(bState, tx)
return err
}
// BlockFactory is the main data structure for DPoS block factory.
type BlockFactory struct {
*component.ComponentHub
jobQueue chan interface{}
workerQueue chan *bpInfo
bpTimeoutC chan interface{}
quit <-chan interface{}
maxBlockBodySize uint32
ID string
privKey crypto.PrivKey
txOp chain.TxOp
sdb *state.ChainStateDB
}
// NewBlockFactory returns a new BlockFactory
func NewBlockFactory(hub *component.ComponentHub, quitC <-chan interface{}) *BlockFactory {
bf := &BlockFactory{
ComponentHub: hub,
jobQueue: make(chan interface{}, slotQueueMax),
workerQueue: make(chan *bpInfo),
bpTimeoutC: make(chan interface{}, 1),
maxBlockBodySize: chain.MaxBlockBodySize(),
quit: quitC,
ID: p2p.NodeSID(),
privKey: p2p.NodePrivKey(),
}
bf.txOp = chain.NewCompTxOp(
// timeout check
chain.TxOpFn(func(bState *state.BlockState, txIn *types.Tx) error {
return bf.checkBpTimeout()
}),
)
return bf
}
func (bf *BlockFactory) setStateDB(sdb *state.ChainStateDB) {
bf.sdb = sdb.Clone()
}
// Start run a DPoS block factory service.
func (bf *BlockFactory) Start() {
go func() {
go bf.worker()
go bf.controller()
}()
}
// JobQueue returns the queue for block production triggering.
func (bf *BlockFactory) JobQueue() chan<- interface{} {
return bf.jobQueue
}
func (bf *BlockFactory) controller() {
defer shutdownMsg("block factory controller")
beginBlock := func(bpi *bpInfo) error {
// This is only for draining an unconsumed message, which means
// the previous block is generated within timeout. This code
// is needed since an empty block will be generated without it.
if err := bf.checkBpTimeout(); err == chain.ErrQuit {
return err
}
timeLeft := bpi.slot.RemainingTimeMS()
if timeLeft <= 0 {
return chain.ErrTimeout{Kind: "slot", Timeout: timeLeft}
}
select {
case bf.workerQueue <- bpi:
default:
logger.Error().Msgf(
"skip block production for the slot %v (best block: %v) due to a pending job",
spew.Sdump(bpi.slot), bpi.bestBlock.ID())
}
return nil
}
notifyBpTimeout := func(bpi *bpInfo) {
timeout := bpi.slot.GetBpTimeout()
time.Sleep(time.Duration(timeout) * time.Millisecond)
// TODO: skip when the triggered block has already been genearted!
bf.bpTimeoutC <- struct{}{}
logger.Debug().Int64("timeout", timeout).Msg("block production timeout signaled")
}
for {
select {
case info := <-bf.jobQueue:
bpi := info.(*bpInfo)
logger.Debug().Msgf("received bpInfo: %v %v",
log.DoLazyEval(func() string { return bpi.bestBlock.ID() }),
log.DoLazyEval(func() string { return spew.Sdump(bpi.slot) }))
err := beginBlock(bpi)
if err == chain.ErrQuit {
return
} else if err != nil {
logger.Debug().Err(err).Msg("skip block production")
continue
}
notifyBpTimeout(bpi)
case <-bf.quit:
return
}
}
}
func (bf *BlockFactory) worker() {
defer shutdownMsg("the block factory worker")
runtime.LockOSThread()
lpbNo := bsLoader.lpbNo()
logger.Info().Uint64("lastly produced block", lpbNo).
Msg("start the block factory worker")
for {
select {
case bpi := <-bf.workerQueue:
retry:
block, blockState, err := bf.generateBlock(bpi, lpbNo)
if err == chain.ErrQuit {
return
}
if err == chain.ErrBestBlock {
time.Sleep(tickDuration())
// This means the best block is beging changed by the chain
// service. If the chain service quickly executes the
// block, there may be still some remaining time to produce
// block in the current slot, though. Thus retry block
// production.
logger.Info().Err(err).Msg("retry block production")
bpi.updateBestBLock()
goto retry
} else if err != nil {
logger.Info().Err(err).Msg("failed to produce block")
continue
}
err = chain.ConnectBlock(bf, block, blockState)
if err == nil {
lpbNo = block.BlockNo()
} else {
logger.Error().Msg(err.Error())
}
case <-bf.quit:
return
}
}
}
func (bf *BlockFactory) generateBlock(bpi *bpInfo, lpbNo types.BlockNo) (*types.Block, *state.BlockState, error) {
ts := bpi.slot.UnixNano()
blockState := bf.sdb.NewBlockState(bpi.bestBlock.GetHeader().GetBlocksRootHash())
txOp := chain.NewCompTxOp(
bf.txOp,
newTxExec(bpi.bestBlock.GetHeader().GetBlockNo()+1, ts),
)
block, err := chain.GenerateBlock(bf, bpi.bestBlock, blockState, txOp, ts)
if err != nil {
return nil, nil, err
}
block.SetConfirms(block.BlockNo() - lpbNo)
if err := block.Sign(bf.privKey); err != nil {
return nil, nil, err
}
logger.Info().
Str("BP", bf.ID).Str("id", block.ID()).
Str("sroot", enc.ToString(block.GetHeader().GetBlocksRootHash())).
Uint64("no", block.BlockNo()).Uint64("confirms", block.Confirms()).
Uint64("lpb", lpbNo).
Msg("block produced")
return block, blockState, nil
}
func (bf *BlockFactory) checkBpTimeout() error {
select {
case <-bf.bpTimeoutC:
return chain.ErrTimeout{Kind: "block"}
case <-bf.quit:
return chain.ErrQuit
default:
return nil
}
}
func shutdownMsg(m string) {
logger.Info().Msgf("shutdown initiated. stop the %s", m)
}