From 2110ca2abcc51d8ba70ababc87a1cfa5b4277390 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 25 Sep 2024 12:59:49 +0200 Subject: [PATCH 1/2] feat(core,runtime): add address codecs in environment --- core/appmodule/v2/environment.go | 5 +++ go.mod | 1 + go.sum | 2 -- runtime/environment.go | 52 ++++++++++++++++++++++++++------ runtime/module.go | 5 +++ runtime/v2/module.go | 27 +++++++++++------ simapp/go.mod | 1 + simapp/go.sum | 2 -- tests/go.mod | 1 + tests/go.sum | 2 -- 10 files changed, 72 insertions(+), 26 deletions(-) diff --git a/core/appmodule/v2/environment.go b/core/appmodule/v2/environment.go index 450e10e162fe..0d22eff661d6 100644 --- a/core/appmodule/v2/environment.go +++ b/core/appmodule/v2/environment.go @@ -1,6 +1,7 @@ package appmodulev2 import ( + "cosmossdk.io/core/address" "cosmossdk.io/core/branch" "cosmossdk.io/core/event" "cosmossdk.io/core/gas" @@ -26,4 +27,8 @@ type Environment struct { KVStoreService store.KVStoreService MemStoreService store.MemoryStoreService + + AddressCodec address.Codec + ValidatorAddressCodec address.ValidatorAddressCodec + ConsensusAddressCodec address.ConsensusAddressCodec } diff --git a/go.mod b/go.mod index 0c54a37dd9d4..be772d6b4ac2 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 4508c5975094..0bcb76d15c4f 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/runtime/environment.go b/runtime/environment.go index cfe70414f169..907b807a815c 100644 --- a/runtime/environment.go +++ b/runtime/environment.go @@ -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" @@ -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 { @@ -63,6 +68,18 @@ func EnvWithMemStoreService(memStoreService store.MemoryStoreService) EnvOption } } +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 + } +} + // failingMsgRouter is a message router that panics when accessed // this is to ensure all fields are set in environment type failingMsgRouter struct { @@ -120,3 +137,18 @@ type failingMemStore struct { func (failingMemStore) OpenMemoryStore(context.Context) store.KVStore { panic("memory store not set") } + +// failingAddressCodec is an address codec that panics 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+" ") +} diff --git a/runtime/module.go b/runtime/module.go index 773efe10a3e2..da5ae191d824 100644 --- a/runtime/module.go +++ b/runtime/module.go @@ -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" @@ -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{} @@ -275,6 +279,7 @@ func ProvideEnvironment( EnvWithMsgRouterService(msgServiceRouter), EnvWithQueryRouterService(queryServiceRouter), EnvWithMemStoreService(memKvService), + EnvWithAddressCodecs(addressCodec, valAddressCodec, consAddressCodec), ) } diff --git a/runtime/v2/module.go b/runtime/v2/module.go index d4a9b4b534d9..fcdc01b61c55 100644 --- a/runtime/v2/module.go +++ b/runtime/v2/module.go @@ -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" @@ -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, @@ -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 diff --git a/simapp/go.mod b/simapp/go.mod index f6e537b4a73d..61eab8cbaabc 100644 --- a/simapp/go.mod +++ b/simapp/go.mod @@ -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 diff --git a/simapp/go.sum b/simapp/go.sum index 943624caf8b8..d5b01062aae1 100644 --- a/simapp/go.sum +++ b/simapp/go.sum @@ -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= diff --git a/tests/go.mod b/tests/go.mod index a2af50c06077..c800610105d8 100644 --- a/tests/go.mod +++ b/tests/go.mod @@ -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 diff --git a/tests/go.sum b/tests/go.sum index a0ab6f8190ac..76be3e0679c7 100644 --- a/tests/go.sum +++ b/tests/go.sum @@ -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= From af7776f06ef489091451165f91044b9f8b8b0086 Mon Sep 17 00:00:00 2001 From: Julien Robert Date: Wed, 25 Sep 2024 14:31:21 +0200 Subject: [PATCH 2/2] bot feedback Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- runtime/environment.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/runtime/environment.go b/runtime/environment.go index 907b807a815c..e375503d4eaf 100644 --- a/runtime/environment.go +++ b/runtime/environment.go @@ -68,6 +68,7 @@ func EnvWithMemStoreService(memStoreService store.MemoryStoreService) EnvOption } } +// EnvWithAddressCodecs sets the address codecs in the environment. func EnvWithAddressCodecs( addressCodec address.Codec, validatorAddressCodec address.ValidatorAddressCodec, @@ -138,7 +139,7 @@ func (failingMemStore) OpenMemoryStore(context.Context) store.KVStore { panic("memory store not set") } -// failingAddressCodec is an address codec that panics when accessed +// failingAddressCodec is an address codec that returns an error when accessed type failingAddressCodec struct { address.Codec