CommitSync returns tmsp.Result

This commit is contained in:
Jae Kwon 2016-03-23 02:50:29 -07:00
parent 5a3975d54a
commit 6785b9a3b6
10 changed files with 82 additions and 50 deletions

View File

@ -248,44 +248,44 @@ func (cli *Client) SetOptionSync(key string, value string) (log string, err erro
return reqres.Response.Log, nil
}
func (cli *Client) AppendTxSync(tx []byte) (code types.CodeType, result []byte, log string, err error) {
func (cli *Client) AppendTxSync(tx []byte) (res types.Result) {
reqres := cli.queueRequest(types.RequestAppendTx(tx))
cli.FlushSync()
if cli.err != nil {
return types.CodeType_InternalError, nil, "", cli.err
return types.ErrInternalError.SetLog(cli.err.Error())
}
res := reqres.Response
return res.Code, res.Data, res.Log, nil
resp := reqres.Response
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
}
func (cli *Client) CheckTxSync(tx []byte) (code types.CodeType, result []byte, log string, err error) {
func (cli *Client) CheckTxSync(tx []byte) (res types.Result) {
reqres := cli.queueRequest(types.RequestCheckTx(tx))
cli.FlushSync()
if cli.err != nil {
return types.CodeType_InternalError, nil, "", cli.err
return types.ErrInternalError.SetLog(cli.err.Error())
}
res := reqres.Response
return res.Code, res.Data, res.Log, nil
resp := reqres.Response
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
}
func (cli *Client) CommitSync() (hash []byte, log string, err error) {
func (cli *Client) CommitSync() (res types.Result) {
reqres := cli.queueRequest(types.RequestCommit())
cli.FlushSync()
if cli.err != nil {
return nil, "", cli.err
return types.ErrInternalError.SetLog(cli.err.Error())
}
res := reqres.Response
return res.Data, res.Log, nil
resp := reqres.Response
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
}
func (cli *Client) QuerySync(query []byte) (code types.CodeType, result []byte, log string, err error) {
func (cli *Client) QuerySync(query []byte) (res types.Result) {
reqres := cli.queueRequest(types.RequestQuery(query))
cli.FlushSync()
if cli.err != nil {
return types.CodeType_InternalError, nil, "", cli.err
return types.ErrInternalError.SetLog(cli.err.Error())
}
res := reqres.Response
return res.Code, res.Data, res.Log, nil
resp := reqres.Response
return types.Result{Code: resp.Code, Data: resp.Data, Log: resp.Log}
}
func (cli *Client) InitChainSync(validators []*types.Validator) (err error) {

View File

@ -43,7 +43,7 @@ func (app *CounterApplication) AppendTx(tx []byte) types.Result {
}
}
app.txCount += 1
return types.NewResultOK(nil, "")
return types.OK
}
func (app *CounterApplication) CheckTx(tx []byte) types.Result {
@ -59,18 +59,18 @@ func (app *CounterApplication) CheckTx(tx []byte) types.Result {
}
}
}
return types.NewResultOK(nil, "")
return types.OK
}
func (app *CounterApplication) Commit() (hash []byte, log string) {
func (app *CounterApplication) Commit() types.Result {
app.hashCount += 1
if app.txCount == 0 {
return nil, ""
return types.OK
} else {
hash := make([]byte, 8)
binary.BigEndian.PutUint64(hash, uint64(app.txCount))
return hash, ""
return types.NewResultOK(hash, "")
}
}

View File

@ -35,16 +35,16 @@ func (app *DummyApplication) AppendTx(tx []byte) types.Result {
} else {
app.state.Set(tx, tx)
}
return types.NewResultOK(nil, "")
return types.OK
}
func (app *DummyApplication) CheckTx(tx []byte) types.Result {
return types.NewResultOK(nil, "")
return types.OK
}
func (app *DummyApplication) Commit() (hash []byte, log string) {
hash = app.state.Hash()
return hash, ""
func (app *DummyApplication) Commit() types.Result {
hash := app.state.Hash()
return types.NewResultOK(hash, "")
}
func (app *DummyApplication) Query(query []byte) types.Result {

View File

@ -27,8 +27,8 @@ func (app *NilApplication) CheckTx(tx []byte) types.Result {
return types.NewResultOK(nil, "")
}
func (app *NilApplication) Commit() (hash []byte, log string) {
return []byte("nil"), ""
func (app *NilApplication) Commit() types.Result {
return types.NewResultOK([]byte("nil"), "")
}
func (app *NilApplication) Query(query []byte) types.Result {

View File

@ -143,8 +143,8 @@ func (s *Server) handleRequest(req *types.Request, responses chan<- *types.Respo
res := s.app.CheckTx(req.Data)
responses <- types.ResponseCheckTx(res.Code, res.Data, res.Log)
case types.MessageType_Commit:
hash, logStr := s.app.Commit()
responses <- types.ResponseCommit(hash, logStr)
res := s.app.Commit()
responses <- types.ResponseCommit(res.Code, res.Data, res.Log)
case types.MessageType_Query:
res := s.app.Query(req.Data)
responses <- types.ResponseQuery(res.Code, res.Data, res.Log)

View File

@ -85,20 +85,22 @@ func setOption(client *tmspcli.Client, key, value string) {
}
func commit(client *tmspcli.Client, hashExp []byte) {
hash, log, err := client.CommitSync()
if err != nil {
panic(Fmt("committing %v\nlog: %v", err, log))
res := client.CommitSync()
_, data, log := res.Code, res.Data, res.Log
if res.IsErr() {
panic(Fmt("committing %v\nlog: %v", log))
}
if !bytes.Equal(hash, hashExp) {
if !bytes.Equal(res.Data, hashExp) {
panic(Fmt("Commit hash was unexpected. Got %X expected %X",
hash, hashExp))
data, hashExp))
}
}
func appendTx(client *tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
code, data, log, err := client.AppendTxSync(txBytes)
if err != nil {
panic(Fmt("appending tx %X: %v\nlog: %v", txBytes, err, log))
res := client.AppendTxSync(txBytes)
code, data, log := res.Code, res.Data, res.Log
if res.IsErr() {
panic(Fmt("appending tx %X: %v\nlog: %v", txBytes, log))
}
if code != codeExp {
panic(Fmt("AppendTx response code was unexpected. Got %v expected %v. Log: %v",
@ -111,9 +113,10 @@ func appendTx(client *tmspcli.Client, txBytes []byte, codeExp types.CodeType, da
}
func checkTx(client *tmspcli.Client, txBytes []byte, codeExp types.CodeType, dataExp []byte) {
code, data, log, err := client.CheckTxSync(txBytes)
if err != nil {
panic(Fmt("checking tx %X: %v\nlog: %v", txBytes, err, log))
res := client.CheckTxSync(txBytes)
code, data, log := res.Code, res.Data, res.Log
if res.IsErr() {
panic(Fmt("checking tx %X: %v\nlog: %v", txBytes, log))
}
if code != codeExp {
panic(Fmt("CheckTx response code was unexpected. Got %v expected %v. Log: %v",

View File

@ -16,7 +16,7 @@ type Application interface {
CheckTx(tx []byte) Result
// Return the application Merkle root hash
Commit() (hash []byte, log string)
Commit() Result
// Query for state
Query(query []byte) Result

24
types/errors.go Normal file
View File

@ -0,0 +1,24 @@
package types
var (
OK = NewResultOK(nil, "")
ErrInternalError = NewError(CodeType_InternalError, "Internal error")
ErrEncodingError = NewError(CodeType_EncodingError, "Encoding error")
ErrBadNonce = NewError(CodeType_BadNonce, "Error bad nonce")
ErrUnauthorized = NewError(CodeType_Unauthorized, "Unauthorized")
ErrInsufficientFunds = NewError(CodeType_InsufficientFunds, "Insufficient funds")
ErrUnknownRequest = NewError(CodeType_UnknownRequest, "Unknown request")
ErrBaseDuplicateAddress = NewError(CodeType_BaseDuplicateAddress, "Error duplicate address")
ErrBaseEncodingError = NewError(CodeType_BaseEncodingError, "Error encoding error")
ErrBaseInsufficientFees = NewError(CodeType_BaseInsufficientFees, "Error insufficient fees")
ErrBaseInsufficientFunds = NewError(CodeType_BaseInsufficientFunds, "Error insufficient funds")
ErrBaseInsufficientGasPrice = NewError(CodeType_BaseInsufficientGasPrice, "Error insufficient gas price")
ErrBaseInvalidAddress = NewError(CodeType_BaseInvalidAddress, "Error invalid address")
ErrBaseInvalidAmount = NewError(CodeType_BaseInvalidAmount, "Error invalid amount")
ErrBaseInvalidPubKey = NewError(CodeType_BaseInvalidPubKey, "Error invalid pubkey")
ErrBaseInvalidSequence = NewError(CodeType_BaseInvalidSequence, "Error invalid sequence")
ErrBaseInvalidSignature = NewError(CodeType_BaseInvalidSignature, "Error invalid signature")
ErrBaseUnknownPubKey = NewError(CodeType_BaseUnknownPubKey, "Error unknown pubkey")
)

View File

@ -111,37 +111,38 @@ func ResponseSetOption(log string) *Response {
}
}
func ResponseAppendTx(code CodeType, result []byte, log string) *Response {
func ResponseAppendTx(code CodeType, data []byte, log string) *Response {
return &Response{
Type: MessageType_AppendTx,
Code: code,
Data: result,
Data: data,
Log: log,
}
}
func ResponseCheckTx(code CodeType, result []byte, log string) *Response {
func ResponseCheckTx(code CodeType, data []byte, log string) *Response {
return &Response{
Type: MessageType_CheckTx,
Code: code,
Data: result,
Data: data,
Log: log,
}
}
func ResponseCommit(hash []byte, log string) *Response {
func ResponseCommit(code CodeType, data []byte, log string) *Response {
return &Response{
Type: MessageType_Commit,
Data: hash,
Code: code,
Data: data,
Log: log,
}
}
func ResponseQuery(code CodeType, result []byte, log string) *Response {
func ResponseQuery(code CodeType, data []byte, log string) *Response {
return &Response{
Type: MessageType_Query,
Code: code,
Data: result,
Data: data,
Log: log,
}
}

View File

@ -23,6 +23,10 @@ func (res Result) IsOK() bool {
return res.Code == CodeType_OK
}
func (res Result) IsErr() bool {
return res.Code != CodeType_OK
}
func (res Result) Error() string {
return fmt.Sprintf("TMSP error code:%v, data:%X, log:%v", res.Code, res.Data, res.Log)
}