Skip to content

Commit

Permalink
Fixes after code movement.
Browse files Browse the repository at this point in the history
  • Loading branch information
andreibancioiu committed Dec 19, 2024
1 parent 1272a82 commit 0e34756
Show file tree
Hide file tree
Showing 18 changed files with 156 additions and 268 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ require (
github.com/multiversx/mx-chain-es-indexer-go v1.7.11-0.20241118100151-956a1f23c5c1
github.com/multiversx/mx-chain-logger-go v1.0.15
github.com/multiversx/mx-chain-scenario-go v1.4.4
github.com/multiversx/mx-chain-storage-go v1.0.19-0.20241213090416-f46569554341
github.com/multiversx/mx-chain-storage-go v1.0.19-0.20241219081227-acfcb55b960e
github.com/multiversx/mx-chain-vm-common-go v1.5.16
github.com/multiversx/mx-chain-vm-go v1.5.37
github.com/multiversx/mx-chain-vm-v1_2-go v1.2.68
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -397,8 +397,8 @@ github.com/multiversx/mx-chain-logger-go v1.0.15 h1:HlNdK8etyJyL9NQ+6mIXyKPEBo+w
github.com/multiversx/mx-chain-logger-go v1.0.15/go.mod h1:t3PRKaWB1M+i6gUfD27KXgzLJJC+mAQiN+FLlL1yoGQ=
github.com/multiversx/mx-chain-scenario-go v1.4.4 h1:DVE2V+FPeyD/yWoC+KEfPK3jsFzHeruelESfpTlf460=
github.com/multiversx/mx-chain-scenario-go v1.4.4/go.mod h1:kI+TWR3oIEgUkbwkHCPo2CQ3VjIge+ezGTibiSGwMxo=
github.com/multiversx/mx-chain-storage-go v1.0.19-0.20241213090416-f46569554341 h1:SydNXPZIt7UpcveL8mUnOGAh+Oped851w2bGbaGqsWw=
github.com/multiversx/mx-chain-storage-go v1.0.19-0.20241213090416-f46569554341/go.mod h1:Ec+CrhDskz+UPcw/WjOCtQS4uCA1GNCseO3qM6SHj+A=
github.com/multiversx/mx-chain-storage-go v1.0.19-0.20241219081227-acfcb55b960e h1:7+trDIUedNYbhZpePvgLzCi7cadcFhKrnluXrUD2RKQ=
github.com/multiversx/mx-chain-storage-go v1.0.19-0.20241219081227-acfcb55b960e/go.mod h1:Ec+CrhDskz+UPcw/WjOCtQS4uCA1GNCseO3qM6SHj+A=
github.com/multiversx/mx-chain-vm-common-go v1.5.16 h1:g1SqYjxl7K66Y1O/q6tvDJ37fzpzlxCSfRzSm/woQQY=
github.com/multiversx/mx-chain-vm-common-go v1.5.16/go.mod h1:1rSkXreUZNXyPTTdhj47M+Fy62yjxbu3aAsXEtKN3UY=
github.com/multiversx/mx-chain-vm-go v1.5.37 h1:Iy3KCvM+DOq1f9UPA7uYK/rI3ZbBOXc2CVNO2/vm5zw=
Expand Down
8 changes: 2 additions & 6 deletions process/block/preprocess/selectionSession.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/multiversx/mx-chain-core-go/data/transaction"
"github.com/multiversx/mx-chain-go/process"
"github.com/multiversx/mx-chain-go/state"
"github.com/multiversx/mx-chain-go/storage/txcache"
vmcommon "github.com/multiversx/mx-chain-vm-common-go"
)

Expand Down Expand Up @@ -43,7 +42,7 @@ func newSelectionSession(args argsSelectionSession) (*selectionSession, error) {

// GetAccountState returns the state of an account.
// Will be called by mempool during transaction selection.
func (session *selectionSession) GetAccountState(address []byte) (*txcache.AccountState, error) {
func (session *selectionSession) GetAccountState(address []byte) (state.UserAccountHandler, error) {
account, err := session.getExistingAccount(address)
if err != nil {
return nil, err
Expand All @@ -54,10 +53,7 @@ func (session *selectionSession) GetAccountState(address []byte) (*txcache.Accou
return nil, process.ErrWrongTypeAssertion
}

return &txcache.AccountState{
Nonce: userAccount.GetNonce(),
Balance: userAccount.GetBalance(),
}, nil
return userAccount, nil
}

func (session *selectionSession) getExistingAccount(address []byte) (vmcommon.AccountHandler, error) {
Expand Down
7 changes: 2 additions & 5 deletions storage/txcache/txcache.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
// TODO: Remove this file in a future PR.
package txcache

import (
"github.com/multiversx/mx-chain-storage-go/txcache"
"github.com/multiversx/mx-chain-storage-go/types"
"github.com/multiversx/mx-chain-go/txcache"
)

// WrappedTransaction contains a transaction, its hash and extra information
type WrappedTransaction = txcache.WrappedTransaction

// AccountState represents the state of an account (as seen by the mempool)
type AccountState = types.AccountState

// MempoolHost provides blockchain information for mempool operations
type MempoolHost = txcache.MempoolHost

Expand Down
41 changes: 39 additions & 2 deletions testscommon/txcachemocks/mempoolHostMock.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,33 @@ 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
extraGasLimitForGuarded uint64
extraGasLimitForRelayed uint64

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

// NewMempoolHostMock -
func NewMempoolHostMock() *MempoolHostMock {
return &MempoolHostMock{}
return &MempoolHostMock{
minGasLimit: 50_000,
minGasPrice: 1_000_000_000,
gasPerDataByte: 1500,
gasPriceModifier: 0.01,
extraGasLimitForGuarded: 50_000,
extraGasLimitForRelayed: 50_000,
}
}

// ComputeTxFee -
Expand All @@ -23,7 +38,29 @@ func (mock *MempoolHostMock) ComputeTxFee(tx data.TransactionWithFeeHandler) *bi
return mock.ComputeTxFeeCalled(tx)
}

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

gasLimitForMovement := mock.minGasLimit + dataLength*mock.gasPerDataByte

if txAsGuarded, ok := tx.(data.GuardedTransactionHandler); ok && len(txAsGuarded.GetGuardianAddr()) > 0 {
gasLimitForMovement += mock.extraGasLimitForGuarded
}

if txAsRelayed, ok := tx.(data.RelayedTransactionHandler); ok && len(txAsRelayed.GetRelayerAddr()) > 0 {
gasLimitForMovement += mock.extraGasLimitForRelayed
}

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 -
Expand Down
96 changes: 96 additions & 0 deletions testscommon/txcachemocks/selectionSessionMock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package txcachemocks

import (
"math/big"
"sync"

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

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

NumCallsGetAccountState int

AccountStateByAddress map[string]*stateMock.UserAccountStub
GetAccountStateCalled func(address []byte) (state.UserAccountHandler, error)
IsIncorrectlyGuardedCalled func(tx data.TransactionHandler) bool
}

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

// 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) (state.UserAccountHandler, error) {
mock.mutex.Lock()
defer mock.mutex.Unlock()

mock.NumCallsGetAccountState++

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() *stateMock.UserAccountStub {
return &stateMock.UserAccountStub{
Nonce: 0,
Balance: big.NewInt(1000000000000000000),
}
}
Loading

0 comments on commit 0e34756

Please sign in to comment.