aboutsummaryrefslogtreecommitdiffstats
path: root/rpc/api
diff options
context:
space:
mode:
Diffstat (limited to 'rpc/api')
-rw-r--r--rpc/api/debug.go100
-rw-r--r--rpc/api/debug_args.go24
-rw-r--r--rpc/api/debug_js.go7
-rw-r--r--rpc/api/eth.go8
-rw-r--r--rpc/api/eth_args.go39
-rw-r--r--rpc/api/parsing.go18
6 files changed, 165 insertions, 31 deletions
diff --git a/rpc/api/debug.go b/rpc/api/debug.go
index b451d8662..f16f62d2e 100644
--- a/rpc/api/debug.go
+++ b/rpc/api/debug.go
@@ -2,6 +2,8 @@ package api
import (
"fmt"
+ "strings"
+ "time"
"github.com/ethereum/ethash"
"github.com/ethereum/go-ethereum/core/state"
@@ -11,6 +13,7 @@ import (
"github.com/ethereum/go-ethereum/rpc/codec"
"github.com/ethereum/go-ethereum/rpc/shared"
"github.com/ethereum/go-ethereum/xeth"
+ "github.com/rcrowley/go-metrics"
)
const (
@@ -26,6 +29,7 @@ var (
"debug_processBlock": (*debugApi).ProcessBlock,
"debug_seedHash": (*debugApi).SeedHash,
"debug_setHead": (*debugApi).SetHead,
+ "debug_metrics": (*debugApi).Metrics,
}
)
@@ -171,3 +175,99 @@ func (self *debugApi) SeedHash(req *shared.Request) (interface{}, error) {
return nil, err
}
}
+
+func (self *debugApi) Metrics(req *shared.Request) (interface{}, error) {
+ args := new(MetricsArgs)
+ if err := self.codec.Decode(req.Params, &args); err != nil {
+ return nil, shared.NewDecodeParamError(err.Error())
+ }
+ // Create a rate formatter
+ units := []string{"", "K", "M", "G", "T", "E", "P"}
+ round := func(value float64, prec int) string {
+ unit := 0
+ for value >= 1000 {
+ unit, value, prec = unit+1, value/1000, 2
+ }
+ return fmt.Sprintf(fmt.Sprintf("%%.%df%s", prec, units[unit]), value)
+ }
+ format := func(total float64, rate float64) string {
+ return fmt.Sprintf("%s (%s/s)", round(total, 0), round(rate, 2))
+ }
+ // Iterate over all the metrics, and just dump for now
+ counters := make(map[string]interface{})
+ metrics.DefaultRegistry.Each(func(name string, metric interface{}) {
+ // Create or retrieve the counter hierarchy for this metric
+ root, parts := counters, strings.Split(name, "/")
+ for _, part := range parts[:len(parts)-1] {
+ if _, ok := root[part]; !ok {
+ root[part] = make(map[string]interface{})
+ }
+ root = root[part].(map[string]interface{})
+ }
+ name = parts[len(parts)-1]
+
+ // Fill the counter with the metric details, formatting if requested
+ if args.Raw {
+ switch metric := metric.(type) {
+ case metrics.Meter:
+ root[name] = map[string]interface{}{
+ "AvgRate01Min": metric.Rate1(),
+ "AvgRate05Min": metric.Rate5(),
+ "AvgRate15Min": metric.Rate15(),
+ "MeanRate": metric.RateMean(),
+ "Overall": float64(metric.Count()),
+ }
+
+ case metrics.Timer:
+ root[name] = map[string]interface{}{
+ "AvgRate01Min": metric.Rate1(),
+ "AvgRate05Min": metric.Rate5(),
+ "AvgRate15Min": metric.Rate15(),
+ "MeanRate": metric.RateMean(),
+ "Overall": float64(metric.Count()),
+ "Percentiles": map[string]interface{}{
+ "5": metric.Percentile(0.05),
+ "20": metric.Percentile(0.2),
+ "50": metric.Percentile(0.5),
+ "80": metric.Percentile(0.8),
+ "95": metric.Percentile(0.95),
+ },
+ }
+
+ default:
+ root[name] = "Unknown metric type"
+ }
+ } else {
+ switch metric := metric.(type) {
+ case metrics.Meter:
+ root[name] = map[string]interface{}{
+ "Avg01Min": format(metric.Rate1()*60, metric.Rate1()),
+ "Avg05Min": format(metric.Rate5()*300, metric.Rate5()),
+ "Avg15Min": format(metric.Rate15()*900, metric.Rate15()),
+ "Overall": format(float64(metric.Count()), metric.RateMean()),
+ }
+
+ case metrics.Timer:
+ root[name] = map[string]interface{}{
+ "Avg01Min": format(metric.Rate1()*60, metric.Rate1()),
+ "Avg05Min": format(metric.Rate5()*300, metric.Rate5()),
+ "Avg15Min": format(metric.Rate15()*900, metric.Rate15()),
+ "Overall": format(float64(metric.Count()), metric.RateMean()),
+ "Maximum": time.Duration(metric.Max()).String(),
+ "Minimum": time.Duration(metric.Min()).String(),
+ "Percentiles": map[string]interface{}{
+ "5": time.Duration(metric.Percentile(0.05)).String(),
+ "20": time.Duration(metric.Percentile(0.2)).String(),
+ "50": time.Duration(metric.Percentile(0.5)).String(),
+ "80": time.Duration(metric.Percentile(0.8)).String(),
+ "95": time.Duration(metric.Percentile(0.95)).String(),
+ },
+ }
+
+ default:
+ root[name] = "Unknown metric type"
+ }
+ }
+ })
+ return counters, nil
+}
diff --git a/rpc/api/debug_args.go b/rpc/api/debug_args.go
index b9b5aa27e..b72fb03ae 100644
--- a/rpc/api/debug_args.go
+++ b/rpc/api/debug_args.go
@@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"math/big"
+ "reflect"
"github.com/ethereum/go-ethereum/rpc/shared"
)
@@ -45,3 +46,26 @@ func (args *WaitForBlockArgs) UnmarshalJSON(b []byte) (err error) {
return nil
}
+
+type MetricsArgs struct {
+ Raw bool
+}
+
+func (args *MetricsArgs) UnmarshalJSON(b []byte) (err error) {
+ var obj []interface{}
+ if err := json.Unmarshal(b, &obj); err != nil {
+ return shared.NewDecodeParamError(err.Error())
+ }
+ if len(obj) > 1 {
+ return fmt.Errorf("metricsArgs needs 0, 1 arguments")
+ }
+ // default values when not provided
+ if len(obj) >= 1 && obj[0] != nil {
+ if value, ok := obj[0].(bool); !ok {
+ return fmt.Errorf("invalid argument %v", reflect.TypeOf(obj[0]))
+ } else {
+ args.Raw = value
+ }
+ }
+ return nil
+}
diff --git a/rpc/api/debug_js.go b/rpc/api/debug_js.go
index 35fecb75f..bd3a6dfb2 100644
--- a/rpc/api/debug_js.go
+++ b/rpc/api/debug_js.go
@@ -46,6 +46,13 @@ web3._extend({
params: 1,
inputFormatter: [web3._extend.formatters.formatInputInt],
outputFormatter: function(obj) { return obj; }
+ }),
+ new web3._extend.Method({
+ name: 'metrics',
+ call: 'debug_metrics',
+ params: 1,
+ inputFormatter: [web3._extend.formatters.formatInputBool],
+ outputFormatter: function(obj) { return obj; }
})
],
properties:
diff --git a/rpc/api/eth.go b/rpc/api/eth.go
index 6c071569c..db0b4b024 100644
--- a/rpc/api/eth.go
+++ b/rpc/api/eth.go
@@ -354,14 +354,6 @@ func (self *ethApi) GetBlockByNumber(req *shared.Request) (interface{}, error) {
block := self.xeth.EthBlockByNumber(args.BlockNumber)
br := NewBlockRes(block, args.IncludeTxs)
- // If request was for "pending", nil nonsensical fields
- if args.BlockNumber == -2 {
- br.BlockHash = nil
- br.BlockNumber = nil
- br.Miner = nil
- br.Nonce = nil
- br.LogsBloom = nil
- }
return br, nil
}
diff --git a/rpc/api/eth_args.go b/rpc/api/eth_args.go
index b5507832d..8f64280d3 100644
--- a/rpc/api/eth_args.go
+++ b/rpc/api/eth_args.go
@@ -885,10 +885,10 @@ func newTx(t *types.Transaction) *tx {
tx: t,
To: to,
From: from.Hex(),
- Value: t.Amount.String(),
+ Value: t.Value().String(),
Nonce: strconv.Itoa(int(t.Nonce())),
Data: "0x" + common.Bytes2Hex(t.Data()),
- GasLimit: t.GasLimit.String(),
+ GasLimit: t.Gas().String(),
GasPrice: t.GasPrice().String(),
}
}
@@ -905,16 +905,21 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) {
return shared.NewDecodeParamError(err.Error())
}
- trans := new(types.Transaction)
- trans.Amount = new(big.Int)
- trans.GasLimit = new(big.Int)
- trans.Price = new(big.Int)
+ var (
+ nonce uint64
+ to common.Address
+ amount = new(big.Int).Set(common.Big0)
+ gasLimit = new(big.Int).Set(common.Big0)
+ gasPrice = new(big.Int).Set(common.Big0)
+ data []byte
+ contractCreation = true
+ )
if val, found := fields["To"]; found {
if strVal, ok := val.(string); ok && len(strVal) > 0 {
tx.To = strVal
- to := common.StringToAddress(strVal)
- trans.Recipient = &to
+ to = common.HexToAddress(strVal)
+ contractCreation = false
}
}
@@ -927,7 +932,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) {
if val, found := fields["Nonce"]; found {
if strVal, ok := val.(string); ok {
tx.Nonce = strVal
- if trans.AccountNonce, err = strconv.ParseUint(strVal, 10, 64); err != nil {
+ if nonce, err = strconv.ParseUint(strVal, 10, 64); err != nil {
return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Nonce - %v", err))
}
}
@@ -939,7 +944,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) {
if val, found := fields["Value"]; found {
if strVal, ok := val.(string); ok {
tx.Value = strVal
- if _, parseOk = trans.Amount.SetString(strVal, 0); !parseOk {
+ if _, parseOk = amount.SetString(strVal, 0); !parseOk {
return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.Amount - %v", err))
}
}
@@ -949,9 +954,9 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) {
if strVal, ok := val.(string); ok {
tx.Data = strVal
if strings.HasPrefix(strVal, "0x") {
- trans.Payload = common.Hex2Bytes(strVal[2:])
+ data = common.Hex2Bytes(strVal[2:])
} else {
- trans.Payload = common.Hex2Bytes(strVal)
+ data = common.Hex2Bytes(strVal)
}
}
}
@@ -959,7 +964,7 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) {
if val, found := fields["GasLimit"]; found {
if strVal, ok := val.(string); ok {
tx.GasLimit = strVal
- if _, parseOk = trans.GasLimit.SetString(strVal, 0); !parseOk {
+ if _, parseOk = gasLimit.SetString(strVal, 0); !parseOk {
return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasLimit - %v", err))
}
}
@@ -968,13 +973,17 @@ func (tx *tx) UnmarshalJSON(b []byte) (err error) {
if val, found := fields["GasPrice"]; found {
if strVal, ok := val.(string); ok {
tx.GasPrice = strVal
- if _, parseOk = trans.Price.SetString(strVal, 0); !parseOk {
+ if _, parseOk = gasPrice.SetString(strVal, 0); !parseOk {
return shared.NewDecodeParamError(fmt.Sprintf("Unable to decode tx.GasPrice - %v", err))
}
}
}
- tx.tx = trans
+ if contractCreation {
+ tx.tx = types.NewContractCreation(nonce, amount, gasLimit, gasPrice, data)
+ } else {
+ tx.tx = types.NewTransaction(nonce, to, amount, gasLimit, gasPrice, data)
+ }
return nil
}
diff --git a/rpc/api/parsing.go b/rpc/api/parsing.go
index 85a9165e5..632462c31 100644
--- a/rpc/api/parsing.go
+++ b/rpc/api/parsing.go
@@ -270,29 +270,31 @@ func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
res.BlockHash = newHexData(block.Hash())
res.ParentHash = newHexData(block.ParentHash())
res.Nonce = newHexData(block.Nonce())
- res.Sha3Uncles = newHexData(block.Header().UncleHash)
+ res.Sha3Uncles = newHexData(block.UncleHash())
res.LogsBloom = newHexData(block.Bloom())
- res.TransactionRoot = newHexData(block.Header().TxHash)
+ res.TransactionRoot = newHexData(block.TxHash())
res.StateRoot = newHexData(block.Root())
- res.Miner = newHexData(block.Header().Coinbase)
+ res.Miner = newHexData(block.Coinbase())
res.Difficulty = newHexNum(block.Difficulty())
res.TotalDifficulty = newHexNum(block.Td)
res.Size = newHexNum(block.Size().Int64())
- res.ExtraData = newHexData(block.Header().Extra)
+ res.ExtraData = newHexData(block.Extra())
res.GasLimit = newHexNum(block.GasLimit())
res.GasUsed = newHexNum(block.GasUsed())
res.UnixTimestamp = newHexNum(block.Time())
- res.Transactions = make([]*TransactionRes, len(block.Transactions()))
- for i, tx := range block.Transactions() {
+ txs := block.Transactions()
+ res.Transactions = make([]*TransactionRes, len(txs))
+ for i, tx := range txs {
res.Transactions[i] = NewTransactionRes(tx)
res.Transactions[i].BlockHash = res.BlockHash
res.Transactions[i].BlockNumber = res.BlockNumber
res.Transactions[i].TxIndex = newHexNum(i)
}
- res.Uncles = make([]*UncleRes, len(block.Uncles()))
- for i, uncle := range block.Uncles() {
+ uncles := block.Uncles()
+ res.Uncles = make([]*UncleRes, len(uncles))
+ for i, uncle := range uncles {
res.Uncles[i] = NewUncleRes(uncle)
}