/
mock_keepers.go
254 lines (202 loc) · 7.02 KB
/
mock_keepers.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
package keeper
import (
"fmt"
"time"
tenderminttypes "github.com/cometbft/cometbft/types"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
)
// account keeper mock
type mockAccountKeeper struct{}
func (k mockAccountKeeper) IterateAccounts(ctx sdk.Context, process func(authtypes.AccountI) (stop bool)) {
}
func (k mockAccountKeeper) GetAccount(ctx sdk.Context, addr sdk.AccAddress) authtypes.AccountI {
return nil
}
func (k mockAccountKeeper) GetModuleAccount(ctx sdk.Context, module string) authtypes.ModuleAccountI {
moduleAddress := authtypes.NewModuleAddress(module).String()
baseAccount := authtypes.NewBaseAccount(nil, nil, 0, 0)
baseAccount.Address = moduleAddress
return authtypes.NewModuleAccount(baseAccount, module, authtypes.Burner, authtypes.Staking)
}
func (k mockAccountKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
return GetModuleAddress(moduleName)
}
func (k mockAccountKeeper) SetModuleAccount(sdk.Context, authtypes.ModuleAccountI) {
}
// mock bank keeper
var balance map[string]sdk.Coins = make(map[string]sdk.Coins)
type mockBankKeeper struct{}
func init_balance() {
balance = make(map[string]sdk.Coins)
}
func (k mockBankKeeper) SpendableCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
return nil
}
func (k mockBankKeeper) GetSupply(ctx sdk.Context, denom string) sdk.Coin {
total := sdk.NewCoin(denom, sdk.ZeroInt())
for _, coins := range balance {
for _, coin := range coins {
if coin.Denom == denom {
total = total.Add(coin)
}
}
}
return total
}
func (k mockBankKeeper) LockedCoins(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
return k.GetAllBalances(ctx, addr)
}
func (k mockBankKeeper) GetBalance(ctx sdk.Context, addr sdk.AccAddress, denom string) sdk.Coin {
coins := balance[addr.String()]
for i := 0; i < coins.Len(); i++ {
if coins.GetDenomByIndex(i) == denom {
return coins[i]
}
}
return sdk.NewCoin(denom, sdk.ZeroInt())
}
func (k mockBankKeeper) GetAllBalances(ctx sdk.Context, addr sdk.AccAddress) sdk.Coins {
return balance[addr.String()]
}
func (k mockBankKeeper) SendCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {
moduleAcc := GetModuleAddress(recipientModule)
accountCoins := k.GetAllBalances(ctx, senderAddr)
if !accountCoins.IsAllGTE(amt) {
return fmt.Errorf("not enough coins")
}
k.SubFromBalance(senderAddr, amt)
k.AddToBalance(moduleAcc, amt)
return nil
}
func (k mockBankKeeper) UndelegateCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
return k.SendCoinsFromModuleToAccount(ctx, senderModule, recipientAddr, amt)
}
func (k mockBankKeeper) SendCoinsFromModuleToAccount(ctx sdk.Context, senderModule string, recipientAddr sdk.AccAddress, amt sdk.Coins) error {
moduleAcc := GetModuleAddress(senderModule)
accountCoins := k.GetAllBalances(ctx, moduleAcc)
if !accountCoins.IsAllGTE(amt) {
return fmt.Errorf("not enough coins")
}
k.SubFromBalance(moduleAcc, amt)
k.AddToBalance(recipientAddr, amt)
return nil
}
func (k mockBankKeeper) SendCoinsFromModuleToModule(ctx sdk.Context, senderModule string, recipientModule string, amt sdk.Coins) error {
senderModuleAcc := GetModuleAddress(senderModule)
return k.SendCoinsFromAccountToModule(ctx, senderModuleAcc, recipientModule, amt)
}
func (k mockBankKeeper) DelegateCoinsFromAccountToModule(ctx sdk.Context, senderAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) error {
// TODO support multiple coins
return k.SendCoinsFromAccountToModule(ctx, senderAddr, recipientModule, amt)
}
func (k mockBankKeeper) MintCoins(ctx sdk.Context, moduleName string, amounts sdk.Coins) error {
acc := GetModuleAddress(moduleName)
k.AddToBalance(acc, amounts)
return nil
}
func (k mockBankKeeper) BurnCoins(ctx sdk.Context, moduleName string, amounts sdk.Coins) error {
acc := GetModuleAddress(moduleName)
k.SubFromBalance(acc, amounts)
return nil
}
func (k mockBankKeeper) SetBalance(ctx sdk.Context, addr sdk.AccAddress, amounts sdk.Coins) error {
balance[addr.String()] = amounts
return nil
}
func (k mockBankKeeper) AddToBalance(addr sdk.AccAddress, amounts sdk.Coins) error {
if _, ok := balance[addr.String()]; ok {
for _, coin := range amounts {
balance[addr.String()] = balance[addr.String()].Add(coin)
}
} else {
balance[addr.String()] = amounts
}
return nil
}
func (k mockBankKeeper) SubFromBalance(addr sdk.AccAddress, amounts sdk.Coins) error {
if _, ok := balance[addr.String()]; ok {
balance[addr.String()] = balance[addr.String()].Sub(amounts...)
} else {
return fmt.Errorf("acount is empty, can't sub")
}
return nil
}
func (k mockBankKeeper) BlockedAddr(addr sdk.AccAddress) bool {
return false
}
type MockBlockStore struct {
height int64
blockHistory map[int64]*tenderminttypes.Block
}
var (
fixedTime bool
fixedDate = time.Date(2024, time.March, 1, 1, 1, 1, 1, time.UTC)
)
func (b *MockBlockStore) SetHeight(height int64) {
b.height = height
}
func SetFixedTime() {
fixedTime = true
}
func (b *MockBlockStore) AdvanceBlock(blockTime time.Duration) {
// keep block height in mock blockstore
blockInt64 := b.height + 1
b.SetHeight(blockInt64)
// create mock block header
blockHeader := tenderminttypes.Header{}
blockHeader.Height = blockInt64
if prevBlock, ok := b.blockHistory[b.height-1]; ok {
blockHeader.Time = prevBlock.Time.Add(blockTime)
} else if !fixedTime {
blockHeader.Time = time.Now().Add(blockTime)
} else {
blockHeader.Time = fixedDate.Add(blockTime)
}
// update the blockstore's block history with current block
b.SetBlockHistoryEntry(blockInt64, &tenderminttypes.Block{Header: blockHeader})
}
func (b *MockBlockStore) SetBlockHistoryEntry(height int64, blockCore *tenderminttypes.Block) {
b.blockHistory[height] = blockCore
}
func (b *MockBlockStore) Base() int64 {
return 0
}
func (b *MockBlockStore) Height() int64 {
return b.height
}
func (b *MockBlockStore) Size() int64 {
return 0
}
func (b *MockBlockStore) LoadBaseMeta() *tenderminttypes.BlockMeta {
return nil
}
func (b *MockBlockStore) LoadBlockMeta(height int64) *tenderminttypes.BlockMeta {
return &tenderminttypes.BlockMeta{}
}
func (b *MockBlockStore) LoadBlock(height int64) *tenderminttypes.Block {
return b.blockHistory[height]
}
func (b *MockBlockStore) SaveBlock(block *tenderminttypes.Block, blockParts *tenderminttypes.PartSet, seenCommit *tenderminttypes.Commit) {
}
func (b *MockBlockStore) PruneBlocks(height int64) (uint64, error) {
return 0, nil
}
func (b *MockBlockStore) LoadBlockByHash(hash []byte) *tenderminttypes.Block {
return nil
}
func (b *MockBlockStore) LoadBlockPart(height int64, index int) *tenderminttypes.Part {
return nil
}
func (b *MockBlockStore) LoadBlockCommit(height int64) *tenderminttypes.Commit {
return nil
}
func (b *MockBlockStore) LoadSeenCommit(height int64) *tenderminttypes.Commit {
return nil
}
func (b *MockBlockStore) LoadBlockMetaByHash(hash []byte) *tenderminttypes.BlockMeta {
return nil
}
func (b *MockBlockStore) DeleteLatestBlock() error {
return nil
}