diff --git a/client/local_client.go b/client/local_client.go index a34fde10..1150db4a 100644 --- a/client/local_client.go +++ b/client/local_client.go @@ -111,9 +111,7 @@ func (app *localClient) CommitAsync() *ReqRes { func (app *localClient) InitChainAsync(validators []*types.Validator) *ReqRes { app.mtx.Lock() - if bcApp, ok := app.Application.(types.BlockchainAware); ok { - bcApp.InitChain(validators) - } + app.Application.InitChain(validators) reqRes := app.callback( types.ToRequestInitChain(validators), types.ToResponseInitChain(), @@ -124,9 +122,7 @@ func (app *localClient) InitChainAsync(validators []*types.Validator) *ReqRes { func (app *localClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqRes { app.mtx.Lock() - if bcApp, ok := app.Application.(types.BlockchainAware); ok { - bcApp.BeginBlock(hash, header) - } + app.Application.BeginBlock(hash, header) app.mtx.Unlock() return app.callback( types.ToRequestBeginBlock(hash, header), @@ -136,10 +132,7 @@ func (app *localClient) BeginBlockAsync(hash []byte, header *types.Header) *ReqR func (app *localClient) EndBlockAsync(height uint64) *ReqRes { app.mtx.Lock() - var resEndBlock types.ResponseEndBlock - if bcApp, ok := app.Application.(types.BlockchainAware); ok { - resEndBlock = bcApp.EndBlock(height) - } + resEndBlock := app.Application.EndBlock(height) app.mtx.Unlock() return app.callback( types.ToRequestEndBlock(height), @@ -201,27 +194,21 @@ func (app *localClient) CommitSync() (res types.Result) { func (app *localClient) InitChainSync(validators []*types.Validator) (err error) { app.mtx.Lock() - if bcApp, ok := app.Application.(types.BlockchainAware); ok { - bcApp.InitChain(validators) - } + app.Application.InitChain(validators) app.mtx.Unlock() return nil } func (app *localClient) BeginBlockSync(hash []byte, header *types.Header) (err error) { app.mtx.Lock() - if bcApp, ok := app.Application.(types.BlockchainAware); ok { - bcApp.BeginBlock(hash, header) - } + app.Application.BeginBlock(hash, header) app.mtx.Unlock() return nil } func (app *localClient) EndBlockSync(height uint64) (resEndBlock types.ResponseEndBlock, err error) { app.mtx.Lock() - if bcApp, ok := app.Application.(types.BlockchainAware); ok { - resEndBlock = bcApp.EndBlock(height) - } + resEndBlock = app.Application.EndBlock(height) app.mtx.Unlock() return resEndBlock, nil } diff --git a/example/chain_aware/chain_aware_app.go b/example/block_aware/block_aware_app.go similarity index 100% rename from example/chain_aware/chain_aware_app.go rename to example/block_aware/block_aware_app.go diff --git a/example/chain_aware/chain_aware_test.go b/example/block_aware/block_aware_test.go similarity index 100% rename from example/chain_aware/chain_aware_test.go rename to example/block_aware/block_aware_test.go diff --git a/example/counter/counter.go b/example/counter/counter.go index a55a1fd1..8fce72e2 100644 --- a/example/counter/counter.go +++ b/example/counter/counter.go @@ -80,3 +80,13 @@ func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.Response return types.ResponseQuery{Log: Fmt("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)} } } + +func (app *CounterApplication) InitChain(validators []*types.Validator) { +} + +func (app *CounterApplication) BeginBlock(hash []byte, header *types.Header) { +} + +func (app *CounterApplication) EndBlock(height uint64) types.ResponseEndBlock { + return types.ResponseEndBlock{} +} diff --git a/example/dummy/dummy.go b/example/dummy/dummy.go index ac2bd462..73c938e7 100644 --- a/example/dummy/dummy.go +++ b/example/dummy/dummy.go @@ -70,3 +70,13 @@ func (app *DummyApplication) Query(reqQuery types.RequestQuery) (resQuery types. return } } + +func (app *DummyApplication) InitChain(validators []*types.Validator) { +} + +func (app *DummyApplication) BeginBlock(hash []byte, header *types.Header) { +} + +func (app *DummyApplication) EndBlock(height uint64) types.ResponseEndBlock { + return types.ResponseEndBlock{} +} diff --git a/example/dummy/dummy_test.go b/example/dummy/dummy_test.go index 7ca12a6e..fbbf97a5 100644 --- a/example/dummy/dummy_test.go +++ b/example/dummy/dummy_test.go @@ -180,14 +180,13 @@ func makeApplyBlock(t *testing.T, dummy types.Application, heightInt int, diff [ Height: height, } - dummyChain := dummy.(types.BlockchainAware) // hmm... - dummyChain.BeginBlock(hash, header) + dummy.BeginBlock(hash, header) for _, tx := range txs { if r := dummy.DeliverTx(tx); r.IsErr() { t.Fatal(r) } } - resEndBlock := dummyChain.EndBlock(height) + resEndBlock := dummy.EndBlock(height) dummy.Commit() valsEqual(t, diff, resEndBlock.Diffs) diff --git a/example/nil/nil_app.go b/example/nil/nil_app.go index 95ac8d59..5285cc39 100644 --- a/example/nil/nil_app.go +++ b/example/nil/nil_app.go @@ -34,3 +34,13 @@ func (app *NilApplication) Commit() types.Result { func (app *NilApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) { return resQuery } + +func (app *NilApplication) InitChain(validators []*types.Validator) { +} + +func (app *NilApplication) BeginBlock(hash []byte, header *types.Header) { +} + +func (app *NilApplication) EndBlock(height uint64) types.ResponseEndBlock { + return types.ResponseEndBlock{} +} diff --git a/server/socket_server.go b/server/socket_server.go index 46114027..bc1980e2 100644 --- a/server/socket_server.go +++ b/server/socket_server.go @@ -187,22 +187,14 @@ func (s *SocketServer) handleRequest(req *types.Request, responses chan<- *types resQuery := s.app.Query(*r.Query) responses <- types.ToResponseQuery(resQuery) case *types.Request_InitChain: - if app, ok := s.app.(types.BlockchainAware); ok { - app.InitChain(r.InitChain.Validators) - } + s.app.InitChain(r.InitChain.Validators) responses <- types.ToResponseInitChain() case *types.Request_BeginBlock: - if app, ok := s.app.(types.BlockchainAware); ok { - app.BeginBlock(r.BeginBlock.Hash, r.BeginBlock.Header) - } + s.app.BeginBlock(r.BeginBlock.Hash, r.BeginBlock.Header) responses <- types.ToResponseBeginBlock() case *types.Request_EndBlock: - if app, ok := s.app.(types.BlockchainAware); ok { - resEndBlock := app.EndBlock(r.EndBlock.Height) - responses <- types.ToResponseEndBlock(resEndBlock) - } else { - responses <- types.ToResponseEndBlock(types.ResponseEndBlock{}) - } + resEndBlock := s.app.EndBlock(r.EndBlock.Height) + responses <- types.ToResponseEndBlock(resEndBlock) default: responses <- types.ToResponseException("Unknown request") } diff --git a/types/application.go b/types/application.go index 404437fe..b45305f8 100644 --- a/types/application.go +++ b/types/application.go @@ -6,42 +6,25 @@ import ( // Applications 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 - // Return application info - Info() ResponseInfo + // Mempool Connection + CheckTx(tx []byte) Result // Validate a tx for the mempool - // Set application option (e.g. mode=mempool, mode=consensus) - SetOption(key string, value string) (log string) - - // Deliver a tx - DeliverTx(tx []byte) Result - - // Validate a tx for the mempool - CheckTx(tx []byte) Result - - // Query for state - Query(reqQuery RequestQuery) ResponseQuery - - // Return the application Merkle root hash - Commit() Result -} - -// Some applications can choose to implement BlockchainAware -type BlockchainAware interface { - - // Initialize blockchain - // validators: genesis validators from TendermintCore - InitChain(validators []*Validator) - - // Signals the beginning of a block - BeginBlock(hash []byte, header *Header) - - // Signals the end of a block - // diffs: changed validators from app to TendermintCore - EndBlock(height uint64) ResponseEndBlock + // 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 + 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 type GRPCApplication struct { app Application } @@ -88,23 +71,16 @@ func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*Re } func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) { - if chainAware, ok := app.app.(BlockchainAware); ok { - chainAware.InitChain(req.Validators) - } - return &ResponseInitChain{}, nil + app.app.InitChain(req.Validators) + return &ResponseInitChain{}, nil // NOTE: empty return } func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) { - if chainAware, ok := app.app.(BlockchainAware); ok { - chainAware.BeginBlock(req.Hash, req.Header) - } - return &ResponseBeginBlock{}, nil + app.app.BeginBlock(req.Hash, req.Header) + return &ResponseBeginBlock{}, nil // NOTE: empty return } func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) { - if chainAware, ok := app.app.(BlockchainAware); ok { - resEndBlock := chainAware.EndBlock(req.Height) - return &resEndBlock, nil - } - return &ResponseEndBlock{}, nil + resEndBlock := app.app.EndBlock(req.Height) + return &resEndBlock, nil }