/
c_block_callbacks.go
365 lines (332 loc) · 11.5 KB
/
c_block_callbacks.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
package gossip
import (
"sort"
"sync"
"sync/atomic"
"time"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/log"
"github.com/skyhighblockchain/push-base/hash"
"github.com/skyhighblockchain/push-base/inter/dag"
"github.com/skyhighblockchain/push-base/inter/idx"
"github.com/skyhighblockchain/push-base/inter/pos"
"github.com/skyhighblockchain/push-base/push"
"github.com/skyhighblockchain/push-base/utils/workers"
"github.com/skyhighblockchain/skyhigh/evmcore"
"github.com/skyhighblockchain/skyhigh/gossip/blockproc"
"github.com/skyhighblockchain/skyhigh/gossip/blockproc/verwatcher"
"github.com/skyhighblockchain/skyhigh/gossip/emitter"
"github.com/skyhighblockchain/skyhigh/gossip/evmstore"
"github.com/skyhighblockchain/skyhigh/gossip/sfcapi"
"github.com/skyhighblockchain/skyhigh/inter"
"github.com/skyhighblockchain/skyhigh/skyhigh"
)
type ExtendedTxPosition struct {
evmstore.TxPosition
EventCreator idx.ValidatorID
}
// GetConsensusCallbacks returns single (for Service) callback instance.
func (s *Service) GetConsensusCallbacks() push.ConsensusCallbacks {
return push.ConsensusCallbacks{
BeginBlock: consensusCallbackBeginBlockFn(
s.blockProcTasks,
&s.blockProcWg,
&s.blockBusyFlag,
s.store,
s.blockProcModules,
s.config.TxIndex,
&s.feed,
s.emitter,
s.verWatcher,
nil,
),
}
}
// consensusCallbackBeginBlockFn takes only necessaries for block processing and
// makes push.BeginBlockFn.
// Note that onBlockEnd would be run async.
func consensusCallbackBeginBlockFn(
parallelTasks *workers.Workers,
wg *sync.WaitGroup,
blockBusyFlag *uint32,
store *Store,
blockProc BlockProc,
txIndex bool,
feed *ServiceFeed,
emitter *emitter.Emitter,
verWatcher *verwatcher.VerWarcher,
onBlockEnd func(block *inter.Block, preInternalReceipts, internalReceipts, externalReceipts types.Receipts),
) push.BeginBlockFn {
return func(cBlock *push.Block) push.BlockCallbacks {
wg.Wait()
start := time.Now()
// Note: take copies to avoid race conditions with API calls
bs := store.GetBlockState().Copy()
es := store.GetEpochState().Copy()
// merge cheaters to ensure that every cheater will get punished even if only previous (not current) Atropos observed a doublesign
// this feature is needed because blocks may be skipped even if cheaters list isn't empty
// otherwise cheaters would get punished after a first block where cheaters were observed
bs.EpochCheaters = mergeCheaters(bs.EpochCheaters, cBlock.Cheaters)
// Get stateDB
statedb, err := store.evm.StateDB(bs.FinalizedStateRoot)
if err != nil {
log.Crit("Failed to open StateDB", "err", err)
}
eventProcessor := blockProc.EventsModule.Start(bs, es)
atroposTime := bs.LastBlock.Time + 1
atroposDegenerate := true
confirmedEvents := make(hash.OrderedEvents, 0, 3*es.Validators.Len())
return push.BlockCallbacks{
ApplyEvent: func(_e dag.Event) {
e := _e.(inter.EventI)
if cBlock.Atropos == e.ID() {
atroposTime = e.MedianTime()
atroposDegenerate = false
}
if !e.NoTxs() {
// non-empty events only
confirmedEvents = append(confirmedEvents, e.ID())
}
eventProcessor.ProcessConfirmedEvent(e)
if emitter != nil {
emitter.OnEventConfirmed(e)
}
},
EndBlock: func() (newValidators *pos.Validators) {
if atroposTime <= bs.LastBlock.Time {
atroposTime = bs.LastBlock.Time + 1
}
blockCtx := blockproc.BlockCtx{
Idx: bs.LastBlock.Idx + 1,
Time: atroposTime,
Atropos: cBlock.Atropos,
}
// Note:
// it's possible that a previous Atropos observes current Atropos (1)
// (even stronger statement is true - it's possible that current Atropos is equal to a previous Atropos).
// (1) is true when and only when ApplyEvent wasn't called.
// In other words, we should assume that every non-cheater root may be elected as an Atropos in any order,
// even if typically every previous Atropos happened-before current Atropos
// We have to skip block in case (1) to ensure that every block ID is unique.
// If Atropos ID wasn't used as a block ID, it wouldn't be required.
skipBlock := atroposDegenerate
// Check if empty block should be pruned
emptyBlock := confirmedEvents.Len() == 0 && cBlock.Cheaters.Len() == 0
skipBlock = skipBlock || (emptyBlock && blockCtx.Time < bs.LastBlock.Time+es.Rules.Blocks.MaxEmptyBlockSkipPeriod)
// Finalize the progress of eventProcessor
bs = eventProcessor.Finalize(blockCtx, skipBlock) // TODO: refactor to not mutate the bs, it is unclear
if skipBlock {
// save the latest block state even if block is skipped
store.SetBlockEpochState(bs, es)
log.Debug("Frame is skipped", "atropos", cBlock.Atropos.String())
return nil
}
sealer := blockProc.SealerModule.Start(blockCtx, bs, es)
sealing := sealer.EpochSealing()
txListener := blockProc.TxListenerModule.Start(blockCtx, bs, es, statedb)
evmStateReader := &EvmStateReader{
ServiceFeed: feed,
store: store,
}
onNewLogAll := func(l *types.Log) {
txListener.OnNewLog(l)
if verWatcher != nil {
verWatcher.OnNewLog(l)
}
sfcapi.OnNewLog(store.sfcapi, l)
}
evmProcessor := blockProc.EVMModule.Start(blockCtx, statedb, evmStateReader, onNewLogAll, es.Rules)
// Execute pre-internal transactions
preInternalTxs := blockProc.PreTxTransactor.PopInternalTxs(blockCtx, bs, es, sealing, statedb)
preInternalReceipts := evmProcessor.Execute(preInternalTxs, true)
bs = txListener.Finalize()
for _, r := range preInternalReceipts {
if r.Status == 0 {
log.Warn("Pre-internal transaction reverted", "txid", r.TxHash.String())
}
}
// Seal epoch if requested
if sealing {
sealer.Update(bs, es)
bs, es = sealer.SealEpoch() // TODO: refactor to not mutate the bs, it is unclear
store.SetBlockEpochState(bs, es)
newValidators = es.Validators
txListener.Update(bs, es)
}
// At this point, newValidators may be returned and the rest of the code may be executed in a parallel thread
blockFn := func() {
// Execute post-internal transactions
internalTxs := blockProc.PostTxTransactor.PopInternalTxs(blockCtx, bs, es, sealing, statedb)
internalReceipts := evmProcessor.Execute(internalTxs, true)
for _, r := range internalReceipts {
if r.Status == 0 {
log.Warn("Internal transaction reverted", "txid", r.TxHash.String())
}
}
// sort events by Lamport time
sort.Sort(confirmedEvents)
// new block
var block = &inter.Block{
Time: blockCtx.Time,
Atropos: cBlock.Atropos,
Events: hash.Events(confirmedEvents),
}
for _, tx := range append(preInternalTxs, internalTxs...) {
block.InternalTxs = append(block.InternalTxs, tx.Hash())
}
block, blockEvents := spillBlockEvents(store, block, es.Rules)
txs := make(types.Transactions, 0, blockEvents.Len()*10)
for _, e := range blockEvents {
txs = append(txs, e.Txs()...)
}
externalReceipts := evmProcessor.Execute(txs, false)
evmBlock, skippedTxs, allReceipts := evmProcessor.Finalize()
block.SkippedTxs = skippedTxs
block.Root = hash.Hash(evmBlock.Root)
block.GasUsed = evmBlock.GasUsed
// memorize event position of each tx
txPositions := make(map[common.Hash]ExtendedTxPosition)
for _, e := range blockEvents {
for i, tx := range e.Txs() {
// If tx was met in multiple events, then assign to first ordered event
if _, ok := txPositions[tx.Hash()]; ok {
continue
}
txPositions[tx.Hash()] = ExtendedTxPosition{
TxPosition: evmstore.TxPosition{
Event: e.ID(),
EventOffset: uint32(i),
},
EventCreator: e.Creator(),
}
}
}
// memorize block position of each tx
for i, tx := range evmBlock.Transactions {
// not skipped txs only
position := txPositions[tx.Hash()]
position.Block = blockCtx.Idx
position.BlockOffset = uint32(i)
txPositions[tx.Hash()] = position
}
// call OnNewReceipt
for i, r := range allReceipts {
creator := txPositions[r.TxHash].EventCreator
if creator != 0 && es.Validators.Get(creator) == 0 {
creator = 0
}
txListener.OnNewReceipt(evmBlock.Transactions[i], r, creator)
}
bs = txListener.Finalize() // TODO: refactor to not mutate the bs
bs.FinalizedStateRoot = block.Root
// At this point, block state is finalized
// Build index for not skipped txs
if txIndex {
for _, tx := range evmBlock.Transactions {
// not skipped txs only
store.evm.SetTxPosition(tx.Hash(), txPositions[tx.Hash()].TxPosition)
}
// Index receipts
if allReceipts.Len() != 0 {
store.evm.SetReceipts(blockCtx.Idx, allReceipts)
for _, r := range allReceipts {
store.evm.IndexLogs(r.Logs...)
}
}
}
for _, tx := range append(preInternalTxs, internalTxs...) {
store.evm.SetTx(tx.Hash(), tx)
}
store.SetBlock(blockCtx.Idx, block)
store.SetBlockIndex(block.Atropos, blockCtx.Idx)
bs.LastBlock = blockCtx
store.SetBlockEpochState(bs, es)
store.EvmStore().SetCachedEvmBlock(blockCtx.Idx, evmBlock)
// Notify about new block and txs
if feed != nil {
feed.newBlock.Send(evmcore.ChainHeadNotify{Block: evmBlock})
feed.newTxs.Send(core.NewTxsEvent{Txs: evmBlock.Transactions})
var logs []*types.Log
for _, r := range allReceipts {
for _, l := range r.Logs {
logs = append(logs, l)
}
}
feed.newLogs.Send(logs)
}
if onBlockEnd != nil {
onBlockEnd(block, preInternalReceipts, internalReceipts, externalReceipts)
}
store.commitEVM()
log.Info("New block", "index", blockCtx.Idx, "id", block.Atropos, "gas_used",
evmBlock.GasUsed, "skipped_txs", len(block.SkippedTxs), "txs", len(evmBlock.Transactions), "t", common.PrettyDuration(time.Since(start)))
}
if confirmedEvents.Len() != 0 {
atomic.StoreUint32(blockBusyFlag, 1)
wg.Add(1)
err := parallelTasks.Enqueue(func() {
defer atomic.StoreUint32(blockBusyFlag, 0)
defer wg.Done()
blockFn()
})
if err != nil {
panic(err)
}
} else {
blockFn()
}
return newValidators
},
}
}
}
// spillBlockEvents excludes first events which exceed MaxBlockGas
func spillBlockEvents(store *Store, block *inter.Block, network skyhigh.Rules) (*inter.Block, inter.EventPayloads) {
fullEvents := make(inter.EventPayloads, len(block.Events))
if len(block.Events) == 0 {
return block, fullEvents
}
gasPowerUsedSum := uint64(0)
// iterate in reversed order
for i := len(block.Events) - 1; ; i-- {
id := block.Events[i]
e := store.GetEventPayload(id)
if e == nil {
log.Crit("Block event not found", "event", id.String())
}
fullEvents[i] = e
gasPowerUsedSum += e.GasPowerUsed()
// stop if limit is exceeded, erase [:i] events
if gasPowerUsedSum > network.Blocks.MaxBlockGas {
// spill
block.Events = block.Events[i+1:]
fullEvents = fullEvents[i+1:]
break
}
if i == 0 {
break
}
}
return block, fullEvents
}
func mergeCheaters(a, b push.Cheaters) push.Cheaters {
if len(b) == 0 {
return a
}
if len(a) == 0 {
return b
}
aSet := a.Set()
merged := make(push.Cheaters, 0, len(b)+len(a))
for _, v := range a {
merged = append(merged, v)
}
for _, v := range b {
if _, ok := aSet[v]; !ok {
merged = append(merged, v)
}
}
return merged
}