Skip to content

Commit

Permalink
Add eth_estimateGas, eth_getBlockByNumber
Browse files Browse the repository at this point in the history
  • Loading branch information
dcb9 committed Sep 10, 2018
1 parent 3200341 commit 4348be9
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pkg/eth/rpc_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,3 +310,80 @@ func (r *GetFilterChangesRequest) UnmarshalJSON(data []byte) error {

return nil
}

// ========== eth_estimateGas ============= //

type EstimateGasResponse string

// ========== eth_getBlockByNumber ============= //

type (
GetBlockByNumberRequest struct {
BlockNumber json.RawMessage
FullTransaction bool
}

/*
{
"number": "0x1b4",
"hash": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331",
"parentHash": "0x9646252be9520f6e71339a8df9c55e4d7619deeb018d2a3f2d21fc165dde5eb5",
"nonce": "0xe04d296d2460cfb8472af2c5fd05b5a214109c25688d3704aed5484f9a7792f2",
"sha3Uncles": "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
"logsBloom": "0xe670ec64341771606e55d6b4ca35a1a6b75ee3d5145a99d05921026d1527331",
"transactionsRoot": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"stateRoot": "0xd5855eb08b3387c0af375e9cdb6acfc05eb8f519e419b874b6ff2ffda7ed1dff",
"miner": "0x4e65fda2159562a496f9f3522f89122a3088497a",
"difficulty": "0x027f07",
"totalDifficulty": "0x027f07",
"extraData": "0x0000000000000000000000000000000000000000000000000000000000000000",
"size": "0x027f07",
"gasLimit": "0x9f759",
"gasUsed": "0x9f759",
"timestamp": "0x54e34e8e",
"transactions": [{}],
"uncles": ["0x1606e5...", "0xd5145a9..."]
}
*/
GetBlockByNumberResponse struct {
Number string `json:"number"`
Hash string `json:"hash"`
ParentHash string `json:"parentHash"`
Nonce string `json:"nonce"`
Sha3Uncles string `json:"sha3Uncles"`
LogsBloom string `json:"logsBloom"`
TransactionsRoot string `json:"transactionsRoot"`
StateRoot string `json:"stateRoot"`
Miner string `json:"miner"`
Difficulty string `json:"difficulty"`
TotalDifficulty string `json:"totalDifficulty"`
ExtraData string `json:"extraData"`
Size string `json:"size"`
GasLimit string `json:"gasLimit"`
GasUsed string `json:"gasUsed"`
Timestamp string `json:"timestamp"`
Transactions []string `json:"transactions"`
Uncles []string `json:"uncles"`
}
)

func (r *GetBlockByNumberRequest) UnmarshalJSON(data []byte) error {
var params []json.RawMessage
if err := json.Unmarshal(data, &params); err != nil {
return err
}

if len(params) == 0 {
return errors.New("params must be set")
}

var fullTx bool
if err := json.Unmarshal(params[1], &fullTx); err != nil {
return err
}

r.BlockNumber = params[0]
r.FullTransaction = fullTx

return nil
}
2 changes: 2 additions & 0 deletions pkg/qtum/jsonrpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const (
MethodSearchLogs = "searchlogs"
MethodWaitForLogs = "waitforlogs"
MethodGetBlockHash = "getblockhash"
MethodGetBlockHeader = "getblockheader"
MethodGetBlock = "getblock"
MethodGetAddressesByAccount = "getaddressesbyaccount"
MethodGetAccountInfo = "getaccountinfo"
MethodGenerate = "generate"
Expand Down
16 changes: 16 additions & 0 deletions pkg/qtum/method.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ func (m *Method) GetBlockHash(b *big.Int) (resp GetBlockHashResponse, err error)
return
}

func (m *Method) GetBlockHeader(hash string) (resp *GetBlockHeaderResponse, err error) {
req := GetBlockHeaderRequest{
Hash: hash,
}
err = m.Request(MethodGetBlockHeader, &req, &resp)
return
}

func (m *Method) GetBlock(hash string) (resp *GetBlockResponse, err error) {
req := GetBlockRequest{
Hash: hash,
}
err = m.Request(MethodGetBlock, &req, &resp)
return
}

func (m *Method) Generate(blockNum int, maxTries *int) (resp GenerateResponse, err error) {
req := GenerateRequest{
BlockNum: blockNum,
Expand Down
136 changes: 136 additions & 0 deletions pkg/qtum/rpc_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,3 +723,139 @@ func (r *GenerateRequest) MarshalJSON() ([]byte, error) {

return json.Marshal(params)
}

// ========== GetBlockHeader ============= //
type (
GetBlockHeaderRequest struct {
Hash string
NotVerbose bool
}

/*
{
"hash": "bba11e1bacc69ba535d478cf1f2e542da3735a517b0b8eebaf7e6bb25eeb48c5",
"confirmations": 1,
"height": 3983,
"version": 536870912,
"versionHex": "20000000",
"merkleroot": "0b5f03dc9d456c63c587cc554b70c1232449be43d1df62bc25a493b04de90334",
"time": 1536551888,
"mediantime": 1536551728,
"nonce": 0,
"bits": "207fffff",
"difficulty": 4.656542373906925e-10,
"chainwork": "0000000000000000000000000000000000000000000000000000000000001f20",
"hashStateRoot": "3e49216e58f1ad9e6823b5095dc532f0a6cc44943d36ff4a7b1aa474e172d672",
"hashUTXORoot": "130a3e712d9f8b06b83f5ebf02b27542fb682cdff3ce1af1c17b804729d88a47",
"previousblockhash": "6d7d56af09383301e1bb32a97d4a5c0661d62302c06a778487d919b7115543be",
"flags": "proof-of-stake",
"proofhash": "15bd6006ecbab06708f705ecf68664b78b388e4d51416cdafb019d5b90239877",
"modifier": "a79c00d1d570743ca8135a173d535258026d26bafbc5f3d951c3d33486b1f120"
}
*/
GetBlockHeaderResponse struct {
Hash string `json:"hash"`
Confirmations int `json:"confirmations"`
Height int `json:"height"`
Version int `json:"version"`
VersionHex string `json:"versionHex"`
Merkleroot string `json:"merkleroot"`
Time uint64 `json:"time"`
Mediantime int `json:"mediantime"`
Nonce int `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
Chainwork string `json:"chainwork"`
HashStateRoot string `json:"hashStateRoot"`
HashUTXORoot string `json:"hashUTXORoot"`
Previousblockhash string `json:"previousblockhash"`
Flags string `json:"flags"`
Proofhash string `json:"proofhash"`
Modifier string `json:"modifier"`
}
)

func (r *GetBlockHeaderRequest) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{
r.Hash,
!r.NotVerbose,
})
}

// ========== GetBlock ============= //
type (
GetBlockRequest struct {
Hash string
Verbosity *int
}

/*
{
"hash": "bba11e1bacc69ba535d478cf1f2e542da3735a517b0b8eebaf7e6bb25eeb48c5",
"confirmations": 57,
"strippedsize": 584,
"size": 620,
"weight": 2372,
"height": 3983,
"version": 536870912,
"versionHex": "20000000",
"merkleroot": "0b5f03dc9d456c63c587cc554b70c1232449be43d1df62bc25a493b04de90334",
"hashStateRoot": "3e49216e58f1ad9e6823b5095dc532f0a6cc44943d36ff4a7b1aa474e172d672",
"hashUTXORoot": "130a3e712d9f8b06b83f5ebf02b27542fb682cdff3ce1af1c17b804729d88a47",
"tx": [
"3208dc44733cbfa11654ad5651305428de473ef1e61a1ec07b0c1a5f4843be91",
"8fcd819194cce6a8454b2bec334d3448df4f097e9cdc36707bfd569900268950"
],
"time": 1536551888,
"mediantime": 1536551728,
"nonce": 0,
"bits": "207fffff",
"difficulty": 4.656542373906925e-10,
"chainwork": "0000000000000000000000000000000000000000000000000000000000001f20",
"previousblockhash": "6d7d56af09383301e1bb32a97d4a5c0661d62302c06a778487d919b7115543be",
"nextblockhash": "d7758774cfdd6bab7774aa891ae035f1dc5a2ff44240784b5e7bdfd43a7a6ec1",
"flags": "proof-of-stake",
"proofhash": "15bd6006ecbab06708f705ecf68664b78b388e4d51416cdafb019d5b90239877",
"modifier": "a79c00d1d570743ca8135a173d535258026d26bafbc5f3d951c3d33486b1f120",
"signature": "3045022100a6ab6c2b14b1f73e734f1a61d4d22385748e48836492723a6ab37cdf38525aba022014a51ecb9e51f5a7a851641683541fec6f8f20205d0db49e50b2a4e5daed69d2"
}
*/
GetBlockResponse struct {
Hash string `json:"hash"`
Confirmations int `json:"confirmations"`
Strippedsize int `json:"strippedsize"`
Size int `json:"size"`
Weight int `json:"weight"`
Height int `json:"height"`
Version int `json:"version"`
VersionHex string `json:"versionHex"`
Merkleroot string `json:"merkleroot"`
HashStateRoot string `json:"hashStateRoot"`
HashUTXORoot string `json:"hashUTXORoot"`
Tx []string `json:"tx"`
Time int `json:"time"`
Mediantime int `json:"mediantime"`
Nonce int `json:"nonce"`
Bits string `json:"bits"`
Difficulty float64 `json:"difficulty"`
Chainwork string `json:"chainwork"`
Previousblockhash string `json:"previousblockhash"`
Nextblockhash string `json:"nextblockhash"`
Flags string `json:"flags"`
Proofhash string `json:"proofhash"`
Modifier string `json:"modifier"`
Signature string `json:"signature"`
}
)

func (r *GetBlockRequest) MarshalJSON() ([]byte, error) {
verbosity := 1
if r.Verbosity != nil {
verbosity = *r.Verbosity
}

return json.Marshal([]interface{}{
r.Hash,
verbosity,
})
}
24 changes: 24 additions & 0 deletions pkg/transformer/eth_estimateGas.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package transformer

import (
"github.com/dcb9/janus/pkg/eth"
"github.com/dcb9/janus/pkg/qtum"
)

// ProxyETHEstimateGas implements ETHProxy
type ProxyETHEstimateGas struct {
*qtum.Qtum
}

func (p *ProxyETHEstimateGas) Method() string {
return "eth_estimateGas"
}

func (p *ProxyETHEstimateGas) Request(rawreq *eth.JSONRPCRequest) (interface{}, error) {
return p.request()
}

func (p *ProxyETHEstimateGas) request() (*eth.EstimateGasResponse, error) {
gas := eth.EstimateGasResponse("0x500000")
return &gas, nil
}
72 changes: 72 additions & 0 deletions pkg/transformer/eth_getBlockByNumber.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package transformer

import (
"github.com/dcb9/janus/pkg/eth"
"github.com/dcb9/janus/pkg/qtum"
"github.com/dcb9/janus/pkg/utils"
"github.com/ethereum/go-ethereum/common/hexutil"
)

// ProxyETHGetBlockByNumber implements ETHProxy
type ProxyETHGetBlockByNumber struct {
*qtum.Qtum
}

func (p *ProxyETHGetBlockByNumber) Method() string {
return "eth_getBlockByNumber"
}

func (p *ProxyETHGetBlockByNumber) Request(rawreq *eth.JSONRPCRequest) (interface{}, error) {
var req eth.GetBlockByNumberRequest
if err := unmarshalRequest(rawreq.Params, &req); err != nil {
return nil, err
}

return p.request(&req)
}
func (p *ProxyETHGetBlockByNumber) request(req *eth.GetBlockByNumberRequest) (*eth.GetBlockByNumberResponse, error) {
blockNum, err := getQtumBlockNumber(req.BlockNumber, 0)
if err != nil {
return nil, err
}

blockHash, err := p.GetBlockHash(blockNum)
if err != nil {
return nil, err
}

blockHeaderResp, err := p.GetBlockHeader(string(blockHash))
if err != nil {
return nil, err
}

blockResp, err := p.GetBlock(string(blockHash))
if err != nil {
return nil, err
}

txs := make([]string, 0, len(blockResp.Tx))
for _, tx := range blockResp.Tx {
txs = append(txs, utils.AddHexPrefix(tx))
}

return &eth.GetBlockByNumberResponse{
Hash: utils.AddHexPrefix(blockHeaderResp.Hash),
Nonce: hexutil.EncodeUint64(uint64(blockHeaderResp.Nonce)),
Number: hexutil.EncodeUint64(uint64(blockHeaderResp.Height)),
ParentHash: utils.AddHexPrefix(blockHeaderResp.Previousblockhash),
Difficulty: hexutil.EncodeUint64(uint64(blockHeaderResp.Difficulty)),
Timestamp: hexutil.EncodeUint64(blockHeaderResp.Time),
StateRoot: utils.AddHexPrefix(blockHeaderResp.HashStateRoot),
Size: hexutil.EncodeUint64(uint64(blockResp.Size)),
Transactions: txs,
TransactionsRoot: utils.AddHexPrefix(blockResp.Merkleroot),

ExtraData: "0x0",
Miner: "0x0000000000000000000000000000000000000000",
TotalDifficulty: "0x0",
GasLimit: "0x0",
GasUsed: "0x0",
Uncles: []string{},
}, nil
}
3 changes: 3 additions & 0 deletions pkg/transformer/transformer.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ func DefaultProxies(qtumRPCClient *qtum.Qtum) []ETHProxy {
&ProxyETHNewBlockFilter{Qtum: qtumRPCClient, blockFilter: blockFilter},
&ProxyETHGetFilterChanges{Qtum: qtumRPCClient, blockFilter: blockFilter},
&ProxyETHUninstallFilter{Qtum: qtumRPCClient, blockFilter: blockFilter},

&ProxyETHEstimateGas{Qtum: qtumRPCClient},
&ProxyETHGetBlockByNumber{Qtum: qtumRPCClient},
}
}

Expand Down

0 comments on commit 4348be9

Please sign in to comment.