-
Notifications
You must be signed in to change notification settings - Fork 669
/
apricot_builder.go
113 lines (97 loc) · 3.09 KB
/
apricot_builder.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
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package builder
import (
"fmt"
"time"
"go.uber.org/zap"
"github.com/ava-labs/avalanchego/ids"
"github.com/ava-labs/avalanchego/vms/platformvm/blocks"
"github.com/ava-labs/avalanchego/vms/platformvm/state"
"github.com/ava-labs/avalanchego/vms/platformvm/txs"
"github.com/ava-labs/avalanchego/vms/platformvm/txs/executor"
)
func buildApricotBlock(
builder *builder,
parentID ids.ID,
height uint64,
timestamp time.Time,
shouldAdvanceTime bool,
parentState state.Chain,
) (blocks.Block, error) {
// try including as many standard txs as possible. No need to advance chain time
if builder.Mempool.HasApricotDecisionTxs() {
return blocks.NewApricotStandardBlock(
parentID,
height,
builder.Mempool.PeekApricotDecisionTxs(targetBlockSize),
)
}
// try rewarding stakers whose staking period ends at current chain time.
stakerTxID, shouldReward, err := builder.getNextStakerToReward(parentState.GetTimestamp(), parentState)
if err != nil {
return nil, fmt.Errorf("could not find next staker to reward: %w", err)
}
if shouldReward {
rewardValidatorTx, err := builder.txBuilder.NewRewardValidatorTx(stakerTxID)
if err != nil {
return nil, fmt.Errorf("could not build tx to reward staker: %w", err)
}
return blocks.NewApricotProposalBlock(
parentID,
height,
rewardValidatorTx,
)
}
// try advancing chain time
if shouldAdvanceTime {
advanceTimeTx, err := builder.txBuilder.NewAdvanceTimeTx(timestamp)
if err != nil {
return nil, fmt.Errorf("could not build tx to reward staker: %w", err)
}
return blocks.NewApricotProposalBlock(
parentID,
height,
advanceTimeTx,
)
}
// Clean out transactions with an invalid timestamp.
builder.dropExpiredStakerTxs(timestamp)
// Check the mempool
if !builder.Mempool.HasStakerTx() {
builder.txExecutorBackend.Ctx.Log.Debug("no pending txs to issue into a block")
return nil, errNoPendingBlocks
}
tx, err := nextApricotProposalTx(builder, timestamp, parentState)
if err != nil {
builder.txExecutorBackend.Ctx.Log.Error(
"failed to get the next proposal tx",
zap.Error(err),
)
return nil, err
}
return blocks.NewApricotProposalBlock(
parentID,
height,
tx,
)
}
// Try to get/make a proposal tx to put into a block.
// Any returned error is unexpected.
func nextApricotProposalTx(builder *builder, timestamp time.Time, parentState state.Chain) (*txs.Tx, error) {
tx := builder.Mempool.PeekStakerTx()
startTime := tx.Unsigned.(txs.Staker).StartTime()
// Check whether this staker starts within at most [MaxFutureStartTime].
// If it does, issue the staking tx.
// If it doesn't, issue an advance time tx.
maxChainStartTime := parentState.GetTimestamp().Add(executor.MaxFutureStartTime)
if !startTime.After(maxChainStartTime) {
return tx, nil
}
// The chain timestamp is too far in the past. Advance it.
advanceTimeTx, err := builder.txBuilder.NewAdvanceTimeTx(timestamp)
if err != nil {
return nil, fmt.Errorf("could not build tx to advance time: %w", err)
}
return advanceTimeTx, nil
}