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