Skip to content

Commit 59293c2

Browse files
committed
kill pools.testEvalContext struct and eval.TestEvalContext interface
1 parent 38edb5c commit 59293c2

File tree

2 files changed

+38
-71
lines changed

2 files changed

+38
-71
lines changed

data/pools/transactionPool.go

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,8 @@ type TransactionPool struct {
9898
// exceed the txPoolMaxSize. This flag is reset to false OnNewBlock
9999
stateproofOverflowed bool
100100

101-
testBlockEvaluator *eval.TestBlockEvaluator
102-
testBlockEvaluatorMu deadlock.RWMutex
101+
txnGroupTester *eval.TransactionGroupTester
102+
txnGroupTesterMu deadlock.RWMutex
103103

104104
// shutdown is set to true when the pool is being shut down. It is checked in exported methods
105105
// to prevent pool operations like remember and recomputing the block evaluator
@@ -117,36 +117,6 @@ type BlockEvaluator interface {
117117
ResetTxnBytes()
118118
}
119119

120-
// testEvalContext implements the eval.TestEvalContext interface. It allows for concurrent
121-
// calls to TestTransactionGroup for candidate transactions calling transactionPool.Test.
122-
type testEvalContext struct {
123-
ledger *ledger.Ledger
124-
block bookkeeping.Block
125-
proto config.ConsensusParams
126-
specials transactions.SpecialAddresses
127-
}
128-
129-
func newTestBlockEvaluator(ledger *ledger.Ledger, block bookkeeping.Block) *eval.TestBlockEvaluator {
130-
return &eval.TestBlockEvaluator{
131-
TestEvalContext: &testEvalContext{
132-
ledger: ledger,
133-
block: block,
134-
proto: config.Consensus[block.CurrentProtocol],
135-
specials: transactions.SpecialAddresses{
136-
FeeSink: block.FeeSink,
137-
RewardsPool: block.RewardsPool,
138-
},
139-
}}
140-
}
141-
142-
func (c *testEvalContext) Proto() config.ConsensusParams { return c.proto }
143-
func (c *testEvalContext) Specials() transactions.SpecialAddresses { return c.specials }
144-
func (c *testEvalContext) TxnContext() transactions.TxnContext { return c.block }
145-
func (c *testEvalContext) CheckDup(firstValid, lastValid basics.Round, txid transactions.Txid, txl ledgercore.Txlease) error {
146-
// will call txTail.checkDup, which uses an RLock for concurrent access.
147-
return c.ledger.CheckDup(c.proto, c.block.BlockHeader.Round, firstValid, lastValid, txid, txl)
148-
}
149-
150120
// VotingAccountSupplier provides a list of possible participating account addresses valid for a given round.
151121
type VotingAccountSupplier interface {
152122
VotingAccountsForRound(basics.Round) []basics.Address
@@ -429,14 +399,14 @@ func (pool *TransactionPool) Test(txgroup []transactions.SignedTxn) error {
429399
return err
430400
}
431401

432-
pool.testBlockEvaluatorMu.RLock()
433-
defer pool.testBlockEvaluatorMu.RUnlock()
402+
pool.txnGroupTesterMu.RLock()
403+
defer pool.txnGroupTesterMu.RUnlock()
434404

435-
if pool.testBlockEvaluator == nil {
405+
if pool.txnGroupTester == nil {
436406
return fmt.Errorf("Test: testEvalCtx is nil")
437407
}
438408

439-
return pool.testBlockEvaluator.TestTransactionGroup(txgroup)
409+
return pool.txnGroupTester.TestTransactionGroup(txgroup)
440410
}
441411

442412
type poolIngestParams struct {
@@ -783,9 +753,19 @@ func (pool *TransactionPool) recomputeBlockEvaluator(committedTxIDs map[transact
783753
return
784754
}
785755

786-
pool.testBlockEvaluatorMu.Lock()
787-
pool.testBlockEvaluator = newTestBlockEvaluator(pool.ledger, next)
788-
pool.testBlockEvaluatorMu.Unlock()
756+
pool.txnGroupTesterMu.Lock()
757+
pool.txnGroupTester = &eval.TransactionGroupTester{
758+
CheckDup: func(firstValid, lastValid basics.Round, txid transactions.Txid, txl ledgercore.Txlease) error {
759+
return pool.ledger.CheckDup(config.Consensus[next.CurrentProtocol], next.BlockHeader.Round, firstValid, lastValid, txid, txl)
760+
},
761+
TxnContext: next,
762+
Proto: config.Consensus[next.CurrentProtocol],
763+
Specials: transactions.SpecialAddresses{
764+
FeeSink: next.FeeSink,
765+
RewardsPool: next.RewardsPool,
766+
},
767+
}
768+
pool.txnGroupTesterMu.Unlock()
789769

790770
var asmStats telemetryspec.AssembleBlockMetrics
791771
asmStats.StartCount = len(txgroups)

ledger/eval/eval.go

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -904,50 +904,37 @@ func (eval *BlockEvaluator) ResetTxnBytes() {
904904
eval.blockTxBytes = 0
905905
}
906906

907-
// TestEvalContext defines the evaluation context required by TestBlockEvaluator
908-
// to check for well-formedness and duplicate detection.
909-
type TestEvalContext interface {
910-
Proto() config.ConsensusParams
911-
Specials() transactions.SpecialAddresses
912-
TxnContext() transactions.TxnContext
913-
CheckDup(firstValid, lastValid basics.Round, txid transactions.Txid, txl ledgercore.Txlease) error
914-
}
915-
916-
// Proto implements the TestEvalContext interface.
917-
func (eval *BlockEvaluator) Proto() config.ConsensusParams { return eval.proto }
918-
919-
// Specials implements the TestEvalContext interface.
920-
func (eval *BlockEvaluator) Specials() transactions.SpecialAddresses { return eval.specials }
921-
922-
// TxnContext implements the TestEvalContext interface.
923-
func (eval *BlockEvaluator) TxnContext() transactions.TxnContext { return eval.block }
924-
925-
// CheckDup implements the TestEvalContext interface.
926-
func (eval *BlockEvaluator) CheckDup(firstValid, lastValid basics.Round, txid transactions.Txid, txl ledgercore.Txlease) error {
927-
return eval.state.checkDup(firstValid, lastValid, txid, txl)
928-
}
929-
930907
// TestTransactionGroup is only called by tests.
931908
func (eval *BlockEvaluator) TestTransactionGroup(txgroup []transactions.SignedTxn) error {
932-
return TestBlockEvaluator{eval}.TestTransactionGroup(txgroup)
909+
return TransactionGroupTester{
910+
Proto: eval.proto,
911+
Specials: eval.specials,
912+
TxnContext: eval.block,
913+
CheckDup: eval.state.checkDup,
914+
}.TestTransactionGroup(txgroup)
933915
}
934916

935-
// TestBlockEvaluator uses a TestEvalContext to perform basic transaction checks.
936-
type TestBlockEvaluator struct{ TestEvalContext }
917+
// TransactionGroupTester performs basic transaction checks for well-formedness and duplicate detection.
918+
type TransactionGroupTester struct {
919+
CheckDup func(firstValid, lastValid basics.Round, txid transactions.Txid, txl ledgercore.Txlease) error
920+
TxnContext transactions.TxnContext
921+
Proto config.ConsensusParams
922+
Specials transactions.SpecialAddresses
923+
}
937924

938925
// TestTransactionGroup performs basic duplicate detection and well-formedness checks
939926
// on a transaction group, but does not actually add the transactions to the block
940927
// evaluator, or modify the block evaluator state in any other visible way.
941928
// It uses a TestEvalContext to access needed recent ledger state.
942-
func (eval TestBlockEvaluator) TestTransactionGroup(txgroup []transactions.SignedTxn) error {
929+
func (eval TransactionGroupTester) TestTransactionGroup(txgroup []transactions.SignedTxn) error {
943930
// Nothing to do if there are no transactions.
944931
if len(txgroup) == 0 {
945932
return nil
946933
}
947934

948-
if len(txgroup) > eval.Proto().MaxTxGroupSize {
935+
if len(txgroup) > eval.Proto.MaxTxGroupSize {
949936
return &ledgercore.TxGroupMalformedError{
950-
Msg: fmt.Sprintf("group size %d exceeds maximum %d", len(txgroup), eval.Proto().MaxTxGroupSize),
937+
Msg: fmt.Sprintf("group size %d exceeds maximum %d", len(txgroup), eval.Proto.MaxTxGroupSize),
951938
Reason: ledgercore.TxGroupMalformedErrorReasonExceedMaxSize,
952939
}
953940
}
@@ -998,14 +985,14 @@ func (eval TestBlockEvaluator) TestTransactionGroup(txgroup []transactions.Signe
998985
// TestTransaction performs basic duplicate detection and well-formedness checks
999986
// on a single transaction, but does not actually add the transaction to the block
1000987
// evaluator, or modify the block evaluator state in any other visible way.
1001-
func (eval TestBlockEvaluator) TestTransaction(txn transactions.SignedTxn) error {
988+
func (eval TransactionGroupTester) TestTransaction(txn transactions.SignedTxn) error {
1002989
// Transaction valid (not expired)?
1003-
err := txn.Txn.Alive(eval.TxnContext())
990+
err := txn.Txn.Alive(eval.TxnContext)
1004991
if err != nil {
1005992
return err
1006993
}
1007994

1008-
err = txn.Txn.WellFormed(eval.Specials(), eval.Proto())
995+
err = txn.Txn.WellFormed(eval.Specials, eval.Proto)
1009996
if err != nil {
1010997
txnErr := ledgercore.TxnNotWellFormedError(fmt.Sprintf("transaction %v: malformed: %v", txn.ID(), err))
1011998
return &txnErr

0 commit comments

Comments
 (0)