@@ -22,6 +22,7 @@ import (
22
22
"crypto/ecdsa"
23
23
"encoding/json"
24
24
"errors"
25
+ "hash"
25
26
"math/big"
26
27
"reflect"
27
28
"sort"
@@ -45,6 +46,7 @@ import (
45
46
"github.com/ethereum/go-ethereum/event"
46
47
"github.com/ethereum/go-ethereum/params"
47
48
"github.com/ethereum/go-ethereum/rpc"
49
+ "golang.org/x/crypto/sha3"
48
50
)
49
51
50
52
func TestTransaction_RoundTripRpcJSON (t * testing.T ) {
@@ -549,7 +551,7 @@ func TestCall(t *testing.T) {
549
551
overrides : StateOverride {
550
552
randomAccounts [2 ].addr : OverrideAccount {
551
553
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 ))},
553
555
},
554
556
},
555
557
want : "0x000000000000000000000000000000000000000000000000000000000000007b" ,
@@ -624,3 +626,102 @@ func hex2Bytes(str string) *hexutil.Bytes {
624
626
rpcBytes := hexutil .Bytes (common .Hex2Bytes (str ))
625
627
return & rpcBytes
626
628
}
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