Skip to content

Commit 6e3aa86

Browse files
authored
internal/ethapi: minor refactor in block serialization (#27268)
1 parent c2148c6 commit 6e3aa86

File tree

2 files changed

+107
-19
lines changed

2 files changed

+107
-19
lines changed

internal/ethapi/api.go

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

12651265
if inclTx {
1266-
formatTx := func(tx *types.Transaction) (interface{}, error) {
1267-
return tx.Hash(), nil
1266+
formatTx := func(idx int, tx *types.Transaction) interface{} {
1267+
return tx.Hash()
12681268
}
12691269
if fullTx {
1270-
formatTx = func(tx *types.Transaction) (interface{}, error) {
1271-
return newRPCTransactionFromBlockHash(block, tx.Hash(), config), nil
1270+
formatTx = func(idx int, tx *types.Transaction) interface{} {
1271+
return newRPCTransactionFromBlockIndex(block, uint64(idx), config)
12721272
}
12731273
}
12741274
txs := block.Transactions()
12751275
transactions := make([]interface{}, len(txs))
1276-
var err error
12771276
for i, tx := range txs {
1278-
if transactions[i], err = formatTx(tx); err != nil {
1279-
return nil, err
1280-
}
1277+
transactions[i] = formatTx(i, tx)
12811278
}
12821279
fields["transactions"] = transactions
12831280
}
@@ -1424,16 +1421,6 @@ func newRPCRawTransactionFromBlockIndex(b *types.Block, index uint64) hexutil.By
14241421
return blob
14251422
}
14261423

1427-
// newRPCTransactionFromBlockHash returns a transaction that will serialize to the RPC representation.
1428-
func newRPCTransactionFromBlockHash(b *types.Block, hash common.Hash, config *params.ChainConfig) *RPCTransaction {
1429-
for idx, tx := range b.Transactions() {
1430-
if tx.Hash() == hash {
1431-
return newRPCTransactionFromBlockIndex(b, uint64(idx), config)
1432-
}
1433-
}
1434-
return nil
1435-
}
1436-
14371424
// accessListResult returns an optional accesslist
14381425
// It's the result of the `debug_createAccessList` RPC call.
14391426
// It contains an error if the transaction itself failed.

internal/ethapi/api_test.go

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"crypto/ecdsa"
2323
"encoding/json"
2424
"errors"
25+
"hash"
2526
"math/big"
2627
"reflect"
2728
"sort"
@@ -45,6 +46,7 @@ import (
4546
"github.com/ethereum/go-ethereum/event"
4647
"github.com/ethereum/go-ethereum/params"
4748
"github.com/ethereum/go-ethereum/rpc"
49+
"golang.org/x/crypto/sha3"
4850
)
4951

5052
func TestTransaction_RoundTripRpcJSON(t *testing.T) {
@@ -549,7 +551,7 @@ func TestCall(t *testing.T) {
549551
overrides: StateOverride{
550552
randomAccounts[2].addr: OverrideAccount{
551553
Code: hex2Bytes("6080604052348015600f57600080fd5b506004361060285760003560e01c80638381f58a14602d575b600080fd5b60336049565b6040518082815260200191505060405180910390f35b6000548156fea2646970667358221220eab35ffa6ab2adfe380772a48b8ba78e82a1b820a18fcb6f59aa4efb20a5f60064736f6c63430007040033"),
552-
StateDiff: &map[common.Hash]common.Hash{common.Hash{}: common.BigToHash(big.NewInt(123))},
554+
StateDiff: &map[common.Hash]common.Hash{{}: common.BigToHash(big.NewInt(123))},
553555
},
554556
},
555557
want: "0x000000000000000000000000000000000000000000000000000000000000007b",
@@ -624,3 +626,102 @@ func hex2Bytes(str string) *hexutil.Bytes {
624626
rpcBytes := hexutil.Bytes(common.Hex2Bytes(str))
625627
return &rpcBytes
626628
}
629+
630+
// testHasher is the helper tool for transaction/receipt list hashing.
631+
// The original hasher is trie, in order to get rid of import cycle,
632+
// use the testing hasher instead.
633+
type testHasher struct {
634+
hasher hash.Hash
635+
}
636+
637+
func newHasher() *testHasher {
638+
return &testHasher{hasher: sha3.NewLegacyKeccak256()}
639+
}
640+
641+
func (h *testHasher) Reset() {
642+
h.hasher.Reset()
643+
}
644+
645+
func (h *testHasher) Update(key, val []byte) error {
646+
h.hasher.Write(key)
647+
h.hasher.Write(val)
648+
return nil
649+
}
650+
651+
func (h *testHasher) Hash() common.Hash {
652+
return common.BytesToHash(h.hasher.Sum(nil))
653+
}
654+
655+
func TestRPCMarshalBlock(t *testing.T) {
656+
var (
657+
txs []*types.Transaction
658+
to = common.BytesToAddress([]byte{0x11})
659+
)
660+
for i := uint64(1); i <= 4; i++ {
661+
var tx *types.Transaction
662+
if i%2 == 0 {
663+
tx = types.NewTx(&types.LegacyTx{
664+
Nonce: i,
665+
GasPrice: big.NewInt(11111),
666+
Gas: 1111,
667+
To: &to,
668+
Value: big.NewInt(111),
669+
Data: []byte{0x11, 0x11, 0x11},
670+
})
671+
} else {
672+
tx = types.NewTx(&types.AccessListTx{
673+
ChainID: big.NewInt(1337),
674+
Nonce: i,
675+
GasPrice: big.NewInt(11111),
676+
Gas: 1111,
677+
To: &to,
678+
Value: big.NewInt(111),
679+
Data: []byte{0x11, 0x11, 0x11},
680+
})
681+
}
682+
txs = append(txs, tx)
683+
}
684+
block := types.NewBlock(&types.Header{Number: big.NewInt(100)}, txs, nil, nil, newHasher())
685+
686+
var testSuite = []struct {
687+
inclTx bool
688+
fullTx bool
689+
want string
690+
}{
691+
// without txs
692+
{
693+
inclTx: false,
694+
fullTx: false,
695+
want: `{"difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x64","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x296","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","transactionsRoot":"0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e","uncles":[]}`,
696+
},
697+
// only tx hashes
698+
{
699+
inclTx: true,
700+
fullTx: false,
701+
want: `{"difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x64","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x296","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","transactions":["0x7d39df979e34172322c64983a9ad48302c2b889e55bda35324afecf043a77605","0x9bba4c34e57c875ff57ac8d172805a26ae912006985395dc1bdf8f44140a7bf4","0x98909ea1ff040da6be56bc4231d484de1414b3c1dac372d69293a4beb9032cb5","0x12e1f81207b40c3bdcc13c0ee18f5f86af6d31754d57a0ea1b0d4cfef21abef1"],"transactionsRoot":"0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e","uncles":[]}`,
702+
},
703+
704+
// full tx details
705+
{
706+
inclTx: true,
707+
fullTx: true,
708+
want: `{"difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","hash":"0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","number":"0x64","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","receiptsRoot":"0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421","sha3Uncles":"0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347","size":"0x296","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","transactions":[{"blockHash":"0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee","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":"0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee","blockNumber":"0x64","from":"0x0000000000000000000000000000000000000000","gas":"0x457","gasPrice":"0x2b67","hash":"0x9bba4c34e57c875ff57ac8d172805a26ae912006985395dc1bdf8f44140a7bf4","input":"0x111111","nonce":"0x2","to":"0x0000000000000000000000000000000000000011","transactionIndex":"0x1","value":"0x6f","type":"0x0","chainId":"0x7fffffffffffffee","v":"0x0","r":"0x0","s":"0x0"},{"blockHash":"0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee","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":"0x9b73c83b25d0faf7eab854e3684c7e394336d6e135625aafa5c183f27baa8fee","blockNumber":"0x64","from":"0x0000000000000000000000000000000000000000","gas":"0x457","gasPrice":"0x2b67","hash":"0x12e1f81207b40c3bdcc13c0ee18f5f86af6d31754d57a0ea1b0d4cfef21abef1","input":"0x111111","nonce":"0x4","to":"0x0000000000000000000000000000000000000011","transactionIndex":"0x3","value":"0x6f","type":"0x0","chainId":"0x7fffffffffffffee","v":"0x0","r":"0x0","s":"0x0"}],"transactionsRoot":"0x661a9febcfa8f1890af549b874faf9fa274aede26ef489d9db0b25daa569450e","uncles":[]}`,
709+
},
710+
}
711+
712+
for i, tc := range testSuite {
713+
resp, err := RPCMarshalBlock(block, tc.inclTx, tc.fullTx, params.MainnetChainConfig)
714+
if err != nil {
715+
t.Errorf("test %d: got error %v", i, err)
716+
continue
717+
}
718+
out, err := json.Marshal(resp)
719+
if err != nil {
720+
t.Errorf("test %d: json marshal error: %v", i, err)
721+
continue
722+
}
723+
if have := string(out); have != tc.want {
724+
t.Errorf("test %d: want: %s have: %s", i, tc.want, have)
725+
}
726+
}
727+
}

0 commit comments

Comments
 (0)