-
Notifications
You must be signed in to change notification settings - Fork 4
/
abci.go
86 lines (72 loc) · 2.28 KB
/
abci.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
package oracle
import (
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/ODIN-PROTOCOL/odin-core/x/oracle/keeper"
"github.com/ODIN-PROTOCOL/odin-core/x/oracle/types"
)
// handleBeginBlock re-calculates and saves the rolling seed value based on block hashes.
func handleBeginBlock(ctx sdk.Context, k keeper.Keeper) error {
// Update rolling seed used for pseudorandom oracle provider selection.
rollingSeed, err := k.GetRollingSeed(ctx)
if err != nil {
return err
}
err = k.SetRollingSeed(ctx, append(rollingSeed[1:], ctx.HeaderHash()[0]))
if err != nil {
return err
}
// Reward a portion of block rewards (inflation + tx fee) to active oracle validators.
err = k.AllocateTokens(ctx, ctx.VoteInfos())
if err != nil {
return err
}
params, err := k.GetParams(ctx)
if err != nil {
return err
}
// Reset the price to the original price if a new boundary period has begun
rewardThresholdBlocks := params.DataProviderRewardThreshold.Blocks
previousBlock := uint64(ctx.BlockHeight() - 1)
if previousBlock%rewardThresholdBlocks == 0 {
initialReward := params.DataProviderRewardPerByte
err = k.SetAccumulatedDataProvidersRewards(
ctx,
types.NewDataProvidersAccumulatedRewards(initialReward, sdk.NewCoins()),
)
if err != nil {
return err
}
}
return nil
}
// handleEndBlock cleans up the state during end block. See comment in the implementation!
func handleEndBlock(ctx sdk.Context, k keeper.Keeper) error {
pendingResolveList, err := k.GetPendingResolveList(ctx)
if err != nil {
return err
}
// Loops through all requests in the resolvable list to resolve all of them!
for _, reqID := range pendingResolveList {
err = k.ResolveRequest(ctx, reqID)
if err != nil {
return err
}
err = k.AllocateRewardsToDataProviders(ctx, reqID)
if err != nil {
return err
}
}
// Once all the requests are resolved, we can clear the list.
err = k.SetPendingResolveList(ctx, []types.RequestID{})
if err != nil {
return err
}
// Lastly, we clean up data requests that are supposed to be expired.
err = k.ProcessExpiredRequests(ctx)
if err != nil {
return err
}
// NOTE: We can remove old requests from state to optimize space, using `k.DeleteRequest`
// and `k.DeleteReports`. We don't do that now as it is premature optimization at this state.
return nil
}