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: performance improvement #5

Merged
merged 9 commits into from
Jul 5, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
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
4 changes: 4 additions & 0 deletions abci/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ type Client interface {
LoadSnapshotChunkSync(types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error)
ApplySnapshotChunkSync(types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error)
ProcessProposalSync(types.RequestProcessProposal) (*types.ResponseProcessProposal, error)

PreDeliverTxAsync(types.RequestPreDeliverTx)
PreBeginBlockSync(types.RequestPreBeginBlock) error
PreCommitSync(types.RequestPreCommit) error
}

// ----------------------------------------
Expand Down
14 changes: 14 additions & 0 deletions abci/client/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,3 +452,17 @@ func (cli *grpcClient) EthQuerySync(req types.RequestEthQuery) (*types.ResponseE
reqres := cli.EthQueryAsync(req)
return cli.finishSyncCall(reqres).GetEthQuery(), cli.Error()
}

// ----------------------------------------

func (cli *grpcClient) PreDeliverTxAsync(req types.RequestPreDeliverTx) {
panic("should not happen")
}

func (cli *grpcClient) PreBeginBlockSync(req types.RequestPreBeginBlock) error {
panic("should not happen")
}

func (cli *grpcClient) PreCommitSync(req types.RequestPreCommit) error {
panic("should not happen")
}
30 changes: 28 additions & 2 deletions abci/client/local_client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package abcicli

import (
"fmt"

types "github.com/cometbft/cometbft/abci/types"
"github.com/cometbft/cometbft/libs/service"
cmtsync "github.com/cometbft/cometbft/libs/sync"
Expand Down Expand Up @@ -309,7 +311,8 @@ func (app *localClient) OfferSnapshotSync(req types.RequestOfferSnapshot) (*type
}

func (app *localClient) LoadSnapshotChunkSync(
req types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) {
req types.RequestLoadSnapshotChunk,
) (*types.ResponseLoadSnapshotChunk, error) {
app.mtx.Lock()
defer app.mtx.Unlock()

Expand All @@ -318,7 +321,8 @@ func (app *localClient) LoadSnapshotChunkSync(
}

func (app *localClient) ApplySnapshotChunkSync(
req types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) {
req types.RequestApplySnapshotChunk,
) (*types.ResponseApplySnapshotChunk, error) {
app.mtx.Lock()
defer app.mtx.Unlock()

Expand Down Expand Up @@ -377,3 +381,25 @@ func (app *localClient) EthQuerySync(req types.RequestEthQuery) (*types.Response
res := app.Application.EthQuery(req)
return &res, nil
}

// -------------------------------------------------------

func (app *localClient) PreDeliverTxAsync(req types.RequestPreDeliverTx) {
app.Application.PreDeliverTx(req)
}

func (app *localClient) PreBeginBlockSync(req types.RequestPreBeginBlock) error {
res := app.Application.PreBeginBlock(req)
if res.IsErr() {
return fmt.Errorf(res.Error)
}
return nil
}

func (app *localClient) PreCommitSync(req types.RequestPreCommit) error {
res := app.Application.PreCommit(req)
if res.IsErr() {
return fmt.Errorf(res.Error)
}
return nil
}
33 changes: 33 additions & 0 deletions abci/client/mocks/client.go

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

22 changes: 19 additions & 3 deletions abci/client/socket_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ func (cli *socketClient) sendRequestsRoutine(conn io.Writer) {
func (cli *socketClient) recvResponseRoutine(conn io.Reader) {
r := bufio.NewReader(conn)
for {
var res = &types.Response{}
res := &types.Response{}
err := types.ReadMessage(r, res)
if err != nil {
cli.stopForError(fmt.Errorf("read message: %w", err))
Expand Down Expand Up @@ -394,7 +394,8 @@ func (cli *socketClient) OfferSnapshotSync(req types.RequestOfferSnapshot) (*typ
}

func (cli *socketClient) LoadSnapshotChunkSync(
req types.RequestLoadSnapshotChunk) (*types.ResponseLoadSnapshotChunk, error) {
req types.RequestLoadSnapshotChunk,
) (*types.ResponseLoadSnapshotChunk, error) {
reqres := cli.queueRequest(types.ToRequestLoadSnapshotChunk(req))
if err := cli.FlushSync(); err != nil {
return nil, err
Expand All @@ -404,7 +405,8 @@ func (cli *socketClient) LoadSnapshotChunkSync(
}

func (cli *socketClient) ApplySnapshotChunkSync(
req types.RequestApplySnapshotChunk) (*types.ResponseApplySnapshotChunk, error) {
req types.RequestApplySnapshotChunk,
) (*types.ResponseApplySnapshotChunk, error) {
reqres := cli.queueRequest(types.ToRequestApplySnapshotChunk(req))
if err := cli.FlushSync(); err != nil {
return nil, err
Expand Down Expand Up @@ -542,3 +544,17 @@ func (cli *socketClient) EthQuerySync(req types.RequestEthQuery) (*types.Respons

return reqres.Response.GetEthQuery(), cli.Error()
}

// ----------------------------------------

func (cli *socketClient) PreDeliverTxAsync(req types.RequestPreDeliverTx) {
panic("should not happen")
}

func (cli *socketClient) PreBeginBlockSync(req types.RequestPreBeginBlock) error {
panic("should not happen")
}

func (cli *socketClient) PreCommitSync(req types.RequestPreCommit) error {
panic("should not happen")
}
12 changes: 12 additions & 0 deletions abci/example/kvstore/persistent_kvstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,18 @@ func (app *PersistentKVStoreApplication) EthQuery(query types.RequestEthQuery) t
panic("should not happen")
}

func (app *PersistentKVStoreApplication) PreBeginBlock(req types.RequestPreBeginBlock) types.ResponsePrefetch {
panic("should not happen")
}

func (app *PersistentKVStoreApplication) PreDeliverTx(req types.RequestPreDeliverTx) {
panic("should not happen")
}

func (app *PersistentKVStoreApplication) PreCommit(req types.RequestPreCommit) types.ResponsePrefetch {
panic("should not happen")
}

// -----------------------------

const (
Expand Down
29 changes: 29 additions & 0 deletions abci/types/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type Application interface {

// Serve EVM json-rpc request
EthQuery(RequestEthQuery) ResponseEthQuery

// Prefetch
PreBeginBlock(RequestPreBeginBlock) ResponsePrefetch
PreDeliverTx(RequestPreDeliverTx)
PreCommit(RequestPreCommit) ResponsePrefetch
}

//-------------------------------------------------------
Expand Down Expand Up @@ -122,6 +127,18 @@ func (BaseApplication) EthQuery(req RequestEthQuery) ResponseEthQuery {

// -------------------------------------------------------

func (BaseApplication) PreBeginBlock(req RequestPreBeginBlock) ResponsePrefetch {
return ResponsePrefetch{Code: CodeTypeOK}
}

func (BaseApplication) PreDeliverTx(req RequestPreDeliverTx) {}

func (BaseApplication) PreCommit(req RequestPreCommit) ResponsePrefetch {
return ResponsePrefetch{Code: CodeTypeOK}
}

// -------------------------------------------------------

// GRPCApplication is a GRPC wrapper for Application
type GRPCApplication struct {
app Application
Expand Down Expand Up @@ -225,3 +242,15 @@ func (app *GRPCApplication) EthQuery(ctx context.Context, req *RequestEthQuery)
res := app.app.EthQuery(*req)
return &res, nil
}

func (app *GRPCApplication) PreBeginBlock(ctx context.Context, req *RequestPreBeginBlock) (*ResponsePrefetch, error) {
panic("should not be called")
}

func (app *GRPCApplication) PreDeliverTx(ctx context.Context, req *RequestPreDeliverTx) (*ResponsePrefetch, error) {
panic("should not be called")
}

func (app *GRPCApplication) PreCommit(ctx context.Context, req *RequestPreCommit) (*ResponsePrefetch, error) {
panic("should not be called")
}
33 changes: 33 additions & 0 deletions abci/types/mocks/application.go

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

20 changes: 16 additions & 4 deletions abci/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ func (r ResponseProcessProposal) IsStatusUnknown() bool {
return r.Status == ResponseProcessProposal_UNKNOWN
}

// IsOK returns true if Code is OK.
func (r ResponsePrefetch) IsOK() bool {
return r.Code == CodeTypeOK
}

// IsErr returns true if Code is something other than OK.
func (r ResponsePrefetch) IsErr() bool {
return r.Code != CodeTypeOK
}

//---------------------------------------------------------------------------
// override JSON marshaling so we emit defaults (ie. disable omitempty)

Expand Down Expand Up @@ -122,9 +132,11 @@ type jsonRoundTripper interface {
json.Unmarshaler
}

var _ jsonRoundTripper = (*ResponseCommit)(nil)
var _ jsonRoundTripper = (*ResponseQuery)(nil)
var _ jsonRoundTripper = (*ResponseDeliverTx)(nil)
var _ jsonRoundTripper = (*ResponseCheckTx)(nil)
var (
_ jsonRoundTripper = (*ResponseCommit)(nil)
_ jsonRoundTripper = (*ResponseQuery)(nil)
_ jsonRoundTripper = (*ResponseDeliverTx)(nil)
_ jsonRoundTripper = (*ResponseCheckTx)(nil)
)

var _ jsonRoundTripper = (*EventAttribute)(nil)
Loading