Skip to content

Commit

Permalink
refactor(core): abci 2.0 integration (#15960)
Browse files Browse the repository at this point in the history
  • Loading branch information
tac0turtle committed May 1, 2023
1 parent 9167fbc commit 279c7af
Show file tree
Hide file tree
Showing 30 changed files with 181 additions and 441 deletions.
9 changes: 6 additions & 3 deletions client/grpc/cmtservice/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ var (
)

type (
abciQueryFn = func(abci.RequestQuery) abci.ResponseQuery
abciQueryFn = func(context.Context, *abci.RequestQuery) (*abci.ResponseQuery, error)

queryServer struct {
clientCtx client.Context
Expand Down Expand Up @@ -218,7 +218,7 @@ func (s queryServer) GetNodeInfo(ctx context.Context, _ *GetNodeInfoRequest) (*G
return &resp, nil
}

func (s queryServer) ABCIQuery(_ context.Context, req *ABCIQueryRequest) (*ABCIQueryResponse, error) {
func (s queryServer) ABCIQuery(ctx context.Context, req *ABCIQueryRequest) (*ABCIQueryResponse, error) {
if s.queryFn == nil {
return nil, status.Error(codes.Internal, "ABCI Query handler undefined")
}
Expand All @@ -241,7 +241,10 @@ func (s queryServer) ABCIQuery(_ context.Context, req *ABCIQueryRequest) (*ABCIQ
}
}

res := s.queryFn(req.ToABCIRequestQuery())
res, err := s.queryFn(ctx, req.ToABCIRequestQuery())
if err != nil {
return nil, err
}
return FromABCIResponseQuery(res), nil
}

Expand Down
6 changes: 3 additions & 3 deletions client/grpc/cmtservice/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (

// ToABCIRequestQuery converts a gRPC ABCIQueryRequest type to an ABCI
// RequestQuery type.
func (req *ABCIQueryRequest) ToABCIRequestQuery() abci.RequestQuery {
return abci.RequestQuery{
func (req *ABCIQueryRequest) ToABCIRequestQuery() *abci.RequestQuery {
return &abci.RequestQuery{
Data: req.Data,
Path: req.Path,
Height: req.Height,
Expand All @@ -17,7 +17,7 @@ func (req *ABCIQueryRequest) ToABCIRequestQuery() abci.RequestQuery {

// FromABCIResponseQuery converts an ABCI ResponseQuery type to a gRPC
// ABCIQueryResponse type.
func FromABCIResponseQuery(res abci.ResponseQuery) *ABCIQueryResponse {
func FromABCIResponseQuery(res *abci.ResponseQuery) *ABCIQueryResponse {
var proofOps *ProofOps

if res.ProofOps != nil {
Expand Down
8 changes: 4 additions & 4 deletions runtime/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,17 @@ func (a *App) Load(loadLatest bool) error {
}

// BeginBlocker application updates every begin block
func (a *App) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) (abci.ResponseBeginBlock, error) {
return a.ModuleManager.BeginBlock(ctx, req)
func (a *App) BeginBlocker(ctx sdk.Context) (sdk.BeginBlock, error) {
return a.ModuleManager.BeginBlock(ctx)
}

// EndBlocker application updates every end block
func (a *App) EndBlocker(ctx sdk.Context) ([]abci.ValidatorUpdate, []abci.Event, error) {
func (a *App) EndBlocker(ctx sdk.Context) (sdk.EndBlock, error) {
return a.ModuleManager.EndBlock(ctx)
}

// InitChainer initializes the chain.
func (a *App) InitChainer(ctx sdk.Context, req abci.RequestInitChain) (abci.ResponseInitChain, error) {
func (a *App) InitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
var genesisState map[string]json.RawMessage
if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
Expand Down
14 changes: 6 additions & 8 deletions runtime/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import (
"google.golang.org/protobuf/reflect/protodesc"
"google.golang.org/protobuf/reflect/protoregistry"

abci "github.com/cometbft/cometbft/abci/types"

runtimev1alpha1 "cosmossdk.io/api/cosmos/app/runtime/v1alpha1"
appv1alpha1 "cosmossdk.io/api/cosmos/app/v1alpha1"
"cosmossdk.io/core/appmodule"
Expand Down Expand Up @@ -60,7 +58,7 @@ func init() {
ProvideKVStoreKey,
ProvideTransientStoreKey,
ProvideMemoryStoreKey,
ProvideDeliverTx,
// ProvideDeliverTx,
ProvideKVStoreService,
ProvideMemoryStoreService,
ProvideTransientStoreService,
Expand Down Expand Up @@ -185,11 +183,11 @@ func ProvideMemoryStoreKey(key depinject.ModuleKey, app *AppBuilder) *storetypes
return storeKey
}

func ProvideDeliverTx(appBuilder *AppBuilder) func(abci.RequestDeliverTx) abci.ResponseDeliverTx {
return func(tx abci.RequestDeliverTx) abci.ResponseDeliverTx {
return appBuilder.app.BaseApp.DeliverTx(tx)
}
}
// func ProvideDeliverTx(appBuilder *AppBuilder) func(abci.RequestDeliverTx) abci.ResponseDeliverTx { // TODO: what to do here?
// return func(tx abci.RequestDeliverTx) abci.ResponseDeliverTx {
// return appBuilder.app.BaseApp.DeliverTx(tx)
// }
// }

func ProvideKVStoreService(config *runtimev1alpha1.Module, key depinject.ModuleKey, app *AppBuilder) store.KVStoreService {
storeKey := ProvideKVStoreKey(config, key, app)
Expand Down
6 changes: 3 additions & 3 deletions runtime/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ type AppI interface {
LegacyAmino() *codec.LegacyAmino

// Application updates every begin block.
BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) (abci.ResponseBeginBlock, error)
BeginBlocker(ctx sdk.Context) (sdk.BeginBlock, error)

// Application updates every end block.
EndBlocker(ctx sdk.Context) ([]abci.ValidatorUpdate, []abci.Event, error)
EndBlocker(ctx sdk.Context) (sdk.EndBlock, error)

// Application update at chain (i.e app) initialization.
InitChainer(ctx sdk.Context, req abci.RequestInitChain) (abci.ResponseInitChain, error)
InitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error)

// Loads the app at a given height.
LoadHeight(height int64) error
Expand Down
2 changes: 1 addition & 1 deletion server/api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (s *Server) Start(ctx context.Context, cfg config.Config) error {
cmtCfg.WriteTimeout = time.Duration(cfg.API.RPCWriteTimeout) * time.Second
cmtCfg.MaxBodyBytes = int64(cfg.API.RPCMaxBodyBytes)

listener, err := tmrpcserver.Listen(cfg.API.Address, cmtCfg)
listener, err := tmrpcserver.Listen(cfg.API.Address, cmtCfg.MaxOpenConnections)
if err != nil {
s.mtx.Unlock()
return err
Expand Down
8 changes: 4 additions & 4 deletions server/mock/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,21 @@ type GenesisJSON struct {

// InitChainer returns a function that can initialize the chain
// with key/value pairs
func InitChainer(key storetypes.StoreKey) func(sdk.Context, abci.RequestInitChain) (abci.ResponseInitChain, error) {
return func(ctx sdk.Context, req abci.RequestInitChain) (abci.ResponseInitChain, error) {
func InitChainer(key storetypes.StoreKey) func(sdk.Context, *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
return func(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
stateJSON := req.AppStateBytes

genesisState := new(GenesisJSON)
err := json.Unmarshal(stateJSON, genesisState)
if err != nil {
return abci.ResponseInitChain{}, err
return &abci.ResponseInitChain{}, err
}

for _, val := range genesisState.Values {
store := ctx.KVStore(key)
store.Set([]byte(val.Key), []byte(val.Value))
}
return abci.ResponseInitChain{}, nil
return &abci.ResponseInitChain{}, nil
}
}

Expand Down
32 changes: 16 additions & 16 deletions server/mock/app_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package mock

import (
"context"
"math/rand"
"testing"
"time"

"cosmossdk.io/log"
abci "github.com/cometbft/cometbft/abci/types"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
"github.com/stretchr/testify/require"

simtypes "github.com/cosmos/cosmos-sdk/types/simulation"
Expand Down Expand Up @@ -38,16 +38,17 @@ func TestInitApp(t *testing.T) {
req := abci.RequestInitChain{
AppStateBytes: appState,
}
app.InitChain(req)
app.Commit()
app.InitChain(context.TODO(), &req)
app.Commit(context.TODO(), &abci.RequestCommit{})

// make sure we can query these values
query := abci.RequestQuery{
Path: "/store/main/key",
Data: []byte("foo"),
}

qres := app.Query(query)
qres, err := app.Query(context.TODO(), &query)
require.NoError(t, err)
require.Equal(t, uint32(0), qres.Code, qres.Log)
require.Equal(t, []byte("bar"), qres.Value)
}
Expand All @@ -64,26 +65,25 @@ func TestDeliverTx(t *testing.T) {
tx := NewTx(key, value, randomAccounts[0].Address)
txBytes := tx.GetSignBytes()

app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{
AppHash: []byte("apphash"),
Height: 1,
}})

dres := app.DeliverTx(abci.RequestDeliverTx{Tx: txBytes})
require.Equal(t, uint32(0), dres.Code, dres.Log)

app.EndBlock(abci.RequestEndBlock{})
res, err := app.FinalizeBlock(context.TODO(), &abci.RequestFinalizeBlock{
Hash: []byte("apphash"),
Height: 1,
Txs: [][]byte{txBytes},
})
require.NoError(t, err)
require.NotEmpty(t, res.AppHash)

cres := app.Commit()
require.NotEmpty(t, cres.Data)
_, err = app.Commit(context.TODO(), &abci.RequestCommit{})
require.NoError(t, err)

// make sure we can query these values
query := abci.RequestQuery{
Path: "/store/main/key",
Data: []byte(key),
}

qres := app.Query(query)
qres, err := app.Query(context.TODO(), &query)
require.NoError(t, err)
require.Equal(t, uint32(0), qres.Code, qres.Log)
require.Equal(t, []byte(value), qres.Value)
}
3 changes: 2 additions & 1 deletion server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/armon/go-metrics"
"github.com/cometbft/cometbft/abci/server"
cmtcmd "github.com/cometbft/cometbft/cmd/cometbft/commands"
cmtcfg "github.com/cometbft/cometbft/config"
"github.com/cometbft/cometbft/node"
"github.com/cometbft/cometbft/p2p"
pvm "github.com/cometbft/cometbft/privval"
Expand Down Expand Up @@ -327,7 +328,7 @@ func startInProcess(svrCtx *Context, clientCtx client.Context, appCreator types.
nodeKey,
proxy.NewLocalClientCreator(app),
genDocProvider,
node.DefaultDBProvider,
cmtcfg.DefaultDBProvider,
node.DefaultMetricsProvider(cfg.Instrumentation),
servercmtlog.CometZeroLogWrapper{Logger: svrCtx.Logger},
)
Expand Down
10 changes: 5 additions & 5 deletions simapp/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,21 +564,21 @@ func (app *SimApp) setPostHandler() {
func (app *SimApp) Name() string { return app.BaseApp.Name() }

// BeginBlocker application updates every begin block
func (app *SimApp) BeginBlocker(ctx sdk.Context, req abci.RequestBeginBlock) (abci.ResponseBeginBlock, error) {
return app.ModuleManager.BeginBlock(ctx, req)
func (app *SimApp) BeginBlocker(ctx sdk.Context) (sdk.BeginBlock, error) {
return app.ModuleManager.BeginBlock(ctx)
}

// EndBlocker application updates every end block
func (app *SimApp) EndBlocker(ctx sdk.Context, req abci.RequestEndBlock) (abci.ResponseEndBlock, error) {
return app.ModuleManager.EndBlock(ctx, req)
func (app *SimApp) EndBlocker(ctx sdk.Context) (sdk.EndBlock, error) {
return app.ModuleManager.EndBlock(ctx)
}

func (a *SimApp) Configurator() module.Configurator {
return a.configurator
}

// InitChainer application update at chain initialization
func (app *SimApp) InitChainer(ctx sdk.Context, req abci.RequestInitChain) (abci.ResponseInitChain, error) {
func (app *SimApp) InitChainer(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
var genesisState GenesisState
if err := json.Unmarshal(req.AppStateBytes, &genesisState); err != nil {
panic(err)
Expand Down
7 changes: 4 additions & 3 deletions simapp/app_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simapp

import (
"context"
"encoding/json"
"fmt"
"testing"
Expand Down Expand Up @@ -64,7 +65,7 @@ func TestSimAppExportAndBlockedAddrs(t *testing.T) {
)
}

app.Commit()
app.Commit(context.TODO(), &abci.RequestCommit{})

// Making a new app object with the db, so that initchain hasn't been called
app2 := NewSimApp(logger.With("instance", "second"), db, nil, true, simtestutil.NewAppOptionsWithFlagHome(t.TempDir()))
Expand Down Expand Up @@ -107,8 +108,8 @@ func TestRunMigrations(t *testing.T) {
}

// Initialize the chain
app.InitChain(abci.RequestInitChain{})
app.Commit()
app.InitChain(context.TODO(), &abci.RequestInitChain{})
app.Commit(context.TODO(), &abci.RequestCommit{})

testCases := []struct {
name string
Expand Down
3 changes: 2 additions & 1 deletion simapp/sim_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simapp

import (
"context"
"encoding/json"
"flag"
"fmt"
Expand Down Expand Up @@ -296,7 +297,7 @@ func TestAppSimulationAfterImport(t *testing.T) {
newApp := NewSimApp(log.NewNopLogger(), newDB, nil, true, appOptions, fauxMerkleModeOpt, baseapp.SetChainID(SimAppChainID))
require.Equal(t, "SimApp", newApp.Name())

newApp.InitChain(abci.RequestInitChain{
newApp.InitChain(context.TODO(), &abci.RequestInitChain{
AppStateBytes: exported.AppState,
ChainId: SimAppChainID,
})
Expand Down
38 changes: 18 additions & 20 deletions simapp/test_helpers.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package simapp

import (
"context"
"encoding/json"
"fmt"
"os"
Expand All @@ -10,7 +11,6 @@ import (
sdkmath "cosmossdk.io/math"
abci "github.com/cometbft/cometbft/abci/types"
cmtjson "github.com/cometbft/cometbft/libs/json"
cmtproto "github.com/cometbft/cometbft/proto/tendermint/types"
cmttypes "github.com/cometbft/cometbft/types"
dbm "github.com/cosmos/cosmos-db"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -83,12 +83,11 @@ func NewSimappWithCustomOptions(t *testing.T, isCheckTx bool, options SetupOptio
require.NoError(t, err)

// Initialize the chain
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simtestutil.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
app.InitChain(context.TODO(), &abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simtestutil.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)
}

Expand Down Expand Up @@ -135,22 +134,21 @@ func SetupWithGenesisValSet(t *testing.T, valSet *cmttypes.ValidatorSet, genAccs
require.NoError(t, err)

// init chain will set the validator set and initialize the genesis accounts
app.InitChain(
abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simtestutil.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
app.InitChain(context.TODO(), &abci.RequestInitChain{
Validators: []abci.ValidatorUpdate{},
ConsensusParams: simtestutil.DefaultConsensusParams,
AppStateBytes: stateBytes,
},
)

// commit genesis changes
app.Commit()
app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{
Height: app.LastBlockHeight() + 1,
AppHash: app.LastCommitID().Hash,
ValidatorsHash: valSet.Hash(),
NextValidatorsHash: valSet.Hash(),
}})
app.Commit(context.TODO(), &abci.RequestCommit{})
// app.BeginBlock(abci.RequestBeginBlock{Header: cmtproto.Header{
// Height: app.LastBlockHeight() + 1,
// AppHash: app.LastCommitID().Hash,
// ValidatorsHash: valSet.Hash(),
// NextValidatorsHash: valSet.Hash(),
// }})

return app
}
Expand Down
2 changes: 1 addition & 1 deletion tests/e2e/params/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func (s *E2ETestSuite) SetupSuite() {

// Make sure not to forget to persist `myParams` into the actual store,
// this is done in InitChain.
app.SetInitChainer(func(ctx sdk.Context, req abci.RequestInitChain) (abci.ResponseInitChain, error) {
app.SetInitChainer(func(ctx sdk.Context, req *abci.RequestInitChain) (*abci.ResponseInitChain, error) {
subspace.SetParamSet(ctx, &paramSet)

return app.InitChainer(ctx, req)
Expand Down
Loading

0 comments on commit 279c7af

Please sign in to comment.