odr_test.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. // fullcallmsg is the message type used for call transations.
  90. type fullcallmsg struct {
  91. from *state.StateObject
  92. to *common.Address
  93. gas, gasPrice *big.Int
  94. value *big.Int
  95. data []byte
  96. }
  97. // accessor boilerplate to implement core.Message
  98. func (m fullcallmsg) From() (common.Address, error) { return m.from.Address(), nil }
  99. func (m fullcallmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil }
  100. func (m fullcallmsg) Nonce() uint64 { return 0 }
  101. func (m fullcallmsg) CheckNonce() bool { return false }
  102. func (m fullcallmsg) To() *common.Address { return m.to }
  103. func (m fullcallmsg) GasPrice() *big.Int { return m.gasPrice }
  104. func (m fullcallmsg) Gas() *big.Int { return m.gas }
  105. func (m fullcallmsg) Value() *big.Int { return m.value }
  106. func (m fullcallmsg) Data() []byte { return m.data }
  107. // callmsg is the message type used for call transations.
  108. type lightcallmsg struct {
  109. from *light.StateObject
  110. to *common.Address
  111. gas, gasPrice *big.Int
  112. value *big.Int
  113. data []byte
  114. }
  115. // accessor boilerplate to implement core.Message
  116. func (m lightcallmsg) From() (common.Address, error) { return m.from.Address(), nil }
  117. func (m lightcallmsg) FromFrontier() (common.Address, error) { return m.from.Address(), nil }
  118. func (m lightcallmsg) Nonce() uint64 { return 0 }
  119. func (m lightcallmsg) CheckNonce() bool { return false }
  120. func (m lightcallmsg) To() *common.Address { return m.to }
  121. func (m lightcallmsg) GasPrice() *big.Int { return m.gasPrice }
  122. func (m lightcallmsg) Gas() *big.Int { return m.gas }
  123. func (m lightcallmsg) Value() *big.Int { return m.value }
  124. func (m lightcallmsg) Data() []byte { return m.data }
  125. func odrContractCall(ctx context.Context, db ethdb.Database, config *params.ChainConfig, bc *core.BlockChain, lc *light.LightChain, bhash common.Hash) []byte {
  126. data := common.Hex2Bytes("60CD26850000000000000000000000000000000000000000000000000000000000000000")
  127. var res []byte
  128. for i := 0; i < 3; i++ {
  129. data[35] = byte(i)
  130. if bc != nil {
  131. header := bc.GetHeaderByHash(bhash)
  132. statedb, err := state.New(header.Root, db)
  133. if err == nil {
  134. from := statedb.GetOrNewStateObject(testBankAddress)
  135. from.SetBalance(common.MaxBig)
  136. msg := fullcallmsg{
  137. from: from,
  138. gas: big.NewInt(100000),
  139. gasPrice: big.NewInt(0),
  140. value: big.NewInt(0),
  141. data: data,
  142. to: &testContractAddr,
  143. }
  144. vmenv := core.NewEnv(statedb, config, bc, msg, header, vm.Config{})
  145. gp := new(core.GasPool).AddGas(common.MaxBig)
  146. ret, _, _ := core.ApplyMessage(vmenv, msg, gp)
  147. res = append(res, ret...)
  148. }
  149. } else {
  150. header := lc.GetHeaderByHash(bhash)
  151. state := light.NewLightState(light.StateTrieID(header), lc.Odr())
  152. from, err := state.GetOrNewStateObject(ctx, testBankAddress)
  153. if err == nil {
  154. from.SetBalance(common.MaxBig)
  155. msg := lightcallmsg{
  156. from: from,
  157. gas: big.NewInt(100000),
  158. gasPrice: big.NewInt(0),
  159. value: big.NewInt(0),
  160. data: data,
  161. to: &testContractAddr,
  162. }
  163. vmenv := light.NewEnv(ctx, state, config, lc, msg, header, vm.Config{})
  164. gp := new(core.GasPool).AddGas(common.MaxBig)
  165. ret, _, _ := core.ApplyMessage(vmenv, msg, gp)
  166. if vmenv.Error() == nil {
  167. res = append(res, ret...)
  168. }
  169. }
  170. }
  171. }
  172. return res
  173. }
  174. func testOdr(t *testing.T, protocol int, expFail uint64, fn odrTestFn) {
  175. // Assemble the test environment
  176. pm, db, odr := newTestProtocolManagerMust(t, false, 4, testChainGen)
  177. lpm, ldb, odr := newTestProtocolManagerMust(t, true, 0, nil)
  178. _, err1, lpeer, err2 := newTestPeerPair("peer", protocol, pm, lpm)
  179. select {
  180. case <-time.After(time.Millisecond * 100):
  181. case err := <-err1:
  182. t.Fatalf("peer 1 handshake error: %v", err)
  183. case err := <-err2:
  184. t.Fatalf("peer 1 handshake error: %v", err)
  185. }
  186. lpm.synchronise(lpeer)
  187. test := func(expFail uint64) {
  188. for i := uint64(0); i <= pm.blockchain.CurrentHeader().Number.Uint64(); i++ {
  189. bhash := core.GetCanonicalHash(db, i)
  190. b1 := fn(light.NoOdr, db, pm.chainConfig, pm.blockchain.(*core.BlockChain), nil, bhash)
  191. ctx, _ := context.WithTimeout(context.Background(), 200*time.Millisecond)
  192. b2 := fn(ctx, ldb, lpm.chainConfig, nil, lpm.blockchain.(*light.LightChain), bhash)
  193. eq := bytes.Equal(b1, b2)
  194. exp := i < expFail
  195. if exp && !eq {
  196. t.Errorf("odr mismatch")
  197. }
  198. if !exp && eq {
  199. t.Errorf("unexpected odr match")
  200. }
  201. }
  202. }
  203. // temporarily remove peer to test odr fails
  204. odr.UnregisterPeer(lpeer)
  205. // expect retrievals to fail (except genesis block) without a les peer
  206. test(expFail)
  207. odr.RegisterPeer(lpeer)
  208. // expect all retrievals to pass
  209. test(5)
  210. odr.UnregisterPeer(lpeer)
  211. // still expect all retrievals to pass, now data should be cached locally
  212. test(5)
  213. }