Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mempool improvements #62

Merged
merged 194 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
194 commits
Select commit Hold shift + click to select a range
41fb6b8
On NotifyAccountNonce, remove transactions with lower nonces.
andreibancioiu Jul 1, 2024
dc2ff1c
WrappedTransaction.computeFee() delegates to the Node's economicsData…
andreibancioiu Jul 3, 2024
1875775
Remove older feeComputationHelper.
andreibancioiu Jul 3, 2024
5282c22
New score function (avg ppu, scaled, logistic, re-scaled). Refactoring.
andreibancioiu Jul 3, 2024
8504682
Integrate new score params in txListForSender.
andreibancioiu Jul 3, 2024
3d04082
Implement noncesTracker. Able to tell if a nonces sequence is "spotle…
andreibancioiu Jul 3, 2024
ad6bc75
Adjust tests.
andreibancioiu Jul 4, 2024
d3f9e63
Adjust log levels.
andreibancioiu Jul 4, 2024
3dd13fe
Adjust some tests.
andreibancioiu Jul 4, 2024
5874167
Fix score, fix tests.
andreibancioiu Jul 5, 2024
ccf6cf3
Simplification of sender score params.
andreibancioiu Jul 5, 2024
7095682
Fix score, add tests.
andreibancioiu Jul 5, 2024
21e1354
Optimizations.
andreibancioiu Jul 5, 2024
5e2d116
Optimize "findInsertionPlace".
andreibancioiu Jul 5, 2024
c795b7a
Fix tests.
andreibancioiu Jul 5, 2024
11ebe83
Additional tests.
andreibancioiu Jul 8, 2024
5fb4b11
Monitoring cleanup.
andreibancioiu Jul 8, 2024
b7dd503
Compute score on tx addition / removal. Sort senders only on selectio…
andreibancioiu Jul 8, 2024
6f61e51
Fix tests, benchmarks.
andreibancioiu Jul 8, 2024
c1697a5
Optimization at insertion time.
andreibancioiu Jul 8, 2024
a2bc648
Optimization at tx removal.
andreibancioiu Jul 8, 2024
377369b
Bit of refactoring, more tests.
andreibancioiu Jul 8, 2024
610342c
Monitor senders histogram.
andreibancioiu Jul 10, 2024
ce5c433
Additional logging.
andreibancioiu Jul 10, 2024
4d835de
Batchsize, bandwidth computation.
andreibancioiu Jul 10, 2024
aec42e7
Additional monitoring.
andreibancioiu Jul 10, 2024
14f8719
Adjust printing.
andreibancioiu Jul 10, 2024
3557e7c
computeSelectionSenderConstraints() based on score.
andreibancioiu Jul 10, 2024
b1b22d2
Fix condition.
andreibancioiu Jul 10, 2024
acdfd8d
Rename constant, fix array length.
andreibancioiu Jul 10, 2024
f996c68
Renaming.
andreibancioiu Jul 10, 2024
46c0d40
Some todos, notes.
andreibancioiu Jul 11, 2024
73b5848
notifyAccountNonce: remove sender if empty after txs removal.
andreibancioiu Jul 11, 2024
9c234af
No more logic on "sweep sweepable senders". Old transactions are remo…
andreibancioiu Jul 11, 2024
c5cb6fb
Remove concept of "grace period" for senders with initial gaps. No tr…
andreibancioiu Jul 11, 2024
3a15954
Remove the counter numFailedSelections.
andreibancioiu Jul 11, 2024
1f4033a
Minor refactoring.
andreibancioiu Jul 11, 2024
ad8c55b
A little bit of refactoring.
andreibancioiu Jul 11, 2024
ba403bd
Add total gas constraints when selecting transactions.
andreibancioiu Jul 15, 2024
e8e808d
Bit of refactoring.
andreibancioiu Jul 15, 2024
7a2e122
Shuffle the items of a group (senders with the same score). Add some …
andreibancioiu Jul 15, 2024
0714177
Some refactoring.
andreibancioiu Jul 15, 2024
61d37d4
Fix linter, fix tests.
andreibancioiu Jul 15, 2024
e50fbe5
Monitoring, refactoring, optimizations. Fix score computation.
andreibancioiu Jul 16, 2024
4f7726d
Optimization on notifyAccountNonce.
andreibancioiu Jul 16, 2024
1da6b62
Adjust some tests.
andreibancioiu Jul 17, 2024
8920291
Fix build.
andreibancioiu Jul 17, 2024
50fa20e
Better diagnostics.
andreibancioiu Jul 18, 2024
13a3a96
Diagnose selection.
andreibancioiu Jul 25, 2024
ed6b638
Simplifications, refactoring.
andreibancioiu Jul 25, 2024
9211efa
Monitoring simplifications.
andreibancioiu Jul 25, 2024
dede39c
Fix logs.
andreibancioiu Jul 25, 2024
843bbd2
Extra logs, test, comments etc.
andreibancioiu Jul 26, 2024
9b8f619
Refactoring, fix interface etc.
andreibancioiu Jul 30, 2024
07437b3
Fix diagnosis / duration.
andreibancioiu Aug 6, 2024
a03e955
Additional logging.
andreibancioiu Sep 6, 2024
91e0519
Additional logs when gaps are detected (at selection time).
andreibancioiu Sep 6, 2024
2235ce3
Fix evictTransactionsWithLowerNonces.
andreibancioiu Sep 6, 2024
f79ad1b
Sketch readme.
andreibancioiu Oct 2, 2024
4a30d0c
Docs, work in progress.
andreibancioiu Oct 2, 2024
8352a3f
Docs, work in progress. Additional tests.
andreibancioiu Oct 2, 2024
712f71a
On selection, don't use "numRequested" anymore (limit is wrt. "gas re…
andreibancioiu Oct 29, 2024
a30472f
Preparatory cleanup.
andreibancioiu Oct 29, 2024
a71a512
Sketch selection using merges.
andreibancioiu Oct 30, 2024
a8c39b7
Sketch getTxsWithoutGaps (not tested).
andreibancioiu Oct 30, 2024
5a42102
Sketch computePricePerGasUnit.
andreibancioiu Oct 31, 2024
d1063be
Fix isTransactionGreater().
andreibancioiu Oct 31, 2024
0d96e18
Sketch parallelization (not tested).
andreibancioiu Oct 31, 2024
b8cf28a
Fix & simplify mergeTwoBunchesOfTransactions.
andreibancioiu Oct 31, 2024
f40df76
Integrate new selection (work in progress).
andreibancioiu Oct 31, 2024
5e67c02
Sketch / fix merging.
andreibancioiu Oct 31, 2024
ce28507
Improve isTransactionGreater().
andreibancioiu Oct 31, 2024
e50d419
Add some tests.
andreibancioiu Oct 31, 2024
fc80fe3
Sketch eviction (not tested).
andreibancioiu Oct 31, 2024
430744f
Simplify / refactor.
andreibancioiu Nov 1, 2024
71de44f
Optimize / simplify merging.
andreibancioiu Nov 1, 2024
9099c66
Fix eviction, add some tests.
andreibancioiu Nov 1, 2024
688b985
Fix call of wg.Done().
andreibancioiu Nov 1, 2024
5fa3797
Tests, trial and error.
andreibancioiu Nov 1, 2024
0b2e201
Additional testing.
andreibancioiu Nov 1, 2024
65d4953
selectTransactionsUsingHeap().
andreibancioiu Nov 1, 2024
4a9fa71
Refactor; isTransactionMoreDesirableByProtocol().
andreibancioiu Nov 1, 2024
5a3fb74
More unit tests.
andreibancioiu Nov 1, 2024
4b1ec6d
More unit tests.
andreibancioiu Nov 1, 2024
8d28cc6
Refactor. Split min/max heaps.
andreibancioiu Nov 1, 2024
7565d6e
Fix config, fix tests.
andreibancioiu Nov 1, 2024
a6faf2e
Reimplement eviction, using min heap.
andreibancioiu Nov 1, 2024
1920a2f
Fix eviction, work in progress.
andreibancioiu Nov 1, 2024
608bda0
Remove selection using merges / merge sort.
andreibancioiu Nov 1, 2024
786f4e2
Refactor, fix tests.
andreibancioiu Nov 4, 2024
8b89f4c
Fix disabled cache.
andreibancioiu Nov 4, 2024
81bb99d
Use float for PPU.
andreibancioiu Nov 4, 2024
0681184
Additional guard for gas limit.
andreibancioiu Nov 4, 2024
15c28eb
Refactor, simplify monitoring.
andreibancioiu Nov 4, 2024
62c0f96
Rename file.
andreibancioiu Nov 4, 2024
8348e11
Refactor, fix logging.
andreibancioiu Nov 4, 2024
4240502
Additional logging.
andreibancioiu Nov 4, 2024
be56be9
Fix selection (accumulated gas condition).
andreibancioiu Nov 4, 2024
ff89596
Log accumulated gas.
andreibancioiu Nov 4, 2024
e0332d2
Fix detection of initial gaps.
andreibancioiu Nov 4, 2024
8f53bbf
Cleanup, logging, benchmarks, minor optimizations.
andreibancioiu Nov 4, 2024
9b2b959
Refactor / split file.
andreibancioiu Nov 4, 2024
105701d
Optimize isTransactionMoreDesirableByProtocol().
andreibancioiu Nov 4, 2024
0257f64
Limit selection by "maxNum" transactions.
andreibancioiu Nov 5, 2024
3d7c01c
Additional tests and benchmarks.
andreibancioiu Nov 5, 2024
7ee693c
Move tests.
andreibancioiu Nov 5, 2024
6a69b3e
Fix disabled cache.
andreibancioiu Nov 5, 2024
7c5b85d
Fix lint warnings.
andreibancioiu Nov 5, 2024
8e3d20f
Remove unused constant (fix linter).
andreibancioiu Nov 5, 2024
2bc824a
Minor simplification at eviction. Add function getTxsReversed().
andreibancioiu Nov 5, 2024
74bc040
Cleanup, some readme, refactoring.
andreibancioiu Nov 5, 2024
5ee99a2
Cleanup.
andreibancioiu Nov 5, 2024
09f30ed
Fix after self-review.
andreibancioiu Nov 5, 2024
9e31b00
Adjust logging.
andreibancioiu Nov 6, 2024
e8cf425
Merge branch 'rc/v1.7.next1' into selection-by-ppu
andreibancioiu Nov 7, 2024
1a7e9f2
Fix after review.
andreibancioiu Nov 10, 2024
b45ce82
In "getSequentialTxs", handle duplicates, as well.
andreibancioiu Nov 10, 2024
fe86b68
On nonce notifications, don't remove transactions. On RemoveTxByHash,…
andreibancioiu Nov 11, 2024
44d940a
Fix handling of lower nonces (act only when nonce is known). Refactor…
andreibancioiu Nov 12, 2024
53c5bd8
Implement ForgetAllAccountNonces().
andreibancioiu Nov 12, 2024
b91be71
Describe selection flow in readme.
andreibancioiu Nov 13, 2024
10f1c36
Typos.
andreibancioiu Nov 13, 2024
8cd2eb0
Update readme.
andreibancioiu Nov 13, 2024
fa41478
Fix after review.
andreibancioiu Nov 13, 2024
a8324c5
Merge pull request #37 from multiversx/rc/v1.7.next1
sstanculeanu Nov 13, 2024
2231c3d
Don't notify about account nonces, don't forget etc.
andreibancioiu Nov 13, 2024
b22177e
When selecting transactions, receive an account nonce provider.
andreibancioiu Nov 13, 2024
3714684
Refactor, pass "AccountNonceProvider" in constructor.
andreibancioiu Nov 13, 2024
74e7ac5
Quick sketch of selection changes.
andreibancioiu Nov 13, 2024
d583eae
Fix tests and comments.
andreibancioiu Nov 13, 2024
94131c7
Refactoring.
andreibancioiu Nov 13, 2024
4ca46fd
Refactor, optimize.
andreibancioiu Nov 13, 2024
20d4ba4
Fix after self-review.
andreibancioiu Nov 14, 2024
68835d1
Fix after review, drop logic around fnv32.
andreibancioiu Nov 14, 2024
ee6476d
Update readme.
andreibancioiu Nov 14, 2024
4f94f63
Merge branch 'selection-by-ppu' into MX-16107-no-more-notify
andreibancioiu Nov 14, 2024
0f01e5c
Additional tests.
andreibancioiu Nov 14, 2024
80b4979
Fix after review.
andreibancioiu Nov 14, 2024
f97351c
Receive the nonce provider in SelectTransactions, instead of receivin…
andreibancioiu Nov 15, 2024
0db9d39
Break the selection loop if it takes too long.
andreibancioiu Nov 15, 2024
3bbf408
AccountNonceProvider becomes AccountStateProvider (more information f…
andreibancioiu Nov 18, 2024
bae6b43
Additional logs on cross tx cache.
andreibancioiu Nov 18, 2024
613f5ba
Hold fee on tx, handle accumulated fees. Avoid non-executable transac…
andreibancioiu Nov 18, 2024
95a888a
Reference new core-go.
andreibancioiu Nov 18, 2024
ebe0e12
Handle guarded transactions with same nonce.
andreibancioiu Nov 18, 2024
c61ce2a
Better readme etc.
andreibancioiu Nov 18, 2024
26f0189
Fix tests.
andreibancioiu Nov 19, 2024
03c5adb
Better handling of not-executable transactions.
andreibancioiu Nov 20, 2024
679a465
A few optimizations.
andreibancioiu Nov 20, 2024
de2620b
Fix fee exceeded balance detection. Refactoring.
andreibancioiu Nov 20, 2024
fe1bd09
Additional unit tests.
andreibancioiu Nov 20, 2024
67c1c6e
Adjust benchmark output.
andreibancioiu Nov 20, 2024
e20d2e5
Fix tests.
andreibancioiu Nov 20, 2024
b2fa1ce
Additional unit tests.
andreibancioiu Nov 20, 2024
48853fb
Fix after self review.
andreibancioiu Nov 20, 2024
f159f60
Fix after self review.
andreibancioiu Nov 20, 2024
d5ef41b
Remove out-of-reality test.
andreibancioiu Nov 20, 2024
457c06c
Update readme.
andreibancioiu Nov 21, 2024
1229a5f
Merge pull request #55 from multiversx/selection-by-ppu
andreibancioiu Nov 21, 2024
1b8274d
Merge branch 'feat/mempool' into MX-16107-no-more-notify
andreibancioiu Nov 21, 2024
92d0476
Fix linter issues.
andreibancioiu Nov 21, 2024
93c958a
Fix after review (part 1).
andreibancioiu Nov 24, 2024
65f6207
Call "isBadlyGuarded()" of the node.
andreibancioiu Nov 24, 2024
42abafc
Rename "AccountStateProvider" to "SelectionSession".
andreibancioiu Nov 25, 2024
3a7e727
Additional tests.
andreibancioiu Nov 25, 2024
afd51e2
Handle consumed balance.
andreibancioiu Nov 25, 2024
00adab2
Additional unit tests.
andreibancioiu Nov 25, 2024
0710e1b
Delayed commit.
andreibancioiu Nov 26, 2024
8a0cb50
Fix after review (part 2).
andreibancioiu Nov 26, 2024
edbfc56
Merge branch 'MX-16107-no-more-notify' into MX-16179-guardians
andreibancioiu Nov 27, 2024
d1b4205
Fix test.
andreibancioiu Nov 27, 2024
cde9ed9
Merge branch 'MX-16179-guardians' into MX-16160-consumed-balance
andreibancioiu Nov 27, 2024
37ace93
Merge pull request #57 from multiversx/MX-16107-no-more-notify
andreibancioiu Nov 27, 2024
679f656
Merge branch 'feat/mempool' into MX-16179-guardians
andreibancioiu Nov 27, 2024
90581ee
Fix after review.
andreibancioiu Nov 28, 2024
c8b1efb
Merge pull request #58 from multiversx/MX-16179-guardians
andreibancioiu Nov 28, 2024
156b124
Merge branch 'feat/mempool' into MX-16160-consumed-balance
andreibancioiu Nov 28, 2024
a3a849d
Merge pull request #59 from multiversx/MX-16160-consumed-balance
AdoAdoAdo Nov 28, 2024
77bbd7a
Rename "TxGasHandler" to "MempoolHost".
andreibancioiu Nov 28, 2024
ea3ee2d
Move "GetTransferredValue()" to "MempoolHost". Define transferred val…
andreibancioiu Nov 28, 2024
a019fe1
Update description of isTransactionMoreValuableForNetwork.
andreibancioiu Nov 29, 2024
d11b529
Fix typo.
andreibancioiu Nov 29, 2024
da4eff4
Merge pull request #60 from multiversx/MX-16211-optimize-get-value
andreibancioiu Nov 29, 2024
5a5a12b
Merge branch 'feat/mempool' into fix-txcache-readme
andreibancioiu Nov 29, 2024
3182661
Merge pull request #61 from multiversx/fix-txcache-readme
andreibancioiu Nov 29, 2024
d97a6ba
Merge branch 'main' into mempool-update-2024-11-29
andreibancioiu Nov 29, 2024
d0d8ce4
Fix linter issues.
andreibancioiu Nov 29, 2024
8c5c73c
Merge pull request #63 from multiversx/mempool-update-2024-11-29
andreibancioiu Nov 29, 2024
68d174a
Add benchmarks for addition in worst case.
andreibancioiu Dec 2, 2024
f9a268f
Fix linter issue.
andreibancioiu Dec 2, 2024
8d4a698
Merge pull request #64 from multiversx/MX-16202-sender-constraints
andreibancioiu Dec 2, 2024
729f45b
Adjust benchmarks, lower maxNum when selecting transactions.
andreibancioiu Dec 2, 2024
f8323f4
Adjust test config.
andreibancioiu Dec 2, 2024
16da4a5
Merge pull request #65 from multiversx/MX-16207-selection-max-num
andreibancioiu Dec 2, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 1 addition & 6 deletions common/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package common

import (
"errors"

"github.com/multiversx/mx-chain-core-go/core"
)

Expand Down Expand Up @@ -50,9 +51,6 @@ var ErrFailedCacheEviction = errors.New("failed eviction within cache")
// ErrImmuneItemsCapacityReached signals that capacity for immune items is reached
var ErrImmuneItemsCapacityReached = errors.New("capacity reached for immune items")

// ErrItemAlreadyInCache signals that an item is already in cache
var ErrItemAlreadyInCache = errors.New("item already in cache")

// ErrCacheSizeInvalid signals that size of cache is less than 1
var ErrCacheSizeInvalid = errors.New("cache size is less than 1")

Expand All @@ -71,9 +69,6 @@ var ErrNegativeSizeInBytes = errors.New("negative size in bytes")
// ErrNilTimeCache signals that a nil time cache has been provided
var ErrNilTimeCache = errors.New("nil time cache")

// ErrNilTxGasHandler signals that a nil tx gas handler was provided
var ErrNilTxGasHandler = errors.New("nil tx gas handler")

// ErrNilStoredDataFactory signals that a nil stored data factory has been provided
var ErrNilStoredDataFactory = errors.New("nil stored data factory")

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.20
require (
github.com/hashicorp/golang-lru v0.6.0
github.com/multiversx/concurrent-map v0.1.4
github.com/multiversx/mx-chain-core-go v1.2.21
github.com/multiversx/mx-chain-core-go v1.2.23
github.com/multiversx/mx-chain-logger-go v1.0.15
github.com/stretchr/testify v1.7.2
github.com/syndtr/goleveldb v1.0.1-0.20220721030215-126854af5e6d
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@ github.com/mr-tron/base58 v1.2.0 h1:T/HDJBh4ZCPbU39/+c3rRvE0uKBQlU27+QI8LJ4t64o=
github.com/mr-tron/base58 v1.2.0/go.mod h1:BinMc/sQntlIE1frQmRFPUoPA1Zkr8VRgBdjWI2mNwc=
github.com/multiversx/concurrent-map v0.1.4 h1:hdnbM8VE4b0KYJaGY5yJS2aNIW9TFFsUYwbO0993uPI=
github.com/multiversx/concurrent-map v0.1.4/go.mod h1:8cWFRJDOrWHOTNSqgYCUvwT7c7eFQ4U2vKMOp4A/9+o=
github.com/multiversx/mx-chain-core-go v1.2.21 h1:+XVKznPTlUU5EFS1A8chtS8fStW60upRIyF4Pgml19I=
github.com/multiversx/mx-chain-core-go v1.2.21/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE=
github.com/multiversx/mx-chain-core-go v1.2.23 h1:8WlCGqJHR2HQ0vN4feJwb7W4VrCwBGIzPPHunOOg5Wc=
github.com/multiversx/mx-chain-core-go v1.2.23/go.mod h1:B5zU4MFyJezmEzCsAHE9YNULmGCm2zbPHvl9hazNxmE=
github.com/multiversx/mx-chain-logger-go v1.0.15 h1:HlNdK8etyJyL9NQ+6mIXyKPEBo+wRqOwi3n+m2QIHXc=
github.com/multiversx/mx-chain-logger-go v1.0.15/go.mod h1:t3PRKaWB1M+i6gUfD27KXgzLJJC+mAQiN+FLlL1yoGQ=
github.com/nxadm/tail v1.4.4/go.mod h1:kenIhsEOeOJmVchQTgglprH7qJGnHDVpk1VPCcaMI8A=
Expand Down
65 changes: 65 additions & 0 deletions testscommon/txcachemocks/mempoolHostMock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package txcachemocks

import (
"math/big"

"github.com/multiversx/mx-chain-core-go/core"
"github.com/multiversx/mx-chain-core-go/data"
)

// MempoolHostMock -
type MempoolHostMock struct {
minGasLimit uint64
minGasPrice uint64
gasPerDataByte uint64
gasPriceModifier float64

ComputeTxFeeCalled func(tx data.TransactionWithFeeHandler) *big.Int
GetTransferredValueCalled func(tx data.TransactionHandler) *big.Int
}

// NewMempoolHostMock -
func NewMempoolHostMock() *MempoolHostMock {
return &MempoolHostMock{
minGasLimit: 50000,
minGasPrice: 1000000000,
gasPerDataByte: 1500,
gasPriceModifier: 0.01,
}
}

// ComputeTxFee -
func (mock *MempoolHostMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *big.Int {
if mock.ComputeTxFeeCalled != nil {
return mock.ComputeTxFeeCalled(tx)
}

dataLength := uint64(len(tx.GetData()))
gasPriceForMovement := tx.GetGasPrice()
gasPriceForProcessing := uint64(float64(gasPriceForMovement) * mock.gasPriceModifier)

gasLimitForMovement := mock.minGasLimit + dataLength*mock.gasPerDataByte
if tx.GetGasLimit() < gasLimitForMovement {
panic("tx.GetGasLimit() < gasLimitForMovement")
}

gasLimitForProcessing := tx.GetGasLimit() - gasLimitForMovement
feeForMovement := core.SafeMul(gasPriceForMovement, gasLimitForMovement)
feeForProcessing := core.SafeMul(gasPriceForProcessing, gasLimitForProcessing)
fee := big.NewInt(0).Add(feeForMovement, feeForProcessing)
return fee
}

// GetTransferredValue -
func (mock *MempoolHostMock) GetTransferredValue(tx data.TransactionHandler) *big.Int {
if mock.GetTransferredValueCalled != nil {
return mock.GetTransferredValueCalled(tx)
}

return tx.GetValue()
}

// IsInterfaceNil -
func (mock *MempoolHostMock) IsInterfaceNil() bool {
return mock == nil
}
91 changes: 91 additions & 0 deletions testscommon/txcachemocks/selectionSessionMock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package txcachemocks

import (
"math/big"
"sync"

"github.com/multiversx/mx-chain-core-go/data"
"github.com/multiversx/mx-chain-storage-go/types"
)

// SelectionSessionMock -
type SelectionSessionMock struct {
mutex sync.Mutex

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

// NewSelectionSessionMock -
func NewSelectionSessionMock() *SelectionSessionMock {
return &SelectionSessionMock{
AccountStateByAddress: make(map[string]*types.AccountState),
}
}

// SetNonce -
func (mock *SelectionSessionMock) SetNonce(address []byte, nonce uint64) {
mock.mutex.Lock()
defer mock.mutex.Unlock()

key := string(address)

if mock.AccountStateByAddress[key] == nil {
mock.AccountStateByAddress[key] = newDefaultAccountState()
}

mock.AccountStateByAddress[key].Nonce = nonce
}

// SetBalance -
func (mock *SelectionSessionMock) SetBalance(address []byte, balance *big.Int) {
mock.mutex.Lock()
defer mock.mutex.Unlock()

key := string(address)

if mock.AccountStateByAddress[key] == nil {
mock.AccountStateByAddress[key] = newDefaultAccountState()
}

mock.AccountStateByAddress[key].Balance = balance
}

// GetAccountState -
func (mock *SelectionSessionMock) GetAccountState(address []byte) (*types.AccountState, error) {
mock.mutex.Lock()
defer mock.mutex.Unlock()

if mock.GetAccountStateCalled != nil {
return mock.GetAccountStateCalled(address)
}

state, ok := mock.AccountStateByAddress[string(address)]
if ok {
return state, nil
}

return newDefaultAccountState(), nil
}

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

return false
}

// IsInterfaceNil -
func (mock *SelectionSessionMock) IsInterfaceNil() bool {
return mock == nil
}

func newDefaultAccountState() *types.AccountState {
return &types.AccountState{
Nonce: 0,
Balance: big.NewInt(1000000000000000000),
}
}
59 changes: 0 additions & 59 deletions testscommon/txcachemocks/txGasHandlerMock.go

This file was deleted.

Loading