forked from tendermint/tendermint
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
532 additions
and
189 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package oracle | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
|
||
"github.com/tendermint/tendermint/abci/example/code" | ||
"github.com/tendermint/tendermint/abci/types" | ||
) | ||
|
||
type Application struct { | ||
types.BaseApplication | ||
|
||
hashCount int | ||
txCount int | ||
serial bool | ||
} | ||
|
||
func NewApplication(serial bool) *Application { | ||
return &Application{serial: serial} | ||
} | ||
|
||
func (app *Application) Info(req types.RequestInfo) types.ResponseInfo { | ||
return types.ResponseInfo{Data: fmt.Sprintf("{\"hashes\":%v,\"txs\":%v}", app.hashCount, app.txCount)} | ||
} | ||
|
||
func (app *Application) SetOption(req types.RequestSetOption) types.ResponseSetOption { | ||
key, value := req.Key, req.Value | ||
if key == "serial" && value == "on" { | ||
app.serial = true | ||
} else { | ||
/* | ||
TODO Panic and have the ABCI server pass an exception. | ||
The client can call SetOptionSync() and get an `error`. | ||
return types.ResponseSetOption{ | ||
Error: fmt.Sprintf("Unknown key (%s) or value (%s)", key, value), | ||
} | ||
*/ | ||
return types.ResponseSetOption{} | ||
} | ||
|
||
return types.ResponseSetOption{} | ||
} | ||
|
||
func (app *Application) DeliverTx(req types.RequestDeliverTx) types.ResponseDeliverTx { | ||
if app.serial { | ||
if len(req.Tx) > 8 { | ||
return types.ResponseDeliverTx{ | ||
Code: code.CodeTypeEncodingError, | ||
Log: fmt.Sprintf("Max tx size is 8 bytes, got %d", len(req.Tx))} | ||
} | ||
tx8 := make([]byte, 8) | ||
copy(tx8[len(tx8)-len(req.Tx):], req.Tx) | ||
txValue := binary.BigEndian.Uint64(tx8) | ||
if txValue != uint64(app.txCount) { | ||
return types.ResponseDeliverTx{ | ||
Code: code.CodeTypeBadNonce, | ||
Log: fmt.Sprintf("Invalid nonce. Expected %v, got %v", app.txCount, txValue)} | ||
} | ||
} | ||
app.txCount++ | ||
return types.ResponseDeliverTx{Code: code.CodeTypeOK} | ||
} | ||
|
||
func (app *Application) CheckTx(req types.RequestCheckTx) types.ResponseCheckTx { | ||
var isOracleTx bool | ||
if app.serial { | ||
if len(req.Tx) > 8 { | ||
return types.ResponseCheckTx{ | ||
Code: code.CodeTypeEncodingError, | ||
Log: fmt.Sprintf("Max tx size is 8 bytes, got %d", len(req.Tx))} | ||
} | ||
tx8 := make([]byte, 8) | ||
copy(tx8[len(tx8)-len(req.Tx):], req.Tx) | ||
txValue := binary.BigEndian.Uint64(tx8) | ||
if txValue < uint64(app.txCount) { | ||
return types.ResponseCheckTx{ | ||
Code: code.CodeTypeBadNonce, | ||
Log: fmt.Sprintf("Invalid nonce. Expected >= %v, got %v", app.txCount, txValue)} | ||
} | ||
|
||
// even numbers are oracle tx | ||
isOracleTx = txValue%2 == 0 | ||
} | ||
return types.ResponseCheckTx{Code: code.CodeTypeOK, IsOracleTx: isOracleTx} | ||
} | ||
|
||
func (app *Application) Commit() (resp types.ResponseCommit) { | ||
app.hashCount++ | ||
if app.txCount == 0 { | ||
return types.ResponseCommit{} | ||
} | ||
hash := make([]byte, 8) | ||
binary.BigEndian.PutUint64(hash, uint64(app.txCount)) | ||
return types.ResponseCommit{Data: hash} | ||
} | ||
|
||
func (app *Application) Query(reqQuery types.RequestQuery) types.ResponseQuery { | ||
switch reqQuery.Path { | ||
case "hash": | ||
return types.ResponseQuery{Value: []byte(fmt.Sprintf("%v", app.hashCount))} | ||
case "tx": | ||
return types.ResponseQuery{Value: []byte(fmt.Sprintf("%v", app.txCount))} | ||
default: | ||
return types.ResponseQuery{Log: fmt.Sprintf("Invalid query path. Expected hash or tx, got %v", reqQuery.Path)} | ||
} | ||
} |
Oops, something went wrong.