mirror of
https://github.com/scroll-tech/scroll.git
synced 2026-04-23 03:00:50 -04:00
record tx
This commit is contained in:
29
database/migrate/migrations/00007_transaction.sql
Normal file
29
database/migrate/migrations/00007_transaction.sql
Normal file
@@ -0,0 +1,29 @@
|
||||
-- +goose Up
|
||||
-- +goose StatementBegin
|
||||
|
||||
create table transaction
|
||||
(
|
||||
hash VARCHAR NOT NULL,
|
||||
tx_hash VARCHAR NOT NULL,
|
||||
sender VARCHAR NOT NULL,
|
||||
nonce BIGINT NOT NULL,
|
||||
target VARCHAR NOT NULL,
|
||||
gas BIGINT NOT NULL,
|
||||
gas_limit BIGINT NOT NULL,
|
||||
value VARCHAR NOT NULL,
|
||||
data TEXT DEFAULT NULL,
|
||||
created_time TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
);
|
||||
|
||||
create unique index transaction_hash_uindex
|
||||
on transaction (hash);
|
||||
|
||||
create unique index transaction_tx_hash_uindex
|
||||
on transaction (tx_hash);
|
||||
|
||||
-- +goose StatementEnd
|
||||
|
||||
-- +goose Down
|
||||
-- +goose StatementBegin
|
||||
drop table if exists transaction;
|
||||
-- +goose StatementEnd
|
||||
@@ -4,10 +4,11 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"scroll-tech/common/types"
|
||||
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/scroll-tech/go-ethereum/common"
|
||||
etypes "github.com/scroll-tech/go-ethereum/core/types"
|
||||
|
||||
"scroll-tech/common/types"
|
||||
)
|
||||
|
||||
// L1BlockOrm l1_block operation interface
|
||||
@@ -110,3 +111,9 @@ type L2MessageOrm interface {
|
||||
|
||||
GetRelayL2MessageTxHash(nonce uint64) (sql.NullString, error) // for unit tests only
|
||||
}
|
||||
|
||||
// TxOrm transaction operation interfaces.
|
||||
type TxOrm interface {
|
||||
SaveTx(hash, sender string, tx *etypes.Transaction) error
|
||||
GetTxByHash(hash string) (*types.TxMessage, error)
|
||||
}
|
||||
|
||||
51
database/orm/transaction.go
Normal file
51
database/orm/transaction.go
Normal file
@@ -0,0 +1,51 @@
|
||||
package orm
|
||||
|
||||
import (
|
||||
"github.com/jmoiron/sqlx"
|
||||
"github.com/scroll-tech/go-ethereum/common/hexutil"
|
||||
"github.com/scroll-tech/go-ethereum/core/types"
|
||||
|
||||
stypes "scroll-tech/common/types"
|
||||
)
|
||||
|
||||
type txOrm struct {
|
||||
db *sqlx.DB
|
||||
}
|
||||
|
||||
var _ TxOrm = (*txOrm)(nil)
|
||||
|
||||
// NewTxOrm create an TxOrm instance.
|
||||
func NewTxOrm(db *sqlx.DB) TxOrm {
|
||||
return &txOrm{db: db}
|
||||
}
|
||||
|
||||
// SaveTx stores tx message into db.
|
||||
func (t *txOrm) SaveTx(hash, sender string, tx *types.Transaction) error {
|
||||
if tx == nil {
|
||||
return nil
|
||||
}
|
||||
var target string
|
||||
if tx.To() != nil {
|
||||
target = tx.To().String()
|
||||
}
|
||||
_, err := t.db.Exec(
|
||||
t.db.Rebind(t.db.Rebind("INSERT INTO transaction (hash, tx_hash, sender, nonce, target, gas, gas_limit, value, data) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);")),
|
||||
hash,
|
||||
tx.Hash().String(),
|
||||
sender,
|
||||
tx.Nonce(),
|
||||
target,
|
||||
tx.GasPrice().Uint64(),
|
||||
tx.Gas(),
|
||||
hexutil.Encode(tx.Data()),
|
||||
)
|
||||
return err
|
||||
}
|
||||
|
||||
// GetTxByHash returns tx message by message hash.
|
||||
func (t *txOrm) GetTxByHash(hash string) (*stypes.TxMessage, error) {
|
||||
db := t.db
|
||||
row := db.QueryRowx(db.Rebind("SELECT * FROM transaction WHERE hash = ?"), hash)
|
||||
txMsg := &stypes.TxMessage{}
|
||||
return txMsg, row.Scan(&txMsg)
|
||||
}
|
||||
@@ -15,6 +15,7 @@ type OrmFactory interface {
|
||||
orm.L1MessageOrm
|
||||
orm.L2MessageOrm
|
||||
orm.SessionInfoOrm
|
||||
orm.TxOrm
|
||||
GetDB() *sqlx.DB
|
||||
Beginx() (*sqlx.Tx, error)
|
||||
Close() error
|
||||
@@ -27,6 +28,7 @@ type ormFactory struct {
|
||||
orm.L1MessageOrm
|
||||
orm.L2MessageOrm
|
||||
orm.SessionInfoOrm
|
||||
orm.TxOrm
|
||||
*sqlx.DB
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user