Skip to content

Commit

Permalink
Merge pull request #66 from multiversx/rc/spica-patch-mempool
Browse files Browse the repository at this point in the history
Rc/spica patch mempool
  • Loading branch information
sstanculeanu authored Dec 11, 2024
2 parents a8324c5 + 1df41c6 commit 87978fa
Show file tree
Hide file tree
Showing 44 changed files with 2,913 additions and 3,261 deletions.
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

0 comments on commit 87978fa

Please sign in to comment.