Skip to content
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

Submission pruning improvements #204

Merged
merged 4 commits into from
Nov 23, 2022
Merged

Conversation

KonradStaniec
Copy link
Collaborator

@KonradStaniec KonradStaniec commented Nov 16, 2022

This pr makes btccheckpoint module feature complete (except rewards) and simplifies the logic around keeping track which submission is in what state.

New features:

  • choosing best submission for the epoch
  • removing all submissions except the best one for finalized epoch
  • removing submissions which are on fork or not known by btclight client
  • making sure that there are always better subbmission in parent epochs to avoid out of order checkpoints.

This breaking change as (I have created breaking label):

  • db layout is changes there is not longer need for different types of indexes for unconfirmed, confirmed, finalized submissions
  • I have changed protobuff of BTCSpvProof to use BTCHeaderBytes custom type for confirming headers

@KonradStaniec KonradStaniec changed the base branch from main to dev November 16, 2022 18:50
@KonradStaniec KonradStaniec added the CONSENSUS BREAKING Breaks consensus label Nov 18, 2022
@KonradStaniec KonradStaniec requested review from vitsalis and SebastianElvis and removed request for vitsalis November 18, 2022 14:19
@KonradStaniec KonradStaniec marked this pull request as ready for review November 18, 2022 14:20
Copy link
Member

@SebastianElvis SebastianElvis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lgtm and thanks for the comprehensive fuzz tests! Left some comments with considerations of ZoneConcierge.

@@ -26,20 +26,17 @@ message SubmissionKey {
repeated TransactionKey key = 1;
}

enum EpochStatus {
enum BtcStatus {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this renamed? Actually I prefer the previous name 😓

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yea, it is beacouse it is used in two contexts now i.e

  • when we are checking submission status
  • and later when we prune submissions and decide which submission is the best we also use this enum to mark epoch status.

Having is this use the same enum, enables easy state comparison like bestSubmissionStatus > currentEpoch.Status, to check is we should change epoch status.

So I kinda agree previous type name was a bit better, but imo having it to be more generic and covering more cases is worth it in terms of code simplicity.

Comment on lines 33 to 54
SubmissionBtcInfo struct {
SubmissionKey types.SubmissionKey
// Depth of the oldest btc header of the submission
OldestBlockDepth uint64

// Depth of the youngest btc header of the submission
YoungestBlockDepth uint64

// Index of the latest transaction in youngest submission block
LatestTxIndex uint32
}

EpochChangesSummary struct {
SubmissionsToDelete []*types.SubmissionKey
SubmissionsToKeep []*types.SubmissionKey
EpochBestSubmission *SubmissionBtcInfo
BestSubmissionIdx int
}

EpochInfo struct {
bestSubmission *SubmissionBtcInfo
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a better place to define these structs is types/. The current keeper.go is already long enough and it's better to make it only include keeper-related functions.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, is it possible to make EpochInfo and SubmissionBtcInfo protobuf objects? The ZoneConcierge module may use them for the timestamp proofs.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 on moving SubmissionBtcInfo and it relted methods to types. It seems this is pretty well defined type to public.

As for EpochInfo and EpochChangesSummary those are rather implementation details which I would rather make package private in keeper.

As for protobuf, I would prefer to have types as simple structs until this really will be required.

var depth = initial
for _, tk := range sk.Key {
d, err := k.headerDepth(ctx, tk.Hash)
// HappenedAfter returns true if `this` submission happened after `that` submission
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above, HappenedAfter, SubmissionDepth, and IsBetterThan can also be moved to types/

}
}

func (k Keeper) checkConfirmed(ctx sdk.Context) {
func (k Keeper) clearEpochData(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will we invoke this function? Looks like it removes every submission in an epoch...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are two cases when this could happen:

  • all submissions in epoch landed on forks or were evicted by btc light client
  • parent epoch lost all submissions

@@ -22,7 +23,7 @@ func BlockCreationResultToProofs(inputs []*dg.BlockCreationResult) []*btcctypes.
var spvs []*btcctypes.BTCSpvProof

for _, input := range inputs {
headerBytes := input.HeaderBytes
headerBytes, _ := bbn.NewBTCHeaderBytesFromBytes(input.HeaderBytes)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should panic here if the error is not nil, right?

Comment on lines +546 to +547
// a bit of special case, when parent epoch lost all its submissions but current
// epoch was already in submitted/confirmed states.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When will we face this situation? Looks like this will revert a lot of things...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So to be honest, this situation can only happen in most fresh blocks and it is probably dependent on our choice of parameters (epoch length, w, k etc) but we need to handle it nevertheless.

It is worth remember that we always require that older epoch have always at least one submission deeper on the main btc chain than the new submission for the newer epoch. We also want to preserve this property if some reorgs happen. This is necessary to avoid out of order checkpoints.

So this would happen in case like:

epoch 1 - submission with depth 2 on main chain
epoch 2 - submission with depth 1 on main chain
epoch 3 - submission with depth 0 on main chain

Now when re-org happen, that will make epoch 1 submission land on fork, also need to clear submissions in epochs 2 and 3, as those no longer have submission which is their parent (i.e is deeper on the btc main chain)

@@ -130,6 +131,39 @@ func GenerateMessageWithRandomSubmitter(blockResults []*dg.BlockCreationResult)
return &msg
}

func generateMessageWithRandomSubmitterForEpoch() *btcctypes.MsgInsertBTCSpvProof {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we move this to datagen? This will be useful for vigilante as well.

@KonradStaniec KonradStaniec merged commit 7aa50d1 into dev Nov 23, 2022
@KonradStaniec KonradStaniec deleted the submission-pruning-improvements branch November 23, 2022 17:52
vitsalis pushed a commit that referenced this pull request Nov 28, 2022
* Add pruning of submissions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CONSENSUS BREAKING Breaks consensus
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants