Skip to content

Commit

Permalink
refine orm
Browse files Browse the repository at this point in the history
  • Loading branch information
colinlyguo committed Jan 24, 2024
1 parent dc757ff commit c874816
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 30 deletions.
15 changes: 7 additions & 8 deletions database/migrate/migrations/00015_pending_transaction.sql
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,22 @@ CREATE TABLE pending_transaction
(
id SERIAL PRIMARY KEY,

-- context info
context_id VARCHAR NOT NULL, -- batch hash in commit/finalize tx, block hash in update gas oracle tx
hash VARCHAR NOT NULL,
type SMALLINT NOT NULL,
status SMALLINT NOT NULL,
rlp_encoding BYTEA NOT NULL,

-- debugging info
chain_id BIGINT NOT NULL,
type SMALLINT NOT NULL,
gas_tip_cap BIGINT NOT NULL,
gas_fee_cap BIGINT NOT NULL,
gas_fee_cap BIGINT NOT NULL, -- based on geth's implementation, it's gas price in legacy tx.
gas_limit BIGINT NOT NULL,
nonce BIGINT NOT NULL,
submit_block_number BIGINT NOT NULL,

-- sender info
sender_name VARCHAR NOT NULL,
sender_service VARCHAR NOT NULL,
sender_address VARCHAR NOT NULL,
Expand All @@ -28,15 +31,11 @@ CREATE TABLE pending_transaction
deleted_at TIMESTAMP(0) DEFAULT NULL
);

CREATE INDEX idx_pending_transaction_on_sender_type_status_nonce
ON pending_transaction (sender_type, status, nonce);

CREATE UNIQUE INDEX unique_idx_pending_transaction_on_hash ON pending_transaction(hash);

CREATE INDEX idx_pending_transaction_on_sender_type_status_nonce_gas_fee_cap ON pending_transaction (sender_type, status, nonce, gas_fee_cap);
CREATE INDEX idx_pending_transaction_on_sender_address_nonce ON pending_transaction(sender_address, nonce);

COMMENT ON COLUMN pending_transaction.type IS 'unknown, commit batch, finalize batch, L1 gas oracle, L2 gas oracle';

COMMENT ON COLUMN pending_transaction.sender_type IS 'unknown, commit batch, finalize batch, L1 gas oracle, L2 gas oracle';
COMMENT ON COLUMN pending_transaction.status IS 'unknown, pending, replaced, confirmed, confirmed failed';

-- +goose StatementEnd
Expand Down
42 changes: 21 additions & 21 deletions rollup/internal/orm/orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ func TestTransactionOrm(t *testing.T) {
assert.NoError(t, err)
assert.NoError(t, migrate.ResetDB(sqlDB))

tx1 := gethTypes.NewTx(&gethTypes.DynamicFeeTx{
tx0 := gethTypes.NewTx(&gethTypes.DynamicFeeTx{
Nonce: 0,
To: &common.Address{},
Data: []byte{},
Expand All @@ -333,7 +333,7 @@ func TestTransactionOrm(t *testing.T) {
R: big.NewInt(0),
S: big.NewInt(0),
})
tx2 := gethTypes.NewTx(&gethTypes.DynamicFeeTx{
tx1 := gethTypes.NewTx(&gethTypes.DynamicFeeTx{
Nonce: 0,
To: &common.Address{},
Data: []byte{},
Expand All @@ -348,50 +348,50 @@ func TestTransactionOrm(t *testing.T) {
S: big.NewInt(0),
})
senderMeta := &SenderMeta{
Name: "test",
Service: "test",
Name: "testName",
Service: "testService",
Address: common.HexToAddress("0x1"),
Type: types.SenderTypeUnknown,
Type: types.SenderTypeCommitBatch,
}

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

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

err = pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(context.Background(), tx1.Hash().String(), types.TxStatusReplaced)
err = pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(context.Background(), tx0.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)
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)
assert.Equal(t, tx1.Type(), txs[1].Type)
assert.Equal(t, tx1.Nonce(), txs[1].Nonce)
assert.Equal(t, tx1.Gas(), txs[1].GasLimit)
assert.Equal(t, tx1.GasTipCap().Uint64(), txs[1].GasTipCap)
assert.Equal(t, tx1.GasFeeCap().Uint64(), txs[1].GasFeeCap)
assert.Equal(t, tx1.ChainId().Uint64(), txs[1].ChainID)
assert.Equal(t, senderMeta.Name, txs[1].SenderName)
assert.Equal(t, senderMeta.Service, txs[1].SenderService)
assert.Equal(t, senderMeta.Address.String(), txs[1].SenderAddress)
assert.Equal(t, senderMeta.Type, txs[1].SenderType)

err = pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(context.Background(), tx2.Hash().String(), types.TxStatusConfirmed)
err = pendingTransactionOrm.UpdatePendingTransactionStatusByTxHash(context.Background(), tx1.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())
err = pendingTransactionOrm.UpdateOtherTransactionsAsFailedByNonce(context.Background(), senderMeta.Address.String(), tx1.Nonce(), tx1.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())
status, err := pendingTransactionOrm.GetTxStatusByTxHash(context.Background(), tx0.Hash().String())
assert.NoError(t, err)
assert.Equal(t, types.TxStatusConfirmedFailed, status)
}
2 changes: 1 addition & 1 deletion rollup/internal/orm/pending_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ type PendingTransaction struct {
ID uint `json:"id" gorm:"id;primaryKey"`
ContextID string `json:"context_id" gorm:"context_id"`
Hash string `json:"hash" gorm:"hash"`
Type uint8 `json:"type" gorm:"type"`
ChainID uint64 `json:"chain_id" gorm:"chain_id"`
Type uint8 `json:"type" gorm:"type"`
GasTipCap uint64 `json:"gas_tip_cap" gorm:"gas_tip_cap"`
GasFeeCap uint64 `json:"gas_fee_cap" gorm:"gas_fee_cap"`
GasLimit uint64 `json:"gas_limit" gorm:"gas_limit"`
Expand Down

0 comments on commit c874816

Please sign in to comment.