/
processor.go
337 lines (298 loc) · 11 KB
/
processor.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
package consensus
import (
"bytes"
"context"
"fmt"
"time"
"github.com/filecoin-project/venus/pkg/config"
"github.com/filecoin-project/venus/pkg/constants"
"github.com/filecoin-project/venus/pkg/fvm"
"github.com/filecoin-project/venus/pkg/vm/vmcontext"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/reward"
"github.com/filecoin-project/go-address"
amt4 "github.com/filecoin-project/go-amt-ipld/v4"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/specs-actors/v7/actors/builtin"
"github.com/filecoin-project/venus/pkg/chain"
"github.com/filecoin-project/venus/pkg/vm"
"github.com/filecoin-project/venus/venus-shared/actors/builtin/cron"
"github.com/filecoin-project/venus/venus-shared/types"
"github.com/ipfs/go-cid"
cbor "github.com/ipfs/go-ipld-cbor"
logging "github.com/ipfs/go-log/v2"
cbg "github.com/whyrusleeping/cbor-gen"
)
var processLog = logging.Logger("process block")
// ApplicationResult contains the result of successfully applying one message.
// ExecutionError might be set and the message can still be applied successfully.
// See ApplyMessage() for details.
type ApplicationResult struct {
Receipt *types.MessageReceipt
ExecutionError error
}
// ApplyMessageResult is the result of applying a single message.
type ApplyMessageResult struct {
ApplicationResult // Application-level result, if error is nil.
Failure error // Failure to apply the message
FailureIsPermanent bool // Whether failure is permanent, has no chance of succeeding later.
}
// DefaultProcessor handles all block processing.
type DefaultProcessor struct {
actors vm.ActorCodeLoader
syscalls vm.SyscallsImpl
circulatingSupplyCalculator chain.ICirculatingSupplyCalcualtor
cs *chain.Store
netParamCfg *config.NetworkParamsConfig
}
var _ Processor = (*DefaultProcessor)(nil)
// NewDefaultProcessor creates a default processor from the given state tree and vms.
func NewDefaultProcessor(syscalls vm.SyscallsImpl,
circulatingSupplyCalculator chain.ICirculatingSupplyCalcualtor,
cs *chain.Store,
netParamCfg *config.NetworkParamsConfig) *DefaultProcessor {
return NewConfiguredProcessor(*vm.GetDefaultActors(), syscalls, circulatingSupplyCalculator, cs, netParamCfg)
}
// NewConfiguredProcessor creates a default processor with custom validation and rewards.
func NewConfiguredProcessor(actors vm.ActorCodeLoader,
syscalls vm.SyscallsImpl,
circulatingSupplyCalculator chain.ICirculatingSupplyCalcualtor,
cs *chain.Store,
netParamCfg *config.NetworkParamsConfig) *DefaultProcessor {
return &DefaultProcessor{
actors: actors,
syscalls: syscalls,
circulatingSupplyCalculator: circulatingSupplyCalculator,
cs: cs,
netParamCfg: netParamCfg,
}
}
func (p *DefaultProcessor) ApplyBlocks(ctx context.Context,
blocks []types.BlockMessagesInfo,
ts *types.TipSet,
pstate cid.Cid,
parentEpoch, epoch abi.ChainEpoch,
vmOpts vm.VmOption,
cb vm.ExecCallBack,
) (cid.Cid, []types.MessageReceipt, error) {
toProcessTipset := time.Now()
var (
receipts []types.MessageReceipt
err error
storingEvents = vmOpts.ReturnEvents
events [][]types.Event
)
makeVM := func(base cid.Cid, e abi.ChainEpoch, timestamp uint64) (vm.Interface, error) {
vmOpt := vm.VmOption{
CircSupplyCalculator: vmOpts.CircSupplyCalculator,
LookbackStateGetter: vmOpts.LookbackStateGetter,
NetworkVersion: vmOpts.Fork.GetNetworkVersion(ctx, e),
Rnd: vmOpts.Rnd,
BaseFee: vmOpts.BaseFee,
Fork: vmOpts.Fork,
ActorCodeLoader: vmOpts.ActorCodeLoader,
Epoch: e,
Timestamp: timestamp,
GasPriceSchedule: vmOpts.GasPriceSchedule,
PRoot: base,
Bsstore: vmOpts.Bsstore,
SysCallsImpl: vmOpts.SysCallsImpl,
TipSetGetter: vmOpts.TipSetGetter,
Tracing: vmOpts.Tracing,
ActorDebugging: vmOpts.ActorDebugging,
ReturnEvents: vmOpts.ReturnEvents,
ExecutionLane: vmcontext.ExecutionLanePriority,
}
return fvm.NewVM(ctx, vmOpt)
}
runCron := func(vmCron vm.Interface, epoch abi.ChainEpoch) error {
cronMsg := makeCronTickMessage(epoch)
ret, err := vmCron.ApplyImplicitMessage(ctx, cronMsg)
if err != nil {
return fmt.Errorf("running cron: %w", err)
}
if !ret.Receipt.ExitCode.IsSuccess() {
return fmt.Errorf("cron failed with exit code %d: %w", ret.Receipt.ExitCode, ret.ActorErr)
}
if cb != nil {
if err := cb(cronMsg.Cid(), cronMsg, ret); err != nil {
return fmt.Errorf("callback failed on cron message: %w", err)
}
}
return nil
}
// May get filled with the genesis block header if there are null rounds
// for which to backfill cron execution.
var genesis *types.BlockHeader
// There were null rounds in between the current epoch and the parent epoch.
for i := parentEpoch; i < epoch; i++ {
if i > parentEpoch {
if genesis == nil {
if genesis, err = p.cs.GetGenesisBlock(ctx); err != nil {
return cid.Undef, nil, fmt.Errorf("failed to get genesis when backfilling null rounds: %w", err)
}
}
timestamp := genesis.Timestamp + p.netParamCfg.BlockDelay*(uint64(i))
vmCron, err := makeVM(pstate, i, timestamp)
if err != nil {
return cid.Undef, nil, fmt.Errorf("making cron vm: %w", err)
}
// run cron for null rounds if any
if err = runCron(vmCron, i); err != nil {
return cid.Undef, nil, fmt.Errorf("running cron: %w", err)
}
pstate, err = vmCron.Flush(ctx)
if err != nil {
return cid.Undef, nil, fmt.Errorf("can not Flush vm State To db %vs", err)
}
}
// handle State forks
// XXX: The State tree
pstate, err = vmOpts.Fork.HandleStateForks(ctx, pstate, i, ts)
if err != nil {
return cid.Undef, nil, fmt.Errorf("hand fork error: %v", err)
}
processLog.Debugf("after fork root: %s\n", pstate)
}
vm, err := makeVM(pstate, epoch, vmOpts.Timestamp)
if err != nil {
return cid.Undef, nil, fmt.Errorf("making cron vm: %w", err)
}
processLog.Debugf("process tipset fork: %v\n", time.Since(toProcessTipset).Milliseconds())
// create message tracker
// Note: the same message could have been included by more than one miner
seenMsgs := make(map[cid.Cid]struct{})
// process messages on each block
for index, blkInfo := range blocks {
toProcessBlock := time.Now()
if blkInfo.Block.Miner.Protocol() != address.ID {
panic("precond failure: block miner address must be an IDAddress")
}
// initial miner penalty and gas rewards
// Note: certain msg execution failures can cause the miner To pay for the gas
minerPenaltyTotal := big.Zero()
minerGasRewardTotal := big.Zero()
// Process BLS messages From the block
for _, m := range append(blkInfo.BlsMessages, blkInfo.SecpkMessages...) {
// do not recompute already seen messages
mcid := m.VMMessage().Cid()
if _, found := seenMsgs[mcid]; found {
continue
}
// apply message
ret, err := vm.ApplyMessage(ctx, m)
if err != nil {
return cid.Undef, nil, fmt.Errorf("execute message error %s : %v", mcid, err)
}
// accumulate result
minerPenaltyTotal = big.Add(minerPenaltyTotal, ret.OutPuts.MinerPenalty)
minerGasRewardTotal = big.Add(minerGasRewardTotal, ret.OutPuts.MinerTip)
receipts = append(receipts, ret.Receipt)
if storingEvents {
// Appends nil when no events are returned to preserve positional alignment.
events = append(events, ret.Events)
}
if cb != nil {
if err := cb(m.Cid(), m.VMMessage(), ret); err != nil {
return cid.Undef, nil, err
}
}
// flag msg as seen
seenMsgs[mcid] = struct{}{}
}
// Pay block reward.
// Dragons: missing final protocol design on if/how To determine the nominal power
rewardMessage := makeBlockRewardMessage(blkInfo.Block.Miner, minerPenaltyTotal, minerGasRewardTotal, blkInfo.Block.ElectionProof.WinCount, epoch)
ret, err := vm.ApplyImplicitMessage(ctx, rewardMessage)
if err != nil {
return cid.Undef, nil, err
}
if ret.Receipt.ExitCode != 0 {
return cid.Undef, nil, fmt.Errorf("reward application message failed exit: %d, reason: %v", ret.Receipt.ExitCode, ret.ActorErr)
}
if cb != nil {
if err := cb(rewardMessage.Cid(), rewardMessage, ret); err != nil {
return cid.Undef, nil, fmt.Errorf("callback failed on reward message: %w", err)
}
}
processLog.Debugf("process block %v time %v", index, time.Since(toProcessBlock).Milliseconds())
}
// cron tick
toProcessCron := time.Now()
if err := runCron(vm, epoch); err != nil {
return cid.Cid{}, nil, err
}
processLog.Debugf("process cron: %v", time.Since(toProcessCron).Milliseconds())
// Slice will be empty if not storing events.
for i, evs := range events {
if len(evs) == 0 {
continue
}
switch root, err := storeEventsAMT(ctx, vmOpts.Bsstore, evs); {
case err != nil:
return cid.Undef, nil, fmt.Errorf("failed to store events amt: %w", err)
case i >= len(receipts):
return cid.Undef, nil, fmt.Errorf("assertion failed: receipt and events array lengths inconsistent")
case receipts[i].EventsRoot == nil:
return cid.Undef, nil, fmt.Errorf("assertion failed: VM returned events with no events root")
case root != *receipts[i].EventsRoot:
return cid.Undef, nil, fmt.Errorf("assertion failed: returned events AMT root does not match derived")
}
}
root, err := vm.Flush(ctx)
if err != nil {
return cid.Undef, nil, err
}
// copy to db
return root, receipts, nil
}
func makeCronTickMessage(epoch abi.ChainEpoch) *types.Message {
return &types.Message{
To: cron.Address,
From: builtin.SystemActorAddr,
Nonce: uint64(epoch),
Value: types.NewInt(0),
GasFeeCap: types.NewInt(0),
GasPremium: types.NewInt(0),
GasLimit: constants.BlockGasLimit * 10000, // Make super sure this is never too little
Method: cron.Methods.EpochTick,
Params: nil,
}
}
func makeBlockRewardMessage(blockMiner address.Address,
penalty abi.TokenAmount,
gasReward abi.TokenAmount,
winCount int64,
epoch abi.ChainEpoch,
) *types.Message {
params := &reward.AwardBlockRewardParams{
Miner: blockMiner,
Penalty: penalty,
GasReward: gasReward,
WinCount: winCount,
}
buf := new(bytes.Buffer)
err := params.MarshalCBOR(buf)
if err != nil {
panic(fmt.Errorf("failed To encode built-in block reward. %s", err))
}
return &types.Message{
From: builtin.SystemActorAddr,
To: reward.Address,
Nonce: uint64(epoch),
Value: types.NewInt(0),
GasFeeCap: types.NewInt(0),
GasPremium: types.NewInt(0),
GasLimit: 1 << 30,
Method: reward.Methods.AwardBlockReward,
Params: buf.Bytes(),
}
}
func storeEventsAMT(ctx context.Context, bs cbor.IpldBlockstore, events []types.Event) (cid.Cid, error) {
cst := cbor.NewCborStore(bs)
objs := make([]cbg.CBORMarshaler, len(events))
for i := 0; i < len(events); i++ {
objs[i] = &events[i]
}
return amt4.FromArray(ctx, cst, objs, amt4.UseTreeBitWidth(types.EventAMTBitwidth))
}