protocol_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // Copyright 2014 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 diff
  17. import (
  18. "bytes"
  19. "testing"
  20. "github.com/stretchr/testify/assert"
  21. "github.com/ethereum/go-ethereum/common"
  22. "github.com/ethereum/go-ethereum/core/types"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // Tests that the custom union field encoder and decoder works correctly.
  26. func TestDiffLayersPacketDataEncodeDecode(t *testing.T) {
  27. // Create a "random" hash for testing
  28. var hash common.Hash
  29. for i := range hash {
  30. hash[i] = byte(i)
  31. }
  32. testDiffLayers := []*types.DiffLayer{
  33. {
  34. BlockHash: common.HexToHash("0x1e9624dcd0874958723aa3dae1fe299861e93ef32b980143d798c428bdd7a20a"),
  35. Number: 10479133,
  36. Receipts: []*types.Receipt{{
  37. GasUsed: 100,
  38. TransactionIndex: 1,
  39. }},
  40. Codes: []types.DiffCode{{
  41. Hash: common.HexToHash("0xaece2dbf80a726206cf4df299afa09f9d8f3dcd85ff39bb4b3f0402a3a6af2f5"),
  42. Code: []byte{1, 2, 3, 4},
  43. }},
  44. Destructs: []common.Address{
  45. common.HexToAddress("0x0205bb28ece9289d3fb8eb0c9e999bbd5be2b931"),
  46. },
  47. Accounts: []types.DiffAccount{{
  48. Account: common.HexToAddress("0x18b2a687610328590bc8f2e5fedde3b582a49cda"),
  49. Blob: []byte{2, 3, 4, 5},
  50. }},
  51. Storages: []types.DiffStorage{{
  52. Account: common.HexToAddress("0x18b2a687610328590bc8f2e5fedde3b582a49cda"),
  53. Keys: []string{"abc"},
  54. Vals: [][]byte{{1, 2, 3}},
  55. }},
  56. },
  57. }
  58. // Assemble some table driven tests
  59. tests := []struct {
  60. diffLayers []*types.DiffLayer
  61. fail bool
  62. }{
  63. {fail: false, diffLayers: testDiffLayers},
  64. }
  65. // Iterate over each of the tests and try to encode and then decode
  66. for i, tt := range tests {
  67. originPacket := make([]rlp.RawValue, 0)
  68. for _, diff := range tt.diffLayers {
  69. bz, err := rlp.EncodeToBytes(diff)
  70. assert.NoError(t, err)
  71. originPacket = append(originPacket, bz)
  72. }
  73. bz, err := rlp.EncodeToBytes(DiffLayersPacket(originPacket))
  74. if err != nil && !tt.fail {
  75. t.Fatalf("test %d: failed to encode packet: %v", i, err)
  76. } else if err == nil && tt.fail {
  77. t.Fatalf("test %d: encode should have failed", i)
  78. }
  79. if !tt.fail {
  80. packet := new(DiffLayersPacket)
  81. if err := rlp.DecodeBytes(bz, packet); err != nil {
  82. t.Fatalf("test %d: failed to decode packet: %v", i, err)
  83. }
  84. diffLayers, err := packet.Unpack()
  85. assert.NoError(t, err)
  86. if len(diffLayers) != len(tt.diffLayers) {
  87. t.Fatalf("test %d: encode length mismatch: have %+v, want %+v", i, len(diffLayers), len(tt.diffLayers))
  88. }
  89. expectedPacket := make([]rlp.RawValue, 0)
  90. for _, diff := range diffLayers {
  91. bz, err := rlp.EncodeToBytes(diff)
  92. assert.NoError(t, err)
  93. expectedPacket = append(expectedPacket, bz)
  94. }
  95. for i := 0; i < len(expectedPacket); i++ {
  96. if !bytes.Equal(expectedPacket[i], originPacket[i]) {
  97. t.Fatalf("test %d: data change during encode and decode", i)
  98. }
  99. }
  100. }
  101. }
  102. }
  103. func TestDiffMessages(t *testing.T) {
  104. for i, tc := range []struct {
  105. message interface{}
  106. want []byte
  107. }{
  108. {
  109. DiffCapPacket{true, defaultExtra},
  110. common.FromHex("c20100"),
  111. },
  112. {
  113. GetDiffLayersPacket{1111, []common.Hash{common.HexToHash("0xaece2dbf80a726206cf4df299afa09f9d8f3dcd85ff39bb4b3f0402a3a6af2f5")}},
  114. common.FromHex("e5820457e1a0aece2dbf80a726206cf4df299afa09f9d8f3dcd85ff39bb4b3f0402a3a6af2f5"),
  115. },
  116. } {
  117. if have, _ := rlp.EncodeToBytes(tc.message); !bytes.Equal(have, tc.want) {
  118. t.Errorf("test %d, type %T, have\n\t%x\nwant\n\t%x", i, tc.message, have, tc.want)
  119. }
  120. }
  121. }