odr_test.go 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. // Copyright 2016 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 les
  17. import (
  18. "bytes"
  19. "math/big"
  20. "testing"
  21. "time"
  22. "github.com/ethereum/go-ethereum/common"
  23. "github.com/ethereum/go-ethereum/common/math"
  24. "github.com/ethereum/go-ethereum/core"
  25. "github.com/ethereum/go-ethereum/core/state"
  26. "github.com/ethereum/go-ethereum/core/types"
  27. "github.com/ethereum/go-ethereum/core/vm"
  28. "github.com/ethereum/go-ethereum/ethdb"
  29. "github.com/ethereum/go-ethereum/light"
  30. "github.com/ethereum/go-ethereum/params"
  31. "github.com/ethereum/go-ethereum/rlp"
  32. "golang.org/x/net/context"
  33. )
  34. type odrTestFn func(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte
  35. func TestOdrGetBlockLes1(t *testing.T) { testOdr(t, 1, 1, odrGetBlock) }
  36. func odrGetBlock(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
  37. var block *types.Block
  38. if bc != nil {
  39. block = bc.GetBlockByHash(bhash)
  40. } else {
  41. block, _ = lc.GetBlockByHash(ctx, bhash)
  42. }
  43. if block == nil {
  44. return nil
  45. }
  46. rlp, _ := rlp.EncodeToBytes(block)
  47. return rlp
  48. }
  49. func TestOdrGetReceiptsLes1(t *testing.T) { testOdr(t, 1, 1, odrGetReceipts) }
  50. func odrGetReceipts(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
  51. var receipts types.Receipts
  52. if bc != nil {
  53. receipts = core.GetBlockReceipts(db, bhash, core.GetBlockNumber(db, bhash))
  54. } else {
  55. receipts, _ = light.GetBlockReceipts(ctx, lc.Odr(), bhash, core.GetBlockNumber(db, bhash))
  56. }
  57. if receipts == nil {
  58. return nil
  59. }
  60. rlp, _ := rlp.EncodeToBytes(receipts)
  61. return rlp
  62. }
  63. func TestOdrAccountsLes1(t *testing.T) { testOdr(t, 1, 1, odrAccounts) }
  64. func odrAccounts(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
  65. dummyAddr := common.HexToAddress("1234567812345678123456781234567812345678")
  66. acc := []common.Address{testBankAddress, acc1Addr, acc2Addr, dummyAddr}
  67. var res []byte
  68. for _, addr := range acc {
  69. if bc != nil {
  70. header := bc.GetHeaderByHash(bhash)
  71. st, err := state.New(header.Root, db)
  72. if err == nil {
  73. bal := st.GetBalance(addr)
  74. rlp, _ := rlp.EncodeToBytes(bal)
  75. res = append(res, rlp...)
  76. }
  77. } else {
  78. header := lc.GetHeaderByHash(bhash)
  79. st := light.NewLightState(light.StateTrieID(header), lc.Odr())
  80. bal, err := st.GetBalance(ctx, addr)
  81. if err == nil {
  82. rlp, _ := rlp.EncodeToBytes(bal)
  83. res = append(res, rlp...)
  84. }
  85. }
  86. }
  87. return res
  88. }
  89. func TestOdrContractCallLes1(t *testing.T) { testOdr(t, 1, 2, odrContractCall) }
  90. type callmsg struct {
  91. types.Message
  92. }
  93. func (callmsg) CheckNonce() bool { return false }
  94. func odrContractCall(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
  95. data := common.Hex2Bytes("60CD26850000000000000000000000000000000000000000000000000000000000000000")
  96. var res []byte
  97. for i := 0; i < 3; i++ {
  98. data[35] = byte(i)
  99. if bc != nil {
  100. header := bc.GetHeaderByHash(bhash)
  101. statedb, err := state.New(header.Root, db)
  102. if err == nil {
  103. from := statedb.GetOrNewStateObject(testBankAddress)
  104. from.SetBalance(math.MaxBig256)
  105. msg := callmsg{types.NewMessage(from.Address(), &testContractAddr, 0, new(big.Int), big.NewInt(100000), new(big.Int), data, false)}
  106. context := core.NewEVMContext(msg, header, bc)
  107. vmenv := vm.NewEVM(context, statedb, config, vm.Config{})
  108. //vmenv := core.NewEnv(statedb, config, bc, msg, header, vm.Config{})
  109. gp := new(core.GasPool).AddGas(math.MaxBig256)
  110. ret, _, _ := core.ApplyMessage(vmenv, msg, gp)
  111. res = append(res, ret...)
  112. }
  113. } else {
  114. header := lc.GetHeaderByHash(bhash)
  115. state := light.NewLightState(light.StateTrieID(header), lc.Odr())
  116. vmstate := light.NewVMState(ctx, state)
  117. from, err := state.GetOrNewStateObject(ctx, testBankAddress)
  118. if err == nil {
  119. from.SetBalance(math.MaxBig256)
  120. msg := callmsg{types.NewMessage(from.Address(), &testContractAddr, 0, new(big.Int), big.NewInt(100000), new(big.Int), data, false)}
  121. context := core.NewEVMContext(msg, header, lc)
  122. vmenv := vm.NewEVM(context, vmstate, config, vm.Config{})
  123. //vmenv := light.NewEnv(ctx, state, config, lc, msg, header, vm.Config{})
  124. gp := new(core.GasPool).AddGas(math.MaxBig256)
  125. ret, _, _ := core.ApplyMessage(vmenv, msg, gp)
  126. if vmstate.Error() == nil {
  127. res = append(res, ret...)
  128. }
  129. }
  130. }
  131. }
  132. return res
  133. }
  134. func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) {
  135. // Assemble the test environment
  136. pm, db, odr := newTestProtocolManagerMust(t, false, 4, testChainGen)
  137. lpm, ldb, odr := newTestProtocolManagerMust(t, true, 0, nil)
  138. _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm)
  139. pool := &testServerPool{}
  140. pool.setPeer(lpeer)
  141. odr.serverPool = pool
  142. select {
  143. case <-time.After(time.Millisecond * 100):
  144. case err := <-err1:
  145. t.Fatalf("peer 1 handshake error: %v", err)
  146. case err := <-err2:
  147. t.Fatalf("peer 1 handshake error: %v", err)
  148. }
  149. lpm.synchronise(lpeer)
  150. test := func(expFail uint64) {
  151. for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
  152. bhash := core.GetCanonicalHash(db, i)
  153. b1 := fn(light.NoOdr, db, pm.chainConfig, pm.blockchain.(*core.BlockChain), nil, bhash)
  154. ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
  155. b2 := fn(ctx, ldb, lpm.chainConfig, nil, lpm.blockchain.(*light.LightChain), bhash)
  156. eq := bytes.Equal(b1, b2)
  157. exp := i < expFail
  158. if exp && !eq {
  159. t.Errorf("odr mismatch")
  160. }
  161. if !exp && eq {
  162. t.Errorf("unexpected odr match")
  163. }
  164. }
  165. }
  166. // temporarily remove peer to test odr fails
  167. pool.setPeer(nil)
  168. // expect retrievals to fail (except genesis block) without a les peer
  169. test(expFail)
  170. pool.setPeer(lpeer)
  171. // expect all retrievals to pass
  172. test(5)
  173. pool.setPeer(nil)
  174. // still expect all retrievals to pass, now data should be cached locally
  175. test(5)
  176. }