-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
context.go
82 lines (66 loc) · 2.28 KB
/
context.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
package testutil
import (
"testing"
"time"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/assert"
"cosmossdk.io/core/header"
"cosmossdk.io/log"
"cosmossdk.io/store"
"cosmossdk.io/store/metrics"
storetypes "cosmossdk.io/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
// DefaultContext creates a sdk.Context with a fresh MemDB that can be used in tests.
func DefaultContext(key, tkey storetypes.StoreKey) sdk.Context {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
cms.MountStoreWithDB(tkey, storetypes.StoreTypeTransient, db)
err := cms.LoadLatestVersion()
if err != nil {
panic(err)
}
ctx := sdk.NewContext(cms, false, log.NewNopLogger())
return ctx
}
// DefaultContextWithKeys creates a sdk.Context with a fresh MemDB, mounting the providing keys for usage in the multistore.
// This function is intended to be used for testing purposes only.
func DefaultContextWithKeys(
keys map[string]*storetypes.KVStoreKey,
transKeys map[string]*storetypes.TransientStoreKey,
memKeys map[string]*storetypes.MemoryStoreKey,
) sdk.Context {
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
for _, key := range keys {
cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
}
for _, tKey := range transKeys {
cms.MountStoreWithDB(tKey, storetypes.StoreTypeTransient, db)
}
for _, memkey := range memKeys {
cms.MountStoreWithDB(memkey, storetypes.StoreTypeMemory, db)
}
err := cms.LoadLatestVersion()
if err != nil {
panic(err)
}
return sdk.NewContext(cms, false, log.NewNopLogger())
}
type TestContext struct {
Ctx sdk.Context
DB *dbm.MemDB
CMS store.CommitMultiStore
}
func DefaultContextWithDB(tb testing.TB, key, tkey storetypes.StoreKey) TestContext {
tb.Helper()
db := dbm.NewMemDB()
cms := store.NewCommitMultiStore(db, log.NewNopLogger(), metrics.NewNoOpMetrics())
cms.MountStoreWithDB(key, storetypes.StoreTypeIAVL, db)
cms.MountStoreWithDB(tkey, storetypes.StoreTypeTransient, db)
err := cms.LoadLatestVersion()
assert.NoError(tb, err)
ctx := sdk.NewContext(cms, false, log.NewNopLogger()).WithHeaderInfo(header.Info{Time: time.Now()})
return TestContext{ctx, db, cms}
}