/
logic_end_block_handle_upload_timeout.go
84 lines (68 loc) · 2.75 KB
/
logic_end_block_handle_upload_timeout.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
package keeper
import (
"context"
"github.com/KYVENetwork/chain/x/bundles/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// HandleUploadTimeout is an end block hook that triggers an upload timeout for every pool (if applicable).
func (k Keeper) HandleUploadTimeout(goCtx context.Context) {
ctx := sdk.UnwrapSDKContext(goCtx)
// Iterate over all pool Ids.
for _, pool := range k.poolKeeper.GetAllPools(ctx) {
err := k.AssertPoolCanRun(ctx, pool.Id)
bundleProposal, _ := k.GetBundleProposal(ctx, pool.Id)
// Check if pool is active
if err != nil {
// if pool was disabled we drop the current bundle. We only drop
// if there is an ongoing bundle proposal. Else we just remove the next
// uploader
if err == types.ErrPoolDisabled && bundleProposal.StorageId != "" {
k.dropCurrentBundleProposal(ctx, pool.Id, types.VoteDistribution{
Valid: 0,
Invalid: 0,
Abstain: 0,
Total: 0,
Status: types.BUNDLE_STATUS_DISABLED,
}, "")
} else if bundleProposal.NextUploader != "" {
bundleProposal.NextUploader = ""
k.SetBundleProposal(ctx, bundleProposal)
}
// since a paused or disabled pool can not produce any bundles
// we continue because timeout slashes don't apply in this case
continue
}
// Skip if we haven't reached the upload interval.
if uint64(ctx.BlockTime().Unix()) < (bundleProposal.UpdatedAt + pool.UploadInterval) {
continue
}
// Check if bundle needs to be dropped
if bundleProposal.StorageId != "" {
// check if the quorum was actually reached
voteDistribution := k.GetVoteDistribution(ctx, pool.Id)
if voteDistribution.Status == types.BUNDLE_STATUS_NO_QUORUM {
// handle stakers who did not vote at all
k.handleNonVoters(ctx, pool.Id)
// Get next uploader from all pool stakers
nextUploader := k.chooseNextUploader(ctx, pool.Id)
// If consensus wasn't reached, we drop the bundle and emit an event.
k.dropCurrentBundleProposal(ctx, pool.Id, voteDistribution, nextUploader)
continue
}
}
// Skip if we haven't reached the upload timeout.
if uint64(ctx.BlockTime().Unix()) < (bundleProposal.UpdatedAt + pool.UploadInterval + k.GetUploadTimeout(ctx)) {
continue
}
// We now know that the pool is active and the upload timeout has been reached.
// Now we increase the points of the valaccount
// (if he is still participating in the pool) and select a new one.
if k.stakerKeeper.DoesValaccountExist(ctx, pool.Id, bundleProposal.NextUploader) {
k.addPoint(ctx, pool.Id, bundleProposal.NextUploader)
}
// Update bundle proposal and choose next uploader
bundleProposal.NextUploader = k.chooseNextUploader(ctx, pool.Id)
bundleProposal.UpdatedAt = uint64(ctx.BlockTime().Unix())
k.SetBundleProposal(ctx, bundleProposal)
}
}