1e556d220c
* all: remove notion of trusted checkpoints in the post-merge world * light: remove unused function * eth/ethconfig, les: remove unused config option * les: make linter happy --------- Co-authored-by: Gary Rong <garyrong0905@gmail.com>
147 lines
4.2 KiB
Go
147 lines
4.2 KiB
Go
// 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/>.
|
|
|
|
package light
|
|
|
|
import (
|
|
"context"
|
|
"math"
|
|
"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/rawdb"
|
|
"github.com/ethereum/go-ethereum/core/types"
|
|
"github.com/ethereum/go-ethereum/core/vm"
|
|
"github.com/ethereum/go-ethereum/params"
|
|
)
|
|
|
|
type testTxRelay struct {
|
|
send, discard, mined chan int
|
|
}
|
|
|
|
func (r *testTxRelay) Send(txs types.Transactions) {
|
|
r.send <- len(txs)
|
|
}
|
|
|
|
func (r *testTxRelay) NewHead(head common.Hash, mined []common.Hash, rollback []common.Hash) {
|
|
m := len(mined)
|
|
if m != 0 {
|
|
r.mined <- m
|
|
}
|
|
}
|
|
|
|
func (r *testTxRelay) Discard(hashes []common.Hash) {
|
|
r.discard <- len(hashes)
|
|
}
|
|
|
|
const poolTestTxs = 1000
|
|
const poolTestBlocks = 100
|
|
|
|
// test tx 0..n-1
|
|
var testTx [poolTestTxs]*types.Transaction
|
|
|
|
// txs sent before block i
|
|
func sentTx(i int) int {
|
|
return int(math.Pow(float64(i)/float64(poolTestBlocks), 0.9) * poolTestTxs)
|
|
}
|
|
|
|
// txs included in block i or before that (minedTx(i) <= sentTx(i))
|
|
func minedTx(i int) int {
|
|
return int(math.Pow(float64(i)/float64(poolTestBlocks), 1.1) * poolTestTxs)
|
|
}
|
|
|
|
func txPoolTestChainGen(i int, block *core.BlockGen) {
|
|
s := minedTx(i)
|
|
e := minedTx(i + 1)
|
|
for i := s; i < e; i++ {
|
|
block.AddTx(testTx[i])
|
|
}
|
|
}
|
|
|
|
func TestTxPool(t *testing.T) {
|
|
for i := range testTx {
|
|
testTx[i], _ = types.SignTx(types.NewTransaction(uint64(i), acc1Addr, big.NewInt(10000), params.TxGas, big.NewInt(params.InitialBaseFee), nil), types.HomesteadSigner{}, testBankKey)
|
|
}
|
|
|
|
var (
|
|
sdb = rawdb.NewMemoryDatabase()
|
|
ldb = rawdb.NewMemoryDatabase()
|
|
gspec = &core.Genesis{
|
|
Config: params.TestChainConfig,
|
|
Alloc: core.GenesisAlloc{testBankAddress: {Balance: testBankFunds}},
|
|
BaseFee: big.NewInt(params.InitialBaseFee),
|
|
}
|
|
)
|
|
// Assemble the test environment
|
|
blockchain, _ := core.NewBlockChain(sdb, nil, gspec, nil, ethash.NewFullFaker(), vm.Config{}, nil, nil)
|
|
_, gchain, _ := core.GenerateChainWithGenesis(gspec, ethash.NewFaker(), poolTestBlocks, txPoolTestChainGen)
|
|
if _, err := blockchain.InsertChain(gchain); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
gspec.MustCommit(ldb)
|
|
odr := &testOdr{sdb: sdb, ldb: ldb, serverState: blockchain.StateCache(), indexerConfig: TestClientIndexerConfig}
|
|
relay := &testTxRelay{
|
|
send: make(chan int, 1),
|
|
discard: make(chan int, 1),
|
|
mined: make(chan int, 1),
|
|
}
|
|
lightchain, _ := NewLightChain(odr, params.TestChainConfig, ethash.NewFullFaker())
|
|
txPermanent = 50
|
|
pool := NewTxPool(params.TestChainConfig, lightchain, relay)
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
|
defer cancel()
|
|
|
|
for ii, block := range gchain {
|
|
i := ii + 1
|
|
s := sentTx(i - 1)
|
|
e := sentTx(i)
|
|
for i := s; i < e; i++ {
|
|
pool.Add(ctx, testTx[i])
|
|
got := <-relay.send
|
|
exp := 1
|
|
if got != exp {
|
|
t.Errorf("relay.Send expected len = %d, got %d", exp, got)
|
|
}
|
|
}
|
|
|
|
if _, err := lightchain.InsertHeaderChain([]*types.Header{block.Header()}, 1); err != nil {
|
|
panic(err)
|
|
}
|
|
|
|
got := <-relay.mined
|
|
exp := minedTx(i) - minedTx(i-1)
|
|
if got != exp {
|
|
t.Errorf("relay.NewHead expected len(mined) = %d, got %d", exp, got)
|
|
}
|
|
|
|
exp = 0
|
|
if i > int(txPermanent)+1 {
|
|
exp = minedTx(i-int(txPermanent)-1) - minedTx(i-int(txPermanent)-2)
|
|
}
|
|
if exp != 0 {
|
|
got = <-relay.discard
|
|
if got != exp {
|
|
t.Errorf("relay.Discard expected len = %d, got %d", exp, got)
|
|
}
|
|
}
|
|
}
|
|
}
|