Skip to content

Commit

Permalink
add orm test
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Jan 16, 2024
1 parent ba3bdc0 commit 89fb9a8
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 6 deletions.
70 changes: 70 additions & 0 deletions rollup/internal/orm/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ package orm
import (
"context"
"encoding/json"
"math/big"
"os"
"testing"

"github.com/scroll-tech/go-ethereum/common"
gethTypes "github.com/scroll-tech/go-ethereum/core/types"
"github.com/stretchr/testify/assert"
"gorm.io/gorm"

Expand Down Expand Up @@ -311,3 +313,71 @@ func TestBatchOrm(t *testing.T) {
assert.Equal(t, "finalizeTxHash", updatedBatch.FinalizeTxHash)
assert.Equal(t, types.RollupFinalizeFailed, types.RollupStatus(updatedBatch.RollupStatus))
}

func TestTransactionOrm(t *testing.T) {
sqlDB, err := db.DB()
assert.NoError(t, err)
assert.NoError(t, migrate.ResetDB(sqlDB))

tx1 := createTestTransaction(1, big.NewInt(0), big.NewInt(0))
tx2 := createTestTransaction(1, big.NewInt(1), big.NewInt(1))
senderMeta := createTestSenderMeta("0xSomeAddress", "TestSender", "TestService", types.SenderTypeUnknown)

err = pendingTransactionOrm.InsertPendingTransaction(context.Background(), "TestContextID", senderMeta, tx1, 0)
assert.NoError(t, err)

err = pendingTransactionOrm.InsertPendingTransaction(context.Background(), "TestContextID", senderMeta, tx2, 0)
assert.NoError(t, err)

err = pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(context.Background(), tx1.Hash().String(), types.TxStatusReplaced)
assert.NoError(t, err)

txs, err := pendingTransactionOrm.GetPendingOrReplacedTransactionsBySenderType(context.Background(), senderMeta.Type, 2)
assert.NoError(t, err)
assert.Len(t, txs, 2)

err = pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(context.Background(), tx2.Hash().String(), types.TxStatusConfirmed)
assert.NoError(t, err)

txs, err = pendingTransactionOrm.GetPendingOrReplacedTransactionsBySenderType(context.Background(), senderMeta.Type, 2)
assert.NoError(t, err)
assert.Len(t, txs, 1)

err = pendingTransactionOrm.UpdateOtherTransactionsAsFailedByNonce(context.Background(), senderMeta.Address.String(), tx2.Nonce(), tx2.Hash().String())
assert.NoError(t, err)

txs, err = pendingTransactionOrm.GetPendingOrReplacedTransactionsBySenderType(context.Background(), senderMeta.Type, 2)
assert.NoError(t, err)
assert.Len(t, txs, 0)

status, err := pendingTransactionOrm.GetTxStatusByTxHash(context.Background(), tx1.Hash().String())
assert.NoError(t, err)
assert.Equal(t, types.TxStatusFailed, status)
}

func createTestTransaction(nonce uint64, gasTipCap, gasFeeCap *big.Int) *gethTypes.Transaction {
txData := &gethTypes.DynamicFeeTx{
Nonce: nonce,
To: &common.Address{},
Data: []byte{},
Gas: 21000,
AccessList: gethTypes.AccessList{},
Value: big.NewInt(0),
ChainID: big.NewInt(1),
GasTipCap: gasTipCap,
GasFeeCap: gasFeeCap,
V: big.NewInt(0),
R: big.NewInt(0),
S: big.NewInt(0),
}
return gethTypes.NewTx(txData)
}

func createTestSenderMeta(address, name, service string, senderType types.SenderType) *SenderMeta {
return &SenderMeta{
Name: name,
Service: service,
Address: common.HexToAddress(address),
Type: senderType,
}
}
13 changes: 7 additions & 6 deletions rollup/internal/orm/pending_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (o *PendingTransaction) GetTxStatusByTxHash(ctx context.Context, hash strin
db = db.Model(&PendingTransaction{})
db = db.Select("status")
db = db.Where("hash = ?", hash)
if err := db.First(&status).Error; err != nil {
if err := db.Scan(&status).Error; err != nil {
return types.TxStatusUnknown, fmt.Errorf("failed to get tx status by hash, hash: %v, err: %w", hash, err)
}
return status, nil
Expand All @@ -73,6 +73,7 @@ func (o *PendingTransaction) GetTxStatusByTxHash(ctx context.Context, hash strin
func (o *PendingTransaction) GetPendingOrReplacedTransactionsBySenderType(ctx context.Context, senderType types.SenderType, limit int) ([]PendingTransaction, error) {
var transactions []PendingTransaction
db := o.db.WithContext(ctx)
db = db.Model(&PendingTransaction{})
db = db.Where("sender_type = ?", senderType)
db = db.Where("status = ? OR status = ?", types.TxStatusPending, types.TxStatusReplaced)
db = db.Order("nonce asc")
Expand Down Expand Up @@ -113,23 +114,24 @@ func (o *PendingTransaction) InsertPendingTransaction(ctx context.Context, conte
db = dbTX[0]
}
db = db.WithContext(ctx)
db = db.Model(&PendingTransaction{})
if err := db.Create(newTransaction).Error; err != nil {
return fmt.Errorf("failed to InsertTransaction, error: %w", err)
}
return nil
}

// UpdatePendingTransactionStatusByTxHash updates the status of a transaction based on the transaction hash.
func (o *PendingTransaction) UpdatePendingTransactionStatusByTxHash(ctx context.Context, txHash string, status types.TxStatus, dbTX ...*gorm.DB) error {
func (o *PendingTransaction) UpdatePendingTransactionStatusByTxHash(ctx context.Context, hash string, status types.TxStatus, dbTX ...*gorm.DB) error {
db := o.db
if len(dbTX) > 0 && dbTX[0] != nil {
db = dbTX[0]
}

db = db.WithContext(ctx)
db = db.Model(&PendingTransaction{})
db = db.Where("hash = ?", txHash)
db = db.Where("hash = ?", hash)
if err := db.Update("status", status).Error; err != nil {
return fmt.Errorf("failed to UpdatePendingTransactionStatusByTxHash, txHash: %s, error: %w", txHash, err)
return fmt.Errorf("failed to UpdatePendingTransactionStatusByTxHash, txHash: %s, error: %w", hash, err)
}
return nil
}
Expand All @@ -140,7 +142,6 @@ func (o *PendingTransaction) UpdateOtherTransactionsAsFailedByNonce(ctx context.
if len(dbTX) > 0 && dbTX[0] != nil {
db = dbTX[0]
}

db = db.WithContext(ctx)
db = db.Model(&PendingTransaction{})
db = db.Where("sender_address = ?", senderAddress)
Expand Down

0 comments on commit 89fb9a8

Please sign in to comment.