Skip to content

Commit

Permalink
simplify db and enhance orm_test
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Jan 24, 2024
1 parent cc94470 commit dc757ff
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 38 deletions.
4 changes: 2 additions & 2 deletions database/migrate/migrations/00015_pending_transaction.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ CREATE TABLE pending_transaction
status SMALLINT NOT NULL,
rlp_encoding BYTEA NOT NULL,

gas_fee_cap BIGINT NOT NULL,
chain_id BIGINT NOT NULL,
gas_tip_cap BIGINT NOT NULL,
gas_price BIGINT NOT NULL,
gas_fee_cap BIGINT NOT NULL,
gas_limit BIGINT NOT NULL,
nonce BIGINT NOT NULL,
submit_block_number BIGINT NOT NULL,
Expand Down
78 changes: 46 additions & 32 deletions rollup/internal/orm/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,45 @@ func TestTransactionOrm(t *testing.T) {
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)
tx1 := gethTypes.NewTx(&gethTypes.DynamicFeeTx{
Nonce: 0,
To: &common.Address{},
Data: []byte{},
Gas: 21000,
AccessList: gethTypes.AccessList{},
Value: big.NewInt(0),
ChainID: big.NewInt(1),
GasTipCap: big.NewInt(0),
GasFeeCap: big.NewInt(1),
V: big.NewInt(0),
R: big.NewInt(0),
S: big.NewInt(0),
})
tx2 := gethTypes.NewTx(&gethTypes.DynamicFeeTx{
Nonce: 0,
To: &common.Address{},
Data: []byte{},
Gas: 42000,
AccessList: gethTypes.AccessList{},
Value: big.NewInt(0),
ChainID: big.NewInt(1),
GasTipCap: big.NewInt(1),
GasFeeCap: big.NewInt(2),
V: big.NewInt(0),
R: big.NewInt(0),
S: big.NewInt(0),
})
senderMeta := &SenderMeta{
Name: "test",
Service: "test",
Address: common.HexToAddress("0x1"),
Type: types.SenderTypeUnknown,
}

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

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

err = pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(context.Background(), tx1.Hash().String(), types.TxStatusReplaced)
Expand All @@ -335,6 +366,16 @@ func TestTransactionOrm(t *testing.T) {
txs, err := pendingTransactionOrm.GetPendingOrReplacedTransactionsBySenderType(context.Background(), senderMeta.Type, 2)
assert.NoError(t, err)
assert.Len(t, txs, 2)
assert.Equal(t, uint64(0), txs[0].Nonce)
assert.Equal(t, uint64(0), txs[1].Nonce)
assert.Equal(t, uint64(21000), txs[0].GasLimit)
assert.Equal(t, uint64(42000), txs[1].GasLimit)
assert.Equal(t, uint64(0), txs[0].GasTipCap)
assert.Equal(t, uint64(1), txs[1].GasTipCap)
assert.Equal(t, uint64(1), txs[0].GasFeeCap)
assert.Equal(t, uint64(2), txs[1].GasFeeCap)
assert.Equal(t, uint64(1), txs[0].ChainID)
assert.Equal(t, uint64(1), txs[1].ChainID)

err = pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(context.Background(), tx2.Hash().String(), types.TxStatusConfirmed)
assert.NoError(t, err)
Expand All @@ -354,30 +395,3 @@ func TestTransactionOrm(t *testing.T) {
assert.NoError(t, err)
assert.Equal(t, types.TxStatusConfirmedFailed, 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,
}
}
9 changes: 5 additions & 4 deletions rollup/internal/orm/pending_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type PendingTransaction struct {
ContextID string `json:"context_id" gorm:"context_id"`
Hash string `json:"hash" gorm:"hash"`
Type uint8 `json:"type" gorm:"type"`
GasFeeCap uint64 `json:"gas_fee_cap" gorm:"gas_fee_cap"`
ChainID uint64 `json:"chain_id" gorm:"chain_id"`
GasTipCap uint64 `json:"gas_tip_cap" gorm:"gas_tip_cap"`
GasPrice uint64 `json:"gas_price" gorm:"gas_price"`
GasFeeCap uint64 `json:"gas_fee_cap" gorm:"gas_fee_cap"`
GasLimit uint64 `json:"gas_limit" gorm:"gas_limit"`
Nonce uint64 `json:"nonce" gorm:"nonce"`
SubmitBlockNumber uint64 `json:"submit_block_number" gorm:"submit_block_number"`
Expand Down Expand Up @@ -69,14 +69,15 @@ func (o *PendingTransaction) GetTxStatusByTxHash(ctx context.Context, hash strin
return status, nil
}

// GetPendingOrReplacedTransactionsBySenderType retrieves pending or replaced transactions filtered by sender type, ordered by nonce, and limited to a specified count.
// GetPendingOrReplacedTransactionsBySenderType retrieves pending or replaced transactions filtered by sender type, ordered by nonce, then gas_fee_cap (gas_price in legacy tx), and limited to a specified count.
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")
db = db.Order("gas_fee_cap asc")
db = db.Limit(limit)
if err := db.Find(&transactions).Error; err != nil {
return nil, fmt.Errorf("failed to get pending or replaced transactions by sender type, error: %w", err)
Expand All @@ -95,9 +96,9 @@ func (o *PendingTransaction) InsertPendingTransaction(ctx context.Context, conte
ContextID: contextID,
Hash: tx.Hash().String(),
Type: tx.Type(),
ChainID: tx.ChainId().Uint64(),
GasFeeCap: tx.GasFeeCap().Uint64(),
GasTipCap: tx.GasTipCap().Uint64(),
GasPrice: tx.GasPrice().Uint64(),
GasLimit: tx.Gas(),
Nonce: tx.Nonce(),
SubmitBlockNumber: submitBlockNumber,
Expand Down

0 comments on commit dc757ff

Please sign in to comment.