Skip to content

Commit 89a769e

Browse files
committed
internal/ethapi: minor refactor in block serialization ethereum#27268
1 parent 4472b1c commit 89a769e

File tree

2 files changed

+132
-18
lines changed

2 files changed

+132
-18
lines changed

internal/ethapi/api.go

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,21 +1448,18 @@ func RPCMarshalBlock(block *types.Block, inclTx bool, fullTx bool, config *param
14481448
fields["size"] = hexutil.Uint64(block.Size())
14491449

14501450
if inclTx {
1451-
formatTx := func(tx *types.Transaction) (interface{}, error) {
1452-
return tx.Hash(), nil
1451+
formatTx := func(idx int, tx *types.Transaction) interface{} {
1452+
return tx.Hash()
14531453
}
14541454
if fullTx {
1455-
formatTx = func(tx *types.Transaction) (interface{}, error) {
1456-
return newRPCTransactionFromBlockHash(block, tx.Hash(), config), nil
1455+
formatTx = func(idx int, tx *types.Transaction) interface{} {
1456+
return newRPCTransactionFromBlockIndex(block, uint64(idx), config)
14571457
}
14581458
}
14591459
txs := block.Transactions()
14601460
transactions := make([]interface{}, len(txs))
1461-
var err error
14621461
for i, tx := range txs {
1463-
if transactions[i], err = formatTx(tx); err != nil {
1464-
return nil, err
1465-
}
1462+
transactions[i] = formatTx(i, tx)
14661463
}
14671464
fields["transactions"] = transactions
14681465
}
@@ -1737,16 +1734,6 @@ func newRPCRawTransactionFromBlockIndex(b *types.Block, index uint64) hexutil.By
17371734
return blob
17381735
}
17391736

1740-
// newRPCTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
1741-
func newRPCTransactionFromBlockHash(b *types.Block, hash common.Hash, config *params.ChainConfig) *RPCTransaction {
1742-
for idx, tx := range b.Transactions() {
1743-
if tx.Hash() == hash {
1744-
return newRPCTransactionFromBlockIndex(b, uint64(idx), config)
1745-
}
1746-
}
1747-
return nil
1748-
}
1749-
17501737
// accessListResult returns an optional accesslist
17511738
// Its the result of the `debug_createAccessList` RPC call.
17521739
// It contains an error if the transaction itself failed.

internal/ethapi/api_test.go

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
// Copyright 2023 The go-ethereum Authors
2+
// This file is part of the go-ethereum library.
3+
//
4+
// The go-ethereum library is free software: you can redistribute it and/or modify
5+
// it under the terms of the GNU Lesser General Public License as published by
6+
// the Free Software Foundation, either version 3 of the License, or
7+
// (at your option) any later version.
8+
//
9+
// The go-ethereum library is distributed in the hope that it will be useful,
10+
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
// GNU Lesser General Public License for more details.
13+
//
14+
// You should have received a copy of the GNU Lesser General Public License
15+
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
16+
17+
package ethapi
18+
19+
import (
20+
"encoding/json"
21+
"hash"
22+
"math/big"
23+
"testing"
24+
25+
"github.com/XinFinOrg/XDPoSChain/common"
26+
"github.com/XinFinOrg/XDPoSChain/core/types"
27+
"github.com/XinFinOrg/XDPoSChain/params"
28+
"golang.org/x/crypto/sha3"
29+
)
30+
31+
// testHasher is the helper tool for transaction/receipt list hashing.
32+
// The original hasher is trie, in order to get rid of import cycle,
33+
// use the testing hasher instead.
34+
type testHasher struct {
35+
hasher hash.Hash
36+
}
37+
38+
func newHasher() *testHasher {
39+
return &testHasher{hasher: sha3.NewLegacyKeccak256()}
40+
}
41+
42+
func (h *testHasher) Reset() {
43+
h.hasher.Reset()
44+
}
45+
46+
func (h *testHasher) Update(key, val []byte) {
47+
h.hasher.Write(key)
48+
h.hasher.Write(val)
49+
}
50+
51+
func (h *testHasher) Hash() common.Hash {
52+
return common.BytesToHash(h.hasher.Sum(nil))
53+
}
54+
55+
func TestRPCMarshalBlock(t *testing.T) {
56+
var (
57+
txs []*types.Transaction
58+
to = common.BytesToAddress([]byte{0x11})
59+
)
60+
for i := uint64(1); i <= 4; i++ {
61+
var tx *types.Transaction
62+
if i%2 == 0 {
63+
tx = types.NewTx(&types.LegacyTx{
64+
Nonce: i,
65+
GasPrice: big.NewInt(11111),
66+
Gas: 1111,
67+
To: &to,
68+
Value: big.NewInt(111),
69+
Data: []byte{0x11, 0x11, 0x11},
70+
})
71+
} else {
72+
tx = types.NewTx(&types.AccessListTx{
73+
ChainID: big.NewInt(1337),
74+
Nonce: i,
75+
GasPrice: big.NewInt(11111),
76+
Gas: 1111,
77+
To: &to,
78+
Value: big.NewInt(111),
79+
Data: []byte{0x11, 0x11, 0x11},
80+
})
81+
}
82+
txs = append(txs, tx)
83+
}
84+
block := types.NewBlock(&types.Header{Number: big.NewInt(100)}, txs, nil, nil, newHasher())
85+
86+
var testSuite = []struct {
87+
inclTx bool
88+
fullTx bool
89+
want string
90+
}{
91+
// without txs
92+
{
93+
inclTx: false,
94+
fullTx: false,
95+
want: `{"difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x2cb4e4b5b5be5a2520377e87e8d7d2cf83fc0783fa6518d67b9606d3c5317b50","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x64","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","penalties":"0x","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x299","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","transactionsRoot":"0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e","uncles":[],"validator":"0x","validators":"0x"}`,
96+
},
97+
// only tx hashes
98+
{
99+
inclTx: true,
100+
fullTx: false,
101+
want: `{"difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x2cb4e4b5b5be5a2520377e87e8d7d2cf83fc0783fa6518d67b9606d3c5317b50","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x64","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","penalties":"0x","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x299","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","transactions":["0x7d39df979e34172322c64983a9ad48302c2b889e55bda35324afecf043a77605","0x9bba4c34e57c875ff57ac8d172805a26ae912006985395dc1bdf8f44140a7bf4","0x98909ea1ff040da6be56bc4231d484de1414b3c1dac372d69293a4beb9032cb5","0x12e1f81207b40c3bdcc13c0ee18f5f86af6d31754d57a0ea1b0d4cfef21abef1"],"transactionsRoot":"0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e","uncles":[],"validator":"0x","validators":"0x"}`,
102+
},
103+
104+
// full tx details
105+
{
106+
inclTx: true,
107+
fullTx: true,
108+
want: `{"difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x2cb4e4b5b5be5a2520377e87e8d7d2cf83fc0783fa6518d67b9606d3c5317b50","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x64","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","penalties":"0x","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x299","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","transactions":[{"blockHash":"0x2cb4e4b5b5be5a2520377e87e8d7d2cf83fc0783fa6518d67b9606d3c5317b50","blockNumber":"0x64","from":"0x0000000000000000000000000000000000000000","gas":"0x457","gasPrice":"0x2b67","hash":"0x7d39df979e34172322c64983a9ad48302c2b889e55bda35324afecf043a77605","input":"0x111111","nonce":"0x1","to":"0x0000000000000000000000000000000000000011","transactionIndex":"0x0","value":"0x6f","type":"0x1","accessList":[],"chainId":"0x539","v":"0x0","r":"0x0","s":"0x0"},{"blockHash":"0x2cb4e4b5b5be5a2520377e87e8d7d2cf83fc0783fa6518d67b9606d3c5317b50","blockNumber":"0x64","from":"0x0000000000000000000000000000000000000000","gas":"0x457","gasPrice":"0x2b67","hash":"0x9bba4c34e57c875ff57ac8d172805a26ae912006985395dc1bdf8f44140a7bf4","input":"0x111111","nonce":"0x2","to":"0x0000000000000000000000000000000000000011","transactionIndex":"0x1","value":"0x6f","type":"0x0","v":"0x0","r":"0x0","s":"0x0"},{"blockHash":"0x2cb4e4b5b5be5a2520377e87e8d7d2cf83fc0783fa6518d67b9606d3c5317b50","blockNumber":"0x64","from":"0x0000000000000000000000000000000000000000","gas":"0x457","gasPrice":"0x2b67","hash":"0x98909ea1ff040da6be56bc4231d484de1414b3c1dac372d69293a4beb9032cb5","input":"0x111111","nonce":"0x3","to":"0x0000000000000000000000000000000000000011","transactionIndex":"0x2","value":"0x6f","type":"0x1","accessList":[],"chainId":"0x539","v":"0x0","r":"0x0","s":"0x0"},{"blockHash":"0x2cb4e4b5b5be5a2520377e87e8d7d2cf83fc0783fa6518d67b9606d3c5317b50","blockNumber":"0x64","from":"0x0000000000000000000000000000000000000000","gas":"0x457","gasPrice":"0x2b67","hash":"0x12e1f81207b40c3bdcc13c0ee18f5f86af6d31754d57a0ea1b0d4cfef21abef1","input":"0x111111","nonce":"0x4","to":"0x0000000000000000000000000000000000000011","transactionIndex":"0x3","value":"0x6f","type":"0x0","v":"0x0","r":"0x0","s":"0x0"}],"transactionsRoot":"0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e","uncles":[],"validator":"0x","validators":"0x"}`,
109+
},
110+
}
111+
112+
for i, tc := range testSuite {
113+
resp, err := RPCMarshalBlock(block, tc.inclTx, tc.fullTx, params.MainnetChainConfig)
114+
if err != nil {
115+
t.Errorf("test %d: got error %v", i, err)
116+
continue
117+
}
118+
out, err := json.Marshal(resp)
119+
if err != nil {
120+
t.Errorf("test %d: json marshal error: %v", i, err)
121+
continue
122+
}
123+
if have := string(out); have != tc.want {
124+
t.Errorf("test %d:\nwant: %s\nhave: %s", i, tc.want, have)
125+
}
126+
}
127+
}

0 commit comments

Comments
 (0)