Skip to content

Commit

Permalink
refactor: zerodep core (#21239)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle authored Aug 12, 2024
1 parent fba4dac commit 810a542
Show file tree
Hide file tree
Showing 89 changed files with 165 additions and 373 deletions.
2 changes: 1 addition & 1 deletion client/v2/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ require (
github.com/cosmos/crypto v0.1.2 // indirect
github.com/cosmos/go-bip39 v1.0.0 // indirect
github.com/cosmos/gogogateway v1.2.0 // indirect
github.com/cosmos/gogoproto v1.6.0
github.com/cosmos/gogoproto v1.6.1-0.20240809124342-d6a57064ada0
github.com/cosmos/iavl v1.2.1-0.20240725141113-7adc688cf179 // indirect
github.com/cosmos/ics23/go v0.10.0 // indirect
github.com/cosmos/ledger-cosmos-go v0.13.3 // indirect
Expand Down
4 changes: 2 additions & 2 deletions client/v2/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,8 @@ github.com/cosmos/go-bip39 v1.0.0/go.mod h1:RNJv0H/pOIVgxw6KS7QeX2a0Uo0aKUlfhZ4x
github.com/cosmos/gogogateway v1.2.0 h1:Ae/OivNhp8DqBi/sh2A8a1D0y638GpL3tkmLQAiKxTE=
github.com/cosmos/gogogateway v1.2.0/go.mod h1:iQpLkGWxYcnCdz5iAdLcRBSw3h7NXeOkZ4GUkT+tbFI=
github.com/cosmos/gogoproto v1.4.2/go.mod h1:cLxOsn1ljAHSV527CHOtaIP91kK6cCrZETRBrkzItWU=
github.com/cosmos/gogoproto v1.6.0 h1:Xm0F/96O5Ox4g6xGgjA41rWaaPjYtOdTi59uBcV2qEE=
github.com/cosmos/gogoproto v1.6.0/go.mod h1:Y+g956rcUf2vr4uwtCcK/1Xx9BWVluCtcI9vsh0GHmk=
github.com/cosmos/gogoproto v1.6.1-0.20240809124342-d6a57064ada0 h1:qZdcY4sKyAnaUwFRtSKkk9YdeMuQixWxLagI/8jhJo4=
github.com/cosmos/gogoproto v1.6.1-0.20240809124342-d6a57064ada0/go.mod h1:Y+g956rcUf2vr4uwtCcK/1Xx9BWVluCtcI9vsh0GHmk=
github.com/cosmos/iavl v1.2.1-0.20240725141113-7adc688cf179 h1:wmwDn7V3RodN9auB3FooSQxs46nHVE3u0mb87TJkZFE=
github.com/cosmos/iavl v1.2.1-0.20240725141113-7adc688cf179/go.mod h1:GiM43q0pB+uG53mLxLDzimxM9l/5N9UuSY3/D0huuVw=
github.com/cosmos/ics23/go v0.10.0 h1:iXqLLgp2Lp+EdpIuwXTYIQU+AiHj9mOC2X9ab++bZDM=
Expand Down
4 changes: 0 additions & 4 deletions collections/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,9 @@ require (
)

require (
github.com/cosmos/gogoproto v1.6.0 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tidwall/btree v1.7.0 // indirect
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 // indirect
google.golang.org/protobuf v1.34.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

Expand Down
10 changes: 0 additions & 10 deletions collections/go.sum
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
github.com/cosmos/gogoproto v1.6.0 h1:Xm0F/96O5Ox4g6xGgjA41rWaaPjYtOdTi59uBcV2qEE=
github.com/cosmos/gogoproto v1.6.0/go.mod h1:Y+g956rcUf2vr4uwtCcK/1Xx9BWVluCtcI9vsh0GHmk=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/tidwall/btree v1.7.0 h1:L1fkJH/AuEh5zBnnBbmTwQ5Lt+bRJ5A8EWecslvo9iI=
github.com/tidwall/btree v1.7.0/go.mod h1:twD9XRA5jj9VUQGELzDO4HPQTNJsoWWfYEL+EUQ2cKY=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225 h1:LfspQV/FYTatPTr/3HzIcmiUFH7PGP+OQ6mgDYo3yuQ=
golang.org/x/exp v0.0.0-20240222234643-814bf88cf225/go.mod h1:CxmFvTBINI24O/j8iY7H1xHzx2i4OsyguNBmN/uPtqc=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
13 changes: 8 additions & 5 deletions core/app/codec.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
package app

import (
"github.com/cosmos/gogoproto/jsonpb"
gogoproto "github.com/cosmos/gogoproto/proto"
"cosmossdk.io/core/transaction"
)

// MsgInterfaceProtoName defines the protobuf name of the cosmos Msg interface
const MsgInterfaceProtoName = "cosmos.base.v1beta1.Msg"

type ProtoCodec interface {
Marshal(v gogoproto.Message) ([]byte, error)
Unmarshal(data []byte, v gogoproto.Message) error
Marshal(v transaction.Msg) ([]byte, error)
Unmarshal(data []byte, v transaction.Msg) error
Name() string
}

type InterfaceRegistry interface {
jsonpb.AnyResolver
AnyResolver
ListImplementations(ifaceTypeURL string) []string
ListAllInterfaces() []string
}

type AnyResolver = interface {
Resolve(typeUrl string) (transaction.Msg, error)
}
94 changes: 5 additions & 89 deletions core/appmodule/v2/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,104 +2,20 @@ package appmodule

import (
"context"
"fmt"

transaction "cosmossdk.io/core/transaction"
)

type (
// PreMsgHandler is a handler that is executed before Handler. If it errors the execution reverts.
PreMsgHandler = func(ctx context.Context, msg Message) error
PreMsgHandler = func(ctx context.Context, msg transaction.Msg) error
// Handler handles the state transition of the provided message.
Handler = func(ctx context.Context, msg Message) (msgResp Message, err error)
Handler = func(ctx context.Context, msg transaction.Msg) (msgResp transaction.Msg, err error)
// PostMsgHandler runs after Handler, only if Handler does not error. If PostMsgHandler errors
// then the execution is reverted.
PostMsgHandler = func(ctx context.Context, msg, msgResp Message) error
PostMsgHandler = func(ctx context.Context, msg, msgResp transaction.Msg) error
)

// RegisterHandler is a helper function that modules can use to not lose type safety when registering handlers to the
// QueryRouter or MsgRouter. Example usage:
// ```go
//
// func (k Keeper) QueryBalance(ctx context.Context, req *types.QueryBalanceRequest) (*types.QueryBalanceResponse, error) {
// ... query logic ...
// }
//
// func (m Module) RegisterQueryHandlers(router appmodule.QueryRouter) {
// appmodule.RegisterHandler(router, keeper.QueryBalance)
// }
//
// ```
func RegisterHandler[R interface{ Register(string, Handler) }, Req, Resp Message](
router R,
handler func(ctx context.Context, msg Req) (msgResp Resp, err error),
) {
untypedHandler := func(ctx context.Context, m Message) (Message, error) {
typed, ok := m.(Req)
if !ok {
return nil, fmt.Errorf("unexpected type %T, wanted: %T", m, *new(Req))
}
return handler(ctx, typed)
}
router.Register(messageName[Req](), untypedHandler)
}

// RegisterPreHandler is a helper function that modules can use to not lose type safety when registering PreMsgHandler to the
// PreMsgRouter. Example usage:
// ```go
//
// func (k Keeper) BeforeSend(ctx context.Context, req *types.MsgSend) (*types.QueryBalanceResponse, error) {
// ... before send logic ...
// }
//
// func (m Module) RegisterPreMsgHandlers(router appmodule.PreMsgRouter) {
// appmodule.RegisterPreHandler(router, keeper.BeforeSend)
// }
//
// ```
func RegisterPreHandler[Req Message](
router PreMsgRouter,
handler func(ctx context.Context, msg Req) error,
) {
untypedHandler := func(ctx context.Context, m Message) error {
typed, ok := m.(Req)
if !ok {
return fmt.Errorf("unexpected type %T, wanted: %T", m, *new(Req))
}
return handler(ctx, typed)
}
router.Register(messageName[Req](), untypedHandler)
}

// RegisterPostHandler is a helper function that modules can use to not lose type safety when registering handlers to the
// PostMsgRouter. Example usage:
// ```go
//
// func (k Keeper) AfterSend(ctx context.Context, req *types.MsgSend, resp *types.MsgSendResponse) error {
// ... query logic ...
// }
//
// func (m Module) RegisterPostMsgHandlers(router appmodule.PostMsgRouter) {
// appmodule.RegisterPostHandler(router, keeper.AfterSend)
// }
//
// ```
func RegisterPostHandler[Req, Resp Message](
router PostMsgRouter,
handler func(ctx context.Context, msg Req, msgResp Resp) error,
) {
untypedHandler := func(ctx context.Context, m, mResp Message) error {
typed, ok := m.(Req)
if !ok {
return fmt.Errorf("unexpected type %T, wanted: %T", m, *new(Req))
}
typedResp, ok := mResp.(Resp)
if !ok {
return fmt.Errorf("unexpected type %T, wanted: %T", m, *new(Resp))
}
return handler(ctx, typed, typedResp)
}
router.Register(messageName[Req](), untypedHandler)
}

// msg handler

type PreMsgRouter interface {
Expand Down
16 changes: 0 additions & 16 deletions core/appmodule/v2/message.go

This file was deleted.

4 changes: 2 additions & 2 deletions core/event/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package event
import (
"context"

gogoproto "github.com/cosmos/gogoproto/proto"
"cosmossdk.io/core/transaction"
)

// Service represents an event service which can retrieve and set an event manager in a context.
Expand All @@ -19,7 +19,7 @@ type Manager interface {
// Emit emits events represented as a protobuf message (as described in ADR 032).
//
// Callers SHOULD assume that these events will not be included in consensus.
Emit(event gogoproto.Message) error
Emit(event transaction.Msg) error

// EmitKV emits an event based on an event and kv-pair attributes.
//
Expand Down
12 changes: 3 additions & 9 deletions core/go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
module cosmossdk.io/core

go 1.20

require github.com/cosmos/gogoproto v1.6.0
// Core is meant to have zero dependencies, so we can use it as a dependency
// in other modules without having to worry about circular dependencies.

require (
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/go-cmp v0.6.0 // indirect
golang.org/x/exp v0.0.0-20231006140011-7918f672742d // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
go 1.20

// Version tagged too early and incompatible with v0.50 (latest at the time of tagging)
retract v0.12.0
10 changes: 0 additions & 10 deletions core/go.sum
Original file line number Diff line number Diff line change
@@ -1,10 +0,0 @@
github.com/cosmos/gogoproto v1.6.0 h1:Xm0F/96O5Ox4g6xGgjA41rWaaPjYtOdTi59uBcV2qEE=
github.com/cosmos/gogoproto v1.6.0/go.mod h1:Y+g956rcUf2vr4uwtCcK/1Xx9BWVluCtcI9vsh0GHmk=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d h1:jtJma62tbqLibJ5sFQz8bKtEM8rJBtfilJ2qTU199MI=
golang.org/x/exp v0.0.0-20231006140011-7918f672742d/go.mod h1:ldy0pHrwJyGW56pPQzzkH36rKxoZW1tw7ZJpeKx+hdo=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
6 changes: 3 additions & 3 deletions core/registry/legacy.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package registry

import (
gogoproto "github.com/cosmos/gogoproto/proto"
"cosmossdk.io/core/transaction"
)

type InterfaceRegistrar interface {
Expand All @@ -14,12 +14,12 @@ type InterfaceRegistrar interface {
//
// Ex:
// registry.RegisterInterface("cosmos.base.v1beta1.Msg", (*sdk.Msg)(nil))
RegisterInterface(protoName string, iface interface{}, impls ...gogoproto.Message)
RegisterInterface(protoName string, iface interface{}, impls ...transaction.Msg)

// RegisterImplementations registers impls as concrete implementations of
// the interface iface.
//
// Ex:
// registry.RegisterImplementations((*sdk.Msg)(nil), &MsgSend{}, &MsgMultiSend{})
RegisterImplementations(iface interface{}, impls ...gogoproto.Message)
RegisterImplementations(iface interface{}, impls ...transaction.Msg)
}
8 changes: 4 additions & 4 deletions core/router/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package router
import (
"context"

gogoproto "github.com/cosmos/gogoproto/proto"
"cosmossdk.io/core/transaction"
)

// Service is the interface that wraps the basic methods for a router.
Expand All @@ -12,7 +12,7 @@ type Service interface {
// CanInvoke returns an error if the given request cannot be invoked.
CanInvoke(ctx context.Context, typeURL string) error
// InvokeTyped execute a message or query. It should be used when the called knows the type of the response.
InvokeTyped(ctx context.Context, req, res gogoproto.Message) error
// InvokeUntyped execute a message or query. It should be used when the called doesn't know the type of the response.
InvokeUntyped(ctx context.Context, req gogoproto.Message) (res gogoproto.Message, err error)
InvokeTyped(ctx context.Context, req, res transaction.Msg) error
// InvokeUntyped execute a Msg or query. It should be used when the called doesn't know the type of the response.
InvokeUntyped(ctx context.Context, req transaction.Msg) (res transaction.Msg, err error)
}
7 changes: 3 additions & 4 deletions core/testing/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ package coretesting
import (
"context"

gogoproto "github.com/cosmos/gogoproto/proto"

"cosmossdk.io/core/event"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
)

type dummyKey struct{}
Expand All @@ -15,7 +14,7 @@ func Context() context.Context {
dummy := &dummyCtx{
stores: map[string]store.KVStore{},
events: map[string][]event.Event{},
protoEvents: map[string][]gogoproto.Message{},
protoEvents: map[string][]transaction.Msg{},
}

ctx := context.WithValue(context.Background(), dummyKey{}, dummy)
Expand All @@ -28,7 +27,7 @@ type dummyCtx struct {
// maps event emitted by the actor.
events map[string][]event.Event
// maps proto events emitted by the actor.
protoEvents map[string][]gogoproto.Message
protoEvents map[string][]transaction.Msg
}

func unwrap(ctx context.Context) *dummyCtx {
Expand Down
7 changes: 3 additions & 4 deletions core/testing/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package coretesting
import (
"context"

gogoproto "github.com/cosmos/gogoproto/proto"

"cosmossdk.io/core/event"
"cosmossdk.io/core/transaction"
)

var _ event.Service = (*MemEventsService)(nil)
Expand All @@ -30,7 +29,7 @@ func (e MemEventsService) GetEvents(ctx context.Context) []event.Event {
return unwrap(ctx).events[e.moduleName]
}

func (e MemEventsService) GetProtoEvents(ctx context.Context) []gogoproto.Message {
func (e MemEventsService) GetProtoEvents(ctx context.Context) []transaction.Msg {
return unwrap(ctx).protoEvents[e.moduleName]
}

Expand All @@ -39,7 +38,7 @@ type eventManager struct {
ctx *dummyCtx
}

func (e eventManager) Emit(event gogoproto.Message) error {
func (e eventManager) Emit(event transaction.Msg) error {
e.ctx.protoEvents[e.moduleName] = append(e.ctx.protoEvents[e.moduleName], event)
return nil
}
Expand Down
Loading

0 comments on commit 810a542

Please sign in to comment.