DRY up the use of toHex in the project and move it to common
This commit is contained in:
28
rpc/api.go
28
rpc/api.go
@ -9,11 +9,11 @@ import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
"github.com/ethereum/go-ethereum/crypto"
|
||||
"github.com/ethereum/go-ethereum/ethdb"
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/event"
|
||||
"github.com/ethereum/go-ethereum/event/filter"
|
||||
"github.com/ethereum/go-ethereum/state"
|
||||
@ -251,7 +251,7 @@ func (p *EthereumApi) Transact(args *NewTxArgs, reply *interface{}) (err error)
|
||||
|
||||
result, _ := account.Transact(common.FromHex(args.To), common.FromHex(args.Value), common.FromHex(args.Gas), common.FromHex(args.GasPrice), common.FromHex(args.Data))
|
||||
if len(result) > 0 {
|
||||
*reply = toHex(result)
|
||||
*reply = common.ToHex(result)
|
||||
}
|
||||
} else if _, exists := p.register[args.From]; exists {
|
||||
p.register[ags.From] = append(p.register[args.From], args)
|
||||
@ -291,7 +291,7 @@ func (p *EthereumApi) GetBalance(args *GetBalanceArgs, reply *interface{}) error
|
||||
return err
|
||||
}
|
||||
state := p.getStateWithNum(args.BlockNumber).SafeGet(args.Address)
|
||||
*reply = toHex(state.Balance().Bytes())
|
||||
*reply = common.ToHex(state.Balance().Bytes())
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -384,7 +384,7 @@ func (p *EthereumApi) NewWhisperFilter(args *WhisperFilterArgs, reply *interface
|
||||
}
|
||||
id = p.xeth().Whisper().Watch(opts)
|
||||
p.messages[id] = &whisperFilter{timeout: time.Now()}
|
||||
*reply = toHex(big.NewInt(int64(id)).Bytes())
|
||||
*reply = common.ToHex(big.NewInt(int64(id)).Bytes())
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -480,7 +480,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||
return err
|
||||
}
|
||||
*reply = toHex(crypto.Sha3(common.FromHex(args.Data)))
|
||||
*reply = common.ToHex(crypto.Sha3(common.FromHex(args.Data)))
|
||||
case "web3_clientVersion":
|
||||
*reply = p.xeth().Backend().Version()
|
||||
case "net_version":
|
||||
@ -488,7 +488,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
case "net_listening":
|
||||
*reply = p.xeth().IsListening()
|
||||
case "net_peerCount":
|
||||
*reply = toHex(big.NewInt(int64(p.xeth().PeerCount())).Bytes())
|
||||
*reply = common.ToHex(big.NewInt(int64(p.xeth().PeerCount())).Bytes())
|
||||
case "eth_coinbase":
|
||||
// TODO handling of empty coinbase due to lack of accounts
|
||||
res := p.xeth().Coinbase()
|
||||
@ -500,11 +500,11 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
case "eth_mining":
|
||||
*reply = p.xeth().IsMining()
|
||||
case "eth_gasPrice":
|
||||
*reply = toHex(defaultGasPrice.Bytes())
|
||||
*reply = common.ToHex(defaultGasPrice.Bytes())
|
||||
case "eth_accounts":
|
||||
*reply = p.xeth().Accounts()
|
||||
case "eth_blockNumber":
|
||||
*reply = toHex(p.xeth().Backend().ChainManager().CurrentBlock().Number().Bytes())
|
||||
*reply = common.ToHex(p.xeth().Backend().ChainManager().CurrentBlock().Number().Bytes())
|
||||
case "eth_getBalance":
|
||||
args := new(GetBalanceArgs)
|
||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||
@ -539,7 +539,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*reply = toHex(big.NewInt(v).Bytes())
|
||||
*reply = common.ToHex(big.NewInt(v).Bytes())
|
||||
case "eth_getBlockTransactionCountByNumber":
|
||||
args := new(GetBlockByNumberArgs)
|
||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||
@ -550,7 +550,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*reply = toHex(big.NewInt(v).Bytes())
|
||||
*reply = common.ToHex(big.NewInt(v).Bytes())
|
||||
case "eth_getUncleCountByBlockHash":
|
||||
args := new(GetBlockByHashArgs)
|
||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||
@ -561,7 +561,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*reply = toHex(big.NewInt(v).Bytes())
|
||||
*reply = common.ToHex(big.NewInt(v).Bytes())
|
||||
case "eth_getUncleCountByBlockNumber":
|
||||
args := new(GetBlockByNumberArgs)
|
||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||
@ -572,7 +572,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
*reply = toHex(big.NewInt(v).Bytes())
|
||||
*reply = common.ToHex(big.NewInt(v).Bytes())
|
||||
case "eth_getData", "eth_getCode":
|
||||
args := new(GetDataArgs)
|
||||
if err := json.Unmarshal(req.Params, &args); err != nil {
|
||||
@ -663,7 +663,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
return NewValidationError("Index", "does not exist")
|
||||
}
|
||||
|
||||
uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false)
|
||||
uncle, err := p.GetBlockByHash(common.ToHex(v.Uncles[args.Index]), false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -682,7 +682,7 @@ func (p *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) error
|
||||
return NewValidationError("Index", "does not exist")
|
||||
}
|
||||
|
||||
uncle, err := p.GetBlockByHash(toHex(v.Uncles[args.Index]), false)
|
||||
uncle, err := p.GetBlockByHash(common.ToHex(v.Uncles[args.Index]), false)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import (
|
||||
// "fmt"
|
||||
"math/big"
|
||||
|
||||
"github.com/ethereum/go-ethereum/common"
|
||||
"github.com/ethereum/go-ethereum/core/types"
|
||||
)
|
||||
|
||||
@ -56,23 +57,23 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
|
||||
}
|
||||
|
||||
// convert strict types to hexified strings
|
||||
ext.BlockNumber = toHex(big.NewInt(b.BlockNumber).Bytes())
|
||||
ext.BlockHash = toHex(b.BlockHash)
|
||||
ext.ParentHash = toHex(b.ParentHash)
|
||||
ext.Nonce = toHex(b.Nonce)
|
||||
ext.Sha3Uncles = toHex(b.Sha3Uncles)
|
||||
ext.LogsBloom = toHex(b.LogsBloom)
|
||||
ext.TransactionRoot = toHex(b.TransactionRoot)
|
||||
ext.StateRoot = toHex(b.StateRoot)
|
||||
ext.Miner = toHex(b.Miner)
|
||||
ext.Difficulty = toHex(big.NewInt(b.Difficulty).Bytes())
|
||||
ext.TotalDifficulty = toHex(big.NewInt(b.TotalDifficulty).Bytes())
|
||||
ext.Size = toHex(big.NewInt(b.Size).Bytes())
|
||||
// ext.ExtraData = toHex(b.ExtraData)
|
||||
ext.GasLimit = toHex(big.NewInt(b.GasLimit).Bytes())
|
||||
// ext.MinGasPrice = toHex(big.NewInt(b.MinGasPrice).Bytes())
|
||||
ext.GasUsed = toHex(big.NewInt(b.GasUsed).Bytes())
|
||||
ext.UnixTimestamp = toHex(big.NewInt(b.UnixTimestamp).Bytes())
|
||||
ext.BlockNumber = common.ToHex(big.NewInt(b.BlockNumber).Bytes())
|
||||
ext.BlockHash = common.ToHex(b.BlockHash)
|
||||
ext.ParentHash = common.ToHex(b.ParentHash)
|
||||
ext.Nonce = common.ToHex(b.Nonce)
|
||||
ext.Sha3Uncles = common.ToHex(b.Sha3Uncles)
|
||||
ext.LogsBloom = common.ToHex(b.LogsBloom)
|
||||
ext.TransactionRoot = common.ToHex(b.TransactionRoot)
|
||||
ext.StateRoot = common.ToHex(b.StateRoot)
|
||||
ext.Miner = common.ToHex(b.Miner)
|
||||
ext.Difficulty = common.ToHex(big.NewInt(b.Difficulty).Bytes())
|
||||
ext.TotalDifficulty = common.ToHex(big.NewInt(b.TotalDifficulty).Bytes())
|
||||
ext.Size = common.ToHex(big.NewInt(b.Size).Bytes())
|
||||
// ext.ExtraData = common.ToHex(b.ExtraData)
|
||||
ext.GasLimit = common.ToHex(big.NewInt(b.GasLimit).Bytes())
|
||||
// ext.MinGasPrice = common.ToHex(big.NewInt(b.MinGasPrice).Bytes())
|
||||
ext.GasUsed = common.ToHex(big.NewInt(b.GasUsed).Bytes())
|
||||
ext.UnixTimestamp = common.ToHex(big.NewInt(b.UnixTimestamp).Bytes())
|
||||
ext.Transactions = make([]interface{}, len(b.Transactions))
|
||||
if b.fullTx {
|
||||
for i, tx := range b.Transactions {
|
||||
@ -80,12 +81,12 @@ func (b *BlockRes) MarshalJSON() ([]byte, error) {
|
||||
}
|
||||
} else {
|
||||
for i, tx := range b.Transactions {
|
||||
ext.Transactions[i] = toHex(tx.Hash)
|
||||
ext.Transactions[i] = common.ToHex(tx.Hash)
|
||||
}
|
||||
}
|
||||
ext.Uncles = make([]string, len(b.Uncles))
|
||||
for i, v := range b.Uncles {
|
||||
ext.Uncles[i] = toHex(v)
|
||||
ext.Uncles[i] = common.ToHex(v)
|
||||
}
|
||||
|
||||
return json.Marshal(ext)
|
||||
@ -160,17 +161,17 @@ func (t *TransactionRes) MarshalJSON() ([]byte, error) {
|
||||
Input string `json:"input"`
|
||||
}
|
||||
|
||||
ext.Hash = toHex(t.Hash)
|
||||
ext.Nonce = toHex(big.NewInt(t.Nonce).Bytes())
|
||||
ext.BlockHash = toHex(t.BlockHash)
|
||||
ext.BlockNumber = toHex(big.NewInt(t.BlockNumber).Bytes())
|
||||
ext.TxIndex = toHex(big.NewInt(t.TxIndex).Bytes())
|
||||
ext.From = toHex(t.From)
|
||||
ext.To = toHex(t.To)
|
||||
ext.Value = toHex(big.NewInt(t.Value).Bytes())
|
||||
ext.Gas = toHex(big.NewInt(t.Gas).Bytes())
|
||||
ext.GasPrice = toHex(big.NewInt(t.GasPrice).Bytes())
|
||||
ext.Input = toHex(t.Input)
|
||||
ext.Hash = common.ToHex(t.Hash)
|
||||
ext.Nonce = common.ToHex(big.NewInt(t.Nonce).Bytes())
|
||||
ext.BlockHash = common.ToHex(t.BlockHash)
|
||||
ext.BlockNumber = common.ToHex(big.NewInt(t.BlockNumber).Bytes())
|
||||
ext.TxIndex = common.ToHex(big.NewInt(t.TxIndex).Bytes())
|
||||
ext.From = common.ToHex(t.From)
|
||||
ext.To = common.ToHex(t.To)
|
||||
ext.Value = common.ToHex(big.NewInt(t.Value).Bytes())
|
||||
ext.Gas = common.ToHex(big.NewInt(t.Gas).Bytes())
|
||||
ext.GasPrice = common.ToHex(big.NewInt(t.GasPrice).Bytes())
|
||||
ext.Input = common.ToHex(t.Input)
|
||||
|
||||
return json.Marshal(ext)
|
||||
}
|
||||
|
17
rpc/util.go
17
rpc/util.go
@ -119,17 +119,8 @@ func (self JsonWrapper) ParseRequestBody(req *http.Request) (RpcRequest, error)
|
||||
return reqParsed, nil
|
||||
}
|
||||
|
||||
func toHex(b []byte) string {
|
||||
hex := common.Bytes2Hex(b)
|
||||
// Prefer output of "0x0" instead of "0x"
|
||||
if len(hex) == 0 {
|
||||
hex = "0"
|
||||
}
|
||||
return "0x" + hex
|
||||
}
|
||||
|
||||
func i2hex(n int) string {
|
||||
return toHex(big.NewInt(int64(n)).Bytes())
|
||||
return common.ToHex(big.NewInt(int64(n)).Bytes())
|
||||
}
|
||||
|
||||
type RpcServer interface {
|
||||
@ -150,11 +141,11 @@ func toLogs(logs state.Logs) (ls []Log) {
|
||||
for i, log := range logs {
|
||||
var l Log
|
||||
l.Topic = make([]string, len(log.Topics()))
|
||||
l.Address = toHex(log.Address())
|
||||
l.Data = toHex(log.Data())
|
||||
l.Address = common.ToHex(log.Address())
|
||||
l.Data = common.ToHex(log.Data())
|
||||
l.Number = log.Number()
|
||||
for j, topic := range log.Topics() {
|
||||
l.Topic[j] = toHex(topic)
|
||||
l.Topic[j] = common.ToHex(topic)
|
||||
}
|
||||
ls[i] = l
|
||||
}
|
||||
|
Reference in New Issue
Block a user