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: add core module API #11340

Closed
wants to merge 10 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
74 changes: 49 additions & 25 deletions api/cosmos/msg/v1/msg.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions api/cosmos/nft/v1beta1/query_grpc.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions api/cosmos/upgrade/v1beta1/query.pulsar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions core/Makefile
@@ -0,0 +1,6 @@
proto-gen:
(cd internal/example; buf generate)
# update-proto-pin
(cd internal/example; buf build --exclude-imports -o proto_image.bin.gz)

.PHONY: proto-gen
53 changes: 53 additions & 0 deletions core/app/handler.go
@@ -0,0 +1,53 @@
package app

import (
"context"

"google.golang.org/grpc"

"github.com/cosmos/cosmos-sdk/container"
)

func NewHandler() *Handler {
return &Handler{}
}

// Handler describes an ABCI app handler.
type Handler struct {
MsgServices []ServiceImpl
QueryServices []ServiceImpl

// BeginBlocker doesn't take or return any special arguments as this
// is the most stable across Tendermint versions and most common need
// for modules. Special parameters can be inspected and/or returned
// using custom hooks that the app will provide which may vary from
// one Tendermint release to another.
BeginBlocker func(context.Context) error
Comment on lines +20 to +25
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we get in the example an example of BeginBlocker? And those special hooks too, e.g. to access block info


// EndBlocker doesn't take or return any special arguments as this
// is the most stable across Tendermint versions and most common need
// for modules. Special parameters can be inspected and/or returned
// using custom hooks that the app will provide which may vary from
// one Tendermint release to another.
EndBlocker func(context.Context) error
}

// RegisterService registers a msg or query service. If the cosmos.msg.v1.service
// option is set true on the service, then it is registered as a msg service,
// otherwise it is registered as a query service.
func (h *Handler) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's unclear to me where baseapp (more specifically, the router and the middlewares) goes in core

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have some ideas but honestly I think I'll need to spike that part out a bit more to have a clear design

//TODO implement me
panic("implement me")
}

// ServiceImpl describes a gRPC service implementation to be registered with
// grpc.ServiceRegistrar.
type ServiceImpl struct {
Desc *grpc.ServiceDesc
Impl interface{}
}

func (h *Handler) IsOnePerModuleType() {}

var _ container.OnePerModuleType = &Handler{}
var _ grpc.ServiceRegistrar = &Handler{}
24 changes: 24 additions & 0 deletions core/blockinfo/service.go
@@ -0,0 +1,24 @@
package blockinfo

import (
"context"

"google.golang.org/protobuf/types/known/timestamppb"
)

// Service is a type which retrieves basic block info from a context independent
// of any specific Tendermint core version. Modules which need a specific
// Tendermint header should use a different service and should expect to need
// to update whenever Tendermint makes any changes.
type Service interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally in favor of a service approach here.

GetBlockInfo(ctx context.Context) BlockInfo
}

// BlockInfo represents basic block info independent of any specific Tendermint
// core version.
type BlockInfo interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why an interface instead of a concrete type? Since it is just a blob of information.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach allows avoiding extra allocation and copying when wrapping a block header

ChainID() string
Height() int64
Time() *timestamppb.Timestamp
Hash() []byte
}
29 changes: 29 additions & 0 deletions core/event/manager.go
@@ -0,0 +1,29 @@
package event

import (
"context"

"google.golang.org/protobuf/proto"
)

// Manager represents an event manager.
type Manager interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this a service too?

Copy link
Member Author

@aaronc aaronc Mar 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can always default to a no-op event sink

Emit(proto.Message) error
EmitLegacy(eventType string, attrs ...LegacyEventAttribute) error
}

// LegacyEventAttribute is a legacy (untyped) event attribute.
type LegacyEventAttribute struct {
Key, Value string
}

// GetManager will always return a non-nil event manager with a no-op event
// manager being returned if there is no manager in the context.
func GetManager(ctx context.Context) Manager {
panic("TODO")
}

// WithManager creates a new context with the provided event manager.
func WithManager(ctx context.Context, manager Manager) context.Context {
panic("TODO")
}
42 changes: 42 additions & 0 deletions core/gas/meter.go
@@ -0,0 +1,42 @@
package gas

import "context"

type Gas = uint64

// Meter represents a gas meter.
type Meter interface {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why isn't this a service too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same reason as events - we can default to no-op

GasConsumed() Gas
GasConsumedToLimit() Gas
GasRemaining() Gas
Limit() Gas
ConsumeGas(amount Gas, descriptor string)
RefundGas(amount Gas, descriptor string)
IsPastLimit() bool
IsOutOfGas() bool
String() string
}

// GetMeter returns the current transaction-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context a dummy instance
// will be returned.
func GetMeter(ctx context.Context) Meter {
panic("TODO")
}

// GetBlockMeter returns the current block-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context a dummy instance
// will be returned.
func GetBlockMeter(ctx context.Context) Meter {
panic("TODO")
}

// WithMeter returns a new context with the provided transaction-level gas meter.
func WithMeter(ctx context.Context, meter Meter) context.Context {
panic("TODO")
}

// WithMeter returns a new context with the provided block-level gas meter.
func WithBlockMeter(ctx context.Context, meter Meter) context.Context {
panic("TODO")
}
38 changes: 38 additions & 0 deletions core/go.mod
@@ -0,0 +1,38 @@
module github.com/cosmos/cosmos-sdk/core

go 1.17

require (
github.com/cosmos/cosmos-proto v1.0.0-alpha7
github.com/cosmos/cosmos-sdk/api v0.1.0-alpha5
github.com/cosmos/cosmos-sdk/container v1.0.0-alpha.2
github.com/gogo/protobuf v1.3.2
github.com/tendermint/tm-db v0.6.7
google.golang.org/grpc v1.44.0
google.golang.org/protobuf v1.27.1
)

require (
github.com/DataDog/zstd v1.4.1 // indirect
github.com/cespare/xxhash v1.1.0 // indirect
github.com/cosmos/gorocksdb v1.2.0 // indirect
github.com/dgraph-io/badger/v2 v2.2007.2 // indirect
github.com/dgraph-io/ristretto v0.0.3-0.20200630154024-f66de99634de // indirect
github.com/dgryski/go-farm v0.0.0-20190423205320-6a90982ecee2 // indirect
github.com/dustin/go-humanize v1.0.0 // indirect
github.com/fogleman/gg v1.3.0 // indirect
github.com/goccy/go-graphviz v0.0.9 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/jmhodges/levigo v1.0.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/syndtr/goleveldb v1.0.1-0.20200815110645-5c35d600f0ca // indirect
go.etcd.io/bbolt v1.3.6 // indirect
golang.org/x/image v0.0.0-20200119044424-58c23975cae1 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb // indirect
)