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 #12239

Closed
wants to merge 50 commits into from
Closed
Show file tree
Hide file tree
Changes from 21 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
c59fef9
feat: add core module API
aaronc Mar 9, 2022
8251594
docs
aaronc Mar 9, 2022
204cd7a
add full example setup
aaronc Mar 9, 2022
afcdbcd
docs
aaronc Mar 9, 2022
964f481
docs
aaronc Mar 9, 2022
80fb734
docs
aaronc Mar 9, 2022
91c7b8f
Merge branch 'master' into aaronc/core-api
aaronc Mar 9, 2022
40d493a
add extension resolver
aaronc Mar 10, 2022
b432e37
Merge remote-tracking branch 'origin/aaronc/core-api' into aaronc/cor…
aaronc Mar 10, 2022
963bbae
update proto
aaronc Mar 10, 2022
81049cd
Merge branch 'main' of github.com:cosmos/cosmos-sdk into aaronc/core-api
aaronc Jun 13, 2022
68abaa7
revert
aaronc Jun 13, 2022
8ade043
updates
aaronc Jun 13, 2022
4de0773
docs
aaronc Jun 13, 2022
024f366
docs
aaronc Jun 13, 2022
b7e175b
Merge branch 'main' into aaronc/core-api
aaronc Jun 13, 2022
33f99b2
Merge branch 'main' into aaronc/core-api
tac0turtle Jun 14, 2022
8e0f531
Update core/blockinfo/service.go
aaronc Jun 14, 2022
3c6b412
Update core/blockinfo/service.go
aaronc Jun 14, 2022
25acd02
add ValidatorUpdateService
aaronc Jun 14, 2022
807c2e0
Merge remote-tracking branch 'origin/aaronc/core-api' into aaronc/cor…
aaronc Jun 14, 2022
043b6fb
Merge branch 'main' into aaronc/core-api
aaronc Jun 14, 2022
7f9b15c
updates
aaronc Aug 19, 2022
da5e6c2
updates
aaronc Aug 19, 2022
be1e98c
Merge remote-tracking branch 'origin/aaronc/core-api' into aaronc/cor…
aaronc Aug 19, 2022
a6d2531
Merge branch 'main' of github.com:cosmos/cosmos-sdk into aaronc/core-api
aaronc Aug 19, 2022
daac685
revert
aaronc Aug 19, 2022
b2e0881
Update core/gas/meter.go
aaronc Aug 19, 2022
fb3a5e6
revert
aaronc Aug 19, 2022
1081d45
Apply suggestions from code review
aaronc Aug 19, 2022
f9d2522
Apply suggestions from code review
aaronc Aug 19, 2022
3cbe6c5
Merge remote-tracking branch 'origin/aaronc/core-api' into aaronc/cor…
aaronc Aug 19, 2022
dfe781d
API simplifications
aaronc Aug 29, 2022
2635c0b
event updates
aaronc Aug 29, 2022
6a6fc51
ADR-033 updates
aaronc Aug 29, 2022
ac62df1
updates client
aaronc Aug 29, 2022
260077d
WIP on example
aaronc Sep 8, 2022
3ccd485
Merge branch 'main' of github.com:cosmos/cosmos-sdk into aaronc/core-api
aaronc Oct 10, 2022
5328e62
fix import
aaronc Oct 10, 2022
002d9d2
buf mod update
aaronc Oct 10, 2022
b1ae3f9
Merge branch 'main' of github.com:cosmos/cosmos-sdk into aaronc/core-api
aaronc Oct 10, 2022
f9a903d
fix tests
aaronc Oct 10, 2022
481d107
revert
aaronc Oct 10, 2022
6db3f0a
remove empty file
aaronc Oct 10, 2022
0438fdc
Merge branch 'main' into aaronc/core-api
alexanderbez Oct 12, 2022
8b27a49
Merge branch 'main' into aaronc/core-api
aaronc Oct 12, 2022
fdc9933
updates
aaronc Oct 20, 2022
f5dd33a
update core API to extension interfaces
aaronc Oct 20, 2022
0c28208
Merge remote-tracking branch 'origin/aaronc/core-api' into aaronc/cor…
aaronc Oct 20, 2022
6e5b9c7
update core API to extension interfaces
aaronc Oct 20, 2022
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
69 changes: 46 additions & 23 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.

53 changes: 53 additions & 0 deletions core/appmodule/handler.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package appmodule

import (
"context"

"google.golang.org/grpc"

"github.com/cosmos/cosmos-sdk/depinject"
aaronc marked this conversation as resolved.
Show resolved Hide resolved
)

// Handler describes an ABCI app module handler. It can be injected into a
// depinject container as a one-per-module type (in the pointer variant).
aaronc marked this conversation as resolved.
Show resolved Hide resolved
type Handler struct {
// Services are the msg and query services for the module. Msg services
// must be annotated with the option cosmos.msg.v1.service = true.
Services []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
aaronc marked this conversation as resolved.
Show resolved Hide resolved
// using custom hooks that the app will provide which may vary from
// one Tendermint release to another.
Copy link
Collaborator

Choose a reason for hiding this comment

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

maybe you can mention some hooks here, or point where these hooks are defined?

Copy link
Member Author

@aaronc aaronc Aug 19, 2022

Choose a reason for hiding this comment

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

I added an example with the ValidatorUpdateService in this PR and described this more in the ADR

BeginBlocker func(context.Context) error

// 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
aaronc marked this conversation as resolved.
Show resolved Hide resolved
// using custom hooks that the app will provide which may vary from
// one Tendermint release to another.
EndBlocker func(context.Context) error
tac0turtle marked this conversation as resolved.
Show resolved Hide resolved
}

// 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,
aaronc marked this conversation as resolved.
Show resolved Hide resolved
// otherwise it is registered as a query service.
func (h *Handler) RegisterService(desc *grpc.ServiceDesc, impl interface{}) {
h.Services = append(h.Services, ServiceImpl{
Desc: desc,
Impl: impl,
})
}

// 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 _ depinject.OnePerModuleType = &Handler{}
var _ grpc.ServiceRegistrar = &Handler{}
37 changes: 37 additions & 0 deletions core/blockinfo/service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// Package blockinfo provides an API for app modules to get basic
// information about blocks that is available against any underlying Tendermint
// core version (or other consensus layer that could be used in the future).
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
aaronc marked this conversation as resolved.
Show resolved Hide resolved
// to update whenever Tendermint makes any changes. blockinfo.Service is a
// core API type that should be provided by the runtime module being used to
// build an app via depinject.
type Service interface {
// GetBlockInfo returns the current block info for the context.
GetBlockInfo(ctx context.Context) BlockInfo
}

// BlockInfo represents basic block info independent of any specific Tendermint
// core version.
type BlockInfo interface {
// ChainID returns the chain ID.
ChainID() string

// Height returns the current block height.
Height() int64

// Time returns the current block timestamp.
Time() *timestamppb.Timestamp

// Hash returns the current block hash.
Hash() []byte
}
8 changes: 8 additions & 0 deletions core/compat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package core

// RuntimeCompatibilityVersion indicates what semantic version of the runtime
// module is required to support the full API exposed in this version of core.
// The runtime module that is loaded can emit an error or warning if the version
// specified here is greater than what that runtime can support and some
// features may not work as expected.
const RuntimeCompatibilityVersion = 1
34 changes: 34 additions & 0 deletions core/event/manager.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Package event provides a basic API for app modules to emit events.
package event

import (
"context"

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

// Service represents an event service which can retrieve and set an event manager in a context.
// event.Service is a core API type that should be provided by the runtime module being used to
// build an app via depinject.
type Service interface {
// GetManager returns the event manager for the context or a no-op event manager if one isn't attached.
GetManager(context.Context) Manager

// WithManager returns a new context with the provided event manager attached.
WithManager(context.Context, Manager) context.Context
}

// Manager represents an event manager.
type Manager interface {

// Emit emits a typed protobuf event.
Emit(proto.Message) error

// EmitLegacy emits a legacy (untyped) tendermint event.
EmitLegacy(eventType string, attrs ...LegacyEventAttribute) error
}

// LegacyEventAttribute is a legacy (untyped) event attribute.
type LegacyEventAttribute struct {
Key, Value string
}
40 changes: 40 additions & 0 deletions core/gas/meter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Package gas provides a basic API for app modules to track gas usage.
package gas

import "context"

type Gas = uint64

// Service represents a gas service which can retrieve and set a gas meter in a context.
// gas.Service is a core API type that should be provided by the runtime module being used to
// build an app via depinject.
type Service interface {
// GetMeter returns the current transaction-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context an infinite gas meter
// will be returned.
GetMeter(context.Context)

// GetBlockMeter returns the current block-level gas meter. A non-nil meter
// is always returned. When one is unavailable in the context an infinite gas meter
// will be returned.
GetBlockMeter(context.Context)

// WithMeter returns a new context with the provided transaction-level gas meter.
WithMeter(ctx context.Context, meter Meter) context.Context

// WithMeter returns a new context with the provided block-level gas meter.
aaronc marked this conversation as resolved.
Show resolved Hide resolved
WithBlockMeter(ctx context.Context, meter Meter) context.Context
}

// Meter represents a gas meter.
type Meter interface {
GasConsumed() Gas
GasConsumedToLimit() Gas
GasRemaining() Gas
Limit() Gas
ConsumeGas(amount Gas, descriptor string)
RefundGas(amount Gas, descriptor string)
IsPastLimit() bool
IsOutOfGas() bool
String() string
}
18 changes: 15 additions & 3 deletions core/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,35 @@ require (
cosmossdk.io/api v0.1.0-alpha8
github.com/cosmos/cosmos-proto v1.0.0-alpha7
github.com/cosmos/cosmos-sdk/depinject v1.0.0-alpha.4
github.com/gogo/protobuf v1.3.2
github.com/tendermint/tm-db v0.6.7
google.golang.org/grpc v1.46.2
google.golang.org/protobuf v1.28.0
gotest.tools/v3 v3.2.0
sigs.k8s.io/yaml v1.3.0
)

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/golang/protobuf v1.5.2 // indirect
github.com/golang/snappy v0.0.1 // indirect
github.com/google/btree v1.0.0 // indirect
github.com/google/go-cmp v0.5.6 // indirect
github.com/kr/pretty v0.1.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/exp v0.0.0-20220428152302-39d4317da171 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654 // indirect
golang.org/x/text v0.3.5 // indirect
google.golang.org/genproto v0.0.0-20211223182754-3ac035c7e7cb // indirect
google.golang.org/grpc v1.46.2 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)

Expand Down
Loading