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 {
|
|
|
|
|
2015-11-09 16:29:45 -08:00
|
|
|
// 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)
|
2016-01-25 08:01:18 -08:00
|
|
|
SetOption(key string, value string) (log string)
|
2015-11-27 10:14:46 -08:00
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
// Append a tx
|
2016-03-20 17:10:13 -07:00
|
|
|
AppendTx(tx []byte) Result
|
2015-11-02 07:39:53 -08:00
|
|
|
|
2016-01-08 16:52:02 -08:00
|
|
|
// Validate a tx for the mempool
|
2016-03-20 17:10:13 -07:00
|
|
|
CheckTx(tx []byte) Result
|
2016-01-08 16:52:02 -08:00
|
|
|
|
2016-01-18 14:37:42 -08:00
|
|
|
// Query for state
|
2016-03-20 17:10:13 -07:00
|
|
|
Query(query []byte) Result
|
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
|
|
|
|
2016-03-05 19:18:34 -08:00
|
|
|
// Some applications can choose to implement BlockchainAware
|
|
|
|
type BlockchainAware interface {
|
2016-02-28 19:19:29 -08:00
|
|
|
|
2016-03-05 19:18:34 -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
|
|
|
|
2016-03-05 19:18:34 -08: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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (app *GRPCApplication) AppendTx(ctx context.Context, req *RequestAppendTx) (*ResponseAppendTx, error) {
|
|
|
|
r := app.app.AppendTx(req.Tx)
|
|
|
|
return &ResponseAppendTx{r.Code, r.Data, r.Log}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
r := app.app.Query(req.Query)
|
|
|
|
return &ResponseQuery{r.Code, r.Data, r.Log}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|