Skip to content

Commit

Permalink
core, les: fix les unit tests (#19823)
Browse files Browse the repository at this point in the history
  • Loading branch information
rjl493456442 authored and karalabe committed Jul 22, 2019
1 parent a32a2b9 commit b80c840
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 12 deletions.
4 changes: 2 additions & 2 deletions core/tx_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -745,13 +745,13 @@ func (pool *TxPool) AddRemotes(txs []*types.Transaction) []error {
}

// This is like AddRemotes, but waits for pool reorganization. Tests use this method.
func (pool *TxPool) addRemotesSync(txs []*types.Transaction) []error {
func (pool *TxPool) AddRemotesSync(txs []*types.Transaction) []error {
return pool.addTxs(txs, false, true)
}

// This is like AddRemotes with a single transaction, but waits for pool reorganization. Tests use this method.
func (pool *TxPool) addRemoteSync(tx *types.Transaction) error {
errs := pool.addRemotesSync([]*types.Transaction{tx})
errs := pool.AddRemotesSync([]*types.Transaction{tx})
return errs[0]
}

Expand Down
18 changes: 9 additions & 9 deletions core/tx_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ func TestStateChangeDuringTransactionPoolReset(t *testing.T) {
t.Fatalf("Invalid nonce, want 0, got %d", nonce)
}

pool.addRemotesSync([]*types.Transaction{tx0, tx1})
pool.AddRemotesSync([]*types.Transaction{tx0, tx1})

nonce = pool.Nonce(address)
if nonce != 2 {
Expand Down Expand Up @@ -587,7 +587,7 @@ func TestTransactionPostponing(t *testing.T) {
txs = append(txs, tx)
}
}
for i, err := range pool.addRemotesSync(txs) {
for i, err := range pool.AddRemotesSync(txs) {
if err != nil {
t.Fatalf("tx %d: failed to add transactions: %v", i, err)
}
Expand Down Expand Up @@ -683,7 +683,7 @@ func TestTransactionGapFilling(t *testing.T) {
defer sub.Unsubscribe()

// Create a pending and a queued transaction with a nonce-gap in between
pool.addRemotesSync([]*types.Transaction{
pool.AddRemotesSync([]*types.Transaction{
transaction(0, 100000, key),
transaction(2, 100000, key),
})
Expand Down Expand Up @@ -800,7 +800,7 @@ func testTransactionQueueGlobalLimiting(t *testing.T, nolocals bool) {
nonces[addr]++
}
// Import the batch and verify that limits have been enforced
pool.addRemotesSync(txs)
pool.AddRemotesSync(txs)

queued := 0
for addr, list := range pool.queue {
Expand Down Expand Up @@ -988,7 +988,7 @@ func TestTransactionPendingGlobalLimiting(t *testing.T) {
}
}
// Import the batch and verify that limits have been enforced
pool.addRemotesSync(txs)
pool.AddRemotesSync(txs)

pending := 0
for _, list := range pool.pending {
Expand Down Expand Up @@ -1068,7 +1068,7 @@ func TestTransactionPendingMinimumAllowance(t *testing.T) {
}
}
// Import the batch and verify that limits have been enforced
pool.addRemotesSync(txs)
pool.AddRemotesSync(txs)

for addr, list := range pool.pending {
if list.Len() != int(config.AccountSlots) {
Expand Down Expand Up @@ -1124,7 +1124,7 @@ func TestTransactionPoolRepricing(t *testing.T) {
ltx := pricedTransaction(0, 100000, big.NewInt(1), keys[3])

// Import the batch and that both pending and queued transactions match up
pool.addRemotesSync(txs)
pool.AddRemotesSync(txs)
pool.AddLocal(ltx)

pending, queued := pool.Stats()
Expand Down Expand Up @@ -1404,7 +1404,7 @@ func TestTransactionPoolStableUnderpricing(t *testing.T) {
for i := uint64(0); i < config.GlobalSlots; i++ {
txs = append(txs, pricedTransaction(i, 100000, big.NewInt(1), keys[0]))
}
pool.addRemotesSync(txs)
pool.AddRemotesSync(txs)

pending, queued := pool.Stats()
if pending != int(config.GlobalSlots) {
Expand Down Expand Up @@ -1658,7 +1658,7 @@ func TestTransactionStatusCheck(t *testing.T) {
txs = append(txs, pricedTransaction(2, 100000, big.NewInt(1), keys[2])) // Queued only

// Import the transaction and ensure they are correctly added
pool.addRemotesSync(txs)
pool.AddRemotesSync(txs)

pending, queued := pool.Stats()
if pending != 2 {
Expand Down
11 changes: 10 additions & 1 deletion les/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ type BlockChain interface {

type txPool interface {
AddRemotes(txs []*types.Transaction) []error
AddRemotesSync(txs []*types.Transaction) []error
Status(hashes []common.Hash) []core.TxStatus
}

Expand Down Expand Up @@ -125,6 +126,9 @@ type ProtocolManager struct {

// Callbacks
synced func() bool

// Testing fields
addTxsSync bool
}

// NewProtocolManager returns a new ethereum sub protocol manager. The Ethereum sub protocol manages peers capable
Expand Down Expand Up @@ -1044,7 +1048,12 @@ func (pm *ProtocolManager) handleMsg(p *peer) error {
hash := tx.Hash()
stats[i] = pm.txStatus(hash)
if stats[i].Status == core.TxStatusUnknown {
if errs := pm.txpool.AddRemotes([]*types.Transaction{tx}); errs[0] != nil {
addFn := pm.txpool.AddRemotes
// Add txs synchronously for testing purpose
if pm.addTxsSync {
addFn = pm.txpool.AddRemotesSync
}
if errs := addFn([]*types.Transaction{tx}); errs[0] != nil {
stats[i].Error = errs[0].Error()
continue
}
Expand Down
1 change: 1 addition & 0 deletions les/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,7 @@ func TestGetBloombitsProofs(t *testing.T) {
func TestTransactionStatusLes2(t *testing.T) {
server, tearDown := newServerEnv(t, 0, 2, nil)
defer tearDown()
server.pm.addTxsSync = true

chain := server.pm.blockchain.(*core.BlockChain)
config := core.DefaultTxPoolConfig
Expand Down
Empty file added les/transactions.rlp
Empty file.

0 comments on commit b80c840

Please sign in to comment.