-
Notifications
You must be signed in to change notification settings - Fork 3.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: v2 services helpers and demo using x/bank
#23057
base: main
Are you sure you want to change the base?
Changes from all commits
dab4f61
ccdbad4
ef438eb
9dfae05
779bd84
2e7ca6d
6161b6a
7355259
46a30d3
89e5b26
05a8577
f827e73
6c7dc2a
ed63aca
c8b7460
4956083
b5703d4
c7bc69d
1442dc2
9d98953
d29ed65
d42ac1f
a6ffa04
fb55664
87fb4f9
e18d809
4f56126
f67b33f
997bf85
ed61ace
47a681e
9f54324
0cea6dd
1838bbf
31198b6
e932ea5
d17569e
71ad965
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package coretesting | ||
|
||
import ( | ||
"context" | ||
|
||
appmodulev2 "cosmossdk.io/core/appmodule/v2" | ||
corecontext "cosmossdk.io/core/context" | ||
corelog "cosmossdk.io/core/log" | ||
"cosmossdk.io/core/router" | ||
"cosmossdk.io/core/store" | ||
) | ||
|
||
type TestEnvironmentConfig struct { | ||
ModuleName string | ||
Logger corelog.Logger | ||
MsgRouter router.Service | ||
QueryRouter router.Service | ||
} | ||
|
||
type TestEnvironment struct { | ||
appmodulev2.Environment | ||
|
||
testEventService TestEventService | ||
testHeaderService TestHeaderService | ||
} | ||
|
||
func NewTestEnvironment(cfg TestEnvironmentConfig) (TestContext, TestEnvironment) { | ||
ctx := Context() | ||
|
||
testEventService := NewTestEventService(ctx, cfg.ModuleName) | ||
testHeaderService := TestHeaderService{} | ||
|
||
env := TestEnvironment{ | ||
Environment: appmodulev2.Environment{ | ||
Logger: cfg.Logger, | ||
BranchService: nil, | ||
EventService: testEventService, | ||
GasService: TestGasService{}, | ||
HeaderService: testHeaderService, | ||
QueryRouterService: cfg.QueryRouter, | ||
MsgRouterService: cfg.MsgRouter, | ||
TransactionService: TestTransactionService{}, | ||
KVStoreService: KVStoreService(ctx, cfg.ModuleName), | ||
MemStoreService: nil, | ||
}, | ||
testEventService: testEventService, | ||
testHeaderService: testHeaderService, | ||
} | ||
|
||
// set internal context to point to environment | ||
ctx.Context = context.WithValue(ctx.Context, corecontext.EnvironmentContextKey, env.Environment) | ||
return ctx, env | ||
} | ||
|
||
func (env TestEnvironment) EventService() TestEventService { | ||
return env.testEventService | ||
} | ||
|
||
func (env TestEnvironment) KVStoreService() store.KVStoreService { | ||
return env.Environment.KVStoreService | ||
} | ||
|
||
func (env TestEnvironment) HeaderService() TestHeaderService { | ||
return env.testHeaderService | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package coretesting | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/core/gas" | ||
) | ||
|
||
var _ gas.Service = &TestGasService{} | ||
|
||
type TestGasService struct{} | ||
|
||
func (m TestGasService) GasMeter(ctx context.Context) gas.Meter { | ||
dummy := unwrap(ctx) | ||
|
||
return dummy.gasMeter | ||
} | ||
|
||
func (m TestGasService) GasConfig(ctx context.Context) gas.GasConfig { | ||
dummy := unwrap(ctx) | ||
|
||
return dummy.gasConfig | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package coretesting | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/core/header" | ||
) | ||
|
||
var _ header.Service = &TestHeaderService{} | ||
|
||
type TestHeaderService struct{} | ||
|
||
func (e TestHeaderService) HeaderInfo(ctx context.Context) header.Info { | ||
return unwrap(ctx).header | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package coretesting | ||
|
||
import ( | ||
"context" | ||
|
||
"cosmossdk.io/core/transaction" | ||
) | ||
|
||
var _ transaction.Service = &TestTransactionService{} | ||
|
||
type TestTransactionService struct{} | ||
|
||
func (m TestTransactionService) ExecMode(ctx context.Context) transaction.ExecMode { | ||
dummy := unwrap(ctx) | ||
|
||
return dummy.execMode | ||
} |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -139,7 +139,7 @@ func ListenerMux(listeners ...Listener) Listener { | |||||||
|
||||||||
mux.onBatch = func(batch PacketBatch) error { | ||||||||
for _, listener := range listeners { | ||||||||
err := batch.apply(&listener) //nolint:gosec // aliasing is safe here | ||||||||
err := batch.apply(&listener) | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Address potential memory aliasing in loop iteration. The loop variable Apply this diff to create a local copy of the listener: -err := batch.apply(&listener)
+listenerCopy := listener
+err := batch.apply(&listenerCopy) 📝 Committable suggestion
Suggested change
🧰 Tools🪛 golangci-lint (1.62.2)[medium] 142-142: G601: Implicit memory aliasing in for loop. (gosec) |
||||||||
if err != nil { | ||||||||
return err | ||||||||
} | ||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created the TestContext wrapper so that we can extend with methods to modify it like the ones here