odr_test.go 6.7 KB

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