Browse Source

update everything for Params and Result types

pull/1780/head
Ethan Buchman 7 years ago
parent
commit
cfdec76020
11 changed files with 253 additions and 127 deletions
  1. +24
    -24
      client/local_client.go
  2. +21
    -21
      example/counter/counter.go
  3. +1
    -1
      example/kvstore/helpers.go
  4. +9
    -9
      example/kvstore/kvstore.go
  5. +9
    -9
      example/kvstore/kvstore_test.go
  6. +20
    -20
      example/kvstore/persistent_kvstore.go
  7. +15
    -15
      server/socket_server.go
  8. +37
    -28
      types/application.go
  9. +32
    -0
      types/codetype.go
  10. +49
    -0
      types/params.go
  11. +36
    -0
      types/result.go

+ 24
- 24
client/local_client.go View File

@ -53,21 +53,21 @@ func (app *localClient) EchoAsync(msg string) *ReqRes {
func (app *localClient) InfoAsync(req types.RequestInfo) *ReqRes {
app.mtx.Lock()
res := app.Application.Info(req)
res := app.Application.Info(types.ToParamsInfo(req))
app.mtx.Unlock()
return app.callback(
types.ToRequestInfo(req),
types.ToResponseInfo(res),
types.ToResponseInfo(types.FromResultInfo(res)),
)
}
func (app *localClient) SetOptionAsync(req types.RequestSetOption) *ReqRes {
app.mtx.Lock()
res := app.Application.SetOption(req)
res := app.Application.SetOption(types.ToParamsSetOption(req))
app.mtx.Unlock()
return app.callback(
types.ToRequestSetOption(req),
types.ToResponseSetOption(res),
types.ToResponseSetOption(types.FromResultSetOption(res)),
)
}
@ -77,7 +77,7 @@ func (app *localClient) DeliverTxAsync(tx []byte) *ReqRes {
app.mtx.Unlock()
return app.callback(
types.ToRequestDeliverTx(tx),
types.ToResponseDeliverTx(res),
types.ToResponseDeliverTx(types.FromResultDeliverTx(res)),
)
}
@ -87,17 +87,17 @@ func (app *localClient) CheckTxAsync(tx []byte) *ReqRes {
app.mtx.Unlock()
return app.callback(
types.ToRequestCheckTx(tx),
types.ToResponseCheckTx(res),
types.ToResponseCheckTx(types.FromResultCheckTx(res)),
)
}
func (app *localClient) QueryAsync(req types.RequestQuery) *ReqRes {
app.mtx.Lock()
res := app.Application.Query(req)
res := app.Application.Query(types.ToParamsQuery(req))
app.mtx.Unlock()
return app.callback(
types.ToRequestQuery(req),
types.ToResponseQuery(res),
types.ToResponseQuery(types.FromResultQuery(res)),
)
}
@ -107,16 +107,16 @@ func (app *localClient) CommitAsync() *ReqRes {
app.mtx.Unlock()
return app.callback(
types.ToRequestCommit(),
types.ToResponseCommit(res),
types.ToResponseCommit(types.FromResultCommit(res)),
)
}
func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
app.mtx.Lock()
res := app.Application.InitChain(req)
res := app.Application.InitChain(types.ToParamsInitChain(req))
reqRes := app.callback(
types.ToRequestInitChain(req),
types.ToResponseInitChain(res),
types.ToResponseInitChain(types.FromResultInitChain(res)),
)
app.mtx.Unlock()
return reqRes
@ -124,21 +124,21 @@ func (app *localClient) InitChainAsync(req types.RequestInitChain) *ReqRes {
func (app *localClient) BeginBlockAsync(req types.RequestBeginBlock) *ReqRes {
app.mtx.Lock()
res := app.Application.BeginBlock(req)
res := app.Application.BeginBlock(types.ToParamsBeginBlock(req))
app.mtx.Unlock()
return app.callback(
types.ToRequestBeginBlock(req),
types.ToResponseBeginBlock(res),
types.ToResponseBeginBlock(types.FromResultBeginBlock(res)),
)
}
func (app *localClient) EndBlockAsync(req types.RequestEndBlock) *ReqRes {
app.mtx.Lock()
res := app.Application.EndBlock(req)
res := app.Application.EndBlock(types.ToParamsEndBlock(req))
app.mtx.Unlock()
return app.callback(
types.ToRequestEndBlock(req),
types.ToResponseEndBlock(res),
types.ToResponseEndBlock(types.FromResultEndBlock(res)),
)
}
@ -154,63 +154,63 @@ func (app *localClient) EchoSync(msg string) (*types.ResponseEcho, error) {
func (app *localClient) InfoSync(req types.RequestInfo) (*types.ResponseInfo, error) {
app.mtx.Lock()
res := app.Application.Info(req)
res := types.FromResultInfo(app.Application.Info(types.ToParamsInfo(req)))
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) SetOptionSync(req types.RequestSetOption) (*types.ResponseSetOption, error) {
app.mtx.Lock()
res := app.Application.SetOption(req)
res := types.FromResultSetOption(app.Application.SetOption(types.ToParamsSetOption(req)))
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) DeliverTxSync(tx []byte) (*types.ResponseDeliverTx, error) {
app.mtx.Lock()
res := app.Application.DeliverTx(tx)
res := types.FromResultDeliverTx(app.Application.DeliverTx(tx))
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) CheckTxSync(tx []byte) (*types.ResponseCheckTx, error) {
app.mtx.Lock()
res := app.Application.CheckTx(tx)
res := types.FromResultCheckTx(app.Application.CheckTx(tx))
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) QuerySync(req types.RequestQuery) (*types.ResponseQuery, error) {
app.mtx.Lock()
res := app.Application.Query(req)
res := types.FromResultQuery(app.Application.Query(types.ToParamsQuery(req)))
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) CommitSync() (*types.ResponseCommit, error) {
app.mtx.Lock()
res := app.Application.Commit()
res := types.FromResultCommit(app.Application.Commit())
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) InitChainSync(req types.RequestInitChain) (*types.ResponseInitChain, error) {
app.mtx.Lock()
res := app.Application.InitChain(req)
res := types.FromResultInitChain(app.Application.InitChain(types.ToParamsInitChain(req)))
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) BeginBlockSync(req types.RequestBeginBlock) (*types.ResponseBeginBlock, error) {
app.mtx.Lock()
res := app.Application.BeginBlock(req)
res := types.FromResultBeginBlock(app.Application.BeginBlock(types.ToParamsBeginBlock(req)))
app.mtx.Unlock()
return &res, nil
}
func (app *localClient) EndBlockSync(req types.RequestEndBlock) (*types.ResponseEndBlock, error) {
app.mtx.Lock()
res := app.Application.EndBlock(req)
res := types.FromResultEndBlock(app.Application.EndBlock(types.ToParamsEndBlock(req)))
app.mtx.Unlock()
return &res, nil
}


+ 21
- 21
example/counter/counter.go View File

@ -21,11 +21,11 @@ func NewCounterApplication(serial bool) *CounterApplication {
return &CounterApplication{serial: serial}
}
func (app *CounterApplication) Info(req types.RequestInfo) types.ResponseInfo {
return types.ResponseInfo{Data: cmn.Fmt("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)}
func (app *CounterApplication) Info(req types.ParamsInfo) types.ResultInfo {
return types.ResultInfo{Data: cmn.Fmt("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)}
}
func (app *CounterApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
func (app *CounterApplication) SetOption(req types.ParamsSetOption) types.ResultSetOption {
key, value := req.Key, req.Value
if key == "serial" && value == "on" {
app.serial = true
@ -33,20 +33,20 @@ func (app *CounterApplication) SetOption(req types.RequestSetOption) types.Respo
/*
TODO Panic and have the ABCI server pass an exception.
The client can call SetOptionSync() and get an `error`.
return types.ResponseSetOption{
return types.ResultSetOption{
Error: cmn.Fmt("Unknown key (%s) or value (%s)", key, value),
}
*/
return types.ResponseSetOption{}
return types.ResultSetOption{}
}
return types.ResponseSetOption{}
return types.ResultSetOption{}
}
func (app *CounterApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
func (app *CounterApplication) DeliverTx(tx []byte) types.ResultDeliverTx {
if app.serial {
if len(tx) > 8 {
return types.ResponseDeliverTx{
return types.ResultDeliverTx{
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Max tx size is 8 bytes, got %d", len(tx))}
}
@ -54,19 +54,19 @@ func (app *CounterApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8)
if txValue != uint64(app.txCount) {
return types.ResponseDeliverTx{
return types.ResultDeliverTx{
Code: code.CodeTypeBadNonce,
Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)}
}
}
app.txCount++
return types.ResponseDeliverTx{Code: code.CodeTypeOK}
return types.ResultDeliverTx{Code: code.CodeTypeOK}
}
func (app *CounterApplication) CheckTx(tx []byte) types.ResponseCheckTx {
func (app *CounterApplication) CheckTx(tx []byte) types.ResultCheckTx {
if app.serial {
if len(tx) > 8 {
return types.ResponseCheckTx{
return types.ResultCheckTx{
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Max tx size is 8 bytes, got %d", len(tx))}
}
@ -74,31 +74,31 @@ func (app *CounterApplication) CheckTx(tx []byte) types.ResponseCheckTx {
copy(tx8[len(tx8)-len(tx):], tx)
txValue := binary.BigEndian.Uint64(tx8)
if txValue < uint64(app.txCount) {
return types.ResponseCheckTx{
return types.ResultCheckTx{
Code: code.CodeTypeBadNonce,
Log: fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue)}
}
}
return types.ResponseCheckTx{Code: code.CodeTypeOK}
return types.ResultCheckTx{Code: code.CodeTypeOK}
}
func (app *CounterApplication) Commit() (resp types.ResponseCommit) {
func (app *CounterApplication) Commit() (resp types.ResultCommit) {
app.hashCount++
if app.txCount == 0 {
return types.ResponseCommit{}
return types.ResultCommit{}
}
hash := make([]byte, 8)
binary.BigEndian.PutUint64(hash, uint64(app.txCount))
return types.ResponseCommit{Data: hash}
return types.ResultCommit{Data: hash}
}
func (app *CounterApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
func (app *CounterApplication) Query(reqQuery types.ParamsQuery) types.ResultQuery {
switch reqQuery.Path {
case "hash":
return types.ResponseQuery{Value: []byte(cmn.Fmt("%v", app.hashCount))}
return types.ResultQuery{Value: []byte(cmn.Fmt("%v", app.hashCount))}
case "tx":
return types.ResponseQuery{Value: []byte(cmn.Fmt("%v", app.txCount))}
return types.ResultQuery{Value: []byte(cmn.Fmt("%v", app.txCount))}
default:
return types.ResponseQuery{Log: cmn.Fmt("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)}
return types.ResultQuery{Log: cmn.Fmt("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)}
}
}

+ 1
- 1
example/kvstore/helpers.go View File

@ -32,7 +32,7 @@ func RandVals(cnt int) []types.Validator {
// which allows tests to pass and is fine as long as you
// don't make any tx that modify the validator state
func InitKVStore(app *PersistentKVStoreApplication) {
app.InitChain(types.RequestInitChain{
app.InitChain(types.ParamsInitChain{
Validators: RandVals(1),
GenesisBytes: []byte("[]"),
})


+ 9
- 9
example/kvstore/kvstore.go View File

@ -64,12 +64,12 @@ func NewKVStoreApplication() *KVStoreApplication {
return &KVStoreApplication{state: state}
}
func (app *KVStoreApplication) Info(req types.RequestInfo) (resInfo types.ResponseInfo) {
return types.ResponseInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size)}
func (app *KVStoreApplication) Info(req types.ParamsInfo) (resInfo types.ResultInfo) {
return types.ResultInfo{Data: fmt.Sprintf("{\"size\":%v}", app.state.Size)}
}
// tx is either "key=value" or just arbitrary bytes
func (app *KVStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
func (app *KVStoreApplication) DeliverTx(tx []byte) types.ResultDeliverTx {
var key, value []byte
parts := bytes.Split(tx, []byte("="))
if len(parts) == 2 {
@ -84,24 +84,24 @@ func (app *KVStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
{[]byte("app.creator"), []byte("jae")},
{[]byte("app.key"), key},
}
return types.ResponseDeliverTx{Code: code.CodeTypeOK, Tags: tags}
return types.ResultDeliverTx{Code: code.CodeTypeOK, Tags: tags}
}
func (app *KVStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx {
return types.ResponseCheckTx{Code: code.CodeTypeOK}
func (app *KVStoreApplication) CheckTx(tx []byte) types.ResultCheckTx {
return types.ResultCheckTx{Code: code.CodeTypeOK}
}
func (app *KVStoreApplication) Commit() types.ResponseCommit {
func (app *KVStoreApplication) Commit() types.ResultCommit {
// Using a memdb - just return the big endian size of the db
appHash := make([]byte, 8)
binary.PutVarint(appHash, app.state.Size)
app.state.AppHash = appHash
app.state.Height += 1
saveState(app.state)
return types.ResponseCommit{Data: appHash}
return types.ResultCommit{Data: appHash}
}
func (app *KVStoreApplication) Query(reqQuery types.RequestQuery) (resQuery types.ResponseQuery) {
func (app *KVStoreApplication) Query(reqQuery types.ParamsQuery) (resQuery types.ResultQuery) {
if reqQuery.Prove {
value := app.state.db.Get(prefixKey(reqQuery.Data))
resQuery.Index = -1 // TODO make Proof return index


+ 9
- 9
example/kvstore/kvstore_test.go View File

@ -25,7 +25,7 @@ func testKVStore(t *testing.T, app types.Application, tx []byte, key, value stri
require.False(t, ar.IsErr(), ar)
// make sure query is fine
resQuery := app.Query(types.RequestQuery{
resQuery := app.Query(types.ParamsQuery{
Path: "/store",
Data: []byte(key),
})
@ -33,7 +33,7 @@ func testKVStore(t *testing.T, app types.Application, tx []byte, key, value stri
require.Equal(t, value, string(resQuery.Value))
// make sure proof is fine
resQuery = app.Query(types.RequestQuery{
resQuery = app.Query(types.ParamsQuery{
Path: "/store",
Data: []byte(key),
Prove: true,
@ -79,7 +79,7 @@ func TestPersistentKVStoreInfo(t *testing.T) {
InitKVStore(kvstore)
height := int64(0)
resInfo := kvstore.Info(types.RequestInfo{})
resInfo := kvstore.Info(types.ParamsInfo{})
if resInfo.LastBlockHeight != height {
t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
}
@ -90,11 +90,11 @@ func TestPersistentKVStoreInfo(t *testing.T) {
header := types.Header{
Height: int64(height),
}
kvstore.BeginBlock(types.RequestBeginBlock{hash, header, nil, nil})
kvstore.EndBlock(types.RequestEndBlock{header.Height})
kvstore.BeginBlock(types.ParamsBeginBlock{hash, header, nil, nil})
kvstore.EndBlock(types.ParamsEndBlock{header.Height})
kvstore.Commit()
resInfo = kvstore.Info(types.RequestInfo{})
resInfo = kvstore.Info(types.ParamsInfo{})
if resInfo.LastBlockHeight != height {
t.Fatalf("expected height of %d, got %d", height, resInfo.LastBlockHeight)
}
@ -114,7 +114,7 @@ func TestValUpdates(t *testing.T) {
nInit := 5
vals := RandVals(total)
// iniitalize with the first nInit
kvstore.InitChain(types.RequestInitChain{
kvstore.InitChain(types.ParamsInitChain{
Validators: vals[:nInit],
})
@ -176,13 +176,13 @@ func makeApplyBlock(t *testing.T, kvstore types.Application, heightInt int, diff
Height: height,
}
kvstore.BeginBlock(types.RequestBeginBlock{hash, header, nil, nil})
kvstore.BeginBlock(types.ParamsBeginBlock{hash, header, nil, nil})
for _, tx := range txs {
if r := kvstore.DeliverTx(tx); r.IsErr() {
t.Fatal(r)
}
}
resEndBlock := kvstore.EndBlock(types.RequestEndBlock{header.Height})
resEndBlock := kvstore.EndBlock(types.ParamsEndBlock{header.Height})
kvstore.Commit()
valsEqual(t, diff, resEndBlock.ValidatorUpdates)


+ 20
- 20
example/kvstore/persistent_kvstore.go View File

@ -50,19 +50,19 @@ func (app *PersistentKVStoreApplication) SetLogger(l log.Logger) {
app.logger = l
}
func (app *PersistentKVStoreApplication) Info(req types.RequestInfo) types.ResponseInfo {
func (app *PersistentKVStoreApplication) Info(req types.ParamsInfo) types.ResultInfo {
res := app.app.Info(req)
res.LastBlockHeight = app.app.state.Height
res.LastBlockAppHash = app.app.state.AppHash
return res
}
func (app *PersistentKVStoreApplication) SetOption(req types.RequestSetOption) types.ResponseSetOption {
func (app *PersistentKVStoreApplication) SetOption(req types.ParamsSetOption) types.ResultSetOption {
return app.app.SetOption(req)
}
// tx is either "val:pubkey/power" or "key=value" or just arbitrary bytes
func (app *PersistentKVStoreApplication) DeliverTx(tx []byte) types.ResponseDeliverTx {
func (app *PersistentKVStoreApplication) DeliverTx(tx []byte) types.ResultDeliverTx {
// if it starts with "val:", update the validator set
// format is "val:pubkey/power"
if isValidatorTx(tx) {
@ -75,40 +75,40 @@ func (app *PersistentKVStoreApplication) DeliverTx(tx []byte) types.ResponseDeli
return app.app.DeliverTx(tx)
}
func (app *PersistentKVStoreApplication) CheckTx(tx []byte) types.ResponseCheckTx {
func (app *PersistentKVStoreApplication) CheckTx(tx []byte) types.ResultCheckTx {
return app.app.CheckTx(tx)
}
// Commit will panic if InitChain was not called
func (app *PersistentKVStoreApplication) Commit() types.ResponseCommit {
func (app *PersistentKVStoreApplication) Commit() types.ResultCommit {
return app.app.Commit()
}
func (app *PersistentKVStoreApplication) Query(reqQuery types.RequestQuery) types.ResponseQuery {
func (app *PersistentKVStoreApplication) Query(reqQuery types.ParamsQuery) types.ResultQuery {
return app.app.Query(reqQuery)
}
// Save the validators in the merkle tree
func (app *PersistentKVStoreApplication) InitChain(req types.RequestInitChain) types.ResponseInitChain {
func (app *PersistentKVStoreApplication) InitChain(req types.ParamsInitChain) types.ResultInitChain {
for _, v := range req.Validators {
r := app.updateValidator(v)
if r.IsErr() {
app.logger.Error("Error updating validators", "r", r)
}
}
return types.ResponseInitChain{}
return types.ResultInitChain{}
}
// Track the block hash and header information
func (app *PersistentKVStoreApplication) BeginBlock(req types.RequestBeginBlock) types.ResponseBeginBlock {
func (app *PersistentKVStoreApplication) BeginBlock(req types.ParamsBeginBlock) types.ResultBeginBlock {
// reset valset changes
app.ValUpdates = make([]types.Validator, 0)
return types.ResponseBeginBlock{}
return types.ResultBeginBlock{}
}
// Update the validator set
func (app *PersistentKVStoreApplication) EndBlock(req types.RequestEndBlock) types.ResponseEndBlock {
return types.ResponseEndBlock{ValidatorUpdates: app.ValUpdates}
func (app *PersistentKVStoreApplication) EndBlock(req types.ParamsEndBlock) types.ResultEndBlock {
return types.ResultEndBlock{ValidatorUpdates: app.ValUpdates}
}
//---------------------------------------------
@ -139,13 +139,13 @@ func isValidatorTx(tx []byte) bool {
// format is "val:pubkey/power"
// pubkey is raw 32-byte ed25519 key
func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.ResponseDeliverTx {
func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.ResultDeliverTx {
tx = tx[len(ValidatorSetChangePrefix):]
//get the pubkey and power
pubKeyAndPower := strings.Split(string(tx), "/")
if len(pubKeyAndPower) != 2 {
return types.ResponseDeliverTx{
return types.ResultDeliverTx{
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Expected 'pubkey/power'. Got %v", pubKeyAndPower)}
}
@ -154,7 +154,7 @@ func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.Respon
// decode the pubkey
pubkey, err := hex.DecodeString(pubkeyS)
if err != nil {
return types.ResponseDeliverTx{
return types.ResultDeliverTx{
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Pubkey (%s) is invalid hex", pubkeyS)}
}
@ -162,7 +162,7 @@ func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.Respon
// decode the power
power, err := strconv.ParseInt(powerS, 10, 64)
if err != nil {
return types.ResponseDeliverTx{
return types.ResultDeliverTx{
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Power (%s) is not an int", powerS)}
}
@ -172,12 +172,12 @@ func (app *PersistentKVStoreApplication) execValidatorTx(tx []byte) types.Respon
}
// add, update, or remove a validator
func (app *PersistentKVStoreApplication) updateValidator(v types.Validator) types.ResponseDeliverTx {
func (app *PersistentKVStoreApplication) updateValidator(v types.Validator) types.ResultDeliverTx {
key := []byte("val:" + string(v.PubKey.Data))
if v.Power == 0 {
// remove validator
if !app.app.state.db.Has(key) {
return types.ResponseDeliverTx{
return types.ResultDeliverTx{
Code: code.CodeTypeUnauthorized,
Log: fmt.Sprintf("Cannot remove non-existent validator %X", key)}
}
@ -186,7 +186,7 @@ func (app *PersistentKVStoreApplication) updateValidator(v types.Validator) type
// add or update validator
value := bytes.NewBuffer(make([]byte, 0))
if err := types.WriteMessage(&v, value); err != nil {
return types.ResponseDeliverTx{
return types.ResultDeliverTx{
Code: code.CodeTypeEncodingError,
Log: fmt.Sprintf("Error encoding validator: %v", err)}
}
@ -196,5 +196,5 @@ func (app *PersistentKVStoreApplication) updateValidator(v types.Validator) type
// we only update the changes array if we successfully updated the tree
app.ValUpdates = append(app.ValUpdates, v)
return types.ResponseDeliverTx{Code: code.CodeTypeOK}
return types.ResultDeliverTx{Code: code.CodeTypeOK}
}

+ 15
- 15
server/socket_server.go View File

@ -172,32 +172,32 @@ func (s *SocketServer) handleRequest(req *types.Request, responses chan<- *types
case *types.Request_Flush:
responses <- types.ToResponseFlush()
case *types.Request_Info:
res := s.app.Info(*r.Info)
responses <- types.ToResponseInfo(res)
res := s.app.Info(types.ToParamsInfo(*r.Info))
responses <- types.ToResponseInfo(types.FromResultInfo(res))
case *types.Request_SetOption:
res := s.app.SetOption(*r.SetOption)
responses <- types.ToResponseSetOption(res)
res := s.app.SetOption(types.ToParamsSetOption(*r.SetOption))
responses <- types.ToResponseSetOption(types.FromResultSetOption(res))
case *types.Request_DeliverTx:
res := s.app.DeliverTx(r.DeliverTx.Tx)
responses <- types.ToResponseDeliverTx(res)
responses <- types.ToResponseDeliverTx(types.FromResultDeliverTx(res))
case *types.Request_CheckTx:
res := s.app.CheckTx(r.CheckTx.Tx)
responses <- types.ToResponseCheckTx(res)
responses <- types.ToResponseCheckTx(types.FromResultCheckTx(res))
case *types.Request_Commit:
res := s.app.Commit()
responses <- types.ToResponseCommit(res)
responses <- types.ToResponseCommit(types.FromResultCommit(res))
case *types.Request_Query:
res := s.app.Query(*r.Query)
responses <- types.ToResponseQuery(res)
res := s.app.Query(types.ToParamsQuery(*r.Query))
responses <- types.ToResponseQuery(types.FromResultQuery(res))
case *types.Request_InitChain:
res := s.app.InitChain(*r.InitChain)
responses <- types.ToResponseInitChain(res)
res := s.app.InitChain(types.ToParamsInitChain(*r.InitChain))
responses <- types.ToResponseInitChain(types.FromResultInitChain(res))
case *types.Request_BeginBlock:
res := s.app.BeginBlock(*r.BeginBlock)
responses <- types.ToResponseBeginBlock(res)
res := s.app.BeginBlock(types.ToParamsBeginBlock(*r.BeginBlock))
responses <- types.ToResponseBeginBlock(types.FromResultBeginBlock(res))
case *types.Request_EndBlock:
res := s.app.EndBlock(*r.EndBlock)
responses <- types.ToResponseEndBlock(res)
res := s.app.EndBlock(types.ToParamsEndBlock(*r.EndBlock))
responses <- types.ToResponseEndBlock(types.FromResultEndBlock(res))
default:
responses <- types.ToResponseException("Unknown request")
}


+ 37
- 28
types/application.go View File

@ -84,55 +84,64 @@ func NewGRPCApplication(app Application) *GRPCApplication {
return &GRPCApplication{app}
}
func (app *GRPCApplication) Echo(ctx context.Context, req *ParamsEcho) (*ResultEcho, error) {
return &ResultEcho{req.Message}, nil
func (app *GRPCApplication) Echo(ctx context.Context, req *RequestEcho) (*ResponseEcho, error) {
return &ResponseEcho{req.Message}, nil
}
func (app *GRPCApplication) Flush(ctx context.Context, req *ParamsFlush) (*ResultFlush, error) {
return &ResultFlush{}, nil
func (app *GRPCApplication) Flush(ctx context.Context, req *RequestFlush) (*ResponseFlush, error) {
return &ResponseFlush{}, nil
}
func (app *GRPCApplication) Info(ctx context.Context, req *ParamsInfo) (*ResultInfo, error) {
res := app.app.Info(*req)
return &res, nil
func (app *GRPCApplication) Info(ctx context.Context, req *RequestInfo) (*ResponseInfo, error) {
res := app.app.Info(ToParamsInfo(*req))
r := FromResultInfo(res)
return &r, nil
}
func (app *GRPCApplication) SetOption(ctx context.Context, req *ParamsSetOption) (*ResultSetOption, error) {
res := app.app.SetOption(*req)
return &res, nil
func (app *GRPCApplication) SetOption(ctx context.Context, req *RequestSetOption) (*ResponseSetOption, error) {
res := app.app.SetOption(ToParamsSetOption(*req))
r := FromResultSetOption(res)
return &r, nil
}
func (app *GRPCApplication) DeliverTx(ctx context.Context, req *ParamsDeliverTx) (*ResultDeliverTx, error) {
func (app *GRPCApplication) DeliverTx(ctx context.Context, req *RequestDeliverTx) (*ResponseDeliverTx, error) {
res := app.app.DeliverTx(req.Tx)
return &res, nil
r := FromResultDeliverTx(res)
return &r, nil
}
func (app *GRPCApplication) CheckTx(ctx context.Context, req *ParamsCheckTx) (*ResultCheckTx, error) {
func (app *GRPCApplication) CheckTx(ctx context.Context, req *RequestCheckTx) (*ResponseCheckTx, error) {
res := app.app.CheckTx(req.Tx)
return &res, nil
r := FromResultCheckTx(res)
return &r, nil
}
func (app *GRPCApplication) Query(ctx context.Context, req *ParamsQuery) (*ResultQuery, error) {
res := app.app.Query(*req)
return &res, nil
func (app *GRPCApplication) Query(ctx context.Context, req *RequestQuery) (*ResponseQuery, error) {
res := app.app.Query(ToParamsQuery(*req))
r := FromResultQuery(res)
return &r, nil
}
func (app *GRPCApplication) Commit(ctx context.Context, req *ParamsCommit) (*ResultCommit, error) {
func (app *GRPCApplication) Commit(ctx context.Context, req *RequestCommit) (*ResponseCommit, error) {
res := app.app.Commit()
return &res, nil
r := FromResultCommit(res)
return &r, nil
}
func (app *GRPCApplication) InitChain(ctx context.Context, req *ParamsInitChain) (*ResultInitChain, error) {
res := app.app.InitChain(*req)
return &res, nil
func (app *GRPCApplication) InitChain(ctx context.Context, req *RequestInitChain) (*ResponseInitChain, error) {
res := app.app.InitChain(ToParamsInitChain(*req))
r := FromResultInitChain(res)
return &r, nil
}
func (app *GRPCApplication) BeginBlock(ctx context.Context, req *ParamsBeginBlock) (*ResultBeginBlock, error) {
res := app.app.BeginBlock(*req)
return &res, nil
func (app *GRPCApplication) BeginBlock(ctx context.Context, req *RequestBeginBlock) (*ResponseBeginBlock, error) {
res := app.app.BeginBlock(ToParamsBeginBlock(*req))
r := FromResultBeginBlock(res)
return &r, nil
}
func (app *GRPCApplication) EndBlock(ctx context.Context, req *ParamsEndBlock) (*ResultEndBlock, error) {
res := app.app.EndBlock(*req)
return &res, nil
func (app *GRPCApplication) EndBlock(ctx context.Context, req *RequestEndBlock) (*ResponseEndBlock, error) {
res := app.app.EndBlock(ToParamsEndBlock(*req))
r := FromResultEndBlock(res)
return &r, nil
}

+ 32
- 0
types/codetype.go View File

@ -33,3 +33,35 @@ func (r ResponseQuery) IsOK() bool {
func (r ResponseQuery) IsErr() bool {
return r.Code != CodeTypeOK
}
//----------------------------------------------------
// IsOK returns true if Code is OK.
func (r ResultCheckTx) IsOK() bool {
return r.Code == CodeTypeOK
}
// IsErr returns true if Code is something other than OK.
func (r ResultCheckTx) IsErr() bool {
return r.Code != CodeTypeOK
}
// IsOK returns true if Code is OK.
func (r ResultDeliverTx) IsOK() bool {
return r.Code == CodeTypeOK
}
// IsErr returns true if Code is something other than OK.
func (r ResultDeliverTx) IsErr() bool {
return r.Code != CodeTypeOK
}
// IsOK returns true if Code is OK.
func (r ResultQuery) IsOK() bool {
return r.Code == CodeTypeOK
}
// IsErr returns true if Code is something other than OK.
func (r ResultQuery) IsErr() bool {
return r.Code != CodeTypeOK
}

+ 49
- 0
types/params.go View File

@ -11,16 +11,36 @@ type ParamsInfo struct {
Version string `json:"version,omitempty"`
}
func ToParamsInfo(req RequestInfo) ParamsInfo {
return ParamsInfo{
Version: req.Version,
}
}
type ParamsSetOption struct {
Key string `json:"key,omitempty"`
Value string `json:"value,omitempty"`
}
func ToParamsSetOption(req RequestSetOption) ParamsSetOption {
return ParamsSetOption{
Key: req.Key,
Value: req.Value,
}
}
type ParamsInitChain struct {
Validators []Validator `json:"validators"`
GenesisBytes []byte `json:"genesis_bytes,omitempty"`
}
func ToParamsInitChain(req RequestInitChain) ParamsInitChain {
return ParamsInitChain{
Validators: req.Validators,
GenesisBytes: req.GenesisBytes,
}
}
type ParamsQuery struct {
Data []byte `json:"data,omitempty"`
Path string `json:"path,omitempty"`
@ -28,6 +48,15 @@ type ParamsQuery struct {
Prove bool `json:"prove,omitempty"`
}
func ToParamsQuery(req RequestQuery) ParamsQuery {
return ParamsQuery{
Data: req.Data,
Path: req.Path,
Height: req.Height,
Prove: req.Prove,
}
}
type ParamsBeginBlock struct {
Hash []byte `json:"hash,omitempty"`
Header Header `json:"header"`
@ -35,6 +64,20 @@ type ParamsBeginBlock struct {
ByzantineValidators []Evidence `json:"byzantine_validators"`
}
func ToParamsBeginBlock(req RequestBeginBlock) ParamsBeginBlock {
vals := make([]SigningValidator, len(req.Validators))
for i := 0; i < len(vals); i++ {
v := req.Validators[i]
vals[i] = *v
}
return ParamsBeginBlock{
Hash: req.Hash,
Header: req.Header,
Validators: vals,
ByzantineValidators: req.ByzantineValidators,
}
}
type ParamsCheckTx struct {
Tx []byte `json:"tx,omitempty"`
}
@ -47,5 +90,11 @@ type ParamsEndBlock struct {
Height int64 `json:"height,omitempty"`
}
func ToParamsEndBlock(req RequestEndBlock) ParamsEndBlock {
return ParamsEndBlock{
Height: req.Height,
}
}
type ParamsCommit struct {
}

+ 36
- 0
types/result.go View File

@ -21,6 +21,10 @@ type ResultInfo struct {
LastBlockAppHash []byte `json:"last_block_app_hash,omitempty"`
}
func FromResultInfo(res ResultInfo) ResponseInfo {
return ResponseInfo(res)
}
type ResultSetOption struct {
Code uint32 `json:"code,omitempty"`
// bytes data = 2;
@ -28,10 +32,18 @@ type ResultSetOption struct {
Info string `json:"info,omitempty"`
}
func FromResultSetOption(res ResultSetOption) ResponseSetOption {
return ResponseSetOption(res)
}
type ResultInitChain struct {
Validators []Validator `json:"validators"`
}
func FromResultInitChain(res ResultInitChain) ResponseInitChain {
return ResponseInitChain(res)
}
type ResultQuery struct {
Code uint32 `json:"code,omitempty"`
// bytes data = 2; // use "value" instead.
@ -44,10 +56,18 @@ type ResultQuery struct {
Height int64 `json:"height,omitempty"`
}
func FromResultQuery(res ResultQuery) ResponseQuery {
return ResponseQuery(res)
}
type ResultBeginBlock struct {
Tags []common.KVPair `json:"tags,omitempty"`
}
func FromResultBeginBlock(res ResultBeginBlock) ResponseBeginBlock {
return ResponseBeginBlock(res)
}
type ResultCheckTx struct {
Code uint32 `json:"code,omitempty"`
Data []byte `json:"data,omitempty"`
@ -59,6 +79,10 @@ type ResultCheckTx struct {
Fee common.KI64Pair `json:"fee"`
}
func FromResultCheckTx(res ResultCheckTx) ResponseCheckTx {
return ResponseCheckTx(res)
}
type ResultDeliverTx struct {
Code uint32 `json:"code,omitempty"`
Data []byte `json:"data,omitempty"`
@ -70,13 +94,25 @@ type ResultDeliverTx struct {
Fee common.KI64Pair `json:"fee"`
}
func FromResultDeliverTx(res ResultDeliverTx) ResponseDeliverTx {
return ResponseDeliverTx(res)
}
type ResultEndBlock struct {
ValidatorUpdates []Validator `json:"validator_updates"`
ConsensusParamUpdates *ConsensusParams `json:"consensus_param_updates,omitempty"`
Tags []common.KVPair `json:"tags,omitempty"`
}
func FromResultEndBlock(res ResultEndBlock) ResponseEndBlock {
return ResponseEndBlock(res)
}
type ResultCommit struct {
// reserve 1
Data []byte `json:"data,omitempty"`
}
func FromResultCommit(res ResultCommit) ResponseCommit {
return ResponseCommit(res)
}

Loading…
Cancel
Save