From f279171a2888adf20293d3c6e8ff4f4bf2c80655 Mon Sep 17 00:00:00 2001 From: Ethan Buchman Date: Mon, 18 Sep 2017 15:51:48 -0400 Subject: [PATCH] use request structs for InitChain and BeginBlock --- client/client.go | 12 ++++++------ client/grpc_client.go | 16 ++++++++-------- client/local_client.go | 20 ++++++++++---------- client/socket_client.go | 16 ++++++++-------- example/block_aware/block_aware_app.go | 2 +- example/block_aware/block_aware_test.go | 2 +- example/dummy/persistent_dummy.go | 8 ++++---- server/socket_server.go | 4 ++-- types/application.go | 17 +++++++++-------- types/base_app.go | 4 ++-- types/messages.go | 8 ++++---- types/result.go | 1 + 12 files changed, 56 insertions(+), 54 deletions(-) diff --git a/client/client.go b/client/client.go index c911dd876..3cc5df369 100644 --- a/client/client.go +++ b/client/client.go @@ -20,7 +20,7 @@ type Client interface { SetOptionAsync(key string, value string) *ReqRes DeliverTxAsync(tx []byte) *ReqRes CheckTxAsync(tx []byte) *ReqRes - QueryAsync(reqQuery types.RequestQuery) *ReqRes + QueryAsync(types.RequestQuery) *ReqRes CommitAsync() *ReqRes FlushSync() error @@ -29,15 +29,15 @@ type Client interface { SetOptionSync(key string, value string) (res types.Result) DeliverTxSync(tx []byte) (res types.Result) CheckTxSync(tx []byte) (res types.Result) - QuerySync(reqQuery types.RequestQuery) (resQuery types.ResponseQuery, err error) + QuerySync(types.RequestQuery) (resQuery types.ResponseQuery, err error) CommitSync() (res types.Result) - InitChainAsync(validators []*types.Validator) *ReqRes - BeginBlockAsync(hash []byte, header *types.Header) *ReqRes + InitChainAsync(types.RequestInitChain) *ReqRes + BeginBlockAsync(types.RequestBeginBlock) *ReqRes EndBlockAsync(height uint64) *ReqRes - InitChainSync(validators []*types.Validator) (err error) - BeginBlockSync(hash []byte, header *types.Header) (err error) + InitChainSync(types.RequestInitChain) (err error) + BeginBlockSync(types.RequestBeginBlock) (err error) EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error) } diff --git a/client/grpc_client.go b/client/grpc_client.go index 9743cdf62..a990455f3 100644 --- a/client/grpc_client.go +++ b/client/grpc_client.go @@ -190,8 +190,8 @@ func (cli *grpcClient) CommitAsync() *ReqRes { return cli.finishAsyncCall(req, &types.Response{&types.Response_Commit{res}}) } -func (cli *grpcClient) InitChainAsync(validators []*types.Validator) *ReqRes { - req := types.ToRequestInitChain(validators) +func (cli *grpcClient) InitChainAsync(params types.RequestInitChain) *ReqRes { + req := types.ToRequestInitChain(params) res, err := cli.client.InitChain(context.Background(), req.GetInitChain(), grpc.FailFast(true)) if err != nil { cli.StopForError(err) @@ -199,8 +199,8 @@ func (cli *grpcClient) InitChainAsync(validators []*types.Validator) *ReqRes { return cli.finishAsyncCall(req, &types.Response{&types.Response_InitChain{res}}) } -func (cli *grpcClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqRes { - req := types.ToRequestBeginBlock(hash, header) +func (cli *grpcClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes { + req := types.ToRequestBeginBlock(params) res, err := cli.client.BeginBlock(context.Background(), req.GetBeginBlock(), grpc.FailFast(true)) if err != nil { cli.StopForError(err) @@ -319,13 +319,13 @@ func (cli *grpcClient) CommitSync() (res types.Result) { return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} } -func (cli *grpcClient) InitChainSync(validators []*types.Validator) (err error) { - cli.InitChainAsync(validators) +func (cli *grpcClient) InitChainSync(params types.RequestInitChain) (err error) { + cli.InitChainAsync(params) return cli.Error() } -func (cli *grpcClient) BeginBlockSync(hash []byte, header *types.Header) (err error) { - cli.BeginBlockAsync(hash, header) +func (cli *grpcClient) BeginBlockSync(params types.RequestBeginBlock) (err error) { + cli.BeginBlockAsync(params) return cli.Error() } diff --git a/client/local_client.go b/client/local_client.go index 37f76abb3..6bf455c6b 100644 --- a/client/local_client.go +++ b/client/local_client.go @@ -109,23 +109,23 @@ func (app *localClient) CommitAsync() *ReqRes { ) } -func (app *localClient) InitChainAsync(validators []*types.Validator) *ReqRes { +func (app *localClient) InitChainAsync(params types.RequestInitChain) *ReqRes { app.mtx.Lock() - app.Application.InitChain(validators) + app.Application.InitChain(params) reqRes := app.callback( - types.ToRequestInitChain(validators), + types.ToRequestInitChain(params), types.ToResponseInitChain(), ) app.mtx.Unlock() return reqRes } -func (app *localClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqRes { +func (app *localClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes { app.mtx.Lock() - app.Application.BeginBlock(hash, header) + app.Application.BeginBlock(params) app.mtx.Unlock() return app.callback( - types.ToRequestBeginBlock(hash, header), + types.ToRequestBeginBlock(params), types.ToResponseBeginBlock(), ) } @@ -192,16 +192,16 @@ func (app *localClient) CommitSync() (res types.Result) { return res } -func (app *localClient) InitChainSync(validators []*types.Validator) (err error) { +func (app *localClient) InitChainSync(params types.RequestInitChain) (err error) { app.mtx.Lock() - app.Application.InitChain(validators) + app.Application.InitChain(params) app.mtx.Unlock() return nil } -func (app *localClient) BeginBlockSync(hash []byte, header *types.Header) (err error) { +func (app *localClient) BeginBlockSync(params types.RequestBeginBlock) (err error) { app.mtx.Lock() - app.Application.BeginBlock(hash, header) + app.Application.BeginBlock(params) app.mtx.Unlock() return nil } diff --git a/client/socket_client.go b/client/socket_client.go index f95eac5cc..1da93494a 100644 --- a/client/socket_client.go +++ b/client/socket_client.go @@ -255,12 +255,12 @@ func (cli *socketClient) CommitAsync() *ReqRes { return cli.queueRequest(types.ToRequestCommit()) } -func (cli *socketClient) InitChainAsync(validators []*types.Validator) *ReqRes { - return cli.queueRequest(types.ToRequestInitChain(validators)) +func (cli *socketClient) InitChainAsync(params types.RequestInitChain) *ReqRes { + return cli.queueRequest(types.ToRequestInitChain(params)) } -func (cli *socketClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqRes { - return cli.queueRequest(types.ToRequestBeginBlock(hash, header)) +func (cli *socketClient) BeginBlockAsync(params types.RequestBeginBlock) *ReqRes { + return cli.queueRequest(types.ToRequestBeginBlock(params)) } func (cli *socketClient) EndBlockAsync(height uint64) *ReqRes { @@ -352,8 +352,8 @@ func (cli *socketClient) CommitSync() (res types.Result) { return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log} } -func (cli *socketClient) InitChainSync(validators []*types.Validator) (err error) { - cli.queueRequest(types.ToRequestInitChain(validators)) +func (cli *socketClient) InitChainSync(params types.RequestInitChain) (err error) { + cli.queueRequest(types.ToRequestInitChain(params)) cli.FlushSync() if err := cli.Error(); err != nil { return err @@ -361,8 +361,8 @@ func (cli *socketClient) InitChainSync(validators []*types.Validator) (err error return nil } -func (cli *socketClient) BeginBlockSync(hash []byte, header *types.Header) (err error) { - cli.queueRequest(types.ToRequestBeginBlock(hash, header)) +func (cli *socketClient) BeginBlockSync(params types.RequestBeginBlock) (err error) { + cli.queueRequest(types.ToRequestBeginBlock(params)) cli.FlushSync() if err := cli.Error(); err != nil { return err diff --git a/example/block_aware/block_aware_app.go b/example/block_aware/block_aware_app.go index 8bacecd13..c88f22896 100644 --- a/example/block_aware/block_aware_app.go +++ b/example/block_aware/block_aware_app.go @@ -55,7 +55,7 @@ func (app *ChainAwareApplication) Query(reqQuery types.RequestQuery) (resQuery t } } -func (app *ChainAwareApplication) BeginBlock(hash []byte, header *types.Header) { +func (app *ChainAwareApplication) BeginBlock(reqBeginBlock types.RequestBeginBlock) { app.beginCount++ return } diff --git a/example/block_aware/block_aware_test.go b/example/block_aware/block_aware_test.go index 05a5c05c3..64b6755e2 100644 --- a/example/block_aware/block_aware_test.go +++ b/example/block_aware/block_aware_test.go @@ -37,7 +37,7 @@ func TestChainAware(t *testing.T) { hash := []byte("fake block hash") header := &types.Header{} for i := uint64(0); i < n; i++ { - client.BeginBlockSync(hash, header) + client.BeginBlockSync(&types.RequestBeginBlock{hash, header}) client.EndBlockSync(i) client.CommitSync() } diff --git a/example/dummy/persistent_dummy.go b/example/dummy/persistent_dummy.go index fb59a2724..303876e80 100644 --- a/example/dummy/persistent_dummy.go +++ b/example/dummy/persistent_dummy.go @@ -106,8 +106,8 @@ func (app *PersistentDummyApplication) Query(reqQuery types.RequestQuery) types. } // Save the validators in the merkle tree -func (app *PersistentDummyApplication) InitChain(validators []*types.Validator) { - for _, v := range validators { +func (app *PersistentDummyApplication) InitChain(params types.RequestInitChain) { + for _, v := range params.Validators { r := app.updateValidator(v) if r.IsErr() { app.logger.Error("Error updating validators", "r", r) @@ -116,9 +116,9 @@ func (app *PersistentDummyApplication) InitChain(validators []*types.Validator) } // Track the block hash and header information -func (app *PersistentDummyApplication) BeginBlock(hash []byte, header *types.Header) { +func (app *PersistentDummyApplication) BeginBlock(params types.RequestBeginBlock) { // update latest block info - app.blockHeader = header + app.blockHeader = params.Header // reset valset changes app.changes = make([]*types.Validator, 0) diff --git a/server/socket_server.go b/server/socket_server.go index 2b01dbd70..88011a056 100644 --- a/server/socket_server.go +++ b/server/socket_server.go @@ -186,10 +186,10 @@ func (s *SocketServer) handleRequest(req *types.Request, responses chan<- *types resQuery := s.app.Query(*r.Query) responses <- types.ToResponseQuery(resQuery) case *types.Request_InitChain: - s.app.InitChain(r.InitChain.Validators) + s.app.InitChain(*r.InitChain) responses <- types.ToResponseInitChain() case *types.Request_BeginBlock: - s.app.BeginBlock(r.BeginBlock.Hash, r.BeginBlock.Header) + s.app.BeginBlock(*r.BeginBlock) responses <- types.ToResponseBeginBlock() case *types.Request_EndBlock: resEndBlock := s.app.EndBlock(r.EndBlock.Height) diff --git a/types/application.go b/types/application.go index b45305f8d..8f17bf502 100644 --- a/types/application.go +++ b/types/application.go @@ -4,27 +4,28 @@ import ( context "golang.org/x/net/context" ) -// Applications +// Application is an interface that enables any finite, deterministic state machine +// to be driven by a blockchain-based replication engine via the ABCI type Application interface { // Info/Query Connection Info() ResponseInfo // Return application info SetOption(key string, value string) (log string) // Set application option - Query(reqQuery RequestQuery) ResponseQuery // Query for state + Query(RequestQuery) ResponseQuery // Query for state // Mempool Connection CheckTx(tx []byte) Result // Validate a tx for the mempool // Consensus Connection - InitChain(validators []*Validator) // Initialize blockchain with validators from TendermintCore - BeginBlock(hash []byte, header *Header) // Signals the beginning of a block - DeliverTx(tx []byte) Result // Deliver a tx for full processing + InitChain(RequestInitChain) // Initialize blockchain with validators and other info from TendermintCore + BeginBlock(RequestBeginBlock) // Signals the beginning of a block + DeliverTx(tx []byte) Result // Deliver a tx for full processing EndBlock(height uint64) ResponseEndBlock // Signals the end of a block, returns changes to the validator set Commit() Result // Commit the state and return the application Merkle root hash } //------------------------------------ -// GRPC wrapper for application +// GRPCApplication is a GRPC wrapper for Application type GRPCApplication struct { app Application } @@ -71,12 +72,12 @@ func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*Re } func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) { - app.app.InitChain(req.Validators) + app.app.InitChain(*req) return &ResponseInitChain{}, nil // NOTE: empty return } func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) { - app.app.BeginBlock(req.Hash, req.Header) + app.app.BeginBlock(*req) return &ResponseBeginBlock{}, nil // NOTE: empty return } diff --git a/types/base_app.go b/types/base_app.go index 1d4f84b8a..71c99cc2f 100644 --- a/types/base_app.go +++ b/types/base_app.go @@ -31,10 +31,10 @@ func (app *BaseApplication) Query(reqQuery RequestQuery) (resQuery ResponseQuery return } -func (app *BaseApplication) InitChain(validators []*Validator) { +func (app *BaseApplication) InitChain(reqInitChain RequestInitChain) { } -func (app *BaseApplication) BeginBlock(hash []byte, header *Header) { +func (app *BaseApplication) BeginBlock(reqBeginBlock RequestBeginBlock) { } func (app *BaseApplication) EndBlock(height uint64) (resEndBlock ResponseEndBlock) { diff --git a/types/messages.go b/types/messages.go index fe2d310ac..c7f24a132 100644 --- a/types/messages.go +++ b/types/messages.go @@ -55,15 +55,15 @@ func ToRequestQuery(reqQuery RequestQuery) *Request { } } -func ToRequestInitChain(validators []*Validator) *Request { +func ToRequestInitChain(reqInitChain RequestInitChain) *Request { return &Request{ - Value: &Request_InitChain{&RequestInitChain{validators}}, + Value: &Request_InitChain{&reqInitChain}, } } -func ToRequestBeginBlock(hash []byte, header *Header) *Request { +func ToRequestBeginBlock(reqBeginBlock RequestBeginBlock) *Request { return &Request{ - Value: &Request_BeginBlock{&RequestBeginBlock{hash, header}}, + Value: &Request_BeginBlock{&reqBeginBlock}, } } diff --git a/types/result.go b/types/result.go index 7bf52c90a..abf1e964c 100644 --- a/types/result.go +++ b/types/result.go @@ -6,6 +6,7 @@ import ( "github.com/tendermint/go-wire/data" ) +// Result is a common result object for ABCI calls. // CONTRACT: a zero Result is OK. type Result struct { Code CodeType `json:"code"`