Skip to content

Commit c8e5d53

Browse files
committed
Merge pull request bnb-chain#618 from tgerring/issue613
Issue bnb-chain#613
2 parents 4a4da9a + dbf1710 commit c8e5d53

3 files changed

Lines changed: 27 additions & 28 deletions

File tree

‎rpc/api.go‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,15 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
113113
return err
114114
}
115115

116-
block := NewBlockRes(api.xeth().EthBlockByHash(args.BlockHash))
116+
block := NewBlockRes(api.xeth().EthBlockByHash(args.BlockHash), false)
117117
*reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes())
118118
case "eth_getBlockTransactionCountByNumber":
119119
args := new(GetBlockByNumberArgs)
120120
if err := json.Unmarshal(req.Params, &args); err != nil {
121121
return err
122122
}
123123

124-
block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber))
124+
block := NewBlockRes(api.xeth().EthBlockByNumber(args.BlockNumber), false)
125125
*reply = common.ToHex(big.NewInt(int64(len(block.Transactions))).Bytes())
126126
case "eth_getUncleCountByBlockHash":
127127
args := new(GetBlockByHashArgs)
@@ -130,7 +130,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
130130
}
131131

132132
block := api.xeth().EthBlockByHash(args.BlockHash)
133-
br := NewBlockRes(block)
133+
br := NewBlockRes(block, false)
134134
*reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes())
135135
case "eth_getUncleCountByBlockNumber":
136136
args := new(GetBlockByNumberArgs)
@@ -139,7 +139,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
139139
}
140140

141141
block := api.xeth().EthBlockByNumber(args.BlockNumber)
142-
br := NewBlockRes(block)
142+
br := NewBlockRes(block, false)
143143
*reply = common.ToHex(big.NewInt(int64(len(br.Uncles))).Bytes())
144144
case "eth_getData", "eth_getCode":
145145
args := new(GetDataArgs)
@@ -179,8 +179,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
179179
}
180180

181181
block := api.xeth().EthBlockByHash(args.BlockHash)
182-
br := NewBlockRes(block)
183-
br.fullTx = args.IncludeTxs
182+
br := NewBlockRes(block, true)
184183

185184
*reply = br
186185
case "eth_getBlockByNumber":
@@ -190,8 +189,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
190189
}
191190

192191
block := api.xeth().EthBlockByNumber(args.BlockNumber)
193-
br := NewBlockRes(block)
194-
br.fullTx = args.IncludeTxs
192+
br := NewBlockRes(block, true)
195193

196194
*reply = br
197195
case "eth_getTransactionByHash":
@@ -214,8 +212,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
214212
}
215213

216214
block := api.xeth().EthBlockByHash(args.Hash)
217-
br := NewBlockRes(block)
218-
br.fullTx = true
215+
br := NewBlockRes(block, true)
219216

220217
if args.Index >= int64(len(br.Transactions)) || args.Index < 0 {
221218
return NewValidationError("Index", "does not exist")
@@ -228,8 +225,7 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
228225
}
229226

230227
block := api.xeth().EthBlockByNumber(args.BlockNumber)
231-
v := NewBlockRes(block)
232-
v.fullTx = true
228+
v := NewBlockRes(block, true)
233229

234230
if args.Index >= int64(len(v.Transactions)) || args.Index < 0 {
235231
return NewValidationError("Index", "does not exist")
@@ -241,14 +237,14 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
241237
return err
242238
}
243239

244-
br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash))
240+
br := NewBlockRes(api.xeth().EthBlockByHash(args.Hash), false)
245241

246242
if args.Index >= int64(len(br.Uncles)) || args.Index < 0 {
247243
return NewValidationError("Index", "does not exist")
248244
}
249245

250246
uhash := br.Uncles[args.Index]
251-
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.String()))
247+
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.String()), false)
252248

253249
*reply = uncle
254250
case "eth_getUncleByBlockNumberAndIndex":
@@ -258,15 +254,14 @@ func (api *EthereumApi) GetRequestReply(req *RpcRequest, reply *interface{}) err
258254
}
259255

260256
block := api.xeth().EthBlockByNumber(args.BlockNumber)
261-
v := NewBlockRes(block)
262-
v.fullTx = true
257+
v := NewBlockRes(block, true)
263258

264259
if args.Index >= int64(len(v.Uncles)) || args.Index < 0 {
265260
return NewValidationError("Index", "does not exist")
266261
}
267262

268263
uhash := v.Uncles[args.Index]
269-
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.String()))
264+
uncle := NewBlockRes(api.xeth().EthBlockByHash(uhash.String()), false)
270265

271266
*reply = uncle
272267
case "eth_getCompilers":

‎rpc/responses.go‎

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ type BlockRes struct {
2929
Uncles []*hexdata `json:"uncles"`
3030
}
3131

32-
func NewBlockRes(block *types.Block) *BlockRes {
32+
func NewBlockRes(block *types.Block, fullTx bool) *BlockRes {
33+
// TODO respect fullTx flag
34+
3335
if block == nil {
3436
return &BlockRes{}
3537
}
3638

3739
res := new(BlockRes)
40+
res.fullTx = fullTx
3841
res.BlockNumber = newHexNum(block.Number())
3942
res.BlockHash = newHexData(block.Hash())
4043
res.ParentHash = newHexData(block.ParentHash())
@@ -52,11 +55,20 @@ func NewBlockRes(block *types.Block) *BlockRes {
5255
// res.MinGasPrice =
5356
res.GasUsed = newHexNum(block.GasUsed())
5457
res.UnixTimestamp = newHexNum(block.Time())
55-
res.Transactions = NewTransactionsRes(block.Transactions())
58+
59+
res.Transactions = make([]*TransactionRes, len(block.Transactions()))
60+
for i, tx := range block.Transactions() {
61+
res.Transactions[i] = NewTransactionRes(tx)
62+
res.Transactions[i].BlockHash = res.BlockHash
63+
res.Transactions[i].BlockNumber = res.BlockNumber
64+
res.Transactions[i].TxIndex = newHexNum(i)
65+
}
66+
5667
res.Uncles = make([]*hexdata, len(block.Uncles()))
5768
for i, uncle := range block.Uncles() {
5869
res.Uncles[i] = newHexData(uncle.Hash())
5970
}
71+
6072
return res
6173
}
6274

@@ -91,14 +103,6 @@ func NewTransactionRes(tx *types.Transaction) *TransactionRes {
91103
return v
92104
}
93105

94-
func NewTransactionsRes(txs []*types.Transaction) []*TransactionRes {
95-
v := make([]*TransactionRes, len(txs))
96-
for i, tx := range txs {
97-
v[i] = NewTransactionRes(tx)
98-
}
99-
return v
100-
}
101-
102106
// type FilterLogRes struct {
103107
// Hash string `json:"hash"`
104108
// Address string `json:"address"`

‎rpc/responses_test.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func TestNewBlockRes(t *testing.T) {
4949
"timestamp": reNum,
5050
}
5151

52-
v := NewBlockRes(block)
52+
v := NewBlockRes(block, false)
5353
j, _ := json.Marshal(v)
5454

5555
for k, re := range tests {

0 commit comments

Comments
 (0)