-
Notifications
You must be signed in to change notification settings - Fork 129
/
Copy pathbuild.go
323 lines (265 loc) · 8.27 KB
/
build.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
// Copyright 2021 ChainSafe Systems (ON)
// SPDX-License-Identifier: LGPL-3.0-only
package babe
import (
"bytes"
"errors"
"fmt"
"time"
"github.com/ChainSafe/gossamer/dot/state"
"github.com/ChainSafe/gossamer/dot/types"
"github.com/ChainSafe/gossamer/lib/babe/inherents"
"github.com/ChainSafe/gossamer/lib/common"
"github.com/ChainSafe/gossamer/lib/crypto/sr25519"
"github.com/ChainSafe/gossamer/lib/transaction"
"github.com/ChainSafe/gossamer/pkg/scale"
ethmetrics "github.com/ethereum/go-ethereum/metrics"
)
const (
buildBlockTimer = "gossamer/proposer/block/constructed"
buildBlockErrors = "gossamer/proposer/block/constructed/errors"
)
// construct a block for this slot with the given parent
func (b *Service) buildBlock(parent *types.Header, slot Slot, rt Runtime,
authorityIndex uint32, preRuntimeDigest *types.PreRuntimeDigest) (*types.Block, error) {
builder := NewBlockBuilder(
b.keypair,
b.transactionState,
b.blockState,
authorityIndex,
preRuntimeDigest,
)
// is necessary to enable ethmetrics to be possible register values
ethmetrics.Enable()
start := time.Now()
block, err := builder.buildBlock(parent, slot, rt)
if err != nil {
builderErrors := ethmetrics.GetOrRegisterCounter(buildBlockErrors, nil)
builderErrors.Inc(1)
return nil, err
}
timerMetrics := ethmetrics.GetOrRegisterTimer(buildBlockTimer, nil)
timerMetrics.Update(time.Since(start))
return block, nil
}
// BlockBuilder builds blocks.
type BlockBuilder struct {
keypair *sr25519.Keypair
transactionState TransactionState
blockState state.BlockState
currentAuthorityIndex uint32
preRuntimeDigest *types.PreRuntimeDigest
}
// NewBlockBuilder creates a new block builder.
func NewBlockBuilder(
kp *sr25519.Keypair,
ts TransactionState,
bs state.BlockState,
authidx uint32,
preRuntimeDigest *types.PreRuntimeDigest,
) *BlockBuilder {
return &BlockBuilder{
keypair: kp,
transactionState: ts,
blockState: bs,
currentAuthorityIndex: authidx,
preRuntimeDigest: preRuntimeDigest,
}
}
func (b *BlockBuilder) buildBlock(parent *types.Header, slot Slot, rt Runtime) (*types.Block, error) {
logger.Tracef("build block with parent %s and slot: %s", parent, slot)
// create new block header
number := parent.Number + 1
digest := types.NewDigest()
err := digest.Add(*b.preRuntimeDigest)
if err != nil {
return nil, err
}
header := types.NewHeader(parent.Hash(), common.Hash{}, common.Hash{}, number, digest)
// initialise block header
err = rt.InitializeBlock(header)
if err != nil {
return nil, err
}
logger.Trace("initialised block")
// add block inherents
inherents, err := buildBlockInherents(slot, rt, parent)
if err != nil {
return nil, fmt.Errorf("cannot build inherents: %s", err)
}
logger.Tracef("built block encoded inherents: %v", inherents)
// add block extrinsics
included := b.buildBlockExtrinsics(slot, rt)
logger.Trace("built block extrinsics")
// finalise block
header, err = rt.FinalizeBlock()
if err != nil {
b.addToQueue(included)
return nil, fmt.Errorf("cannot finalise block: %s", err)
}
logger.Trace("finalised block")
// create seal and add to digest
seal, err := b.buildBlockSeal(header)
if err != nil {
return nil, err
}
err = header.Digest.Add(*seal)
if err != nil {
return nil, err
}
logger.Trace("built block seal")
body, err := extrinsicsToBody(inherents, included)
if err != nil {
return nil, err
}
block := &types.Block{
Header: *header,
Body: body,
}
return block, nil
}
// buildBlockSeal creates the seal for the block header.
// the seal consists of the ConsensusEngineID and a signature of the encoded block header.
func (b *BlockBuilder) buildBlockSeal(header *types.Header) (*types.SealDigest, error) {
encHeader, err := scale.Marshal(*header)
if err != nil {
return nil, err
}
hash, err := common.Blake2bHash(encHeader)
if err != nil {
return nil, err
}
sig, err := b.keypair.Sign(hash[:])
if err != nil {
return nil, err
}
return &types.SealDigest{
ConsensusEngineID: types.BabeEngineID,
Data: sig,
}, nil
}
// buildBlockExtrinsics applies extrinsics to the block. it returns an array of included extrinsics.
// for each extrinsic in queue, add it to the block, until the slot ends or the block is full.
// if any extrinsic fails, it returns an empty array and an error.
func (b *BlockBuilder) buildBlockExtrinsics(slot Slot, rt ExtrinsicHandler) []*transaction.ValidTransaction {
var included []*transaction.ValidTransaction
slotEnd := slot.start.Add(slot.duration * 2 / 3) // reserve last 1/3 of slot for block finalisation
timeout := slotEnd.Sub(slot.start) // timeout relative to the slot start
slotTimer := time.NewTimer(timeout)
for {
txn := b.transactionState.PopWithTimer(slotTimer.C)
slotTimerExpired := txn == nil
if slotTimerExpired {
break
}
extrinsic := txn.Extrinsic
logger.Tracef("build block, applying extrinsic %s", extrinsic)
ret, err := rt.ApplyExtrinsic(extrinsic)
if err != nil {
logger.Warnf("determining apply extrinsic call error: %s", err)
continue
}
err = determineErr(ret)
if err != nil {
logger.Warnf("error when applying extrinsic %s: %s", extrinsic, err)
// Failure of the module call dispatching doesn't invalidate the extrinsic.
// It is included in the block.
if _, ok := err.(*DispatchOutcomeError); !ok {
continue
}
// don't drop transactions that may be valid in a later block ie.
// run out of gas for this block or have a nonce that may be valid in a later block
var e *TransactionValidityError
if !errors.As(err, &e) {
continue
}
if errors.Is(e.msg, errExhaustsResources) || errors.Is(e.msg, errInvalidTransaction) {
hash, err := b.transactionState.Push(txn)
if err != nil {
logger.Debugf("failed to re-add transaction with hash %s to queue: %s", hash, err)
}
}
}
logger.Debugf("build block applied extrinsic %s", extrinsic)
included = append(included, txn)
}
return included
}
func buildBlockInherents(slot Slot, rt ExtrinsicHandler, parent *types.Header) ([][]byte, error) {
// Setup inherents: add timstap0
idata := types.NewInherentData()
err := idata.SetInherent(types.Timstap0, uint64(slot.start.UnixMilli()))
if err != nil {
return nil, err
}
// add babeslot
err = idata.SetInherent(types.Babeslot, slot.number)
if err != nil {
return nil, err
}
parachainInherent := inherents.ParachainInherentData{
ParentHeader: *parent,
}
// add parachn0 and newheads
// for now we can use "empty" values, as we require parachain-specific
// logic to actually provide the data.
if err = idata.SetInherent(types.Parachn0, parachainInherent); err != nil {
return nil, fmt.Errorf("setting inherent %q: %w", types.Parachn0, err)
}
if err = idata.SetInherent(types.Newheads, []byte{0}); err != nil {
return nil, fmt.Errorf("setting inherent %q: %w", types.Newheads, err)
}
ienc, err := idata.Encode()
if err != nil {
return nil, err
}
// Call BlockBuilder_inherent_extrinsics which returns the inherents as extrinsics
inherentExts, err := rt.InherentExtrinsics(ienc)
if err != nil {
return nil, err
}
// decode inherent extrinsics
var exts [][]byte
err = scale.Unmarshal(inherentExts, &exts)
if err != nil {
return nil, err
}
// apply each inherent extrinsic
for _, ext := range exts {
in, err := scale.Marshal(ext)
if err != nil {
return nil, err
}
ret, err := rt.ApplyExtrinsic(in)
if err != nil {
return nil, err
}
if !bytes.Equal(ret, []byte{0, 0}) {
errTxt := determineErr(ret)
return nil, fmt.Errorf("error applying inherent: %s", errTxt)
}
}
return exts, nil
}
func (b *BlockBuilder) addToQueue(txs []*transaction.ValidTransaction) {
for _, t := range txs {
hash, err := b.transactionState.Push(t)
if err != nil {
logger.Tracef("Failed to add transaction to queue: %s", err)
} else {
logger.Tracef("Added transaction with hash %s to queue", hash)
}
}
}
func extrinsicsToBody(inherents [][]byte, txs []*transaction.ValidTransaction) (types.Body, error) {
extrinsics := types.BytesArrayToExtrinsics(inherents)
for _, tx := range txs {
var decExt []byte
err := scale.Unmarshal(tx.Extrinsic, &decExt)
if err != nil {
return nil, err
}
extrinsics = append(extrinsics, decExt)
}
return types.Body(extrinsics), nil
}