Skip to content

Commit

Permalink
Merge pull request #228 from QOSGroup/develop
Browse files Browse the repository at this point in the history
merge to master
  • Loading branch information
TokenxyWZY authored Dec 13, 2019
2 parents e96a43d + 607d319 commit a8b4684
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 3 deletions.
20 changes: 19 additions & 1 deletion baseabci/baseapp.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,8 +394,26 @@ func (app *BaseApp) CheckTx(req abci.RequestCheckTx) (res abci.ResponseCheckTx)
return toResponseCheckTx(err.Result())
}



defer func() {
if r := recover(); r != nil {
switch r.(type) {
case types.ErrorOutOfGas:
log := "checkTxStd out of gas"
result = types.ErrOutOfGas(log).Result()
default:
log := fmt.Sprintf("checkTxStd recovered: %v\nstack:\n%v", r, string(debug.Stack()))
result = types.ErrInternal(log).Result()
}

res = toResponseCheckTx(result)
}
}()

// 初始化context相关数据
ctx := app.checkState.ctx.WithTxBytes(req.Tx)

switch implTx := tx.(type) {
case *txs.TxStd:
ctx = setGasMeter(ctx, implTx)
Expand Down Expand Up @@ -678,7 +696,7 @@ func setGasMeter(ctx ctx.Context, tx *txs.TxStd) ctx.Context {
for _, itx := range tx.ITxs {
txsGas = txsGas.Add(itx.CalcGas())
}
gm.ConsumeGas(uint64(txsGas.Int64()), "sum of itxs' CalcGas")
gm.ConsumeGas(uint64(txsGas.Int64()), "exceeded limit gas or overflow")

return ctx.WithGasMeter(gm)
}
Expand Down
65 changes: 65 additions & 0 deletions client/rpc/tendermint.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/QOSGroup/qbase/types"
"github.com/gorilla/mux"
types2 "github.com/tendermint/tendermint/types"
types3 "github.com/tendermint/tendermint/abci/types"
"io/ioutil"
"net/http"
"strconv"
Expand Down Expand Up @@ -39,10 +40,27 @@ type TxsSearchItem struct {
GasUsed int64 `json:"gas_used"`
}

type EventKeyPair struct {
Key string `json:"key"`
Value string `json:"value"`
}

type Event struct {
Type string `json:"type"`
Pairs []EventKeyPair `json:"pairs"`
}

type EventResult struct {
BeginBlockEvents []Event `json:"begin_block_events"`
TxEvents []Event `json:"tx_events"`
EndBlockEvents []Event `json:"end_block_events"`
}

func registerTendermintRoutes(ctx context.CLIContext, m *mux.Router) {
m.HandleFunc("/node_status", queryNodeStatusHandleFn(ctx)).Methods("GET")
m.HandleFunc("/blocks/latest", queryLatestBlockHandleFn(ctx)).Methods("GET")
m.HandleFunc("/blocks/{height}", queryBlockHandleFn(ctx)).Methods("GET")
m.HandleFunc("/blocks/{height}/events", queryBlockEventsHandleFn(ctx)).Methods("GET")
m.HandleFunc("/blocks/txs/{height}", queryBlockTxsHandleFn(ctx)).Methods("GET")
m.HandleFunc("/validators/latest", queryLatestValidatorsHandleFn(ctx)).Methods("GET")
m.HandleFunc("/validators/{height}", queryValidatorsHandleFn(ctx)).Methods("GET")
Expand All @@ -60,6 +78,53 @@ func registerTendermintRoutes(ctx context.CLIContext, m *mux.Router) {
m.HandleFunc("/txs/search", queryTxsByCondition(ctx)).Methods("POST")
}

func queryBlockEventsHandleFn(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
m := mux.Vars(request)

height, err := strconv.ParseInt(m["height"], 10, 64)
if err != nil {
WriteErrorResponse(writer, http.StatusBadRequest, err.Error())
return
}

blockResults, err := cliContext.Client.BlockResults(&height)
if err != nil {
WriteErrorResponse(writer, http.StatusBadRequest, err.Error())
return
}

eventResult := EventResult{}

for _ , tx := range blockResults.Results.DeliverTx {
for _ , event := range tx.Events {
eventResult.TxEvents = append(eventResult.TxEvents, covertToEvent(event))
}
}

for _, event := range blockResults.Results.BeginBlock.Events {
eventResult.BeginBlockEvents = append(eventResult.BeginBlockEvents, covertToEvent(event))
}

for _, event := range blockResults.Results.EndBlock.Events {
eventResult.EndBlockEvents = append(eventResult.EndBlockEvents, covertToEvent(event))
}

PostProcessResponseBare(writer, cliContext, eventResult)
}
}

func covertToEvent(event2 types3.Event) (e Event) {
e.Type = event2.Type
for _ , pair := range event2.Attributes {
e.Pairs = append(e.Pairs , EventKeyPair{
Key: string(pair.Key),
Value: string(pair.Value),
})
}
return
}

func queryTxsByCondition(cliContext context.CLIContext) func(http.ResponseWriter, *http.Request) {
return func(writer http.ResponseWriter, request *http.Request) {
body, err := ioutil.ReadAll(request.Body)
Expand Down
16 changes: 14 additions & 2 deletions client/types/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,21 @@ func GetCommands(cmds ...*cobra.Command) []*cobra.Command {

// PostCommands adds common flags for commands to post tx
func PostCommands(cmds ...*cobra.Command) []*cobra.Command {
for _, c := range cmds {
return PostCustomMaxGasCommands(cmds, make([]int64, 0, len(cmds)))
}

func PostCustomMaxGasCommands(cmds []*cobra.Command, defaultGases []int64) []*cobra.Command {

for len(defaultGases) < len(cmds) {
defaultGases = append(defaultGases, DefaultMaxGas)
}

for i, c := range cmds {

defaultGas := defaultGases[i]

c.Flags().Int64(FlagNonce, 0, "account nonce to sign the tx")
c.Flags().Int64(FlagMaxGas, DefaultMaxGas, "gas limit to set per tx")
c.Flags().Int64(FlagMaxGas, defaultGas, "gas limit to set per tx")
c.Flags().String(FlagChainID, "", "Chain ID of tendermint node")
c.Flags().String(FlagNode, "tcp://localhost:26657", "<host>:<port> to tendermint rpc interface for this chain")
c.Flags().Bool(FlagTrustNode, false, "Trust connected full node (don't verify proofs for responses)")
Expand Down
8 changes: 8 additions & 0 deletions store/types/gas.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,20 @@ type ErrorOutOfGas struct {
Descriptor string
}

func (err ErrorOutOfGas) Error() string {
return err.Descriptor
}

// ErrorGasOverflow defines an error thrown when an action results gas consumption
// unsigned integer overflow.
type ErrorGasOverflow struct {
Descriptor string
}

func (err ErrorGasOverflow) Error() string {
return err.Descriptor
}

// GasMeter interface to track gas consumption
type GasMeter interface {
GasConsumed() Gas
Expand Down

0 comments on commit a8b4684

Please sign in to comment.