2022-05-24 20:39:40 +02:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2020-12-14 11:27:15 +02:00
|
|
|
// 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/>.
|
|
|
|
|
|
|
|
package eth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2023-05-25 08:57:02 +08:00
|
|
|
"strconv"
|
|
|
|
"sync"
|
2020-12-14 11:27:15 +02:00
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 12:23:02 +01:00
|
|
|
"github.com/ethereum/go-ethereum/consensus"
|
2020-12-14 11:27:15 +02:00
|
|
|
"github.com/ethereum/go-ethereum/consensus/ethash"
|
|
|
|
"github.com/ethereum/go-ethereum/core"
|
|
|
|
"github.com/ethereum/go-ethereum/core/forkid"
|
|
|
|
"github.com/ethereum/go-ethereum/core/rawdb"
|
2023-06-16 15:29:40 +03:00
|
|
|
"github.com/ethereum/go-ethereum/core/txpool"
|
2020-12-14 11:27:15 +02:00
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
|
|
"github.com/ethereum/go-ethereum/eth/downloader"
|
2023-04-10 18:36:45 +08:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/bsc"
|
2020-12-14 11:27:15 +02:00
|
|
|
"github.com/ethereum/go-ethereum/eth/protocols/eth"
|
|
|
|
"github.com/ethereum/go-ethereum/event"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/params"
|
|
|
|
)
|
|
|
|
|
|
|
|
// testEthHandler is a mock event handler to listen for inbound network requests
|
|
|
|
// on the `eth` protocol and convert them into a more easily testable form.
|
|
|
|
type testEthHandler struct {
|
|
|
|
blockBroadcasts event.Feed
|
|
|
|
txAnnounces event.Feed
|
|
|
|
txBroadcasts event.Feed
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *testEthHandler) Chain() *core.BlockChain { panic("no backing chain") }
|
|
|
|
func (h *testEthHandler) TxPool() eth.TxPool { panic("no backing tx pool") }
|
|
|
|
func (h *testEthHandler) AcceptTxs() bool { return true }
|
|
|
|
func (h *testEthHandler) RunPeer(*eth.Peer, eth.Handler) error { panic("not used in tests") }
|
|
|
|
func (h *testEthHandler) PeerInfo(enode.ID) interface{} { panic("not used in tests") }
|
|
|
|
|
|
|
|
func (h *testEthHandler) Handle(peer *eth.Peer, packet eth.Packet) error {
|
|
|
|
switch packet := packet.(type) {
|
|
|
|
case *eth.NewBlockPacket:
|
|
|
|
h.blockBroadcasts.Send(packet.Block)
|
|
|
|
return nil
|
|
|
|
|
2022-10-31 15:23:26 +01:00
|
|
|
case *eth.NewPooledTransactionHashesPacket66:
|
2020-12-14 11:27:15 +02:00
|
|
|
h.txAnnounces.Send(([]common.Hash)(*packet))
|
|
|
|
return nil
|
|
|
|
|
2022-10-31 15:23:26 +01:00
|
|
|
case *eth.NewPooledTransactionHashesPacket68:
|
|
|
|
h.txAnnounces.Send(packet.Hashes)
|
|
|
|
return nil
|
|
|
|
|
2020-12-14 11:27:15 +02:00
|
|
|
case *eth.TransactionsPacket:
|
|
|
|
h.txBroadcasts.Send(([]*types.Transaction)(*packet))
|
|
|
|
return nil
|
|
|
|
|
|
|
|
case *eth.PooledTransactionsPacket:
|
|
|
|
h.txBroadcasts.Send(([]*types.Transaction)(*packet))
|
|
|
|
return nil
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("unexpected eth packet type in tests: %T", packet))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that peers are correctly accepted (or rejected) based on the advertised
|
|
|
|
// fork IDs in the protocol handshake.
|
2021-04-08 18:06:03 +03:00
|
|
|
func TestForkIDSplit66(t *testing.T) { testForkIDSplit(t, eth.ETH66) }
|
2022-10-31 15:23:26 +01:00
|
|
|
func TestForkIDSplit67(t *testing.T) { testForkIDSplit(t, eth.ETH67) }
|
|
|
|
func TestForkIDSplit68(t *testing.T) { testForkIDSplit(t, eth.ETH68) }
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
func testForkIDSplit(t *testing.T, protocol uint) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
engine = ethash.NewFaker()
|
|
|
|
|
|
|
|
configNoFork = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(1)}
|
|
|
|
configProFork = ¶ms.ChainConfig{
|
2023-04-10 18:36:45 +08:00
|
|
|
HomesteadBlock: big.NewInt(1),
|
|
|
|
EIP150Block: big.NewInt(2),
|
|
|
|
EIP155Block: big.NewInt(2),
|
|
|
|
EIP158Block: big.NewInt(2),
|
|
|
|
ByzantiumBlock: big.NewInt(3),
|
|
|
|
ConstantinopleBlock: big.NewInt(4),
|
|
|
|
PetersburgBlock: big.NewInt(4),
|
|
|
|
IstanbulBlock: big.NewInt(4),
|
|
|
|
MuirGlacierBlock: big.NewInt(4),
|
|
|
|
RamanujanBlock: big.NewInt(4),
|
|
|
|
NielsBlock: big.NewInt(4),
|
|
|
|
MirrorSyncBlock: big.NewInt(4),
|
|
|
|
BrunoBlock: big.NewInt(4),
|
|
|
|
EulerBlock: big.NewInt(5),
|
|
|
|
GibbsBlock: big.NewInt(5),
|
|
|
|
NanoBlock: big.NewInt(5),
|
|
|
|
MoranBlock: big.NewInt(5),
|
2023-04-21 17:07:11 +08:00
|
|
|
LubanBlock: big.NewInt(6),
|
|
|
|
PlatoBlock: big.NewInt(6),
|
2023-05-19 21:32:10 +08:00
|
|
|
HertzBlock: big.NewInt(7),
|
2020-12-14 11:27:15 +02:00
|
|
|
}
|
|
|
|
dbNoFork = rawdb.NewMemoryDatabase()
|
|
|
|
dbProFork = rawdb.NewMemoryDatabase()
|
|
|
|
|
|
|
|
gspecNoFork = &core.Genesis{Config: configNoFork}
|
|
|
|
gspecProFork = &core.Genesis{Config: configProFork}
|
|
|
|
|
2022-08-31 00:22:28 +08:00
|
|
|
chainNoFork, _ = core.NewBlockChain(dbNoFork, nil, gspecNoFork, nil, engine, vm.Config{}, nil, nil)
|
|
|
|
chainProFork, _ = core.NewBlockChain(dbProFork, nil, gspecProFork, nil, engine, vm.Config{}, nil, nil)
|
2020-12-14 11:27:15 +02:00
|
|
|
|
2022-09-08 02:21:59 +08:00
|
|
|
_, blocksNoFork, _ = core.GenerateChainWithGenesis(gspecNoFork, engine, 2, nil)
|
|
|
|
_, blocksProFork, _ = core.GenerateChainWithGenesis(gspecProFork, engine, 2, nil)
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
ethNoFork, _ = newHandler(&handlerConfig{
|
|
|
|
Database: dbNoFork,
|
|
|
|
Chain: chainNoFork,
|
|
|
|
TxPool: newTestTxPool(),
|
2023-04-10 18:36:45 +08:00
|
|
|
VotePool: newTestVotePool(),
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 12:23:02 +01:00
|
|
|
Merger: consensus.NewMerger(rawdb.NewMemoryDatabase()),
|
2020-12-14 11:27:15 +02:00
|
|
|
Network: 1,
|
|
|
|
Sync: downloader.FullSync,
|
|
|
|
BloomCache: 1,
|
|
|
|
})
|
|
|
|
ethProFork, _ = newHandler(&handlerConfig{
|
|
|
|
Database: dbProFork,
|
|
|
|
Chain: chainProFork,
|
|
|
|
TxPool: newTestTxPool(),
|
2023-04-10 18:36:45 +08:00
|
|
|
VotePool: newTestVotePool(),
|
all: core rework for the merge transition (#23761)
* all: work for eth1/2 transtition
* consensus/beacon, eth: change beacon difficulty to 0
* eth: updates
* all: add terminalBlockDifficulty config, fix rebasing issues
* eth: implemented merge interop spec
* internal/ethapi: update to v1.0.0.alpha.2
This commit updates the code to the new spec, moving payloadId into
it's own object. It also fixes an issue with finalizing an empty blockhash.
It also properly sets the basefee
* all: sync polishes, other fixes + refactors
* core, eth: correct semantics for LeavePoW, EnterPoS
* core: fixed rebasing artifacts
* core: light: performance improvements
* core: use keyed field (f)
* core: eth: fix compilation issues + tests
* eth/catalyst: dbetter error codes
* all: move Merger to consensus/, remove reliance on it in bc
* all: renamed EnterPoS and LeavePoW to ReachTDD and FinalizePoS
* core: make mergelogs a function
* core: use InsertChain instead of InsertBlock
* les: drop merger from lightchain object
* consensus: add merger
* core: recoverAncestors in catalyst mode
* core: fix nitpick
* all: removed merger from beacon, use TTD, nitpicks
* consensus: eth: add docstring, removed unnecessary code duplication
* consensus/beacon: better comment
* all: easy to fix nitpicks by karalabe
* consensus/beacon: verify known headers to be sure
* core: comments
* core: eth: don't drop peers who advertise blocks, nitpicks
* core: never add beacon blocks to the future queue
* core: fixed nitpicks
* consensus/beacon: simplify IsTTDReached check
* consensus/beacon: correct IsTTDReached check
Co-authored-by: rjl493456442 <garyrong0905@gmail.com>
Co-authored-by: Péter Szilágyi <peterke@gmail.com>
2021-11-26 12:23:02 +01:00
|
|
|
Merger: consensus.NewMerger(rawdb.NewMemoryDatabase()),
|
2020-12-14 11:27:15 +02:00
|
|
|
Network: 1,
|
|
|
|
Sync: downloader.FullSync,
|
|
|
|
BloomCache: 1,
|
|
|
|
})
|
|
|
|
)
|
2023-05-25 08:57:02 +08:00
|
|
|
ethNoFork.Start(1000, 1000)
|
|
|
|
ethProFork.Start(1000, 1000)
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
// Clean up everything after ourselves
|
|
|
|
defer chainNoFork.Stop()
|
|
|
|
defer chainProFork.Stop()
|
|
|
|
|
|
|
|
defer ethNoFork.Stop()
|
|
|
|
defer ethProFork.Stop()
|
|
|
|
|
|
|
|
// Both nodes should allow the other to connect (same genesis, next fork is the same)
|
|
|
|
p2pNoFork, p2pProFork := p2p.MsgPipe()
|
|
|
|
defer p2pNoFork.Close()
|
|
|
|
defer p2pProFork.Close()
|
|
|
|
|
2021-05-25 22:20:36 +02:00
|
|
|
peerNoFork := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{1}, "", nil, p2pNoFork), p2pNoFork, nil)
|
|
|
|
peerProFork := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{2}, "", nil, p2pProFork), p2pProFork, nil)
|
2020-12-14 11:27:15 +02:00
|
|
|
defer peerNoFork.Close()
|
|
|
|
defer peerProFork.Close()
|
|
|
|
|
|
|
|
errc := make(chan error, 2)
|
|
|
|
go func(errc chan error) {
|
|
|
|
errc <- ethNoFork.runEthPeer(peerProFork, func(peer *eth.Peer) error { return nil })
|
|
|
|
}(errc)
|
|
|
|
go func(errc chan error) {
|
|
|
|
errc <- ethProFork.runEthPeer(peerNoFork, func(peer *eth.Peer) error { return nil })
|
|
|
|
}(errc)
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("frontier nofork <-> profork failed: %v", err)
|
|
|
|
}
|
|
|
|
case <-time.After(250 * time.Millisecond):
|
|
|
|
t.Fatalf("frontier nofork <-> profork handler timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Progress into Homestead. Fork's match, so we don't care what the future holds
|
|
|
|
chainNoFork.InsertChain(blocksNoFork[:1])
|
|
|
|
chainProFork.InsertChain(blocksProFork[:1])
|
|
|
|
|
|
|
|
p2pNoFork, p2pProFork = p2p.MsgPipe()
|
|
|
|
defer p2pNoFork.Close()
|
|
|
|
defer p2pProFork.Close()
|
|
|
|
|
|
|
|
peerNoFork = eth.NewPeer(protocol, p2p.NewPeer(enode.ID{1}, "", nil), p2pNoFork, nil)
|
|
|
|
peerProFork = eth.NewPeer(protocol, p2p.NewPeer(enode.ID{2}, "", nil), p2pProFork, nil)
|
|
|
|
defer peerNoFork.Close()
|
|
|
|
defer peerProFork.Close()
|
|
|
|
|
|
|
|
errc = make(chan error, 2)
|
|
|
|
go func(errc chan error) {
|
|
|
|
errc <- ethNoFork.runEthPeer(peerProFork, func(peer *eth.Peer) error { return nil })
|
|
|
|
}(errc)
|
|
|
|
go func(errc chan error) {
|
|
|
|
errc <- ethProFork.runEthPeer(peerNoFork, func(peer *eth.Peer) error { return nil })
|
|
|
|
}(errc)
|
|
|
|
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("homestead nofork <-> profork failed: %v", err)
|
|
|
|
}
|
|
|
|
case <-time.After(250 * time.Millisecond):
|
|
|
|
t.Fatalf("homestead nofork <-> profork handler timeout")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Progress into Spurious. Forks mismatch, signalling differing chains, reject
|
|
|
|
chainNoFork.InsertChain(blocksNoFork[1:2])
|
|
|
|
chainProFork.InsertChain(blocksProFork[1:2])
|
|
|
|
|
|
|
|
p2pNoFork, p2pProFork = p2p.MsgPipe()
|
|
|
|
defer p2pNoFork.Close()
|
|
|
|
defer p2pProFork.Close()
|
|
|
|
|
2021-05-25 22:20:36 +02:00
|
|
|
peerNoFork = eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{1}, "", nil, p2pNoFork), p2pNoFork, nil)
|
|
|
|
peerProFork = eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{2}, "", nil, p2pProFork), p2pProFork, nil)
|
2020-12-14 11:27:15 +02:00
|
|
|
defer peerNoFork.Close()
|
|
|
|
defer peerProFork.Close()
|
|
|
|
|
|
|
|
errc = make(chan error, 2)
|
|
|
|
go func(errc chan error) {
|
|
|
|
errc <- ethNoFork.runEthPeer(peerProFork, func(peer *eth.Peer) error { return nil })
|
|
|
|
}(errc)
|
|
|
|
go func(errc chan error) {
|
|
|
|
errc <- ethProFork.runEthPeer(peerNoFork, func(peer *eth.Peer) error { return nil })
|
|
|
|
}(errc)
|
|
|
|
|
|
|
|
var successes int
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
select {
|
|
|
|
case err := <-errc:
|
|
|
|
if err == nil {
|
|
|
|
successes++
|
|
|
|
if successes == 2 { // Only one side disconnects
|
|
|
|
t.Fatalf("fork ID rejection didn't happen")
|
|
|
|
}
|
|
|
|
}
|
2023-04-10 18:36:45 +08:00
|
|
|
case <-time.After(10000 * time.Millisecond):
|
2020-12-14 11:27:15 +02:00
|
|
|
t.Fatalf("split peers not rejected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that received transactions are added to the local pool.
|
2021-04-08 18:06:03 +03:00
|
|
|
func TestRecvTransactions66(t *testing.T) { testRecvTransactions(t, eth.ETH66) }
|
2022-10-31 15:23:26 +01:00
|
|
|
func TestRecvTransactions67(t *testing.T) { testRecvTransactions(t, eth.ETH67) }
|
|
|
|
func TestRecvTransactions68(t *testing.T) { testRecvTransactions(t, eth.ETH68) }
|
2020-12-14 11:27:15 +02:00
|
|
|
|
2023-04-10 18:36:45 +08:00
|
|
|
func testRecvTransactions(t *testing.T, protocol uint) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a message handler, configure it to accept transactions and watch them
|
|
|
|
handler := newTestHandler()
|
|
|
|
defer handler.close()
|
|
|
|
|
2023-04-25 18:06:50 +08:00
|
|
|
handler.handler.acceptTxs.Store(true) // mark synced to accept transactions
|
2023-04-10 18:36:45 +08:00
|
|
|
|
|
|
|
txs := make(chan core.NewTxsEvent)
|
|
|
|
sub := handler.txpool.SubscribeNewTxsEvent(txs)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
// Create a source peer to send messages through and a sink handler to receive them
|
|
|
|
p2pSrc, p2pSink := p2p.MsgPipe()
|
|
|
|
defer p2pSrc.Close()
|
|
|
|
defer p2pSink.Close()
|
|
|
|
|
|
|
|
src := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{1}, "", nil, p2pSrc), p2pSrc, handler.txpool)
|
|
|
|
sink := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{2}, "", nil, p2pSink), p2pSink, handler.txpool)
|
|
|
|
defer src.Close()
|
|
|
|
defer sink.Close()
|
|
|
|
|
|
|
|
go handler.handler.runEthPeer(sink, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(handler.handler), peer)
|
|
|
|
})
|
|
|
|
// Run the handshake locally to avoid spinning up a source handler
|
|
|
|
var (
|
|
|
|
genesis = handler.chain.Genesis()
|
|
|
|
head = handler.chain.CurrentBlock()
|
2023-03-02 08:29:15 +02:00
|
|
|
td = handler.chain.GetTd(head.Hash(), head.Number.Uint64())
|
2023-04-10 18:36:45 +08:00
|
|
|
)
|
|
|
|
if err := src.Handshake(1, td, head.Hash(), genesis.Hash(), forkid.NewIDWithChain(handler.chain), forkid.NewFilter(handler.chain), nil); err != nil {
|
|
|
|
t.Fatalf("failed to run protocol handshake")
|
|
|
|
}
|
|
|
|
// Send the transaction to the sink and verify that it's added to the tx pool
|
|
|
|
tx := types.NewTransaction(0, common.Address{}, big.NewInt(0), 100000, big.NewInt(0), nil)
|
|
|
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
|
|
|
|
|
|
|
|
if err := src.SendTransactions([]*types.Transaction{tx}); err != nil {
|
|
|
|
t.Fatalf("failed to send transaction: %v", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case event := <-txs:
|
|
|
|
if len(event.Txs) != 1 {
|
|
|
|
t.Errorf("wrong number of added transactions: got %d, want 1", len(event.Txs))
|
|
|
|
} else if event.Txs[0].Hash() != tx.Hash() {
|
|
|
|
t.Errorf("added wrong tx hash: got %v, want %v", event.Txs[0].Hash(), tx.Hash())
|
|
|
|
}
|
|
|
|
case <-time.After(2 * time.Second):
|
|
|
|
t.Errorf("no NewTxsEvent received within 2 seconds")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestWaitSnapExtensionTimout67(t *testing.T) { testWaitSnapExtensionTimout(t, eth.ETH67) }
|
|
|
|
|
|
|
|
func testWaitSnapExtensionTimout(t *testing.T, protocol uint) {
|
2022-07-05 11:14:21 +08:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a message handler, configure it to accept transactions and watch them
|
|
|
|
handler := newTestHandler()
|
|
|
|
defer handler.close()
|
|
|
|
|
|
|
|
// Create a source peer to send messages through and a sink handler to receive them
|
|
|
|
_, p2pSink := p2p.MsgPipe()
|
|
|
|
defer p2pSink.Close()
|
|
|
|
|
|
|
|
protos := []p2p.Protocol{
|
|
|
|
{
|
|
|
|
Name: "snap",
|
|
|
|
Version: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:36:45 +08:00
|
|
|
sink := eth.NewPeer(protocol, p2p.NewPeerWithProtocols(enode.ID{2}, protos, "", []p2p.Cap{
|
2022-07-05 11:14:21 +08:00
|
|
|
{
|
|
|
|
Name: "snap",
|
|
|
|
Version: 1,
|
|
|
|
},
|
|
|
|
}), p2pSink, nil)
|
|
|
|
defer sink.Close()
|
|
|
|
|
|
|
|
err := handler.handler.runEthPeer(sink, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(handler.handler), peer)
|
|
|
|
})
|
|
|
|
|
|
|
|
if err == nil || err.Error() != "peer wait timeout" {
|
|
|
|
t.Fatalf("error should be `peer wait timeout`")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-04-10 18:36:45 +08:00
|
|
|
func TestWaitBscExtensionTimout67(t *testing.T) { testWaitBscExtensionTimout(t, eth.ETH67) }
|
|
|
|
|
|
|
|
func testWaitBscExtensionTimout(t *testing.T, protocol uint) {
|
2020-12-14 11:27:15 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a message handler, configure it to accept transactions and watch them
|
|
|
|
handler := newTestHandler()
|
|
|
|
defer handler.close()
|
|
|
|
|
|
|
|
// Create a source peer to send messages through and a sink handler to receive them
|
2023-04-10 18:36:45 +08:00
|
|
|
_, p2pSink := p2p.MsgPipe()
|
2020-12-14 11:27:15 +02:00
|
|
|
defer p2pSink.Close()
|
|
|
|
|
2023-04-10 18:36:45 +08:00
|
|
|
protos := []p2p.Protocol{
|
|
|
|
{
|
|
|
|
Name: "bsc",
|
|
|
|
Version: bsc.Bsc1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
sink := eth.NewPeer(protocol, p2p.NewPeerWithProtocols(enode.ID{2}, protos, "", []p2p.Cap{
|
|
|
|
{
|
|
|
|
Name: "bsc",
|
|
|
|
Version: bsc.Bsc1,
|
|
|
|
},
|
|
|
|
}), p2pSink, nil)
|
2020-12-14 11:27:15 +02:00
|
|
|
defer sink.Close()
|
|
|
|
|
2023-04-10 18:36:45 +08:00
|
|
|
err := handler.handler.runEthPeer(sink, func(peer *eth.Peer) error {
|
2020-12-14 11:27:15 +02:00
|
|
|
return eth.Handle((*ethHandler)(handler.handler), peer)
|
|
|
|
})
|
|
|
|
|
2023-04-10 18:36:45 +08:00
|
|
|
if err == nil || err.Error() != "peer wait timeout" {
|
|
|
|
t.Fatalf("error should be `peer wait timeout`")
|
2020-12-14 11:27:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks that pending transactions are sent.
|
2021-04-08 18:06:03 +03:00
|
|
|
func TestSendTransactions66(t *testing.T) { testSendTransactions(t, eth.ETH66) }
|
2022-10-31 15:23:26 +01:00
|
|
|
func TestSendTransactions67(t *testing.T) { testSendTransactions(t, eth.ETH67) }
|
|
|
|
func TestSendTransactions68(t *testing.T) { testSendTransactions(t, eth.ETH68) }
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
func testSendTransactions(t *testing.T, protocol uint) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a message handler and fill the pool with big transactions
|
|
|
|
handler := newTestHandler()
|
|
|
|
defer handler.close()
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
insert := make([]*txpool.Transaction, 100)
|
2020-12-14 11:27:15 +02:00
|
|
|
for nonce := range insert {
|
2021-08-24 21:52:58 +03:00
|
|
|
tx := types.NewTransaction(uint64(nonce), common.Address{}, big.NewInt(0), 100000, big.NewInt(0), make([]byte, 10240))
|
2020-12-14 11:27:15 +02:00
|
|
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
insert[nonce] = &txpool.Transaction{Tx: tx}
|
2020-12-14 11:27:15 +02:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
go handler.txpool.Add(insert, false, false) // Need goroutine to not block on feed
|
|
|
|
time.Sleep(250 * time.Millisecond) // Wait until tx events get out of the system (can't use events, tx broadcaster races with peer join)
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
// Create a source handler to send messages through and a sink peer to receive them
|
|
|
|
p2pSrc, p2pSink := p2p.MsgPipe()
|
|
|
|
defer p2pSrc.Close()
|
|
|
|
defer p2pSink.Close()
|
|
|
|
|
2021-05-25 22:20:36 +02:00
|
|
|
src := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{1}, "", nil, p2pSrc), p2pSrc, handler.txpool)
|
|
|
|
sink := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{2}, "", nil, p2pSink), p2pSink, handler.txpool)
|
2020-12-14 11:27:15 +02:00
|
|
|
defer src.Close()
|
|
|
|
defer sink.Close()
|
|
|
|
|
|
|
|
go handler.handler.runEthPeer(src, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(handler.handler), peer)
|
|
|
|
})
|
|
|
|
// Run the handshake locally to avoid spinning up a source handler
|
|
|
|
var (
|
|
|
|
genesis = handler.chain.Genesis()
|
|
|
|
head = handler.chain.CurrentBlock()
|
2023-03-02 08:29:15 +02:00
|
|
|
td = handler.chain.GetTd(head.Hash(), head.Number.Uint64())
|
2020-12-14 11:27:15 +02:00
|
|
|
)
|
2022-07-05 11:14:21 +08:00
|
|
|
if err := sink.Handshake(1, td, head.Hash(), genesis.Hash(), forkid.NewIDWithChain(handler.chain), forkid.NewFilter(handler.chain), nil); err != nil {
|
2020-12-14 11:27:15 +02:00
|
|
|
t.Fatalf("failed to run protocol handshake")
|
|
|
|
}
|
|
|
|
// After the handshake completes, the source handler should stream the sink
|
|
|
|
// the transactions, subscribe to all inbound network events
|
|
|
|
backend := new(testEthHandler)
|
|
|
|
|
|
|
|
anns := make(chan []common.Hash)
|
|
|
|
annSub := backend.txAnnounces.Subscribe(anns)
|
|
|
|
defer annSub.Unsubscribe()
|
|
|
|
|
|
|
|
bcasts := make(chan []*types.Transaction)
|
|
|
|
bcastSub := backend.txBroadcasts.Subscribe(bcasts)
|
|
|
|
defer bcastSub.Unsubscribe()
|
|
|
|
|
|
|
|
go eth.Handle(backend, sink)
|
|
|
|
|
|
|
|
// Make sure we get all the transactions on the correct channels
|
|
|
|
seen := make(map[common.Hash]struct{})
|
|
|
|
for len(seen) < len(insert) {
|
|
|
|
switch protocol {
|
2022-10-31 15:23:26 +01:00
|
|
|
case 66, 67, 68:
|
2020-12-14 11:27:15 +02:00
|
|
|
select {
|
|
|
|
case hashes := <-anns:
|
|
|
|
for _, hash := range hashes {
|
|
|
|
if _, ok := seen[hash]; ok {
|
|
|
|
t.Errorf("duplicate transaction announced: %x", hash)
|
|
|
|
}
|
|
|
|
seen[hash] = struct{}{}
|
|
|
|
}
|
|
|
|
case <-bcasts:
|
2021-11-26 13:26:03 +02:00
|
|
|
t.Errorf("initial tx broadcast received on post eth/66")
|
2020-12-14 11:27:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("unsupported protocol, please extend test")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tx := range insert {
|
2023-06-16 15:29:40 +03:00
|
|
|
if _, ok := seen[tx.Tx.Hash()]; !ok {
|
|
|
|
t.Errorf("missing transaction: %x", tx.Tx.Hash())
|
2020-12-14 11:27:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that transactions get propagated to all attached peers, either via direct
|
|
|
|
// broadcasts or via announcements/retrievals.
|
2021-04-08 18:06:03 +03:00
|
|
|
func TestTransactionPropagation66(t *testing.T) { testTransactionPropagation(t, eth.ETH66) }
|
2022-10-31 15:23:26 +01:00
|
|
|
func TestTransactionPropagation67(t *testing.T) { testTransactionPropagation(t, eth.ETH67) }
|
|
|
|
func TestTransactionPropagation68(t *testing.T) { testTransactionPropagation(t, eth.ETH68) }
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
func testTransactionPropagation(t *testing.T, protocol uint) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a source handler to send transactions from and a number of sinks
|
|
|
|
// to receive them. We need multiple sinks since a one-to-one peering would
|
|
|
|
// broadcast all transactions without announcement.
|
|
|
|
source := newTestHandler()
|
2023-04-25 18:06:50 +08:00
|
|
|
source.handler.snapSync.Store(false) // Avoid requiring snap, otherwise some will be dropped below
|
2020-12-14 11:27:15 +02:00
|
|
|
defer source.close()
|
|
|
|
|
|
|
|
sinks := make([]*testHandler, 10)
|
|
|
|
for i := 0; i < len(sinks); i++ {
|
|
|
|
sinks[i] = newTestHandler()
|
|
|
|
defer sinks[i].close()
|
|
|
|
|
2023-04-25 18:06:50 +08:00
|
|
|
sinks[i].handler.acceptTxs.Store(true) // mark synced to accept transactions
|
2020-12-14 11:27:15 +02:00
|
|
|
}
|
|
|
|
// Interconnect all the sink handlers with the source handler
|
|
|
|
for i, sink := range sinks {
|
|
|
|
sink := sink // Closure for gorotuine below
|
|
|
|
|
|
|
|
sourcePipe, sinkPipe := p2p.MsgPipe()
|
|
|
|
defer sourcePipe.Close()
|
|
|
|
defer sinkPipe.Close()
|
|
|
|
|
2021-11-26 13:26:03 +02:00
|
|
|
sourcePeer := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{byte(i + 1)}, "", nil, sourcePipe), sourcePipe, source.txpool)
|
2021-05-25 22:20:36 +02:00
|
|
|
sinkPeer := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{0}, "", nil, sinkPipe), sinkPipe, sink.txpool)
|
2020-12-14 11:27:15 +02:00
|
|
|
defer sourcePeer.Close()
|
|
|
|
defer sinkPeer.Close()
|
|
|
|
|
|
|
|
go source.handler.runEthPeer(sourcePeer, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(source.handler), peer)
|
|
|
|
})
|
|
|
|
go sink.handler.runEthPeer(sinkPeer, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(sink.handler), peer)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// Subscribe to all the transaction pools
|
|
|
|
txChs := make([]chan core.NewTxsEvent, len(sinks))
|
|
|
|
for i := 0; i < len(sinks); i++ {
|
|
|
|
txChs[i] = make(chan core.NewTxsEvent, 1024)
|
|
|
|
|
|
|
|
sub := sinks[i].txpool.SubscribeNewTxsEvent(txChs[i])
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
}
|
|
|
|
// Fill the source pool with transactions and wait for them at the sinks
|
2023-06-16 15:29:40 +03:00
|
|
|
txs := make([]*txpool.Transaction, 1024)
|
2020-12-14 11:27:15 +02:00
|
|
|
for nonce := range txs {
|
|
|
|
tx := types.NewTransaction(uint64(nonce), common.Address{}, big.NewInt(0), 100000, big.NewInt(0), nil)
|
|
|
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
|
|
|
|
|
2023-06-16 15:29:40 +03:00
|
|
|
txs[nonce] = &txpool.Transaction{Tx: tx}
|
2020-12-14 11:27:15 +02:00
|
|
|
}
|
2023-06-16 15:29:40 +03:00
|
|
|
source.txpool.Add(txs, false, false)
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
// Iterate through all the sinks and ensure they all got the transactions
|
|
|
|
for i := range sinks {
|
2021-11-26 13:26:03 +02:00
|
|
|
for arrived, timeout := 0, false; arrived < len(txs) && !timeout; {
|
2020-12-14 11:27:15 +02:00
|
|
|
select {
|
|
|
|
case event := <-txChs[i]:
|
|
|
|
arrived += len(event.Txs)
|
2022-11-11 13:22:54 +01:00
|
|
|
case <-time.After(2 * time.Second):
|
2020-12-14 11:27:15 +02:00
|
|
|
t.Errorf("sink %d: transaction propagation timed out: have %d, want %d", i, arrived, len(txs))
|
2021-11-26 13:26:03 +02:00
|
|
|
timeout = true
|
2020-12-14 11:27:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-05 11:14:21 +08:00
|
|
|
// Tests that local pending transactions get propagated to peers.
|
|
|
|
func TestTransactionPendingReannounce(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a source handler to announce transactions from and a sink handler
|
|
|
|
// to receive them.
|
|
|
|
source := newTestHandler()
|
|
|
|
defer source.close()
|
|
|
|
|
|
|
|
sink := newTestHandler()
|
|
|
|
defer sink.close()
|
2023-09-07 16:39:29 +08:00
|
|
|
sink.handler.acceptTxs.Store(true) // mark synced to accept transactions
|
2022-07-05 11:14:21 +08:00
|
|
|
|
|
|
|
sourcePipe, sinkPipe := p2p.MsgPipe()
|
|
|
|
defer sourcePipe.Close()
|
|
|
|
defer sinkPipe.Close()
|
|
|
|
|
|
|
|
sourcePeer := eth.NewPeer(eth.ETH66, p2p.NewPeer(enode.ID{0}, "", nil), sourcePipe, source.txpool)
|
|
|
|
sinkPeer := eth.NewPeer(eth.ETH66, p2p.NewPeer(enode.ID{0}, "", nil), sinkPipe, sink.txpool)
|
|
|
|
defer sourcePeer.Close()
|
|
|
|
defer sinkPeer.Close()
|
|
|
|
|
|
|
|
go source.handler.runEthPeer(sourcePeer, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(source.handler), peer)
|
|
|
|
})
|
|
|
|
go sink.handler.runEthPeer(sinkPeer, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(sink.handler), peer)
|
|
|
|
})
|
|
|
|
|
|
|
|
// Subscribe transaction pools
|
|
|
|
txCh := make(chan core.NewTxsEvent, 1024)
|
|
|
|
sub := sink.txpool.SubscribeNewTxsEvent(txCh)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
txs := make([]*types.Transaction, 64)
|
|
|
|
for nonce := range txs {
|
|
|
|
tx := types.NewTransaction(uint64(nonce), common.Address{}, big.NewInt(0), 100000, big.NewInt(0), nil)
|
|
|
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
|
|
|
|
|
|
|
|
txs[nonce] = tx
|
|
|
|
}
|
|
|
|
source.txpool.ReannouceTransactions(txs)
|
|
|
|
|
|
|
|
for arrived := 0; arrived < len(txs); {
|
|
|
|
select {
|
|
|
|
case event := <-txCh:
|
|
|
|
arrived += len(event.Txs)
|
|
|
|
case <-time.NewTimer(time.Second).C:
|
|
|
|
t.Errorf("sink: transaction propagation timed out: have %d, want %d", arrived, len(txs))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-14 11:27:15 +02:00
|
|
|
// Tests that blocks are broadcast to a sqrt number of peers only.
|
|
|
|
func TestBroadcastBlock1Peer(t *testing.T) { testBroadcastBlock(t, 1, 1) }
|
|
|
|
func TestBroadcastBlock2Peers(t *testing.T) { testBroadcastBlock(t, 2, 1) }
|
|
|
|
func TestBroadcastBlock3Peers(t *testing.T) { testBroadcastBlock(t, 3, 1) }
|
|
|
|
func TestBroadcastBlock4Peers(t *testing.T) { testBroadcastBlock(t, 4, 2) }
|
|
|
|
func TestBroadcastBlock5Peers(t *testing.T) { testBroadcastBlock(t, 5, 2) }
|
|
|
|
func TestBroadcastBlock8Peers(t *testing.T) { testBroadcastBlock(t, 9, 3) }
|
|
|
|
func TestBroadcastBlock12Peers(t *testing.T) { testBroadcastBlock(t, 12, 3) }
|
|
|
|
func TestBroadcastBlock16Peers(t *testing.T) { testBroadcastBlock(t, 16, 4) }
|
|
|
|
func TestBroadcastBloc26Peers(t *testing.T) { testBroadcastBlock(t, 26, 5) }
|
|
|
|
func TestBroadcastBlock100Peers(t *testing.T) { testBroadcastBlock(t, 100, 10) }
|
|
|
|
|
|
|
|
func testBroadcastBlock(t *testing.T, peers, bcasts int) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a source handler to broadcast blocks from and a number of sinks
|
|
|
|
// to receive them.
|
|
|
|
source := newTestHandlerWithBlocks(1)
|
|
|
|
defer source.close()
|
|
|
|
|
|
|
|
sinks := make([]*testEthHandler, peers)
|
|
|
|
for i := 0; i < len(sinks); i++ {
|
|
|
|
sinks[i] = new(testEthHandler)
|
|
|
|
}
|
|
|
|
// Interconnect all the sink handlers with the source handler
|
|
|
|
var (
|
|
|
|
genesis = source.chain.Genesis()
|
|
|
|
td = source.chain.GetTd(genesis.Hash(), genesis.NumberU64())
|
|
|
|
)
|
|
|
|
for i, sink := range sinks {
|
|
|
|
sink := sink // Closure for gorotuine below
|
|
|
|
|
|
|
|
sourcePipe, sinkPipe := p2p.MsgPipe()
|
|
|
|
defer sourcePipe.Close()
|
|
|
|
defer sinkPipe.Close()
|
|
|
|
|
2021-08-24 21:52:58 +03:00
|
|
|
sourcePeer := eth.NewPeer(eth.ETH66, p2p.NewPeerPipe(enode.ID{byte(i)}, "", nil, sourcePipe), sourcePipe, nil)
|
|
|
|
sinkPeer := eth.NewPeer(eth.ETH66, p2p.NewPeerPipe(enode.ID{0}, "", nil, sinkPipe), sinkPipe, nil)
|
2020-12-14 11:27:15 +02:00
|
|
|
defer sourcePeer.Close()
|
|
|
|
defer sinkPeer.Close()
|
|
|
|
|
|
|
|
go source.handler.runEthPeer(sourcePeer, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(source.handler), peer)
|
|
|
|
})
|
2022-07-05 11:14:21 +08:00
|
|
|
if err := sinkPeer.Handshake(1, td, genesis.Hash(), genesis.Hash(), forkid.NewIDWithChain(source.chain), forkid.NewFilter(source.chain), nil); err != nil {
|
2020-12-14 11:27:15 +02:00
|
|
|
t.Fatalf("failed to run protocol handshake")
|
|
|
|
}
|
|
|
|
go eth.Handle(sink, sinkPeer)
|
|
|
|
}
|
|
|
|
// Subscribe to all the transaction pools
|
|
|
|
blockChs := make([]chan *types.Block, len(sinks))
|
|
|
|
for i := 0; i < len(sinks); i++ {
|
|
|
|
blockChs[i] = make(chan *types.Block, 1)
|
|
|
|
defer close(blockChs[i])
|
|
|
|
|
|
|
|
sub := sinks[i].blockBroadcasts.Subscribe(blockChs[i])
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
}
|
|
|
|
// Initiate a block propagation across the peers
|
|
|
|
time.Sleep(100 * time.Millisecond)
|
2023-03-02 08:29:15 +02:00
|
|
|
header := source.chain.CurrentBlock()
|
|
|
|
source.handler.BroadcastBlock(source.chain.GetBlock(header.Hash(), header.Number.Uint64()), true)
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
// Iterate through all the sinks and ensure the correct number got the block
|
|
|
|
done := make(chan struct{}, peers)
|
|
|
|
for _, ch := range blockChs {
|
|
|
|
ch := ch
|
|
|
|
go func() {
|
|
|
|
<-ch
|
|
|
|
done <- struct{}{}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
var received int
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-done:
|
|
|
|
received++
|
|
|
|
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
if received != bcasts {
|
|
|
|
t.Errorf("broadcast count mismatch: have %d, want %d", received, bcasts)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that a propagated malformed block (uncles or transactions don't match
|
|
|
|
// with the hashes in the header) gets discarded and not broadcast forward.
|
2021-04-08 18:06:03 +03:00
|
|
|
func TestBroadcastMalformedBlock66(t *testing.T) { testBroadcastMalformedBlock(t, eth.ETH66) }
|
2022-10-31 15:23:26 +01:00
|
|
|
func TestBroadcastMalformedBlock67(t *testing.T) { testBroadcastMalformedBlock(t, eth.ETH67) }
|
|
|
|
func TestBroadcastMalformedBlock68(t *testing.T) { testBroadcastMalformedBlock(t, eth.ETH68) }
|
2020-12-14 11:27:15 +02:00
|
|
|
|
|
|
|
func testBroadcastMalformedBlock(t *testing.T, protocol uint) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
// Create a source handler to broadcast blocks from and a number of sinks
|
|
|
|
// to receive them.
|
|
|
|
source := newTestHandlerWithBlocks(1)
|
|
|
|
defer source.close()
|
|
|
|
|
|
|
|
// Create a source handler to send messages through and a sink peer to receive them
|
|
|
|
p2pSrc, p2pSink := p2p.MsgPipe()
|
|
|
|
defer p2pSrc.Close()
|
|
|
|
defer p2pSink.Close()
|
|
|
|
|
2021-05-25 22:20:36 +02:00
|
|
|
src := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{1}, "", nil, p2pSrc), p2pSrc, source.txpool)
|
|
|
|
sink := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{2}, "", nil, p2pSink), p2pSink, source.txpool)
|
2020-12-14 11:27:15 +02:00
|
|
|
defer src.Close()
|
|
|
|
defer sink.Close()
|
|
|
|
|
|
|
|
go source.handler.runEthPeer(src, func(peer *eth.Peer) error {
|
|
|
|
return eth.Handle((*ethHandler)(source.handler), peer)
|
|
|
|
})
|
|
|
|
// Run the handshake locally to avoid spinning up a sink handler
|
|
|
|
var (
|
|
|
|
genesis = source.chain.Genesis()
|
|
|
|
td = source.chain.GetTd(genesis.Hash(), genesis.NumberU64())
|
|
|
|
)
|
2022-07-05 11:14:21 +08:00
|
|
|
if err := sink.Handshake(1, td, genesis.Hash(), genesis.Hash(), forkid.NewIDWithChain(source.chain), forkid.NewFilter(source.chain), nil); err != nil {
|
2020-12-14 11:27:15 +02:00
|
|
|
t.Fatalf("failed to run protocol handshake")
|
|
|
|
}
|
|
|
|
// After the handshake completes, the source handler should stream the sink
|
|
|
|
// the blocks, subscribe to inbound network events
|
|
|
|
backend := new(testEthHandler)
|
|
|
|
|
|
|
|
blocks := make(chan *types.Block, 1)
|
|
|
|
sub := backend.blockBroadcasts.Subscribe(blocks)
|
|
|
|
defer sub.Unsubscribe()
|
|
|
|
|
|
|
|
go eth.Handle(backend, sink)
|
|
|
|
|
|
|
|
// Create various combinations of malformed blocks
|
|
|
|
head := source.chain.CurrentBlock()
|
2023-03-02 08:29:15 +02:00
|
|
|
block := source.chain.GetBlock(head.Hash(), head.Number.Uint64())
|
2020-12-14 11:27:15 +02:00
|
|
|
|
2023-03-02 08:29:15 +02:00
|
|
|
malformedUncles := head
|
2020-12-14 11:27:15 +02:00
|
|
|
malformedUncles.UncleHash[0]++
|
2023-03-02 08:29:15 +02:00
|
|
|
malformedTransactions := head
|
2020-12-14 11:27:15 +02:00
|
|
|
malformedTransactions.TxHash[0]++
|
2023-03-02 08:29:15 +02:00
|
|
|
malformedEverything := head
|
2020-12-14 11:27:15 +02:00
|
|
|
malformedEverything.UncleHash[0]++
|
|
|
|
malformedEverything.TxHash[0]++
|
|
|
|
|
|
|
|
// Try to broadcast all malformations and ensure they all get discarded
|
|
|
|
for _, header := range []*types.Header{malformedUncles, malformedTransactions, malformedEverything} {
|
2023-03-02 08:29:15 +02:00
|
|
|
block := types.NewBlockWithHeader(header).WithBody(block.Transactions(), block.Uncles())
|
2020-12-14 11:27:15 +02:00
|
|
|
if err := src.SendNewBlock(block, big.NewInt(131136)); err != nil {
|
|
|
|
t.Fatalf("failed to broadcast block: %v", err)
|
|
|
|
}
|
|
|
|
select {
|
|
|
|
case <-blocks:
|
|
|
|
t.Fatalf("malformed block forwarded")
|
|
|
|
case <-time.After(100 * time.Millisecond):
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-25 08:57:02 +08:00
|
|
|
|
|
|
|
func TestOptionMaxPeersPerIP(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
handler := newTestHandler()
|
|
|
|
defer handler.close()
|
|
|
|
var (
|
|
|
|
genesis = handler.chain.Genesis()
|
|
|
|
head = handler.chain.CurrentBlock()
|
2023-09-07 16:39:29 +08:00
|
|
|
td = handler.chain.GetTd(head.Hash(), head.Number.Uint64())
|
2023-05-25 08:57:02 +08:00
|
|
|
wg = sync.WaitGroup{}
|
|
|
|
maxPeersPerIP = handler.handler.maxPeersPerIP
|
|
|
|
uniPort = 1000
|
|
|
|
)
|
|
|
|
|
|
|
|
tryFunc := func(tryNum int, ip1 string, ip2 string, trust bool, doneCh chan struct{}) {
|
|
|
|
// Create a source peer to send messages through and a sink handler to receive them
|
|
|
|
p2pSrc, p2pSink := p2p.MsgPipe()
|
|
|
|
defer p2pSrc.Close()
|
|
|
|
defer p2pSink.Close()
|
|
|
|
|
|
|
|
peer1 := p2p.NewPeerPipe(enode.ID{0}, "", nil, p2pSrc)
|
|
|
|
peer1.UpdateTestRemoteAddr(ip1 + strconv.Itoa(uniPort))
|
|
|
|
peer2 := p2p.NewPeerPipe(enode.ID{byte(uniPort)}, "", nil, p2pSink)
|
|
|
|
peer2.UpdateTestRemoteAddr(ip2 + strconv.Itoa(uniPort))
|
|
|
|
if trust {
|
|
|
|
peer2.UpdateTrustFlagTest()
|
|
|
|
}
|
|
|
|
uniPort++
|
|
|
|
|
|
|
|
src := eth.NewPeer(eth.ETH66, peer1, p2pSrc, handler.txpool)
|
|
|
|
sink := eth.NewPeer(eth.ETH66, peer2, p2pSink, handler.txpool)
|
|
|
|
defer src.Close()
|
|
|
|
defer sink.Close()
|
|
|
|
|
|
|
|
wg.Add(1)
|
2023-09-21 12:02:59 +08:00
|
|
|
time.Sleep(time.Duration((tryNum-1)*200) * time.Millisecond)
|
2023-05-25 08:57:02 +08:00
|
|
|
go func(num int) {
|
|
|
|
err := handler.handler.runEthPeer(sink, func(peer *eth.Peer) error {
|
|
|
|
wg.Done()
|
|
|
|
<-doneCh
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
// err is nil, connection ok and it is closed by the doneCh
|
|
|
|
if err == nil {
|
|
|
|
if trust || num <= maxPeersPerIP {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// if num > maxPeersPerIP and not trust, should report: p2p.DiscTooManyPeers
|
|
|
|
t.Errorf("current num is %d, maxPeersPerIP is %d, should failed", num, maxPeersPerIP)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wg.Done()
|
|
|
|
if trust {
|
|
|
|
t.Errorf("trust node should not failed, num is %d, maxPeersPerIP is %d, but failed:%s", num, maxPeersPerIP, err)
|
|
|
|
}
|
|
|
|
// err should be p2p.DiscTooManyPeers and num > maxPeersPerIP
|
|
|
|
if err == p2p.DiscTooManyPeers && num > maxPeersPerIP {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
t.Errorf("current num is %d, maxPeersPerIP is %d, but failed:%s", num, maxPeersPerIP, err)
|
|
|
|
}(tryNum)
|
|
|
|
|
|
|
|
if err := src.Handshake(1, td, head.Hash(), genesis.Hash(), forkid.NewIDWithChain(handler.chain), forkid.NewFilter(handler.chain), nil); err != nil {
|
|
|
|
t.Fatalf("failed to run protocol handshake")
|
|
|
|
}
|
|
|
|
// make sure runEthPeer execute one by one.
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
// case 1: normal case
|
|
|
|
doneCh1 := make(chan struct{})
|
|
|
|
for tryNum := 1; tryNum <= maxPeersPerIP+2; tryNum++ {
|
|
|
|
tryFunc(tryNum, "1.2.3.11:", "1.2.3.22:", false, doneCh1)
|
|
|
|
}
|
|
|
|
close(doneCh1)
|
|
|
|
|
|
|
|
// case 2: once the previous connection was unregisterred, new connections with same IP can be accepted.
|
|
|
|
doneCh2 := make(chan struct{})
|
|
|
|
for tryNum := 1; tryNum <= maxPeersPerIP+2; tryNum++ {
|
|
|
|
tryFunc(tryNum, "1.2.3.11:", "1.2.3.22:", false, doneCh2)
|
|
|
|
}
|
|
|
|
close(doneCh2)
|
|
|
|
|
|
|
|
// case 3: ipv6 address, like: [2001:db8::1]:80
|
|
|
|
doneCh3 := make(chan struct{})
|
|
|
|
for tryNum := 1; tryNum <= maxPeersPerIP+2; tryNum++ {
|
|
|
|
tryFunc(tryNum, "[2001:db8::11]:", "[2001:db8::22]:", false, doneCh3)
|
|
|
|
}
|
|
|
|
close(doneCh3)
|
|
|
|
|
|
|
|
// case 4: same as case 2, but for ipv6
|
|
|
|
doneCh4 := make(chan struct{})
|
|
|
|
for tryNum := 1; tryNum <= maxPeersPerIP+2; tryNum++ {
|
|
|
|
tryFunc(tryNum, "[2001:db8::11]:", "[2001:db8::22]:", false, doneCh4)
|
|
|
|
}
|
|
|
|
close(doneCh4)
|
|
|
|
|
|
|
|
// case 5: test trust node
|
|
|
|
doneCh5 := make(chan struct{})
|
|
|
|
for tryNum := 1; tryNum <= maxPeersPerIP+2; tryNum++ {
|
|
|
|
tryFunc(tryNum, "[2001:db8::11]:", "[2001:db8::22]:", true, doneCh5)
|
|
|
|
}
|
|
|
|
close(doneCh5)
|
|
|
|
}
|