rlp_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // Copyright 2019 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. package core
  17. import (
  18. "fmt"
  19. "math/big"
  20. "testing"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/consensus/ethash"
  23. "github.com/ethereum/go-ethereum/core/rawdb"
  24. "github.com/ethereum/go-ethereum/core/types"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/params"
  27. "github.com/ethereum/go-ethereum/rlp"
  28. "golang.org/x/crypto/sha3"
  29. )
  30. func getBlock(transactions int, uncles int, dataSize int) *types.Block {
  31. var (
  32. aa = common.HexToAddress("0x000000000000000000000000000000000000aaaa")
  33. // Generate a canonical chain to act as the main dataset
  34. engine = ethash.NewFaker()
  35. db = rawdb.NewMemoryDatabase()
  36. // A sender who makes transactions, has some funds
  37. key, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
  38. address = crypto.PubkeyToAddress(key.PublicKey)
  39. funds = big.NewInt(1000000000)
  40. gspec = &Genesis{
  41. Config: params.TestChainConfig,
  42. Alloc: GenesisAlloc{address: {Balance: funds}},
  43. }
  44. genesis = gspec.MustCommit(db)
  45. )
  46. // We need to generate as many blocks +1 as uncles
  47. blocks, _ := GenerateChain(params.TestChainConfig, genesis, engine, db, uncles+1,
  48. func(n int, b *BlockGen) {
  49. if n == uncles {
  50. // Add transactions and stuff on the last block
  51. for i := 0; i < transactions; i++ {
  52. tx, _ := types.SignTx(types.NewTransaction(uint64(i), aa,
  53. big.NewInt(0), 50000, big.NewInt(1), make([]byte, dataSize)), types.HomesteadSigner{}, key)
  54. b.AddTx(tx)
  55. }
  56. for i := 0; i < uncles; i++ {
  57. b.AddUncle(&types.Header{ParentHash: b.PrevBlock(n - 1 - i).Hash(), Number: big.NewInt(int64(n - i))})
  58. }
  59. }
  60. })
  61. block := blocks[len(blocks)-1]
  62. return block
  63. }
  64. // TestRlpIterator tests that individual transactions can be picked out
  65. // from blocks without full unmarshalling/marshalling
  66. func TestRlpIterator(t *testing.T) {
  67. for _, tt := range []struct {
  68. txs int
  69. uncles int
  70. datasize int
  71. }{
  72. {0, 0, 0},
  73. {0, 2, 0},
  74. {10, 0, 0},
  75. {10, 2, 0},
  76. {10, 2, 50},
  77. } {
  78. testRlpIterator(t, tt.txs, tt.uncles, tt.datasize)
  79. }
  80. }
  81. func testRlpIterator(t *testing.T, txs, uncles, datasize int) {
  82. desc := fmt.Sprintf("%d txs [%d datasize] and %d uncles", txs, datasize, uncles)
  83. bodyRlp, _ := rlp.EncodeToBytes(getBlock(txs, uncles, datasize).Body())
  84. it, err := rlp.NewListIterator(bodyRlp)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. // Check that txs exist
  89. if !it.Next() {
  90. t.Fatal("expected two elems, got zero")
  91. }
  92. txdata := it.Value()
  93. // Check that uncles exist
  94. if !it.Next() {
  95. t.Fatal("expected two elems, got one")
  96. }
  97. // No more after that
  98. if it.Next() {
  99. t.Fatal("expected only two elems, got more")
  100. }
  101. txIt, err := rlp.NewListIterator(txdata)
  102. if err != nil {
  103. t.Fatal(err)
  104. }
  105. var gotHashes []common.Hash
  106. var expHashes []common.Hash
  107. for txIt.Next() {
  108. gotHashes = append(gotHashes, crypto.Keccak256Hash(txIt.Value()))
  109. }
  110. var expBody types.Body
  111. err = rlp.DecodeBytes(bodyRlp, &expBody)
  112. if err != nil {
  113. t.Fatal(err)
  114. }
  115. for _, tx := range expBody.Transactions {
  116. expHashes = append(expHashes, tx.Hash())
  117. }
  118. if gotLen, expLen := len(gotHashes), len(expHashes); gotLen != expLen {
  119. t.Fatalf("testcase %v: length wrong, got %d exp %d", desc, gotLen, expLen)
  120. }
  121. // also sanity check against input
  122. if gotLen := len(gotHashes); gotLen != txs {
  123. t.Fatalf("testcase %v: length wrong, got %d exp %d", desc, gotLen, txs)
  124. }
  125. for i, got := range gotHashes {
  126. if exp := expHashes[i]; got != exp {
  127. t.Errorf("testcase %v: hash wrong, got %x, exp %x", desc, got, exp)
  128. }
  129. }
  130. }
  131. // BenchmarkHashing compares the speeds of hashing a rlp raw data directly
  132. // without the unmarshalling/marshalling step
  133. func BenchmarkHashing(b *testing.B) {
  134. // Make a pretty fat block
  135. var (
  136. bodyRlp []byte
  137. blockRlp []byte
  138. )
  139. {
  140. block := getBlock(200, 2, 50)
  141. bodyRlp, _ = rlp.EncodeToBytes(block.Body())
  142. blockRlp, _ = rlp.EncodeToBytes(block)
  143. }
  144. var got common.Hash
  145. var hasher = sha3.NewLegacyKeccak256()
  146. b.Run("iteratorhashing", func(b *testing.B) {
  147. b.ResetTimer()
  148. for i := 0; i < b.N; i++ {
  149. var hash common.Hash
  150. it, err := rlp.NewListIterator(bodyRlp)
  151. if err != nil {
  152. b.Fatal(err)
  153. }
  154. it.Next()
  155. txs := it.Value()
  156. txIt, err := rlp.NewListIterator(txs)
  157. if err != nil {
  158. b.Fatal(err)
  159. }
  160. for txIt.Next() {
  161. hasher.Reset()
  162. hasher.Write(txIt.Value())
  163. hasher.Sum(hash[:0])
  164. got = hash
  165. }
  166. }
  167. })
  168. var exp common.Hash
  169. b.Run("fullbodyhashing", func(b *testing.B) {
  170. b.ResetTimer()
  171. for i := 0; i < b.N; i++ {
  172. var body types.Body
  173. rlp.DecodeBytes(bodyRlp, &body)
  174. for _, tx := range body.Transactions {
  175. exp = tx.Hash()
  176. }
  177. }
  178. })
  179. b.Run("fullblockhashing", func(b *testing.B) {
  180. b.ResetTimer()
  181. for i := 0; i < b.N; i++ {
  182. var block types.Block
  183. rlp.DecodeBytes(blockRlp, &block)
  184. for _, tx := range block.Transactions() {
  185. tx.Hash()
  186. }
  187. }
  188. })
  189. if got != exp {
  190. b.Fatalf("hash wrong, got %x exp %x", got, exp)
  191. }
  192. }