2022-05-24 13:39:40 -05:00
|
|
|
// Copyright 2020 The go-ethereum Authors
|
2020-12-14 03:27:15 -06: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"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"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"
|
|
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
2024-12-03 02:30:26 -06:00
|
|
|
"github.com/ethereum/go-ethereum/eth/ethconfig"
|
2020-12-14 03:27:15 -06: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
|
|
|
|
|
2024-02-08 07:49:19 -06:00
|
|
|
case *eth.NewPooledTransactionHashesPacket:
|
2022-10-31 09:23:26 -05:00
|
|
|
h.txAnnounces.Send(packet.Hashes)
|
|
|
|
return nil
|
|
|
|
|
2020-12-14 03:27:15 -06:00
|
|
|
case *eth.TransactionsPacket:
|
|
|
|
h.txBroadcasts.Send(([]*types.Transaction)(*packet))
|
|
|
|
return nil
|
|
|
|
|
2023-10-03 07:03:19 -05:00
|
|
|
case *eth.PooledTransactionsResponse:
|
2020-12-14 03:27:15 -06:00
|
|
|
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.
|
2022-10-31 09:23:26 -05:00
|
|
|
func TestForkIDSplit68(t *testing.T) { testForkIDSplit(t, eth.ETH68) }
|
2020-12-14 03:27:15 -06:00
|
|
|
|
|
|
|
func testForkIDSplit(t *testing.T, protocol uint) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
var (
|
|
|
|
engine = ethash.NewFaker()
|
|
|
|
|
|
|
|
configNoFork = ¶ms.ChainConfig{HomesteadBlock: big.NewInt(1)}
|
|
|
|
configProFork = ¶ms.ChainConfig{
|
|
|
|
HomesteadBlock: big.NewInt(1),
|
|
|
|
EIP150Block: big.NewInt(2),
|
|
|
|
EIP155Block: big.NewInt(2),
|
|
|
|
EIP158Block: big.NewInt(2),
|
|
|
|
ByzantiumBlock: big.NewInt(3),
|
|
|
|
}
|
|
|
|
dbNoFork = rawdb.NewMemoryDatabase()
|
|
|
|
dbProFork = rawdb.NewMemoryDatabase()
|
|
|
|
|
|
|
|
gspecNoFork = &core.Genesis{Config: configNoFork}
|
|
|
|
gspecProFork = &core.Genesis{Config: configProFork}
|
|
|
|
|
2024-09-04 08:03:06 -05:00
|
|
|
chainNoFork, _ = core.NewBlockChain(dbNoFork, nil, gspecNoFork, nil, engine, vm.Config{}, nil)
|
|
|
|
chainProFork, _ = core.NewBlockChain(dbProFork, nil, gspecProFork, nil, engine, vm.Config{}, nil)
|
2020-12-14 03:27:15 -06:00
|
|
|
|
2022-09-07 13:21:59 -05:00
|
|
|
_, blocksNoFork, _ = core.GenerateChainWithGenesis(gspecNoFork, engine, 2, nil)
|
|
|
|
_, blocksProFork, _ = core.GenerateChainWithGenesis(gspecProFork, engine, 2, nil)
|
2020-12-14 03:27:15 -06:00
|
|
|
|
|
|
|
ethNoFork, _ = newHandler(&handlerConfig{
|
|
|
|
Database: dbNoFork,
|
|
|
|
Chain: chainNoFork,
|
|
|
|
TxPool: newTestTxPool(),
|
|
|
|
Network: 1,
|
2024-12-03 02:30:26 -06:00
|
|
|
Sync: ethconfig.FullSync,
|
2020-12-14 03:27:15 -06:00
|
|
|
BloomCache: 1,
|
|
|
|
})
|
|
|
|
ethProFork, _ = newHandler(&handlerConfig{
|
|
|
|
Database: dbProFork,
|
|
|
|
Chain: chainProFork,
|
|
|
|
TxPool: newTestTxPool(),
|
|
|
|
Network: 1,
|
2024-12-03 02:30:26 -06:00
|
|
|
Sync: ethconfig.FullSync,
|
2020-12-14 03:27:15 -06:00
|
|
|
BloomCache: 1,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
ethNoFork.Start(1000)
|
|
|
|
ethProFork.Start(1000)
|
|
|
|
|
|
|
|
// 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 15:20:36 -05: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 03:27:15 -06: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 15:20:36 -05: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 03:27:15 -06: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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-time.After(250 * time.Millisecond):
|
|
|
|
t.Fatalf("split peers not rejected")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that received transactions are added to the local pool.
|
2022-10-31 09:23:26 -05:00
|
|
|
func TestRecvTransactions68(t *testing.T) { testRecvTransactions(t, eth.ETH68) }
|
2020-12-14 03:27:15 -06: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()
|
|
|
|
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
handler.handler.synced.Store(true) // mark synced to accept transactions
|
2020-12-14 03:27:15 -06:00
|
|
|
|
|
|
|
txs := make(chan core.NewTxsEvent)
|
2023-10-04 04:36:36 -05:00
|
|
|
sub := handler.txpool.SubscribeTransactions(txs, false)
|
2020-12-14 03:27:15 -06:00
|
|
|
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()
|
|
|
|
|
2021-05-25 15:20:36 -05: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 03:27:15 -06:00
|
|
|
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()
|
|
|
|
)
|
all: nuke total difficulty (#30744)
The total difficulty is the sum of all block difficulties from genesis
to a certain block. This value was used in PoW for deciding which chain
is heavier, and thus which chain to select. Since PoS has a different
fork selection algorithm, all blocks since the merge have a difficulty
of 0, and all total difficulties are the same for the past 2 years.
Whilst the TDs are mostly useless nowadays, there was never really a
reason to mess around removing them since they are so tiny. This
reasoning changes when we go down the path of pruned chain history. In
order to reconstruct any TD, we **must** retrieve all the headers from
chain head to genesis and then iterate all the difficulties to compute
the TD.
In a world where we completely prune past chain segments (bodies,
receipts, headers), it is not possible to reconstruct the TD at all. In
a world where we still keep chain headers and prune only the rest,
reconstructing it possible as long as we process (or download) the chain
forward from genesis, but trying to snap sync the head first and
backfill later hits the same issue, the TD becomes impossible to
calculate until genesis is backfilled.
All in all, the TD is a messy out-of-state, out-of-consensus computed
field that is overall useless nowadays, but code relying on it forces
the client into certain modes of operation and prevents other modes or
other optimizations. This PR completely nukes out the TD from the node.
It doesn't compute it, it doesn't operate on it, it's as if it didn't
even exist.
Caveats:
- Whenever we have APIs that return TD (devp2p handshake, tracer, etc.)
we return a TD of 0.
- For era files, we recompute the TD during export time (fairly quick)
to retain the format content.
- It is not possible to "verify" the merge point (i.e. with TD gone, TTD
is useless). Since we're not verifying PoW any more, just blindly trust
it, not verifying but blindly trusting the many year old merge point
seems just the same trust model.
- Our tests still need to be able to generate pre and post merge blocks,
so they need a new way to split the merge without TTD. The PR introduces
a settable ttdBlock field on the consensus object which is used by tests
as the block where originally the TTD happened. This is not needed for
live nodes, we never want to generate old blocks.
- One merge transition consensus test was disabled. With a
non-operational TD, testing how the client reacts to TTD is useless, it
cannot react.
Questions:
- Should we also drop total terminal difficulty from the genesis json?
It's a number we cannot react on any more, so maybe it would be cleaner
to get rid of even more concepts.
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2025-01-28 11:55:41 -06:00
|
|
|
if err := src.Handshake(1, head.Hash(), genesis.Hash(), forkid.NewIDWithChain(handler.chain), forkid.NewFilter(handler.chain)); err != nil {
|
2020-12-14 03:27:15 -06:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This test checks that pending transactions are sent.
|
2022-10-31 09:23:26 -05:00
|
|
|
func TestSendTransactions68(t *testing.T) { testSendTransactions(t, eth.ETH68) }
|
2020-12-14 03:27:15 -06: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()
|
|
|
|
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 03:13:34 -05:00
|
|
|
insert := make([]*types.Transaction, 100)
|
2020-12-14 03:27:15 -06:00
|
|
|
for nonce := range insert {
|
2021-08-24 13:52:58 -05:00
|
|
|
tx := types.NewTransaction(uint64(nonce), common.Address{}, big.NewInt(0), 100000, big.NewInt(0), make([]byte, 10240))
|
2020-12-14 03:27:15 -06:00
|
|
|
tx, _ = types.SignTx(tx, types.HomesteadSigner{}, testKey)
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 03:13:34 -05:00
|
|
|
insert[nonce] = tx
|
2020-12-14 03:27:15 -06:00
|
|
|
}
|
core/txpool: remove locals-tracking from txpools (#30559)
Replaces #29297, descendant from #27535
---------
This PR removes `locals` as a concept from transaction pools. Therefore,
the pool now acts as very a good simulation/approximation of how our
peers' pools behave. What this PR does instead, is implement a
locals-tracker, which basically is a little thing which, from time to
time, asks the pool "did you forget this transaction?". If it did, the
tracker resubmits it.
If the txpool _had_ forgotten it, chances are that the peers had also
forgotten it. It will be propagated again.
Doing this change means that we can simplify the pool internals, quite a
lot.
### The semantics of `local`
Historically, there has been two features, or usecases, that has been
combined into the concept of `locals`.
1. "I want my local node to remember this transaction indefinitely, and
resubmit to the network occasionally"
2. "I want this (valid) transaction included to be top-prio for my
miner"
This PR splits these features up, let's call it `1: local` and `2:
prio`. The `prio` is not actually individual transaction, but rather a
set of `address`es to prioritize.
The attribute `local` means it will be tracked, and `prio` means it will
be prioritized by miner.
For `local`: anything transaction received via the RPC is marked as
`local`, and tracked by the tracker.
For `prio`: any transactions from this sender is included first, when
building a block. The existing commandline-flag `--txpool.locals` sets
the set of `prio` addresses.
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2025-02-04 10:23:01 -06:00
|
|
|
go handler.txpool.Add(insert, 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 03:27:15 -06: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 15:20:36 -05: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 03:27:15 -06: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()
|
|
|
|
)
|
all: nuke total difficulty (#30744)
The total difficulty is the sum of all block difficulties from genesis
to a certain block. This value was used in PoW for deciding which chain
is heavier, and thus which chain to select. Since PoS has a different
fork selection algorithm, all blocks since the merge have a difficulty
of 0, and all total difficulties are the same for the past 2 years.
Whilst the TDs are mostly useless nowadays, there was never really a
reason to mess around removing them since they are so tiny. This
reasoning changes when we go down the path of pruned chain history. In
order to reconstruct any TD, we **must** retrieve all the headers from
chain head to genesis and then iterate all the difficulties to compute
the TD.
In a world where we completely prune past chain segments (bodies,
receipts, headers), it is not possible to reconstruct the TD at all. In
a world where we still keep chain headers and prune only the rest,
reconstructing it possible as long as we process (or download) the chain
forward from genesis, but trying to snap sync the head first and
backfill later hits the same issue, the TD becomes impossible to
calculate until genesis is backfilled.
All in all, the TD is a messy out-of-state, out-of-consensus computed
field that is overall useless nowadays, but code relying on it forces
the client into certain modes of operation and prevents other modes or
other optimizations. This PR completely nukes out the TD from the node.
It doesn't compute it, it doesn't operate on it, it's as if it didn't
even exist.
Caveats:
- Whenever we have APIs that return TD (devp2p handshake, tracer, etc.)
we return a TD of 0.
- For era files, we recompute the TD during export time (fairly quick)
to retain the format content.
- It is not possible to "verify" the merge point (i.e. with TD gone, TTD
is useless). Since we're not verifying PoW any more, just blindly trust
it, not verifying but blindly trusting the many year old merge point
seems just the same trust model.
- Our tests still need to be able to generate pre and post merge blocks,
so they need a new way to split the merge without TTD. The PR introduces
a settable ttdBlock field on the consensus object which is used by tests
as the block where originally the TTD happened. This is not needed for
live nodes, we never want to generate old blocks.
- One merge transition consensus test was disabled. With a
non-operational TD, testing how the client reacts to TTD is useless, it
cannot react.
Questions:
- Should we also drop total terminal difficulty from the genesis json?
It's a number we cannot react on any more, so maybe it would be cleaner
to get rid of even more concepts.
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2025-01-28 11:55:41 -06:00
|
|
|
if err := sink.Handshake(1, head.Hash(), genesis.Hash(), forkid.NewIDWithChain(handler.chain), forkid.NewFilter(handler.chain)); err != nil {
|
2020-12-14 03:27:15 -06: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 {
|
2024-02-08 07:49:19 -06:00
|
|
|
case 68:
|
2020-12-14 03:27:15 -06: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 05:26:03 -06:00
|
|
|
t.Errorf("initial tx broadcast received on post eth/66")
|
2020-12-14 03:27:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
panic("unsupported protocol, please extend test")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, tx := range insert {
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 03:13:34 -05:00
|
|
|
if _, ok := seen[tx.Hash()]; !ok {
|
|
|
|
t.Errorf("missing transaction: %x", tx.Hash())
|
2020-12-14 03:27:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests that transactions get propagated to all attached peers, either via direct
|
|
|
|
// broadcasts or via announcements/retrievals.
|
2022-10-31 09:23:26 -05:00
|
|
|
func TestTransactionPropagation68(t *testing.T) { testTransactionPropagation(t, eth.ETH68) }
|
2020-12-14 03:27:15 -06: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 05:06:50 -05:00
|
|
|
source.handler.snapSync.Store(false) // Avoid requiring snap, otherwise some will be dropped below
|
2020-12-14 03:27:15 -06:00
|
|
|
defer source.close()
|
|
|
|
|
|
|
|
sinks := make([]*testHandler, 10)
|
|
|
|
for i := 0; i < len(sinks); i++ {
|
|
|
|
sinks[i] = newTestHandler()
|
|
|
|
defer sinks[i].close()
|
|
|
|
|
core, accounts, eth, trie: handle genesis state missing (#28171)
* core, accounts, eth, trie: handle genesis state missing
* core, eth, trie: polish
* core: manage txpool subscription in mainpool
* eth/backend: fix test
* cmd, eth: fix test
* core/rawdb, trie/triedb/pathdb: address comments
* eth, trie: address comments
* eth: inline the function
* eth: use synced flag
* core/txpool: revert changes in txpool
* core, eth, trie: rename functions
2023-09-28 02:00:53 -05:00
|
|
|
sinks[i].handler.synced.Store(true) // mark synced to accept transactions
|
2020-12-14 03:27:15 -06:00
|
|
|
}
|
|
|
|
// Interconnect all the sink handlers with the source handler
|
|
|
|
for i, sink := range sinks {
|
|
|
|
sourcePipe, sinkPipe := p2p.MsgPipe()
|
|
|
|
defer sourcePipe.Close()
|
|
|
|
defer sinkPipe.Close()
|
|
|
|
|
2021-11-26 05:26:03 -06:00
|
|
|
sourcePeer := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{byte(i + 1)}, "", nil, sourcePipe), sourcePipe, source.txpool)
|
2021-05-25 15:20:36 -05:00
|
|
|
sinkPeer := eth.NewPeer(protocol, p2p.NewPeerPipe(enode.ID{0}, "", nil, sinkPipe), sinkPipe, sink.txpool)
|
2020-12-14 03:27:15 -06: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)
|
|
|
|
|
2023-10-04 04:36:36 -05:00
|
|
|
sub := sinks[i].txpool.SubscribeTransactions(txChs[i], false)
|
2020-12-14 03:27:15 -06:00
|
|
|
defer sub.Unsubscribe()
|
|
|
|
}
|
|
|
|
// Fill the source pool with transactions and wait for them at the sinks
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 03:13:34 -05:00
|
|
|
txs := make([]*types.Transaction, 1024)
|
2020-12-14 03:27:15 -06: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)
|
core/types: support for optional blob sidecar in BlobTx (#27841)
This PR removes the newly added txpool.Transaction wrapper type, and instead adds a way
of keeping the blob sidecar within types.Transaction. It's better this way because most
code in go-ethereum does not care about blob transactions, and probably never will. This
will start mattering especially on the client side of RPC, where all APIs are based on
types.Transaction. Users need to be able to use the same signing flows they already
have.
However, since blobs are only allowed in some places but not others, we will now need to
add checks to avoid creating invalid blocks. I'm still trying to figure out the best place
to do some of these. The way I have it currently is as follows:
- In block validation (import), txs are verified not to have a blob sidecar.
- In miner, we strip off the sidecar when committing the transaction into the block.
- In TxPool validation, txs must have a sidecar to be added into the blobpool.
- Note there is a special case here: when transactions are re-added because of a chain
reorg, we cannot use the transactions gathered from the old chain blocks as-is,
because they will be missing their blobs. This was previously handled by storing the
blobs into the 'blobpool limbo'. The code has now changed to store the full
transaction in the limbo instead, but it might be confusing for code readers why we're
not simply adding the types.Transaction we already have.
Code changes summary:
- txpool.Transaction removed and all uses replaced by types.Transaction again
- blobpool now stores types.Transaction instead of defining its own blobTx format for storage
- the blobpool limbo now stores types.Transaction instead of storing only the blobs
- checks to validate the presence/absence of the blob sidecar added in certain critical places
2023-08-14 03:13:34 -05:00
|
|
|
txs[nonce] = tx
|
2020-12-14 03:27:15 -06:00
|
|
|
}
|
core/txpool: remove locals-tracking from txpools (#30559)
Replaces #29297, descendant from #27535
---------
This PR removes `locals` as a concept from transaction pools. Therefore,
the pool now acts as very a good simulation/approximation of how our
peers' pools behave. What this PR does instead, is implement a
locals-tracker, which basically is a little thing which, from time to
time, asks the pool "did you forget this transaction?". If it did, the
tracker resubmits it.
If the txpool _had_ forgotten it, chances are that the peers had also
forgotten it. It will be propagated again.
Doing this change means that we can simplify the pool internals, quite a
lot.
### The semantics of `local`
Historically, there has been two features, or usecases, that has been
combined into the concept of `locals`.
1. "I want my local node to remember this transaction indefinitely, and
resubmit to the network occasionally"
2. "I want this (valid) transaction included to be top-prio for my
miner"
This PR splits these features up, let's call it `1: local` and `2:
prio`. The `prio` is not actually individual transaction, but rather a
set of `address`es to prioritize.
The attribute `local` means it will be tracked, and `prio` means it will
be prioritized by miner.
For `local`: anything transaction received via the RPC is marked as
`local`, and tracked by the tracker.
For `prio`: any transactions from this sender is included first, when
building a block. The existing commandline-flag `--txpool.locals` sets
the set of `prio` addresses.
---------
Co-authored-by: Gary Rong <garyrong0905@gmail.com>
2025-02-04 10:23:01 -06:00
|
|
|
source.txpool.Add(txs, false)
|
2020-12-14 03:27:15 -06:00
|
|
|
|
|
|
|
// Iterate through all the sinks and ensure they all got the transactions
|
|
|
|
for i := range sinks {
|
2021-11-26 05:26:03 -06:00
|
|
|
for arrived, timeout := 0, false; arrived < len(txs) && !timeout; {
|
2020-12-14 03:27:15 -06:00
|
|
|
select {
|
|
|
|
case event := <-txChs[i]:
|
|
|
|
arrived += len(event.Txs)
|
2022-11-11 06:22:54 -06:00
|
|
|
case <-time.After(2 * time.Second):
|
2020-12-14 03:27:15 -06:00
|
|
|
t.Errorf("sink %d: transaction propagation timed out: have %d, want %d", i, arrived, len(txs))
|
2021-11-26 05:26:03 -06:00
|
|
|
timeout = true
|
2020-12-14 03:27:15 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|