Skip to content

Commit

Permalink
added account-txs endpoint
Browse files Browse the repository at this point in the history
patch in account-txs endpoint

small patch

added trust-node false

added proof query param

fixed proof

change function name in gas_calulator
  • Loading branch information
scottburch committed Jan 26, 2021
1 parent a72e2c6 commit 6be8845
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 7 deletions.
76 changes: 76 additions & 0 deletions x/crud/client/rest/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,88 @@ import (
"github.com/bluzelle/curium/x/crud/internal/keeper"
"github.com/bluzelle/curium/x/crud/internal/types"
"github.com/cosmos/cosmos-sdk/client/context"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/rest"
"github.com/cosmos/cosmos-sdk/x/auth/client/utils"
"github.com/gorilla/mux"
"io/ioutil"
"net/http"
"strconv"
"strings"
)

func AccountTxsHandler(cliCtx context.CLIContext) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
const NUM_OF_BLOCKS = 100
const PAGE_SIZE = 100

err := r.ParseForm()
if err != nil {
rest.WriteErrorResponse(
w, http.StatusBadRequest,
fmt.Sprintf("failed to parse query parameters: %s", err),
)
return
}

trustNode := true
if r.FormValue("proof") == "true" {
trustNode = false
}
cliCtx = cliCtx.WithTrustNode(trustNode)
cliCtx = cliCtx.WithChainID(r.FormValue("chainId"))
if trustNode == false && cliCtx.Verifier == nil {
verifier, err := context.CreateVerifier(cliCtx, context.DefaultVerifierCacheSize)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
}
cliCtx = cliCtx.WithVerifier(verifier)
}

vars := mux.Vars(r)
var response = make([]sdk.TxResponse, 0)

start, err := strconv.ParseInt(vars["start"], 10, 64)
if err != nil {
rest.WriteErrorResponse(w, http.StatusBadRequest, err.Error())
}

cliCtx, ok := rest.ParseQueryHeightOrReturnBadRequest(w, cliCtx, r)
if !ok {
return
}

events := []string{
fmt.Sprintf("tx.height>=%d", start),
fmt.Sprintf("tx.height<=%d", start+NUM_OF_BLOCKS),
"message.action='send'",
fmt.Sprintf("transfer.recipient='%s'", vars["address"]),
}

page := 1

for {
searchResult, err := utils.QueryTxsByEvents(cliCtx, events, page, PAGE_SIZE)
if err != nil {
if strings.Contains(err.Error(), "page should be within") {
break
}
rest.WriteErrorResponse(w, http.StatusInternalServerError, err.Error())
return
}

response = append(response, searchResult.Txs...)

if len(searchResult.Txs) < PAGE_SIZE {
break
}
page = page + 1
}

rest.PostProcessResponseBare(w, cliCtx, response)
}
}

func AbciQueryHandler(cliCtx context.CLIContext) http.HandlerFunc {

type queryReq struct {
Expand Down
1 change: 1 addition & 0 deletions x/crud/client/rest/rest.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// RegisterRoutes - Central function to define routes that get registered by the main application
func RegisterRoutes(cliCtx context.CLIContext, r *mux.Router, storeName string) {
r.HandleFunc("/abci-query", AbciQueryHandler(cliCtx)).Methods("POST")
r.HandleFunc("/account-txs/{address}/{start}", AccountTxsHandler(cliCtx)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/count", storeName), BlzCountHandler(cliCtx)).Methods("POST")
r.HandleFunc(fmt.Sprintf("/%s/count/{UUID}", storeName), BlzQCountHandler(cliCtx, storeName)).Methods("GET")
r.HandleFunc(fmt.Sprintf("/%s/create", storeName), BlzCreateHandler(cliCtx)).Methods("POST")
Expand Down
4 changes: 2 additions & 2 deletions x/crud/gas_calculator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ const (

func CalculateGasForLease(lease int64, bytes int) uint64 {
leaseDays := LeaseInDays(lease)
gasRate := LeaseGasRate(leaseDays)
gasRate := leaseGasRatePerByte(leaseDays)
return uint64(math.Round(gasRate * leaseDays * math.Max(float64(bytes), 200000)))
}

func LeaseInDays(lease int64) float64 {
return (float64(lease) / 24 / 60 / 60) * 5.5
}

func LeaseGasRate(days float64) float64 {
func leaseGasRatePerByte(days float64) float64 {
return LeaseGasRateDefaultValue + (LeaseGasRateMaximumValue-LeaseGasRateDefaultValue)/
math.Pow(1.0+math.Pow(days/LeaseGasRateParamC, LeaseGasRateParamB), LeaseGasRateParamG)
}
10 changes: 5 additions & 5 deletions x/crud/gas_calculator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ func daysToLease(days int64) int64 {
}


func TestLeaseGasRate(t *testing.T) {
assert.Equal(t, LeaseGasRate(0), 3.0)
assert.Equal(t, LeaseGasRate(1), 2.999876836999191)
assert.Equal(t, LeaseGasRate(10), 2.961551101112777)
assert.Equal(t, LeaseGasRate(120), 1.0262720621151311)
func TestLeaseGasRatePerByte(t *testing.T) {
assert.Equal(t, leaseGasRatePerByte(0), 3.0)
assert.Equal(t, leaseGasRatePerByte(1), 2.999876836999191)
assert.Equal(t, leaseGasRatePerByte(10), 2.961551101112777)
assert.Equal(t, leaseGasRatePerByte(120), 1.0262720621151311)


}
Expand Down

0 comments on commit 6be8845

Please sign in to comment.