-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
epoching: API: epoch_msgs/{epoch_num} -> all events during this epoch #108
Merged
Merged
Changes from 3 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
c381929
init
SebastianElvis a5f87b7
fix CI
SebastianElvis e9174be
fix
SebastianElvis 18affe4
minor
SebastianElvis 527b82d
Merge branch 'main' into epoching-epoch-events-api
SebastianElvis 54a3127
tests
SebastianElvis 91cf670
prepare for fixing CI
SebastianElvis 7189c38
reject msgs during 0 epoch
SebastianElvis 39485d2
reject msgs in 0epoch
SebastianElvis d592381
update event
SebastianElvis abe11a4
fix CI
SebastianElvis 146068f
Merge branch 'main' into epoching-epoch-events-api
SebastianElvis b73ba34
tidy
SebastianElvis f049f49
Merge branch 'main' into epoching-epoch-events-api
SebastianElvis File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -9,69 +9,76 @@ import ( | |||||
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" | ||||||
) | ||||||
|
||||||
// InitQueueLength initialises the msg queue length to 0 | ||||||
func (k Keeper) InitQueueLength(ctx sdk.Context) { | ||||||
store := ctx.KVStore(k.storeKey) | ||||||
// InitQueueLength initialises the msg queue length of the current epoch to 0 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
func (k Keeper) InitMsgQueue(ctx sdk.Context) { | ||||||
store := k.msgQueueLengthStore(ctx) | ||||||
|
||||||
epochNumber := k.GetEpoch(ctx).EpochNumber | ||||||
epochNumberBytes := sdk.Uint64ToBigEndian(epochNumber) | ||||||
queueLenBytes := sdk.Uint64ToBigEndian(0) | ||||||
store.Set(types.QueueLengthKey, queueLenBytes) | ||||||
store.Set(epochNumberBytes, queueLenBytes) | ||||||
} | ||||||
|
||||||
// GetQueueLength fetches the number of queued messages | ||||||
func (k Keeper) GetQueueLength(ctx sdk.Context) uint64 { | ||||||
store := ctx.KVStore(k.storeKey) | ||||||
// GetQueueLength fetches the number of queued messages of a given epoch | ||||||
func (k Keeper) GetQueueLength(ctx sdk.Context, epochNumber uint64) uint64 { | ||||||
store := k.msgQueueLengthStore(ctx) | ||||||
epochNumberBytes := sdk.Uint64ToBigEndian(epochNumber) | ||||||
|
||||||
// get queue len in bytes from DB | ||||||
bz := store.Get(types.QueueLengthKey) | ||||||
bz := store.Get(epochNumberBytes) | ||||||
if bz == nil { | ||||||
panic(types.ErrUnknownQueueLen) | ||||||
aakoshh marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
// unmarshal | ||||||
return sdk.BigEndianToUint64(bz) | ||||||
} | ||||||
|
||||||
// setQueueLength sets the msg queue length | ||||||
func (k Keeper) setQueueLength(ctx sdk.Context, queueLen uint64) { | ||||||
store := ctx.KVStore(k.storeKey) | ||||||
|
||||||
queueLenBytes := sdk.Uint64ToBigEndian(queueLen) | ||||||
store.Set(types.QueueLengthKey, queueLenBytes) | ||||||
// GetQueueLength fetches the number of queued messages of the current epoch | ||||||
func (k Keeper) GetCurrentQueueLength(ctx sdk.Context) uint64 { | ||||||
epochNumber := k.GetEpoch(ctx).EpochNumber | ||||||
return k.GetQueueLength(ctx, epochNumber) | ||||||
} | ||||||
|
||||||
// incQueueLength adds the queue length by 1 | ||||||
func (k Keeper) incQueueLength(ctx sdk.Context) { | ||||||
queueLen := k.GetQueueLength(ctx) | ||||||
// incCurrentQueueLength adds the queue length of the current epoch by 1 | ||||||
func (k Keeper) incCurrentQueueLength(ctx sdk.Context) { | ||||||
store := k.msgQueueLengthStore(ctx) | ||||||
|
||||||
epochNumber := k.GetEpoch(ctx).EpochNumber | ||||||
epochNumberBytes := sdk.Uint64ToBigEndian(epochNumber) | ||||||
|
||||||
queueLen := k.GetQueueLength(ctx, epochNumber) | ||||||
incrementedQueueLen := queueLen + 1 | ||||||
k.setQueueLength(ctx, incrementedQueueLen) | ||||||
incrementedQueueLenBytes := sdk.Uint64ToBigEndian(incrementedQueueLen) | ||||||
|
||||||
store.Set(epochNumberBytes, incrementedQueueLenBytes) | ||||||
} | ||||||
|
||||||
// EnqueueMsg enqueues a message to the queue of the current epoch | ||||||
func (k Keeper) EnqueueMsg(ctx sdk.Context, msg types.QueuedMessage) { | ||||||
// prefix: QueuedMsgKey | ||||||
store := ctx.KVStore(k.storeKey) | ||||||
queuedMsgStore := prefix.NewStore(store, types.QueuedMsgKey) | ||||||
epochNumber := k.GetEpoch(ctx).EpochNumber | ||||||
store := k.msgQueueStore(ctx, epochNumber) | ||||||
|
||||||
// key: queueLenBytes | ||||||
queueLen := k.GetQueueLength(ctx) | ||||||
// key: index, in this case = queueLenBytes | ||||||
queueLen := k.GetCurrentQueueLength(ctx) | ||||||
queueLenBytes := sdk.Uint64ToBigEndian(queueLen) | ||||||
// value: msgBytes | ||||||
msgBytes, err := k.cdc.Marshal(&msg) | ||||||
if err != nil { | ||||||
panic(sdkerrors.Wrap(types.ErrMarshal, err.Error())) | ||||||
} | ||||||
queuedMsgStore.Set(queueLenBytes, msgBytes) | ||||||
store.Set(queueLenBytes, msgBytes) | ||||||
|
||||||
// increment queue length | ||||||
k.incQueueLength(ctx) | ||||||
k.incCurrentQueueLength(ctx) | ||||||
} | ||||||
|
||||||
// GetEpochMsgs returns the set of messages queued in the current epoch | ||||||
func (k Keeper) GetEpochMsgs(ctx sdk.Context) []*types.QueuedMessage { | ||||||
// GetEpochMsgs returns the set of messages queued in a given epoch | ||||||
func (k Keeper) GetEpochMsgs(ctx sdk.Context, epochNumber uint64) []*types.QueuedMessage { | ||||||
queuedMsgs := []*types.QueuedMessage{} | ||||||
store := ctx.KVStore(k.storeKey) | ||||||
store := k.msgQueueStore(ctx, epochNumber) | ||||||
|
||||||
// add each queued msg to queuedMsgs | ||||||
iterator := sdk.KVStorePrefixIterator(store, types.QueuedMsgKey) | ||||||
iterator := store.Iterator(nil, nil) | ||||||
defer iterator.Close() | ||||||
for ; iterator.Valid(); iterator.Next() { | ||||||
queuedMsgBytes := iterator.Value() | ||||||
|
@@ -85,20 +92,10 @@ func (k Keeper) GetEpochMsgs(ctx sdk.Context) []*types.QueuedMessage { | |||||
return queuedMsgs | ||||||
} | ||||||
|
||||||
// ClearEpochMsgs removes all messages in the queue | ||||||
func (k Keeper) ClearEpochMsgs(ctx sdk.Context) { | ||||||
store := ctx.KVStore(k.storeKey) | ||||||
|
||||||
// remove all epoch msgs | ||||||
iterator := sdk.KVStorePrefixIterator(store, types.QueuedMsgKey) | ||||||
defer iterator.Close() | ||||||
for ; iterator.Valid(); iterator.Next() { | ||||||
key := iterator.Key() | ||||||
store.Delete(key) | ||||||
} | ||||||
|
||||||
// set queue len to zero | ||||||
k.setQueueLength(ctx, 0) | ||||||
// GetCurrentEpochMsgs returns the set of messages queued in the current epoch | ||||||
func (k Keeper) GetCurrentEpochMsgs(ctx sdk.Context) []*types.QueuedMessage { | ||||||
epochNumber := k.GetEpoch(ctx).EpochNumber | ||||||
return k.GetEpochMsgs(ctx, epochNumber) | ||||||
} | ||||||
|
||||||
// HandleQueuedMsg unwraps a QueuedMessage and forwards it to the staking module | ||||||
|
@@ -153,3 +150,23 @@ func cacheTxContext(ctx sdk.Context, txid []byte, msgid []byte) (sdk.Context, sd | |||||
|
||||||
return ctx.WithMultiStore(msCache), msCache | ||||||
} | ||||||
|
||||||
// msgQueueStore returns the queue of msgs of a given epoch | ||||||
// prefix: MsgQueueKey || epochNumber | ||||||
// key: index | ||||||
// value: msg | ||||||
func (k Keeper) msgQueueStore(ctx sdk.Context, epochNumber uint64) prefix.Store { | ||||||
store := ctx.KVStore(k.storeKey) | ||||||
msgQueueStore := prefix.NewStore(store, types.MsgQueueKey) | ||||||
epochNumberBytes := sdk.Uint64ToBigEndian(epochNumber) | ||||||
return prefix.NewStore(msgQueueStore, epochNumberBytes) | ||||||
} | ||||||
|
||||||
// msgQueueLengthStore returns the length of the msg queue of a given epoch | ||||||
// prefix: QueueLengthKey | ||||||
// key: epochNumber | ||||||
// value: queue length | ||||||
func (k Keeper) msgQueueLengthStore(ctx sdk.Context) prefix.Store { | ||||||
store := ctx.KVStore(k.storeKey) | ||||||
return prefix.NewStore(store, types.QueueLengthKey) | ||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I understand epoch 0 is the genesis block. There the
genutil
module executes theMsgCreateValidator
methods directly, we have no delays. Why is this copying necessary?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah
MsgCreateValidator
does not go through the msg queue so is not delayed.Cosmos SDK does not execute
BeginBlock
andEndBlock
for the genesis block. If someone submits a wrapped msg (e.g.,MsgWrappedDelegate
) at genesis block (i.e., in epoch 0), then it will be put in epoch 0's msg queue. However, since epoch 0 does not haveEndBlock
invocation, the queued msg will never be handled. This corner case was tested by fuzzing tests inepoch_msg_queue_test.go
.This PR works around this issue by copying all queued msgs in epoch 0 to epoch 1 upon
BeginBlock
of block 1. Another fix I can think of is to reject all wrapped msgs submitted during epoch 0. Do you have any better alternatives?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the explanation. I just don't understand how anyone can send anything in the genesis block. The genesis block is created purely based on the
genesis.json
file, isn't it?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's weird that the system handles messages during block 0. So perhaps let's reject validator-related msgs during block 0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe it was just an artifact of the simulation? Rejection sounds good to me.