/
test_common.go
150 lines (119 loc) · 4.28 KB
/
test_common.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
// nolint
package auth
import (
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/libs/log"
dbm "github.com/tendermint/tm-db"
"github.com/pocblockchain/pocc/codec"
"github.com/pocblockchain/pocc/store"
sdk "github.com/pocblockchain/pocc/types"
"github.com/pocblockchain/pocc/x/auth/types"
"github.com/pocblockchain/pocc/x/params/subspace"
"github.com/pocblockchain/pocc/x/supply/exported"
)
type testInput struct {
cdc *codec.Codec
ctx sdk.Context
ak AccountKeeper
sk types.SupplyKeeper
}
// moduleAccount defines an account for modules that holds coins on a pool
type moduleAccount struct {
*types.BaseAccount
name string `json:"name" yaml:"name"` // name of the module
permissions []string `json:"permissions" yaml"permissions"` // permissions of module account
}
// HasPermission returns whether or not the module account has permission.
func (ma moduleAccount) HasPermission(permission string) bool {
for _, perm := range ma.permissions {
if perm == permission {
return true
}
}
return false
}
// GetName returns the the name of the holder's module
func (ma moduleAccount) GetName() string {
return ma.name
}
// GetPermissions returns permissions granted to the module account
func (ma moduleAccount) GetPermissions() []string {
return ma.permissions
}
func setupTestInput() testInput {
db := dbm.NewMemDB()
cdc := codec.New()
types.RegisterCodec(cdc)
cdc.RegisterInterface((*exported.ModuleAccountI)(nil), nil)
cdc.RegisterConcrete(&moduleAccount{}, "poc/ModuleAccount", nil)
codec.RegisterCrypto(cdc)
authCapKey := sdk.NewKVStoreKey("authCapKey")
keyParams := sdk.NewKVStoreKey("subspace")
tkeyParams := sdk.NewTransientStoreKey("transient_subspace")
ms := store.NewCommitMultiStore(db)
ms.MountStoreWithDB(authCapKey, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(keyParams, sdk.StoreTypeIAVL, db)
ms.MountStoreWithDB(tkeyParams, sdk.StoreTypeTransient, db)
ms.LoadLatestVersion()
ps := subspace.NewSubspace(cdc, keyParams, tkeyParams, types.DefaultParamspace)
ak := NewAccountKeeper(cdc, authCapKey, ps, types.ProtoBaseAccount)
sk := NewDummySupplyKeeper(ak)
ctx := sdk.NewContext(ms, abci.Header{ChainID: "test-chain-id"}, false, log.NewNopLogger())
ak.SetParams(ctx, types.DefaultParams())
return testInput{cdc: cdc, ctx: ctx, ak: ak, sk: sk}
}
// DummySupplyKeeper defines a supply keeper used only for testing to avoid
// circle dependencies
type DummySupplyKeeper struct {
ak AccountKeeper
}
// NewDummySupplyKeeper creates a DummySupplyKeeper instance
func NewDummySupplyKeeper(ak AccountKeeper) DummySupplyKeeper {
return DummySupplyKeeper{ak}
}
// SendCoinsFromAccountToModule for the dummy supply keeper
func (sk DummySupplyKeeper) SendCoinsFromAccountToModule(ctx sdk.Context, fromAddr sdk.AccAddress, recipientModule string, amt sdk.Coins) sdk.Error {
fromAcc := sk.ak.GetAccount(ctx, fromAddr)
moduleAcc := sk.GetModuleAccount(ctx, recipientModule)
newFromCoins, hasNeg := fromAcc.GetCoins().SafeSub(amt)
if hasNeg {
return sdk.ErrInsufficientCoins(fromAcc.GetCoins().String())
}
newToCoins := moduleAcc.GetCoins().Add(amt)
if err := fromAcc.SetCoins(newFromCoins); err != nil {
return sdk.ErrInternal(err.Error())
}
if err := moduleAcc.SetCoins(newToCoins); err != nil {
return sdk.ErrInternal(err.Error())
}
sk.ak.SetAccount(ctx, fromAcc)
sk.ak.SetAccount(ctx, moduleAcc)
return nil
}
// GetModuleAccount for dummy supply keeper
func (sk DummySupplyKeeper) GetModuleAccount(ctx sdk.Context, moduleName string) exported.ModuleAccountI {
addr := sk.GetModuleAddress(moduleName)
acc := sk.ak.GetAccount(ctx, addr)
if acc != nil {
macc, ok := acc.(exported.ModuleAccountI)
if ok {
return macc
}
}
moduleAddress := sk.GetModuleAddress(moduleName)
baseAcc := types.NewBaseAccountWithAddress(moduleAddress)
// create a new module account
macc := &moduleAccount{
BaseAccount: &baseAcc,
name: moduleName,
permissions: []string{"basic"},
}
maccI := (sk.ak.NewAccount(ctx, macc)).(exported.ModuleAccountI)
sk.ak.SetAccount(ctx, maccI)
return maccI
}
// GetModuleAddress for dummy supply keeper
func (sk DummySupplyKeeper) GetModuleAddress(moduleName string) sdk.AccAddress {
return sdk.AccAddress(crypto.AddressHash([]byte(moduleName)))
}