Skip to content
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

feat(core,runtime): add address codecs in environment #21905

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/appmodule/v2/environment.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package appmodulev2

import (
"cosmossdk.io/core/address"
"cosmossdk.io/core/branch"
"cosmossdk.io/core/event"
"cosmossdk.io/core/gas"
Expand All @@ -26,4 +27,8 @@ type Environment struct {

KVStoreService store.KVStoreService
MemStoreService store.MemoryStoreService

AddressCodec address.Codec
ValidatorAddressCodec address.ValidatorAddressCodec
ConsensusAddressCodec address.ConsensusAddressCodec
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ require (
replace (
cosmossdk.io/api => ./api
cosmossdk.io/collections => ./collections
cosmossdk.io/core => ./core
cosmossdk.io/store => ./store
cosmossdk.io/x/bank => ./x/bank
cosmossdk.io/x/staking => ./x/staking
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88e
buf.build/gen/go/cosmos/gogo-proto/protocolbuffers/go v1.34.2-20240130113600-88ef6483f90f.2/go.mod h1:HqcXMSa5qnNuakaMUo+hWhF51mKbcrZxGl9Vp5EeJXc=
cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cloud.google.com/go v0.34.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw=
cosmossdk.io/core v1.0.0-alpha.3 h1:pnxaYAas7llXgVz1lM7X6De74nWrhNKnB3yMKe4OUUA=
cosmossdk.io/core v1.0.0-alpha.3/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 h1:NxxUo0GMJUbIuVg0R70e3cbn9eFTEuMr7ev1AFvypdY=
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29/go.mod h1:8s2tPeJtSiQuoyPmr2Ag7meikonISO4Fv4MoO8+ORrs=
cosmossdk.io/depinject v1.0.0 h1:dQaTu6+O6askNXO06+jyeUAnF2/ssKwrrszP9t5q050=
Expand Down
53 changes: 43 additions & 10 deletions runtime/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package runtime

import (
"context"
"fmt"

"google.golang.org/grpc"
"google.golang.org/protobuf/runtime/protoiface"

"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/log"
"cosmossdk.io/core/store"
Expand All @@ -24,16 +26,19 @@ func NewEnvironment(
opts ...EnvOption,
) appmodule.Environment {
env := appmodule.Environment{
Logger: logger,
EventService: EventService{},
HeaderService: HeaderService{},
BranchService: BranchService{},
GasService: GasService{},
TransactionService: TransactionService{},
KVStoreService: kvService,
MsgRouterService: NewMsgRouterService(failingMsgRouter{}),
QueryRouterService: NewQueryRouterService(failingQueryRouter{}),
MemStoreService: failingMemStore{},
Logger: logger,
EventService: EventService{},
HeaderService: HeaderService{},
BranchService: BranchService{},
GasService: GasService{},
TransactionService: TransactionService{},
KVStoreService: kvService,
MsgRouterService: NewMsgRouterService(failingMsgRouter{}),
QueryRouterService: NewQueryRouterService(failingQueryRouter{}),
MemStoreService: failingMemStore{},
AddressCodec: failingAddressCodec{},
ValidatorAddressCodec: failingAddressCodec{prefix: "validator"},
ConsensusAddressCodec: failingAddressCodec{prefix: "consensus"},
}

for _, opt := range opts {
Expand Down Expand Up @@ -63,6 +68,19 @@ func EnvWithMemStoreService(memStoreService store.MemoryStoreService) EnvOption
}
}

// EnvWithAddressCodecs sets the address codecs in the environment.
func EnvWithAddressCodecs(
addressCodec address.Codec,
validatorAddressCodec address.ValidatorAddressCodec,
consensusAddressCodec address.ConsensusAddressCodec,
) EnvOption {
return func(env *appmodule.Environment) {
env.AddressCodec = addressCodec
env.ValidatorAddressCodec = validatorAddressCodec
env.ConsensusAddressCodec = consensusAddressCodec
}
}
julienrbrt marked this conversation as resolved.
Show resolved Hide resolved

// failingMsgRouter is a message router that panics when accessed
// this is to ensure all fields are set in environment
type failingMsgRouter struct {
Expand Down Expand Up @@ -120,3 +138,18 @@ type failingMemStore struct {
func (failingMemStore) OpenMemoryStore(context.Context) store.KVStore {
panic("memory store not set")
}

// failingAddressCodec is an address codec that returns an error when accessed
type failingAddressCodec struct {
address.Codec

prefix string
}

func (f failingAddressCodec) StringToBytes(text string) ([]byte, error) {
return nil, fmt.Errorf("%saddress codec not set", f.prefix+" ")
}

func (f failingAddressCodec) BytesToString(bz []byte) (string, error) {
return "", fmt.Errorf("%saddress codec not set", f.prefix+" ")
}
5 changes: 5 additions & 0 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/core/address"
"cosmossdk.io/core/appmodule"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/registry"
Expand Down Expand Up @@ -254,6 +255,9 @@ func ProvideEnvironment(
app *AppBuilder,
msgServiceRouter *baseapp.MsgServiceRouter,
queryServiceRouter *baseapp.GRPCQueryRouter,
addressCodec address.Codec,
valAddressCodec address.ValidatorAddressCodec,
consAddressCodec address.ConsensusAddressCodec,
) (store.KVStoreService, store.MemoryStoreService, appmodule.Environment) {
var (
kvService store.KVStoreService = failingStoreService{}
Expand All @@ -275,6 +279,7 @@ func ProvideEnvironment(
EnvWithMsgRouterService(msgServiceRouter),
EnvWithQueryRouterService(queryServiceRouter),
EnvWithMemStoreService(memKvService),
EnvWithAddressCodecs(addressCodec, valAddressCodec, consAddressCodec),
)
}

Expand Down
27 changes: 17 additions & 10 deletions runtime/v2/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
autocliv1 "cosmossdk.io/api/cosmos/autocli/v1"
reflectionv1 "cosmossdk.io/api/cosmos/reflection/v1"
"cosmossdk.io/core/address"
appmodulev2 "cosmossdk.io/core/appmodule/v2"
"cosmossdk.io/core/comet"
"cosmossdk.io/core/header"
Expand Down Expand Up @@ -178,6 +179,9 @@ func ProvideEnvironment[T transaction.Tx](
appBuilder *AppBuilder[T],
kvFactory store.KVStoreServiceFactory,
headerService header.Service,
addressCodec address.Codec,
validatorAddressCodec address.ValidatorAddressCodec,
consensusAddressCodec address.ConsensusAddressCodec,
) (
appmodulev2.Environment,
store.KVStoreService,
Expand Down Expand Up @@ -207,16 +211,19 @@ func ProvideEnvironment[T transaction.Tx](
}

env := appmodulev2.Environment{
Logger: logger,
BranchService: stf.BranchService{},
EventService: stf.NewEventService(),
GasService: stf.NewGasMeterService(),
HeaderService: headerService,
QueryRouterService: stf.NewQueryRouterService(),
MsgRouterService: stf.NewMsgRouterService([]byte(key.Name())),
TransactionService: services.NewContextAwareTransactionService(),
KVStoreService: kvService,
MemStoreService: memKvService,
Logger: logger,
BranchService: stf.BranchService{},
EventService: stf.NewEventService(),
GasService: stf.NewGasMeterService(),
HeaderService: headerService,
QueryRouterService: stf.NewQueryRouterService(),
MsgRouterService: stf.NewMsgRouterService([]byte(key.Name())),
TransactionService: services.NewContextAwareTransactionService(),
KVStoreService: kvService,
MemStoreService: memKvService,
AddressCodec: addressCodec,
ValidatorAddressCodec: validatorAddressCodec,
ConsensusAddressCodec: consensusAddressCodec,
}

return env, kvService, memKvService
Expand Down
1 change: 1 addition & 0 deletions simapp/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ replace (
cosmossdk.io/api => ../api
cosmossdk.io/client/v2 => ../client/v2
cosmossdk.io/collections => ../collections
cosmossdk.io/core => ../core
cosmossdk.io/store => ../store
cosmossdk.io/tools/confix => ../tools/confix
cosmossdk.io/x/accounts => ../x/accounts
Expand Down
2 changes: 0 additions & 2 deletions simapp/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ cloud.google.com/go/webrisk v1.4.0/go.mod h1:Hn8X6Zr+ziE2aNd8SliSDWpEnSS1u4R9+xX
cloud.google.com/go/webrisk v1.5.0/go.mod h1:iPG6fr52Tv7sGk0H6qUFzmL3HHZev1htXuWDEEsqMTg=
cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1Vwf+KmJENM0=
cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M=
cosmossdk.io/core v1.0.0-alpha.3 h1:pnxaYAas7llXgVz1lM7X6De74nWrhNKnB3yMKe4OUUA=
cosmossdk.io/core v1.0.0-alpha.3/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 h1:NxxUo0GMJUbIuVg0R70e3cbn9eFTEuMr7ev1AFvypdY=
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29/go.mod h1:8s2tPeJtSiQuoyPmr2Ag7meikonISO4Fv4MoO8+ORrs=
cosmossdk.io/depinject v1.0.0 h1:dQaTu6+O6askNXO06+jyeUAnF2/ssKwrrszP9t5q050=
Expand Down
1 change: 1 addition & 0 deletions tests/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ replace (
cosmossdk.io/api => ../api
cosmossdk.io/client/v2 => ../client/v2
cosmossdk.io/collections => ../collections
cosmossdk.io/core => ../core
cosmossdk.io/store => ../store
cosmossdk.io/x/accounts => ../x/accounts
cosmossdk.io/x/accounts/defaults/base => ../x/accounts/defaults/base
Expand Down
2 changes: 0 additions & 2 deletions tests/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,6 @@ cloud.google.com/go/webrisk v1.4.0/go.mod h1:Hn8X6Zr+ziE2aNd8SliSDWpEnSS1u4R9+xX
cloud.google.com/go/webrisk v1.5.0/go.mod h1:iPG6fr52Tv7sGk0H6qUFzmL3HHZev1htXuWDEEsqMTg=
cloud.google.com/go/workflows v1.6.0/go.mod h1:6t9F5h/unJz41YqfBmqSASJSXccBLtD1Vwf+KmJENM0=
cloud.google.com/go/workflows v1.7.0/go.mod h1:JhSrZuVZWuiDfKEFxU0/F1PQjmpnpcoISEXH2bcHC3M=
cosmossdk.io/core v1.0.0-alpha.3 h1:pnxaYAas7llXgVz1lM7X6De74nWrhNKnB3yMKe4OUUA=
cosmossdk.io/core v1.0.0-alpha.3/go.mod h1:3u9cWq1FAVtiiCrDPpo4LhR+9V6k/ycSG4/Y/tREWCY=
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29 h1:NxxUo0GMJUbIuVg0R70e3cbn9eFTEuMr7ev1AFvypdY=
cosmossdk.io/core/testing v0.0.0-20240923163230-04da382a9f29/go.mod h1:8s2tPeJtSiQuoyPmr2Ag7meikonISO4Fv4MoO8+ORrs=
cosmossdk.io/depinject v1.0.0 h1:dQaTu6+O6askNXO06+jyeUAnF2/ssKwrrszP9t5q050=
Expand Down
Loading