tendermint/types/application.go

111 lines
3.2 KiB
Go
Raw Normal View History

2015-11-02 07:39:53 -08:00
package types
2016-05-17 21:54:32 -07:00
import (
context "golang.org/x/net/context"
)
2016-02-28 19:19:29 -08:00
// Applications
2015-11-02 07:39:53 -08:00
type Application interface {
// Return application info
2016-12-26 16:49:29 -08:00
Info() ResponseInfo
2015-11-02 07:39:53 -08:00
2015-11-27 10:14:46 -08:00
// Set application option (e.g. mode=mempool, mode=consensus)
SetOption(key string, value string) (log string)
2015-11-27 10:14:46 -08:00
2017-01-12 12:27:08 -08:00
// Deliver a tx
DeliverTx(tx []byte) Result
2015-11-02 07:39:53 -08:00
// Validate a tx for the mempool
CheckTx(tx []byte) Result
2016-01-18 14:37:42 -08:00
// Query for state
Query(reqQuery RequestQuery) ResponseQuery
2017-01-10 06:49:26 -08:00
2016-05-05 13:58:10 -07:00
// Return the application Merkle root hash
Commit() Result
2015-11-02 07:39:53 -08:00
}
2016-02-28 19:19:29 -08:00
// Some applications can choose to implement BlockchainAware
type BlockchainAware interface {
2016-02-28 19:19:29 -08:00
// Initialize blockchain
// validators: genesis validators from TendermintCore
InitChain(validators []*Validator)
2016-02-28 19:19:29 -08:00
2016-03-26 22:35:23 -07:00
// Signals the beginning of a block
2016-09-09 20:01:53 -07:00
BeginBlock(hash []byte, header *Header)
2016-03-26 22:35:23 -07:00
// Signals the end of a block
2016-05-05 13:58:10 -07:00
// diffs: changed validators from app to TendermintCore
2016-12-26 22:12:32 -08:00
EndBlock(height uint64) ResponseEndBlock
2016-02-28 19:19:29 -08:00
}
2016-05-17 21:54:32 -07:00
//------------------------------------
type GRPCApplication struct {
app Application
}
func NewGRPCApplication(app Application) *GRPCApplication {
return &GRPCApplication{app}
}
func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
return &ResponseEcho{req.Message}, nil
}
func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
return &ResponseFlush{}, nil
}
func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
2016-12-26 17:44:36 -08:00
resInfo := app.app.Info()
return &resInfo, nil
2016-05-17 21:54:32 -07:00
}
func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
return &ResponseSetOption{app.app.SetOption(req.Key, req.Value)}, nil
}
2017-01-12 12:27:08 -08:00
func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
r := app.app.DeliverTx(req.Tx)
return &ResponseDeliverTx{r.Code, r.Data, r.Log}, nil
2016-05-17 21:54:32 -07:00
}
func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
r := app.app.CheckTx(req.Tx)
return &ResponseCheckTx{r.Code, r.Data, r.Log}, nil
}
func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
resQuery := app.app.Query(*req)
return &resQuery, nil
2017-01-10 06:49:26 -08:00
}
2016-05-17 21:54:32 -07:00
func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
r := app.app.Commit()
return &ResponseCommit{r.Code, r.Data, r.Log}, nil
}
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
}
func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
if chainAware, ok := app.app.(BlockchainAware); ok {
2016-09-09 20:01:53 -07:00
chainAware.BeginBlock(req.Hash, req.Header)
2016-05-17 21:54:32 -07:00
}
return &ResponseBeginBlock{}, nil
}
func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
if chainAware, ok := app.app.(BlockchainAware); ok {
2016-12-26 22:12:32 -08:00
resEndBlock := chainAware.EndBlock(req.Height)
return &resEndBlock, nil
2016-05-17 21:54:32 -07:00
}
return &ResponseEndBlock{}, nil
}