Skip to content

Commit

Permalink
Merge branch 'feat/mempool' into MX-16160-consumed-balance
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Nov 28, 2024
2 parents cde9ed9 + c8b1efb commit 156b124
Show file tree
Hide file tree
Showing 7 changed files with 26 additions and 30 deletions.
16 changes: 8 additions & 8 deletions testscommon/txcachemocks/selectionSessionMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ import (
type SelectionSessionMock struct {
mutex sync.Mutex

AccountStateByAddress map[string]*types.AccountState
GetAccountStateCalled func(address []byte) (*types.AccountState, error)
IsBadlyGuardedCalled func(tx data.TransactionHandler) bool
GetTransferredValueCalled func(tx data.TransactionHandler) *big.Int
AccountStateByAddress map[string]*types.AccountState
GetAccountStateCalled func(address []byte) (*types.AccountState, error)
IsIncorrectlyGuardedCalled func(tx data.TransactionHandler) bool
GetTransferredValueCalled func(tx data.TransactionHandler) *big.Int
}

// NewSelectionSessionMock -
Expand Down Expand Up @@ -70,10 +70,10 @@ func (mock *SelectionSessionMock) GetAccountState(address []byte) (*types.Accoun
return newDefaultAccountState(), nil
}

// IsBadlyGuarded -
func (mock *SelectionSessionMock) IsBadlyGuarded(tx data.TransactionHandler) bool {
if mock.IsBadlyGuardedCalled != nil {
return mock.IsBadlyGuardedCalled(tx)
// IsIncorrectlyGuarded -
func (mock *SelectionSessionMock) IsIncorrectlyGuarded(tx data.TransactionHandler) bool {
if mock.IsIncorrectlyGuardedCalled != nil {
return mock.IsIncorrectlyGuardedCalled(tx)
}

return false
Expand Down
2 changes: 1 addition & 1 deletion txcache/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ Thus, the mempool selects transactions using an efficient and value-driven algor
- If a middle nonce gap is detected, the sender is skipped (from now on) in the current selection session.
- Transactions with nonces lower than the current nonce of the sender are skipped.
- Transactions having the same nonce as a previously selected one (in the scope of a sender) are skipped. Also see paragraph 5.
- Badly guarded transactions are skipped.
- Incorrectly guarded transactions are skipped.
- Once the accumulated fees of selected transactions of a given sender exceed the sender's balance, the sender is skipped (from now one).


Expand Down
2 changes: 1 addition & 1 deletion txcache/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type TxGasHandler interface {
// SelectionSession provides blockchain information for transaction selection
type SelectionSession interface {
GetAccountState(accountKey []byte) (*types.AccountState, error)
IsBadlyGuarded(tx data.TransactionHandler) bool
IsIncorrectlyGuarded(tx data.TransactionHandler) bool
GetTransferredValue(tx data.TransactionHandler) *big.Int
IsInterfaceNil() bool
}
Expand Down
2 changes: 1 addition & 1 deletion txcache/selection.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func detectSkippableTransaction(session SelectionSession, item *transactionsHeap
if item.detectLowerNonce() {
return true
}
if item.detectBadlyGuarded(session) {
if item.detectIncorrectlyGuarded(session) {
return true
}
if item.detectNonceDuplicate() {
Expand Down
10 changes: 3 additions & 7 deletions txcache/selection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,18 +224,14 @@ func TestTxCache_SelectTransactions_HandlesNotExecutableTransactions(t *testing.
require.Equal(t, 200000, int(accumulatedGas))
})

t.Run("with badly guarded", func(t *testing.T) {
t.Run("with incorrectly guarded", func(t *testing.T) {
cache := newUnconstrainedCacheToTest()
session := txcachemocks.NewSelectionSessionMock()
session.SetNonce([]byte("alice"), 1)
session.SetNonce([]byte("bob"), 42)

session.IsBadlyGuardedCalled = func(tx data.TransactionHandler) bool {
if bytes.Equal(tx.GetData(), []byte("t")) {
return true
}

return false
session.IsIncorrectlyGuardedCalled = func(tx data.TransactionHandler) bool {
return bytes.Equal(tx.GetData(), []byte("t"))
}

cache.AddTx(createTx([]byte("hash-alice-1"), "alice", 1).withData([]byte("x")).withGasLimit(100000))
Expand Down
10 changes: 5 additions & 5 deletions txcache/transactionsHeapItem.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,16 +161,16 @@ func (item *transactionsHeapItem) detectLowerNonce() bool {
return isLowerNonce
}

func (item *transactionsHeapItem) detectBadlyGuarded(session SelectionSession) bool {
isBadlyGuarded := session.IsBadlyGuarded(item.currentTransaction.Tx)
if isBadlyGuarded {
logSelect.Trace("transactionsHeapItem.detectBadlyGuarded",
func (item *transactionsHeapItem) detectIncorrectlyGuarded(session SelectionSession) bool {
IsIncorrectlyGuarded := session.IsIncorrectlyGuarded(item.currentTransaction.Tx)
if IsIncorrectlyGuarded {
logSelect.Trace("transactionsHeapItem.detectIncorrectlyGuarded",
"tx", item.currentTransaction.TxHash,
"sender", item.sender,
)
}

return isBadlyGuarded
return IsIncorrectlyGuarded
}

func (item *transactionsHeapItem) detectNonceDuplicate() bool {
Expand Down
14 changes: 7 additions & 7 deletions txcache/transactionsHeapItem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,29 +274,29 @@ func TestTransactionsHeapItem_detectNonceDuplicate(t *testing.T) {
})
}

func TestTransactionsHeapItem_detectBadlyGuarded(t *testing.T) {
t.Run("is not badly guarded", func(t *testing.T) {
func TestTransactionsHeapItem_detectIncorrectlyGuarded(t *testing.T) {
t.Run("is correctly guarded", func(t *testing.T) {
session := txcachemocks.NewSelectionSessionMock()
session.IsBadlyGuardedCalled = func(tx data.TransactionHandler) bool {
session.IsIncorrectlyGuardedCalled = func(tx data.TransactionHandler) bool {
return false
}

item, err := newTransactionsHeapItem(bunchOfTransactions{createTx([]byte("tx-1"), "alice", 42)})
require.NoError(t, err)

require.False(t, item.detectBadlyGuarded(session))
require.False(t, item.detectIncorrectlyGuarded(session))
})

t.Run("is badly guarded", func(t *testing.T) {
t.Run("is incorrectly guarded", func(t *testing.T) {
session := txcachemocks.NewSelectionSessionMock()
session.IsBadlyGuardedCalled = func(tx data.TransactionHandler) bool {
session.IsIncorrectlyGuardedCalled = func(tx data.TransactionHandler) bool {
return true
}

item, err := newTransactionsHeapItem(bunchOfTransactions{createTx([]byte("tx-1"), "alice", 42)})
require.NoError(t, err)

require.True(t, item.detectBadlyGuarded(session))
require.True(t, item.detectIncorrectlyGuarded(session))
})
}

Expand Down

0 comments on commit 156b124

Please sign in to comment.