2016-11-09 04:01:56 +03:00
|
|
|
// Copyright 2016 The go-ethereum Authors
|
|
|
|
// This file is part of the go-ethereum library.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
// This file contains some shares testing functionality, common to multiple
|
|
|
|
// different files and modules being tested.
|
|
|
|
|
|
|
|
package les
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/rand"
|
|
|
|
"math/big"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2017-04-05 01:16:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
2017-01-17 14:19:50 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2016-10-14 06:51:29 +03:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/ethdb"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/les/flowcontrol"
|
|
|
|
"github.com/ethereum/go-ethereum/light"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
testBankKey, _ = crypto.HexToECDSA("b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291")
|
|
|
|
testBankAddress = crypto.PubkeyToAddress(testBankKey.PublicKey)
|
|
|
|
testBankFunds = big.NewInt(1000000)
|
|
|
|
|
|
|
|
acc1Key, _ = crypto.HexToECDSA("8a1f9a8f95be41cd7ccb6168179afb4504aefe388d1e14474d32c45c72ce7b7a")
|
|
|
|
acc2Key, _ = crypto.HexToECDSA("49a7b37aa6f6645917e7b807e9d1c00d4fa71f18343b0d4122a4d2df64dd6fee")
|
|
|
|
acc1Addr = crypto.PubkeyToAddress(acc1Key.PublicKey)
|
|
|
|
acc2Addr = crypto.PubkeyToAddress(acc2Key.PublicKey)
|
|
|
|
|
|
|
|
testContractCode = common.Hex2Bytes("606060405260cc8060106000396000f360606040526000357c01000000000000000000000000000000000000000000000000000000009004806360cd2685146041578063c16431b914606b57603f565b005b6055600480803590602001909190505060a9565b6040518082815260200191505060405180910390f35b60886004808035906020019091908035906020019091905050608a565b005b80600060005083606481101560025790900160005b50819055505b5050565b6000600060005082606481101560025790900160005b5054905060c7565b91905056")
|
|
|
|
testContractAddr common.Address
|
|
|
|
testContractCodeDeployed = testContractCode[16:]
|
|
|
|
testContractDeployed = uint64(2)
|
|
|
|
|
|
|
|
testBufLimit = uint64(100)
|
2017-01-04 22:17:24 +03:00
|
|
|
|
|
|
|
bigTxGas = new(big.Int).SetUint64(params.TxGas)
|
2016-10-14 06:51:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
/*
|
|
|
|
contract test {
|
|
|
|
|
|
|
|
uint256[100] data;
|
|
|
|
|
|
|
|
function Put(uint256 addr, uint256 value) {
|
|
|
|
data[addr] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
function Get(uint256 addr) constant returns (uint256 value) {
|
|
|
|
return data[addr];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
func testChainGen(i int, block *core.BlockGen) {
|
2016-11-02 15:44:13 +03:00
|
|
|
signer := types.HomesteadSigner{}
|
|
|
|
|
2016-10-14 06:51:29 +03:00
|
|
|
switch i {
|
|
|
|
case 0:
|
|
|
|
// In block 1, the test bank sends account #1 some ether.
|
2017-01-04 22:17:24 +03:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(10000), bigTxGas, nil, nil), signer, testBankKey)
|
2016-10-14 06:51:29 +03:00
|
|
|
block.AddTx(tx)
|
|
|
|
case 1:
|
|
|
|
// In block 2, the test bank sends some more ether to account #1.
|
|
|
|
// acc1Addr passes it on to account #2.
|
|
|
|
// acc1Addr creates a test contract.
|
2017-01-04 22:17:24 +03:00
|
|
|
tx1, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), acc1Addr, big.NewInt(1000), bigTxGas, nil, nil), signer, testBankKey)
|
2016-10-14 06:51:29 +03:00
|
|
|
nonce := block.TxNonce(acc1Addr)
|
2017-01-04 22:17:24 +03:00
|
|
|
tx2, _ := types.SignTx(types.NewTransaction(nonce, acc2Addr, big.NewInt(1000), bigTxGas, nil, nil), signer, acc1Key)
|
2016-10-14 06:51:29 +03:00
|
|
|
nonce++
|
2017-01-05 14:59:17 +03:00
|
|
|
tx3, _ := types.SignTx(types.NewContractCreation(nonce, big.NewInt(0), big.NewInt(200000), big.NewInt(0), testContractCode), signer, acc1Key)
|
2016-10-14 06:51:29 +03:00
|
|
|
testContractAddr = crypto.CreateAddress(acc1Addr, nonce)
|
|
|
|
block.AddTx(tx1)
|
|
|
|
block.AddTx(tx2)
|
|
|
|
block.AddTx(tx3)
|
|
|
|
case 2:
|
|
|
|
// Block 3 is empty but was mined by account #2.
|
|
|
|
block.SetCoinbase(acc2Addr)
|
|
|
|
block.SetExtra([]byte("yeehaw"))
|
|
|
|
data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000001")
|
2017-01-05 14:59:17 +03:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), big.NewInt(100000), nil, data), signer, testBankKey)
|
2016-10-14 06:51:29 +03:00
|
|
|
block.AddTx(tx)
|
|
|
|
case 3:
|
|
|
|
// Block 4 includes blocks 2 and 3 as uncle headers (with modified extra data).
|
|
|
|
b2 := block.PrevBlock(1).Header()
|
|
|
|
b2.Extra = []byte("foo")
|
|
|
|
block.AddUncle(b2)
|
|
|
|
b3 := block.PrevBlock(2).Header()
|
|
|
|
b3.Extra = []byte("foo")
|
|
|
|
block.AddUncle(b3)
|
|
|
|
data := common.Hex2Bytes("C16431B900000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002")
|
2017-01-05 14:59:17 +03:00
|
|
|
tx, _ := types.SignTx(types.NewTransaction(block.TxNonce(testBankAddress), testContractAddr, big.NewInt(0), big.NewInt(100000), nil, data), signer, testBankKey)
|
2016-10-14 06:51:29 +03:00
|
|
|
block.AddTx(tx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testRCL() RequestCostList {
|
|
|
|
cl := make(RequestCostList, len(reqList))
|
|
|
|
for i, code := range reqList {
|
|
|
|
cl[i].MsgCode = code
|
|
|
|
cl[i].BaseCost = 0
|
|
|
|
cl[i].ReqCost = 0
|
|
|
|
}
|
|
|
|
return cl
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestProtocolManager creates a new protocol manager for testing purposes,
|
|
|
|
// with the given number of blocks already known, and potential notification
|
|
|
|
// channels for different events.
|
2017-06-21 13:27:38 +03:00
|
|
|
func newTestProtocolManager(lightSync bool, blocks int, generator func(int, *core.BlockGen), peers *peerSet, odr *LesOdr, db ethdb.Database) (*ProtocolManager, error) {
|
2016-10-14 06:51:29 +03:00
|
|
|
var (
|
2017-04-05 01:16:29 +03:00
|
|
|
evmux = new(event.TypeMux)
|
|
|
|
engine = ethash.NewFaker()
|
|
|
|
gspec = core.Genesis{
|
2017-03-02 16:03:33 +03:00
|
|
|
Config: params.TestChainConfig,
|
|
|
|
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
|
|
|
|
}
|
|
|
|
genesis = gspec.MustCommit(db)
|
2017-08-08 19:31:08 +03:00
|
|
|
chain BlockChain
|
2016-10-14 06:51:29 +03:00
|
|
|
)
|
2017-06-21 13:27:38 +03:00
|
|
|
if peers == nil {
|
|
|
|
peers = newPeerSet()
|
|
|
|
}
|
2016-10-14 06:51:29 +03:00
|
|
|
|
|
|
|
if lightSync {
|
2017-08-18 13:58:36 +03:00
|
|
|
chain, _ = light.NewLightChain(odr, gspec.Config, engine)
|
2016-10-14 06:51:29 +03:00
|
|
|
} else {
|
2017-08-18 13:58:36 +03:00
|
|
|
blockchain, _ := core.NewBlockChain(db, gspec.Config, engine, vm.Config{})
|
2017-03-02 16:03:33 +03:00
|
|
|
gchain, _ := core.GenerateChain(gspec.Config, genesis, db, blocks, generator)
|
2016-10-14 06:51:29 +03:00
|
|
|
if _, err := blockchain.InsertChain(gchain); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
chain = blockchain
|
|
|
|
}
|
|
|
|
|
2017-06-21 13:27:38 +03:00
|
|
|
pm, err := NewProtocolManager(gspec.Config, lightSync, NetworkId, evmux, engine, peers, chain, nil, db, odr, nil, make(chan struct{}), new(sync.WaitGroup))
|
2016-10-14 06:51:29 +03:00
|
|
|
if err != nil {
|
2017-06-21 13:27:38 +03:00
|
|
|
return nil, err
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
if !lightSync {
|
|
|
|
srv := &LesServer{protocolManager: pm}
|
|
|
|
pm.server = srv
|
|
|
|
|
|
|
|
srv.defParams = &flowcontrol.ServerParams{
|
|
|
|
BufLimit: testBufLimit,
|
|
|
|
MinRecharge: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
srv.fcManager = flowcontrol.NewClientManager(50, 10, 1000000000)
|
|
|
|
srv.fcCostStats = newCostStats(nil)
|
|
|
|
}
|
2017-06-21 13:27:38 +03:00
|
|
|
pm.Start()
|
|
|
|
return pm, nil
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// newTestProtocolManagerMust creates a new protocol manager for testing purposes,
|
|
|
|
// with the given number of blocks already known, and potential notification
|
|
|
|
// channels for different events. In case of an error, the constructor force-
|
|
|
|
// fails the test.
|
2017-06-21 13:27:38 +03:00
|
|
|
func newTestProtocolManagerMust(t *testing.T, lightSync bool, blocks int, generator func(int, *core.BlockGen), peers *peerSet, odr *LesOdr, db ethdb.Database) *ProtocolManager {
|
|
|
|
pm, err := newTestProtocolManager(lightSync, blocks, generator, peers, odr, db)
|
2016-10-14 06:51:29 +03:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to create protocol manager: %v", err)
|
|
|
|
}
|
2017-06-21 13:27:38 +03:00
|
|
|
return pm
|
2016-10-14 06:51:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// testPeer is a simulated peer to allow testing direct network calls.
|
|
|
|
type testPeer struct {
|
|
|
|
net p2p.MsgReadWriter // Network layer reader/writer to simulate remote messaging
|
|
|
|
app *p2p.MsgPipeRW // Application layer reader/writer to simulate the local side
|
|
|
|
*peer
|
|
|
|
}
|
|
|
|
|
|
|
|
// newTestPeer creates a new peer registered at the given protocol manager.
|
|
|
|
func newTestPeer(t *testing.T, name string, version int, pm *ProtocolManager, shake bool) (*testPeer, <-chan error) {
|
|
|
|
// Create a message pipe to communicate through
|
|
|
|
app, net := p2p.MsgPipe()
|
|
|
|
|
|
|
|
// Generate a random id and create the peer
|
|
|
|
var id discover.NodeID
|
|
|
|
rand.Read(id[:])
|
|
|
|
|
|
|
|
peer := pm.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), net)
|
|
|
|
|
|
|
|
// Start the peer on a new thread
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case pm.newPeerCh <- peer:
|
|
|
|
errc <- pm.handle(peer)
|
|
|
|
case <-pm.quitSync:
|
|
|
|
errc <- p2p.DiscQuitting
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
tp := &testPeer{
|
|
|
|
app: app,
|
|
|
|
net: net,
|
|
|
|
peer: peer,
|
|
|
|
}
|
|
|
|
// Execute any implicitly requested handshakes and return
|
|
|
|
if shake {
|
|
|
|
td, head, genesis := pm.blockchain.Status()
|
|
|
|
headNum := pm.blockchain.CurrentHeader().Number.Uint64()
|
|
|
|
tp.handshake(t, td, head, headNum, genesis)
|
|
|
|
}
|
|
|
|
return tp, errc
|
|
|
|
}
|
|
|
|
|
|
|
|
func newTestPeerPair(name string, version int, pm, pm2 *ProtocolManager) (*peer, <-chan error, *peer, <-chan error) {
|
|
|
|
// Create a message pipe to communicate through
|
|
|
|
app, net := p2p.MsgPipe()
|
|
|
|
|
|
|
|
// Generate a random id and create the peer
|
|
|
|
var id discover.NodeID
|
|
|
|
rand.Read(id[:])
|
|
|
|
|
|
|
|
peer := pm.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), net)
|
|
|
|
peer2 := pm2.newPeer(version, NetworkId, p2p.NewPeer(id, name, nil), app)
|
|
|
|
|
|
|
|
// Start the peer on a new thread
|
|
|
|
errc := make(chan error, 1)
|
|
|
|
errc2 := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case pm.newPeerCh <- peer:
|
|
|
|
errc <- pm.handle(peer)
|
|
|
|
case <-pm.quitSync:
|
|
|
|
errc <- p2p.DiscQuitting
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
go func() {
|
|
|
|
select {
|
|
|
|
case pm2.newPeerCh <- peer2:
|
|
|
|
errc2 <- pm2.handle(peer2)
|
|
|
|
case <-pm2.quitSync:
|
|
|
|
errc2 <- p2p.DiscQuitting
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return peer, errc, peer2, errc2
|
|
|
|
}
|
|
|
|
|
|
|
|
// handshake simulates a trivial handshake that expects the same state from the
|
|
|
|
// remote side as we are simulating locally.
|
|
|
|
func (p *testPeer) handshake(t *testing.T, td *big.Int, head common.Hash, headNum uint64, genesis common.Hash) {
|
|
|
|
var expList keyValueList
|
|
|
|
expList = expList.add("protocolVersion", uint64(p.version))
|
|
|
|
expList = expList.add("networkId", uint64(NetworkId))
|
|
|
|
expList = expList.add("headTd", td)
|
|
|
|
expList = expList.add("headHash", head)
|
|
|
|
expList = expList.add("headNum", headNum)
|
|
|
|
expList = expList.add("genesisHash", genesis)
|
|
|
|
sendList := make(keyValueList, len(expList))
|
|
|
|
copy(sendList, expList)
|
|
|
|
expList = expList.add("serveHeaders", nil)
|
|
|
|
expList = expList.add("serveChainSince", uint64(0))
|
|
|
|
expList = expList.add("serveStateSince", uint64(0))
|
|
|
|
expList = expList.add("txRelay", nil)
|
|
|
|
expList = expList.add("flowControl/BL", testBufLimit)
|
|
|
|
expList = expList.add("flowControl/MRR", uint64(1))
|
|
|
|
expList = expList.add("flowControl/MRC", testRCL())
|
|
|
|
|
|
|
|
if err := p2p.ExpectMsg(p.app, StatusMsg, expList); err != nil {
|
|
|
|
t.Fatalf("status recv: %v", err)
|
|
|
|
}
|
|
|
|
if err := p2p.Send(p.app, StatusMsg, sendList); err != nil {
|
|
|
|
t.Fatalf("status send: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
p.fcServerParams = &flowcontrol.ServerParams{
|
|
|
|
BufLimit: testBufLimit,
|
|
|
|
MinRecharge: 1,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// close terminates the local side of the peer, notifying the remote protocol
|
|
|
|
// manager of termination.
|
|
|
|
func (p *testPeer) close() {
|
|
|
|
p.app.Close()
|
|
|
|
}
|