-
Notifications
You must be signed in to change notification settings - Fork 165
/
epoching.go
176 lines (157 loc) · 5.35 KB
/
epoching.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
package types
import (
"time"
errorsmod "cosmossdk.io/errors"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/cometbft/cometbft/crypto/tmhash"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// NewEpoch constructs a new Epoch object
// The relationship between block and epoch is as follows, assuming epoch interval of 5:
// 0 | 1 2 3 4 5 | 6 7 8 9 10 |
// 0 | 1 | 2 |
func NewEpoch(epochNumber uint64, epochInterval uint64, firstBlockHeight uint64, lastBlockHeader *tmproto.Header) Epoch {
return Epoch{
EpochNumber: epochNumber,
CurrentEpochInterval: epochInterval,
FirstBlockHeight: firstBlockHeight,
LastBlockHeader: lastBlockHeader,
// NOTE: SealerHeader will be set in the next epoch
}
}
func (e Epoch) GetLastBlockHeight() uint64 {
if e.EpochNumber == 0 {
return 0
}
return e.FirstBlockHeight + e.CurrentEpochInterval - 1
}
func (e Epoch) GetSecondBlockHeight() uint64 {
if e.EpochNumber == 0 {
panic("should not be called when epoch number is zero")
}
return e.FirstBlockHeight + 1
}
func (e Epoch) IsLastBlock(ctx sdk.Context) bool {
return e.GetLastBlockHeight() == uint64(ctx.BlockHeight())
}
func (e Epoch) IsFirstBlock(ctx sdk.Context) bool {
return e.FirstBlockHeight == uint64(ctx.BlockHeight())
}
func (e Epoch) IsSecondBlock(ctx sdk.Context) bool {
if e.EpochNumber == 0 {
return false
}
return e.GetSecondBlockHeight() == uint64(ctx.BlockHeight())
}
// IsFirstBlockOfNextEpoch checks whether the current block is the first block of
// the next epoch
// CONTRACT: IsFirstBlockOfNextEpoch can only be called by the epoching module
// once upon the first block of a new epoch
// other modules should use IsFirstBlock instead.
func (e Epoch) IsFirstBlockOfNextEpoch(ctx sdk.Context) bool {
if e.EpochNumber == 0 {
return ctx.BlockHeight() == 1
} else {
height := uint64(ctx.BlockHeight())
return e.FirstBlockHeight+e.CurrentEpochInterval == height
}
}
// WithinBoundary checks whether the given height is within this epoch or not
func (e Epoch) WithinBoundary(height uint64) bool {
if height < e.FirstBlockHeight || height > uint64(e.LastBlockHeader.Height) {
return false
} else {
return true
}
}
// ValidateBasic does sanity checks on Epoch
func (e Epoch) ValidateBasic() error {
if e.CurrentEpochInterval < 2 {
return ErrInvalidEpoch.Wrapf("CurrentEpochInterval (%d) < 2", e.CurrentEpochInterval)
}
return nil
}
// NewQueuedMessage creates a new QueuedMessage from a wrapped msg
// i.e., wrapped -> unwrapped -> QueuedMessage
func NewQueuedMessage(blockHeight uint64, blockTime time.Time, txid []byte, msg sdk.Msg) (QueuedMessage, error) {
// marshal the actual msg (MsgDelegate, MsgBeginRedelegate, MsgUndelegate, ...) inside isQueuedMessage_Msg
// TODO (non-urgent): after we bump to Cosmos SDK v0.46, add MsgCancelUnbondingDelegation
var qmsg isQueuedMessage_Msg
var msgBytes []byte
var err error
switch msgWithType := msg.(type) {
case *MsgWrappedDelegate:
if msgBytes, err = msgWithType.Msg.Marshal(); err != nil {
return QueuedMessage{}, err
}
qmsg = &QueuedMessage_MsgDelegate{
MsgDelegate: msgWithType.Msg,
}
case *MsgWrappedBeginRedelegate:
if msgBytes, err = msgWithType.Msg.Marshal(); err != nil {
return QueuedMessage{}, err
}
qmsg = &QueuedMessage_MsgBeginRedelegate{
MsgBeginRedelegate: msgWithType.Msg,
}
case *MsgWrappedUndelegate:
if msgBytes, err = msgWithType.Msg.Marshal(); err != nil {
return QueuedMessage{}, err
}
qmsg = &QueuedMessage_MsgUndelegate{
MsgUndelegate: msgWithType.Msg,
}
case *stakingtypes.MsgCreateValidator:
if msgBytes, err = msgWithType.Marshal(); err != nil {
return QueuedMessage{}, err
}
qmsg = &QueuedMessage_MsgCreateValidator{
MsgCreateValidator: msgWithType,
}
default:
return QueuedMessage{}, ErrUnwrappedMsgType
}
queuedMsg := QueuedMessage{
TxId: txid,
MsgId: tmhash.Sum(msgBytes),
BlockHeight: blockHeight,
BlockTime: &blockTime,
Msg: qmsg,
}
return queuedMsg, nil
}
func (qm QueuedMessage) GetSigners() []sdk.AccAddress {
return qm.UnwrapToSdkMsg().GetSigners()
}
func (qm QueuedMessage) ValidateBasic() error {
return qm.UnwrapToSdkMsg().ValidateBasic()
}
// UnpackInterfaces implements UnpackInterfacesMessage.UnpackInterfaces
func (qm QueuedMessage) UnpackInterfaces(unpacker codectypes.AnyUnpacker) error {
var pubKey cryptotypes.PubKey
msgWithType, ok := qm.UnwrapToSdkMsg().(*stakingtypes.MsgCreateValidator)
if !ok {
return nil
}
return unpacker.UnpackAny(msgWithType.Pubkey, &pubKey)
}
func (qm *QueuedMessage) UnwrapToSdkMsg() sdk.Msg {
var unwrappedMsgWithType sdk.Msg
// TODO (non-urgent): after we bump to Cosmos SDK v0.46, add MsgCancelUnbondingDelegation
switch unwrappedMsg := qm.Msg.(type) {
case *QueuedMessage_MsgCreateValidator:
unwrappedMsgWithType = unwrappedMsg.MsgCreateValidator
case *QueuedMessage_MsgDelegate:
unwrappedMsgWithType = unwrappedMsg.MsgDelegate
case *QueuedMessage_MsgUndelegate:
unwrappedMsgWithType = unwrappedMsg.MsgUndelegate
case *QueuedMessage_MsgBeginRedelegate:
unwrappedMsgWithType = unwrappedMsg.MsgBeginRedelegate
default:
panic(errorsmod.Wrap(ErrInvalidQueuedMessageType, qm.String()))
}
return unwrappedMsgWithType
}