Skip to content

Commit

Permalink
tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
kocubinski committed Sep 27, 2024
1 parent 9e42ec8 commit db41984
Show file tree
Hide file tree
Showing 9 changed files with 626 additions and 55 deletions.
26 changes: 15 additions & 11 deletions server/v2/appmanager/appmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appmanager
import (
"bytes"
"context"
"cosmossdk.io/core/gas"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -135,27 +136,30 @@ func (a AppManager[T]) Simulate(ctx context.Context, tx T) (server.TxResult, cor

// Query queries the application at the provided version.
// CONTRACT: Version must always be provided, if 0, get latest
func (a AppManager[T]) Query(ctx context.Context, version uint64, request transaction.Msg) (transaction.Msg, error) {
func (a AppManager[T]) Query(
ctx context.Context, version uint64, request transaction.Msg,
) (transaction.Msg, gas.Gas, error) {
var (
queryState corestore.ReaderMap
err error
)
// if version is provided attempt to do a height query.
if version != 0 {
queryState, err := a.db.StateAt(version)
if err != nil {
return nil, err
}
return a.stf.Query(ctx, queryState, a.config.QueryGasLimit, request)
queryState, err = a.db.StateAt(version)
} else { // otherwise rely on latest available state.
_, queryState, err = a.db.StateLatest()
}

// otherwise rely on latest available state.
_, queryState, err := a.db.StateLatest()
if err != nil {
return nil, err
return nil, 0, err
}
return a.stf.Query(ctx, queryState, a.config.QueryGasLimit, request)
}

// QueryWithState executes a query with the provided state. This allows to process a query
// independently of the db state. For example, it can be used to process a query with temporary
// and uncommitted state
func (a AppManager[T]) QueryWithState(ctx context.Context, state corestore.ReaderMap, request transaction.Msg) (transaction.Msg, error) {
func (a AppManager[T]) QueryWithState(
ctx context.Context, state corestore.ReaderMap, request transaction.Msg,
) (transaction.Msg, gas.Gas, error) {
return a.stf.Query(ctx, state, a.config.QueryGasLimit, request)
}
3 changes: 2 additions & 1 deletion server/v2/appmanager/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package appmanager
import (
"context"

"cosmossdk.io/core/gas"
"cosmossdk.io/core/server"
"cosmossdk.io/core/store"
"cosmossdk.io/core/transaction"
Expand Down Expand Up @@ -39,5 +40,5 @@ type StateTransitionFunction[T transaction.Tx] interface {
state store.ReaderMap,
gasLimit uint64,
req transaction.Msg,
) (transaction.Msg, error)
) (transaction.Msg, gas.Gas, error)
}
14 changes: 10 additions & 4 deletions server/v2/cometbft/abci.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,9 @@ func (c *Consensus[T]) Info(ctx context.Context, _ *abciproto.InfoRequest) (*abc

// Query implements types.Application.
// It is called by cometbft to query application state.
func (c *Consensus[T]) Query(ctx context.Context, req *abciproto.QueryRequest) (resp *abciproto.QueryResponse, err error) {
func (c *Consensus[T]) Query(
ctx context.Context, req *abciproto.QueryRequest,
) (resp *abciproto.QueryResponse, err error) {
// check if it's a gRPC method
makeGRPCRequest, isGRPC := c.grpcMethodsMap[req.Path]
if isGRPC {
Expand All @@ -198,7 +200,7 @@ func (c *Consensus[T]) Query(ctx context.Context, req *abciproto.QueryRequest) (
if err != nil {
return nil, fmt.Errorf("unable to decode gRPC request with path %s from ABCI.Query: %w", req.Path, err)
}
res, err := c.app.Query(ctx, uint64(req.Height), protoRequest)
res, _, err := c.app.Query(ctx, uint64(req.Height), protoRequest)
if err != nil {
resp := QueryResult(err, c.cfg.AppTomlConfig.Trace)
resp.Height = req.Height
Expand Down Expand Up @@ -238,7 +240,9 @@ func (c *Consensus[T]) Query(ctx context.Context, req *abciproto.QueryRequest) (
}

// InitChain implements types.Application.
func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRequest) (*abciproto.InitChainResponse, error) {
func (c *Consensus[T]) InitChain(ctx context.Context, req *abciproto.InitChainRequest) (
*abciproto.InitChainResponse, error,
) {
c.logger.Info("InitChain", "initialHeight", req.InitialHeight, "chainID", req.ChainId)

// store chainID to be used later on in execution
Expand Down Expand Up @@ -554,7 +558,9 @@ func (c *Consensus[T]) VerifyVoteExtension(
}

// ExtendVote implements types.Application.
func (c *Consensus[T]) ExtendVote(ctx context.Context, req *abciproto.ExtendVoteRequest) (*abciproto.ExtendVoteResponse, error) {
func (c *Consensus[T]) ExtendVote(ctx context.Context, req *abciproto.ExtendVoteRequest) (
*abciproto.ExtendVoteResponse, error,
) {
// If vote extensions are not enabled, as a safety precaution, we return an
// error.
cp, err := c.GetConsensusParams(ctx)
Expand Down
25 changes: 18 additions & 7 deletions server/v2/cometbft/handlers/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package handlers

import (
"context"
"cosmossdk.io/core/gas"
"errors"
"fmt"

Expand All @@ -16,7 +17,9 @@ import (

type AppManager[T transaction.Tx] interface {
ValidateTx(ctx context.Context, tx T) (server.TxResult, error)
Query(ctx context.Context, version uint64, request transaction.Msg) (response transaction.Msg, err error)
Query(
ctx context.Context, version uint64, request transaction.Msg,
) (response transaction.Msg, gas gas.Gas, err error)
}

type DefaultProposalHandler[T transaction.Tx] struct {
Expand All @@ -32,10 +35,12 @@ func NewDefaultProposalHandler[T transaction.Tx](mp mempool.Mempool[T]) *Default
}

func (h *DefaultProposalHandler[T]) PrepareHandler() PrepareHandler[T] {
return func(ctx context.Context, app AppManager[T], codec transaction.Codec[T], req *abci.PrepareProposalRequest) ([]T, error) {
return func(
ctx context.Context, app AppManager[T], codec transaction.Codec[T], req *abci.PrepareProposalRequest,
) ([]T, error) {
var maxBlockGas uint64

res, err := app.Query(ctx, 0, &consensustypes.QueryParamsRequest{})
res, _, err := app.Query(ctx, 0, &consensustypes.QueryParamsRequest{})
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -98,15 +103,17 @@ func (h *DefaultProposalHandler[T]) PrepareHandler() PrepareHandler[T] {
}

func (h *DefaultProposalHandler[T]) ProcessHandler() ProcessHandler[T] {
return func(ctx context.Context, app AppManager[T], codec transaction.Codec[T], req *abci.ProcessProposalRequest) error {
return func(
ctx context.Context, app AppManager[T], codec transaction.Codec[T], req *abci.ProcessProposalRequest,
) error {
// If the mempool is nil we simply return ACCEPT,
// because PrepareProposal may have included txs that could fail verification.
_, isNoOp := h.mempool.(mempool.NoOpMempool[T])
if h.mempool == nil || isNoOp {
return nil
}

res, err := app.Query(ctx, 0, &consensustypes.QueryParamsRequest{})
res, _, err := app.Query(ctx, 0, &consensustypes.QueryParamsRequest{})
if err != nil {
return err
}
Expand Down Expand Up @@ -174,7 +181,9 @@ func decodeTxs[T transaction.Tx](codec transaction.Codec[T], txsBz [][]byte) []T
// NoOpPrepareProposal defines a no-op PrepareProposal handler. It will always
// return the transactions sent by the client's request.
func NoOpPrepareProposal[T transaction.Tx]() PrepareHandler[T] {
return func(ctx context.Context, app AppManager[T], codec transaction.Codec[T], req *abci.PrepareProposalRequest) ([]T, error) {
return func(
ctx context.Context, app AppManager[T], codec transaction.Codec[T], req *abci.PrepareProposalRequest,
) ([]T, error) {
return decodeTxs(codec, req.Txs), nil
}
}
Expand All @@ -198,7 +207,9 @@ func NoOpExtendVote() ExtendVoteHandler {
// NoOpVerifyVoteExtensionHandler defines a no-op VerifyVoteExtension handler. It
// will always return an ACCEPT status with no error.
func NoOpVerifyVoteExtensionHandler() VerifyVoteExtensionhandler {
return func(context.Context, store.ReaderMap, *abci.VerifyVoteExtensionRequest) (*abci.VerifyVoteExtensionResponse, error) {
return func(context.Context, store.ReaderMap, *abci.VerifyVoteExtensionRequest) (
*abci.VerifyVoteExtensionResponse, error,
) {
return &abci.VerifyVoteExtensionResponse{Status: abci.VERIFY_VOTE_EXTENSION_STATUS_ACCEPT}, nil
}
}
7 changes: 4 additions & 3 deletions server/v2/stf/stf.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,16 +436,17 @@ func (s STF[T]) Query(
state store.ReaderMap,
gasLimit uint64,
req transaction.Msg,
) (transaction.Msg, error) {
) (transaction.Msg, gas.Gas, error) {
queryState := s.branchFn(state)
hi, err := s.getHeaderInfo(queryState)
if err != nil {
return nil, err
return nil, 0, err
}
queryCtx := s.makeContext(ctx, nil, queryState, internal.ExecModeSimulate)
queryCtx.setHeaderInfo(hi)
queryCtx.setGasLimit(gasLimit)
return s.queryRouter.Invoke(queryCtx, req)
res, err := s.queryRouter.Invoke(queryCtx, req)
return res, queryCtx.meter.Consumed(), err
}

// clone clones STF.
Expand Down
2 changes: 1 addition & 1 deletion tests/integration/v2/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ func (a *App) StateLatestContext(t *testing.T) context.Context {
_, state, err := a.Store.StateLatest()
require.NoError(t, err)
writeableState := branch.DefaultNewWriterMap(state)
iCtx := integrationContext{state: writeableState}
iCtx := &integrationContext{state: writeableState}
return context.WithValue(context.Background(), contextKey, iCtx)
}

Expand Down
Loading

0 comments on commit db41984

Please sign in to comment.