Skip to content

Commit

Permalink
Remove claim txs (0xPolygonHermez#2048)
Browse files Browse the repository at this point in the history
* remove claim txs

* fix tests

* fix tests

* fix tests

* remove deposit_count

* fix query

* fix query

---------

Co-authored-by: Thiago Lemos <[email protected]>
  • Loading branch information
ToniRamirezM and tclemos authored Apr 28, 2023
1 parent c7d86d9 commit 34b232e
Show file tree
Hide file tree
Showing 23 changed files with 95 additions and 457 deletions.
4 changes: 0 additions & 4 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -289,10 +289,6 @@ func Test_Defaults(t *testing.T) {
path: "StateDB.MaxConns",
expectedValue: 200,
},
{
path: "Pool.FreeClaimGasLimit",
expectedValue: uint64(150000),
},
{
path: "Pool.MaxTxBytesSize",
expectedValue: uint64(30132),
Expand Down
1 change: 0 additions & 1 deletion config/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ EnableLog = false
MaxConns = 200
[Pool]
FreeClaimGasLimit = 150000
IntervalToRefreshBlockedAddresses = "5m"
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
Expand Down
1 change: 0 additions & 1 deletion config/environments/local/local.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ EnableLog = false
MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
IntervalToRefreshBlockedAddresses = "5m"
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
Expand Down
1 change: 0 additions & 1 deletion config/environments/mainnet/public.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ EnableLog = false
MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
DefaultMinGasPriceAllowed = 1000000000
Expand Down
1 change: 0 additions & 1 deletion config/environments/public/public.node.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ EnableLog = false
MaxConns = 200

[Pool]
FreeClaimGasLimit = 1500000
IntervalToRefreshBlockedAddresses = "5m"
MaxTxBytesSize=30132
MaxTxDataBytesSize=30000
Expand Down
18 changes: 9 additions & 9 deletions jsonrpc/mocks/mock_pool.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion jsonrpc/types/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type PoolInterface interface {
GetGasPrice(ctx context.Context) (uint64, error)
GetNonce(ctx context.Context, address common.Address) (uint64, error)
GetPendingTxHashesSince(ctx context.Context, since time.Time) ([]common.Hash, error)
GetPendingTxs(ctx context.Context, isClaims bool, limit uint64) ([]pool.Transaction, error)
GetPendingTxs(ctx context.Context, limit uint64) ([]pool.Transaction, error)
CountPendingTransactions(ctx context.Context) (uint64, error)
GetTxByHash(ctx context.Context, hash common.Hash) (*pool.Transaction, error)
}
Expand Down
3 changes: 0 additions & 3 deletions pool/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ import (

// Config is the pool configuration
type Config struct {
// FreeClaimGasLimit is the max gas allowed use to do a free claim
FreeClaimGasLimit uint64 `mapstructure:"FreeClaimGasLimit"`

// IntervalToRefreshBlockedAddresses is the time it takes to sync the
// blocked address list from db to memory
IntervalToRefreshBlockedAddresses types.Duration `mapstructure:"IntervalToRefreshBlockedAddresses"`
Expand Down
7 changes: 3 additions & 4 deletions pool/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,21 @@ type storage interface {
GetNonce(ctx context.Context, address common.Address) (uint64, error)
GetPendingTxHashesSince(ctx context.Context, since time.Time) ([]common.Hash, error)
GetTxsByFromAndNonce(ctx context.Context, from common.Address, nonce uint64) ([]Transaction, error)
GetTxsByStatus(ctx context.Context, state TxStatus, isClaims bool, limit uint64) ([]Transaction, error)
GetNonWIPTxsByStatus(ctx context.Context, status TxStatus, isClaims bool, limit uint64) ([]Transaction, error)
GetTxsByStatus(ctx context.Context, state TxStatus, limit uint64) ([]Transaction, error)
GetNonWIPTxsByStatus(ctx context.Context, status TxStatus, limit uint64) ([]Transaction, error)
IsTxPending(ctx context.Context, hash common.Hash) (bool, error)
SetGasPrice(ctx context.Context, gasPrice uint64) error
UpdateTxsStatus(ctx context.Context, updateInfo []TxStatusUpdateInfo) error
UpdateTxStatus(ctx context.Context, updateInfo TxStatusUpdateInfo) error
UpdateTxWIPStatus(ctx context.Context, hash common.Hash, isWIP bool) error
GetTxs(ctx context.Context, filterStatus TxStatus, isClaims bool, minGasPrice, limit uint64) ([]*Transaction, error)
GetTxs(ctx context.Context, filterStatus TxStatus, minGasPrice, limit uint64) ([]*Transaction, error)
GetTxFromAddressFromByHash(ctx context.Context, hash common.Hash) (common.Address, uint64, error)
GetTxByHash(ctx context.Context, hash common.Hash) (*Transaction, error)
GetTxZkCountersByHash(ctx context.Context, hash common.Hash) (*state.ZKCounters, error)
DeleteTransactionByHash(ctx context.Context, hash common.Hash) error
MarkWIPTxsAsPending(ctx context.Context) error
GetAllAddressesBlocked(ctx context.Context) ([]common.Address, error)
MinGasPriceSince(ctx context.Context, timestamp time.Time) (uint64, error)
DepositCountExists(ctx context.Context, depositCount uint64) (bool, error)
}

type stateInterface interface {
Expand Down
89 changes: 31 additions & 58 deletions pool/pgpoolstorage/pgpoolstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ func (p *PostgresPoolStorage) AddTx(ctx context.Context, tx pool.Transaction) er
status,
gas_price,
nonce,
is_claims,
cumulative_gas_used,
used_keccak_hashes,
used_poseidon_hashes,
Expand All @@ -74,31 +73,28 @@ func (p *PostgresPoolStorage) AddTx(ctx context.Context, tx pool.Transaction) er
received_at,
from_address,
is_wip,
ip,
deposit_count
ip
)
VALUES
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)
($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18)
ON CONFLICT (hash) DO UPDATE SET
encoded = $2,
decoded = $3,
status = $4,
gas_price = $5,
nonce = $6,
is_claims = $7,
cumulative_gas_used = $8,
used_keccak_hashes = $9,
used_poseidon_hashes = $10,
used_poseidon_paddings = $11,
used_mem_aligns = $12,
used_arithmetics = $13,
used_binaries = $14,
used_steps = $15,
received_at = $16,
from_address = $17,
is_wip = $18,
ip = $19,
deposit_count = $20
cumulative_gas_used = $7,
used_keccak_hashes = $8,
used_poseidon_hashes = $9,
used_poseidon_paddings = $10,
used_mem_aligns = $11,
used_arithmetics = $12,
used_binaries = $13,
used_steps = $14,
received_at = $15,
from_address = $16,
is_wip = $17,
ip = $18
`

// Get FromAddress from the JSON data
Expand All @@ -115,7 +111,6 @@ func (p *PostgresPoolStorage) AddTx(ctx context.Context, tx pool.Transaction) er
tx.Status,
gasPrice,
nonce,
tx.IsClaims,
tx.CumulativeGasUsed,
tx.UsedKeccakHashes,
tx.UsedPoseidonHashes,
Expand All @@ -127,8 +122,7 @@ func (p *PostgresPoolStorage) AddTx(ctx context.Context, tx pool.Transaction) er
tx.ReceivedAt,
fromAddress,
tx.IsWIP,
tx.IP,
tx.DepositCount); err != nil {
tx.IP); err != nil {
return err
}
return nil
Expand All @@ -137,18 +131,18 @@ func (p *PostgresPoolStorage) AddTx(ctx context.Context, tx pool.Transaction) er
// GetTxsByStatus returns an array of transactions filtered by status
// limit parameter is used to limit amount txs from the db,
// if limit = 0, then there is no limit
func (p *PostgresPoolStorage) GetTxsByStatus(ctx context.Context, status pool.TxStatus, isClaims bool, limit uint64) ([]pool.Transaction, error) {
func (p *PostgresPoolStorage) GetTxsByStatus(ctx context.Context, status pool.TxStatus, limit uint64) ([]pool.Transaction, error) {
var (
rows pgx.Rows
err error
sql string
)
if limit == 0 {
sql = "SELECT encoded, status, received_at, is_wip, ip, deposit_count, failed_reason FROM pool.transaction WHERE status = $1 ORDER BY gas_price DESC"
sql = "SELECT encoded, status, received_at, is_wip, ip, failed_reason FROM pool.transaction WHERE status = $1 ORDER BY gas_price DESC"
rows, err = p.db.Query(ctx, sql, status.String())
} else {
sql = "SELECT encoded, status, received_at, is_wip, ip, deposit_count, failed_reason FROM pool.transaction WHERE status = $1 AND is_claims = $2 ORDER BY gas_price DESC LIMIT $3"
rows, err = p.db.Query(ctx, sql, status.String(), isClaims, limit)
sql = "SELECT encoded, status, received_at, is_wip, ip, failed_reason FROM pool.transaction WHERE status = $1 ORDER BY gas_price DESC LIMIT $2"
rows, err = p.db.Query(ctx, sql, status.String(), limit)
}
if err != nil {
return nil, err
Expand All @@ -170,18 +164,18 @@ func (p *PostgresPoolStorage) GetTxsByStatus(ctx context.Context, status pool.Tx
// GetNonWIPTxsByStatus returns an array of transactions filtered by status
// limit parameter is used to limit amount txs from the db,
// if limit = 0, then there is no limit
func (p *PostgresPoolStorage) GetNonWIPTxsByStatus(ctx context.Context, status pool.TxStatus, isClaims bool, limit uint64) ([]pool.Transaction, error) {
func (p *PostgresPoolStorage) GetNonWIPTxsByStatus(ctx context.Context, status pool.TxStatus, limit uint64) ([]pool.Transaction, error) {
var (
rows pgx.Rows
err error
sql string
)
if limit == 0 {
sql = "SELECT encoded, status, received_at, is_wip, ip, deposit_count, failed_reason FROM pool.transaction WHERE is_wip IS FALSE and status = $1 ORDER BY gas_price DESC"
sql = "SELECT encoded, status, received_at, is_wip, ip, failed_reason FROM pool.transaction WHERE is_wip IS FALSE and status = $1 ORDER BY gas_price DESC"
rows, err = p.db.Query(ctx, sql, status.String())
} else {
sql = "SELECT encoded, status, received_at, is_wip, ip, deposit_count, failed_reason FROM pool.transaction WHERE is_wip IS FALSE and status = $1 AND is_claims = $2 ORDER BY gas_price DESC LIMIT $3"
rows, err = p.db.Query(ctx, sql, status.String(), isClaims, limit)
sql = "SELECT encoded, status, received_at, is_wip, ip, failed_reason FROM pool.transaction WHERE is_wip IS FALSE and status = $1 ORDER BY gas_price DESC LIMIT $2"
rows, err = p.db.Query(ctx, sql, status.String(), limit)
}
if err != nil {
return nil, err
Expand Down Expand Up @@ -222,7 +216,7 @@ func (p *PostgresPoolStorage) GetPendingTxHashesSince(ctx context.Context, since
}

// GetTxs gets txs with the lowest nonce
func (p *PostgresPoolStorage) GetTxs(ctx context.Context, filterStatus pool.TxStatus, isClaims bool, minGasPrice, limit uint64) ([]*pool.Transaction, error) {
func (p *PostgresPoolStorage) GetTxs(ctx context.Context, filterStatus pool.TxStatus, minGasPrice, limit uint64) ([]*pool.Transaction, error) {
query := `
SELECT
encoded,
Expand All @@ -243,11 +237,10 @@ func (p *PostgresPoolStorage) GetTxs(ctx context.Context, filterStatus pool.TxSt
pool.transaction p1
WHERE
status = $1 AND
gas_price >= $2 AND
is_claims = $3
gas_price >= $2
ORDER BY
nonce ASC
LIMIT $4
LIMIT $3
`

if filterStatus == pool.TxStatusFailed {
Expand All @@ -272,11 +265,10 @@ func (p *PostgresPoolStorage) GetTxs(ctx context.Context, filterStatus pool.TxSt
pool.transaction p1
WHERE
status = $1 AND
gas_price >= $2 AND
is_claims = $3
gas_price >= $2
ORDER BY
nonce ASC
LIMIT $4
LIMIT $3
) as tmp
ORDER BY nonce ASC
`
Expand All @@ -293,7 +285,7 @@ func (p *PostgresPoolStorage) GetTxs(ctx context.Context, filterStatus pool.TxSt
isWIP bool
)

args := []interface{}{filterStatus, minGasPrice, isClaims, limit}
args := []interface{}{filterStatus, minGasPrice, limit}

rows, err := p.db.Query(ctx, query, args...)
if errors.Is(err, pgx.ErrNoRows) {
Expand Down Expand Up @@ -475,7 +467,7 @@ func (p *PostgresPoolStorage) IsTxPending(ctx context.Context, hash common.Hash)

// GetTxsByFromAndNonce get all the transactions from the pool with the same from and nonce
func (p *PostgresPoolStorage) GetTxsByFromAndNonce(ctx context.Context, from common.Address, nonce uint64) ([]pool.Transaction, error) {
sql := `SELECT encoded, status, received_at, is_wip, ip, deposit_count, failed_reason
sql := `SELECT encoded, status, received_at, is_wip, ip, failed_reason
FROM pool.transaction
WHERE from_address = $1
AND nonce = $2`
Expand Down Expand Up @@ -597,11 +589,10 @@ func scanTx(rows pgx.Rows) (*pool.Transaction, error) {
encoded, status, ip string
receivedAt time.Time
isWIP bool
depositCount *uint64
failedReason *string
)

if err := rows.Scan(&encoded, &status, &receivedAt, &isWIP, &ip, &depositCount, &failedReason); err != nil {
if err := rows.Scan(&encoded, &status, &receivedAt, &isWIP, &ip, &failedReason); err != nil {
return nil, err
}

Expand All @@ -620,7 +611,6 @@ func scanTx(rows pgx.Rows) (*pool.Transaction, error) {
tx.ReceivedAt = receivedAt
tx.IsWIP = isWIP
tx.IP = ip
tx.DepositCount = depositCount
tx.FailedReason = failedReason

return tx, nil
Expand Down Expand Up @@ -698,20 +688,3 @@ func (p *PostgresPoolStorage) GetAllAddressesBlocked(ctx context.Context) ([]com

return addrs, nil
}

// DepositCountExists checks if already exists a `pending` or `selected` transaction
// in the pool with the provided deposit count
func (p *PostgresPoolStorage) DepositCountExists(ctx context.Context, depositCount uint64) (bool, error) {
var exists bool
req := `
SELECT EXISTS (SELECT 1
FROM pool.transaction
WHERE deposit_count = $1
AND status IN ($2, $3))`
err := p.db.QueryRow(ctx, req, depositCount, pool.TxStatusPending, pool.TxStatusSelected).Scan(&exists)
if err != nil && err != sql.ErrNoRows {
return false, err
}

return exists, nil
}
Loading

0 comments on commit 34b232e

Please sign in to comment.