Skip to content

Commit 93c958a

Browse files
Fix after review (part 1).
1 parent 92d0476 commit 93c958a

7 files changed

+20
-140
lines changed

testscommon/txcachemocks/accountStateProviderMock.go

+14-14
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ package txcachemocks
22

33
import (
44
"math/big"
5+
"sync"
56

67
"github.com/multiversx/mx-chain-storage-go/types"
78
)
89

910
// AccountStateProviderMock -
1011
type AccountStateProviderMock struct {
12+
mutex sync.Mutex
13+
1114
AccountStateByAddress map[string]*types.AccountState
1215
GetAccountStateCalled func(address []byte) (*types.AccountState, error)
1316
}
@@ -21,6 +24,9 @@ func NewAccountStateProviderMock() *AccountStateProviderMock {
2124

2225
// SetNonce -
2326
func (mock *AccountStateProviderMock) SetNonce(address []byte, nonce uint64) {
27+
mock.mutex.Lock()
28+
defer mock.mutex.Unlock()
29+
2430
key := string(address)
2531

2632
if mock.AccountStateByAddress[key] == nil {
@@ -32,28 +38,23 @@ func (mock *AccountStateProviderMock) SetNonce(address []byte, nonce uint64) {
3238

3339
// SetBalance -
3440
func (mock *AccountStateProviderMock) SetBalance(address []byte, balance *big.Int) {
35-
key := string(address)
36-
37-
if mock.AccountStateByAddress[key] == nil {
38-
mock.AccountStateByAddress[key] = newDefaultAccountState()
39-
}
40-
41-
mock.AccountStateByAddress[key].Balance = balance
42-
}
41+
mock.mutex.Lock()
42+
defer mock.mutex.Unlock()
4343

44-
// SetGuardian -
45-
func (mock *AccountStateProviderMock) SetGuardian(address []byte, guardian []byte) {
4644
key := string(address)
4745

4846
if mock.AccountStateByAddress[key] == nil {
4947
mock.AccountStateByAddress[key] = newDefaultAccountState()
5048
}
5149

52-
mock.AccountStateByAddress[key].Guardian = guardian
50+
mock.AccountStateByAddress[key].Balance = balance
5351
}
5452

5553
// GetAccountState -
5654
func (mock *AccountStateProviderMock) GetAccountState(address []byte) (*types.AccountState, error) {
55+
mock.mutex.Lock()
56+
defer mock.mutex.Unlock()
57+
5758
if mock.GetAccountStateCalled != nil {
5859
return mock.GetAccountStateCalled(address)
5960
}
@@ -73,8 +74,7 @@ func (mock *AccountStateProviderMock) IsInterfaceNil() bool {
7374

7475
func newDefaultAccountState() *types.AccountState {
7576
return &types.AccountState{
76-
Nonce: 0,
77-
Balance: big.NewInt(1000000000000000000),
78-
Guardian: nil,
77+
Nonce: 0,
78+
Balance: big.NewInt(1000000000000000000),
7979
}
8080
}

txcache/selection_test.go

-20
Original file line numberDiff line numberDiff line change
@@ -221,26 +221,6 @@ func TestTxCache_SelectTransactions_HandlesNotExecutableTransactions(t *testing.
221221
require.Len(t, sorted, expectedNumSelected)
222222
require.Equal(t, 200000, int(accumulatedGas))
223223
})
224-
225-
t.Run("with guardians", func(t *testing.T) {
226-
cache := newUnconstrainedCacheToTest()
227-
accountStateProvider := txcachemocks.NewAccountStateProviderMock()
228-
accountStateProvider.SetNonce([]byte("alice"), 1)
229-
accountStateProvider.SetNonce([]byte("bob"), 42)
230-
accountStateProvider.SetGuardian([]byte("bob"), []byte("heidi"))
231-
232-
cache.AddTx(createTx([]byte("hash-alice-1"), "alice", 1))
233-
cache.AddTx(createTx([]byte("hash-bob-42a"), "bob", 42))
234-
cache.AddTx(createTx([]byte("hash-bob-42b"), "bob", 42).withGuardian([]byte("heidi")).withGasLimit(100000))
235-
cache.AddTx(createTx([]byte("hash-bob-43"), "bob", 43).withGuardian([]byte("grace")).withGasLimit(100000))
236-
237-
sorted, accumulatedGas := cache.SelectTransactions(accountStateProvider, math.MaxUint64, math.MaxInt, selectionLoopMaximumDuration)
238-
require.Len(t, sorted, 2)
239-
require.Equal(t, 150000, int(accumulatedGas))
240-
241-
require.Equal(t, "hash-alice-1", string(sorted[0].TxHash))
242-
require.Equal(t, "hash-bob-42b", string(sorted[1].TxHash))
243-
})
244224
}
245225

246226
func TestTxCache_SelectTransactions_WhenTransactionsAddedInReversedNonceOrder(t *testing.T) {

txcache/transactionsHeapItem.go

+2-19
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package txcache
22

33
import (
4-
"bytes"
54
"math/big"
65

76
"github.com/multiversx/mx-chain-storage-go/types"
@@ -159,24 +158,8 @@ func (item *transactionsHeapItem) detectLowerNonce() bool {
159158
}
160159

161160
func (item *transactionsHeapItem) detectBadlyGuarded() bool {
162-
if item.senderState == nil {
163-
return false
164-
}
165-
166-
transactionGuardian := item.currentTransaction.Guardian
167-
accountGuardian := item.senderState.Guardian
168-
169-
isBadlyGuarded := !bytes.Equal(transactionGuardian, accountGuardian)
170-
if isBadlyGuarded {
171-
logSelect.Trace("transactionsHeapItem.detectBadlyGuarded",
172-
"tx", item.currentTransaction.TxHash,
173-
"sender", item.sender,
174-
"transactionGuardian", transactionGuardian,
175-
"accountGuardian", accountGuardian,
176-
)
177-
}
178-
179-
return isBadlyGuarded
161+
// See MX-16179.
162+
return false
180163
}
181164

182165
func (item *transactionsHeapItem) detectNonceDuplicate() bool {

txcache/transactionsHeapItem_test.go

-72
Original file line numberDiff line numberDiff line change
@@ -199,78 +199,6 @@ func TestTransactionsHeapItem_detectLowerNonce(t *testing.T) {
199199
})
200200
}
201201

202-
func TestTransactionsHeapItem_detectBadlyGuarded(t *testing.T) {
203-
txGasHandler := txcachemocks.NewTxGasHandlerMock()
204-
205-
a := createTx([]byte("tx-1"), "alice", 42)
206-
b := createTx([]byte("tx-7"), "bob", 43).withGuardian([]byte("heidi"))
207-
208-
a.precomputeFields(txGasHandler)
209-
b.precomputeFields(txGasHandler)
210-
211-
t.Run("unknown", func(t *testing.T) {
212-
item, err := newTransactionsHeapItem(bunchOfTransactions{a})
213-
require.NoError(t, err)
214-
215-
require.False(t, item.detectBadlyGuarded())
216-
})
217-
218-
t.Run("transaction has no guardian, account has no guardian", func(t *testing.T) {
219-
item, err := newTransactionsHeapItem(bunchOfTransactions{a})
220-
require.NoError(t, err)
221-
222-
item.senderState = &types.AccountState{
223-
Guardian: nil,
224-
}
225-
226-
require.False(t, item.detectBadlyGuarded())
227-
})
228-
229-
t.Run("transaction has guardian, account has guardian, they match", func(t *testing.T) {
230-
item, err := newTransactionsHeapItem(bunchOfTransactions{b})
231-
require.NoError(t, err)
232-
233-
item.senderState = &types.AccountState{
234-
Guardian: []byte("heidi"),
235-
}
236-
237-
require.False(t, item.detectBadlyGuarded())
238-
})
239-
240-
t.Run("transaction has guardian, account has guardian, they don't match", func(t *testing.T) {
241-
item, err := newTransactionsHeapItem(bunchOfTransactions{b})
242-
require.NoError(t, err)
243-
244-
item.senderState = &types.AccountState{
245-
Guardian: []byte("grace"),
246-
}
247-
248-
require.True(t, item.detectBadlyGuarded())
249-
})
250-
251-
t.Run("transaction has guardian, account does not", func(t *testing.T) {
252-
item, err := newTransactionsHeapItem(bunchOfTransactions{b})
253-
require.NoError(t, err)
254-
255-
item.senderState = &types.AccountState{
256-
Guardian: nil,
257-
}
258-
259-
require.True(t, item.detectBadlyGuarded())
260-
})
261-
262-
t.Run("transaction has no guardian, account has guardian", func(t *testing.T) {
263-
item, err := newTransactionsHeapItem(bunchOfTransactions{a})
264-
require.NoError(t, err)
265-
266-
item.senderState = &types.AccountState{
267-
Guardian: []byte("heidi"),
268-
}
269-
270-
require.True(t, item.detectBadlyGuarded())
271-
})
272-
}
273-
274202
func TestTransactionsHeapItem_detectNonceDuplicate(t *testing.T) {
275203
a := createTx([]byte("tx-1"), "alice", 42)
276204
b := createTx([]byte("tx-2"), "alice", 43)

txcache/wrappedTransaction.go

-6
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ type WrappedTransaction struct {
2323
// Additional note: "WrappedTransaction" objects are created by the Node, in dataRetriever/txpool/shardedTxPool.go.
2424
Fee *big.Int
2525
PricePerUnit uint64
26-
Guardian []byte
2726
}
2827

2928
// precomputeFields computes (and caches) the (average) price per gas unit.
@@ -34,11 +33,6 @@ func (wrappedTx *WrappedTransaction) precomputeFields(txGasHandler TxGasHandler)
3433
if gasLimit != 0 {
3534
wrappedTx.PricePerUnit = wrappedTx.Fee.Uint64() / gasLimit
3635
}
37-
38-
txAsGuardedTransaction, ok := wrappedTx.Tx.(data.GuardedTransactionHandler)
39-
if ok {
40-
wrappedTx.Guardian = txAsGuardedTransaction.GetGuardianAddr()
41-
}
4236
}
4337

4438
// Equality is out of scope (not possible in our case).

txcache/wrappedTransaction_test.go

+2-6
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,24 @@ func TestWrappedTransaction_precomputeFields(t *testing.T) {
1616

1717
require.Equal(t, "51500000000000", tx.Fee.String())
1818
require.Equal(t, oneBillion, int(tx.PricePerUnit))
19-
require.Empty(t, tx.Guardian)
2019
})
2120

22-
t.Run("move balance gas limit and execution gas limit (1)", func(t *testing.T) {
21+
t.Run("move balance gas limit and execution gas limit (a)", func(t *testing.T) {
2322
tx := createTx([]byte("b"), "b", 1).withDataLength(1).withGasLimit(51501).withGasPrice(oneBillion)
2423
tx.precomputeFields(txGasHandler)
2524

2625
require.Equal(t, "51500010000000", tx.Fee.String())
2726
require.Equal(t, 999_980_777, int(tx.PricePerUnit))
28-
require.Empty(t, tx.Guardian)
2927
})
3028

31-
t.Run("move balance gas limit and execution gas limit (2)", func(t *testing.T) {
29+
t.Run("move balance gas limit and execution gas limit (b)", func(t *testing.T) {
3230
tx := createTx([]byte("c"), "c", 1).withDataLength(1).withGasLimit(oneMilion).withGasPrice(oneBillion)
3331
tx.precomputeFields(txGasHandler)
3432

3533
actualFee := 51500*oneBillion + (oneMilion-51500)*oneBillion/100
3634
require.Equal(t, "60985000000000", tx.Fee.String())
3735
require.Equal(t, 60_985_000_000_000, actualFee)
3836
require.Equal(t, actualFee/oneMilion, int(tx.PricePerUnit))
39-
require.Empty(t, tx.Guardian)
4037
})
4138

4239
t.Run("with guardian", func(t *testing.T) {
@@ -45,7 +42,6 @@ func TestWrappedTransaction_precomputeFields(t *testing.T) {
4542

4643
require.Equal(t, "50000000000000", tx.Fee.String())
4744
require.Equal(t, oneBillion, int(tx.PricePerUnit))
48-
require.Equal(t, []byte("heidi"), tx.Guardian)
4945
})
5046
}
5147

types/accountState.go

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import "math/big"
44

55
// AccountState represents the state of an account, as seen by the mempool
66
type AccountState struct {
7-
Nonce uint64
8-
Balance *big.Int
9-
Guardian []byte
7+
Nonce uint64
8+
Balance *big.Int
109
}

0 commit comments

Comments
 (0)