merge feat/record_transaction branch

This commit is contained in:
maskpp
2023-04-06 15:55:08 +08:00
12 changed files with 185 additions and 66 deletions

View File

@@ -1,7 +1,7 @@
-- +goose Up
-- +goose StatementBegin
create table transaction
create table scroll_transaction
(
id VARCHAR NOT NULL,
tx_hash VARCHAR NOT NULL,
@@ -14,15 +14,15 @@ create table transaction
created_time TIMESTAMP(0) NOT NULL DEFAULT CURRENT_TIMESTAMP
);
create unique index transaction_id_uindex
on transaction (id);
create unique index scroll_transaction_id_uindex
on scroll_transaction (id);
create unique index transaction_tx_hash_uindex
on transaction (tx_hash);
create unique index scroll_transaction_tx_hash_uindex
on scroll_transaction (tx_hash);
-- +goose StatementEnd
-- +goose Down
-- +goose StatementBegin
drop table if exists transaction;
drop table if exists scroll_transaction;
-- +goose StatementEnd

View File

@@ -112,9 +112,9 @@ type L2MessageOrm interface {
GetRelayL2MessageTxHash(nonce uint64) (sql.NullString, error) // for unit tests only
}
// TxOrm transaction operation interfaces.
type TxOrm interface {
SaveTx(id, sender string, txType types.TxType, tx *etypes.Transaction) error
// ScrollTxOrm transaction operation interfaces.
type ScrollTxOrm interface {
SaveTx(id, sender string, txType types.ScrollTxType, tx *etypes.Transaction) error
UpdateTxMsgByID(hash string, txHash string) error
GetTxByID(id string) (*types.ScrollTx, error)
GetL1TxMessages(fields map[string]interface{}, args ...string) (uint64, []*types.ScrollTx, error)

View File

@@ -11,19 +11,19 @@ import (
stypes "scroll-tech/common/types"
)
type txOrm struct {
type scrollTxOrm struct {
db *sqlx.DB
}
var _ TxOrm = (*txOrm)(nil)
var _ ScrollTxOrm = (*scrollTxOrm)(nil)
// NewTxOrm create an TxOrm instance.
func NewTxOrm(db *sqlx.DB) TxOrm {
return &txOrm{db: db}
// NewScrollTxOrm create an ScrollTxOrm instance.
func NewScrollTxOrm(db *sqlx.DB) ScrollTxOrm {
return &scrollTxOrm{db: db}
}
// SaveTx stores tx message into db.
func (t *txOrm) SaveTx(id, sender string, txType stypes.TxType, tx *types.Transaction) error {
func (t *scrollTxOrm) SaveTx(id, sender string, txType stypes.ScrollTxType, tx *types.Transaction) error {
if tx == nil {
return nil
}
@@ -32,7 +32,7 @@ func (t *txOrm) SaveTx(id, sender string, txType stypes.TxType, tx *types.Transa
target = tx.To().String()
}
_, err := t.db.Exec(
t.db.Rebind("INSERT INTO transaction (id, tx_hash, sender, nonce, target, value, data, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"),
t.db.Rebind("INSERT INTO scroll_transaction (id, tx_hash, sender, nonce, target, value, data, type) VALUES (?, ?, ?, ?, ?, ?, ?, ?);"),
id,
tx.Hash().String(),
sender,
@@ -46,16 +46,16 @@ func (t *txOrm) SaveTx(id, sender string, txType stypes.TxType, tx *types.Transa
}
// UpdateTxMsgByID remove data content by id.
func (t *txOrm) UpdateTxMsgByID(id string, txHash string) error {
func (t *scrollTxOrm) UpdateTxMsgByID(id string, txHash string) error {
db := t.db
_, err := db.Exec(db.Rebind("UPDATE transaction SET data = '', tx_hash = ? WHERE id = ?;"), txHash, id)
_, err := db.Exec(db.Rebind("UPDATE scroll_transaction SET data = '', tx_hash = ? WHERE id = ?;"), txHash, id)
return err
}
// GetTxByID returns tx message by message id.
func (t *txOrm) GetTxByID(id string) (*stypes.ScrollTx, error) {
func (t *scrollTxOrm) GetTxByID(id string) (*stypes.ScrollTx, error) {
db := t.db
row := db.QueryRowx(db.Rebind("SELECT id, tx_hash, sender, nonce, target, value, data FROM transaction WHERE id = ?"), id)
row := db.QueryRowx(db.Rebind("SELECT id, tx_hash, sender, nonce, target, value, data FROM scroll_transaction WHERE id = ?"), id)
txMsg := &stypes.ScrollTx{}
if err := row.StructScan(txMsg); err != nil {
return nil, err
@@ -63,18 +63,17 @@ func (t *txOrm) GetTxByID(id string) (*stypes.ScrollTx, error) {
return txMsg, nil
}
// GetL1TxMessages gets tx messages by transaction right join l1_message.
// GetL1TxMessages gets tx messages by scroll_transaction right join l1_message.
// sql i.g:
// select l1.msg_hash as id, tx.tx_hash, tx.sender, tx.nonce, tx.target, tx.value, tx.data
// from transaction as tx
// from scroll_transaction as tx
// right join (select msg_hash
//
// from l1_message
// where 1 = 1 AND status = :status AND queue_index > 0
// ORDER BY queue_index ASC
// LIMIT 10) as l1 on tx.id = l1.msg_hash;
func (t *txOrm) GetL1TxMessages(fields map[string]interface{}, args ...string) (uint64, []*stypes.ScrollTx, error) {
func (t *scrollTxOrm) GetL1TxMessages(fields map[string]interface{}, args ...string) (uint64, []*stypes.ScrollTx, error) {
query := "select msg_hash, queue_index from l1_message where 1 = 1"
for key := range fields {
query = query + fmt.Sprintf(" AND %s = :%s", key, key)
@@ -107,7 +106,7 @@ func (t *txOrm) GetL1TxMessages(fields map[string]interface{}, args ...string) (
}
// GetL2TxMessages gets tx messages by transaction right join l2_message.
func (t *txOrm) GetL2TxMessages(fields map[string]interface{}, args ...string) (uint64, []*stypes.ScrollTx, error) {
func (t *scrollTxOrm) GetL2TxMessages(fields map[string]interface{}, args ...string) (uint64, []*stypes.ScrollTx, error) {
query := "select msg_hash from l2_message where 1 = 1"
for key := range fields {
query = query + fmt.Sprintf(" AND %s = :%s", key, key)
@@ -140,7 +139,7 @@ func (t *txOrm) GetL2TxMessages(fields map[string]interface{}, args ...string) (
}
// GetBlockBatchTxMessages gets tx messages by transaction right join block_batch.
func (t *txOrm) GetBlockBatchTxMessages(fields map[string]interface{}, args ...string) (uint64, []*stypes.ScrollTx, error) {
func (t *scrollTxOrm) GetBlockBatchTxMessages(fields map[string]interface{}, args ...string) (uint64, []*stypes.ScrollTx, error) {
query := "select hash, index from block_batch where 1 = 1"
for key := range fields {
query = query + fmt.Sprintf(" AND %s = :%s", key, key)

View File

@@ -15,7 +15,7 @@ type OrmFactory interface {
orm.L1MessageOrm
orm.L2MessageOrm
orm.SessionInfoOrm
orm.TxOrm
orm.ScrollTxOrm
GetDB() *sqlx.DB
Beginx() (*sqlx.Tx, error)
Close() error
@@ -28,7 +28,7 @@ type ormFactory struct {
orm.L1MessageOrm
orm.L2MessageOrm
orm.SessionInfoOrm
orm.TxOrm
orm.ScrollTxOrm
*sqlx.DB
}
@@ -53,7 +53,7 @@ func NewOrmFactory(cfg *DBConfig) (OrmFactory, error) {
L2MessageOrm: orm.NewL2MessageOrm(db),
L1BlockOrm: orm.NewL1BlockOrm(db),
SessionInfoOrm: orm.NewSessionInfoOrm(db),
TxOrm: orm.NewTxOrm(db),
ScrollTxOrm: orm.NewScrollTxOrm(db),
DB: db,
}, nil
}

View File

@@ -86,7 +86,7 @@ var (
ormLayer2 orm.L2MessageOrm
ormBatch orm.BlockBatchOrm
ormSession orm.SessionInfoOrm
ormTx orm.TxOrm
ormTx orm.ScrollTxOrm
auth *bind.TransactOpts
)
@@ -94,8 +94,8 @@ var (
func setupEnv(t *testing.T) error {
// Init db config and start db container.
dbConfig = &database.DBConfig{DriverName: "postgres"}
base.RunImages(t)
dbConfig.DSN = base.DBEndpoint()
//base.RunImages(t)
dbConfig.DSN = "postgres://maskpp:123456@localhost:5432/postgres?sslmode=disable" // base.DBEndpoint()
// Create db handler and reset db.
factory, err := database.NewOrmFactory(dbConfig)
@@ -109,7 +109,7 @@ func setupEnv(t *testing.T) error {
ormLayer2 = orm.NewL2MessageOrm(db)
ormBatch = orm.NewBlockBatchOrm(db)
ormSession = orm.NewSessionInfoOrm(db)
ormTx = orm.NewTxOrm(db)
ormTx = orm.NewScrollTxOrm(db)
templateBlockTrace, err := os.ReadFile("../common/testdata/blockTrace_02.json")
if err != nil {