/
keeper.go
174 lines (144 loc) · 4.98 KB
/
keeper.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
package keeper
import (
"fmt"
"time"
"github.com/cosmos/cosmos-sdk/codec"
storetypes "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
paramtypes "github.com/cosmos/cosmos-sdk/x/params/types"
"github.com/tendermint/tendermint/libs/log"
ctypes "github.com/settlus/chain/types"
"github.com/settlus/chain/x/oracle/types"
)
const BlockTimestampMargin = 15 * time.Second
type (
Keeper struct {
cdc codec.BinaryCodec
storeKey storetypes.StoreKey
paramstore paramtypes.Subspace
AccountKeeper types.AccountKeeper
BankKeeper types.BankKeeper
DistributionKeeper types.DistributionKeeper
StakingKeeper types.StakingKeeper
SettlementKeeper types.SettlementKeeper
distributionName string
}
)
func NewKeeper(
cdc codec.BinaryCodec,
storeKey storetypes.StoreKey,
ps paramtypes.Subspace,
accountKeeper types.AccountKeeper,
bankKeeper types.BankKeeper,
distributionKeeper types.DistributionKeeper,
stakingKeeper types.StakingKeeper,
settlementKeeper types.SettlementKeeper,
distributionName string,
) *Keeper {
// ensure oracle module account is set
if addr := accountKeeper.GetModuleAddress(types.ModuleName); addr == nil {
panic(fmt.Sprintf("%s module account has not been set", types.ModuleName))
}
// set KeyTable if it has not already been set
if !ps.HasKeyTable() {
ps = ps.WithKeyTable(types.ParamKeyTable())
}
return &Keeper{
cdc: cdc,
storeKey: storeKey,
paramstore: ps,
AccountKeeper: accountKeeper,
BankKeeper: bankKeeper,
DistributionKeeper: distributionKeeper,
StakingKeeper: stakingKeeper,
SettlementKeeper: settlementKeeper,
distributionName: distributionName,
}
}
func (k Keeper) Logger(ctx sdk.Context) log.Logger {
return ctx.Logger().With("module", fmt.Sprintf("x/%s", types.ModuleName))
}
func (k Keeper) GetCurrentRoundInfo(ctx sdk.Context) *types.RoundInfo {
params := k.GetParams(ctx)
blockHeight := ctx.BlockHeight()
prevoteEnd, voteEnd := types.CalculateVotePeriod(blockHeight, params.VotePeriod)
oracleData := []*types.OracleData{}
oracleData = appendIfValid(oracleData, k.blockOracleData(params.Whitelist))
oracleData = appendIfValid(oracleData, k.ownershipOracleData(ctx, params.VotePeriod))
roundInfo := types.RoundInfo{
Id: types.CalculateRoundId(blockHeight, params.VotePeriod),
PrevoteEnd: prevoteEnd,
VoteEnd: voteEnd,
OracleData: oracleData,
Timestamp: ctx.BlockHeader().Time.Add(-BlockTimestampMargin).UnixMilli(),
}
return &roundInfo
}
func (k Keeper) blockOracleData(whitelist []*types.Chain) *types.OracleData {
source := make([]string, len(whitelist))
for idx, chain := range whitelist {
source[idx] = chain.ChainId
}
return &types.OracleData{
Topic: types.OralceTopic_BLOCK,
Sources: source,
}
}
func (k Keeper) ownershipOracleData(ctx sdk.Context, votePeriod uint64) *types.OracleData {
// We will retrieve all NFTs that need to be verified collected until the last round.
// Because list of nfts in the current round can be increased as the round progresses
startHeight := types.CalculateRoundStartHeight(ctx.BlockHeight(), votePeriod)
nfts := k.SettlementKeeper.GetAllUniqueNftToVerify(ctx, startHeight-1)
sources := make([]string, len(nfts))
for i, nft := range nfts {
sources[i] = nft.FormatString()
}
return &types.OracleData{
Topic: types.OralceTopic_OWNERSHIP,
Sources: sources,
}
}
// GetBlockData returns block data of a chain
func (k Keeper) GetBlockData(ctx sdk.Context, chainId string) (*types.BlockData, error) {
store := ctx.KVStore(k.storeKey)
chain, err := k.GetChain(ctx, chainId)
if err != nil {
return nil, fmt.Errorf("chain not found: %w", err)
}
bd := store.Get(types.BlockDataKey(chain.ChainId))
if bd == nil {
return nil, types.ErrBlockDataNotFound
}
var blockData types.BlockData
k.cdc.MustUnmarshal(bd, &blockData)
return &blockData, nil
}
// GetAllBlockData returns block data of all chains
func (k Keeper) GetAllBlockData(ctx sdk.Context) []types.BlockData {
store := ctx.KVStore(k.storeKey)
var blockData []types.BlockData
iterator := sdk.KVStorePrefixIterator(store, types.BlockDataKeyPrefix)
defer iterator.Close()
for ; iterator.Valid(); iterator.Next() {
var bd types.BlockData
k.cdc.MustUnmarshal(iterator.Value(), &bd)
blockData = append(blockData, bd)
}
return blockData
}
// SetBlockData sets block data of a chain
func (k Keeper) SetBlockData(ctx sdk.Context, blockData types.BlockData) {
store := ctx.KVStore(k.storeKey)
bd := k.cdc.MustMarshal(&blockData)
store.Set(types.BlockDataKey(blockData.ChainId), bd)
}
func (k Keeper) FillSettlementRecipients(ctx sdk.Context, nftOwnership map[types.Nft]ctypes.HexAddressString) {
startHeight := types.CalculateRoundStartHeight(ctx.BlockHeight(), k.GetParams(ctx).VotePeriod)
k.SettlementKeeper.SetRecipients(ctx, nftOwnership, startHeight-1)
}
func appendIfValid(slice []*types.OracleData, elem *types.OracleData) []*types.OracleData {
if len(elem.Sources) == 0 {
return slice
}
return append(slice, elem)
}