Skip to content

Commit 22a1108

Browse files
author
colinlyguo
committed
fixes
1 parent 637578a commit 22a1108

File tree

2 files changed

+64
-4
lines changed

2 files changed

+64
-4
lines changed

rollup/internal/orm/orm_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,3 +597,61 @@ func TestPendingTransactionOrm(t *testing.T) {
597597
err = pendingTransactionOrm.DeleteTransactionByTxHash(context.Background(), common.HexToHash("0x123"))
598598
assert.Error(t, err) // Should return error for non-existent transaction
599599
}
600+
601+
func TestPendingTransaction_GetMaxNonceBySenderAddress(t *testing.T) {
602+
sqlDB, err := db.DB()
603+
assert.NoError(t, err)
604+
assert.NoError(t, migrate.ResetDB(sqlDB))
605+
606+
// When there are no transactions for this sender address, should return 0
607+
maxNonce, err := pendingTransactionOrm.GetMaxNonceBySenderAddress(context.Background(), "0xdeadbeef")
608+
assert.NoError(t, err)
609+
assert.Equal(t, uint64(0), maxNonce)
610+
611+
// Insert two transactions with different nonces for the same sender address
612+
senderMeta := &SenderMeta{
613+
Name: "testName",
614+
Service: "testService",
615+
Address: common.HexToAddress("0xdeadbeef"),
616+
Type: types.SenderTypeCommitBatch,
617+
}
618+
619+
tx0 := gethTypes.NewTx(&gethTypes.DynamicFeeTx{
620+
Nonce: 1,
621+
To: &common.Address{},
622+
Data: []byte{},
623+
Gas: 21000,
624+
AccessList: gethTypes.AccessList{},
625+
Value: big.NewInt(0),
626+
ChainID: big.NewInt(1),
627+
GasTipCap: big.NewInt(0),
628+
GasFeeCap: big.NewInt(1),
629+
V: big.NewInt(0),
630+
R: big.NewInt(0),
631+
S: big.NewInt(0),
632+
})
633+
tx1 := gethTypes.NewTx(&gethTypes.DynamicFeeTx{
634+
Nonce: 3,
635+
To: &common.Address{},
636+
Data: []byte{},
637+
Gas: 22000,
638+
AccessList: gethTypes.AccessList{},
639+
Value: big.NewInt(0),
640+
ChainID: big.NewInt(1),
641+
GasTipCap: big.NewInt(1),
642+
GasFeeCap: big.NewInt(2),
643+
V: big.NewInt(0),
644+
R: big.NewInt(0),
645+
S: big.NewInt(0),
646+
})
647+
648+
err = pendingTransactionOrm.InsertPendingTransaction(context.Background(), "test", senderMeta, tx0, 0)
649+
assert.NoError(t, err)
650+
err = pendingTransactionOrm.InsertPendingTransaction(context.Background(), "test", senderMeta, tx1, 0)
651+
assert.NoError(t, err)
652+
653+
// Now the max nonce for this sender should be 3
654+
maxNonce, err = pendingTransactionOrm.GetMaxNonceBySenderAddress(context.Background(), senderMeta.Address.String())
655+
assert.NoError(t, err)
656+
assert.Equal(t, uint64(3), maxNonce)
657+
}

rollup/internal/orm/pending_transaction.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,16 @@ func (o *PendingTransaction) UpdateOtherTransactionsAsFailedByNonce(ctx context.
211211
// GetMaxNonceBySenderAddress retrieves the maximum nonce for a specific sender address.
212212
// Returns 0 if no transactions are found for the given address.
213213
func (o *PendingTransaction) GetMaxNonceBySenderAddress(ctx context.Context, senderAddress string) (uint64, error) {
214-
var maxNonce uint64
215214
db := o.db.WithContext(ctx)
216215
db = db.Model(&PendingTransaction{})
217216
db = db.Where("sender_address = ?", senderAddress)
218-
219-
if err := db.Pluck("COALESCE(MAX(nonce), 0)", &maxNonce).Error; err != nil {
217+
var maxNonce uint64
218+
row := db.Model(&PendingTransaction{}).
219+
Select("COALESCE(MAX(nonce), 0)").
220+
Where("sender_address = ?", senderAddress).
221+
Row()
222+
if err := row.Scan(&maxNonce); err != nil {
220223
return 0, fmt.Errorf("failed to get max nonce by sender address, address: %s, err: %w", senderAddress, err)
221224
}
222-
223225
return maxNonce, nil
224226
}

0 commit comments

Comments
 (0)